From 82feecbcb2e281e302997c17bd6a23e03480b0a8 Mon Sep 17 00:00:00 2001 From: fsan Date: Mon, 4 Oct 2021 23:28:08 -0300 Subject: [PATCH] Changed token.t to token.tok_type, started work on scopes --- interpreter.cpp | 4 ++-- lexer.cpp | 2 +- main.cpp | 30 ++---------------------------- orga-comp.h | 14 +++++++++++++- parser.cpp | 28 ++++++++++++++-------------- 5 files changed, 32 insertions(+), 46 deletions(-) diff --git a/interpreter.cpp b/interpreter.cpp index e53bd09..dc40c59 100644 --- a/interpreter.cpp +++ b/interpreter.cpp @@ -2,7 +2,7 @@ void interpreter::evalSTM(struct token stm) { - switch (stm.t) { + switch (stm.tok_type) { case token::STM_ASSIGN: table[stm.lvalue->value] = evalEXP(*stm.rvalue); std::cout << stm.lvalue->value << " igual a " << table[stm.lvalue->value] << '\n'; @@ -20,7 +20,7 @@ interpreter::evalSTM(struct token stm) { int interpreter::evalEXP(struct token exp) { - switch (exp.t) { + switch (exp.tok_type) { case token::EXP_NUMBER: return std::stoi(exp.value); break; diff --git a/lexer.cpp b/lexer.cpp index 21ea6e6..58bb8a5 100644 --- a/lexer.cpp +++ b/lexer.cpp @@ -81,7 +81,7 @@ lexer::lex_file(std::string filename) { void lexer::add_token(token::type type, std::string value) { struct token *token = new struct token; - token->t = type; + token->tok_type = type; token->value = value; _tokens.push_back(*token); } \ No newline at end of file diff --git a/main.cpp b/main.cpp index c415368..4afe07d 100644 --- a/main.cpp +++ b/main.cpp @@ -6,40 +6,14 @@ main(){ // lex -> parse -> analisis semantico // realizar un interprete // interpretar a = 2 + 2 - struct token test; - 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); + interpreter inter; lexer lex; parser parser(lex.lex_file("tst.cfran")); struct token end = parser.parse_tokens(); std::cout << "\n-----------------------\n"; std::cout << "EVALUATING RESULTING TREE:\n"; - inter->evalSTM(end); + inter.evalSTM(end); return 1; } \ No newline at end of file diff --git a/orga-comp.h b/orga-comp.h index c0017b5..20295b3 100644 --- a/orga-comp.h +++ b/orga-comp.h @@ -28,7 +28,7 @@ struct token { EXP_ID, EXP_OPERATION }; - type t; + type tok_type; std::string value; struct token *lvalue; struct token *rvalue; @@ -75,6 +75,18 @@ class parser { size_t pos; }; +class scope { + public: + enum symbol_type{ + VALUE, + }; + private: + struct node { + std::string key; + symbol_type type; + }; +}; + class translate { }; \ No newline at end of file diff --git a/parser.cpp b/parser.cpp index a8a179d..d6320ed 100644 --- a/parser.cpp +++ b/parser.cpp @@ -10,7 +10,7 @@ parser::parse_tokens() { _stack.push(_tokens[pos]); pos++; //_stack.push(tok); - switch (_stack.top().t) { + switch (_stack.top().tok_type) { case token::TOK_ID: std::cout << "STATEMENT\n"; statement(); @@ -43,7 +43,7 @@ parser::reduce() { struct token tok = pop(); std::cout << str_token(tok) << " "; - switch (tok.t) { + switch (tok.tok_type) { case token::TOK_NUM: std::cout << "reduzco numero\n"; reduce_num(tok); @@ -74,11 +74,11 @@ void parser::reduce_stm(struct token stm) { struct token tok = pop(); struct token *tmp; - switch (tok.t) { + switch (tok.tok_type) { case token::STM_ASSIGN: case token::STM_COMPOUND: tmp = new struct token; - tmp->t = token::STM_COMPOUND; + tmp->tok_type = token::STM_COMPOUND; tmp->rvalue = new struct token; *tmp->rvalue = stm; tmp->lvalue = new struct token; @@ -95,7 +95,7 @@ parser::reduce_stm(struct token stm) { void parser::reduce_id(struct token id) { struct token *tmp = new struct token; - tmp->t = token::EXP_ID; + tmp->tok_type = token::EXP_ID; tmp->value = id.value; _stack.push(*tmp); } @@ -103,7 +103,7 @@ parser::reduce_id(struct token id) { void parser::reduce_num(struct token num) { struct token *tmp = new struct token; - tmp->t = token::EXP_NUMBER; + tmp->tok_type = token::EXP_NUMBER; tmp->value = num.value; _stack.push(*tmp); } @@ -114,13 +114,13 @@ parser::reduce_exp(struct token exp) { struct token modifier = pop(); struct token *tmp; struct token *stm; - switch (modifier.t) { + switch (modifier.tok_type) { case token::TOK_ASSIGN: 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"; stm = new struct token; - stm->t = token::STM_ASSIGN; + stm->tok_type = token::STM_ASSIGN; stm->rvalue = new struct token; *stm->rvalue = exp; stm->lvalue = new struct token; @@ -128,14 +128,14 @@ parser::reduce_exp(struct token exp) { _stack.push(*stm); break; case token::TOK_OP: - if(_stack.top().t == token::TOK_NUM) { + if(_stack.top().tok_type == token::TOK_NUM) { reduce_num(pop()); } - if(_stack.top().t == token::TOK_ID) { + if(_stack.top().tok_type == token::TOK_ID) { reduce_id(pop()); } stm = new struct token; - stm->t = token::EXP_OPERATION; + stm->tok_type = token::EXP_OPERATION; stm->value = modifier.value; stm->rvalue = new struct token; *stm->rvalue = exp; @@ -160,7 +160,7 @@ void parser::statement() { _stack.push(_tokens[pos]); pos++; - assert(_stack.top().t == token::TOK_ASSIGN); + assert(_stack.top().tok_type == token::TOK_ASSIGN); expression(); } @@ -169,7 +169,7 @@ parser::expression() { _stack.push(_tokens[pos]); pos++; std::cout << "POS: " << pos << '\n'; - switch (_stack.top().t) { + switch (_stack.top().tok_type) { // case token::EXP_NUMBER: // case token::EXP_ID: case token::TOK_ID: