-
Notifications
You must be signed in to change notification settings - Fork 3
/
AST.h
157 lines (136 loc) · 3.27 KB
/
AST.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
/*
* src/runtime/AST.h
* Wopslang AST Headerfile
*
* 2021, Wops Team
* */
#ifndef WOPS_AST_H
#define WOPS_AST_H
#include <vector>
#include <string>
#include <unordered_map>
#include "../import_/eexec_.h"
#include "../type/variable.h"
#include "../type/array.h"
#include "../type/operator.h"
enum StmtType {
Main,
Declaration,
Expression,
Assignment,
BreakStmt,
ContinueStmt,
IfStmt,
ForStmt,
};
// :TODO Add some comments
class Expr {
private:
bool constant = 0, variable = 0, call = 0;
Variable token;
std::vector<Expr> children;
public:
Expr(std::vector<bool> t, Variable &tkn) {
token = tkn, constant = t[0], variable = t[1], call = t[2];
}
void SetChildren(std::vector<Expr> &rplcment) {
children = rplcment;
}
void AddChild(Expr &child) {
children.push_back(child);
}
Variable Execute(std::unordered_map<std::string, Variable> &storage) {
std::string tkn = token.GetValue();
if (variable) {
auto iter = storage.find(tkn);
if (iter == storage.end())
ErrHandler().CallErr(tkn + " has not declared yet");
return iter->second;
}
if (constant) {
return token;
}
if (call) {
Array arg;
for (Expr child: children) {
arg.container.push_back(child.Execute(storage));
}
ArrayWithCode res = EExecFunc(tkn, arg);
if (res.error == ERROR)
ErrHandler().CallErr("Error occured while calling " + tkn);
return res.var.container[0];
}
// Binary Operation
if (tkn == "+") {
return children[0].Execute(storage) + children[1].Execute(storage);
}
if (tkn == "-") {
return children[0].Execute(storage) - children[1].Execute(storage);
}
if (tkn == "*") {
return children[0].Execute(storage) * children[1].Execute(storage);
}
if (tkn == "/") {
return children[0].Execute(storage) / children[1].Execute(storage);
}
if (tkn == "%") {
return children[0].Execute(storage) - children[1].Execute(storage);
}
if (tkn == "==") {
return children[0].Execute(storage) == children[1].Execute(storage);
}
if (tkn == "!=") {
return children[0].Execute(storage) != children[1].Execute(storage);
}
if (tkn == ">") {
return children[0].Execute(storage) > children[1].Execute(storage);
}
if (tkn == ">=") {
return children[0].Execute(storage) >= children[1].Execute(storage);
}
if (tkn == "<") {
return children[0].Execute(storage) < children[1].Execute(storage);
}
if (tkn == "<=") {
return children[0].Execute(storage) <= children[1].Execute(storage);
}
// Unary Operation
if (tkn == "!") {
return !children[0].Execute(storage);
}
}
};
class AST {
private:
StmtType _t;
std::vector<AST> childStmt;
std::vector<Expr> expression;
std::vector<Variable> argument;
std::unordered_map<std::string, Variable> storage;
public:
AST(StmtType t, std::vector<Variable> argv, std::vector<Expr> expr) {
_t = t, argument = argv, expression = expr;
}
void SetChildren(std::vector<AST> rplcment) {
childStmt = rplcment;
}
void AddChild(AST child) {
childStmt.push_back(child);
}
void SetExpr(std::vector<Expr> rplcment) {
expression = rplcment;
}
void AddExpr(Expr expr) {
expression.push_back(expr);
}
void SetArg(std::vector<Variable> argv) {
argument = argv;
}
void AddArg(Variable argv) {
argument.push_back(argv);
}
void Execute() {
// :TODO Implement it
}
};
#endif