forked from bmx-ng/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.bmx
1252 lines (1015 loc) · 29.9 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-2014 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 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 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:=New StringMap<FuncDeclList>
' Field mungedFuncs:=New StringMap<FuncDecl>
Field debugOut:String
Method PushVarScope()
varStack.Push customVarStack
customVarStack = New TStack
End Method
Method PopVarScope()
customVarStack=TStack(varStack.Pop())
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 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 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
Method MungFuncDecl( fdecl:TFuncDecl )
If fdecl.munged Return
If fdecl.overrides
MungFuncDecl fdecl.overrides
fdecl.munged=fdecl.overrides.munged
Return
EndIf
Local funcs:TFuncDeclList=TFuncDeclList(funcMungs.ValueForKey( fdecl.ident ))
If funcs
For Local tdecl:TFuncDecl=EachIn funcs
If fdecl.argDecls.Length=tdecl.argDecls.Length
Local match:Int=True
For Local i:Int=0 Until fdecl.argDecls.Length
Local ty:TType=TArgDecl( fdecl.argDecls[i].actual ).ty
Local ty2:TType=TArgDecl( tdecl.argDecls[i].actual ).ty
If ty.EqualsType( ty2 ) Continue
match=False
Exit
Next
If match
fdecl.munged=tdecl.munged
Return
EndIf
EndIf
Next
Else
funcs=New TFuncDeclList
funcMungs.Insert fdecl.ident,funcs
EndIf
fdecl.munged="bbm_"+fdecl.ident
If Not funcs.IsEmpty() fdecl.munged:+String(funcs.Count()+1)
funcs.AddLast fdecl
End Method
Method MungDecl( decl:TDecl, allowDupes:Int = False )
If decl.munged Return
Local fdecl:TFuncDecl=TFuncDecl( decl )
'If fdecl And fdecl.IsMethod()
' 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 TModuleDecl( decl.scope )
munged=decl.ModuleScope().munged+"_"+id
EndIf
If TModuleDecl( decl )
munged=decl.ModuleScope().munged+"_"+id
EndIf
' End Select
'DebugStop
If Not munged
If TLocalDecl( decl )
munged="bbt_"+id
Else
If decl.scope Then
munged = decl.scope.munged + "_" + id
' 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)
'add an increasing number to identifier if already used
If mungScope.Contains( munged )
Local i:Int=1
Repeat
i:+1
Until Not mungScope.Contains( munged + i )
munged :+ i
EndIf
mungScope.Insert(munged, decl)
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
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 TransUnaryOp$( op$ )
Select op
Case "+" Return "+"
Case "-" Return "-"
Case "~~" Return op
Case "not" Return "!"
End Select
InternalErr
End Method
Method TransBinaryOp$( op$,rhs$ )
'DebugLog "TransBinaryOp '" + op + "' : '" + rhs + "'"
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 "^"
End Select
InternalErr
End Method
Method TransAssignOp$( 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
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
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 )
Local tmp:TLocalDecl=New TLocalDecl.Create( "",expr.exprType,expr )
MungDecl tmp
Emit TransLocalDecl( tmp.munged,expr )+";"
Return tmp.munged
End Method
'***** Utility *****
Method TransLocalDecl$( munged$,init:TExpr ) Abstract
Method TransGlobalDecl$( munged$,init:TExpr, attrs:Int, ty:TType ) 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 BeginLocalScope()
mungStack.Push mungScope
mungScope:TMap=New TMap'<TDecl>
' mungedScopes.Insert "$",New TMap
End Method
Method EndLocalScope()
mungScope=TMap(mungStack.Pop())
' mungedScopes.Insert "$",Null
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 ) Return decl.munged
If TFieldDecl( decl ) Return TransField( TFieldDecl( decl ),Null )
If TGlobalDecl( decl ) Return TransGlobal( TGlobalDecl( decl ) )
InternalErr
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 )
InternalErr
End Method
Method TransInvokeExpr$( expr:TInvokeExpr )
Local decl:TFuncDecl=TFuncDecl( expr.decl.actual ),t$
'If decl.ident = "OnDebugStop" DebugStop
If Not decl.munged Then
MungDecl decl
End If
If (decl.attrs & FUNC_PTR) And (decl.attrs & FUNC_INIT) And Not expr.InvokedWithBraces Return decl.munged
If ((decl.attrs & FUNC_PTR) Or (expr.decl.attrs & FUNC_PTR)) And Not expr.InvokedWithBraces Return decl.munged
If decl.munged.StartsWith( "$" ) Return TransIntrinsicExpr( decl,Null,expr.args )
If decl Return TransFunc( TFuncDecl(decl),expr.args,Null )
InternalErr
End Method
Method TransInvokeMemberExpr$( expr:TInvokeMemberExpr )
Local decl:TFuncDecl=TFuncDecl( expr.decl.actual ),t$
If decl.munged.StartsWith( "$" ) Return TransIntrinsicExpr( decl,expr.expr,expr.args )
If decl Return TransFunc( TFuncDecl(decl),expr.args,expr.expr )
InternalErr
End Method
Method TransInvokeSuperExpr$( expr:TInvokeSuperExpr )
Local decl:TFuncDecl=TFuncDecl( expr.origFuncDecl.actual ),t$
If decl.munged.StartsWith( "$" ) Return TransIntrinsicExpr( decl,expr )
If decl Return TransSuperFunc( TFuncDecl( decl ),expr.args, expr.classScope )
InternalErr
End Method
Method TransFuncCallExpr:String( expr:TFuncCallExpr )
If TIndexExpr(expr.expr) And TArrayType(TIndexExpr(expr.expr).expr.exprType) And TFunctionPtrType(TArrayType(TIndexExpr(expr.expr).expr.exprType).elemType) Then
Local decl:TDecl = TFunctionPtrType(TArrayType(TIndexExpr(expr.expr).expr.exprType).elemType).func.actual
decl.Semant()
expr.args=expr.CastArgs( expr.args,TFuncDecl(decl) )
Return expr.expr.Trans() + TransArgs(expr.args, TFuncDecl(decl))
End If
InternalErr
End Method
Method TransExprStmt$( stmt:TExprStmt )
Return stmt.expr.TransStmt()
End Method
Method TransAssignStmt$( stmt:TAssignStmt )
If stmt.rhs Return stmt.lhs.TransVar()+TransAssignOp(stmt.op)+stmt.rhs.Trans()
Return stmt.lhs.Trans()
End Method
Method TransReturnStmt$( stmt:TReturnStmt )
Local t$="return"
unreachable=True
If stmt.expr Then
If TObjectType(stmt.expr.exprType) And TNullDecl(TObjectType(stmt.expr.exprType).classDecl) Then
If IsPointerType(stmt.fRetType, 0, TType.T_POINTER) Or IsNumericType(stmt.fRetType) Then
Return t + " 0"
End If
If TStringType(stmt.fRetType) Then
Return t + " &bbEmptyString"
End If
If TArrayType(stmt.fRetType) Then
Return t + " &bbEmptyArray"
End If
End If
t:+" "+stmt.expr.Trans()
End If
EmitTryStack()
Return t
End Method
Method NextExitId:Int(bc:TTryBreakCheck)
If Not bc.exitId Then
exitLabelId :+ 1
bc.exitId = exitLabelId
End If
Return bc.exitId
End Method
Method NextContId:Int(bc:TTryBreakCheck)
If Not bc.contId Then
contLabelId :+ 1
bc.contId = contLabelId
End If
Return bc.contId
End Method
Method TransContinueStmt$( stmt:TContinueStmt )
unreachable=True
Local contLoop:TStmt
' if we are continuing with a loop label, we'll need to find it in the stack
If stmt.label And TLoopLabelExpr(stmt.label) Then
contLoop = TLoopLabelExpr(stmt.label).loop
End If
' get count of Try statements in the stack in this loop
Local count:Int = LoopTryDepth(contLoop)
If count > 0 Then
Local bc:TTryBreakCheck = GetTopLoop(contLoop)
If bc Then
NextContId(bc)
For Local i:Int = 0 Until count
Emit "bbExLeave();"
Next
Emit "goto " + TransLabelCont(bc, False)
Else
InternalErr
End If
Else
' No Try statements in the stack here..
If stmt.label And TLoopLabelExpr(stmt.label) Then
Emit "goto " + TransLoopLabelCont(TLoopLabelExpr(stmt.label).loop.loopLabel.ident, False)
Else
Return "continue"
End If
End If
End Method
Method TransBreakStmt$( stmt:TBreakStmt )
unreachable=True
broken:+1
Local brkLoop:TStmt
' if we are exiting with a loop label, we'll need to find it in the stack
If stmt.label And TLoopLabelExpr(stmt.label) Then
brkLoop = TLoopLabelExpr(stmt.label).loop
End If
' get count of Try statements in the stack in this loop
Local count:Int = LoopTryDepth(brkLoop)
If count > 0 Then
Local bc:TTryBreakCheck = GetTopLoop(brkLoop)
If bc Then
NextExitId(bc)
For Local i:Int = 0 Until count
Emit "bbExLeave();"
Next
Emit "goto " + TransLabelExit(bc, False)
Else
InternalErr
End If
Else
' No Try statements in the stack here..
If stmt.label And TLoopLabelExpr(stmt.label) Then
Emit "goto " + TransLoopLabelExit(TLoopLabelExpr(stmt.label).loop.loopLabel.ident, False)
Else
Return "break"
End If
End If
End Method
Method TransTryStmt$( stmt:TTryStmt )
End Method
Method EmitTryStack() Abstract
Method TransThrowStmt$( stmt:TThrowStmt )
End Method
Method TransBuiltinExpr$( expr:TBuiltinExpr )
If TMinExpr(expr) Return TransMinExpr(TMinExpr(expr))
If TMaxExpr(expr) Return TransMaxExpr(TMaxExpr(expr))
If TAbsExpr(expr) Return TransAbsExpr(TAbsExpr(expr))
If TAscExpr(expr) Return TransAscExpr(TAscExpr(expr))
If TSgnExpr(expr) Return TransSgnExpr(TSgnExpr(expr))
If TLenExpr(expr) Return TransLenExpr(TLenExpr(expr))
If TSizeOfExpr(expr) Return TransSizeOfExpr(TSizeOfExpr(expr))
Err "TODO : TransBuiltinExpr()"
End Method
Method TransMinExpr:String(expr:TMinExpr)
End Method
Method TransMaxExpr:String(expr:TMaxExpr)
End Method
Method TransAbsExpr:String(expr:TAbsExpr)
End Method
Method TransAscExpr:String(expr:TAscExpr)
End Method
Method TransSgnExpr:String(expr:TSgnExpr)
End Method
Method TransLenExpr:String(expr:TLenExpr)
End Method
Method TransSizeOfExpr:String(expr:TSizeOfExpr)
End Method
Method TransLabelCont:String(bc:TTryBreakCheck, jmp:Int = True)
If jmp Then
Return "_contjmp" + bc.contId + ": ;"
Else
Return "_contjmp" + bc.contId + ";"
End If
End Method
Method TransLabelExit:String(bc:TTryBreakCheck, jmp:Int = True)
If jmp Then
Return "_exitjmp" + bc.exitId + ": ;"
Else
Return "_exitjmp" + bc.exitId + ";"
End If
End Method
Method TransLoopLabelCont:String(id:String, jmp:Int = True)
If jmp Then
Return "_loopcont_" + id.ToLower() + ": ;"
Else
Return "_loopcont_" + id.ToLower() + ";"
End If
End Method
Method TransLoopLabelExit:String(id:String, jmp:Int = True)
If jmp Then
Return "_loopexit_" + id.ToLower() + ": ;"
Else
Return "_loopexit_" + id.ToLower() + ";"
End If
End Method
'***** Block statements - all very C like! *****
Method Emit( t$, useIndent:Int = True )
If Not t Return
If useIndent
If t.StartsWith( "}" )
indent=indent[..indent.Length-1]
EndIf
End If
LINES.AddLast indent+t
'code+=indent+t+"~n"
If useIndent
If t.EndsWith( "{" )
indent:+"~t"
EndIf
End If
End Method
Method JoinLines$( file:String )
Local _lines:TStringList = TStringList(outputFiles.ValueForKey(file))
Local code$=_lines.Join( "~n" )
_lines.Clear
Return code
End Method
'returns unreachable status!
Method EmitBlock:Int( block:TBlockDecl )
'DebugStop
'If ENV_CONFIG="debug"
' If TFuncDecl( block ) EmitPushErr
'EndIf
PushEnv block
For Local stmt:TStmt=EachIn block.stmts
_errInfo=stmt.errInfo
If unreachable And ENV_LANG<>"as"
'If stmt.errInfo Print "Unreachable:"+stmt.errInfo
Exit
EndIf
Rem
If ENV_CONFIG="debug"
Local rs:TReturnStmt=TReturnStmt( stmt )
If rs
If rs.expr
EmitSetErr stmt.errInfo
Local t_expr$=TransExprNS( rs.expr )
EmitPopErr
Emit "return "+t_expr+";"
Else
EmitPopErr
Emit "return;"
EndIf
unreachable=True
Continue
EndIf
EmitSetErr stmt.errInfo
EndIf
End Rem
Local t$=stmt.Trans()
If t Emit t+";"
If DEBUG And debugOut Then
Emit debugOut
debugOut = Null
End If
Local v:String = String(customVarStack.Pop())
While v
Emit "bbMemFree" + Bra(v) + ";"
v = String(customVarStack.Pop())
Wend
Next
Local r:Int=unreachable
unreachable=False
PopEnv
Return r
End Method
Method TransDeclStmt$( stmt:TDeclStmt )
Local decl:TLocalDecl=TLocalDecl( stmt.decl )
If decl
MungDecl decl
Return TransLocalDecl( decl.munged,decl.init )
EndIf
Local cdecl:TConstDecl=TConstDecl( stmt.decl )
If cdecl
Return Null
EndIf
Local gdecl:TGlobalDecl=TGlobalDecl( stmt.decl )
If gdecl Then
MungDecl gdecl
If gdecl.inited Return Null
Return TransGlobalDecl( gdecl.munged, gdecl.init, gdecl.attrs, gdecl.ty )
End If
InternalErr
End Method
Method TransIfStmt$( stmt:TIfStmt )
If TConstExpr( stmt.expr )
If TConstExpr( stmt.expr ).value
' Emit "if"+Bra( stmt.expr.Trans() )+"{"
If EmitBlock( stmt.thenBlock ) unreachable=True
' Emit "}"
Else If stmt.elseBlock.stmts.First()
' Emit "if(!"+Bra( stmt.expr.Trans() )+"){"
If EmitBlock( stmt.elseBlock ) unreachable=True
' Emit "}"
EndIf
Else If stmt.elseBlock.stmts.First()
Emit "if"+Bra( stmt.expr.Trans() )+"{"
FreeVarsIfRequired(False)
PushVarScope
Local unr:Int=EmitBlock( stmt.thenBlock )
PopVarScope
Emit "}else{"
FreeVarsIfRequired
Local unr2:Int=EmitBlock( stmt.elseBlock )
Emit "}"
If unr And unr2 unreachable=True
Else
' Emit "if"+ Bra(TransCondition(stmt.expr)) + "{"
' If TVarExpr(stmt.expr) Then
' If TObjectType(TVarExpr(stmt.expr).exprType) Then
' Emit "if"+Bra( stmt.expr.Trans() + "!= &bbNullObject") + "{"
' Else If TStringType(TVarExpr(stmt.expr).exprType) Then
' Emit "if"+Bra( stmt.expr.Trans() + "!= &bbEmptyString") + "{"
' Else
' Emit "if"+Bra( stmt.expr.Trans() )+"{"
' End If
' Else
Emit "if"+Bra( stmt.expr.Trans() )+"{"
FreeVarsIfRequired(False)
' End If
PushVarScope
Local unr:Int=EmitBlock( stmt.thenBlock )
PopVarScope
Emit "}"
FreeVarsIfRequired
EndIf
End Method
Method FreeVarsIfRequired(removeFromStack:Int = True)
If removeFromStack
Local v:String = String(customVarStack.Pop())
While v
Emit "bbMemFree" + Bra(v) + ";"
v = String(customVarStack.Pop())
Wend
Else
For Local v:String = EachIn customVarStack
Emit "bbMemFree" + Bra(v) + ";"
Next
End If
End Method
' Method TransCondition:String(expr:TExpr)
' If TVarExpr(expr) Then
' If TObjectType(TVarExpr(expr).exprType) Then
' Return Bra( expr.Trans() + "!= &bbNullObject")
' Else If TStringType(TVarExpr(expr).exprType) Then
' Return Bra( expr.Trans() + "!= &bbEmptyString")
' Else
' Return Bra( expr.Trans() )
' End If
' Else
' Return Bra( expr.Trans() )
' End If
' End Method
Method TransWhileStmt$( stmt:TWhileStmt )
Local nbroken:Int=broken
Emit "while"+Bra( stmt.expr.Trans() )+"{"
Local check:TTryBreakCheck = New TTryBreakCheck
check.stmt = stmt
PushLoopTryStack(check)
Local unr:Int=EmitBlock( stmt.block )
PopLoopTryStack
If check.contId Then
Emit TransLabelCont(check)
End If
If stmt.loopLabel Then
Emit TransLoopLabelCont(stmt.loopLabel.ident, True)
End If
Emit "}"
If check.exitId Then
Emit TransLabelExit(check)
End If
If stmt.loopLabel Then
Emit TransLoopLabelExit(stmt.loopLabel.ident, True)
End If
If broken=nbroken And TConstExpr( stmt.expr ) And TConstExpr( stmt.expr ).value unreachable=True
broken=nbroken