Compare commits

...

4 Commits

8 changed files with 162 additions and 117 deletions

12
info
View File

@ -20,10 +20,20 @@ MOV a, 0x0004
a = b + 2
=>
stmAssign('a', opexp(exp('b'), '+', exp('2')))
=>
assign('a' binop('b' + 2, scope))
=>
MOV r1, [b]
ADD r1, 0x0002
MOV a, r1
=>
MOV r1, [0xf000]
ADD r1, 0x0002
MOV 0xf001, r1
MOV 0xf001, r1
equiv representation
=>
MOV TMP, 0x0002 exp('2')
ADD TMP, [b] exp('b') '+'
MOV a, TMP

View File

@ -2,10 +2,10 @@
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';
table[stm.value] = evalEXP(*stm.rvalue);
std::cout << stm.value << " igual a " << table[stm.value] << '\n';
break;
case token::STM_COMPOUND:
evalSTM(*stm.lvalue);
@ -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;

View File

@ -1,6 +1,6 @@
#include "orga-comp.h"
std::vector<struct token>
std::vector<struct token*>
lexer::lex_file(std::string filename) {
//leer un char -> ver que es -> pasar a un estado
// [a-z] -> id
@ -37,11 +37,13 @@ lexer::lex_file(std::string filename) {
case '-':
if(id) add_token(token::TOK_ID, *value);
if(num) add_token(token::TOK_NUM, *value);
delete value;
value = new std::string();
*value += c;
add_token(token::TOK_OP, *value);
id = false;
num = false;
delete value;
value = new std::string();
//OP
break;
@ -51,6 +53,7 @@ lexer::lex_file(std::string filename) {
add_token(token::TOK_ASSIGN, "");
id = false;
num = false;
delete value;
value = new std::string();
break;
case ';':
@ -59,6 +62,7 @@ lexer::lex_file(std::string filename) {
add_token(token::TOK_SEMI, "");
id = false;
num = false;
delete value;
value = new std::string();
//STM END
break;
@ -67,6 +71,7 @@ lexer::lex_file(std::string filename) {
if(num) add_token(token::TOK_NUM, *value);
id = false;
num = false;
delete value;
value = new std::string();
default:
break;
@ -81,7 +86,13 @@ 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);
_tokens.push_back(token);
}
lexer::~lexer(){
for(int i = 0; i < _tokens.size(); i++){
delete _tokens[i];
}
}

View File

@ -6,40 +6,16 @@ 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::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";
inter->evalSTM(end);
std::cout << str_token(*end);
inter.evalSTM(*end);
// free_tree(end);
return 1;
}

View File

@ -28,13 +28,15 @@ struct token {
EXP_ID,
EXP_OPERATION
};
type t;
type tok_type;
std::string value;
struct token *lvalue;
struct token *rvalue;
struct token *lvalue = nullptr;
struct token *rvalue = nullptr;
};
void print_tokens(std::vector<struct token> tokens);
void print_tokens(std::vector<struct token*> tokens);
bool leaf(struct token *tok);
void free_tree(struct token *head);
std::string str_token(struct token tok);
class interpreter {
@ -49,32 +51,50 @@ private:
class lexer {
public:
std::vector<struct token> lex_file(std::string filename);
std::vector<struct token*> lex_file(std::string filename);
~lexer();
private:
std::vector<struct token> _tokens;
std::vector<struct token*> _tokens;
void add_token(token::type, std::string value);
};
class parser {
public:
parser(std::vector<struct token> tokens);
struct token parse_tokens();
parser(std::vector<struct token*> tokens);
struct token *parse_tokens();
~parser();
private:
void reduce();
void reduce_num(struct token num);
void reduce_id(struct token id);
void reduce_stm(struct token stm);
void reduce_exp(struct token exp);
void reduce_num(struct token *num);
void reduce_id(struct token *id);
void reduce_stm(struct token *stm);
void reduce_exp(struct token *exp);
void statement(); //pops an statement into stack
void expression(); //pops an expression into stack
void err();
struct token pop();
std::vector<struct token> _tokens;
std::stack<struct token> _stack;
struct token *pop();
void push(struct token *tok);
std::vector<struct token*> _tokens;
std::stack<struct token*> _stack;
size_t pos;
};
class scope {
public:
scope();
scope(scope const &father);
enum symbol_type{
VALUE,
};
private:
struct node {
std::string key;
symbol_type type;
};
struct node head;
};
class translate {
};

View File

@ -1,6 +1,7 @@
#include "orga-comp.h"
#include <iostream>
struct token
struct token *
parser::parse_tokens() {
// LR(k) parsing.
// Pushea al stack hasta encontrar una cantidad de token suficiente
@ -10,9 +11,8 @@ 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();
break;
case token::STM_COMPOUND:
@ -32,7 +32,7 @@ parser::parse_tokens() {
//check if reduce is possible, if it is reduce.
}
parser::parser(std::vector<struct token> tokens) {
parser::parser(std::vector<struct token*> tokens) {
_tokens = tokens;
pos = 0;
}
@ -40,12 +40,12 @@ parser::parser(std::vector<struct token> tokens) {
void
parser::reduce() {
if(_stack.size() == 1) return;
struct token tok = pop();
struct token *tok = pop();
std::cout << str_token(tok) << " ";
switch (tok.t) {
// std::cout << str_token(*tok) << " ";
switch (tok->tok_type) {
case token::TOK_NUM:
std::cout << "reduzco numero\n";
// std::cout << "reduzco numero\n";
reduce_num(tok);
// reduce();
break;
@ -71,20 +71,18 @@ parser::reduce() {
}
void
parser::reduce_stm(struct token stm) {
struct token tok = pop();
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->rvalue = new struct token;
*tmp->rvalue = stm;
tmp->lvalue = new struct token;
*tmp->lvalue = tok;
_stack.push(*tmp);
std::cout << "CHAD STM REDUCER " << str_token(*tmp);
tmp->tok_type = token::STM_COMPOUND;
tmp->rvalue = stm;
tmp->lvalue = tok;
push(tmp);
// std::cout << "CHAD STM REDUCER " << str_token(*tmp);
break;
default:
err();
@ -93,74 +91,74 @@ parser::reduce_stm(struct token stm) {
}
void
parser::reduce_id(struct token id) {
parser::reduce_id(struct token *id) {
struct token *tmp = new struct token;
tmp->t = token::EXP_ID;
tmp->value = id.value;
_stack.push(*tmp);
tmp->tok_type = token::EXP_ID;
tmp->value = id->value;
push(tmp);
}
void
parser::reduce_num(struct token num) {
parser::reduce_num(struct token *num) {
struct token *tmp = new struct token;
tmp->t = token::EXP_NUMBER;
tmp->value = num.value;
_stack.push(*tmp);
tmp->tok_type = token::EXP_NUMBER;
tmp->value = num->value;
push(tmp);
}
void
parser::reduce_exp(struct token exp) {
std::cout << "reducing exp: " << str_token(exp) << "\n";
struct token modifier = pop();
parser::reduce_exp(struct token *exp) {
// std::cout << "reducing exp: " << str_token(*exp) << "\n";
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();
std::cout << str_token(_stack.top()) << "\n";
// std::cout << "assign found\n";
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->rvalue = new struct token;
*stm->rvalue = exp;
stm->lvalue = new struct token;
*stm->lvalue = pop();
_stack.push(*stm);
stm->tok_type = token::STM_ASSIGN;
stm->rvalue = exp;
stm->value = pop()->value;
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->value = modifier.value;
stm->rvalue = new struct token;
*stm->rvalue = exp;
stm->lvalue = new struct token;
*stm->lvalue = pop();
_stack.push(*stm);
stm->tok_type = token::EXP_OPERATION;
stm->value = modifier->value;
stm->rvalue = exp;
stm->lvalue = pop();
push(stm);
//reduce_exp(pop());
break;
default: err();
}
}
struct token
struct token *
parser::pop() {
struct token tmp = _stack.top();
struct token *tmp = _stack.top();
_stack.pop();
return tmp;
}
void
parser::push(struct token *tok) {
_stack.push(tok);
}
void
parser::statement() {
_stack.push(_tokens[pos]);
pos++;
assert(_stack.top().t == token::TOK_ASSIGN);
assert(_stack.top()->tok_type == token::TOK_ASSIGN);
expression();
}
@ -168,8 +166,8 @@ void
parser::expression() {
_stack.push(_tokens[pos]);
pos++;
std::cout << "POS: " << pos << '\n';
switch (_stack.top().t) {
// std::cout << "POS: " << pos << '\n';
switch (_stack.top()->tok_type) {
// case token::EXP_NUMBER:
// case token::EXP_ID:
case token::TOK_ID:
@ -177,14 +175,14 @@ parser::expression() {
expression();
break;
case token::EXP_OPERATION:
std::cout << "Nested operations not supported\n";
// std::cout << "Nested operations not supported\n";
err();
break;
case token::TOK_OP:
expression();
break;
case token::TOK_SEMI:
std::cout << "REDUCE\n";
// std::cout << "REDUCE\n";
reduce();
break;
default:
@ -197,7 +195,14 @@ void
parser::err() {
std::cout
<< "unexpected symbol: "
<< str_token(_stack.top())
<< str_token(*_stack.top())
<< '\n';
exit(-1);
}
parser::~parser(){
free_tree(pop());
// while (_stack.size() > 0) {
// delete pop();
// }
}

0
scope.cpp Normal file
View File

View File

@ -1,16 +1,39 @@
#include "orga-comp.h"
#include <iostream>
void
print_tokens(std::vector<struct token> tokens){
for(struct token tok : tokens) {
std::cout << str_token(tok) << " ";
print_tokens(std::vector<struct token*> tokens){
for(struct token *tok : tokens) {
std::cout << str_token(*tok) << " ";
}
std::cout << '\n';
}
bool
leaf(struct token *tok){
if(tok->lvalue != nullptr)
return false;
if(tok->rvalue != nullptr)
return false;
return true;
}
void
free_tree(struct token *head){
if(head == nullptr)
return;
if (leaf(head)) {
delete(head);
} else {
free_tree(head->lvalue);
free_tree(head->rvalue);
delete(head);
}
}
std::string
str_token(struct token tok){
switch (tok.t) {
switch (tok.tok_type) {
case token::TOK_ID:
return "ID(" + tok.value + ")";
case token::TOK_NUM:
@ -22,9 +45,9 @@ str_token(struct token tok){
case token::TOK_SEMI:
return "SEMI ";
case token::STM_ASSIGN:
return "STM_ASSIGN( " + str_token(*tok.lvalue) + ", " + str_token(*tok.rvalue) + ")";
return "STM_ASSIGN( " + tok.value + ", " + str_token(*tok.rvalue) + ")";
case token::STM_COMPOUND:
return "STM_COMPOUND( " + str_token(*tok.lvalue) + ", " + str_token(*tok.rvalue) + ")";
return "STM_COMPOUND< " + str_token(*tok.lvalue) + ", " + str_token(*tok.rvalue) + ">";
case token::EXP_NUMBER:
return "EXP_NUMBER(" + tok.value + ")";
case token::EXP_OPERATION: