orga-comp/parser.cpp

204 lines
4.0 KiB
C++
Raw Normal View History

2021-10-01 14:20:03 +00:00
#include "orga-comp.h"
struct token
parser::parse_tokens() {
// LR(k) parsing.
// Pushea al stack hasta encontrar una cantidad de token suficiente
// como para saber que hacer.
// El parse a su vez arma un abstract syntax tree.
if(pos < _tokens.size()) {
_stack.push(_tokens[pos]);
pos++;
//_stack.push(tok);
switch (_stack.top().t) {
case token::TOK_ID:
std::cout << "STATEMENT\n";
statement();
break;
case token::STM_COMPOUND:
break;
default:
err();
break;
}
return parse_tokens();
}
/*std::cout << "ESTADO FINAL DEL STACK:\n";
for(int i = 0; i < _stack.size(); i++) {
std::cout << str_token(pop()) << " ";
}*/
return _stack.top();
//check if reduce is possible, if it is reduce.
}
parser::parser(std::vector<struct token> tokens) {
_tokens = tokens;
pos = 0;
}
void
parser::reduce() {
if(_stack.size() == 1) return;
struct token tok = pop();
std::cout << str_token(tok) << " ";
switch (tok.t) {
case token::TOK_NUM:
std::cout << "reduzco numero\n";
reduce_num(tok);
// reduce();
break;
case token::EXP_ID:
case token::EXP_OPERATION:
case token::EXP_NUMBER:
// EL SIGUIENTE ES UN OP O UN ASSIGN
reduce_exp(tok);
break;
case token::TOK_ID:
reduce_id(tok);
break;
case token::TOK_SEMI:
// reduce();
break;
// case token::TOK_ID:
case token::STM_ASSIGN:
reduce_stm(tok);
default: break;
}
reduce();
}
void
parser::reduce_stm(struct token stm) {
struct token tok = pop();
struct token *tmp;
switch (tok.t) {
case token::STM_ASSIGN:
case token::STM_COMPOUND:
tmp = new struct token;
tmp->t = token::STM_COMPOUND;
tmp->rvalue = new struct token;
*tmp->rvalue = stm;
tmp->lvalue = new struct token;
*tmp->lvalue = tok;
_stack.push(*tmp);
std::cout << "CHAD STM REDUCER " << str_token(*tmp);
break;
default:
err();
break;
}
}
void
parser::reduce_id(struct token id) {
struct token *tmp = new struct token;
tmp->t = token::EXP_ID;
tmp->value = id.value;
_stack.push(*tmp);
}
void
parser::reduce_num(struct token num) {
struct token *tmp = new struct token;
tmp->t = token::EXP_NUMBER;
tmp->value = num.value;
_stack.push(*tmp);
}
void
parser::reduce_exp(struct token exp) {
std::cout << "reducing exp: " << str_token(exp) << "\n";
struct token modifier = pop();
struct token *tmp;
struct token *stm;
switch (modifier.t) {
case token::TOK_ASSIGN:
std::cout << "assign found\n";
if(_stack.top().t != token::TOK_ID) err();
std::cout << str_token(_stack.top()) << "\n";
stm = new struct token;
stm->t = token::STM_ASSIGN;
stm->rvalue = new struct token;
*stm->rvalue = exp;
stm->lvalue = new struct token;
*stm->lvalue = pop();
_stack.push(*stm);
break;
case token::TOK_OP:
if(_stack.top().t == token::TOK_NUM) {
reduce_num(pop());
}
if(_stack.top().t == token::TOK_ID) {
reduce_id(pop());
}
stm = new struct token;
stm->t = token::EXP_OPERATION;
stm->value = modifier.value;
stm->rvalue = new struct token;
*stm->rvalue = exp;
stm->lvalue = new struct token;
*stm->lvalue = pop();
_stack.push(*stm);
//reduce_exp(pop());
break;
default: err();
}
}
struct token
parser::pop() {
struct token tmp = _stack.top();
_stack.pop();
return tmp;
}
void
parser::statement() {
_stack.push(_tokens[pos]);
pos++;
assert(_stack.top().t == token::TOK_ASSIGN);
expression();
}
void
parser::expression() {
_stack.push(_tokens[pos]);
pos++;
std::cout << "POS: " << pos << '\n';
switch (_stack.top().t) {
// case token::EXP_NUMBER:
// case token::EXP_ID:
case token::TOK_ID:
case token::TOK_NUM:
expression();
break;
case token::EXP_OPERATION:
std::cout << "Nested operations not supported\n";
err();
break;
case token::TOK_OP:
expression();
break;
case token::TOK_SEMI:
std::cout << "REDUCE\n";
reduce();
break;
default:
err();
break;;
}
}
void
parser::err() {
std::cout
<< "unexpected symbol: "
<< str_token(_stack.top())
<< '\n';
exit(-1);
}