orga-comp/translator.cpp

50 lines
1.4 KiB
C++

#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);
}
}