-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuinterpreter.pas
301 lines (233 loc) · 8.28 KB
/
uinterpreter.pas
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
unit uinterpreter;
{$mode objfpc}{$H+}
interface
uses Classes, Variants, SysUtils, utypes, uerror;
type Tvariables = array of Variant;
TLPI_Interpreter = class(TLPI_ErrorMessage)
private
variables: Tvariables;
procedure dump_variables(symbols: TStringList);
function var_symbol_to_id(var_symbol: AnsiString; symbols: TStringList): Integer;
public
procedure init_variables(varcount: Integer);
function get_variable(var_symbol: AnsiString; symbols: TStringList): Variant;
procedure set_variable(var_symbol: AnsiString; var_value: Variant; symbols: TStringList);
function evaluate(node: TASTTreeNode): Variant;
procedure execute(rootNode: TASTTreeNode; symbols: TStringList);
end;
implementation
uses math, uutils, uconstants;
procedure TLPI_Interpreter.init_variables(varcount: Integer);
var i: Integer;
begin
variables := Tvariables.Create;
SetLength(variables, varcount);
for i := 0 to High(variables) do
variables[i] := 0;
end;
procedure TLPI_Interpreter.dump_variables(symbols: TStringList);
var i, max_name_length: Integer;
begin
if variables = nil then Exit;
// find longest name
max_name_length := 0;
for i := 0 to High(variables) do
max_name_length := max(max_name_length, Length(symbols[i]));
for i := 0 to High(variables) do
messages.Add(padstring(symbols[i], max_name_length) + ' := ' + AnsiString(variables[i]));
end;
// internal helper function
function TLPI_Interpreter.var_symbol_to_id(var_symbol: AnsiString; symbols: TStringList): Integer;
var i: Integer;
begin
Result := Cunknown_var_id;
for i := 0 to High(variables) do
if LowerCase(symbols[i]) = var_symbol then
begin
Result := i;
Break;
end;
end;
function TLPI_Interpreter.get_variable(var_symbol: AnsiString; symbols: TStringList): Variant;
var id: Integer;
begin
Result := '';
id := var_symbol_to_id(LowerCase(var_symbol), symbols);
if id <> Cunknown_var_id then
Result := variables[id]
else
LogError('Requested Variable ' + var_symbol + ' could not be found!', Cunknown_line, Cunknown_operation);
end;
procedure TLPI_Interpreter.set_variable(var_symbol: AnsiString; var_value: Variant; symbols: TStringList);
var id: Integer;
begin
id := var_symbol_to_id(LowerCase(var_symbol), symbols);
if id <> Cunknown_var_id then
variables[id] := var_value
else
begin
SetLength(variables, Length(variables) + 1); // expand array
variables[High(variables)] := var_value; // set the value
symbols.Add(var_symbol); // add the matching symbol
end;
end;
function TLPI_Interpreter.evaluate(node: TASTTreeNode): Variant;
var i, loopCycles, for_from, for_to, for_var_id: Integer;
temp: Extended;
s: AnsiString;
var_a, var_b: Variant;
begin
Result := 0;
if (node = nil) or (isError) then Exit;
case node.operation of
CopNOP: begin {No Operation...} end;
CopVariable:
Result := variables[node.var_id];
CopNumber:
Result := node.n;
CopString:
Result := node.s;
CopAssign:
variables[node.firstChild.var_id] := evaluate(node.secondChild);
CopCompareEqual:
Result := evaluate(node.firstChild) = evaluate(node.secondChild);
CopCompareUnequal:
Result := evaluate(node.firstChild) <> evaluate(node.secondChild);
CopCompareGreater:
Result := Extended(evaluate(node.firstChild)) > Extended(evaluate(node.secondChild));
CopCompareSmaller:
Result := Extended(evaluate(node.firstChild)) < Extended(evaluate(node.secondChild));
CopCompareGreaterOrEqual:
Result := Extended(evaluate(node.firstChild)) >= Extended(evaluate(node.secondChild));
CopCompareSmallerOrEqual:
Result := Extended(evaluate(node.firstChild)) <= Extended(evaluate(node.secondChild));
CopLogicTrue:
Result := True;
CopLogicFalse:
Result := False;
CopLogicAnd:
Result := Boolean(evaluate(node.firstChild)) and Boolean(evaluate(node.secondChild));
CopLogicOr:
Result := Boolean(evaluate(node.firstChild)) or Boolean(evaluate(node.secondChild));
CopLogicNot:
Result := not Boolean(evaluate(node.firstChild));
CopMathAdd:
begin
var_a := evaluate(node.firstChild);
var_b := evaluate(node.secondChild);
if (varType(var_a) = varString) or (varType(var_b) = varString) then
Result := AnsiString(var_a) + AnsiString(var_b)
else
Result := Extended(var_a) + Extended(var_b);
end;
CopMathSub:
Result := evaluate(node.firstChild) - evaluate(node.secondChild);
CopMathMul:
Result := evaluate(node.firstChild) * evaluate(node.secondChild);
CopMathDiv:
begin
temp := evaluate(node.secondChild);
if temp = 0 then
LogError('Division by Zero', node.line, node.operation)
else
Result := evaluate(node.firstChild) / temp;
end;
CopMathDivInt:
begin
temp := round(StrToFloat(evaluate(node.secondChild)));
if temp = 0 then
LogError('Division by Zero', node.line, node.operation)
else
Result := round(StrToFloat(evaluate(node.firstChild))) div round(temp);
end;
CopMathMod:
begin
temp := round(StrToFloat(evaluate(node.secondChild)));
if temp = 0 then
LogError('Modulo by Zero', node.line, node.operation)
else
Result := round(StrToFloat(evaluate(node.firstChild))) mod round(temp);
end;
CopCommandBlock:
for i := 0 to node.children.Count - 1 do evaluate(node.getChild(i));
CopCondition:
begin
if Boolean(evaluate(node.firstChild)) then
evaluate(node.secondChild)
else
begin
// only execute the else node if it is attached
if node.children.Count > 2 then
evaluate(node.thirdChild);
end;
end;
CopLoopWhile:
begin
loopCycles := 0;
while ((Boolean(evaluate(node.firstChild))) and (loopCycles < ClpiInterpreterMaxLoopCycles) and (not isError)) do
begin
evaluate(node.secondChild);
inc(loopCycles);
end;
if (loopCycles >= ClpiInterpreterMaxLoopCycles) then
LogError('Possible infinite loop. Maximum allowed Cycles: ' + IntToStr(ClpiInterpreterMaxLoopCycles), node.line, CopLoopWhile);
end;
CopLoopRepeatUntil:
begin
loopCycles := 0;
repeat
// Count - 2 because last Child = condition!
for i := 0 to node.children.Count - 2 do evaluate(node.getChild(i));
inc(loopCycles);
until (Boolean(evaluate(node.lastChild))) or (loopCycles >= ClpiInterpreterMaxLoopCycles) or (isError);
if (loopCycles >= ClpiInterpreterMaxLoopCycles) then
LogError('Possible infinite loop. Maximum allowed Cycles: ' + IntToStr(ClpiInterpreterMaxLoopCycles), node.line, CopLoopRepeatUntil);
end;
CopLoopForTo:
begin
for_from := round(StrToFloat(evaluate(node.secondChild)));
for_to := round(StrToFloat(evaluate(node.thirdChild)));
for_var_id := node.firstChild.var_id;
if ((for_to - for_from) >= ClpiInterpreterMaxLoopCycles) then
LogError('Possible infinite loop. Maximum allowed Cycles: ' + IntToStr(ClpiInterpreterMaxLoopCycles), node.line, CopLoopForTo);
for i := for_from to for_to do
begin
variables[for_var_id] := i;
evaluate(node.fourthChild);
end;
end;
CopRound:
Result := round(StrToFloat(evaluate(node.firstChild)));
CopWriteln:
begin
s :='';
for i := 0 to node.children.Count - 1 do
s := s + AnsiString(evaluate(node.getChild(i)));
messages.Add(s);
end;
else
LogError('Unknown Operation ' + operationtostr(node.operation), node.line, node.operation);
end;
end;
procedure TLPI_Interpreter.execute(rootNode: TASTTreeNode; symbols: TStringList);
begin
if ClpiDebugMode then
begin
messages.Add('');
messages.Add('*** Interpreter ***');
end;
if rootNode = nil then Exit;
evaluate(rootNode);
if ClpiDebugMode then
begin
if Length(variables) > 0 then
begin
messages.Add('');
messages.Add('# Variable State Dump');
dump_variables(symbols);
end;
messages.Add('');
messages.Add('# Execution halted.');
end;
end;
end.