-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestgsi.src
4719 lines (4676 loc) · 126 KB
/
testgsi.src
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
COLOR={}
COLOR.ERROR="<color=#FF0000>"
COLOR.EOL="</color>"
Exception=function(exception)
return COLOR.ERROR+"[GSI]"+exception+COLOR.EOL
end function
ValueError=function(exception)
return COLOR.ERROR+"ValueError: "+exception+COLOR.EOL
end function
ValueError=function(exception)
return COLOR.ERROR+"ValueError: "+exception+COLOR.EOL
end function
TypeError=function(exception)
return COLOR.ERROR+"TypeError: "+exception+COLOR.EOL
end function
__pyInOp__=function(a, b)
__type_b = typeof(@b)
if not (__type_b == "list" or __type_b == "map" or __type_b =="string") then
exit(TypeError("argument of type '"+__type_b+"' is not iterable"))
end if
if __type_b == "list" then
for __v in b
if a == __v then
return true
end if
end for
return false
else if __type_b == "map" then
for __v in b
if a == __v.key then
return true
end if
end for
return false
else if __type_b == "string" then
return b.indexOf(a) != null
end if
end function
__pyRange__=function(__start, __end, __inc=1)
if __end == null then
__end = __start-1
__start = 0
else
if __inc > 0 then
__end = __end - 1
else
__end = __end + 1
end if
end if
if __start < __end and __inc < 0 or __start > __end and __inc > 0 then
return []
end if
return range(__start, __end, __inc)
end function
list.append=function(__object)
self.push(__object)
end function
list.pop=function(__index=-1)
__object=self[__index]
self.remove(__index)
return __object
end function
list.clear=function()
while self.len > 0
self.remove(0)
end while
end function
string.find=function(__sub, __start)
__ret = self.indexOf(__sub, __start)
if __ret != null then
return __ret
else
return -1
end if
end function
string.rfind=function(__sub)
for __index in range(0,self.len-1,1)
__ret = self.indexOf(__sub, __index)
if __ret != null then
return __ret
end if
end for
return -1
end function
map.keys=function()
return self.indexes
end function
isinstance=function(a, b)
return a isa b
end function
float=function(__value)
return __value.val
end function
int=function(__value)
return floor(__value)
end function
KeyException=function(key)
return Exception((("Key Not Found: '"+key)+"' not found in map"))
end function
KeyException_=function(message)
return Exception(message)
end function
CompilerException=function(message)
return Exception("Compiler Error: "+message)
end function
CompilerException_=function(context,lineNum,message)
return Exception("Compiler Error: "+message+" [line "+lineNum+"]"+context)
end function
LexerException=function(message)
return Exception(("Lexer Error: "+message))
end function
TypeException=function(message)
return Exception(message)
end function
IndexException=function(message)
return Exception(message)
end function
RuntimeException=function(message)
return Exception(("Runtime Error: "+message))
end function
TooManyArgumentsException=function()
return Exception("Too Many Arguments")
end function
UnknownOpException=function(message)
return Exception(message)
end function
LimitExceededException=function(message)
return Exception(message)
end function
UndefinedIdentifierException=function(ident)
return Exception((("Undefined Identifier: '"+ident)+"' is unknown in this context"))
end function
UndefinedLocalException=function(ident)
return Exception((("Undefined Local Identifier: '"+ident)+"' is unknown in this context"))
end function
Type={}
Type.Unknown="Unknown"
Type.Keyword="Keyword"
Type.Number="Number"
Type.String="String"
Type.Identifier="Identifier"
Type.OpAssign="OpAssign"
Type.OpPlus="OpPlus"
Type.OpMinus="OpMinus"
Type.OpTimes="OpTimes"
Type.OpDivide="OpDivide"
Type.OpMod="OpMod"
Type.OpPower="OpPower"
Type.OpEqual="OpEqual"
Type.OpNotEqual="OpNotEqual"
Type.OpGreater="OpGreater"
Type.OpGreatEqual="OpGreatEqual"
Type.OpLesser="OpLesser"
Type.OpLessEqual="OpLessEqual"
Type.OpAssignPlus="OpAssignPlus"
Type.OpAssignMinus="OpAssignMinus"
Type.OpAssignTimes="OpAssignTimes"
Type.OpAssignDivide="OpAssignDivide"
Type.OpAssignMod="OpAssignMod"
Type.OpAssignPower="OpAssignPower"
Type.LParen="LParen"
Type.RParen="RParen"
Type.LSquare="LSquare"
Type.RSquare="RSquare"
Type.LCurly="LCurly"
Type.RCurly="RCurly"
Type.AddressOf="AddressOf"
Type.Comma="Comma"
Type.Dot="Dot"
Type.Colon="Colon"
Type.Comment="Comment"
Type.EOL="EOL"
Token={}
Token.__init__=function(token_type,text)
__result__ = new Token
__result__.type=token_type
__result__.text=text
__result__.afterSpace=false
return __result__
end function
Token.__str__=function()
if self.text == null then
return self.type
end if
return self.type+"("+self.text+")"
end function
Token.EOL=null
Keywords={}
Keywords.all=["break","continue","else","end","for","function","if","in","isa","new","null","then","repeat","return","while","and","or","not","true","false"]
Keywords.IsKeyword=function(text)
return __pyInOp__(text,Keywords.all)
end function
Lexer={}
Lexer.__init__=function(token_input)
__result__ = new Lexer
__result__.lineNum=1
__result__.position=0
__result__.input=token_input
__result__.inputLength=len(token_input)
__result__.pending=[]
return __result__
end function
Lexer.AtEnd=function()
return (self.position >= self.inputLength and len(self.pending) == 0)
end function
Lexer.Peek=function()
if len(self.pending) == 0 then
if self.AtEnd() then
return Token.EOL
end if
self.pending.append(self.Dequeue())
end if
return self.pending[0]
end function
Lexer.Dequeue=function()
if len(self.pending) > 0 then
return self.pending.pop(0)
end if
oldPos=self.position
self.SkipWhitespaceAndComment()
if self.AtEnd() then
return Token.EOL
end if
result=Token.__init__(Type.Unknown,null)
result.afterSpace=self.position > oldPos
startPos=self.position
c=self.input[self.position]
self.position=self.position+1
if not self.AtEnd() then
c2=self.input[self.position]
if c2 == "=" then
if c == "=" then
result.type=Type.OpEqual
else if c == "+" then
result.type=Type.OpAssignPlus
else if c == "-" then
result.type=Type.OpAssignMinus
else if c == "*" then
result.type=Type.OpAssignTimes
else if c == "/" then
result.type=Type.OpAssignDivide
else if c == "%" then
result.type=Type.OpAssignMod
else if c == "^" then
result.type=Type.OpAssignPower
end if
end if
if (c == "!" and c2 == "=") then
result.type=Type.OpNotEqual
end if
if (c == ">" and c2 == "=") then
result.type=Type.OpGreatEqual
end if
if (c == "<" and c2 == "=") then
result.type=Type.OpLessEqual
end if
if result.type != Type.Unknown then
self.position=self.position+1
return result
end if
end if
if c == "+" then
result.type=Type.OpPlus
else if c == "-" then
result.type=Type.OpMinus
else if c == "*" then
result.type=Type.OpTimes
else if c == "/" then
result.type=Type.OpDivide
else if c == "%" then
result.type=Type.OpMod
else if c == "^" then
result.type=Type.OpPower
else if c == "(" then
result.type=Type.LParen
else if c == ")" then
result.type=Type.RParen
else if c == "[" then
result.type=Type.LSquare
else if c == "]" then
result.type=Type.RSquare
else if c == "{" then
result.type=Type.LCurly
else if c == "}" then
result.type=Type.RCurly
else if c == "," then
result.type=Type.Comma
else if c == ":" then
result.type=Type.Colon
else if c == "=" then
result.type=Type.OpAssign
else if c == "<" then
result.type=Type.OpLesser
else if c == ">" then
result.type=Type.OpGreater
else if c == "@" then
result.type=Type.AddressOf
else if (c == ";" or c == ""+char(10)+"") then
result.type=Type.EOL
if c == ";" then
result.text=";"
else
result.text=""+char(10)+""
end if
if c != ";" then
self.lineNum=self.lineNum+1
end if
else if c == ""+char(13)+"" then
result.type=Type.EOL
if (self.position < self.inputLength and self.input[self.position] == ""+char(10)+"") then
self.position=self.position+1
result.text=""+char(13)+""+char(10)+""
else
result.text=""+char(13)+""
end if
self.lineNum=self.lineNum+1
end if
if result.type != Type.Unknown then
return result
end if
if c == "." then
if (self.position >= self.inputLength or not self.IsNumeric(self.input[self.position])) then
result.type=Type.Dot
return result
end if
end if
if (c == "." or self.IsNumeric(c)) then
result.type=Type.Number
while self.position < self.inputLength
lastc=c
c=self.input[self.position]
if (self.IsNumeric(c) or c == "." or c == "E" or c == "e" or ((c == "-" or c == "+") and (lastc == "E" or lastc == "e"))) then
self.position=self.position+1
else
break
end if
end while
else if self.IsIdentifier(c) then
while self.position < self.inputLength
if self.IsIdentifier(self.input[self.position]) then
self.position=self.position+1
else
break
end if
end while
result.text=self.input[startPos:self.position]
if Keywords.IsKeyword(result.text) then
result.type=Type.Keyword
else
result.type=Type.Identifier
end if
if result.text == "end" then
nextWord=self.Dequeue()
if (nextWord != null and nextWord.type == Type.Keyword) then
result.text=((result.text+" ")+nextWord.text)
else
exit(LexerException("'end' without following keyword ('if', 'function', etc.)"))
end if
else if result.text == "else" then
p=self.position
while (p < self.inputLength and (self.input[p] == " " or self.input[p] == ""+char(9)+""))
p=p+1
end while
if ((p+1) < self.inputLength and self.input[p:(p+2)] == "if" and ((p+2) >= self.inputLength or not self.IsIdentifier(self.input[(p+2)]))) then
result.text="else if"
self.position=(p+2)
end if
end if
return result
else if c == """" then
result.type=Type.String
haveDoubledQuotes=false
startPos=self.position
gotEndQuote=false
while self.position < self.inputLength
c=self.input[self.position]
self.position=self.position+1
if c == """" then
if (self.position < self.inputLength and self.input[self.position] == """") then
haveDoubledQuotes=true
self.position=self.position+1
else
gotEndQuote=true
break
end if
else if (c == ""+char(10)+"" or c == ""+char(13)+"") then
break
end if
end while
if not gotEndQuote then
exit(LexerException("missing closing quote ("")"))
end if
result.text=self.input[startPos:(self.position-1)]
if haveDoubledQuotes then
result.text=result.text.replace("""""","""")
end if
return result
else
result.type=Type.Unknown
end if
result.text=self.input[startPos:self.position]
return result
end function
Lexer.SkipWhitespaceAndComment=function()
while (not self.AtEnd() and self.IsWhitespace(self.input[self.position]))
self.position=self.position+1
end while
if (self.position < (self.inputLength-1) and self.input[self.position] == "/" and self.input[(self.position+1)] == "/") then
self.position=self.position+2
while (not self.AtEnd() and self.input[self.position] != ""+char(10)+"")
self.position=self.position+1
end while
end if
end function
Lexer.IsNumeric=function(c)
return "0" <= c <= "9"
end function
Lexer.IsIdentifier=function(c)
return (c == "_" or "a" <= c <= "z" or "A" <= c <= "Z" or "0" <= c <= "9")
end function
Lexer.IsWhitespace=function(c)
return (c == " " or c == ""+char(9)+"")
end function
Lexer.IsAtWhitespace=function()
return (self.AtEnd() or self.IsWhitespace(self.input[self.position]))
end function
Lexer.IsInStringLiteral=function(charPos,source,startPos)
inString=false
for i in __pyRange__(startPos,charPos)
if source[i] == """" then
inString=not inString
end if
end for
return inString
end function
Lexer.commentStartPos=function(source,start_pos)
comment_start=(start_pos-2)
while true
comment_start=source.find("//",(comment_start+2))
if comment_start < 0 then
break
end if
if not Lexer.IsInStringLiteral(comment_start,source,start_pos) then
break
end if
end while
return comment_start
end function
Lexer.TrimComment=function(source)
start_pos=(source.rfind(""+char(10)+"")+1)
comment_start=Lexer.commentStartPos(source,start_pos)
if comment_start >= 0 then
return source[start_pos:comment_start]
end if
return source
end function
Lexer.LastToken=function(source)
startPos=(source.rfind(""+char(10)+"")+1)
commentStart=Lexer.commentStartPos(source,startPos)
if commentStart >= 0 then
endPos=(commentStart-1)
else
endPos=(len(source)-1)
end if
while (endPos >= 0 and Lexer.IsWhitespace(source[endPos]))
endPos=endPos-1
end while
if endPos < 0 then
return Token.EOL
end if
tokStart=endPos
c=source[endPos]
if Lexer.IsIdentifier(c) then
while (tokStart > startPos and Lexer.IsIdentifier(source[(tokStart-1)]))
tokStart=tokStart-1
end while
else if c == """" then
inQuote=true
while tokStart > startPos
tokStart=tokStart-1
if source[tokStart] == """" then
inQuote=not inQuote
if (not inQuote and tokStart > startPos and source[(tokStart-1)] != """") then
break
end if
end if
end while
else if (c == "=" and tokStart > startPos) then
c2=source[(tokStart-1)]
if __pyInOp__(c2,["><=!"]) then
tokStart=tokStart-1
end if
end if
lex=Lexer.__init__(source)
lex.position=tokStart
return lex.Dequeue()
end function
Lexer.Check=function(tok,type_,text,line_num)
if tok == null then
exit(ValueError("Token is None"))
end if
if tok.type != type_ then
exit(ValueError("Token type: expected "+type_+", but got "+tok.type))
end if
if (text != null and tok.text != text) then
exit(ValueError("Token text: expected "+text+", but got "+tok.text))
end if
end function
Lexer.CheckLineNum=function(actual,expected)
if actual != expected then
exit(ValueError("Lexer line number: expected "+expected+", but got "+actual))
end if
end function
Lexer.RunUnitTests=function()
lex=Lexer.__init__("42 * 3.14158")
Lexer.Check(lex.Dequeue(),Type.Number,"42",0)
Lexer.CheckLineNum(lex.lineNum,1)
Lexer.Check(lex.Dequeue(),Type.OpTimes,null,0)
Lexer.Check(lex.Dequeue(),Type.Number,"3.14158",0)
if not lex.AtEnd() then
exit(ValueError("AtEnd not set when it should be"))
end if
Lexer.CheckLineNum(lex.lineNum,1)
lex=Lexer.__init__("6*(.1-foo) end if // and a comment!")
Lexer.Check(lex.Dequeue(),Type.Number,"6",0)
Lexer.CheckLineNum(lex.lineNum,1)
Lexer.Check(lex.Dequeue(),Type.OpTimes,null,0)
Lexer.Check(lex.Dequeue(),Type.LParen,null,0)
Lexer.Check(lex.Dequeue(),Type.Number,".1",0)
Lexer.Check(lex.Dequeue(),Type.OpMinus,null,0)
Lexer.Check(lex.Peek(),Type.Identifier,"foo",0)
Lexer.Check(lex.Peek(),Type.Identifier,"foo",0)
Lexer.Check(lex.Dequeue(),Type.Identifier,"foo",0)
Lexer.Check(lex.Dequeue(),Type.RParen,null,0)
Lexer.Check(lex.Dequeue(),Type.Keyword,"end if",0)
Lexer.Check(lex.Dequeue(),Type.EOL,null,0)
if not lex.AtEnd() then
exit(ValueError("AtEnd not set when it should be"))
end if
Lexer.CheckLineNum(lex.lineNum,1)
lex=Lexer.__init__("""foo"" ""isn't """"real"""""" ""now """""""" double!""")
Lexer.Check(lex.Dequeue(),Type.String,"foo",0)
Lexer.Check(lex.Dequeue(),Type.String,"isn't ""real""",0)
Lexer.Check(lex.Dequeue(),Type.String,"now """" double!",0)
if not lex.AtEnd() then
exit(ValueError("AtEnd not set when it should be"))
end if
lex=Lexer.__init__("foo"+char(10)+"bar"+char(13)+"baz"+char(13)+""+char(10)+"bamf")
Lexer.Check(lex.Dequeue(),Type.Identifier,"foo",0)
Lexer.CheckLineNum(lex.lineNum,1)
Lexer.Check(lex.Dequeue(),Type.EOL,null,0)
Lexer.Check(lex.Dequeue(),Type.Identifier,"bar",0)
Lexer.CheckLineNum(lex.lineNum,2)
Lexer.Check(lex.Dequeue(),Type.EOL,null,0)
Lexer.Check(lex.Dequeue(),Type.Identifier,"baz",0)
Lexer.CheckLineNum(lex.lineNum,3)
Lexer.Check(lex.Dequeue(),Type.EOL,null,0)
Lexer.Check(lex.Dequeue(),Type.Identifier,"bamf",0)
Lexer.CheckLineNum(lex.lineNum,4)
Lexer.Check(lex.Dequeue(),Type.EOL,null,0)
if not lex.AtEnd() then
exit(ValueError("AtEnd not set when it should be"))
end if
lex=Lexer.__init__("x += 42")
Lexer.Check(lex.Dequeue(),Type.Identifier,"x",0)
Lexer.CheckLineNum(lex.lineNum,1)
Lexer.Check(lex.Dequeue(),Type.OpAssignPlus,null,0)
Lexer.Check(lex.Dequeue(),Type.Number,"42",0)
if not lex.AtEnd() then
exit(ValueError("AtEnd not set when it should be"))
end if
Lexer.Check(Lexer.LastToken("x=42 // foo"),Type.Number,"42",0)
Lexer.Check(Lexer.LastToken("x = [1, 2, // foo"),Type.Comma,null,0)
Lexer.Check(Lexer.LastToken("x = [1, 2 // foo"),Type.Number,"2",0)
Lexer.Check(Lexer.LastToken("x = [1, 2 // foo // and ""more"" foo"),Type.Number,"2",0)
Lexer.Check(Lexer.LastToken("x = [""foo"", ""//bar""]"),Type.RSquare,null,0)
Lexer.Check(Lexer.LastToken("print 1 // line 1"+char(10)+"print 2"),Type.Number,"2",0)
Lexer.Check(Lexer.LastToken("print ""Hi""""Quote"" // foo bar"),Type.String,"Hi""Quote",0)
end function
Token.EOL=Token.__init__(Type.EOL,null)
SourceLoc={}
SourceLoc.__init__=function(context,lineNum)
__result__ = new SourceLoc
__result__.context=context
__result__.lineNum=lineNum
return __result__
end function
SourceLoc.__str__=function()
if self.context then
contextStr=self.context+" "
else
contextStr=""
end if
return "["+contextStr+"line "+self.lineNum+"]"
end function
Param={}
Param.__init__=function(name,defaultValue)
__result__ = new Param
__result__.name=name
__result__.defaultValue=defaultValue
return __result__
end function
ValuePair={}
ValuePair.__init__=function(a,b)
__result__ = new ValuePair
__result__.a=a
__result__.b=b
return __result__
end function
ValuePair.Equals=function(other)
if isinstance(other,ValuePair) then
return (self.a == other.a and self.b == other.b)
end if
return false
end function
Value={}
Value.Hash=function()
return hash(self)
end function
Value.DoubleValue=function()
return 0
end function
Value.CanSetElem=function()
return false
end function
Value.CodeForm=function(recursionLimit)
return self.__str__()
end function
Value.CodeForm_=function(vm,recursionLimit)
return self.ToString(vm)
end function
Value.ToString=function(vm)
return self.__str__()
end function
Value.val_=function(context)
return self
end function
Value.Val_=function(context)
return [self,null]
end function
Value.RecursiveEqual=function(rhs)
toDo=[]
visited=[]
toDo.append(ValuePair.__init__(self,rhs))
while len(toDo) > 0
pair=toDo.pop()
visited.append(pair)
if isinstance(pair.a,ValList) then
listA=pair.a
if isinstance(pair.b,ValList) then
listB=pair.b
else
listB=null
end if
if listB == null then
return false
end if
if listA == listB then
continue
end if
aCount=len(listA.values)
if aCount != len(listB.values) then
return false
end if
for i in __pyRange__(aCount)
newPair=ValuePair.__init__(listA.values[i],listB.values[i])
if __pyNotInOp__(newPair,visited) then
toDo.append(newPair)
end if
end for
else if isinstance(pair.a,ValMap) then
mapA=pair.a
if isinstance(pair.b,ValMap) then
mapB=pair.b
else
mapB=null
end if
if mapB == null then
return false
end if
if mapA == mapB then
continue
end if
if len(mapA.map_) != len(mapB.map_) then
return false
end if
for _key in mapA.map_
_result=mapB.TryGetValue_(_key)
if _result[0] == false then
return false
end if
valFromB=_result[1]
valFromA=mapA.map_[_key]
newPair=ValuePair.__init__(valFromA,valFromB)
if __pyNotInOp__(newPair,visited) then
toDo.append(newPair)
end if
end for
else if (pair.a == null or pair.b == null) then
if (pair.a != null or pair.b != null) then
return false
end if
else if pair.a.Equality(pair.b) == 0 then
return false
end if
end while
return true
end function
LocalOnlyMode={}
LocalOnlyMode.Off=0
LocalOnlyMode.Warn=1
LocalOnlyMode.Strict=2
ValVar=new Value
ValVar.implicitResult=null
ValVar.self_=null
ValVar.__init__=function(identifier)
__result__ = new ValVar
__result__.identifier=identifier
__result__.noInvoke=false
__result__.localOnly=LocalOnlyMode.Off
return __result__
end function
ValVar.__str__=function()
if self.noInvoke then
return ("@"+self.identifier)
end if
return self.identifier
end function
ValVar.Equality=function(rhs)
if (isinstance(rhs,ValVar) and rhs.identifier == self.identifier) then
return 1
else
return 0
end if
end function
ValVar.val_=function(context)
if self == self.self_ then
return context.self_
end if
return context.GetVar(self.identifier,LocalOnlyMode.Off)
end function
ValVar.Val_=function(context)
if self == self.self_ then
return context.self_
end if
return [context.GetVar(self.identifier,self.localOnly),null]
end function
ValVar.implicitResult=ValVar.__init__("_")
ValVar.self_=ValVar.__init__("self")
ValSeqElem=new Value
ValSeqElem.__init__=function(sequence,index)
__result__ = new ValSeqElem
__result__.sequence=sequence
__result__.index=index
__result__.noInvoke=false
return __result__
end function
ValSeqElem.__str__=function()
if self.noInvoke then
return "'@'"+self.sequence.__str__()+"["+self.index.__str__()+"]"
else
return self.sequence.__str__()+"["+self.index.__str__()+"]"
end if
end function
ValSeqElem.Equality=function(rhs)
if (isinstance(rhs,ValSeqElem) and rhs.sequence == self.sequence and rhs.index == self.index) then
return 1
else
return 0
end if
end function
ValSeqElem.ToString=function(vm)
return self.__str__()
end function
ValSeqElem.val_=function(context)
return self.Val_(context)[0]
end function
ValSeqElem.Val_=function(context)
baseSeq=self.sequence
if self.sequence == ValVar.self_ then
baseSeq=context.self_
if baseSeq == null then
exit(UndefinedIdentifierException("self"))
end if
end if
valueFoundIn=null
if self.index == null then
idxVal=null
else
idxVal=self.index.val_(context)
end if
if isinstance(idxVal,ValString) then
return self.Resolve(baseSeq,idxVal.value,context)
end if
baseVal=baseSeq.val_(context)
if isinstance(baseVal,ValMap) then
_result=baseVal.Lookup_(idxVal)
result=_result[0]
valueFoundIn=_result[1]
if valueFoundIn == null then
if idxVal == null then
exit(KeyException("null"))
else
exit(KeyException(idxVal.CodeForm(context.vm,1)))
end if
end if
return [result,valueFoundIn]
else if isinstance(baseVal,ValList) then
return [baseVal.GetElem(idxVal),valueFoundIn]
else if isinstance(baseVal,ValString) then
return [baseVal.GetElem(idxVal),valueFoundIn]
else if baseVal == null then
exit(TypeException("Null Reference Exception: can't index into null"))
end if
exit(TypeException("Type Exception: can't index into this type"))
end function
ValSeqElem.Resolve=function(sequence,identifier,context)
includeMapType=true
loopsLeft=ValMap.maxIsaDepth
while sequence != null
if (isinstance(sequence,ValTemp) or isinstance(sequence,ValVar)) then
sequence=sequence.val_(context)
end if
if isinstance(sequence,ValMap) then
idVal=TempValString.Get(identifier)
found=sequence.TryGetValue_(idVal)
TempValString.Release(idVal)
if found[0] then
return [found[1],sequence]
end if
if loopsLeft < 0 then
exit(LimitExceededException("__isa depth exceeded (perhaps a reference loop?)"))
end if
_result=sequence.TryGetValue_(ValString.magicIsA)
if not _result[0] then
if not includeMapType then
exit(KeyException(identifier))
end if
if context.vm.mapType != null then
sequence=context.vm.mapType
else
sequence=Intrinsics.MapType()
end if
includeMapType=false
end if
else if isinstance(sequence,ValList) then
if context.vm.listType != null then
sequence=context.vm.listType
else
sequence=Intrinsics.ListType()
end if
includeMapType=false
else if isinstance(sequence,ValString) then
if context.vm.stringType != null then
sequence=context.vm.stringType
else
sequence=Intrinsics.StringType()
end if
includeMapType=false
else if isinstance(sequence,ValNumber) then
if context.vm.numberType != null then
sequence=context.vm.numberType
else
sequence=Intrinsics.NumberType()
end if
includeMapType=false
else if isinstance(sequence,ValFunction) then
if context.vm.functionType != null then
sequence=context.vm.functionType
else
sequence=Intrinsics.FunctionType()
end if
includeMapType=false
else
exit(TypeException("Type Error (while attempting to look up "+identifier+")"))
end if
loopsLeft=loopsLeft-1
end while
return [null,null]
end function
ValList=new Value
ValList.maxSize=16777215
ValList.__init__=function(values)
__result__ = new ValList
__result__.assignOverride=null
_values=values
if values == null then
_values=[]
end if
__result__.values=_values
return __result__
end function
ValList.BoolValue=function()
return (self.values != null and len(self.values) > 0)
end function
ValList.ToString=function(vm)
return self.CodeForm_(vm,3)
end function
ValList.CodeForm_=function(vm,recursionLimit)
if recursionLimit == 0 then
return "[...]"
end if
if (recursionLimit > 0 and recursionLimit < 3 and vm != null) then
shortName=vm.FindShortName(self)
if shortName != null then
return shortName
end if
end if
strs=[]
for value in self.values
if value == null then
strs.append("null")
else
strs.append(value.CodeForm_(vm,(recursionLimit-1)))
end if
end for
if len(strs) > 0 then
_strs=strs[0]
for _str in strs[1:]
_strs=_strs+(", "+_str)
end for
else
_strs=""
end if
return (("["+_strs)+"]")
end function
ValList.CanSetElem=function()
return true
end function
ValList.SetElem=function(index,value)
i=index.IntValue()
if i < 0 then
i=i+len(self.values)
end if
if (i < 0 or i >= len(self.values)) then
exit(IndexException("Index Error (list index "+index+" out of range)"))
end if
self.values[i]=value
end function
ValList.CodeForm=function(recursionLimit)
if recursionLimit == 0 then
return "[...]"
end if
strs=[]
for value in self.values
if value == null then
strs.append("null")
else
strs.append(value.CodeForm((recursionLimit-1)))
end if
end for
if len(strs) > 0 then
_strs=strs[0]
for _str in strs[1:]
_strs=_strs+(", "+_str)
end for
else
_strs=""
end if
return (("["+_strs)+"]")
end function
ValList.__str__=function()
return self.CodeForm(3)
end function
ValList.EvalCopy=function(context)
result=ValList.__init__()
for value in self.values
if value == null then
result.values.append(null)
else
result.values.append(value.val_(context))
end if
end for
return result
end function
ValList.GetElem=function(index)
if not isinstance(index,ValNumber) then
exit(KeyException_("List index must be numeric"))
end if
i=index.IntValue()
if i < 0 then
i=i+len(self.values)
end if
if (i < 0 or i >= len(self.values)) then
exit(IndexException((("Index Error (list index "+index.__str__())+" out of range)")))
end if
return self.values[i]
end function
ValList.Equality=function(rhs)
if not isinstance(rhs,ValList) then
return 0