orga-comp/orga-comp.h

121 lines
2.6 KiB
C++

#include <bits/types/FILE.h>
#include <cstddef>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <map>
#include <list>
#include <stack>
#include <string>
#include <vector>
#include <assert.h>
struct token {
enum type {
TOK_ID,
TOK_NUM,
TOK_OP,
TOK_ASSIGN,
TOK_DO,
TOK_END,
TOK_LPAREN,
TOK_RPAREN,
TOK_COMMA,
TOK_SEMI,
STM_ASSIGN,
STM_COMPOUND,
EXP_NUMBER,
EXP_ID,
EXP_OPERATION
};
type tok_type;
std::string value;
struct token *lvalue = nullptr;
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);
std::string str_token(struct token tok);
class interpreter {
public:
void evalSTM(struct token stm);
int evalEXP(struct token exp);
private:
std::map<std::string, int> table;
};
class lexer {
public:
std::vector<struct token*> lex_file(std::string filename);
~lexer();
private:
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();
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 statement(); //pops an statement into stack
void expression(); //pops an expression into stack
void err();
struct token *pop();
void push(struct token *tok);
std::vector<struct token*> _tokens;
std::stack<struct token*> _stack;
size_t pos;
};
class scope {
public:
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;
};
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);
};