Compare commits
5 Commits
347a98bd70
...
e451ffd7c5
Author | SHA1 | Date |
---|---|---|
fsan | e451ffd7c5 | |
fsan | 4f1718ce5a | |
fsan | a08402f795 | |
fsan | 3991cb9895 | |
fsan | d7c3ab9b4b |
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
|
||||
|
|
|
@ -5,7 +5,7 @@ interpreter::evalSTM(struct token stm) {
|
|||
switch (stm.tok_type) {
|
||||
case token::STM_ASSIGN:
|
||||
table[stm.value] = evalEXP(*stm.rvalue);
|
||||
std::cout << stm.value << " igual a " << table[stm.value] << '\n';
|
||||
std::cout << stm.value << " igual a " << table.at(stm.value) << '\n';
|
||||
break;
|
||||
case token::STM_COMPOUND:
|
||||
evalSTM(*stm.lvalue);
|
||||
|
@ -25,7 +25,7 @@ interpreter::evalEXP(struct token exp) {
|
|||
return std::stoi(exp.value);
|
||||
break;
|
||||
case token::EXP_ID:
|
||||
return table[exp.value];
|
||||
return table.at(exp.value);
|
||||
break;
|
||||
case token::EXP_OPERATION:
|
||||
if(exp.value == "+") return evalEXP(*exp.lvalue) + evalEXP(*exp.rvalue);
|
||||
|
@ -36,4 +36,5 @@ interpreter::evalEXP(struct token exp) {
|
|||
exit(-1);
|
||||
break;
|
||||
}
|
||||
exit(-1);
|
||||
}
|
6
main.cpp
6
main.cpp
|
@ -7,15 +7,15 @@ main(){
|
|||
// realizar un interprete
|
||||
// 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);
|
||||
// free_tree(end);
|
||||
tran.translate(end);
|
||||
return 1;
|
||||
}
|
31
orga-comp.h
31
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);
|
||||
|
@ -82,19 +90,32 @@ class parser {
|
|||
|
||||
class scope {
|
||||
public:
|
||||
scope();
|
||||
scope(scope const &father);
|
||||
enum symbol_type{
|
||||
NOT_FOUND,
|
||||
VALUE,
|
||||
};
|
||||
scope();
|
||||
void add(std::string key, symbol_type type, size_t pos);
|
||||
symbol_type look(std::string key);
|
||||
// scope(scope const &father);
|
||||
private:
|
||||
struct node {
|
||||
std::string key;
|
||||
symbol_type type;
|
||||
struct node *next = nullptr;
|
||||
};
|
||||
struct node head;
|
||||
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);
|
||||
};
|
16
parser.cpp
16
parser.cpp
|
@ -23,10 +23,6 @@ parser::parse_tokens() {
|
|||
}
|
||||
return parse_tokens();
|
||||
}
|
||||
/*std::cout << "ESTADO FINAL DEL STACK:\n";
|
||||
for(int i = 0; i < _stack.size(); i++) {
|
||||
std::cout << str_token(pop()) << " ";
|
||||
}*/
|
||||
|
||||
return _stack.top();
|
||||
//check if reduce is possible, if it is reduce.
|
||||
|
@ -136,7 +132,6 @@ parser::reduce_exp(struct token *exp) {
|
|||
stm->rvalue = exp;
|
||||
stm->lvalue = pop();
|
||||
push(stm);
|
||||
//reduce_exp(pop());
|
||||
break;
|
||||
default: err();
|
||||
}
|
||||
|
@ -166,23 +161,21 @@ void
|
|||
parser::expression() {
|
||||
_stack.push(_tokens[pos]);
|
||||
pos++;
|
||||
// std::cout << "POS: " << pos << '\n';
|
||||
switch (_stack.top()->tok_type) {
|
||||
// case token::EXP_NUMBER:
|
||||
// case token::EXP_ID:
|
||||
case token::TOK_ID:
|
||||
case token::TOK_NUM:
|
||||
expression();
|
||||
break;
|
||||
case token::EXP_ID:
|
||||
case token::EXP_NUMBER:
|
||||
case token::EXP_OPERATION:
|
||||
// std::cout << "Nested operations not supported\n";
|
||||
std::cout << "This shouldn't be here...\n";
|
||||
err();
|
||||
break;
|
||||
case token::TOK_OP:
|
||||
expression();
|
||||
break;
|
||||
case token::TOK_SEMI:
|
||||
// std::cout << "REDUCE\n";
|
||||
reduce();
|
||||
break;
|
||||
default:
|
||||
|
@ -202,7 +195,4 @@ parser::err() {
|
|||
|
||||
parser::~parser(){
|
||||
free_tree(pop());
|
||||
// while (_stack.size() > 0) {
|
||||
// delete pop();
|
||||
// }
|
||||
}
|
32
scope.cpp
32
scope.cpp
|
@ -0,0 +1,32 @@
|
|||
#include "orga-comp.h"
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
scope::scope(){
|
||||
head = nullptr;
|
||||
}
|
||||
|
||||
void
|
||||
scope::add(std::string key, symbol_type type, size_t pos){
|
||||
if (head == nullptr){
|
||||
head = new node;
|
||||
head->key = key;
|
||||
head->type = type;
|
||||
}else{
|
||||
struct node *tmp = head;
|
||||
head = new node;
|
||||
head->key = key;
|
||||
head->type = type;
|
||||
head->next = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
scope::symbol_type
|
||||
scope::look(std::string key){
|
||||
struct node *tmp = head;
|
||||
while(tmp != nullptr){
|
||||
if(tmp->key == key) return tmp->type;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return NOT_FOUND;
|
||||
}
|
|
@ -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