-
Notifications
You must be signed in to change notification settings - Fork 31
/
pharen.php
4261 lines (3690 loc) · 136 KB
/
pharen.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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if(version_compare(phpversion(), "5.4") < 0){
error_reporting(E_ALL);
}else{
error_reporting(E_ALL ^ E_STRICT);
}
define("COMPILER_SYSTEM", dirname(__FILE__));
define("EXTENSION", ".phn");
require_once(COMPILER_SYSTEM."/lang.php");
require_once(COMPILER_SYSTEM."/debug.php");
require_once(COMPILER_SYSTEM."/lib/sequence.php");
use pharen\Debug;
use Pharen\Lexical as Lexical;
// Some utility functions for use in Pharen
function is_assoc($xs){
return array_keys($xs) !== range(0, count($xs)-1);
}
function split_body_last($xs){
$body = array_slice($xs, 0, -1);
$last = $xs[count($xs)-1];
return array($body, $last);
}
class Annotation {
static $primitives = array('int', 'float', 'string', 'boolean');
public $typename;
public $var;
public $value_type;
public function __construct($typename, $var, $value_type){
$this->typename = $typename;
$this->var = $var;
$this->value_type = $value_type;
}
public function __toString(){
if($this->value_type){
return "<{$this->typename}:{$this->value_type}>";
}else{
return "<{$this->typename}>";
}
}
public function is_primitive(){
$typename = TypeSig::is_exclusive($this->typename) ?
substr($this->typename, 0, -strlen('__exclam'))
: $this->typename;
return in_array($typename, self::$primitives);
}
public function get_php_typename(){
if($this->is_primitive()){
return "";
}else{
return $this->typename;
}
}
}
class TypeSig {
const SAME_VALTYPE = 3;
const SAME_TYPE = 2;
const EMPTY_SIG = 1;
const ANY_MATCH = 0;
public $any = True;
public $exclusive = False;
public $annotations = array();
public static function match_annotations($ann1, $ann2){
$typesig1 = new Typesig;
$typesig1->add_type($ann1);
$typesig2 = new TypeSig;
$typesig2->add_type($ann2);
return $typesig1->match($typesig2);
}
public static function is_exclusive($typename){
return substr($typename, -strlen('__exclam')) === '__exclam';
}
public function __toString(){
$str = "";
foreach($this->annotations as $ann){
if(is_string($ann)){
$str .= "<Any> ";
}else if(is_object($ann)){
$str .= "$ann ";
}
}
return $str;
}
public function add_type($ann){
$ann = $ann === Null ? "Any" : $ann;
if($ann !== "Any"){
$this->any = False;
if(self::is_exclusive($ann->typename)){
$this->exclusive = True;
}
}
array_push($this->annotations, $ann);
}
public function interface_chain_score($subclass, $ancestor){
$reflection = new ReflectionClass($subclass);
$chainlen = 0;
while(!$reflection->implementsInterface($ancestor)){
$chainlen++;
$reflection = $reflection->getParentClass();
}
return 1/pow(2, $chainlen);
}
public function class_chain_score($subclass, $ancestor){
$chainlen = 1;
while($parent=get_parent_class($subclass) !== $ancestor){
$chainlen++;
$subclass = $parent;
}
return 1/pow(2, $chainlen);
}
public function calc_chain_score($subclass, $ancestor){
if(interface_exists($ancestor)){
return $this->interface_chain_score($subclass, $ancestor);
}else{
return $this->class_chain_score($subclass, $ancestor);
}
}
public function match(TypeSig $other){
// thislen is usually the arguments type sig
// otherlen is usually the func parameters type sig
$thisanns = $this->annotations;
$otheranns = $other->annotations;
$thislen = count($thisanns);
$otherlen = count($otheranns);
if($thislen === 0 && $otherlen === 0) return self::EMPTY_SIG;
if($thislen < $otherlen) return 0;
if($thislen > $otherlen){
for($x=$otherlen; $x < $thislen; $x++){
$otheranns[$x] = "Any";
}
}
$score = 0;
for($x=0; $x<$thislen; $x++){
$thistype = $thisanns[$x];
$thisname = Null;
$othertype = $otheranns[$x];
$othername = Null;
$is_exclusive = False;
if(is_object($thistype)){
$thisname = $thistype->typename;
if(self::is_exclusive($thisname)){
$is_exclusive = True;
$thisname = substr($thisname, 0, -strlen('__exclam'));
}
}
if(is_object($othertype)) $othername = $othertype->typename;
if($othertype === "Any"){
if($is_exclusive){
return False;
}else{
$score += self::ANY_MATCH;
}
}else if($othertype->value_type){
if($thistype->value_type !== $othertype->value_type){
return False;
}else{
$score += self::SAME_VALTYPE;
}
}else if($thisname === $othername){
$score += self::SAME_TYPE;
}else if(is_subclass_of($thisname, $othername)){
$chain_score = $this->calc_chain_score($thisname, $othername);
$score += $chain_score;
}else{
return False;
}
}
return $score;
}
}
class FuncPlaceHolder{
public $typesig;
public $return_type;
public $linenum;
public function __construct($name){
$reflection = new ReflectionFunction($name);
$linenum = $reflection->getStartLine();
}
}
class CompileError extends Exception {
public $linenum;
public function __construct($msg, $line){
$this->linenum = $line;
parent::__construct($msg);
}
}
class FuncCallTypeError extends CompileError{
public function __construct($func_name, $func_typesig, $arg_typesig, $func_line, $call_line){
if($func_line){
$defined = "(defined on line $func_line)";
}else{
$defined = "";
}
parent::__construct(
"Wrong argument type signature for: $func_name $defined\n"
. "\tExpecting: $func_typesig\n"
. "\tGiven: $arg_typesig",
$call_line);
}
}
class ExclusiveUnhandledError extends CompileError{
public function __construct($func_name, $typesig, $func_line, $call_line){
if($func_line){
$defined = "(defined on $func_line)";
}else{
$defined = "";
}
parent::__construct(
"Passing exclusive type signature to untyped function: $func_name $defined\n"
. "\tExpecting: Unable to determine\n"
. "\tGiven: $typesig\n",
$call_line);
}
}
class FuncReturnTypeError extends CompileError{
public function __construct($func_name, $specified, $inferred, $func_line, $call_line){
if($func_line){
$defined = "(defined on $func_line)";
}else{
$defined = "";
}
parent::__construct(
"Incompatible return type found for: $func_name $defined\n"
. "\tSpecified: $specified\n"
. "\tInferred: $inferred",
$call_line);
}
}
class AnnotationTypeError extends CompileError{
public function __construct($existing_ann, $new_ann, $varname, $call_line){
parent::__construct(
"Incompatible specified annotation for: $varname\n"
. "\tExisting: $existing_ann\n"
. "\tSpecified with (ann): $new_ann",
$call_line);
}
}
class Token implements IPharenComparable, IPharenHashable{
public $value;
public $quoted;
public $unquoted;
public $unquote_spliced;
public $linenum;
public function __construct($value=null){
$this->value = $value;
}
public function append($ch){
$this->value .= $ch;
}
public function __toString(){
return $this->value;
}
public function eq($other){
if(is_object($other)){
if($other instanceof Token){
return $this->value === $other->value;
}
}else{
return $this->value === $other;
}
}
public function hash(){
return $this->value;
}
}
class OpenParenToken extends Token{
public $closer = "CloseParenToken";
}
class CloseParenToken extends Token{
}
class OpenBracketToken extends Token{
public $closer = "CloseBracketToken";
}
class CloseBracketToken extends Token{
}
class OpenBraceToken extends Token{
public $closer = "CloseBraceToken";
}
class CloseBraceToken extends Token{
}
class NumberToken extends Token{
}
class StringToken extends Token{
}
class UnstringToken extends Token{
}
class ListAccessToken extends Token{
}
class NameToken extends Token{
}
class SplatToken extends Token{
}
class ExplicitVarToken extends Token{
}
class ReaderMacroToken extends Token{
}
class CommentToken extends Token{
}
class AnnotationToken extends Token{
private $paren_count = 0;
public function is_open(){
return $this->paren_count === 0;
}
public function append($ch){
if($ch === '('){
$this->paren_count++;
}else{
$this->paren_count--;
}
parent::append($ch);
}
}
class FuncValToken extends Token{
}
class KeywordToken extends Token{
}
class Lexer{
static $keyword_rewrites = array(
'or' => 'pharen-or',
'and' => 'pharen-and',
'list' => 'pharen-list',
'sort' => 'pharen-sort'
);
public $code;
private $char;
private $tok;
private $state = "new-expression";
private $toks = array();
private $escaping = false;
private $i=0;
private $linenum = 1;
public function __toString(){
return "<".__CLASS__.">";
}
public function __construct($code){
$this->code = trim($code);
}
public function in_sexpr_opening(){
$c = count($this->toks);
if($c < 1)
return False;
$prev_tok = $this->toks[$c-1];
return $prev_tok instanceof OpenParenToken || $prev_tok instanceof ReaderMacroToken;
}
public function finished(){
$first_tok = $this->toks[0];
if(!($first_tok instanceof OpenParenToken || $first_tok instanceof OpenBraceToken || $first_tok instanceof OpenBracketToken))
return True;
$open_class = get_class($first_tok);
$close_class = $first_tok->closer;
$open_instances = 0;
$close_instances = 0;
foreach($this->toks as $tok){
if(get_class($tok) === $open_class){
$open_instances++;
}else if(get_class($tok) === $close_class){
$close_instances++;
}
}
return $close_instances >= $open_instances;
}
public function reset(){
$this->i = 0;
$this->code = "";
$this->toks = array();
$this->state = "new-expression";
$this->esaping = False;
$this->char = "";
$this->linenum = 1;
}
public function lex(){
for($this->i=0;$this->i<strlen($this->code);$this->i++){
$this->get_char();
if($this->char === "\n"){
$this->linenum++;
}
$this->lex_char();
$toks_size = sizeof($this->toks);
if($toks_size == 0 or $this->tok !== $this->toks[$toks_size-1]){
$this->tok->linenum = $this->linenum;
$this->toks[] = $this->tok;
}
}
return $this->toks;
}
public function get_char(){
$this->char = $this->code[$this->i];
}
public function lex_char(){
if($this->state == "string"){
if($this->char == '"' && !$this->escaping){
$this->state = "new-expression";
}else if($this->char == "\\" && !$this->escaping && $this->code[$this->i+1] == '"'){
$this->escaping = True;
$this->tok->append($this->char);
}else if($this->char == "\\" && !$this->escaping && $this->code[$this->i+1] == "\\"){
$this->escaping = True;
$this->tok->append($this->char);
}else{
if($this->escaping){
$this->escaping = False;
}
$this->tok->append($this->char);
}
}else if($this->char == ";"){
$this->state = "comment";
$this->tok = new CommentToken;
}else if($this->state == "comment"){
if($this->char == "\n" or $this->char == "\r"){
$this->state = "new-expression";
}else{
$this->tok->append($this->char);
}
}else if($this->state == "append"){
if(trim($this->char) === "" or $this->char === ","){
if($this->tok instanceof AnnotationToken && $this->tok->is_open()){
$this->tok->append($this->char);
}else{
$this->state = "new-expression";
}
}else if($this->char == ")"){
if($this->tok instanceof AnnotationToken && $this->tok->is_open()){
$this->tok->append($this->char);
}else{
$this->tok = new CloseParenToken;
}
$this->state = "new-expression";
}else if($this->char == "]"){
$this->tok = new CloseBracketToken;
$this->state = "new-expression";
}else if($this->char == '}'){
$this->tok = new CloseBraceToken;
$this->state = "new-expression";
}else{
$this->tok->append($this->char);
}
if($this->state == "new-expression"){
$last_tok = $this->toks[count($this->toks)-1];
if(isset(self::$keyword_rewrites[$last_tok->value])){
$last_tok->value = self::$keyword_rewrites[$last_tok->value];
}
}
}else if($this->state == "new-expression"){
// For function calls, a function name can itself be a sexpr that returns a function name
if($this->char == "("){
$this->tok = new OpenParenToken;
}else if($this->char == ")"){
// Allow for empty set of parens
$this->tok = new CloseParenToken;
}else if($this->char == "["){
$this->tok = new OpenBracketToken;
}else if($this->char == "]"){
$this->tok = new CloseBracketToken;
}else if($this->char == "{"){
$this->tok = new OpenBraceToken;
}else if($this->char == "}"){
$this->tok = new CloseBraceToken;
}else if($this->char == '"'){
$this->tok = new StringToken;
$this->state = "string";
}else if($this->in_sexpr_opening() && $this->char == ':' && $this->code[$this->i+1] != ':'){
$this->tok = new ListAccessToken;
}else if($this->in_sexpr_opening() && $this->char == '$' && trim($this->code[$this->i+1]) != ""){
$this->tok = new ExplicitVarToken;
$this->state = "append";
}else if($this->char == '&' && trim($this->code[$this->i+1]) != ""){
$this->tok = new SplatToken;
$this->state = "append";
}else if($this->char == '#'){
$this->tok = new FuncValToken;
$this->state = "append";
}else if($this->char == '^'){
$this->tok = new AnnotationToken;
$this->state = "append";
}else if($this->char == ':' && $this->code[$this->i+1] != ':'){
$this->tok = new KeywordToken;
$this->state = "append";
}else if($this->char == '~' or $this->char == "'" or $this->char == '@'){
$this->tok = new ReaderMacroToken($this->char);
}else if(is_numeric($this->char)){
$this->tok = new NumberToken($this->char);
$this->state = "append";
}else if(trim($this->char) !== "" and $this->char !== ","){
$this->tok = new NameToken($this->char);
$this->state = "append";
}
}
}
}
class InfixFunc{
public $params = array('x', 'y');
}
class FuncInfo{
static $tmp_counter=0;
private $name;
private $func;
private $force_not_partial;
private $args_given;
static function get_next_name(){
return Node::$ns."__partial".self::$tmp_counter++;
}
public function __construct($name, $force_not_partial, $args, $scope){
if(strstr($name, '$')){
if(($scoped_node = $scope->find($name, True, Null)) !== False){
if(isset($scoped_node->value)){
$this->name = $scoped_node->value;
}
}
}else{
$this->name = $name;
}
$this->args_given = $args;
$this->force_not_partial = $force_not_partial;
if(FuncDefNode::is_pharen_func($this->name)){
$func = FuncDefNode::get_pharen_func($this->name);
if(!($func instanceof FuncPlaceHolder)){
$this->func = $func;
}
}else if(in_array($name, Parser::$INFIX_OPERATORS)){
$this->func = new InfixFunc;
}
}
public function is_partial(){
return !$this->force_not_partial && $this->func && count($this->args_given) < $this->get_num_args_needed();
}
public function get_num_args_needed(){
$num = 0;
foreach($this->func->params as $param){
if(!($param instanceof ListNode
|| $param instanceof AnnotationNode
|| $param instanceof SplatNode)){
$num++;
}else{
break;
}
}
if($this->func instanceof LambdaNode){
$num--;
}
return $num;
}
public function get_tmp_func($parent){
$old_tmp = Node::$tmp;
Node::$tmp = '';
$name = self::get_next_name();
$params_diff = count($this->func->params) - count($this->args_given);
$function = new FuncDefNode($parent);
$function->is_partial = True;
$function->indent = $parent->indent;
$fn = $function->add_child(new LeafNode($function, array(), 'fn'));
$function->add_child(new LeafNode($function, array(), $name));
$params = new LiteralNode($function);
$function->add_child($params);
$body = Null;
if($this->func instanceof InfixFunc){
$body = new InfixNode($function);
}else{
$body = new Node($function);
}
$function->add_child($body);
if(last($this->func->params instanceof SplatNode)){
$body->add_child(new LeafNode($body, array(), "call_user_func_array"));
$splatting = True;
}else{
$splatting = False;
}
$body->add_child(new LeafNode($body, array(), $this->name));
foreach($this->args_given as $arg){
$arg->parent = $body;
$body->add_child($arg);
}
for($x=0;$x<$params_diff;$x++){
$var = new VariableNode($params, array(), "arg$x");
$params->add_child($var);
$var = new VariableNode($body, array(), "arg$x");
$body->add_child($var);
}
$scopeid_node = new VariableNode($params, array(), "__closure_id");
$params->add_child($scopeid_node);
$ret = array($function->compile_statement().$parent->format_line(""), $name);
Node::$tmp = $old_tmp;
return $ret;
}
}
class Scope{
public static $scope_id = 0;
public static $scopes = array();
public $owner;
public $bindings = array();
public $tok_bindings = array();
public $lexical_bindings = array();
public $lexically_needed = array();
public $lex_vars = False;
public $virtual=False;
public $id;
public $postfix;
public $replacements;
public function __construct($owner, $postfix=False, $replacements=array()){
$this->owner = $owner;
$this->id = self::$scope_id++;
$this->postfix = $postfix;
$this->replacements = $replacements;
self::$scopes[$this->id] = $this;
}
public function bind($var_name, $value_node){
// Keep value as just the node to allow for possible implementation of lazy evaluation in the future
$this->bindings[$var_name] = $value_node;
}
public function bind_lexical($var_name, $id){
$this->lexical_bindings[$var_name] = $id;
}
public function bind_tok($var_name, $token){
$this->tok_bindings[$var_name] = $token;
}
public function get_indent(){
return $this->owner->parent instanceof RootNode && $this->owner instanceof BindingNode ? "" : $this->owner->indent."\t";
}
public function rescope($var_name){
Node::$post_tmp .= $this->owner->format_line_indent("Scope::\$scopes['{$this->id}']->bindings['$var_name'] = $var_name;");
}
public function get_binding($var_name){
$value = $this->bindings[$var_name]->compile();
if(MacroNode::$rescope_vars){
$this->rescope($var_name);
}
return "$var_name = $value;";
}
public function get_lexical_binding($var_name, $id){
$value = "Lexical::get_lexical_binding('".Node::$ns."', $id, '$var_name', isset(\$__closure_id)?\$__closure_id:0);";
return "$var_name = $value";
}
public function init_namespace_scope(){
if(!RootNode::$ns){
return $this->owner->format_line("Lexical::\$scopes['".Node::$ns."'] = array();");
}else{
return "";
}
}
public function init_lexical_scope(){
if(count($this->lexically_needed) === 0){
return "";
}else{
return $this->owner->format_line_indent('$__scope_id = Lexical::init_closure("'.Node::$ns.'", '.$this->id.');');
}
}
public function get_lexing($var_name, $force=False, $prefix=""){
if($force){
$this->lexically_needed[$var_name] = True;
}
if(!isset($this->lexically_needed[$var_name])){
return "";
}
return $this->owner->format_line_indent($prefix.'Lexical::bind_lexing("'.Node::$ns."\", {$this->id}, '$var_name', $var_name);");
}
public function get_lexical_bindings(){
$code = "";
foreach($this->lexical_bindings as $var_name=>$id){
$code .= $this->owner->format_line_indent($this->get_lexical_binding($var_name, $id).';');
}
return $code;
}
public function find($var_name, $return_value=False, $from_virtual=Null, $is_tok=False){
$bindings = $is_tok ? $this->tok_bindings : $this->bindings;
if($var_name[0] != '$'){
$var_name = '$'.$var_name;
}
if(!array_key_exists($var_name, $bindings)){
if($this->owner->parent !== Null){
// Not virtual if variable was created from a non-virtual scope inside a virtual scope
// eg: Anonymous function inside let
$virtual = $this->virtual && ($from_virtual === Null || $from_virtual === True);
return $this->owner->parent->get_scope()->find($var_name, $return_value, $virtual, $is_tok);
}else{
return False;
}
}
if(!$from_virtual){
// When the scope below this is not a virtual scope such as a let binding
$this->lexically_needed[$var_name] = True;
}
if($return_value){
return $bindings[$var_name];
}
return $this->id;
}
public function find_immediate($var_name){
if($var_name[0] != '$'){
$var_name = '$'.$var_name;
}
$result = array_key_exists($var_name, $this->bindings) ? $this->bindings[$var_name] : False;
if($result === False && $this->virtual && $this->owner->parent !== Null){
$parent_scope = $this->owner->parent->get_scope();
return $parent_scope->find_immediate($var_name);
}else{
return $result;
}
}
}
class Node implements Iterator, ArrayAccess, Countable{
const REPL_INPUT = "repl_input";
static $in_func = 0;
static $tmpfunc;
static $lambda_tmp;
static $prev_tmp;
static $tmp;
static $post_tmp;
static $ns;
static $tmp_funcname_var=0;
static $delimiter_tokens = array("OpenParenToken", "CloseParenToken");
public $parent;
public $children;
public $tokens;
public $linenum;
public $return_flag = False;
public $has_variable_func = False;
public $in_macro;
public $force_not_partial;
public $returns_special_form;
public $indent = Null;
public $is_method_call;
public $annotation;
public $value_type_match = False;
public $quoted;
public $unquoted;
public $has_splice;
public $scope = Null;
protected $value = "";
static function add_tmp($code){
$code = Node::$prev_tmp.Node::$tmp.$code.Node::$post_tmp;
if(!LambdaNode::$in_lambda_compile){
$code = Node::$lambda_tmp.$code;
Node::$lambda_tmp = '';
}
if(!MacroNode::$ghosting && !MacroNode::$rescope_vars){
Node::$prev_tmp = '';
Node::$tmp = '';
}
Node::$post_tmp = '';
return $code;
}
static function add_tmpfunc($code){
$code = Node::$tmpfunc.$code;
Node::$tmpfunc = '';
return $code;
}
static function get_tmp_funcname_var(){
return "\$__tmpfuncname".self::$tmp_funcname_var++;
}
static function next_scope_id(){
return self::$next_scope_id++;
}
public function __construct($parent=null){
$this->parent = $parent;
$this->children = array();
$this->tokens = array();
if($this->parent instanceof SpecialForm){
$this->indent = $this->parent->indent."\t";
}else if($parent !== Null){
$this->indent = $this->parent->indent;
}
}
public function rewind(){
reset($this->children);
}
public function current(){
return current($this->children);
}
public function key(){
return key($this->children);
}
public function next(){
return next($this->children);
}
public function valid(){
return $this->current() !== False;
}
public function offsetExists($offset){
return isset($this->children[$offset]);
}
public function offsetGet($offset){
if(isset($this->children[$offset])){
$node = $this->children[$offset];
if($node instanceof LeafNode){
return $node->typify();
}else{
$node->is_macro = True;
return $node;
}
}else{
return Null;
}
}
public function offsetSet($offset, $value){
$this->children[$offset] = $value;
}
public function offsetUnset($offset){
unset($this->children[$offset]);
}
public function count(){
return count($this->children);
}
public function increase_indent(){
$this->indent .= "\t";
if(is_array($this->children)){
foreach($this->children as $c){
$c->increase_indent();
}
}
}
public function decrease_indent(){
$this->indent = substr($this->indent, 1);
if(is_array($this->children)){
foreach($this->children as $c){
$c->decrease_indent();
}
}
}
public function format_line($code, $prefix=""){
if($this->indent === Null){
$this->indent = $this->parent instanceof RootNode ? "" : $this->parent->indent."\t";
}
return $this->indent.$prefix.$code."\n";
}
public function format_line_indent($code, $prefix=""){
return "\t".$this->format_line($code, $prefix);
}
public function format_statement($code, $prefix=""){
return Node::add_tmp($this->format_line($code, $prefix));
}
public function get_scope(){
if($this->scope === Null){
return $this->parent->get_scope();
}
return $this->scope;
}
public function set_scope(Scope $scope){
$this->scope = $scope;
}
public function get_exprs(){
return $this->children;
}
protected function split_children(){
return array($this->children[0], array_slice($this->children, 1));
}
public function add_child($child, $tok=Null){
$this->children[] = $child;
if($tok === Null){
# If $tok is null it'll mean that $child is not a leafnode
# and has tokens of its own
$this->tokens[] = $child;
}else{
$this->tokens[] = $tok;
}
}
public function add_children($children){
foreach($children as $c){
$this->children[] = $c;
$c->parent = $this;
}
}
public function get_delims(){