forked from bmx-ng/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctranslator.bmx
3883 lines (3178 loc) · 106 KB
/
ctranslator.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.
'
SuperStrict
Import "parser.bmx"
Type TCTranslator Extends TTranslator
Field _app:TAppDecl
'Field stringConstCount:Int
Field prefix:String
Method New()
_trans = Self
End Method
Method TransSPointer$( ty:TType, withVar:Int = False )
Local p:String
If ty
If withVar And (ty._flags & TType.T_VAR) Then
p:+ "*"
End If
If ty._flags & TType.T_PTR Then
p:+ "*"
End If
If ty._flags & TType.T_PTRPTR Then
p:+ "*"
End If
If ty._flags & TType.T_PTRPTRPTR Then
p:+ "*"
End If
End If
Return p
End Method
Method TransArrayType$( ty:TType)
Local p:String = TransSPointer(ty)
If TByteType( ty ) Return "~q" + p + "b~q"
If TShortType( ty ) Return "~q" + p + "s~q"
If TIntType( ty ) Return "~q" + p + "i~q"
If TFloatType( ty ) Return "~q" + p + "f~q"
If TDoubleType( ty ) Return "~q" + p + "d~q"
If TLongType( ty ) Return "~q" + p + "l~q"
If TStringType( ty ) Return "~q$~q"
If TArrayType( ty ) Return "~q[~q"
If TObjectType( ty ) Return "~q:~q"
If TFunctionPtrType( ty ) Return "~q(~q"
End Method
Method TransDebugScopeType$(ty:TType)
Local p:String = TransSPointer(ty)
If TByteType( ty ) Return p + "b"
If TShortType( ty ) Return p + "s"
If TIntType( ty ) Return p + "i"
If TFloatType( ty ) Return p + "f"
If TDoubleType( ty ) Return p + "d"
If TLongType( ty ) Return p + "l"
If TStringType( ty ) Return "$"
If TArrayType( ty ) Then
Return "[]" + TransDebugScopeType(TArrayType( ty ).elemType)
End If
If TObjectType( ty ) Then
Return ":" + TObjectType( ty ).classDecl.ident
End If
End Method
Method TransType$( ty:TType, ident:String)
Local p:String = TransSPointer(ty, True)
If TVoidType( ty ) Or Not ty Then
'DebugStop
Return "void"
End If
If TBoolType( ty ) Return "BBINT" + p
If TByteType( ty ) Return "BBBYTE" + p
If TShortType( ty ) Return "BBSHORT" + p
If TIntType( ty ) Return "BBINT" + p
If TFloatType( ty ) Return "BBFLOAT" + p
If TDoubleType( ty ) Return "BBDOUBLE" + p
If TLongType( ty ) Return "BBLONG" + p
If TStringType( ty ) Then
If ty._flags & TType.T_CHAR_PTR Then
Return "BBBYTE *"
Else If ty._flags & TType.T_CHAR_PTR Then
Return "BBSHORT *"
End If
Return "BBSTRING" + p
End If
'If TStringVarPtrType( ty ) Return "BBSTRING *"
If TArrayType( ty ) Return "BBARRAY" + p
If TObjectType( ty ) Then
Return TransObject(TObjectType(ty).classdecl) + p
End If
If TFunctionPtrType( ty ) Then
TFunctionPtrType(ty).func.Semant
Local retType:String = TransType(TFunctionPtrType(ty).func.retType, "")
Local args:String
For Local arg:TArgDecl = EachIn TFunctionPtrType(ty).func.argDecls
arg.Semant()
If args Then
args :+ ","
End If
args :+ TransType(arg.ty, "")
Next
Return retType + Bra("* " + ident) + Bra(args)
End If
If TExternObjectType( ty ) Return "struct " + TExternObjectType( ty ).classDecl.munged + p
InternalErr
End Method
Method TransIfcType$( ty:TType )
Local p:String = TransSPointer(ty)
If ty And (ty._flags & TType.T_VAR) Then
p :+ " Var"
End If
If TVoidType( ty ) Or Not ty Then
If opt_issuperstrict Then
Return p
Else
Return "%" + p
End If
End If
If TByteType( ty ) Return "@" + p
If TShortType( ty ) Return "@@" + p
If TIntType( ty ) Return "%" + p
If TFloatType( ty ) Return "#" + p
If TDoubleType( ty ) Return "!" + p
If TLongType( ty ) Return "%%" + p
If TStringType( ty ) Then
If ty._flags & TType.T_CHAR_PTR Then
Return "$z"
Else If ty._flags & TType.T_CHAR_PTR Then
Return "$w"
End If
Return "$" + p
End If
If TArrayType( ty ) Return TransIfcType(TArrayType( ty ).elemType) + "&[]"
If TObjectType( ty ) Return ":" + TObjectType(ty).classDecl.ident
If TFunctionPtrType( ty ) Return TransIfcType(TFunctionPtrType(ty).func.retType) + TransIfcArgs(TFunctionPtrType(ty).func)
If TExternObjectType( ty ) Return ":" + TExternObjectType(ty).classDecl.ident + p
InternalErr
End Method
Method TransRefType$( ty:TType, ident:String )
If TObjectType( ty ) And ty.GetClass().IsInterface() Return "gc_iptr<"+ty.GetClass().actual.munged+">"
Return TransType( ty, ident )
End Method
Method TransValue$( ty:TType,value$ )
If value
If TBoolType( ty ) Return "1"
If TShortType( ty ) Return value
If TIntType( ty ) Return value
If TLongType( ty ) Return value+"LL"
If TFloatType( ty ) Then
If value = "nan.0" Then
Return "bbPOSNANf"
Else If value="-nan.0" Then
Return "bbNEGNANf"
Else If value = "inf.0" Then
Return "bbPOSINFf"
Else If value = "-inf.0" Then
Return "bbNEGINFf"
Else
If value.ToLower().Find("e")>=0 Then
Return value
End If
If value.Find(".") < 0 Then
value :+ ".0"
End If
Return value+"f"
End If
End If
If TDoubleType( ty ) Then
If value = "nan.0" Then
Return "bbPOSNANd"
Else If value="-nan.0" Then
Return "bbNEGNANd"
Else If value = "inf.0" Then
Return "bbPOSINFd"
Else If value = "-inf.0" Then
Return "bbNEGINFd"
Else
If value.ToLower().Find("e") >=0 Then
Return value
End If
If value.Find(".") < 0 Then
value :+ ".0"
End If
Return value+"f"
End If
End If
If TStringType( ty ) Return TransStringConst(value )
If TByteType( ty ) Return value
Else
If TBoolType( ty ) Return "0"
If TNumericType( ty ) Return "0" ' numeric and pointers
If TStringType( ty ) Return "&bbEmptyString"
If TArrayType( ty ) Return "&bbEmptyArray"
If TObjectType( ty ) Then
If TObjectType( ty ).classDecl.IsExtern() Then
Return "0"
Else
Return "&bbNullObject"
End If
End If
If TFunctionPtrType( ty) Return "&brl_blitz_NullFunctionError" ' todo ??
'If TByteType( ty ) Return "0"
EndIf
InternalErr
End Method
Method TransArgs$( args:TExpr[],decl:TFuncDecl, objParam:String = Null )
'If decl.ident="addConnection" DebugStop
Local t$
If objParam And decl.IsMethod() Then
t:+ objParam
End If
For Local i:Int=0 Until decl.argDecls.Length
Local ty:TType = TArgDecl(decl.argDecls[i].actual).ty
If t t:+","
If i < args.length
If TNullExpr(args[i]) Then
t :+ TransValue(ty, Null)
Continue
Else If TStringType(ty) And (ty._flags & TType.T_VAR) Then
If TCastExpr(args[i]) And TStringType(TCastExpr(args[i]).expr.exprType) Then
t:+ "&"
End If
Else If TObjectType(ty) And (ty._flags & TType.T_VAR) Then
If TVarExpr(args[i]) And TObjectType(TVarExpr(args[i]).exprType) Then
t:+ "&"
End If
Else If TFunctionPtrType(ty) Or IsPointerType(ty, TType.T_BYTE) Then
If TInvokeExpr(args[i]) And Not TInvokeExpr(args[i]).decl.IsMethod() Then
If IsPointerType(ty, TType.T_BYTE) Then
t:+ TInvokeExpr(args[i]).Trans()
Else
' need to test scopes to see if we need to use the current instance's function or not
Local fdecl:TFuncDecl = TInvokeExpr(args[i]).decl
If TClassDecl(fdecl.scope) Then
' current scope is related to function scope?
If TClassDecl(_env.scope) And TFuncDecl(_env) And TFuncDecl(_env).IsMethod() Then
'If TClassDecl(decl.scope).ExtendsClass(TClassDecl(fdecl.scope)) Then
Local scope:TScopeDecl = _env.scope
Local obj:String = Bra("struct " + scope.munged + "_obj*")
Local class:String = "(" + obj + "o)->clas"
t:+ class + "->fn_" + fdecl.ident
Else
t:+ fdecl.munged
End If
Else
t:+ fdecl.munged
End If
End If
Continue
End If
' some cases where we are passing a function pointer via a void* parameter.
If TCastExpr(args[i]) And TInvokeExpr(TCastExpr(args[i]).expr) And Not TInvokeExpr(TCastExpr(args[i]).expr).invokedWithBraces Then
t:+ TInvokeExpr(TCastExpr(args[i]).expr).decl.munged
Continue
End If
'If TObjectType(args[i].exprType) 'And TObjectType(args[i].exprType).classDecl = TClassDecl.nullObjectClass Then
'err "NULL"
' t:+ "0"
' Continue
'End If
' Object -> Byte Ptr
If IsPointerType(ty, TType.T_BYTE) And TObjectType(args[i].exprType) Then
t:+ Bra("(BBBYTE*)" + Bra(args[i].Trans())) + "+" + Bra("sizeof(void*)")
Continue
End If
Else If IsNumericType(ty) Then
If TObjectType(args[i].exprType) 'And TObjectType(args[i].exprType).classDecl = TClassDecl.nullObjectClass Then
err "NULL"
t:+ "0"
Continue
End If
End If
t:+TransTemplateCast( ty,args[i].exprType,args[i].Trans() )
Else
decl.argDecls[i].Semant()
' default values
Local init:TExpr = decl.argDecls[i].init
If init Then
If TConstExpr(init) Then
If TObjectType(TConstExpr(init).exprType) Then
t:+"NULLNULLNULL"
' And TNullDecl(TObjectType(TConstExpr(init).exprType).classDecl)) Or (TConstExpr(init).value = "bbNullObject") Then
If TStringType(decl.argDecls[i].ty) Then
t :+ "&bbEmptyString"
Else If TArrayType(decl.argDecls[i].ty) Then
t :+ "&bbEmptyArray"
Else
t :+ "&bbNullObject"
End If
Else
t:+ decl.argDecls[i].init.Trans()
End If
Else If TFunctionPtrType(ty) Then
If TInvokeExpr(init) Then
t:+ TInvokeExpr(init).decl.munged
End If
Else
t:+ decl.argDecls[i].init.Trans()
End If
End If
End If
Next
Return Bra(t)
End Method
Method TransArgsTypes$( args:TExpr[],declArgTypes:TType[])
Local t$
For Local i:Int=0 Until args.Length
If t t:+","
t:+TransTemplateCast( declArgTypes[i],args[i].exprType,args[i].Trans() )
Next
Return Bra(t)
End Method
Method TransPtrCast$( ty:TType,src:TType,expr$,cast$ )
If IsPointerType(ty, 0, TType.T_POINTER | TType.T_VARPTR | TType.T_VAR) Or TFunctionPtrType(ty) Then
' TODO : pointer stuff
If TNullType(src) Return TransValue(ty, Null)
Return expr
End If
'If expr = "NULL" DebugStop
' If TIntType(ty) And TStringType(src) Then
'DebugStop
' Return "bbObjectDowncast" + Bra(expr + ",&" + TStringType(src).cDecl.munged)
' End If
If TNullType(src)
Return TransValue(ty, Null)
End If
If TStringType(ty) And TObjectType(src) Then
If Not TStringType(ty).cDecl Then
ty.Semant()
End If
'If TNullDecl(TObjectType(src).classDecl) Then
' Return "&bbEmptyString"
'End If
Return "bbObjectDowncast" + Bra(expr + ",&" + TStringType(ty).cDecl.munged)
End If
'If TArrayType(ty) And TObjectType(src) Then
' If TNullDecl(TObjectType(src).classDecl) Then
' Return "&bbEmptyArray"
' End If
'End If
If TVarPtrType(src) And TNumericType(ty) Then
Return "*" + expr
End If
If TIntType(ty) And TStringType(src) Then
Return Bra(expr + " != &bbEmptyString")
End If
' If TIntType(ty) And TObjectType(src) Then
' Return Bra(expr + " != &bbNullObject")
' End If
If TObjectType(ty) And TStringType(src) Then
Return expr
End If
If Not TObjectType(ty) Or Not TObjectType(src) Then
DebugStop
InternalErr
End If
Local t$=TransType(ty, "TODO: TransPtrCast")
If src.GetClass().IsInterface() Or ty.GetClass().IsInterface() cast="dynamic"
If src.GetClass().IsInterface() And Not ty.GetClass().IsInterface() Then
Return cast+"_cast<"+TransType(ty, "TODO: TransPtrCast")+">"+Bra( expr )
End If
'upcast?
If src.GetClass().ExtendsClass( ty.GetClass() ) Return expr
If TObjectType(ty) Then
Return "bbObjectDowncast" + Bra(expr + ",&" + TObjectType(ty).classDecl.munged)
End If
Return cast+"_cast<"+TransType(ty, "TODO: TransPtrCast")+">"+Bra( expr )
End Method
'***** Utility *****
Method TransLocalDecl$( munged$,init:TExpr )
If TFunctionPtrType(init.exprType) Then
Return TransType( init.exprType, munged )+"="+init.Trans()
Else
'DebugStop
Return TransType( init.exprType, munged )+" "+munged+"="+init.Trans()
End If
End Method
Method TransGlobalDecl$( munged$,init:TExpr, attrs:Int, ty:TType )
Local glob:String
If Not (attrs & DECL_INITONLY) Then
glob :+"static " + TransType( init.exprType, munged )+" "
End If
glob :+ munged+"="
If TNewObjectExpr(init) And Not (attrs & DECL_INITONLY) Then
glob :+ "0;~n"
glob :+ indent + "if (" + munged + "==0) {~n"
glob :+ indent + "~t" + munged + "=" + init.Trans() + ";~n"
glob :+ indent + "}"
Else
If init Then
If TFunctionPtrType(ty) Then
If TInvokeExpr(init) And Not TInvokeExpr(init).invokedWithBraces Then
glob :+ TInvokeExpr(init).decl.munged
Else
glob :+ init.Trans()
End If
Else
glob :+ init.Trans()
End If
Else
If TFunctionPtrType(ty) Then
glob :+ "&brl_blitz_NullFunctionError"
Else
glob :+ "0"
End If
End If
End If
Return glob
End Method
Method CreateLocal2$( ty:TType, t$ )
Local tmp:TLocalDecl=New TLocalDecl.Create( "", ty,Null )
MungDecl tmp
Emit TransType(ty, "") + " " + tmp.munged + " = bbStringToCString" + Bra(t)+ ";"
customVarStack.Push(tmp.munged)
Return tmp.munged
End Method
Method EmitPushErr()
Emit "pushErr();"
End Method
Method EmitSetErr( info$ )
Emit "errInfo=~q"+info.Replace( "\","/" )+"~q;"
End Method
Method EmitPopErr()
Emit "popErr();"
End Method
'***** Declarations *****
Method TransStatic$( decl:TDecl )
If decl.IsExtern() Then
If Not decl.munged
Return decl.ident
End If
Return decl.munged
Else If _env And decl.scope And decl.scope=_env.ClassScope()
Return decl.munged
Else If TClassDecl( decl.scope )
'Return decl.scope.munged+"::"+decl.munged
Return decl.munged
Else If TModuleDecl( decl.scope )
Return decl.munged
Else If TFuncDecl(decl.scope)
Return decl.munged
Else If TGlobalDecl(decl)
Return decl.munged
Else If TBlockDecl(decl.scope)
Return decl.munged
EndIf
InternalErr
End Method
Method TransTemplateCast$( ty:TType,src:TType,expr$ )
If ty=src Return expr
ty=ty.ActualType()
'src=src.ActualType()
If src.EqualsType( ty ) Return expr
Return TransPtrCast( ty,src,expr,"static" )
End Method
Method TransGlobal$( decl:TGlobalDecl )
Return TransStatic( decl )
End Method
Method TransField$( decl:TFieldDecl,lhs:TExpr )
If lhs Then
Return TransFieldRef(decl, TransSubExpr( lhs ), lhs.exprType)
Else
Return TransFieldRef(decl, "o", Null)
End If
' Local swiz$
' If TObjectType( decl.ty )
' If TObjectType( decl.ty ).classDecl.IsInterface() swiz=".p"
' EndIf
' If lhs Return TransSubExpr( lhs )+"->"+decl.munged+swiz
' Return decl.munged+swiz
End Method
Method TransFunc$( decl:TFuncDecl,args:TExpr[],lhs:TExpr, sup:Int = False, scope:TScopeDecl = Null )
'If decl.ident = "Sqr" DebugStop
' for calling the super class method instead
Local tSuper:String
If sup Then
tSuper = "->super"
End If
If Not decl.munged
MungDecl decl
End If
'If decl.IsMethod()
If lhs And Not TSelfExpr(lhs) Then
If TStringType(lhs.exprType) Then
Return decl.munged + TransArgs(args, decl, TransSubExpr( lhs ))
End If
If TStmtExpr(lhs) Then
lhs = TStmtExpr(lhs).expr
End If
If TVarExpr(lhs) Then
Local cdecl:TClassDecl
If TObjectType(TVarExpr(lhs).decl.ty) Then
cdecl = TObjectType(TVarExpr(lhs).decl.ty).classDecl
Else If TArrayType(TVarExpr(lhs).decl.ty) Then
Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
End If
If decl.attrs & FUNC_PTR Then
'Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
Return TransSubExpr( lhs ) + "->" + decl.munged+TransArgs( args,decl, Null)
Else
Local class:String = TransSubExpr( lhs ) + "->clas" + tSuper
Return class + "->" + TransFuncPrefix(cdecl, decl) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
End If
Else If TNewObjectExpr(lhs) Then
Local cdecl:TClassDecl = TNewObjectExpr(lhs).classDecl
Local class:String = cdecl.munged
Return class + "." + TransFuncPrefix(cdecl, decl) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
Else If TCastExpr(lhs) Then
Local cdecl:TClassDecl = TObjectType(TCastExpr(lhs).ty).classDecl
Local obj:String = Bra(TransObject(cdecl))
If decl.attrs & FUNC_PTR Then
Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
Else
Local class:String = Bra("(" + obj + TransSubExpr( lhs ) + ")->clas" + tSuper)
Return class + "->" + TransFuncPrefix(cdecl, decl) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
End If
Else If TMemberVarExpr(lhs) Then
Local cdecl:TClassDecl = TObjectType(TMemberVarExpr(lhs).decl.ty).classDecl
Local obj:String = Bra(TransObject(cdecl))
Local class:String = Bra("(" + obj + TransSubExpr( lhs ) + ")->clas" + tSuper)
'Local class:String = TransFuncClass(cdecl)
Return class + "->" + TransFuncPrefix(cdecl, decl) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
Else If TInvokeExpr(lhs) Then
' create a local variable of the inner invocation
Local lvar:String = CreateLocal(lhs)
Local obj:String = Bra(TransObject(decl.scope))
Local class:String = Bra("(" + obj + lvar +")->clas" + tSuper)
Return class + "->" + TransFuncPrefix(decl.scope, decl)+ decl.ident+TransArgs( args,decl, lvar )
'Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
'Local class:String = Bra("(" + obj + TransSubExpr( lhs ) +")->clas" + tSuper)
'Local class:String = Bra("&" + decl.scope.munged)
'Return class + "->" + TransFuncPrefix(decl.scope, decl.ident) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
Else If TInvokeMemberExpr(lhs)
' create a local variable of the inner invocation
Local lvar:String = CreateLocal(lhs)
Local obj:String = lvar + "->clas" + tSuper
Return obj + "->" + TransFuncPrefix(decl.scope, decl)+ decl.ident+TransArgs( args,decl, lvar )
Else If TIndexExpr(lhs) Then
Local loc:String = CreateLocal(lhs)
Local obj:String = Bra(TransObject(decl.scope))
'Local class:String = Bra("(" + obj + loc +")->clas" + tSuper)
'Local class:String = Bra("&" + decl.scope.munged)
Local class:String = Bra(loc + "->clas" + tSuper)
Return class + "->" + TransFuncPrefix(decl.scope, decl) + decl.ident+TransArgs( args,decl, loc )
Else
InternalErr
End If
'Return TransSubExpr( lhs )+"->"+decl.munged+TransArgs( args,decl )
'Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
End If
' ((brl_standardio_TCStandardIO_obj*)o->clas)->md_Read(o, xxx, xxx)
If decl.IsMethod() Then
If Not (decl.attrs & FUNC_PTR) Then
Local class:String
If Not scope Then
scope = decl.scope
Local obj:String = Bra(TransObject(scope))
class = "(" + obj + "o)->clas" + tSuper
Else
class = Bra("&" + scope.munged) + tSuper
End If
'Local obj:String = Bra("struct " + scope.munged + "_obj*")
'Local class:String = Bra("(" + obj + "o)->clas" + tSuper)
'Local class:String = Bra("&" + decl.scope.munged)
Return class + "->" + TransFuncPrefix(scope, decl) + decl.ident+TransArgs( args,decl, "o" )
Else
Local obj:String = Bra(TransObject(decl.scope))
Return Bra(obj + "o") + "->" + decl.munged+TransArgs( args,decl )
End If
End If
' built-in functions
Select decl.ident.ToLower()
Case "min", "max", "len", "asc", "chr", "sgn"
Return TransBuiltin(decl, args)
End Select
Return TransStatic( decl )+TransArgs( args,decl )
End Method
Method TransObject:String(decl:TScopeDecl)
If decl.ident = "Object"
Return "BBOBJECT"
Else
Return "struct " + decl.munged + "_obj*"
End If
End Method
Method TransFuncClass:String(decl:TClassDecl)
If decl.ident = "Object"
Return Bra("&bbObjectClass")
Else
Return Bra("&" + decl.munged)
End If
End Method
Method TransFuncPrefix:String(decl:TScopeDecl, fdecl:TFuncDecl)
Local ident:String = fdecl.ident
If Not decl Or decl.ident = "Object" Or ident = "ToString" Or ident = "Compare" Or ident = "SendMessage" Then
Return ""
Else
If fdecl.IsMethod() Then
Return "md_"
Else
Return "fn_"
End If
End If
End Method
Method TransSuperFunc$( decl:TFuncDecl,args:TExpr[], scope:TScopeDecl )
Return TransFunc(decl, args, Null, True, scope)
' If decl.IsMethod()
' Return decl.ClassScope().munged+".md_"+decl.ident+TransArgs( args,decl, "o" )
' Else
' Return decl.ClassScope().munged+".fn_"+decl.ident+TransArgs( args,decl)
' End If
End Method
Method TransBuiltin$( decl:TFuncDecl,args:TExpr[] )
Select decl.ident.ToLower()
Case "min", "max"
Local isFloat:Int
For Local arg:TExpr = EachIn args
If TFloatType(arg.exprType) Or TDoubleType(arg.exprType) Then
isFloat = True
End If
Next
If isFloat Then
Return "bbFloat" + decl.ident + TransArgsTypes(args, [New TFloatType, New TFloatType])
Else
' TODO : Long support
Return "bbInt" + decl.ident + TransArgs(args, decl)
End If
Case "len"
Local arg:TExpr = args[0]
If TStringType(arg.exprType) Then
Return TVarExpr(arg).decl.munged + "->length"
Else If TArrayType(arg.exprType) Then
Return TVarExpr(arg).decl.munged + "->scales[0]"
Else If TCastExpr(arg) Then
If TArrayType(TCastExpr(arg).expr.exprType) Then
Return TCastExpr(arg).expr.Trans() + "->scales[0]"
End If
'other types just have a length of "1"
Else
Return "1"
End If
Case "sgn"
Local arg:TExpr = args[0]
'should be handled already
If TConstExpr(arg) Then InternalErr
'decide whether to use float or int (cast done BEFORE
'sgn() is executed)
'bbFloatSng/bbIntSng call "Sgn#(v#)" as defined
'in config.bmx -> keywords
If TFloatType(arg.exprType) Or TDoubleType(arg.exprType)
'decl.ident contains "sgn", same like "bbFloatSng"
Return "bbFloat" + decl.ident + TransArgsTypes(args, [New TFloatType])
Else
' TODO : Long support
Return "bbInt" + decl.ident + TransArgs(args, decl)
End If
Case "asc"
Local arg:TExpr = args[0]
If TConstExpr(arg) InternalErr ' we should have handled this case already
Return "bbStringAsc" + TransArgs(args, decl)
Case "chr"
If TConstExpr(args[0]) InternalErr ' we should have handled this case already
Return "bbStringFromChar" + TransArgs(args, decl)
End Select
End Method
Method TransMinExpr:String(expr:TMinExpr)
Local s:String
If TDecimalType(expr.exprType) Then
s = "bbFloatMin"
Else If TLongType(expr.exprType) Then
s = "bbLongMin"
Else
s = "bbIntMin"
End If
Return s + Bra(expr.expr.trans() + "," + expr.expr2.Trans())
End Method
Method TransMaxExpr:String(expr:TMaxExpr)
Local s:String
If TDecimalType(expr.exprType) Then
s = "bbFloatMax"
Else If TLongType(expr.exprType) Then
s = "bbLongMax"
Else
s = "bbIntMax"
End If
Return s + Bra(expr.expr.trans() + "," + expr.expr2.Trans())
End Method
Method TransAscExpr:String(expr:TAscExpr)
End Method
Method TransSgnExpr:String(expr:TSgnExpr)
End Method
Method TransAbsExpr:String(expr:TAbsExpr)
Local s:String
If TDecimalType(expr.exprType) Then
s = "bbFloatAbs"
Else If TLongType(expr.exprType)
s = "bbLongAbs"
Else
s = "bbIntAbs"
End If
Return s + Bra(expr.expr.Trans())
End Method
Method TransLenExpr:String(expr:TLenExpr)
'constant strings do not have "->length", so we use the
'precalculated value
If TConstExpr(expr.expr) Then
If TStringType(expr.expr.exprType) Then
Return TConstExpr(expr.expr).value.Length
End If
End If
If TStringType(expr.expr.exprType) Then
Return expr.expr.Trans() + "->length"
Else If TArrayType(expr.expr.exprType) Then
Return expr.expr.Trans() + "->scales[0]"
Else If TCastExpr(expr.expr) Then
If TArrayType(TCastExpr(expr.expr).expr.exprType) Then
Return TCastExpr(expr.expr).expr.Trans() + "->scales[0]"
End If
'other types just have a length of "1"
Else
Return "1"
End If
End Method
Method TransSizeOfExpr:String(expr:TSizeOfExpr)
'ATTENTION:
'current assumption is: 2 Bytes per unicode character are used
Local bytesPerChar:Int = 2
If TVarExpr(expr.expr) Then
'objects
If TObjectType(TVarExpr(expr.expr).exprType) Then
Return Bra(TVarExpr(expr.expr).decl.munged + "->clas->instance_size-(sizeof(void*))")
'numeric types are able to use sizeof(BBINT|BBFLOAT...)
ElseIf IsNumericType(TVarExpr(expr.expr).exprType) Then
Return "sizeof" + Bra(TransType(TVarExpr(expr.expr).exprType, ""))
'strings
ElseIf TStringType(TVarExpr(expr.expr).exprType) Then
'unicode chars each take 2 bytes
Return TVarExpr(expr.expr).decl.munged + "->length * " + bytesPerChar
'arrays
ElseIf TArrayType(TVarExpr(expr.expr).exprType) Then
'normal exprType is something like "int[]" that
'is why it has to be checked against elemType
Local elemType:TType = TArrayType( TVarExpr(expr.expr).exprType ).elemType
'numerics
If IsNumericType(elemType) Then
'multiply element count * size of element type
Return TVarExpr(expr.expr).decl.munged + "->scales[0] * sizeof" + Bra(TransType(elemType, ""))
'strings
ElseIf TStringType(elemType) Then
'arrays of strings are of size: elementCount * pointerSize
Return TVarExpr(expr.expr).decl.munged + "->scales[0] * sizeof(void*)"
'non numeric elements are just connected through "pointers"
'so they could be merged with the string type
Else
'arrays of objects are of size: elementCount * pointerSize
Return TVarExpr(expr.expr).decl.munged + "->scales[0] * sizeof(void*)"
EndIf
'objects, pointers, ... seem to just occupy 1 pointerSize
'it it does not matter what kind of pointer is used
Else
Return "sizeof(void*)"
End If
ElseIf TConstExpr(expr.expr) Then
'const numerics behave exactly the same like variable numerics
If IsNumericType(TConstExpr(expr.expr).exprType) Then
Return "sizeof" + Bra(TransType(TConstExpr(expr.expr).exprType, ""))
'strings
ElseIf TStringType(TConstExpr(expr.expr).exprType) Then
'const strings can rely on the BMX sizeOf()
'Return sizeOf(TConstExpr(expr.expr).value)
'BUT: for now we use 2*chars so it is easier to replace
Return Len(TConstExpr(expr.expr).value) * bytesPerChar
'arrays
ElseIf TArrayType(TConstExpr(expr.expr).exprType) Then
'normal exprType is something like "int[]" that
'is why it has to be checked against elemType
Local elemType:TType = TArrayType( TConstExpr(expr.expr).exprType ).elemType
'numerics
If IsNumericType(elemType) Then
'multiply element count * size of element type
Return TConstExpr(expr.expr).value + "->scales[0] * sizeof" + Bra(TransType(elemType, ""))
'strings
ElseIf TStringType(elemType) Then
'arrays of strings are of size: elementCount * pointerSize
Return TConstExpr(expr.expr).value + "->scales[0] * sizeof(void*)"
'non numeric elements are just connected through "pointers"
'so they could be merged with the string type
Else
'arrays of objects are of size: elementCount * pointerSize
Return TConstExpr(expr.expr).value + "->scales[0] * sizeof(void*)"
EndIf
'objects, pointers, ... seem to just occupy 1 pointerSize
'it it does not matter what kind of pointer is used
Else
Return "sizeof(void*)"
End If
'class instances properties (fields)
ElseIf TMemberVarExpr(expr.expr) Then
Local instanceName:String = TVarExpr(TMemberVarExpr(expr.expr).expr).decl.munged
Local propertyName:String = TMemberVarExpr(expr.expr).decl.ident
Local propertyType:TType = TMemberVarExpr(expr.expr).decl.declTy
'objects
If TObjectType(propertyType) Then
Return Bra(instanceName + "->clas->instance_size-(sizeof(void*))")
'numeric types are able to use sizeof(BBINT|BBFLOAT...)
ElseIf TNumericType(propertyType) Then
Return "sizeof" + Bra(TransType(propertyType, ""))
'strings
ElseIf TStringType(propertyType) Then
'unicode chars each take 2 bytes
Return instanceName + "->" + propertyName + "->length * " + bytesPerChar
'arrays
ElseIf TArrayType(propertyType) Then
'normal exprType is something like "int[]" that
'is why it has to be checked against elemType
Local elemType:TType = TArrayType( propertyType ).elemType
'numerics
If TNumericType(elemType) Then
'multiply element count * size of element type
Return expr.expr.Trans() + "->scales[0] * sizeof" + Bra(TransType(elemType, ""))
'strings
ElseIf TStringType(elemType) Then
'arrays of strings are of size: elementCount * pointerSize
Return expr.expr.Trans() + "->scales[0] * sizeof(void*)"
'non-numeric elements are just connected through "pointers"
'so they could be merged with the string type but to
'keep it extendable easily, keep it split
Else
'arrays of objects are of size: elementCount * pointerSize
Return expr.expr.Trans() + "->scales[0] * sizeof(void*)"
EndIf
'objects, pointers, ... seem to just occupy 1 pointerSize
'it it does not matter what kind of pointer is used
Else
Return "sizeof(void*)"
End If
'class instances method calls
ElseIf TInvokeMemberExpr(expr.expr) Then
'Throw ("Implement TInvokeMemberExpr(expr.expr)")
Print "Implement TInvokeMemberExpr(expr.expr) handling: typeInstance.method() calls"
'Return "sizeof(void*)"
InternalErr
Rem
'I think this part should contain some "temporary variable"
'creation - which then can get handled like all other
'variables
local instanceName:string = TVarExpr(TInvokeMemberExpr(expr.expr).expr).decl.munged
local returnName:string = ""