-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathemulator-statement.php
388 lines (377 loc) · 10.4 KB
/
emulator-statement.php
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
#TODO: PhpParser\Node\Stmt\StaticVar vs PhpParser\Node\Stmt\Static_
use PhpParser\Node;
/**
* Runs a single statement in the emulator
*/
trait EmulatorStatement
{
/**
* Used to check if loop condition is still valid
* @return boolean
*/
protected function loop_condition($i=0)
{
if ($this->terminated)
return true;
if ($this->return)
return true;
if ($this->break)
{
$this->break--;
return true;
}
if ($this->continue)
{
$this->continue--;
if ($this->continue)
return true;
}
if ($i>$this->infinite_loop)
{
$this->error("Infinite loop");
return true;
}
return false;
}
/**
* Runs a single statement
* If input is a statement, it will be run. If its an expression, it will be runned like an statement.
* @param Node $node
*/
protected function run_statement($node)
{
if ($node instanceof Node\Stmt\Echo_)
foreach ($node->exprs as $expr)
$this->output($this->evaluate_expression($expr));
// $this->output_array($this->evaluate_expression_array($node->exprs));
elseif ($node instanceof Node\Stmt\Function_)
return;
elseif ($node instanceof Node\Stmt\If_)
{
$done=false;
if ($this->evaluate_expression($node->cond))
{
$done=true;
$this->run_code($node->stmts);
}
else
{
if (is_array($node->elseifs))
foreach ($node->elseifs as $elseif)
{
if ($this->evaluate_expression($elseif->cond))
{
$done=true;
$this->run_code($elseif->stmts);
break;
}
}
if (!$done and isset($node->else))
$this->run_code($node->else->stmts);
}
}
elseif ($node instanceof Node\Stmt\Return_)
{
if ($node->expr)
$this->return_value=$this->evaluate_expression($node->expr);
else
$this->return_value=null;
$this->return=true;
return $this->return_value;
}
elseif ($node instanceof Node\Stmt\For_) //Loop 1
{
$i=0;
$this->loop_depth++;
for ($this->run_code($node->init);$this->evaluate_expression($node->cond[0]);$this->run_code($node->loop))
{
$i++;
$this->run_code($node->stmts);
if ($this->loop_condition($i))
break;
}
$this->loop_depth--;
}
elseif ($node instanceof Node\Stmt\While_) //Loop 2
{
$i=0;
$this->loop_depth++;
while ($this->evaluate_expression($node->cond))
{
$i++;
$this->run_code($node->stmts);
if ($this->loop_condition($i))
break;
}
$this->loop_depth--;
}
elseif ($node instanceof Node\Stmt\Do_) //Loop 3
{
$i=0;
$this->loop_depth++;
do
{
$i++;
$this->run_code($node->stmts);
if ($this->loop_condition($i))
break;
}
while ($this->evaluate_expression($node->cond));
$this->loop_depth--;
}
elseif ($node instanceof Node\Stmt\Foreach_) //Loop 4
{
//php 5.5+ support byref even if the expression is not byref itself (via a temporary replacement)
//TODO: test all foreach compilations with list
$byref=$node->byRef;
if ($node->expr instanceof Node\Expr\Variable and $byref)
$list=&$this->variable_reference($node->expr);
else
$list=$this->evaluate_expression($node->expr);
$keyed=false;
//OO code here, to prevent double evaluation of list
if ($list instanceof EmulatorObject)
$list=$list->properties;
if (isset($node->keyVar))
{
$keyed=true;
if (!$this->variable_isset($node->keyVar))
$this->variable_set($node->keyVar);
$keyVar=&$this->variable_reference($node->keyVar);
}
// if (!$this->variable_isset($node->valueVar))
// $this->variable_set($node->valueVar);
// $valueVar=&$this->variable_reference($node->valueVar);
$this->loop_depth++;
if ($this->loop_condition())
return null; #if already terminated die
if ($byref)
foreach ($list as $k=>&$v)
{
if ($keyed)
$keyVar=$k;
$this->variable_set_byref($node->valueVar,$v);
$this->run_code($node->stmts);
if ($this->loop_condition())
break;
}
else
foreach ($list as $k=>$v)
{
if ($keyed)
$keyVar=$k;
$this->variable_set($node->valueVar,$v);
$this->run_code($node->stmts);
if ($this->loop_condition())
break;
}
$this->loop_depth--;
}
elseif ($node instanceof Node\Stmt\Declare_)
{
//TODO: handle tick function here, by wrapping it
$data=[];
$code="declare(";
foreach ($node->declares as $declare)
{
$data[$declare->key]=$this->evaluate_expression($declare->value);
$code.="{$declare->key}='".$this->evaluate_expression($declare->value)."',";
}
$code=substr($code,0,-1).");";
eval($code);
}
elseif ($node instanceof Node\Stmt\Switch_)
{
$arg=$this->evaluate_expression($node->cond);
$condition=false;
foreach ($node->cases as $case)
{
if ($case->cond===NULL /* default case*/ or $this->evaluate_expression($case->cond)==$arg)
$condition=true; //run all cases from now forward, until break.
if ($condition)
$this->run_code($case->stmts);
if ($this->loop_condition())
break;
}
}
elseif ($node instanceof Node\Stmt\Break_)
{
if (isset($node->num))
$this->break+=$this->evaluate_expression($node->num);
else
$this->break++;
}
elseif ($node instanceof Node\Stmt\Continue_)
{
//basically, continue 3 means break 2 inner loops and continue on the outer loop
if (isset($node->num))
$num=$this->evaluate_expression($node->num);
else
$num=1;
$this->continue+=$num;
}
elseif ($node instanceof Node\Stmt\Unset_)
{
foreach ($node->vars as $var)
$this->variable_unset($var);
}
elseif ($node instanceof Node\Stmt\Throw_)
{
$this->throw_exception($this->evaluate_expression($node->expr));
}
elseif ($node instanceof Node\Stmt\TryCatch)
{
$framecount=count($this->trace);
$this->try++;
try {
$this->verbose("Starting a Try block (depth:{$this->try})...\n",2);
$this->run_code($node->stmts);
$this->verbose("Ending a Try block without error (depth:{$this->try})...\n",3);
}
catch (Exception $e)
{
$diff=count($this->trace)-$framecount;
if ($diff>0)
{
$this->verbose("Exception of type '".get_class($e)."' cautght, restoring context...\n",2);
for ($i=0;$i<$diff;++$i)
{
$this->context_restore();
array_pop($this->trace);
array_pop($this->variable_stack);
}
$this->reference_variables_to_stack();
$this->verbose("Context restored prior to running catch block ({$diff} stack frames).\n",3);
}
else
$this->verbose("Exception of type '".get_class($e)."' caught, and no context restoration needed.\n",2);
$catch_found=false;
$this->try--; //no longer in the try
$this->verbose("Attempting to find matchin Catch block...\n",3);
foreach ($node->catches as $catch)
{
//each has type, the exception type, var, the exception variable, and stmts
$type=$this->name($catch->type);
if ($e instanceof EmulatedException and $this->is_a($e->object,$type)) //user-defined exception
{
$this->verbose("Catch block (user-defined exception type) found, executing...\n",4);
$this->variable_set($catch->var,$e->object);
$this->run_code($catch->stmts);
$catch_found=true;
break;
}
elseif ($e instanceof $type)
{
$this->verbose("Catch block found, executing...\n",4);
$this->variable_set($catch->var,$e);
$this->run_code($catch->stmts);
$catch_found=true;
break;
}
}
if ($catch_found)
$this->verbose("Catch block complete.\n",3);
else
{
$this->verbose("Could not find any matching catch block, throwing error for further catching...\n",3);
$this->throw_exception($e);
}
$this->try++; //balance off with the one below
}
#TODO: handle finally
$this->try--;
}
elseif ($node instanceof Node\Stmt\Static_)
{
if (end($this->trace)->type==="" and isset($this->functions[strtolower(end($this->trace)->function)])) //statc inside a function
{
$statics=&$this->functions[strtolower($this->current_function)]->statics;
foreach ($node->vars as $var)
{
$name=$this->name($var->name);
if (!array_key_exists($name,$statics))
$statics[$name]=$this->evaluate_expression($var->default);
$this->variables[$name]=&$statics[$name];
}
}
else
{
#TODO:
$this->todo("Global statics not yet supported");
}
}
elseif ($node instanceof Node\Stmt\InlineHTML)
$this->output($node->value);
elseif ($node instanceof Node\Stmt\Global_)
{
foreach ($node->vars as $var)
{
$name=$this->name($var->name);
$globals_=&$this->variable_reference("GLOBALS");
if (!isset($globals_[$name]))
$globals_[$name]=null; //create
// $this->verbose("Aliasing global variable '\${$name}'...\n",4);
$this->variables[$name]=&$globals_[$name];
}
}
elseif ($node instanceof Node\Stmt\Namespace_)
{
//namespaces are not nested, and if used, there is no code outside namespace
//so all of this files is namespace statement(s)
$this->current_namespace=$this->name($node);
$this->current_active_namespaces=[];
$this->verbose("Changing namespace to '{$this->current_namespace}'...\n",2);
$this->run_code($node->stmts);
}
elseif ($node instanceof Node\Stmt\Const_) //constants are not declared ahead of time, they are inline
{
#constant definition:
foreach ($node->consts as $const)
$this->constant_set($const->name,$this->evaluate_expression($const->value));
}
elseif ($node instanceof Node\Stmt\Use_)
;
elseif ($node instanceof Node\Expr)
$this->evaluate_expression($node);
else
{
$this->error("Unknown node type: ",$node);
}
}
function constant_exists($name)
{
if (defined($name)) return true;
$fqname=$this->namespaced_name($name);
return (array_key_exists($fqname, $this->constants))
or (array_key_exists($name, $this->constants));
}
function constant_get($name)
{
if (defined($name))
return constant($name);
$fqname=$this->namespaced_name($name);
if (array_key_exists($fqname, $this->constants))
return $this->constants[$fqname];
elseif (array_key_exists($name, $this->constants))
return $this->constants[$name];
else
{
if (is_string($fqname))
{
$this->notice("Use of undefined constant {$fqname} - assumed '{$fqname}'");
return $fqname;
}
else
$this->error("Undefined constant {$fqname}");
}
}
function constant_set($name,$value)
{
$index=$this->current_namespace($name);
if (array_key_exists($index,$this->constants))
$this->notice("Constant {$index} already defined");
else
$this->constants[$index]=$value;
}
}