diff --git a/build.sh b/build.sh index 91d7e8c..7354469 100644 --- a/build.sh +++ b/build.sh @@ -1,2 +1,2 @@ #!/bin/sh -g++ -g main.cpp interpreter.cpp lexer.cpp parser.cpp utilities.cpp +g++ -g main.cpp interpreter.cpp lexer.cpp parser.cpp utilities.cpp scope.cpp translator.cpp diff --git a/main.cpp b/main.cpp index c5757e5..f05c1a0 100644 --- a/main.cpp +++ b/main.cpp @@ -8,12 +8,14 @@ main(){ // interpretar a = 2 + 2 interpreter inter; lexer lex; + translator tran; std::vector lexed = lex.lex_file("tst.cfran"); parser parser(lexed); struct token *end = parser.parse_tokens(); std::cout << "\n-----------------------\n"; std::cout << "EVALUATING RESULTING TREE:\n"; - std::cout << str_token(*end); + std::cout << str_token(*end) << '\n'; inter.evalSTM(*end); + tran.translate(end); return 1; } \ No newline at end of file diff --git a/orga-comp.h b/orga-comp.h index ad0cc14..b03875c 100644 --- a/orga-comp.h +++ b/orga-comp.h @@ -34,6 +34,14 @@ struct token { struct token *rvalue = nullptr; }; +/* +struct ir { + enum type { + + }; + type tok_type; +};*/ + void print_tokens(std::vector tokens); bool leaf(struct token *tok); void free_tree(struct token *head); @@ -87,7 +95,7 @@ class scope { VALUE, }; scope(); - void add(std::string key, symbol_type type); + void add(std::string key, symbol_type type, size_t pos); symbol_type look(std::string key); // scope(scope const &father); private: @@ -99,6 +107,15 @@ class scope { struct node *head; }; -class translate { - +class translator { + /* + Translate should take a parse tree and + produce an intermediary representation + to be later consumed by a code generator + */ + public: + void translate(struct token *head); + private: + void translate_assign(struct token *stm); + void translate_exp(struct token *exp); }; \ No newline at end of file diff --git a/scope.cpp b/scope.cpp index adce63f..10dc52c 100644 --- a/scope.cpp +++ b/scope.cpp @@ -1,4 +1,5 @@ #include "orga-comp.h" +#include #include scope::scope(){ @@ -6,7 +7,7 @@ scope::scope(){ } void -scope::add(std::string key, symbol_type type){ +scope::add(std::string key, symbol_type type, size_t pos){ if (head == nullptr){ head = new node; head->key = key; diff --git a/translator.cpp b/translator.cpp new file mode 100644 index 0000000..0292727 --- /dev/null +++ b/translator.cpp @@ -0,0 +1,50 @@ +#include "orga-comp.h" +#include +#include +#include + +void +translator::translate(struct token *head){ + switch (head->tok_type) { + case token::STM_COMPOUND: + translate(head->lvalue); + translate(head->rvalue); + break; + case token::STM_ASSIGN: + translate_assign(head); + break; + default: exit(-1); + } +} + +void +translator::translate_assign(struct token *stm){ + struct token *exp = stm->rvalue; + translate_exp(exp); + std::cout << "STORE INTO " << stm->value << '\n'; +} + +void +translator::translate_exp(struct token *exp){ + switch (exp->tok_type) { + case token::EXP_NUMBER: + std::cout << "LOAD LITERAL 0x" << std::hex << exp->value << '\n'; + break; + case token::EXP_ID: + std::cout << "LOAD " << exp->value << '\n'; + break; + case token::EXP_OPERATION: + translate_exp(exp->rvalue); + switch (exp->lvalue->tok_type) { + case token::EXP_NUMBER: + std::cout << "ADD 0x" << std::hex << exp->lvalue->value << '\n'; + break; + case token::EXP_ID: + std::cout << "ADD [" << std::hex << exp->lvalue->value << "]\n"; + break; + default: exit(-1); + } + break; + default: exit(-1); + } +} \ No newline at end of file diff --git a/tst.cfran b/tst.cfran index 225c631..a542959 100644 --- a/tst.cfran +++ b/tst.cfran @@ -1,6 +1,5 @@ a = 2 + 2; j = 2 + 1; h = 2 + 1; -b = 4 + 8; -p = a + 1; -c = 2 - 1; \ No newline at end of file +b = 4 + 8 + 6 + a + 2; +p = a + 1; \ No newline at end of file