Compare commits

...

2 Commits

4 changed files with 57 additions and 38 deletions

View File

@ -1,2 +1,2 @@
#!/bin/sh
g++ -g main.cpp interpreter.cpp lexer.cpp parser.cpp
g++ -g main.cpp interpreter.cpp lexer.cpp parser.cpp utilities.cpp

20
info
View File

@ -8,4 +8,22 @@ exp -> numero (literal) -> number
exp -> id (idExp) -> id
exp -> exp op exp (operacion) -> opexp
op -> + (suma) -> plus
op -> - (resta) -> minus
op -> - (resta) -> minus
IR:
example:
a = 2 + 2
=>
a = 4
=>
MOV a, 0x0004
a = b + 2
=>
MOV r1, [b]
ADD r1, 0x0002
MOV a, r1
=>
MOV r1, [0xf000]
ADD r1, 0x0002
MOV 0xf001, r1

View File

@ -42,40 +42,4 @@ main(){
inter->evalSTM(end);
return 1;
}
void
print_tokens(std::vector<struct token> tokens){
for(struct token tok : tokens) {
std::cout << str_token(tok) << " ";
}
std::cout << '\n';
}
std::string
str_token(struct token tok){
switch (tok.t) {
case token::TOK_ID:
return "ID(" + tok.value + ")";
case token::TOK_NUM:
return "NUM(" + tok.value + ")";
case token::TOK_OP:
return "OP(" + tok.value + ")";
case token::TOK_ASSIGN:
return "ASSIGN ";
case token::TOK_SEMI:
return "SEMI ";
case token::STM_ASSIGN:
return "STM_ASSIGN( " + str_token(*tok.lvalue) + ", " + str_token(*tok.rvalue) + ")";
case token::STM_COMPOUND:
return "STM_COMPOUND( " + str_token(*tok.lvalue) + ", " + str_token(*tok.rvalue) + ")";
case token::EXP_NUMBER:
return "EXP_NUMBER(" + tok.value + ")";
case token::EXP_OPERATION:
return "EXP_OPERATION( " + str_token(*tok.lvalue) + " " + tok.value + " " + str_token(*tok.rvalue) +")";
case token::EXP_ID:
return "EXP_ID ";
default:
return "SOMETHING";
}
}

37
utilities.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "orga-comp.h"
void
print_tokens(std::vector<struct token> tokens){
for(struct token tok : tokens) {
std::cout << str_token(tok) << " ";
}
std::cout << '\n';
}
std::string
str_token(struct token tok){
switch (tok.t) {
case token::TOK_ID:
return "ID(" + tok.value + ")";
case token::TOK_NUM:
return "NUM(" + tok.value + ")";
case token::TOK_OP:
return "OP(" + tok.value + ")";
case token::TOK_ASSIGN:
return "ASSIGN ";
case token::TOK_SEMI:
return "SEMI ";
case token::STM_ASSIGN:
return "STM_ASSIGN( " + str_token(*tok.lvalue) + ", " + str_token(*tok.rvalue) + ")";
case token::STM_COMPOUND:
return "STM_COMPOUND( " + str_token(*tok.lvalue) + ", " + str_token(*tok.rvalue) + ")";
case token::EXP_NUMBER:
return "EXP_NUMBER(" + tok.value + ")";
case token::EXP_OPERATION:
return "EXP_OPERATION( " + str_token(*tok.lvalue) + " " + tok.value + " " + str_token(*tok.rvalue) +")";
case token::EXP_ID:
return "EXP_ID ";
default:
return "SOMETHING";
}
}