-
Notifications
You must be signed in to change notification settings - Fork 6
/
traverse_color.php
executable file
·830 lines (743 loc) · 24.4 KB
/
traverse_color.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
#!/usr/bin/env php
<?php
require './vendor/autoload.php';
use PhpParser\Error;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
class TaintInfo {
public $value = 0;
public $certainty = 1;
function __construct($v, $c) {
$this->value = $v;
$this->certainty = $c;
}
}
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$traverser = new NodeTraverser;
$prettyPrinter = new PrettyPrinter\Standard;
$log = fopen('php://stderr','a');
function pp($msg) {
global $log;
if (gettype($msg) == 'array' || gettype($msg) == 'object') {
fwrite($log, var_dump($msg).PHP_EOL);
}
else {
fwrite($log, $msg.PHP_EOL);
}
}
$code = file_get_contents("php://stdin");
// parse
$stmts = $parser->parse($code);
$tainted_vars = [];
$alias_map = [];
$sources['input'] = ['_GET'=>1, '_POST'=>1, '_COOKIE'=>1, '_ENV'=>1];
$sources['database'] = ['file_get_contents'=>1, 'mysql_fetch_row'=>1];
//$sources['method'] = ['mysqli::query'=>1];
$sinks['sql'] = ['pg_query'=>1, 'mysql_query'=>1, 'mysqli::query'=>1];
$sinks['cmd'] = ['system'=>1];
$sinks['xss'] = ['print_'=>1];
$user_funcs = []; // a map of user defined functions
$sani_funcs['sql'] = ['escape_sql_string'=>1];
$sani_funcs['cmd'] = ['escapeshellcmd'=>1];
$sani_funcs['xss'] = ['htmlspecialchars'=>1];
$cond_mode = 0;
/* construct funcs dict */
foreach($stmts as $stmt) {
if ($stmt instanceof Node\Stmt\Function_) {
$user_funcs[$stmt->name] = $stmt;
}
}
do_statements($stmts, $tainted_vars);
pp($code);
//pp($tainted_vars);
print_stmts($stmts);
fclose($log);
function get_left_side_name($expr) {
if ($expr instanceof Node\Expr\Variable) {
return $expr->name;
}
else if ($expr instanceof Node\Expr\ArrayDimFetch) {
//return $expr->var->name . $expr->dim->value;
return $expr->var->name;
}
else if ($expr instanceof Node\Expr\PropertyFetch) {
return $expr->var->name."::".$expr->name;
}
else if ($expr instanceof Node\Arg) {
//return $expr->var->name . $expr->dim->value;
return $expr->value->name;
}
else if ($expr instanceof Node\Param) {
return $expr->name;
}
else {
echo "unsupported left side value\n";
//pp($expr);
}
}
function deep_copy_assoc_arr($arr) {
$newarray = [];
foreach ($arr as $k=>$v) {
$newarray[$k] = clone $v;
}
return $newarray;
}
/* deprecated union strategy */
// function union_tables($t1, $t2) {
// foreach($t1 as $e1=>$obj1) {
// if (array_key_exists($e1, $t2)) {
// $obj2 = $t2[$e2];
// $obj2->certainty = max($obj1->certainty, $obj2->certainty);
// }
// else {
// $t2[$e1] = $obj1;
// }
// }
// return $t2;
// }
function union_tables($t1, $t2) {
pp("union tables");
pp($t1);
pp($t2);
$keys1 = array_keys($t1);
$keys2 = array_keys($t2);
$allkeys = array_unique(array_merge($keys1, $keys2));
$newtable = [];
foreach($allkeys as $k) {
$sum = 0;
if (array_key_exists($k, $t1)) {
/* if exists in $t1 and $t2 */
if (array_key_exists($k, $t2)) {
$t1[$k]->certainty += $t2[$k]->certainty;
$newtable[$k] = clone $t1[$k];
}
/* if only exists in $t1 */
else {
$newtable[$k] = $t1[$k];
}
}
/* if only exists in $t2 */
else {
$newtable[$k] = $t2[$k];
}
}
//pp($newtable);
return $newtable;
}
function calc_confidence($cond) {
if ($cond instanceof Node\Scalar\LNumber) {
return $cond->value == true;
}
else if ($cond instanceof Node\Expr\ConstFetch) {
$name = $cond->name->parts[0];
if ($name == "true") {
return 1;
}
else if ($name == "false") {
return 0;
}
else {
return 1;
}
}
else {
return 0.5;
}
//pp($cond);
}
function augment_table($out, $in, $confidence) {
// global $cond_mode;
// $confidence = 1;
// if ($cond_mode > 0) {
// $confidence = 0.5;
// }
foreach ($in as $k=>$v) {
if (!array_key_exists($k, $out)) {
$v->certainty *= $confidence;
$out[$k] = $v;
pp("add $k, certainty: $v->certainty");
}
}
foreach ($out as $k=>$v) {
if (!array_key_exists($k, $in)) {
$v->certainty -= $v->certainty*$confidence;
pp("update $k, certainty: $v->certainty");
}
if ($v->certainty == 0) {
unset($out[$k]);
pp("unset $k");
}
}
return $out;
}
function do_assign($left, $right, &$sym_table) {
$left_name = get_left_side_name($left);
$taint_info = eval_expr($right, $sym_table);
if ($taint_info->value > 0) {
$left->setAttribute("tainted", clone $taint_info);
set_var($left,$sym_table);
// if ($right instanceof Node\Expr\ArrayDimFetch) {
// $left->setAttribute("tainted", new TaintInfo($taint_info->value, $taint_info->certainty/2));
// set_var($left, $sym_table);
// }
// else {
// $left->setAttribute("tainted", $taint_info);
// set_var($left,$sym_table);
// }
pp("set $left_name");
}
else {
$target = get_alias($left_name);
if (get_var($target, $sym_table)->value != 0) {
unset($sym_table[$target]);
pp("unset $left_name");
}
}
return $taint_info;
}
function do_statements($func_stmts, &$sym_table) {
global $cond_mode;
/* only consider assign for now */
foreach ($func_stmts as $stmt) {
// if ($stmt instanceof Node\Expr\Assign) {
// pp("process assign...");
// //pp($stmt->var);
// $taint_info = eval_expr($stmt->expr, $sym_table);
// $left = get_left_side_name($stmt->var);
// if ($taint_info->value > 0) {
// /* should also add class */
// if ($stmt->expr instanceof Node\Expr\ArrayDimFetch) {
// $sym_table[$left] = new TaintInfo($taint_info->value, $taint_info->certainty/2);
// }
// else {
// $sym_table[$left] = $taint_info;
// }
// pp("add {$left}");
// }
// else if (check_var($left, $sym_table)->value != 0) {
// /* delete entry in sym_table */
// //if ($cond_mode == 0) {
// unset($sym_table[$left]);
// pp("unset {$left}");
// //}
// }
// else {
// /* pass */
// }
// }
if ($stmt instanceof Node\Expr) {
pp("process expr...");
eval_expr($stmt, $sym_table);
}
else if ($stmt instanceof Node\Stmt\Function_) {
pp("process function declaration");
/* skip declare statement */
continue;
}
/* ignore ifelse for now */
else if ($stmt instanceof Node\Stmt\If_) {
// $out_table = $sym_table;
// $confid = calc_confidence($stmt->cond);
// $cond_mode += 1;
// /* if branch */
// do_statements($stmt->stmts, $sym_table);
// $table1 = $sym_table;
// /* else branch */
// if (is_null($stmt->else) != true) {
// do_statements($stmt->else->stmts, $sym_table);
// $table2 = $sym_table;
// $sym_table = union_tables($table1, $table2);
// }
// $cond_mode -= 1;
// $sym_table = augment_table($out_table, $sym_table, 0.5);
$out_table = $sym_table;
$confid = calc_confidence($stmt->cond);
if (is_null($stmt->else) == true || $confid == 1) {
/* if branch */
do_statements($stmt->stmts, $sym_table);
$sym_table = augment_table($out_table, $sym_table, $confid);
}
else {
if ($confid == 0) {
do_statements($stmt->else->stmts, $sym_table);
$sym_table = augment_table($out_table, $sym_table, $confid);
}
else {
$table1 = deep_copy_assoc_arr($sym_table);
$table2 = deep_copy_assoc_arr($sym_table);
do_statements($stmt->stmts, $table1);
do_statements($stmt->else->stmts, $table2);
$sym_table = union_tables($table1, $table2);
pp("out table:");
pp($out_table);
pp("inner:");
pp($sym_table);
$sym_table = augment_table($out_table, $sym_table, $confid);
}
}
}
else if ($stmt instanceof Node\Stmt\While_) {
$confid = calc_confidence($stmt->cond);
$out_table = $sym_table;
do_statements($stmt->stmts, $sym_table);
$sym_table = augment_table($out_table, $sym_table, $confid);
// /* condition is always true, not condition mode for while */
// if ($is_cond_true) {
// do_statements($stmt->stmts, $sym_table);
// }
// else {
// $cond_mode += 1;
// do_statements($stmt->stmts, $sym_table);
// $cond_mode -= 1;
// }
}
else if ($stmt instanceof Node\Stmt\Return_) {
pp("process return");
return eval_expr($stmt->expr, $sym_table);
}
else {
//pp("process unsupported statement");
echo "unsupported statement type ".get_class($stmt)."\n";
}
//$traverser->traverse(array($stmt));
}
//pp($sym_table);
return new TaintInfo(0, 1);
}
function gen_callee_table($args, $func_proto, $caller_table) {
$newtable = [];
$params = $func_proto->params;
for ($i = 0; $i < count($params); $i++) {
$left = $params[$i];
$right = $args[$i];
$taint_info = eval_expr($right, $caller_table);
$var_name = get_left_side_name($left);
if ($taint_info->value > 0) {
pp("add {$var_name}");
if ($right instanceof Node\Expr\ArrayDimFetch) {
$newtable[$var_name] = new TaintInfo($taint_info->value, $taint_info->certainty/2);
}
else {
$newtable[$var_name] = $taint_info;
}
}
else {
}
}
return $newtable;
}
function get_alias($name) {
global $alias_map;
$target = $name;
while (array_key_exists($target, $alias_map)) {
$target = $alias_map[$target];
}
pp("$name's alias is $target...");
return $target;
}
function get_var($name, $sym_table) {
$target = get_alias($name);
if (array_key_exists($target, $sym_table)) {
return $sym_table[$target];
}
else {
return new TaintInfo(0, 1);
}
}
function set_var($left, &$sym_table) {
$name = get_left_side_name($left);
$target = get_alias($name);
$sym_table[$target] = clone $left->getAttribute("tainted");
}
// function unset_var($name, &$sym_table) {
// $target = get_alias($name);
// unset($sym_table[$target]);
// }
function is_sink($func_name) {
global $sinks;
if (array_key_exists($func_name, $sinks['sql'])) {
return 1;
}
else if (array_key_exists($func_name, $sinks['cmd'])) {
return 2;
}
else if (array_key_exists($func_name, $sinks['xss'])) {
return 4;
}
else {
return false;
}
}
function is_args_tainted($args, $sym_table) {
foreach($args as $arg) {
$taint_value = eval_expr($arg, $sym_table);
if ($taint_value->value != 0) {
return $taint_value;
}
}
return new TaintInfo(0, 1);
}
function is_source($name) {
global $sources;
if (array_key_exists($name, $sources['input'])) {
return 1;
}
else if (array_key_exists($name, $sources['database'])) {
return 2;
}
// else if (array_key_exists($name, $sources['method'])) {
// return 4;
// }
else {
return 0;
}
}
function is_sanitize($name) {
global $sani_funcs;
if (array_key_exists($name, $sani_funcs['sql'])) {
return 1;
}
else if (array_key_exists($name, $sani_funcs['cmd'])) {
return 2;
}
else {
return 0;
}
}
function get_vul_type($source, $sink) {
if ($source == 1 && ($sink == 1 || $sink == 2)) {
return -$sink;
}
else if ($source == 2 && $sink == 4) {
return -$sink;
}
else {
return 0;
}
}
function eval_func($func_name, $args, &$sym_table) {
global $user_funcs;
$source_type = is_source($func_name);
$sink_type = is_sink($func_name);
if ($sink_type != 0) {
$taint_info = is_args_tainted($args, $sym_table);
$vul = get_vul_type($taint_info->value, $sink_type);
return new TaintInfo($vul, $taint_info->certainty);
}
else if ($source_type != 0) {
return new TaintInfo($source_type, 1);
}
/* if user defined function */
else if (array_key_exists($func_name, $user_funcs)) {
$func_proto = $user_funcs[$func_name];
$callee_table = gen_callee_table($args, $func_proto, $sym_table);
return do_statements($func_proto->stmts, $callee_table);
}
/* if built-in function */
else {
// if (is_sanitize($func_name) > 0) {
// return new TaintInfo(0, 1);
// }
/* special cases */
if ($func_name == "array_push") {
$v = eval_expr($args[1], $sym_table);
$sym_table[get_left_side_name($args[0])] = $v;
$args[0]->setAttribute("tainted", $v);
return $v;
}
else {
$info = clone is_args_tainted($args, $sym_table);
$taint_type = $info->value;
$sani_type = is_sanitize($func_name);
if ($taint_type > 0) {
$vul = get_vul_type($taint_type, $sani_type);
if ($vul != 0) {
return new TaintInfo(0, 1);
}
else {
return $info;
}
}
else {
return new TaintInfo(0, 1);
}
}
}
}
function eval_expr($expr, &$sym_table) {
$expr_type = get_class($expr);
if ($expr instanceof Node\Expr\Variable) {
pp("evaluate var {$expr->name}...");
$expr->setAttribute("tainted", clone get_var($expr->name, $sym_table));
}
else if ($expr instanceof Node\Scalar\LNumber) {
pp("evaluate lnumber {$expr->value}...");
$expr->setAttribute("tainted", new TaintInfo(0, 1));
}
else if ($expr instanceof Node\Scalar\String_) {
pp("evaluate string {$expr->value}...");
$expr->setAttribute("tainted", new TaintInfo(0, 1));
}
else if ($expr instanceof Node\Expr\ArrayDimFetch) {
pp("evaluate arraydimfetch...");
//pp($expr);
/* set certainty to 2 since array fetch will lose half certainty */
if (is_source($expr->var->name)) {
$expr->setAttribute("tainted", new TaintInfo(1, 1));
}
else {
$info = eval_expr($expr->var, $sym_table);
$expr->setAttribute("tainted", new TaintInfo($info->value, $info->certainty/2));
//pp($expr);
}
}
else if ($expr instanceof Node\Expr\PropertyFetch) {
pp("evaluate propertyfetch...");
$name = get_left_side_name($expr);
$expr->setAttribute("tainted", clone get_var($name, $sym_table));
}
else if ($expr instanceof Node\Expr\BinaryOp) {
pp("evaluate binaryOp $expr_type...");
$left_v = eval_expr($expr->left, $sym_table);
$right_v = eval_expr($expr->right, $sym_table);
if ($left_v->value) {
$expr->setAttribute("tainted", clone $left_v);
}
else if ($right_v->value) {
$expr->setAttribute("tainted", clone $right_v);
}
else {
$expr->setAttribute("tainted", new TaintInfo(0, 1));
}
}
else if ($expr instanceof Node\Scalar\Encapsed) {
pp("evaluate binaryOp $expr_type...");
$is_set = false;
foreach($expr->parts as $part) {
if (eval_expr($part, $sym_table)) {
$expr->setAttribute("tainted", new TaintInfo(1, 1));
$is_set = true;
break;
}
}
if (!$is_set) {
$expr->setAttribute("tainted", new TaintInfo(0, 1));
}
}
else if ($expr instanceof Node\Scalar\EncapsedStringPart) {
pp("evaluate EncapsedStringPart $expr->value...");
$expr->setAttribute("tainted", new TaintInfo(0, 1));
}
else if ($expr instanceof Node\Expr\FuncCall) {
pp("evaluate funcCall {$expr->name->parts[0]}...");
$func_name = $expr->name->parts[0];
$v = eval_func($func_name, $expr->args, $sym_table);
$expr->setAttribute("tainted", clone $v);
}
else if ($expr instanceof Node\Expr\MethodCall) {
$method_name = "{$expr->var->name}::$expr->name";
pp("evaluate methodCall $method_name...");
$v = eval_func($method_name, $expr->args, $sym_table);
$expr->setAttribute("tainted", $v);
}
else if ($expr instanceof Node\Arg) {
pp("evaluate arg $expr_type");
$v = eval_expr($expr->value, $sym_table);
$expr->setAttribute("tainted", clone $v);
}
else if ($expr instanceof Node\Expr\Array_) {
$expr->setAttribute("tainted", new TaintInfo(0, 1));
}
else if ($expr instanceof Node\Expr\Assign) {
return do_assign($expr->var, $expr->expr, $sym_table);
}
else if ($expr instanceof Node\Expr\AssignRef) {
return do_assignref($expr->var, $expr->expr, $sym_table);
}
else {
echo "unsupported expr type: $expr_type\n";
pp($expr);
}
$return_info = $expr->getAttribute("tainted");
pp("return ".$return_info->value);
//pp($return_info);
if ($return_info->value < 0) {
$percent_certainty = round($return_info->certainty * 100 ) . '%';
if ($return_info->value == -1) {
echo "SQL injection vulnerability found in line {$expr->getline()}, certainty: {$percent_certainty}\n";
}
else if ($return_info->value == -2) {
echo "Command line injection vulnerability found in line {$expr->getline()}, certainty: {$percent_certainty}\n";
}
else if ($return_info->value == -4) {
echo "Persistent XSS vulnerability found in line {$expr->getline()}, certainty: {$percent_certainty}\n";
}
else {
echo "Other type of vulnerability found in line {$expr->getline()}, certainty: {$percent_certainty}\n";
}
}
return $expr->getAttribute("tainted");
}
function do_assignref($left, $right, $sym_table) {
global $alias_map;
$alias_map[get_left_side_name($left)] = $right->name;
return get_var($right->name, $sym_table);
}
/* all types of taint will have the same color, certainty is indicated by opacity */
function set_color($color, $opacity) {
if ($color == "red") {
if ($opacity >= 1) {
echo "\033[1;31m";
}
else {
echo "\033[0;31m";
}
}
else {
echo "do not support other colors";
}
}
function unset_color() {
echo "\033[0m";
}
function print_binary_op($expr) {
if ($expr instanceof Node\Expr\BinaryOp\Concat) {
echo " . ";
}
else if ($expr instanceof Node\Expr\BinaryOp\Plus) {
echo " + ";
}
else if ($expr instanceof Node\Expr\BinaryOp\Minus) {
echo " - ";
}
else {
echo "op not supported: {$expr->gettype()}";
}
}
function print_expr($expr) {
$info = $expr->getAttribute("tainted");
if ($expr instanceof Node\Expr\Variable) {
if (!is_null($info) && $info->value > 0) {
set_color("red", $info->certainty);
}
echo "$". $expr->name;
unset_color();
}
else if ($expr instanceof Node\Scalar\LNumber) {
echo $expr->value;
}
else if ($expr instanceof Node\Scalar\String_) {
echo '"'.$expr->value.'"';
}
else if ($expr instanceof Node\Expr\ArrayDimFetch) {
if (!is_null($info) && $info->value > 0) {
set_color("red", $info->certainty);
}
echo "$".$expr->var->name."[...];\n";
unset_color();
}
else if ($expr instanceof Node\Expr\PropertyFetch) {
echo "propertyfetch";
}
else if ($expr instanceof Node\Expr\BinaryOp) {
print_expr($expr->left);
print_binary_op($expr);
// pp($expr);
print_expr($expr->right);
}
// else if ($expr instanceof Node\Scalar\Encapsed) {
// /* to be filled */
// }
// else if ($expr instanceof Node\Scalar\EncapsedStringPart) {
// pp("evaluate EncapsedStringPart $expr->value...");
// $expr->setAttribute("tainted", new TaintInfo(0, 1));
// }
else if ($expr instanceof Node\Expr\FuncCall) {
$func_name = $expr->name->parts[0];
echo $func_name."(";
//pp($expr);
$args_name = [];
ob_start();
foreach($expr->args as $arg) {
print_expr($arg);
}
$buf = ob_get_contents();
ob_end_clean();
echo substr($buf, 0, -6); //-6 is a very subtle number in this case
unset_color();
echo ");\n";
}
// else if ($expr instanceof Node\Expr\MethodCall) {
// $method_name = "{$expr->var->name}::$expr->name";
// pp("evaluate methodCall $method_name...");
// $v = eval_func($method_name, $expr->args, $sym_table);
// $expr->setAttribute("tainted", $v);
// }
else if ($expr instanceof Node\Arg) {
if (!is_null($info) && $info->value > 0) {
set_color("red", $info->certainty);
}
echo "$" . $expr->value->name. ", ";
unset_color();
//$expr->value->name;
}
else if ($expr instanceof Node\Expr\Array_) {
echo "array();\n";
}
else if ($expr instanceof Node\Expr\Assign) {
print_expr($expr->var);
echo " = ";
print_expr($expr->expr);
}
// else if ($expr instanceof Node\Expr\AssignRef) {
// return do_assignref($expr->var, $expr->expr, $sym_table);
// }
else {
echo "unsupported expr type\n";
//pp($expr);
}
}
function print_stmts($stmts) {
foreach($stmts as $stmt) {
if ($stmt instanceof Node\Expr) {
print_expr($stmt);
}
// else if ($stmt instanceof Node\Stmt\Function_) {
// pp("process function declaration");
// /* skip declare statement */
// continue;
// }
// /* ignore ifelse for now */
// else if ($stmt instanceof Node\Stmt\If_) {
// $out_table = $sym_table;
// $cond_mode += 1;
// do_statements($stmt->stmts, $sym_table);
// $table1 = $sym_table;
// if (is_null($stmt->else) != true) {
// do_statements($stmt->else->stmts, $sym_table);
// $table2 = $sym_table;
// $sym_table = union_tables($table1, $table2);
// }
// $cond_mode -= 1;
// $sym_table = augment_table($out_table, $sym_table, 0.5);
// }
// else if ($stmt instanceof Node\Stmt\While_) {
// $confid = calc_confidence($stmt->cond);
// $out_table = $sym_table;
// do_statements($stmt->stmts, $sym_table);
// $sym_table = augment_table($out_table, $sym_table, $confid);
// }
// else if ($stmt instanceof Node\Stmt\Return_) {
// pp("process return");
// return eval_expr($stmt->expr, $sym_table);
// }
else {
//pp("process unsupported statement");
echo "unsupported statement type ".get_class($stmt)."\n";
}
}
}
?>