forked from Scriptor/pharen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pharen.php
3569 lines (3062 loc) · 113 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."/lib/sequence.php");
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 {
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;
}
}
class TypeSig {
const SAME_VALTYPE = 3;
const SAME_TYPE = 2;
const ANY_MATCH = 0;
public $any = True;
public $annotations = array();
public function add_type($ann){
if($ann !== "Any"){
$this->any = False;
}
array_unshift($this->annotations, $ann);
}
public function match(TypeSig $other){
$thislen = count($this->annotations);
$otherlen = count($other->annotations);
if($thislen !== $otherlen) return 0;
$score = 0;
for($x=0; $x<$thislen; $x++){
$thistype = $this->annotations[$x];
$othertype = $other->annotations[$x];
if($thistype === "Any" || $othertype === "Any"){
$score += self::ANY_MATCH;
}else if(($thistype->value_type && $othertype->value-type) &&
($thistype->value_type === $othertype->value_type)){
$score += self::SAME_VALTYPE;
}else if($thistype->typename === $othertype->typename){
$score += self::SAME_TYPE;
}else{
return 0;
}
}
return $score;
}
}
class Token implements IPharenComparable, IPharenHashable{
public $value;
public $quoted;
public $unquoted;
public $unquote_spliced;
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{
}
class FuncValToken extends Token{
}
class KeywordToken extends Token{
}
class Lexer{
static $keyword_rewrites = array(
'or' => 'pharen-or',
'and' => 'pharen-and',
'list' => 'pharen-list'
);
public $code;
private $char;
private $tok;
private $state = "new-expression";
private $toks = array();
private $escaping = false;
private $i=0;
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 = "";
}
public function lex(){
for($this->i=0;$this->i<strlen($this->code);$this->i++){
$this->get_char();
$this->lex_char();
$toks_size = sizeof($this->toks);
if($toks_size == 0 or $this->tok !== $this->toks[$toks_size-1]){
$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 === ","){
$this->state = "new-expression";
}else if($this->char == ")"){
$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)){
$this->func = FuncDefNode::get_pharen_func($this->name);
}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 $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 $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(){
$node_cls_vars = get_class_vars(get_class($this));
$delims = $node_cls_vars['delimiter_tokens'];
return array(new $delims[0], new $delims[1]);
}
public function get_tokens($delims=Null){
$delims = $delims === Null ? $this->get_delims() : $delims;
$tokens = array($delims[0]);
foreach($this->tokens as $tok){
if(!($tok instanceof Token)){
$tokens = array_merge($tokens, $tok->get_tokens());
}else{
$tokens[] = $tok;
}
}
$tokens[] = $delims[1];
return $tokens;
}
public function convert_to_list($return_as_array=False, $get_values=False){
$list = array();
foreach($this->tokens as $key=>$tok){
if($tok instanceof Node){
$list[] = $val = $tok->convert_to_list($return_as_array, $get_values);
}else{
if($get_values && ($tok instanceof StringToken || $tok instanceof NumberToken)){
$tok_value = $tok->value;
if($tok instanceof NumberToken){
if(ctype_digit($tok_value)){
$tok_value = intval($tok_value);
}else{
$tok_value = floatval($tok_value);
}
}
$list[] = $tok_value;
}else{
$list[] = $tok;
}
}
}
if($return_as_array){
return $list;
}else{
$delims = $this->get_delims();
$list = PharenList::create_from_array($list);
$list->delimiter_tokens = $delims;
return $list;
}
}
public function compile_args($args=Null){
$output = array();
for($x=0; $x<count($args); $x++){
$arg = $args[$x];
if($arg instanceof SpliceWrapper){
$this->has_splice = True;
array_splice($args, $x+1, 0, $arg->get_exprs());
}else if(is_string($arg)){
$output[] = $arg;
}else{
$output[] = $a = $arg->compile();
}
}
return $output;
}
public function search($value){
foreach($this->children as $child){
if($child->search($value)){
return True;
}
}
return False;
}
public function get_last_func_call(){
return $this->children[0];
}
public function get_body_nodes(){
return array();
}
public function get_last_expr(){
return $this;
}
public function get_func_name(){
// Returns the compiled code for the function name and the arguments
// in a function call.
list($func_name_node, $args) = $this->split_children();
if(!($func_name_node instanceof LeafNode) && !($func_name_node instanceof UnquoteWrapper)){
$this->has_variable_func = True;
$func_name = self::get_tmp_funcname_var();
$func_val = $func_name_node->compile();
if($func_name_node instanceof FuncDefNode){
$scope = $this->get_scope();
$name_node = new LeafNode($this, array(), $func_name_node->get_name());
$scope->bind($func_name, $name_node);
}
Node::$tmp .= $this->format_line($func_name." = ".$func_val.";");
}else{
if($func_name_node instanceof VariableNode){
$this->has_variable_func = True;
$ann = $func_name_node->get_annotation();
if($ann && $ann->value_type){
$this->value_type_match = True;
$func_name = $ann->value_type;
}else{
$func_name = $func_name_node->compile();
}
}else{
$func_name = $func_name_node->compile();
}
}
return $func_name;
}
public function create_partial($func){
list($tmp_func, $tmp_name) = $func->get_tmp_func($this->parent);
Node::$tmp .= $tmp_func;
$scope = $this->get_scope();
if(count($scope->lexically_needed) === 0 || $scope->owner instanceof MacroNode){
$scope_id_str = 'Null';
}else{
$scope_id_str = '$__scope_id';
}
return 'new \PharenLambda(\''.RootNode::$ns.'\\\\'.$tmp_name.'\', Lexical::get_closure_id("'.Node::$ns.'", '.$scope_id_str.'))';
}
public function compile($is_statement=False, $is_return=False, $prefix=""){
$scope = $this->get_scope();
$func_name = $this->get_func_name();
$func = new FuncInfo($func_name, $this->force_not_partial, array_slice($this->children, 1), $this->get_scope());
if(MicroNode::is_micro($func_name)){
$micro = MicroNode::get_micro($func_name);
return $micro->get_body(array_slice($this->children, 1), $this->indent);
}else if(!($this->parent instanceof MethodCallNode)
&& MacroNode::is_macro($func_name) && !MacroNode::$ghosting){
$this->in_macro = True;
$unevaluated_args = array_slice($this->children, 1);
$arg_values = array();
foreach($unevaluated_args as $key=>$arg){
$unevaluated_args[$key] = $arg->convert_to_list();
$arg_values[] = $arg->convert_to_list(False, True);
}
MacroNode::evaluate($func_name, $unevaluated_args);
$macro_result = call_user_func_array($func_name, $arg_values);
if($macro_result instanceof QuoteWrapper){
$tokens = $macro_result->get_tokens(Null, $this->get_scope());
$parser = new Parser($tokens);
$macro_result = $parser->parse($this->parent);
$count = count($this->parent->children);
$expanded = $this->parent->children[$count-1];
array_pop($this->parent->children);
if($expanded instanceof SpecialForm or $expanded instanceof BindingNode){
// This prevents it from adding unnecessary semicolons
$this->returns_special_form = True;
}else if(get_class($expanded) == 'Node'){
$old_tmp = Node::$tmp;
$old_prev_tmp = Node::$prev_tmp;
$old_lambda_tmp = Node::$lambda_tmp;
$old_tmpfunc = Node::$tmpfunc;
if(MacroNode::is_macro($expanded->get_func_name())){
$this->returns_special_form = True;
}
Node::add_tmp('');
Node::add_tmpfunc('');
Node::$tmp = $old_tmp;
Node::$prev_tmp = $old_prev_tmp;
Node::$lambda_tmp = $old_lambda_tmp;
Node::$tmpfunc = $old_tmpfunc;
}
if($is_statement){
$code = $expanded->compile_statement($prefix);
if(!$this->returns_special_form){
# Special forms shouldn't have semicolons anyway
$code = trim($code, ";\n");
}
return $code;
}else if($is_return){
return $expanded->compile_return();
}else{
return $expanded->compile();
}
}else if(is_string($macro_result)){
return '"'.$macro_result.'"';
}else if(is_bool($macro_result) || $macro_result===Null){
return $macro_result ? "True" : "False";
}else{
return $macro_result;
}
}else if(!$this->has_splice && $func->is_partial()){
return $this->create_partial($func);
}else if(!$this->is_method_call &&
isset(ExpandableFuncNode::$funcs[$func_name])){
$func_node = ExpandableFuncNode::$funcs[$func_name];
$args = array_slice($this->children, 1);
$typesig = new TypeSig;
foreach($args as $arg){
if($arg instanceof VariableNode){
if($ann = $arg->get_annotation()){
$typesig->add_type($ann);
}else{
$typesig->add_type("Any");
}
}else{
$typesig->add_type("Any");
}
}
$typesig_found = False;
$typesig_map = ExpandableFuncNode::$typesig_map[$func_name];
$highest_score = 0;
if($this->value_type_match){
$func_name = trim($func_name, '^');
$highest_score = -1;
}
foreach($typesig_map as $pair){
$func_typesig = $pair[0];