-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.h
78 lines (72 loc) · 1.28 KB
/
interpreter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <string>
#include <unordered_map>
enum
{
INTEGER,
PLUS,
MINUS,
MULTIPLY,
DIVIDE,
LPAREN,
RPAREN,
BEGIN,
END,
DOT,
ASSIGNMENT,
SEMICOLON,
IDENTIFIER,
END_OF_FILE,
NONE
};
class Token
{
public:
int token_type;
int value;
Token(){};
Token(int type, int value);
std::string token_to_string();
};
class Lexer
{
public:
std::unordered_map<std::string, int> reserved_keywords;
int pos; // index into text
FILE *fp;
Lexer(const char *filename);
char peek();
Token *get_next_token();
void validate();
};
class ASTNode
{
public:
int node_type;
int node_val;
ASTNode *left;
ASTNode *right;
ASTNode(int type, int val);
ASTNode(int type, int val, ASTNode *left, ASTNode *right);
};
class Parser
{
public:
Lexer *lexer;
Token *current_token;
Parser(const char *filename, Token *start_token);
void error();
void validate(int token_type);
ASTNode *factor();
ASTNode *term();
ASTNode *expr();
ASTNode *parse();
};
class Interpreter
{
public:
Lexer *lexer;
Parser *parser;
Interpreter(const char *filename);
int traverse(ASTNode *root); // For now it traverses the tree and returns the value of an expression
void interpret();
};