Changed token.t to token.tok_type, started work on scopes

This commit is contained in:
fsan 2021-10-04 23:28:08 -03:00
parent deaab50a6d
commit 82feecbcb2
5 changed files with 32 additions and 46 deletions

View File

@ -2,7 +2,7 @@
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';
@ -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

@ -81,7 +81,7 @@ 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);
}

View File

@ -6,40 +6,14 @@ 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::cout << "\n-----------------------\n";
std::cout << "EVALUATING RESULTING TREE:\n";
inter->evalSTM(end);
inter.evalSTM(end);
return 1;
}

View File

@ -28,7 +28,7 @@ struct token {
EXP_ID,
EXP_OPERATION
};
type t;
type tok_type;
std::string value;
struct token *lvalue;
struct token *rvalue;
@ -75,6 +75,18 @@ class parser {
size_t pos;
};
class scope {
public:
enum symbol_type{
VALUE,
};
private:
struct node {
std::string key;
symbol_type type;
};
};
class translate {
};

View File

@ -10,7 +10,7 @@ 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();
@ -43,7 +43,7 @@ parser::reduce() {
struct token tok = pop();
std::cout << str_token(tok) << " ";
switch (tok.t) {
switch (tok.tok_type) {
case token::TOK_NUM:
std::cout << "reduzco numero\n";
reduce_num(tok);
@ -74,11 +74,11 @@ void
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->tok_type = token::STM_COMPOUND;
tmp->rvalue = new struct token;
*tmp->rvalue = stm;
tmp->lvalue = new struct token;
@ -95,7 +95,7 @@ parser::reduce_stm(struct token stm) {
void
parser::reduce_id(struct token id) {
struct token *tmp = new struct token;
tmp->t = token::EXP_ID;
tmp->tok_type = token::EXP_ID;
tmp->value = id.value;
_stack.push(*tmp);
}
@ -103,7 +103,7 @@ parser::reduce_id(struct token id) {
void
parser::reduce_num(struct token num) {
struct token *tmp = new struct token;
tmp->t = token::EXP_NUMBER;
tmp->tok_type = token::EXP_NUMBER;
tmp->value = num.value;
_stack.push(*tmp);
}
@ -114,13 +114,13 @@ parser::reduce_exp(struct token exp) {
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();
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->tok_type = token::STM_ASSIGN;
stm->rvalue = new struct token;
*stm->rvalue = exp;
stm->lvalue = new struct token;
@ -128,14 +128,14 @@ parser::reduce_exp(struct token exp) {
_stack.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->tok_type = token::EXP_OPERATION;
stm->value = modifier.value;
stm->rvalue = new struct token;
*stm->rvalue = exp;
@ -160,7 +160,7 @@ void
parser::statement() {
_stack.push(_tokens[pos]);
pos++;
assert(_stack.top().t == token::TOK_ASSIGN);
assert(_stack.top().tok_type == token::TOK_ASSIGN);
expression();
}
@ -169,7 +169,7 @@ parser::expression() {
_stack.push(_tokens[pos]);
pos++;
std::cout << "POS: " << pos << '\n';
switch (_stack.top().t) {
switch (_stack.top().tok_type) {
// case token::EXP_NUMBER:
// case token::EXP_ID:
case token::TOK_ID: