-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetExpression.cpp
More file actions
96 lines (69 loc) · 2.17 KB
/
getExpression.cpp
File metadata and controls
96 lines (69 loc) · 2.17 KB
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
#include "Expression.h"
#include "Declaration.h"
Expression * Expression::getExpression(bool isCommand, deque<Declaration*> * params)
{
if (Minst->Equal("}") || Minst->Equal(")"))
return nullptr;
int OpenSquares = 0;
while (Minst->Match("(")) OpenSquares++;
Expression * result = nullptr;
bool PrevIsOper = true;
char * oper = nullptr, *name = nullptr, * str = nullptr;
BASE64 * num = nullptr;
Expression * exp = nullptr;
deque<Expression*> * exps = new deque<Expression*>();
while ((PrevIsOper && (name = Minst->GetName(false))) || (PrevIsOper && (num = Minst->GetNumberConstant(false))) || (PrevIsOper && (str = Minst->GetStringConstant(false))) || (!PrevIsOper && (oper = Minst->GetOperator(false))) || (PrevIsOper && Minst->Equal("(") && (exp = Expression::getExpression(false, params)))) {
if (name) {
Declaration * param = nullptr;
for (auto param : *params)
if (!strcmp(param->rawName, name))
name = param->name;
if (Minst->Match("(")) {
deque<Expression*> * childexps = new deque<Expression*>();
Expression * childexp = nullptr;
while ((childexp = Expression::getExpression(false, params))) {
childexps->push_back(childexp);
if (!Minst->Match(",")) break;
}
Minst->Matching(")");
exps->push_back(new Expression(name, childexps));
}
else {
if (Minst->Match("="))
if (!exps->size()) {
PrevIsOper = false;
result = new Expression(name, Expression::getExpression(false, params));
break;
}
else
Error("Multiple assignment is not supported.");
else
exps->push_back(new Expression(name));
}
}
if (num)
exps->push_back(new Expression(num));
if (str)
exps->push_back(new Expression(str, 0));
if (oper)
exps->push_back(new Expression(oper, ""));
if (exp)
exps->push_back(exp);
PrevIsOper = false;
if (oper)
PrevIsOper = true;
name = nullptr;
num = nullptr;
str = nullptr;
oper = nullptr;
exp = nullptr;
}
if (PrevIsOper)
Error("Cannot finish expression with an operator");
if (exps->size())
result = new Expression(exps);
while (OpenSquares--) Minst->Matching(")");
if (isCommand)
Minst->Matching(";");
return result;
}