-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexpr.bmx
3736 lines (3137 loc) · 102 KB
/
expr.bmx
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
' Copyright (c) 2013-2024 Bruce A Henderson
'
' Based on the public domain Monkey "trans" by Mark Sibly
'
' This software is provided 'as-is', without any express or implied
' warranty. In no event will the authors be held liable for any damages
' arising from the use of this software.
'
' Permission is granted to anyone to use this software for any purpose,
' including commercial applications, and to alter it and redistribute it
' freely, subject to the following restrictions:
'
' 1. The origin of this software must not be misrepresented; you must not
' claim that you wrote the original software. If you use this software
' in a product, an acknowledgment in the product documentation would be
' appreciated but is not required.
'
' 2. Altered source versions must be plainly marked as such, and must not be
' misrepresented as being the original software.
'
' 3. This notice may not be removed or altered from any source
' distribution.
'
Type TExpr
Field exprType:TType
Field static:Int
Method ToString$()
Return "<TExpr>"
End Method
Method Copy:TExpr()
InternalErr "TExpr.Copy"
End Method
Method Semant:TExpr(options:Int = 0)
InternalErr "TExpr.Semant"
End Method
Method SemantSet:TExpr( op$,rhs:TExpr, options:Int = 0 )
Err ToString()+" cannot be assigned to."
End Method
Method SemantFunc:TExpr( args:TExpr[] , throwError:Int = True, funcCall:Int = False )
Err ToString()+" cannot be invoked."
End Method
Method SemantScope:TScopeDecl()
Return Null
End Method
Method Eval$()
Err ToString()+" cannot be statically evaluated."
End Method
Method EvalConst:TExpr()
Local expr:TExpr = New TConstExpr.Create( exprType,Eval() ).Semant()
If TStringType(TConstExpr(expr).ty) Then
_appInstance.mapStringConsts(TConstExpr(expr).value)
End If
Return expr
End Method
Method Trans$()
Todo
End Method
Method TransStmt$()
Return Trans()
End Method
Method TransVar$()
InternalErr "TExpr.TransVar"
End Method
'semant and cast
Method SemantAndCast:TExpr( ty:TType,castFlags:Int=0 )
Local expr:TExpr=Semant()
Return New TCastExpr.Create( ty,expr,castFlags ).Semant()
End Method
'expr and ty already semanted!
Method Cast:TExpr( ty:TType,castFlags:Int=0 )
If Not exprType Then
Semant()
End If
If exprType.EqualsType( ty ) Return Self
Return New TCastExpr.Create( ty,Self,castFlags ).Semant()
End Method
Method SemantArgs:TExpr[]( args:TExpr[] )
args=args[..]
For Local i:Int=0 Until args.Length
If args[i] Then
If TIdentExpr(args[i]) Then
TIdentExpr(args[i]).isArg = True
End If
args[i]=args[i].Semant()
' if an arg is a invocation without braces, it is *probably* a function pointer.
If TInvokeExpr(args[i]) And Not TInvokeExpr(args[i]).invokedWithBraces Then
' but not if we've already processed it...
If Not (TInvokeExpr(args[i]).decl.attrs & FUNC_PTR) Then
TInvokeExpr(args[i]).exprType = New TFunctionPtrType
Local cp:TDecl = TInvokeExpr(args[i]).decl
cp.Semant
If TFuncDecl(cp) and TFuncDecl(cp).IsMethod() Then
Err "Method cannot be used as a function pointer"
End If
TInvokeExpr(args[i]).decl = TFuncDecl(TInvokeExpr(args[i]).decl.Copy(False))
TInvokeExpr(args[i]).decl.actual = cp
TInvokeExpr(args[i]).decl.attrs :| FUNC_PTR
TFunctionPtrType(TInvokeExpr(args[i]).exprType).func = TInvokeExpr(args[i]).decl
TInvokeExpr(args[i]).decl.semant()
End If
End If
End If
Next
Return args
End Method
Method CastArgs:TExpr[]( args:TExpr[],funcDecl:TFuncDecl )
If args.Length>funcDecl.argDecls.Length Then
Err "Too many function parameters"
End If
' FIXME
'args=args.Resize( funcDecl.argDecls.Length )
' FIXME
For Local i:Int=0 Until funcDecl.argDecls.Length
' ensure funcdecl args are semanted before trying to use them.
If Not funcDecl.argDecls[i].IsSemanted() Then
funcDecl.argDecls[i].Semant()
End If
If i < args.length And args[i]
Local argExpr:TExpr = args[i]
If TInvokeExpr(argExpr) And Not TInvokeExpr(argExpr).invokedWithBraces Then
If Not IsPointerType(funcDecl.argDecls[i].ty, TType.T_BYTE) And Not TFunctionPtrType(funcDecl.argDecls[i].ty) Then
Err "Unable to convert from '" + argExpr.exprType.ToString() + "()' to '" + funcDecl.argDecls[i].ty.ToString() + "'"
End If
End If
If TInvokeMemberExpr(argExpr) And Not TInvokeMemberExpr(argExpr).invokedWithBraces Then
If Not IsPointerType(funcDecl.argDecls[i].ty, TType.T_BYTE) And Not TFunctionPtrType(funcDecl.argDecls[i].ty) Then
Err "Unable to convert from '" + argExpr.exprType.ToString() + "()' to '" + funcDecl.argDecls[i].ty.ToString() + "'"
End If
End If
If funcDecl.argDecls[i].ty._flags & TType.T_VAR Then
If TConstExpr(argExpr) Or TBinaryExpr(argExpr) Or (TIndexExpr(argExpr) And TStringType(TIndexExpr(argExpr).expr.exprType)) Or ..
TInvokeExpr(argExpr) Or TInvokeMemberExpr(argExpr) Or TNewObjectExpr(argExpr) or TNewArrayExpr(argExpr) Then
Err "Expression for 'Var' parameter must be a variable or an element of an array or pointer"
End If
If TVarExpr(argExpr) Or TMemberVarExpr(argExpr) Then
Local decl:TDecl
If TVarExpr(argExpr) Then
decl = TVarExpr(argExpr).decl
Else
decl = TMemberVarExpr(argExpr).decl
End If
If decl.IsReadOnly() Then
If TFieldDecl(decl) Then
Local scope:TFuncDecl = _env.FuncScope()
If Not scope Or Not scope.IsCtor() Or (decl.ClassScope() <> scope.ClassScope()) Then
Err "Expression for 'Var' parameter cannot be a ReadOnly variable"
End If
Else
Err "Expression for 'Var' parameter cannot be a ReadOnly variable"
End If
End If
End If
' passing a non volatile local as Var from within a Try block?
If TVarExpr(argExpr) Then
Local ldecl:TLocalDecl = TLocalDecl(TVarExpr(argExpr).decl)
If ldecl And Not ldecl.volatile Then
Local tryStmtDecl:TTryStmtDecl = _env.FindTry()
If tryStmtDecl And (Not ldecl.declaredInTry Or tryStmtDecl <> ldecl.declaredInTry) Then
ldecl.volatile = True
End If
End If
End If
End If
If (funcDecl.argDecls[i].ty._flags & TType.T_VAR) And Not (funcDecl.argDecls[i].ty.EqualsType(argExpr.exprType)) Then
If (Not TObjectType(funcDecl.argDecls[i].ty)) Or (TObjectType(funcDecl.argDecls[i].ty) And Not argExpr.exprType.ExtendsType(funcDecl.argDecls[i].ty)) Then
err "Variable for 'Var' parameter is not of matching type"
End If
End If
' re-test auto array for compatible consts.
If TArrayExpr(argExpr) And TArrayType(funcDecl.argDecls[i].ty) And TNumericType(TArrayType(funcDecl.argDecls[i].ty).elemType) Then
TArrayExpr(argExpr).toType = TArrayType(funcDecl.argDecls[i].ty).elemType
argExpr.exprType = Null
argExpr.Semant()
End If
args[i]=argExpr.Cast( funcDecl.argDecls[i].ty )
Else If funcDecl.argDecls[i].init
If i = args.length Then
' extend args to add default init entry
args = args[..i + 1]
End If
args[i]=funcDecl.argDecls[i].init
Else
Err "Missing function argument '"+funcDecl.argDecls[i].ident+"'."
EndIf
Next
Return args
End Method
Method BalanceTypes:TType( lhs:TType,rhs:TType, balancePtrs:Int = True )
If TStringType( lhs ) Or TStringType( rhs ) Then
If TObjectType(lhs) Or TObjectType(rhs) Then
If TObjectType(lhs) And TObjectType(lhs).classDecl.ident = "Object" Then
Return lhs
End If
If TObjectType(rhs) And TObjectType(rhs).classDecl.ident = "Object" Then
Return rhs
End If
Return New TStringType
Else
Return New TStringType
End If
End If
If IsPointerType( lhs, 0, TType.T_POINTER ) Or IsPointerType( rhs, 0, TType.T_POINTER ) Then
If balancePtrs Then
If IsPointerType( lhs, 0, TType.T_POINTER ) Return lhs
If IsPointerType( rhs, 0, TType.T_POINTER ) Return rhs
Else
If IsPointerType( lhs, 0, TType.T_POINTER ) Then
If Not IsPointerType( rhs, 0, TType.T_POINTER ) And Not TNullType(rhs) Then
Err "Can't balance types "+lhs.ToString()+" and "+rhs.ToString()+"."
End If
Return lhs
End If
If IsPointerType( rhs, 0, TType.T_POINTER ) Then
If Not IsPointerType( lhs, 0, TType.T_POINTER ) And Not TNullType(lhs) Then
Return rhs
End If
End If
End If
End If
If TDouble128Type( lhs ) Or TDouble128Type( rhs ) Return New TDouble128Type
If TFloat128Type( lhs ) Or TFloat128Type( rhs ) Return New TFloat128Type
If TFloat64Type( lhs ) Or TFloat64Type( rhs ) Return New TFloat64Type
If TDoubleType( lhs ) Or TDoubleType( rhs ) Return New TDoubleType
If TFloatType( lhs ) Or TFloatType( rhs ) Return New TFloatType
If TFunctionPtrType( lhs ) Or TFunctionPtrType( rhs ) Then
If TFunctionPtrType( lhs ) Return lhs
If TFunctionPtrType( rhs ) Return rhs
End If
If TInt128Type( lhs ) Or TInt128Type( rhs ) Return New TInt128Type
If TULongType( lhs ) Or TULongType( rhs ) Return New TULongType
If TSizeTType( lhs ) Or TSizeTType( rhs ) Return New TSizeTType
If TWParamType( lhs ) Or TWParamType( rhs ) Return New TWParamType
If TLongType( lhs ) And TUIntType( rhs ) Return New TULongType
If TUIntType( lhs ) And TLongType( rhs ) Return New TULongType
If TLParamType( lhs ) Or TLParamType( rhs ) Return New TLParamType
If TULongIntType( lhs ) Or TULongIntType( rhs ) Return New TULongIntType
If TLongType( lhs ) Or TLongType( rhs ) Return New TLongType
If TLongIntType( lhs ) Or TLongIntType( rhs ) Return New TLongIntType
If TUIntType( lhs ) Or TUIntType( rhs ) Return New TUIntType
If TIntType( lhs ) Or TIntType( rhs ) Return New TIntType
If TEnumType( lhs ) Or TEnumType( rhs ) Then
If TEnumType( lhs ) Return lhs
If TEnumType( rhs ) Return rhs
End If
If TObjectType( lhs ) And TNullDecl(TObjectType( lhs ).classDecl) Then
Return rhs
End If
If TObjectType( rhs ) And TNullDecl(TObjectType( rhs ).classDecl) Then
Return lhs
End If
If lhs.ExtendsType( rhs ) Return rhs
If rhs.ExtendsType( lhs ) Return lhs
' balance arrays - only for objects... to the lowest common denominator.
If TArrayType( lhs ) And TArrayType( rhs ) Then
If TArrayType( lhs ).isStatic - TArrayType( rhs ).isStatic <> 0 Then
Err "Cannot mix arrays and static arrays"
End If
If TObjectType(TArrayType( lhs ).elemType) And TObjectType(TArrayType( rhs ).elemType) Then
' lhs = Object[]
If TObjectType(TArrayType( lhs ).elemType).classDecl.ident = "Object" Then
Return lhs
End If
' rhs = Object[]
If TObjectType(TArrayType( rhs ).elemType).classDecl.ident = "Object" Then
Return rhs
End If
' does one extend the other? If so, return the base type
If TObjectType(TArrayType( lhs ).elemType).ExtendsType(TObjectType(TArrayType( rhs ).elemType)) Then
Return rhs
End If
If TObjectType(TArrayType( rhs ).elemType).ExtendsType(TObjectType(TArrayType( lhs ).elemType)) Then
Return lhs
End If
' no? then we will fallback to an Object type array
' find the Object classdecl instance
Local modid$="brl.classes"
Local mdecl:TModuleDecl=_env.FindModuleDecl( modid )
' return an array of Objects
Return New TArrayType.Create(New TObjectType.Create(TClassDecl(mdecl.FindDecl( "object" ))))
End If
If TObjectType(TArrayType( lhs ).elemType) And TObjectType(TArrayType( lhs ).elemType).classDecl.ident = "Object" And TStringType(TArrayType( rhs ).elemType) Then
Return lhs
End If
If TObjectType(TArrayType( rhs ).elemType) And TObjectType(TArrayType( rhs ).elemType).classDecl.ident = "Object" And TStringType(TArrayType( lhs ).elemType) Then
Return rhs
End If
If TObjectType(TArrayType( lhs ).elemType) And TObjectType(TArrayType( lhs ).elemType).classDecl.ident = "Object" And TArrayType(TArrayType( rhs ).elemType) Then
Return lhs
End If
If TObjectType(TArrayType( rhs ).elemType) And TObjectType(TArrayType( rhs ).elemType).classDecl.ident = "Object" And TArrayType(TArrayType( lhs ).elemType) Then
Return rhs
End If
' balancing primitive types
If Not TArrayType( lhs ).elemType.EqualsType(TArrayType( rhs ).elemType) Then
Err "Types '" + TArrayType( lhs ).elemType.ToString() + " Array' and '" + TArrayType( rhs ).elemType.ToString() + " Array' are unrelated"
End If
End If
' balance structs
If TObjectType( lhs ) And TObjectType( rhs ) And TObjectType( lhs ).EqualsType(rhs) And TObjectType( lhs ).classDecl.IsStruct() And TObjectType( rhs ).classDecl.IsStruct() Then
Return lhs
End If
Err "Can't balance types "+lhs.ToString()+" and "+rhs.ToString()+"."
End Method
Function CopyExpr:TExpr( expr:TExpr )
If Not expr Return Null
Return expr.Copy()
End Function
Function CopyArgs:TExpr[]( exprs:TExpr[] )
exprs=exprs[..]
For Local i:Int=0 Until exprs.Length
exprs[i]=CopyExpr( exprs[i] )
Next
Return exprs
End Function
End Type
' exec a stmt, return an expr
Type TStmtExpr Extends TExpr
Field stmt:TStmt
Field expr:TExpr
Method Create:TStmtExpr( stmt:TStmt,expr:TExpr )
Self.stmt=stmt
Self.expr=expr
Return Self
End Method
Method Copy:TExpr()
If exprType Return Self
Return New TStmtExpr.Create( stmt,CopyExpr(expr) )
End Method
Method ToString$()
Return "TStmtExpr(,"+expr.ToString()+")"
End Method
Method Semant:TExpr(options:Int = 0)
If exprType Return Self
stmt.Semant()
expr=expr.Semant()
exprType=expr.exprType
Return Self
End Method
Method Trans$()
Return _trans.TransStmtExpr( Self )
End Method
Method TransVar$()
Semant
Return _trans.TransStmtExpr( Self )
End Method
End Type
' literal
Type TConstExpr Extends TExpr
Field ty:TType
Field value$
Field originalValue$
' True if the const was identified as a specific type.
Field typeSpecific:Int
Method Create:TConstExpr( ty:TType,value$ )
originalValue = value
If TNumericType( ty ) And IsPointerType(ty, 0, TType.T_POINTER) Then
Self.ty=ty
If value Then
Self.value = value
Else
Self.value="0"
End If
Return Self
End If
If TIntType( ty ) Or TShortType( ty ) Or TByteType( ty ) Or TLongType( ty ) Or TUIntType( ty ) Or TULongType( ty ) Or TWParamType(ty) Or TLParamType(ty) Or TLongIntType(ty) Or TULongIntType(ty)
Local radix:Int
If value.StartsWith( "%" )
radix=1
Else If value.StartsWith( "$" )
radix=4
EndIf
If radix
Local val:Long = 0
For Local i:Int=1 Until value.Length
Local ch:Int=value[i]
If ch>=48 And ch<58
val=val Shl radix | (ch & 15)
Else
val=val Shl radix | ((ch & 15)+9)
EndIf
Next
If TIntType(ty) And val >= 2147483648:Long Then
value = String( -2147483648:Long + (val - 2147483648:Long))
Else
If TShortType( ty ) Then
value=String( Short(val) )
Else If TByteType( ty ) Then
value=String( Byte(val) )
Else
value=String( val )
End If
End If
Else
If TShortType( ty ) Then
value = String.FromLong(Short(value.ToLong()))
Else If TByteType( ty ) Then
value = String.FromLong(Byte(value.ToLong()))
Else
Local buf:Byte[64]
Local b:Int
Local v:String = value.Trim()
Local leading0:Int = True
If v Then
Local i:Int
If v[0] = Asc("+") Then
i = 1
Else If v[0] = Asc("-") Then
i = 1
buf[b] = Asc("-")
b:+ 1
End If
While i < value.Length
If Not IsDigit(v[i]) Then
Exit
End If
If leading0 And v[i] = Asc("0") Then
i :+ 1
Continue
End If
leading0 = False
buf[b] = v[i]
b :+ 1
i :+ 1
Wend
If leading0 Then
value = "0"
Else
value = String.FromBytes(buf, b)
End If
Else
value = "0"
End If
End If
EndIf
Else If TDecimalType( ty )
If Not (value.Contains("e") Or value.Contains("E") Or value.Contains(".") Or value.Contains("inf") Or value.Contains("nan"))
If TFloatType(ty) Then
value:+".00000000"
Else
value:+".0000000000000000"
End If
EndIf
EndIf
Self.ty=ty
Self.value=value
Return Self
End Method
Method UpdateType(ty:TType)
typeSpecific = True
Create(ty, originalValue)
End Method
Method Copy:TExpr()
Local e:TConstExpr = New TConstExpr.Create( ty,value )
e.originalValue = originalValue
e.typeSpecific = typeSpecific
Return e
End Method
Method ToString$()
Return "TConstExpr(~q"+value+"~q)"
End Method
Method Semant:TExpr(options:Int = 0)
If exprType Return Self
exprType=ty.Semant()
Return Self
End Method
Method Eval$()
Return value
End Method
Method EvalConst:TExpr()
Return Self
End Method
Method Trans$()
Semant
Return _trans.TransConstExpr( Self )
End Method
Method SemantAndCast:TExpr( ty:TType,castFlags:Int=0 )
Local expr:TExpr=Semant()
If expr.exprType.EqualsType( ty ) Return expr
If value = "bbNullObject" Then
Err "bbNullObject"
Return expr
End If
Return New TCastExpr.Create( ty,expr,castFlags ).Semant()
End Method
Method CompatibleWithType:Int(ty:TType)
If Not TDecimalType(ty) Then
If value.Contains("e") Or value.Contains("E") Or value.Contains(".") Or value.Contains("inf") Or value.Contains("nan") Then
Return False
End If
Local val:Long = value.ToLong()
If val < 0 Then
If TByteType(ty) Or TShortType(ty) Or TUIntType(ty) Or TULongType(ty) Or TSizeTType(ty) Or TInt128Type(ty) Or TWParamType(ty) or TULongIntType(ty) Then
Return False
End If
Else
If TByteType(ty) Then
If value <> String.FromInt(Byte(Val)) Then
Return False
End If
End If
If TUIntType(ty) Or ((TSizeTType(ty) Or TWParamType(ty)) And WORD_SIZE = 4) Or (TULongIntType(ty) And TULongIntType(ty).GetSize() = 4) Then
If val > 4294967296:Long Then
Return False
End If
End If
If TULongType(ty) Or ((TSizeTType(ty) Or TWParamType(ty)) And WORD_SIZE = 8) Or (TULongIntType(ty) And TULongIntType(ty).GetSize() = 8) Then
If value.length > 20 Then
Return False
Else If value.length = 20 Then
For Local i:Int = 0 Until value.length
Local v:Int = value[i]
Local n:Int = "18446744073709551616"[i]
If v < n Then
Exit
Else If v > n Then
Return False
End If
Next
End If
End If
End If
If TShortType(ty) Then
If value <> String.FromInt(Short(val)) Then
Return False
End If
End If
If TIntType(ty) Or (TLParamType(ty) And WORD_SIZE = 4) Or (TLongIntType(ty) And TLongIntType(ty).GetSize() = 4) Then
If value <> String.FromInt(Int(val)) Then
Return False
End If
End If
If TLongType(ty) Or (TLParamType(ty) And WORD_SIZE = 8) Or (TLongIntType(ty) And TLongIntType(ty).GetSize() = 8) Then
If value <> String.FromLong(Long(val)) Then
Return False
End If
End If
End If
Return True
End Method
End Type
Type TVarExpr Extends TExpr
Field decl:TVarDecl
Method Create:TVarExpr( decl:TVarDecl )
Self.decl=decl
Return Self
End Method
Method Copy:TExpr()
Return Self
End Method
Method ToString$()
Return "TVarExpr("+decl.ToString()+")"
End Method
Method Semant:TExpr(options:Int = 0)
If exprType Return Self
If Not decl.IsSemanted() InternalErr "TVarExpr.Semant"
exprType=decl.ty.Copy()
Return Self
End Method
Method SemantSet:TExpr( op$,rhs:TExpr, options:Int = 0 )
Return Semant()
End Method
Method Trans$()
Semant
Return _trans.TransTemplateCast( exprType,TVarDecl(decl.actual).ty,_trans.TransVarExpr( Self ) )
End Method
Method TransVar$()
Semant
Return _trans.TransVarExpr( Self )
End Method
End Type
Type TMemberVarExpr Extends TExpr
Field expr:TExpr
Field decl:TVarDecl
Method Create:TMemberVarExpr( expr:TExpr,decl:TVarDecl )
Self.expr=expr
Self.decl=decl
Return Self
End Method
Method Copy:TExpr()
Return Self
End Method
Method ToString$()
Return "TMemberVarExpr("+expr.ToString()+","+decl.ToString()+")"
End Method
Method Semant:TExpr(options:Int = 0)
If exprType Return Self
If Not decl.IsSemanted() InternalErr "TMemberVarExpr.Semant"
exprType=decl.ty.Semant()
Return Self
End Method
Method SemantSet:TExpr( op$,rhs:TExpr, options:Int = 0 )
Return Semant()
End Method
Method Trans$()
Return _trans.TransTemplateCast( exprType,TVarDecl(decl.actual).ty,_trans.TransMemberVarExpr( Self ) )
End Method
Method TransVar$()
Return _trans.TransMemberVarExpr( Self )
End Method
End Type
Type TInvokeExpr Extends TExpr
Field decl:TFuncDecl
Field args:TExpr[]
Field invokedWithBraces:Int
Field isArg:Int
Field isRhs:Int
Method Create:TInvokeExpr( decl:TFuncDecl,args:TExpr[]=Null,invokedWithBraces:Int=True, isArg:Int=False, isRhs:Int = False )
Self.decl=decl
If args Then
Self.args=args
Else
Self.args = New TExpr[0]
End If
Self.invokedWithBraces = invokedWithBraces
Self.isArg = isArg
Self.isRhs = isRhs
Return Self
End Method
Method Copy:TExpr()
Return Self
End Method
Method ToString$()
Local t$="TInvokeExpr("+decl.ToString()
For Local arg:TExpr=EachIn args
t:+","+arg.ToString()
Next
Return t+")"
End Method
Method Semant:TExpr(options:Int = 0)
If exprType Return Self
If Not decl.retType
decl.Semant()
End If
'If TIdentType(decl.retType) Then
exprType = decl.retType.Semant()
'Else
' exprType=decl.retType
'End If
'If ((isArg Or isRhs) And Not invokedWithBraces) And (args = Null Or args.length = 0) Then
' if the call was a statement (even one written without parentheses), then invokedWithBraces is true
' so no complicated checks are needed here; if invokedWithBraces is false, this is definitely not a call
If Not invokedWithBraces Then
If decl.IsMethod() Then
Err "Method cannot be used as a function pointer"
End If
' nothing to do here, as we are a function pointer. i.e. no braces
' and our expression type is a function ptr...
exprType = New TFunctionPtrType.Create(decl)
Else
args=CastArgs( args,decl )
End If
Return Self
End Method
Method Trans$()
' Return _trans.TransTemplateCast( exprType,TFuncDecl(decl.actual).retType,_trans.TransInvokeExpr( Self ) )
Return _trans.TransInvokeExpr( Self )
End Method
Method TransStmt$()
Return _trans.TransInvokeExpr( Self )
End Method
Method Eval$()
Return Super.Eval()
End Method
End Type
Type TInvokeMemberExpr Extends TExpr
Field expr:TExpr
Field decl:TFuncDecl
Field args:TExpr[]
Field isResize:Int 'FIXME - butt ugly!
Field invokedWithBraces:Int
Method Create:TInvokeMemberExpr( expr:TExpr,decl:TFuncDecl,args:TExpr[]=Null, invokedWithBraces:Int = True )
Self.expr=expr
Self.decl=decl
If args
Self.args=args
Else
Self.args = New TExpr[0]
End If
Self.invokedWithBraces = invokedWithBraces
Return Self
End Method
Method Copy:TExpr()
Return Self
End Method
Method ToString$()
Local t$="TInvokeMemberExpr("+expr.ToString()+","+decl.ToString()
For Local arg:TExpr=EachIn args
t:+","+arg.ToString()
Next
Return t+")"
End Method
Method Semant:TExpr(options:Int = 0)
If exprType Return Self
If Not decl.IsSemanted() decl.Semant()
exprType=decl.retType
args=SemantArgs( args )
args=CastArgs( args,decl )
'Array $resize hack!
If TArrayType( exprType ) And TVoidType( TArrayType( exprType ).elemType )
isResize=True
exprType=expr.exprType
EndIf
Return Self
End Method
Method Trans$()
'Array $resize hack!
If isResize Return _trans.TransInvokeMemberExpr( Self )
Return _trans.TransTemplateCast( exprType,TFuncDecl(decl.actual).retType,_trans.TransInvokeMemberExpr( Self ) )
End Method
Method TransStmt$()
Return _trans.TransInvokeMemberExpr( Self )
End Method
End Type
Type TNewObjectExpr Extends TExpr
Field ty:TType
Field args:TExpr[]
Field ctor:TFuncDecl
Field classDecl:TClassDecl
Field instanceExpr:TExpr
Method Create:TNewObjectExpr( ty:TType,args:TExpr[] )
Self.ty=ty
Self.args=args
Return Self
End Method
Method Copy:TExpr()
Return New TNewObjectExpr.Create( ty,CopyArgs(args) )
End Method
Method Semant:TExpr(options:Int = 0)
If exprType Return Self
Local it:TIdentType = TIdentType(ty)
Local iArgs:TExpr[] = SemantArgs(CopyArgs(args))
Local isNewSelf:Int = (it And it.ident = "self")
ty=ty.Semant(True)
If Not ty Then
' maybe it's an instance of a type ?
Local decl:TVarDecl = TVarDecl(_env.FindDecl(it.ident))
If decl And TObjectType(decl.ty) Then
' this legacy feature is deprecated. Issue a warning but let it go for now...
ty = decl.ty
instanceExpr = New TVarExpr.Create(decl).Semant()
'
Warn("Use of New <Object instance> is deprecated, and support will be removed in a future update.")
Else
Err "Type '"+it.ident+"' not found"
End If
Else If isNewSelf Then
Warn("Use of New Self is deprecated, and support will be removed in a future update.")
End If
args=SemantArgs( args )
Local objTy:TObjectType=TObjectType( ty )
Local clsTy:TClassType=TClassType( ty )
If Not objTy And Not clsTy
Err "Expression is not a class."
EndIf
If objTy And Not objTy.classDecl.Semanted() Then
objTy.classDecl.Semant()
End If
'
If clsTy And clsTy.instance Then
instanceExpr = New TSelfExpr.Semant()
End If
If objTy Then
classDecl=objTy.classDecl
Else
classDecl=clsTy.classDecl
End If
If Not instanceExpr Then
If classDecl.IsInterface() Err "Cannot create instance of an interface."
If classDecl.IsAbstract() Err "Cannot create instance of abstract type " + classDecl.ToString() + ..
", which is either declared Abstract or has (or inherits) an abstract Method."
End If
'If classDecl.IsTemplateArg() Err "Cannot create instance of a generic argument."
If classDecl.args And Not classDecl.instanceof Err "Cannot create instance of a generic class."
Local parts:String[]
If it Then
parts = it.ident.ToLower().Split(".")
End If
If classDecl.IsExtern()
Err "Cannot create instance of an extern type"
'If args Err "No suitable constructor found for class "+classDecl.ToString()+"."
Else
' if the New Type doesn't have extra idents (like a create method), then don't use the args in the search.
' otherwise, the args are for the constructor.
If Not parts Or parts.length = 1 Then
ctor=classDecl.FindFuncDecl( "new",args,,,,,SCOPE_CLASS_HEIRARCHY )
If Not ctor Err "No suitable constructor found for class "+classDecl.ToString()+"."
args=CastArgs( args,ctor )
Else
ctor=classDecl.FindFuncDecl( "new",,,,,,SCOPE_CLASS_HEIRARCHY )
If Not ctor Err "No suitable constructor found for class "+classDecl.ToString()+"."
End If
EndIf
' New Self doesn't necessarily create an instance of ourself - we might be an instance of
' a subclass at the time...
If Not isNewSelf Then
classDecl.attrs:|CLASS_INSTANCED
End If
If TClassType(ty) Then
exprType=New TObjectType.Create(TClassType(ty).classDecl)
Else
exprType=ty
End If
If it Then
'Local parts:String[] = it.ident.ToLower().Split(".")
Local i:Int = 0
While i < parts.length And parts[i] <> classDecl.IdentLower() And parts[i] <> "self"
i :+ 1
Wend
i :+ 1
Local expr:TExpr = Self
Local cdecl:TClassDecl = classDecl
Local eType:TType = ty
Local errorDetails:String
While i < parts.length
Local id:String = parts[i]
i :+ 1
' find other member decl (field, etc)
Local decl:TValDecl = TValDecl(cdecl.GetDecl(id))
If TVarDecl(decl) Then
decl.Semant
Local tmp:TLocalDecl=New TLocalDecl.Create( "", eType, expr,, True )
Local varExpr:TExpr = New TMemberVarExpr.Create(New TVarExpr.Create( tmp ), TVarDecl(decl)).Semant()
expr = New TStmtExpr.Create( New TDeclStmt.Create( tmp ), varExpr ).Semant()
eType = decl.ty
If TObjectType(eType) Then
cdecl = TObjectType(expr.exprType).classdecl
End If
If TArrayType(eType) Or TStringType(eType) Then
cdecl = eType.GetClass()
End If
Continue
End If
If TConstDecl(decl) Then
decl.Semant()
expr = New TConstExpr.Create(decl.ty, TConstDecl(decl).value).Semant()
eType = decl.ty
Continue
End If
' find member function.method
Local fdecl:TFuncDecl
Try
fdecl = cdecl.FindFuncDecl(id, iArgs,,,,True,SCOPE_CLASS_HEIRARCHY)
Catch errorMessage:String