-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
translator.bmx
2100 lines (1744 loc) · 54.3 KB
/
translator.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-2023 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.
'
Global _trans:TTranslator
Type TTranslator
Field _app:TAppDecl
Field outputFiles:TMap = New TMap
Field indent$
Field LINES:TStringList'=New TStringList
Field unreachable:Int,broken:Int
Field contLabelId:Int
Field exitLabelId:Int
'Munging needs a big cleanup!
Field globalMungScope:TMap = New TMap
Field mungScope:TMap=New TMap'<TDecl>
Field mungStack:TStack=New TStack'< StringMap<TDecl> >
Field funcMungs:TMap=New TMap'<FuncDeclList>
Field customVarStack:TStack = New TStack
Field varStack:TStack = New TStack
Field tryStack:TStack = New TStack
Field loopTryStack:TStack = New TStack
Field mungedScopes:TMap=New TMap'<StringSet>
'Field funcMungs:TFuncDeclList=New TFuncDeclList
'Field mungedFuncs:TMap=New Map
Field localScopeStack:TStack = New TStack
Field localScope:TStack = New TStack
Field ind:Int
Field debugOut:String
Field processingReturnStatement:Int
Field coverageFileInfo:TMap = New TMap
Field coverageFunctionFileInfo:TMap = New TMap
Method PushVarScope()
varStack.Push customVarStack
customVarStack = New TStack
End Method
Method PopVarScope()
customVarStack=TStack(varStack.Pop())
End Method
Method PushLoopLocalStack(stmt:Object)
ind :+ 1
If DEBUG Then
Emit "// --> STACK = " + ind
End If
localScope.Push stmt
End Method
Method PopLoopLocalStack()
If DEBUG Then
Emit "// <-- STACK = " + ind
End If
ind :- 1
If ind < 0 Then
InternalErr "TTranslator.PopLoopLocalStack"
End If
localScope.Pop
End Method
Method LoopLocalScopeDepth:Int(findStmt:TStmt)
Local count:Int = 0
For Local stmt:Object = EachIn localScope
If TBlockDecl(stmt) = Null Then
If findStmt And TTryBreakCheck(stmt) And findStmt <> TTryBreakCheck(stmt).stmt Then
Continue
End If
Exit
End If
count :+ 1
Next
Return count
End Method
Method DumpLocalScope:Int()
Print "DumpLocalScope:"
For Local stmt:Object = EachIn localScope
Print " " + stmt.ToString()
Next
End Method
Method GetTopLocalLoop:TTryBreakCheck(findStmt:TStmt)
For Local tbc:TTryBreakCheck = EachIn localScope
If findStmt And findStmt <> tbc.stmt Then
Continue
End If
Return tbc
Next
End Method
Method PushLoopTryStack(stmt:Object)
If loopTryStack.Length() = 0 Then
' Try statements can only be applied here to loops
If TTryStmt(stmt) = Null Then
loopTryStack.Push stmt
End If
Else
loopTryStack.Push stmt
End If
End Method
Method TryDownToBlockScopeCount:Int(endBlockType:Int)
Local lastTry:Int
Local firstBlock:Int
Local count:Int
For Local stmt:Object = EachIn localScope
If TBlockDecl(stmt) Then
If TBlockDecl(stmt).blockType & BLOCK_TRY_CATCH Then
lastTry = count
Else If TBlockDecl(stmt).blockType = endBlockType Then
firstBlock = count
Exit
End If
End If
If TTryBreakCheck(stmt) Then
Continue
End If
count :+ 1
Next
Return firstBlock - lastTry
End Method
Method PopLoopTryStack()
loopTryStack.Pop
End Method
Method LoopTryDepth:Int(findStmt:TStmt)
Local count:Int = 0
For Local stmt:Object = EachIn loopTryStack
If TTryStmt(stmt) = Null Then
If findStmt And findStmt <> stmt Then
Continue
End If
Exit
End If
count :+ 1
Next
Return count
End Method
Method LoopTryStmts:TTryStmt[](findStmt:TStmt)
Local stmts:TTryStmt[]
For Local stmt:Object = EachIn loopTryStack
If TTryStmt(stmt) Then
stmts :+ [TTryStmt(stmt)]
Else
If findStmt And findStmt <> stmt Then
Continue
End If
Exit
End If
Next
Return stmts
End Method
Method GetTopLoop:TTryBreakCheck(findStmt:TStmt)
For Local tbc:TTryBreakCheck = EachIn loopTryStack
If findStmt And findStmt <> tbc.stmt Then
Continue
End If
Return tbc
Next
End Method
Function TransManglePointer$( ty:TType )
Local p:String
If ty
If ty._flags & TType.T_VAR Then
p:+ "v"
End If
If ty._flags & TType.T_PTR Then
p:+ "p"
Else If ty._flags & TType.T_PTRPTR Then
p:+ "pp"
Else If ty._flags & TType.T_PTRPTRPTR Then
p:+ "ppp"
End If
End If
Return p
End Function
Function TransMangleType:String(ty:TType)
Local p:String = TransManglePointer(ty)
If TVoidType( ty ) Return "v"
If TByteType( ty ) Return p + "b"
If TShortType( ty ) Return p + "s"
If TIntType( ty ) Return p + "i"
If TUIntType( ty ) Return p + "u"
If TFloatType( ty ) Return p + "f"
If TDoubleType( ty ) Return p + "d"
If TLongType( ty ) Return p + "l"
If TULongType( ty ) Return p + "y"
If TSizeTType( ty ) Return p + "z"
If TFloat64Type( ty ) Return p + "h"
If TFloat128Type( ty ) Return p + "k"
If TInt128Type( ty ) Return p + "j"
If TDouble128Type( ty ) Return p + "m"
If TStringType( ty ) Return p + "S"
If TWParamType( ty ) Return p + "W"
If TLParamType( ty ) Return p + "L"
If TLongIntType( ty ) Return p + "g"
If TULongIntType( ty ) Return p + "G"
If TArrayType( ty ) Then
Return p + "a" + TransMangleType(TArrayType( ty ).elemType)
End If
If TObjectType( ty ) Then
If Not TObjectType( ty ).classdecl.IsExtern()
Return p + "T" + TObjectType( ty ).classDecl.ident
Else
If TObjectType( ty ).classdecl.IsInterface() Then
Return p + "I" + TObjectType(ty).classDecl.ident
ElseIf TObjectType( ty ).classdecl.IsStruct() Then
Return p + "R" + TObjectType(ty).classDecl.ident
Else
Return p + "E" + TObjectType(ty).classDecl.ident
End If
End If
End If
If TFunctionPtrType( ty ) Then
Local func:TFuncDecl = TFunctionPtrType( ty ).func
Local s:String = "F" + MangleMethodArgs(func)
' For Local i:Int = 0 Until func.argDecls.length
' s :+ TransMangleType(func.argDecls[i].ty)
' Next
Return s + "_" + TransMangleType(func.retType) + "_"
End If
If TEnumType( ty ) Return p + "e" + TEnumType( ty ).decl.ident
Err "Unsupported type for name mangling : " + ty.ToString()
End Function
Method MangleMethod:String(fdecl:TFuncDecl)
If (fdecl.IsMethod() And Not fdecl.ClassScope().IsStruct())Or fdecl.IsCtor() Then
Return MangleMethodArgs(fdecl)
Else
Return MangleMethodRetType(fdecl) + MangleMethodArgs(fdecl)
End If
End Method
Method MangleMethodRetType:String(fdecl:TFuncDecl)
If fdecl.retType Then
Return "_" + TransMangleType(fdecl.retType)
Else
Return "_v"
End If
End Method
Function MangleMethodArgs:String(fdecl:TFuncDecl)
Local s:String
For Local arg:TArgDecl = EachIn fdecl.argDecls
If Not s Then
s = "_"
End If
s :+ TransMangleType(arg.ty)
Next
Return s
End Function
Method equalsTorFunc:Int(classDecl:TClassDecl, func:TFuncDecl)
If func.IdentLower() = "new" Or func.IdentLower() = "delete" Then
Return True
End If
Return False
End Method
Method equalsBuiltInFunc:Int(classDecl:TClassDecl, func:TFuncDecl, checked:Int = False)
If func.equalsBuiltIn > -1 Then
Return func.equalsBuiltIn
End If
If checked Or func.IdentLower() = "tostring" Or func.IdentLower() = "compare" Or func.IdentLower() = "sendmessage" Or func.IdentLower() = "new" Or func.IdentLower() = "delete" Then
If classDecl.munged = "bbObjectClass" Then
For Local decl:TFuncDecl = EachIn classDecl.Decls()
If Not decl.IsSemanted() Then
decl.Semant
End If
If decl.IdentLower() = func.IdentLower() Then
Local res:Int = decl.EqualsFunc(func)
If res Then
func.equalsBuiltIn = True
End If
Return res
End If
Next
End If
If classDecl.superClass Then
Return equalsBuiltInFunc(classDecl.superClass, func, True)
End If
End If
func.equalsBuiltIn = False
Return False
End Method
Method equalsIfcBuiltInFunc:Int(classDecl:TClassDecl, func:TFuncDecl, checked:Int = False)
If checked Or func.IdentLower() = "delete" Then
If classDecl.munged = "bbObjectClass" Then
For Local decl:TFuncDecl = EachIn classDecl.Decls()
If Not decl.IsSemanted() Then
decl.Semant
End If
If decl.IdentLower() = func.IdentLower() Then
Return decl.EqualsFunc(func)
End If
Next
End If
If classDecl.superClass Then
Return equalsIfcBuiltInFunc(classDecl.superClass, func, True)
End If
End If
Return False
End Method
Method MungFuncDecl( fdecl:TFuncDecl )
If fdecl.munged Return
Local funcs:TFuncDeclList=TFuncDeclList(funcMungs.ValueForKey( fdecl.ident ))
If funcs
For Local tdecl:TFuncDecl=EachIn funcs
If fdecl.EqualsArgs( tdecl, True ) And fdecl.scope = tdecl.scope
fdecl.munged=tdecl.munged
Return
EndIf
Next
Else
funcs=New TFuncDeclList
funcMungs.Insert fdecl.ident,funcs
EndIf
If fdecl.scope Then
Local id:String = fdecl.ident
If fdecl.attrs & FUNC_OPERATOR Then
id = MungSymbol(id)
End If
fdecl.munged = fdecl.ParentScope().munged + "_" + id
If Not equalsBuiltInFunc(fdecl.classScope(), fdecl) And Not fdecl.noMangle Then
fdecl.munged :+ MangleMethod(fdecl)
End If
' fields are lowercase with underscore prefix.
' a function pointer with FUNC_METHOD is a field function pointer.
'If TFieldDecl(fdecl) Or (TFuncDecl(decl) And (decl.attrs & FUNC_METHOD) And (decl.attrs & FUNC_PTR)) Then
' munged = "_" + munged.ToLower()
'End If
Else
fdecl.munged="bb_"+fdecl.ident
End If
funcs.AddLast fdecl
End Method
Method MungSymbol:String(sym:String)
Select sym
Case "*"
Return "_mul"
Case "/"
Return "_div"
Case "+"
Return "_add"
Case "-"
Return "_sub"
Case "&"
Return "_and"
Case "|"
Return "_or"
Case "~~"
Return "_xor"
Case "^"
Return "_pow"
Case ":*"
Return "_muleq"
Case ":/"
Return "_diveq"
Case ":+"
Return "_addeq"
Case ":-"
Return "_subeq"
Case ":&"
Return "_andeq"
Case ":|"
Return "_oreq"
Case ":~~"
Return "_xoreq"
Case ":^"
Return "_poweq"
Case "<"
Return "_lt"
Case "<="
Return "_le"
Case ">"
Return "_gt"
Case ">="
Return "_ge"
Case "="
Return "_eq"
Case "<>"
Return "_ne"
Case "mod"
Return "_mod"
Case "shl"
Return "_shl"
Case "shr"
Return "_shr"
Case ":mod"
Return "_modeq"
Case ":shl"
Return "_shleq"
Case ":shr"
Return "_shreq"
Case "[]"
Return "_iget"
Case "[]="
Return "_iset"
End Select
Err "?? unknown symbol ?? : " + sym
End Method
Method MungDecl( decl:TDecl, addIfNotInScope:Int = False )
If decl.munged Then
' ensure function args get into local scope correctly.
If addIfNotInScope Then
If Not mungScope.Contains( decl.munged ) Then
mungScope.Insert(decl.munged, decl)
End If
End If
Return
End If
Local fdecl:TFuncDecl=TFuncDecl( decl )
' apply mangling to methods and New (ctors)
' but don't apply mangling to function pointers
If fdecl And fdecl.ClassScope() And Not (fdecl.attrs & FUNC_PTR)
MungFuncDecl( fdecl )
Return
End If
Local id$=decl.ident,munged$
'this lot just makes output a bit more readable...
' Select ENV_LANG
' Case "js"
' If TModuleDecl( decl.scope ) Or TGlobalDecl( decl ) Or (fdecl And Not fdecl.IsMethod())
' munged=decl.ModuleScope().munged+"_"+id
' EndIf
' Case "as"
' If TModuleDecl( decl.scope )
' munged=decl.ModuleScope().munged+"_"+id
' EndIf
' Case "cs"
' If TClassDecl( decl )
' munged=decl.ModuleScope().munged+"_"+id
' EndIf
' Case "java"
' If TClassDecl( decl )
' munged=decl.ModuleScope().munged+"_"+id
' EndIf
' Case "cpp"
If Not munged And TFuncDecl(decl) And TFuncDecl(decl).exported Then
munged = id
Else
If TModuleDecl( decl.scope ) Or (TGlobalDecl(decl) And TModuleDecl(TGlobalDecl(decl).mscope))
munged=decl.ModuleScope().munged+"_"+id
If TClassDecl(decl) And TClassDecl(decl).instArgs Then
For Local ty:TType = EachIn TClassDecl(decl).instArgs
munged :+ TransMangleType(ty)
Next
End If
EndIf
If TModuleDecl( decl )
munged=decl.ModuleScope().munged+"_"+id
EndIf
End If
' End Select
'DebugStop
If Not munged
If TLocalDecl( decl )
munged="bbt_"+id
Else If TLoopLabelDecl(decl)
munged = "_" + TLoopLabelDecl(decl).realIdent
Else
If decl.scope Then
munged = decl.scope.munged + "_" + id
If TClassDecl(decl) And TClassDecl(decl).instArgs Then
For Local ty:TType = EachIn TClassDecl(decl).instArgs
munged :+ TransMangleType(ty)
Next
End If
' fields are lowercase with underscore prefix.
' a function pointer with FUNC_METHOD is a field function pointer.
If TFieldDecl(decl) Or (TFuncDecl(decl) And (decl.attrs & FUNC_METHOD) And (decl.attrs & FUNC_PTR)) Then
munged = "_" + munged.ToLower()
End If
Else
munged="bb_"+id
End If
EndIf
EndIf
'sanitize non-mung-able characters
munged = TStringHelper.Sanitize(munged)
Local scopeSearch:Int = 0
If fdecl And Not fdecl.ClassScope() And Not (fdecl.attrs & FUNC_PTR)
scopeSearch = 1
End If
'add an increasing number to identifier if already used
If MungScopeContains( munged, scopeSearch )
If TFuncDecl(decl) And TFuncDecl(decl).exported Then
Err "Cannot export duplicate identifiers : " + decl.ident
End If
Local i:Int=1
Repeat
i:+1
Until Not MungScopeContains( munged + i, scopeSearch )
munged :+ i
EndIf
mungScope.Insert(munged, decl)
If scopeSearch Then
globalMungScope.Insert(munged, decl)
End If
decl.munged=munged
' a function pointers' real function is stored in "func" - need to set its munged to match the parent.
If TValDecl(decl) Then
If TFunctionPtrType(TValDecl(decl).ty) Then
TFunctionPtrType(TValDecl(decl).ty).func.munged = munged
End If
End If
End Method
Method MungScopeContains:Int( munged:String, scopeSearch:Int )
If Not scopeSearch Then
Return mungScope.Contains( munged )
End If
Return globalMungScope.Contains( munged )
End Method
Rem
Method MungDecl( decl:TDecl )
If decl.munged Return
Local fdecl:TFuncDecl=TFuncDecl( decl )
If fdecl And fdecl.IsMethod() Then
' DebugStop
MungFuncDecl( fdecl )
Return
End If
Local id:String=decl.ident,munged$,scope$
If TLocalDecl( decl )
scope="$"
munged="t_"+id
Else If TClassDecl( decl )
scope=""
munged="c_"+id
Else If TModuleDecl( decl )
scope=""
munged="bb_"+id
Else If TClassDecl( decl.scope )
scope=decl.scope.munged
munged="m_"+id
Else If TModuleDecl( decl.scope )
'If ENV_LANG="cs" Or ENV_LANG="java"
' scope=decl.scope.munged
' munged="g_"+id
'Else
scope=""
munged=decl.scope.munged+"_"+id
'EndIf
Else
InternalErr
EndIf
Local set:TMap=TMap(mungedScopes.ValueForKey( scope ))
If set
If set.Contains( munged.ToLower() )
Local id:Int=1
Repeat
id :+ 1
Local t$=munged+id
If set.Contains( t.ToLower() ) Continue
munged=t
Exit
Forever
End If
Else
If scope="$"
Print "OOPS2"
InternalErr
EndIf
set=New TMap
mungedScopes.Insert scope,set
End If
set.Insert munged.ToLower(), ""
decl.munged=munged
End Method
End Rem
Method Bra$( str$ )
If str.StartsWith( "(" ) And str.EndsWith( ")" )
Local n:Int=1
For Local i:Int=1 Until str.Length-1
Select str[i..i+1]
Case "("
n:+1
Case ")"
n:-1
If Not n Return "("+str+")"
End Select
Next
If n=1 Return str
' If str.FindLast("(")<str.Find(")") Return str
EndIf
Return "("+str+")"
End Method
'Utility C/C++ style...
Method Enquote$( str$ )
Return LangEnquote( str )
End Method
Method EscapeChars:String(str:String)
If str Then
Local found:Int = False
For Local i:Int = 0 Until str.length
If str[i] > 127 Then
found = True
Exit
End If
Next
If Not found Then
Return str
End If
Local s:String
For Local i:Int = 0 Until str.length
Local char:Int = str[i]
If char < 128 Then
s :+ Chr(char)
Else
s :+ "~~" + char + "~~"
End If
Next
Return s
End If
End Method
Method TransUnaryOp$( op$ )
Select op
Case "+" Return "+"
Case "-" Return "-"
Case "~~" Return op
Case "not" Return "!"
End Select
InternalErr "TTranslator.TransUnaryOp"
End Method
Method TransBinaryOp$( op$,rhs$ )
'DebugLog "TransBinaryOp '" + op + "' : '" + rhs + "'"
op = mapSymbol(op)
Select op
Case "+","-"
If rhs.StartsWith( op ) Return op+" "
Return op
Case "*","/" Return op
Case "shl" Return "<<"
Case "shr" Return ">>"
Case "sar" Return ">>"
Case "mod" Return " % "
Case "and" Return " && "
Case "or" Return " || "
Case "=" Return "=="
Case "<>" Return "!="
Case "<","<=",">",">=" Return op
Case "=<" Return "<="
Case "=>" Return ">="
Case "&","|" Return op
Case "~~" Return "^"
Case "<<", ">>" Return Op
Case "%" Return Op
End Select
InternalErr "TTranslator.TransBinaryOp"
End Method
Method TransAssignOp$( op$ )
op = mapSymbol(op)
Select op
Case ":mod" Return "%="
Case ":shl" Return "<<="
Case ":shr" Return ">>="
Case ":sar" Return ">>="
End Select
Return op
End Method
Method ExprPri:Int( expr:TExpr )
'
'1=primary,
'2=postfix
'3=prefix
'
If TNewObjectExpr( expr )
Return 3
Else If TUnaryExpr( expr )
Select TUnaryExpr( expr ).op
Case "+","-","~~","not" Return 3
End Select
InternalErr "TTranslator.ExprPri"
Else If TBinaryExpr( expr )
Select TBinaryExpr( expr ).op
Case "^" Return 4
Case "*","/","mod","%" Return 5
Case "+","-" Return 6
Case "shl","shr", "sar","<<", ">>" Return 7
Case "<","<=",">",">=", "=<", "=>" Return 8
Case "=","<>" Return 9
Case "&" Return 10
Case "~~" Return 11
Case "|" Return 12
Case "and" Return 13
Case "or" Return 14
End Select
InternalErr "TTranslator.ExprPri"
EndIf
Return 2
End Method
Method TransSubExpr$( expr:TExpr,pri:Int=2 )
Local t_expr$=expr.Trans()
'If expr.exprType._flags & TTYPE.T_VAR Then
' t_expr = Bra("*" + t_expr)
'End If
If ExprPri( expr )>pri t_expr=Bra( t_expr )
Return t_expr
End Method
Method TransExprNS$( expr:TExpr )
If TVarExpr( expr ) Return expr.Trans()
If TConstExpr( expr ) Return expr.Trans()
Return CreateLocal( expr )
End Method
Method CreateLocal$( expr:TExpr, init:Int = True, vol:Int = True )
Local tmp:TLocalDecl=New TLocalDecl.Create( "",expr.exprType,expr, True, , vol )
MungDecl tmp
Emit TransLocalDecl( tmp,expr, True, init )+";"
EmitCoverage(_errInfo)
EmitGDBDebug(_errInfo)
Return tmp.munged
End Method
'***** Utility *****
Method TransLocalDecl$( decl:TLocalDecl,init:TExpr, declare:Int = False, outputInit:Int = True ) Abstract
Method TransGlobalDecl$( gdecl:TGlobalDecl ) Abstract
Method EmitPushErr()
End Method
Method EmitSetErr( errInfo$ )
End Method
Method EmitPopErr()
End Method
'***** Declarations *****
Method TransGlobal$( decl:TGlobalDecl ) Abstract
Method TransField$( decl:TFieldDecl,lhs:TExpr ) Abstract
Method TransFunc$( decl:TFuncDecl,args:TExpr[],lhs:TExpr, sup:Int = False, scope:TScopeDecl = Null ) Abstract
Method TransSuperFunc$( decl:TFuncDecl,args:TExpr[], scope:TScopeDecl ) Abstract
'***** Expressions *****
Method TransConstExpr$( expr:TConstExpr ) Abstract
Method TransNewObjectExpr$( expr:TNewObjectExpr ) Abstract
Method TransNewArrayExpr$( expr:TNewArrayExpr ) Abstract
Method TransSelfExpr$( expr:TSelfExpr ) Abstract
Method TransCastExpr$( expr:TCastExpr ) Abstract
Method TransUnaryExpr$( expr:TUnaryExpr ) Abstract
Method TransBinaryExpr$( expr:TBinaryExpr ) Abstract
Method TransIndexExpr$( expr:TIndexExpr ) Abstract
Method TransSliceExpr$( expr:TSliceExpr ) Abstract
Method TransArrayExpr$( expr:TArrayExpr ) Abstract
Method TransArraySizeExpr$ ( expr:TArraySizeExpr ) Abstract
Method TransIntrinsicExpr$( decl:TDecl,expr:TExpr,args:TExpr[]=Null ) Abstract
Method TransArgs$( args:TExpr[],decl:TFuncDecl, objParam:String = Null ) Abstract
Method EmitDebugEnterScope(block:TBlockDecl) Abstract
Method EmitLocalDeclarations(decl:TScopeDecl, v:TValDecl = Null) Abstract
Method TransType$( ty:TType, ident:String, fpReturnTypeFunctionArgs:String = Null, fpReturnTypeClassFunc:Int = False) Abstract
Method TransObject:String(decl:TScopeDecl, this:Int = False) Abstract
Method BeginLocalScope()
mungStack.Push mungScope
mungScope:TMap=New TMap'<TDecl>
' mungedScopes.Insert "$",New TMap
If opt_debug Then
localScopeStack.Push localScope
localScope = New TStack
End If
End Method
Method EndLocalScope()
mungScope=TMap(mungStack.Pop())
' mungedScopes.Insert "$",Null
If opt_debug Then
localScope = TStack(localScopeStack.Pop())
End If
End Method
Rem
Method MungMethodDecl( fdecl:TFuncDecl )
If fdecl.munged Return
If fdecl.overrides
MungMethodDecl fdecl.overrides
fdecl.munged=fdecl.overrides.munged
Return
EndIf
Local funcs:=funcMungs.Get( fdecl.ident )
If funcs
For Local tdecl:=EachIn funcs
If fdecl.EqualsArgs( tdecl )
fdecl.munged=tdecl.munged
Return
EndIf
Next
Else
funcs=New FuncDeclList
funcMungs.Set fdecl.ident,funcs
EndIf
Local id:=fdecl.ident
If mungedFuncs.Contains( id )
Local n:=1
Repeat
n+=1
id=fdecl.ident+String(n)
Until Not mungedFuncs.Contains( id )
EndIf
mungedFuncs.Set id,fdecl
fdecl.munged="p_"+id
funcs.AddLast fdecl
End
End Rem
'***** Simple statements *****
'Expressions
Method TransStmtExpr$( expr:TStmtExpr )
Local t$=expr.stmt.Trans()
If t Emit t+";"
Return expr.expr.Trans()
End Method
Method TransTemplateCast$( ty:TType,src:TType,expr$ )
Return expr
End Method
Method TransVarExpr$( expr:TVarExpr )
Local decl:TVarDecl=TVarDecl( expr.decl.actual )
If decl.munged.StartsWith( "$" ) Return TransIntrinsicExpr( decl,Null )
If TLocalDecl( decl ) Then
If decl.ty._flags & TType.T_VAR Then
Return "*" + decl.munged
Else
Return decl.munged
End If
End If
If TFieldDecl( decl ) Return TransField( TFieldDecl( decl ),Null )
If TGlobalDecl( decl ) Return TransGlobal( TGlobalDecl( decl ) )
InternalErr "TTranslator.TransVarExpr"
End Method
Method TransMemberVarExpr$( expr:TMemberVarExpr )
Local decl:TVarDecl=TVarDecl( expr.decl.actual )
If decl.munged.StartsWith( "$" ) Return TransIntrinsicExpr( decl,expr.expr )
If TFieldDecl( decl ) Return TransField( TFieldDecl( decl ),expr.expr )
If TGlobalDecl( decl ) Return TransGlobal( TGlobalDecl( decl ) )
InternalErr "TTranslator.TransMemberVarExpr"
End Method
Method TransInvokeExpr$( expr:TInvokeExpr )
Local decl:TFuncDecl=TFuncDecl( expr.decl.actual ),t$
If Not decl.munged Then
MungDecl decl
End If