STARTED WORK ON TRANSLATOR TO IR
This commit is contained in:
parent
4f1718ce5a
commit
e451ffd7c5
2
build.sh
2
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
|
||||
|
|
4
main.cpp
4
main.cpp
|
@ -8,12 +8,14 @@ main(){
|
|||
// interpretar a = 2 + 2
|
||||
interpreter inter;
|
||||
lexer lex;
|
||||
translator tran;
|
||||
std::vector<struct token*> 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;
|
||||
}
|
23
orga-comp.h
23
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<struct token*> 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);
|
||||
};
|
|
@ -1,4 +1,5 @@
|
|||
#include "orga-comp.h"
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
#include "orga-comp.h"
|
||||
#include <cstdlib>
|
||||
#include <ios>
|
||||
#include <iostream>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue