-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpr.cpp
More file actions
155 lines (129 loc) · 3.23 KB
/
Expr.cpp
File metadata and controls
155 lines (129 loc) · 3.23 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
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
#include "Expr.h"
/*** Debug print functions ***/
static ostream * _out = NULL;
void setOutEnabled(bool enabled)
{
_out = new ostream((enabled) ? cout.rdbuf() : 0);
}
// usage: "out() << "string" << "\n";" like "std::cout"
ostream& out()
{
if (!_out)
_out = new ostream(cout.rdbuf());
return *_out;
}
/*** Wrapper for Token Expression ***/
string TokenExpr::DebugString()
{
return _tok;
}
/*** Comment Expression ***/
string CommentExpr::DebugString()
{
return "comment found: \"" + _comment + "\"";
}
/*** Input Expression ***/
int InputExpr::_indexesCount = 0;
string InputExpr::DebugString()
{
ostringstream ostr;
ostr << "input " << _index;
return ostr.str();
}
/*** Variable Expression ***/
string VarExpr::DebugString()
{
string s = ((_inversed) ? "inversed " : "");
return s + "var \"" + _name + "\"";
}
/*** Named Variable Expression ***/
string NamedVarExpr::DebugString()
{
return "named var from " + _expr->DebugString();
}
/*** Initialisation Expression ***/
string InitExpr::DebugString()
{
return _LHS->DebugString() + " = " + _RHS->DebugString();
}
/*** Binary Operator Expression ***/
string BinOpExpr::DebugString()
{
return "Binop(" + _LHS->DebugString() + " " + _op.print() + " " + _RHS->DebugString() + ")";
}
/*** Print Expression ***/
string PrintExpr::DebugString()
{
ostringstream ostr;
ostr << "print: \"";
for (vector<Expr *>::iterator it = output.begin(); it != output.end(); it++) {
ostr << (*it)->DebugString();
if (*it != output.back()) // Add "," between each expression (and not for the last item)
ostr << ", ";
}
ostr << "\"";
return ostr.str();
}
/*** Hello Print Expression (print "Hello, {:$}!" if has an input, "Hello, World!" else) ***/
string HelloPrintExpr::DebugString()
{
return "Hello, World!";
}
/*** No Operation Expression ***/
string NopExpr::DebugString()
{
return "nop;";
}
/*** Exit (terminate) Expression ***/
string ExitExpr::DebugString()
{
ostringstream ostr;
ostr << "exit(" << code << ")";
return ostr.str();
}
/*** Push (to global stack) Expression ***/
string PushExpr::DebugString()
{
return "push: " + _expr->DebugString();
}
/*** Pop (from global stack) Expression ***/
string PopExpr::DebugString()
{
return "pop to: " + _expr->DebugString();
}
/*** Clear Global Stack Expression ***/
string ClearExpr::DebugString()
{
return "clear stack";
}
/*** Loop Expression ***/
string LoopExpr::DebugString()
{
ostringstream ostr;
ostr << "loop: | " << _conditionExpr->DebugString();
ostr << " | then | ";
vector<Expr *>::iterator it;
for (it = _thenExprs.begin(); it != _thenExprs.end(); it++) {
ostr << (*it)->DebugString();
if (*it != _thenExprs.back()) // Add "," between each expression
ostr << ", ";
}
ostr << " | else | ";
for (it = _thelseExprs.begin(); it != _thelseExprs.end(); it++) {
ostr << (*it)->DebugString();
if (*it != _thelseExprs.back()) // Add "," between each expression
ostr << ", ";
}
ostr << " |";
return ostr.str();
}
/*** Unkown Expression ***/
string LengthFuncExpr::DebugString()
{
return "get length of: " + _expr->DebugString();
}
/*** Unkown Expression ***/
string UnkownExpr::DebugString()
{
return "unkown expression" + ((_tk) ? (": \"" + _tk + "\"") : "");
}