-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
173 lines (145 loc) · 3.71 KB
/
parser.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef parser
#define parser
#include "lexer.h"
#include <string>
#include <stack>
#include "interpreter.h"
// Enum
class FuncInScope;
class VarInScope;
class ReturnValue;
class VarScope;
class FuncScope;
enum BlockType {
FUNC,
WHILE,
FOR,
IF
};
// Classes
class Expression {
public:
Expression();
virtual std::string toTreeString() = 0;
virtual ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&) = 0;
};
class Program: public Expression {
public:
Program(std::vector<Expression*>);
std::string toTreeString();
void run();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
private:
std::vector<Expression*> expressions;
};
class Parser {
public:
Program parse(Tokens);
Parser();
};
class Statement: public Expression {
public:
Statement();
virtual std::string toString() = 0;
virtual std::string toTreeString() = 0;
virtual ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&) = 0;
void setType(ValueType);
ValueType getType();
private:
ValueType type;
};
class VariableDeclaration: public Expression {
public:
VariableDeclaration(std::string, Statement*);
std::string toTreeString();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
private:
std::string var_name;
Statement* assigned_stm;
};
class Block: public Expression {
public:
Block(std::vector<Expression*>);
std::string toTreeString();
void setBlockType(BlockType);
BlockType getBlockType();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
std::vector<Expression*> getExpressions();
private:
BlockType type;
std::vector<Expression*> expressions;
};
class If: public Expression {
public:
If(Statement*, Block*);
std::string toTreeString();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
private:
Statement* condition;
Block* body;
};
class While: public Expression {
public:
While(Statement*, Block*);
std::string toTreeString();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
private:
Statement* condition;
Block* body;
};
class For: public Expression {
public:
For(VariableDeclaration*, Statement*, VariableDeclaration*, Block*);
std::string toTreeString();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
private:
VariableDeclaration* init;
Statement* condition;
VariableDeclaration* changer;
Block* body;
};
class Return: public Expression {
public:
Return(Statement*);
std::string toTreeString();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
private:
Statement* return_value;
};
class Break: public Expression {
public:
Break();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
std::string toTreeString();
};
class FunctionDefinition: public Expression {
public:
FunctionDefinition(std::string, std::vector<std::string>, Block*);
std::string toTreeString();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
private:
std::string name;
std::vector<std::string> param_names;
Block* body;
};
class Debug: public Expression {
public:
Debug();
ReturnValue eval(VarScope&, VarScope&, FuncScope&, FuncScope&);
std::string toTreeString();
};
// Functions
Expression* parseDeclaration(Tokens&);
Expression* parseIf(Tokens&);
Expression* parseNextExpression(Tokens&, bool = true);
Expression* parseWhile(Tokens&);
Expression* parseFor(Tokens&);
Expression* parseForChangerDeclaration(Tokens&);
Expression* parseFunctionDefinition(Tokens&);
Expression* parseReturn(Tokens&);
Expression* parseBreak(Tokens&);
Expression* parseDebug(Tokens&);
std::vector<std::string> parseFunctionDefinitionParams(Tokens&);
Statement* buildStatement(Tokens&, Token* = NULL);
Block* parseBody(Tokens&, bool);
#endif