Changed token.t to token.tok_type, started work on scopes

This commit is contained in:
fsan 2021-10-04 23:28:08 -03:00
parent deaab50a6d
commit 82feecbcb2
5 changed files with 32 additions and 46 deletions

View File

@ -2,7 +2,7 @@
void void
interpreter::evalSTM(struct token stm) { interpreter::evalSTM(struct token stm) {
switch (stm.t) { switch (stm.tok_type) {
case token::STM_ASSIGN: case token::STM_ASSIGN:
table[stm.lvalue->value] = evalEXP(*stm.rvalue); table[stm.lvalue->value] = evalEXP(*stm.rvalue);
std::cout << stm.lvalue->value << " igual a " << table[stm.lvalue->value] << '\n'; std::cout << stm.lvalue->value << " igual a " << table[stm.lvalue->value] << '\n';
@ -20,7 +20,7 @@ interpreter::evalSTM(struct token stm) {
int int
interpreter::evalEXP(struct token exp) { interpreter::evalEXP(struct token exp) {
switch (exp.t) { switch (exp.tok_type) {
case token::EXP_NUMBER: case token::EXP_NUMBER:
return std::stoi(exp.value); return std::stoi(exp.value);
break; break;

View File

@ -81,7 +81,7 @@ lexer::lex_file(std::string filename) {
void void
lexer::add_token(token::type type, std::string value) { lexer::add_token(token::type type, std::string value) {
struct token *token = new struct token; struct token *token = new struct token;
token->t = type; token->tok_type = type;
token->value = value; token->value = value;
_tokens.push_back(*token); _tokens.push_back(*token);
} }

View File

@ -6,40 +6,14 @@ main(){
// lex -> parse -> analisis semantico // lex -> parse -> analisis semantico
// realizar un interprete // realizar un interprete
// interpretar a = 2 + 2 // interpretar a = 2 + 2
struct token test; interpreter inter;
test.t = test.STM_ASSIGN;
struct token rexp;
rexp.t = token::EXP_OPERATION;
struct token lnum;
lnum.t = test.EXP_NUMBER;
lnum.value = "2";
struct token rnum;
rnum.t = test.EXP_NUMBER;
rnum.value = "2";
struct token lexp;
lexp.t = token::EXP_ID;
lexp.value = "a";
rexp.lvalue = &lnum;
rexp.rvalue = &rnum;
test.rvalue = &rexp;
test.lvalue = &lexp;
interpreter *inter = new interpreter();
inter->evalSTM(test);
lexer lex; lexer lex;
parser parser(lex.lex_file("tst.cfran")); parser parser(lex.lex_file("tst.cfran"));
struct token end = parser.parse_tokens(); struct token end = parser.parse_tokens();
std::cout << "\n-----------------------\n"; std::cout << "\n-----------------------\n";
std::cout << "EVALUATING RESULTING TREE:\n"; std::cout << "EVALUATING RESULTING TREE:\n";
inter->evalSTM(end); inter.evalSTM(end);
return 1; return 1;
} }

View File

@ -28,7 +28,7 @@ struct token {
EXP_ID, EXP_ID,
EXP_OPERATION EXP_OPERATION
}; };
type t; type tok_type;
std::string value; std::string value;
struct token *lvalue; struct token *lvalue;
struct token *rvalue; struct token *rvalue;
@ -75,6 +75,18 @@ class parser {
size_t pos; size_t pos;
}; };
class scope {
public:
enum symbol_type{
VALUE,
};
private:
struct node {
std::string key;
symbol_type type;
};
};
class translate { class translate {
}; };

View File

@ -10,7 +10,7 @@ parser::parse_tokens() {
_stack.push(_tokens[pos]); _stack.push(_tokens[pos]);
pos++; pos++;
//_stack.push(tok); //_stack.push(tok);
switch (_stack.top().t) { switch (_stack.top().tok_type) {
case token::TOK_ID: case token::TOK_ID:
std::cout << "STATEMENT\n"; std::cout << "STATEMENT\n";
statement(); statement();
@ -43,7 +43,7 @@ parser::reduce() {
struct token tok = pop(); struct token tok = pop();
std::cout << str_token(tok) << " "; std::cout << str_token(tok) << " ";
switch (tok.t) { switch (tok.tok_type) {
case token::TOK_NUM: case token::TOK_NUM:
std::cout << "reduzco numero\n"; std::cout << "reduzco numero\n";
reduce_num(tok); reduce_num(tok);
@ -74,11 +74,11 @@ void
parser::reduce_stm(struct token stm) { parser::reduce_stm(struct token stm) {
struct token tok = pop(); struct token tok = pop();
struct token *tmp; struct token *tmp;
switch (tok.t) { switch (tok.tok_type) {
case token::STM_ASSIGN: case token::STM_ASSIGN:
case token::STM_COMPOUND: case token::STM_COMPOUND:
tmp = new struct token; tmp = new struct token;
tmp->t = token::STM_COMPOUND; tmp->tok_type = token::STM_COMPOUND;
tmp->rvalue = new struct token; tmp->rvalue = new struct token;
*tmp->rvalue = stm; *tmp->rvalue = stm;
tmp->lvalue = new struct token; tmp->lvalue = new struct token;
@ -95,7 +95,7 @@ parser::reduce_stm(struct token stm) {
void void
parser::reduce_id(struct token id) { parser::reduce_id(struct token id) {
struct token *tmp = new struct token; struct token *tmp = new struct token;
tmp->t = token::EXP_ID; tmp->tok_type = token::EXP_ID;
tmp->value = id.value; tmp->value = id.value;
_stack.push(*tmp); _stack.push(*tmp);
} }
@ -103,7 +103,7 @@ parser::reduce_id(struct token id) {
void void
parser::reduce_num(struct token num) { parser::reduce_num(struct token num) {
struct token *tmp = new struct token; struct token *tmp = new struct token;
tmp->t = token::EXP_NUMBER; tmp->tok_type = token::EXP_NUMBER;
tmp->value = num.value; tmp->value = num.value;
_stack.push(*tmp); _stack.push(*tmp);
} }
@ -114,13 +114,13 @@ parser::reduce_exp(struct token exp) {
struct token modifier = pop(); struct token modifier = pop();
struct token *tmp; struct token *tmp;
struct token *stm; struct token *stm;
switch (modifier.t) { switch (modifier.tok_type) {
case token::TOK_ASSIGN: case token::TOK_ASSIGN:
std::cout << "assign found\n"; std::cout << "assign found\n";
if(_stack.top().t != token::TOK_ID) err(); if(_stack.top().tok_type != token::TOK_ID) err();
std::cout << str_token(_stack.top()) << "\n"; std::cout << str_token(_stack.top()) << "\n";
stm = new struct token; stm = new struct token;
stm->t = token::STM_ASSIGN; stm->tok_type = token::STM_ASSIGN;
stm->rvalue = new struct token; stm->rvalue = new struct token;
*stm->rvalue = exp; *stm->rvalue = exp;
stm->lvalue = new struct token; stm->lvalue = new struct token;
@ -128,14 +128,14 @@ parser::reduce_exp(struct token exp) {
_stack.push(*stm); _stack.push(*stm);
break; break;
case token::TOK_OP: case token::TOK_OP:
if(_stack.top().t == token::TOK_NUM) { if(_stack.top().tok_type == token::TOK_NUM) {
reduce_num(pop()); reduce_num(pop());
} }
if(_stack.top().t == token::TOK_ID) { if(_stack.top().tok_type == token::TOK_ID) {
reduce_id(pop()); reduce_id(pop());
} }
stm = new struct token; stm = new struct token;
stm->t = token::EXP_OPERATION; stm->tok_type = token::EXP_OPERATION;
stm->value = modifier.value; stm->value = modifier.value;
stm->rvalue = new struct token; stm->rvalue = new struct token;
*stm->rvalue = exp; *stm->rvalue = exp;
@ -160,7 +160,7 @@ void
parser::statement() { parser::statement() {
_stack.push(_tokens[pos]); _stack.push(_tokens[pos]);
pos++; pos++;
assert(_stack.top().t == token::TOK_ASSIGN); assert(_stack.top().tok_type == token::TOK_ASSIGN);
expression(); expression();
} }
@ -169,7 +169,7 @@ parser::expression() {
_stack.push(_tokens[pos]); _stack.push(_tokens[pos]);
pos++; pos++;
std::cout << "POS: " << pos << '\n'; std::cout << "POS: " << pos << '\n';
switch (_stack.top().t) { switch (_stack.top().tok_type) {
// case token::EXP_NUMBER: // case token::EXP_NUMBER:
// case token::EXP_ID: // case token::EXP_ID:
case token::TOK_ID: case token::TOK_ID: