Repository navigation
Expand file tree
/
Copy pathxlator_Kotlin.py
More file actions
883 lines (806 loc) · 41.5 KB
/
Copy pathxlator_Kotlin.py
File metadata and controls
883 lines (806 loc) · 41.5 KB
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
# xlator_Kotlin.py
#
# Kotlin JVM backend. This intentionally starts close to the Java backend
# because CodeDog's JVM/Android libraries already use that object model.
import progSpec
import codeDogParser
import re
from progSpec import cdErr
from xlator_Java import Xlator_Java
class Xlator_Kotlin(Xlator_Java):
LanguageName = "Kotlin"
BuildStrPrefix = "kotlinc "
fileExtension = ".kt"
typeForCounterInt = "Int"
GlobalVarPrefix = "GLOBAL.static_Global."
PtrConnector = "!!."
ObjConnector = "."
NameSegConnector = "."
NameSegFuncConnector = "."
modeIdxType = "Int"
doesLangHaveGlobals = False
funcBodyIndent = " "
funcsDefInClass = True
MakeConstructors = True
blockPrefix = ""
usePrefixOnStatics = False
iteratorsUseOperators = False
renderGenerics = "True"
renameInitFuncs = False
useAllCtorArgs = True
hasMacros = False
useNestedClasses = False
nullValue = "null"
companionMarker = "__KOTLIN_COMPANION__"
enumDefaults = {}
enumTypes = set()
def _cleanType(self, langType):
if langType.startswith("static "):
langType = langType[len("static "):]
if langType.startswith("final "):
langType = langType[len("final "):]
return langType
def _nonNullableType(self, langType):
langType = self._cleanType(langType)
if langType.endswith("?"):
return langType[:-1]
return langType
def _isNullableType(self, langType):
return self._cleanType(langType).endswith("?")
def _isValueType(self, langType):
langType = self._nonNullableType(langType)
return langType in {
"Int", "Long", "Double", "Float", "Boolean", "Char",
"Short", "Byte", "UInt", "ULong", "UShort", "UByte",
}
def _defaultValueForType(self, langType, owner="me"):
langType = self._cleanType(langType)
baseType = self._nonNullableType(langType)
if self._isNullableType(langType) or progSpec.ownerIsPointer(owner):
return "null"
if baseType in ("Int", "Short", "Byte", "UInt", "UShort", "UByte"):
return "0"
if baseType in ("Long", "ULong"):
return "0L"
if baseType == "Double":
return "0.0"
if baseType == "Float":
return "0.0f"
if baseType == "Boolean":
return "false"
if baseType == "Char":
return "' '"
if baseType == "String":
return '""'
if baseType in self.enumDefaults:
return baseType + "." + self.enumDefaults[baseType]
if len(baseType) == 1 or baseType in ("keyType", "valueType", "nodeType"):
return "null as " + baseType
return baseType + "()"
def _rhsExpr(self, rhs):
if rhs == "":
return ""
rhs = rhs.strip()
if rhs.startswith("="):
rhs = rhs[1:].strip()
return rhs
def _fieldDecl(self, indent, mutability, name, langType, rhs, owner="me"):
langType = self._cleanType(langType)
rhsExpr = self._rhsExpr(rhs)
if rhsExpr == "":
rhsExpr = self._defaultValueForType(langType, owner)
return indent + mutability + " " + name + ": " + langType + " = " + rhsExpr + ";\n"
###################################################### CONTAINERS
def codeArrayIndex(self, idx, containerType, LorR_Val, previousSegName, idxTypeSpec):
return "[" + idx + "]"
def emitLoopWithBody(self, header, prologue, body, returnType, mods, genericArgs, indent):
actionText = indent + header + " {\n"
if prologue:
actionText += prologue
whereExprNode = mods.get("whereExpr") if mods else None
untilExprNode = mods.get("untilExpr") if mods else None
if whereExprNode:
whereExprIn = whereExprNode[0] if not isinstance(whereExprNode, str) else whereExprNode
[whereExpr, _whereType] = self.codeGen.codeExpr(whereExprIn, None, None, "RVAL", genericArgs)
actionText += indent + " if (!(" + whereExpr + ")) continue;\n"
if untilExprNode:
untilExprIn = untilExprNode[0] if not isinstance(untilExprNode, str) else untilExprNode
[untilExpr, _untilType] = self.codeGen.codeExpr(untilExprIn, None, None, "RVAL", genericArgs)
actionText += indent + " if (" + untilExpr + ") break;\n"
for repAction in body:
actionText += self.codeGen.codeAction(repAction, indent + " ", returnType, genericArgs)
actionText += indent + "}\n"
return actionText
def codeRangeSpec(self, traversalMode, ctrType, repName, S_low, S_hi, inclusive, indent, body, returnType, mods, genericArgs):
mode = traversalMode or "Forward"
if mode == "Backward":
start = S_hi if inclusive else "(" + S_hi + " - 1)"
header = "for (" + repName + " in " + start + " downTo " + S_low + ")"
else:
op = ".." if inclusive else " until "
if inclusive:
header = "for (" + repName + " in " + S_low + op + S_hi + ")"
else:
header = "for (" + repName + " in " + S_low + op + S_hi + ")"
return self.emitLoopWithBody(header, "", body, returnType, mods, genericArgs, indent)
def traversalLoopWithBodyStr(
self,
classes,
localVarsAlloc,
ctnrTSpec,
binding,
ctnrName,
body,
returnType,
mods,
genericArgs,
indent,
traversalMode=None,
rangeMode=None,
rangeSpec=None,
):
containerInfo = progSpec.getContainerInfo(self.codeGen.classStore, ctnrTSpec)
containerCat = containerInfo["category"]
isAssociative = containerInfo["isAssociative"] or containerInfo["entryShape"] == "entry"
if rangeMode is not None and rangeMode != "keys":
cdErr("Kotlin traversal range mode '" + str(rangeMode) + "' is not implemented yet.")
bkind = binding.get("kind")
axis = binding.get("axis")
def requireSpec(spec, message):
if spec == None:
cdErr(message)
return spec
def mapEntriesAndRangeFilter(entryName):
entriesExpr = ctnrName + ".entries"
rangeFilter = ""
if rangeMode == "keys":
caps = progSpec.getContainerCapabilities(classes, ctnrTSpec)
if "ordered_keys" not in caps.get("tags", set()):
cdErr("keys: range requires ordered_keys capability for container '" + ctnrName + "'.")
if not rangeSpec:
cdErr("Kotlin keys traversal requires a range.")
startPR = rangeSpec.get("rangeStart", None)
endPR = rangeSpec.get("rangeEnd", None)
if startPR is None or endPR is None:
cdErr("Kotlin keys traversal requires start and end keys.")
[startExpr, _startTSpec] = self.codeGen.codeExpr(startPR[0], None, None, "RVAL", genericArgs)
[endExpr, _endTSpec] = self.codeGen.codeExpr(endPR[0], None, None, "RVAL", genericArgs)
upperOp = ">" if bool(getattr(rangeSpec, "inclusiveOp", False)) else ">="
entriesExpr += ".sortedBy { it.key }"
rangeFilter = indent + " if (" + entryName + ".key < " + startExpr + " || " + entryName + ".key " + upperOp + " " + endExpr + ") continue;\n"
return [entriesExpr, rangeFilter]
if bkind == "tuple":
keyName = binding.get("keyName")
valName = binding.get("valName")
if not keyName or not valName:
cdErr("Kotlin tuple traversal missing key/value binding names.")
if not isAssociative:
cdErr("Kotlin tuple traversal requires a map-like container.")
keyTSpec = requireSpec(containerInfo["keyTypeSpec"], "Kotlin tuple traversal requires a key type.")
valTSpec = requireSpec(containerInfo["valueTypeSpec"], "Kotlin tuple traversal requires a value type.")
localVarsAlloc.append([keyName, keyTSpec])
localVarsAlloc.append([valName, valTSpec])
keyType = self.codeGen.convertType(keyTSpec, "var", genericArgs)
valType = self.codeGen.convertType(valTSpec, "var", genericArgs)
entryName = keyName + "_" + valName + "_entry"
[entriesExpr, rangeFilter] = mapEntriesAndRangeFilter(entryName)
header = "for (" + entryName + " in " + entriesExpr + ")"
prologue = (
rangeFilter
+ indent + " var " + keyName + ": " + keyType + " = " + entryName + ".key;\n"
+ indent + " var " + valName + ": " + valType + " = " + entryName + ".value;\n"
)
return self.emitLoopWithBody(header, prologue, body, returnType, mods, genericArgs, indent)
if bkind != "single":
cdErr("Kotlin traversal binding kind missing or unknown.")
repName = binding.get("name")
if not repName:
cdErr("Kotlin traversal missing loop variable name.")
if axis is None:
axis = "value"
if isAssociative:
keyTSpec = requireSpec(containerInfo["keyTypeSpec"], "Kotlin map traversal requires a key type.")
valTSpec = requireSpec(containerInfo["valueTypeSpec"], "Kotlin map traversal requires a value type.")
entryName = repName + "_entry"
[entriesExpr, rangeFilter] = mapEntriesAndRangeFilter(entryName)
header = "for (" + entryName + " in " + entriesExpr + ")"
if axis == "key":
localVarsAlloc.append([repName, keyTSpec])
keyType = self.codeGen.convertType(keyTSpec, "var", genericArgs)
prologue = rangeFilter + indent + " var " + repName + ": " + keyType + " = " + entryName + ".key;\n"
elif axis == "value":
localVarsAlloc.append([repName, valTSpec])
localVarsAlloc.append([repName + "_key", keyTSpec])
keyType = self.codeGen.convertType(keyTSpec, "var", genericArgs)
valType = self.codeGen.convertType(valTSpec, "var", genericArgs)
prologue = (
rangeFilter
+ indent + " var " + repName + ": " + valType + " = " + entryName + ".value;\n"
+ indent + " var " + repName + "_key: " + keyType + " = " + entryName + ".key;\n"
)
else:
cdErr("Kotlin map traversal does not support axis '" + str(axis) + "'.")
return self.emitLoopWithBody(header, prologue, body, returnType, mods, genericArgs, indent)
repTSpec = requireSpec(containerInfo["valueTypeSpec"], "Kotlin traversal requires a value type.")
if containerCat == "string":
localVarsAlloc.append([repName, repTSpec])
loopCntrName = repName + "_key"
localVarsAlloc.append([loopCntrName, {"owner": "me", "fieldType": "Int"}])
header = "for (" + loopCntrName + " in 0 until " + ctnrName + ".length)"
prologue = indent + " var " + repName + ": Char = " + ctnrName + "[" + loopCntrName + "];\n"
return self.emitLoopWithBody(header, prologue, body, returnType, mods, genericArgs, indent)
localVarsAlloc.append([repName, repTSpec])
loopCntrName = repName + "_key"
localVarsAlloc.append([loopCntrName, {"owner": "me", "fieldType": "Int"}])
elemType = self.codeGen.convertType(repTSpec, "var", genericArgs)
if containerCat == "Set":
if traversalMode == "Backward":
cdErr("Kotlin Set traversal does not support Backward.")
header = "for (" + repName + " in " + ctnrName + ")"
return self.emitLoopWithBody(header, "", body, returnType, mods, genericArgs, indent)
if containerInfo["entryShape"] == "value" and containerCat != "string":
if traversalMode == "Backward":
header = "for (" + loopCntrName + " in (" + ctnrName + ".size - 1) downTo 0)"
else:
header = "for (" + loopCntrName + " in 0 until " + ctnrName + ".size)"
prologue = indent + " var " + repName + ": " + elemType + " = " + ctnrName + "[" + loopCntrName + "];\n"
return self.emitLoopWithBody(header, prologue, body, returnType, mods, genericArgs, indent)
cdErr("Kotlin traversal for container category '" + str(containerCat) + "' is not implemented yet.")
###################################################### TYPES / OWNERS
def adjustBaseTypes(self, fType, isContainer):
if fType == "":
return ""
if not isinstance(fType, str):
fType = fType[0]
if fType in self.enumTypes:
return "Int"
if fType in ("int8", "int16", "int32", "int", "uint8", "uint16", "uint32", "uint", "numeric"):
return "Int"
if fType in ("int64", "uint64", "long", "timeValue"):
return "Long"
if fType == "float":
return "Float"
if fType == "double":
return "Double"
if fType in ("bool", "boolean"):
return "Boolean"
if fType == "void" or fType == "none":
return "Unit"
if fType == "string" or fType == "String":
return "String"
if fType == "char":
return "Char"
return progSpec.flattenObjectName(fType)
def applyOwner(self, owner, langType, varMode):
langType = self._cleanType(langType)
if owner in ("me", "const", "we"):
return langType
if owner in ("my", "our", "their"):
if not langType.endswith("?"):
return langType + "?"
return langType
cdErr("ERROR: Owner of type not valid '" + owner + "'")
def makePtrOpt(self, tSpec):
if isinstance(tSpec, dict) and progSpec.typeIsPointer(tSpec):
return "!!"
return ""
def codeExplicitDeref(self, varRef, itemTypeSpec):
return varRef + "!!"
def codeXlatorAllocater(self, tSpec, genericArgs):
owner = progSpec.getOwner(tSpec)
if owner == "const":
cdErr("ERROR: Cannot allocate a 'const' variable.")
reqTagList = progSpec.getReqTagList(tSpec)
fTypeKW = progSpec.fieldTypeKeyword(tSpec)
fTypeKW = self.codeGen.generateGenericStructName(fTypeKW, reqTagList, genericArgs)
return progSpec.flattenObjectName(fTypeKW)
def getConstIntFieldStr(self, fieldName, fieldValue, intSize):
kotlinType = "Int" if intSize == 32 else "Long"
if intSize != 32 and not fieldValue.endswith("L"):
fieldValue += "L"
return self.companionMarker + " const val " + fieldName + ": " + kotlinType + " = " + fieldValue + ";\n"
def getEnumStr(self, fieldName, enumList):
S = ""
count = 0
for enumName in enumList:
S += " " + self.getConstIntFieldStr(enumName, str(count), 32)
count += 1
S += "\n"
return S
def getEnumStructStr(self, fieldName, enumList):
if enumList:
self.enumDefaults[fieldName] = enumList[0]
self.enumTypes.add(fieldName)
S = "object " + fieldName + "{\n"
count = 0
for enumName in enumList:
S += " const val " + enumName + ": Int = " + str(count) + ";\n"
count += 1
S += "}\n"
return S
def getEnumStringifyFunc(self, className, enumList):
return "var " + className + "Strings: ArrayList<String> = arrayListOf(\"" + "\", \"".join(enumList) + "\");\n"
###################################################### EXPRESSIONS
def recodeStringFunctions(self, name, tSpec, lenArgs):
if name == "size":
tSpec["fieldType"] = "int"
tSpec["codeConverter"] = "%0.length"
elif name == "subStr":
if lenArgs == 1:
tSpec["codeConverter"] = "%0.substring(%1, %0.length)"
else:
tSpec["codeConverter"] = "%0.substring(%1, %1 + %2)"
elif name == "append":
tSpec["codeConverter"] = "%0 += %1"
return [name, tSpec]
def convertToInt(self, S, tSpec):
fTypeKW = progSpec.fieldTypeKeyword(tSpec)
if fTypeKW in ("numeric", "int", "Int"):
return S
if fTypeKW == "char" or fTypeKW == "Char":
return "Character.getNumericValue(" + S + ")"
return S
def checkForTypeCastNeed(self, lhsTSpec, rhsTSpec, RHS):
LTypeKW = progSpec.fieldTypeKeyword(lhsTSpec)
RTypeKW = progSpec.fieldTypeKeyword(rhsTSpec)
if LTypeKW in ("bool", "Boolean", "boolean"):
if progSpec.typeIsPointer(rhsTSpec):
return "(" + RHS + " != null)"
if RTypeKW in ("int", "Int", "numeric", "flag"):
if RHS and RHS[0] == "!":
return "(" + RHS[1:] + " == 0)"
return "(" + RHS + " != 0)"
if RHS == "0":
return "false"
if RHS == "1":
return "true"
if isinstance(lhsTSpec, dict) and isinstance(rhsTSpec, dict):
if not progSpec.typeIsPointer(lhsTSpec) and progSpec.typeIsPointer(rhsTSpec):
RHS = "(" + RHS + "!!)"
if LTypeKW in ("char", "Char") and RTypeKW in ("numeric", "int", "Int"):
return "(" + RHS + ").toChar()"
if LTypeKW in ("string", "String") and RTypeKW in ("char", "Char"):
return "(" + RHS + ").toString()"
if LTypeKW in ("int", "Int", "numeric") and RTypeKW in ("long", "Long", "int64", "uint64"):
return "(" + RHS + ").toInt()"
if LTypeKW in ("long", "Long", "int64") and RTypeKW in ("numeric", "int", "Int"):
return "(" + RHS + ").toLong()"
return RHS
def codePlusOperator(self, S, S2, retType1, retType2, opIn):
op = " + " if opIn == "+" else " - "
if opIn == "+":
fType1 = progSpec.fieldTypeKeyword(retType1)
fType2 = progSpec.fieldTypeKeyword(retType2)
if fType2 in ("string", "String") and fType1 not in ("string", "String"):
S = "(" + S + ").toString()"
return S + op + S2
def codeBitwiseOp(self, S, S2, opIn):
opMap = {"&": "and", "^": "xor", "|": "or"}
return "(" + S + " " + opMap[opIn] + " " + S2 + ")"
def codeNotOperator(self, S, S2, retTypeSpec):
if progSpec.typeIsPointer(retTypeSpec):
return ["(" + S2 + " == null)", {"owner": "me", "fieldType": "bool"}]
return [S + "!" + S2, retTypeSpec]
def codeIdentityCheck(self, S, S2, retType1, retType2, opIn):
if opIn == "===":
opIn = "==="
elif opIn == "!==":
opIn = "!=="
elif opIn == "==":
opIn = "=="
elif opIn == "!=":
opIn = "!="
else:
cdErr("ERROR: '==' or '!=' or '===' or '!==' expected.")
S2 = self._coerceComparisonOperand(S2, retType1, retType2)
return S + " " + opIn + " " + self.adjustQuotesForChar(retType1, retType2, S2)
def _coerceComparisonOperand(self, S2, retType1, retType2):
fType1 = progSpec.fieldTypeKeyword(retType1)
fType2 = progSpec.fieldTypeKeyword(retType2)
if fType1 in ("double", "Double") and fType2 in ("numeric", "int", "Int", "uint", "long", "Long", "int64", "uint64"):
return "(" + S2 + ").toDouble()"
if fType1 in ("float", "Float") and fType2 in ("numeric", "int", "Int", "uint", "long", "Long", "int64", "uint64"):
return "(" + S2 + ").toFloat()"
if fType1 in ("long", "Long", "int64", "uint64") and fType2 in ("numeric", "int", "Int", "uint"):
return "(" + S2 + ").toLong()"
return S2
def adjustConditional(self, S, conditionType):
if isinstance(conditionType, dict):
fType = progSpec.fieldTypeKeyword(conditionType)
if progSpec.typeIsPointer(conditionType):
if S[0] == "!":
S = "(" + S[1:] + " == null)"
else:
S = "(" + S + " != null)"
conditionType = "bool"
elif fType == "flag" or progSpec.typeIsInteger(fType):
if S[0] == "!":
S = "(" + S[1:] + " == 0)"
else:
S = "(" + S + " != 0)"
conditionType = "bool"
return [S, conditionType]
def codeFactor(self, item, returnType, expectedTypeSpec, LorRorP_Val, genericArgs):
if isinstance(item[0], str) and item[0] == "[":
retTypeSpec = "noType"
exprTypeSpec = "noType"
elems = []
for expr in item[1:-1]:
[S2, exprTypeSpec] = self.codeGen.codeExpr(expr, returnType, expectedTypeSpec, LorRorP_Val, genericArgs)
elems.append(S2)
if exprTypeSpec != "noType":
retTypeSpec = exprTypeSpec
fTypeKW = "Any"
if exprTypeSpec != "noType":
retTypeKW = progSpec.fieldTypeKeyword(exprTypeSpec)
fTypeKW = self.adjustBaseTypes(retTypeKW, True)
return ["arrayListOf<" + fTypeKW + ">(" + ", ".join(elems) + ")", retTypeSpec]
[S, retTypeSpec] = super().codeFactor(item, returnType, expectedTypeSpec, LorRorP_Val, genericArgs)
S = re.sub(r"\\x([0-9a-fA-F]{2})", lambda m: "\\u00" + m.group(1).upper(), S)
return [S, retTypeSpec]
def codeSpecialReference(self, segSpec, genericArgs):
funcName = segSpec[0]
if len(segSpec) > 2 and funcName == "callPeriodically":
argList = segSpec[2]
[callbackClassName, _callbackClassTypeSpec] = self.codeGen.codeExpr(argList[0][0], None, None, "ARG", genericArgs)
[objName, tSpec] = self.codeGen.codeExpr(argList[1][0], None, None, "ARG", genericArgs)
[interval, _intTypeSpec] = self.codeGen.codeExpr(argList[2][0], None, None, "ARG", genericArgs)
varTypeSpec = progSpec.fieldTypeKeyword(tSpec)
if varTypeSpec == None or varTypeSpec == "void":
varTypeSpec = callbackClassName.strip('"').strip("'")
callbackTargetExpr = objName
if progSpec.ownerIsPointer(progSpec.getOwner(tSpec)) and objName != "this":
callbackTargetExpr = "(" + objName + ")!!"
androidBody = "callbackTarget.run(); callbackHandler.postDelayed(this, callbackIntervalMillis)"
timerBody = "callbackTarget.run()"
platform = None
if hasattr(self.codeGen, "buildTags") and self.codeGen.buildTags != None:
platform = progSpec.fetchTagValue([self.codeGen.buildTags], "Platform")
if self.codeGen.tagStore != None:
platform = platform or progSpec.fetchTagValue([self.codeGen.tagStore], "Platform")
if platform == "Android":
S = (
"run {\n"
" val callbackTarget = " + callbackTargetExpr + "\n"
" val callbackIntervalMillis = (" + interval + ").toLong()\n"
" val callbackHandler = android.os.Handler(android.os.Looper.getMainLooper())\n"
" val callbackRunnable = object : Runnable {\n"
" override fun run() {\n"
" " + androidBody + "\n"
" }\n"
" }\n"
" callbackHandler.postDelayed(callbackRunnable, callbackIntervalMillis)\n"
"}"
)
else:
S = (
"run {\n"
" val callbackTarget = " + callbackTargetExpr + "\n"
" val callbackIntervalMillis = (" + interval + ").toLong()\n"
" val callbackTimer = java.util.Timer()\n"
" callbackTimer.scheduleAtFixedRate(object : java.util.TimerTask() {\n"
" override fun run() {\n"
" " + timerBody + "\n"
" }\n"
" }, callbackIntervalMillis, callbackIntervalMillis)\n"
"}"
)
return [S, "me", "void"]
if len(segSpec) > 2 and funcName == "callOnce":
argList = segSpec[2]
[callbackClassName, _callbackClassTypeSpec] = self.codeGen.codeExpr(argList[0][0], None, None, "ARG", genericArgs)
[objName, tSpec] = self.codeGen.codeExpr(argList[1][0], None, None, "ARG", genericArgs)
[methodName, _methodTypeSpec] = self.codeGen.codeExpr(argList[2][0], None, None, "ARG", genericArgs)
[interval, _intTypeSpec] = self.codeGen.codeExpr(argList[3][0], None, None, "ARG", genericArgs)
callbackTargetExpr = objName
if progSpec.ownerIsPointer(progSpec.getOwner(tSpec)) and objName != "this":
callbackTargetExpr = "(" + objName + ")!!"
methodName = methodName.strip('"').strip("'")
platform = None
if hasattr(self.codeGen, "buildTags") and self.codeGen.buildTags != None:
platform = progSpec.fetchTagValue([self.codeGen.buildTags], "Platform")
if self.codeGen.tagStore != None:
platform = platform or progSpec.fetchTagValue([self.codeGen.tagStore], "Platform")
if platform == "Android":
S = (
"run {\n"
" val callbackTarget = " + callbackTargetExpr + "\n"
" val callbackIntervalMillis = (" + interval + ").toLong()\n"
" val callbackHandler = android.os.Handler(android.os.Looper.getMainLooper())\n"
" callbackHandler.postDelayed({ callbackTarget." + methodName + "() }, callbackIntervalMillis)\n"
"}"
)
else:
S = (
"run {\n"
" val callbackTarget = " + callbackTargetExpr + "\n"
" val callbackIntervalMillis = (" + interval + ").toLong()\n"
" java.util.Timer().schedule(object : java.util.TimerTask() {\n"
" override fun run() { callbackTarget." + methodName + "() }\n"
" }, callbackIntervalMillis)\n"
"}"
)
return [S, "me", "void"]
if len(segSpec) > 2 and funcName == "print":
exprs = []
for P in segSpec[2]:
[S2, _argTypeSpec] = self.codeGen.codeExpr(P[0], None, None, "ARG", genericArgs)
exprs.append("(" + S2 + ").toString()")
return ["System.out.print(" + " + ".join(exprs) + ")", "me", "String"]
return super().codeSpecialReference(segSpec, genericArgs)
###################################################### STRUCTS / FIELDS
def codeMain(self, classes, tags):
platform = progSpec.fetchTagValue(tags, "Platform")
if platform == "Android":
return ["", ""]
return ["", "\nfun main(args: Array<String>) {\n GLOBAL.static_Global.main(args);\n}\n"]
def codeArgText(self, argFieldName, argType, argOwner, tSpec, makeConst, typeArgList):
return "arg_" + argFieldName + ": " + self._cleanType(argType)
def _extractCompanionLines(self, structCode):
bodyLines = []
companionLines = []
for line in structCode.splitlines(True):
stripped = line.lstrip()
if stripped.startswith(self.companionMarker):
companionLines.append(stripped[len(self.companionMarker):])
else:
bodyLines.append(line)
return ["".join(bodyLines), companionLines]
def codeStructText(self, classes, attrList, parentClass, classInherits, classImplements, className, structCode, tags):
structCode = structCode.replace(" _ModeStrings modeStrings = new _ModeStrings();", " var modeStrings: _ModeStrings = _ModeStrings();")
[structCode, companionLines] = self._extractCompanionLines(structCode)
classAttrs = ""
if len(attrList) > 0:
for attr in attrList:
if attr == "abstract" and className != "GLOBAL":
classAttrs += "abstract "
parents = []
if parentClass != "":
parents.append(progSpec.getUnwrappedClassFieldTypeKeyWord(classes, className) + "()")
elif classInherits is not None:
parents.append(progSpec.getUnwrappedClassFieldTypeKeyWord(classes, classInherits[0][0]) + "()")
if classImplements is not None:
for item in classImplements:
if isinstance(item, list):
parents.append(item[0])
else:
parents.append(item)
parentText = ""
if parents:
parentText = " : " + ", ".join(parents)
if companionLines:
companionText = " companion object {\n"
for line in companionLines:
companionText += line
companionText += " }\n"
structCode = companionText + structCode
S = "\n" + classAttrs + "class " + className + parentText + " {\n" + structCode + "}\n"
typeArgList = progSpec.getTypeArgList(className)
if typeArgList is not None:
S = "\n" + classAttrs + self.codeTemplateHeader(className, typeArgList) + parentText + " {\n" + structCode + "}\n"
return [S, ""]
def addSpecialCode(self, filename):
return "\n\n//////////// Kotlin specific code:\n"
def addGLOBALSpecialCode(self, classes, tags):
filename = self.codeGen.makeTagText(tags, "FileName")
specialCode = 'const String: filename <- "' + filename + '"\n'
GLOBAL_CODE = """
struct GLOBAL{
%s
}
""" % (specialCode)
codeDogParser.AddToObjectFromText(classes[0], classes[1], GLOBAL_CODE, "Kotlin special code")
def codeNewVarStr(self, LTSpec, varName, fieldDef, indent, genericArgs, localVarsAlloc):
argList = fieldDef["argList"]
if argList and argList[-1] == "^&useCtor//8":
del argList[-1]
cvrtType = self.codeGen.convertType(LTSpec, "var", genericArgs)
cvrtType = self._cleanType(cvrtType)
owner = progSpec.getOwner(LTSpec)
localVarsAlloc.append([varName, LTSpec])
assignValue = ""
if fieldDef["value"]:
[RHS, RTSpec] = self.codeGen.codeExpr(fieldDef["value"][0], LTSpec, None, "RVAL", genericArgs)
RHS = self.checkForTypeCastNeed(LTSpec, RTSpec, RHS)
assignValue = RHS
elif argList is not None:
modelParams = self.codeGen.chooseCtorModelParams(LTSpec, argList, genericArgs)
[CPL, paramTypeList] = self.codeGen.codeArgList(varName, argList, modelParams, genericArgs)
if self._isValueType(cvrtType) or self._nonNullableType(cvrtType) == "String":
if len(paramTypeList) != 1:
cdErr("Kotlin value initialization expects one argument for " + varName + ".")
assignValue = self.checkForTypeCastNeed(LTSpec, paramTypeList[0], CPL[1:-1])
else:
assignValue = self._nonNullableType(cvrtType) + CPL
elif fieldDef["isAllocated"]:
assignValue = self._defaultValueForType(self._nonNullableType(cvrtType), "me")
else:
assignValue = self._defaultValueForType(cvrtType, owner)
return "var " + varName + ": " + cvrtType + " = " + assignValue
def codeVarFieldRHS_Str(self, fieldName, cvrtType, tSpec, argList, isAllocated, typeArgList, genericArgs):
cvrtType = self._cleanType(cvrtType)
fieldOwner = progSpec.getOwner(tSpec)
if argList is not None:
if argList and argList[-1] == "^&useCtor//8":
del argList[-1]
[CPL, _paramTypeList] = self.codeGen.codeArgList(fieldName, argList, None, genericArgs)
return " = " + self._nonNullableType(cvrtType) + CPL
return " = " + self._defaultValueForType(cvrtType, fieldOwner)
def codeConstField_Str(self, convertedType, fieldName, RHS, className, indent):
convertedType = self._cleanType(convertedType)
rhsExpr = self._rhsExpr(RHS)
if convertedType in ("Int", "Long", "Double", "Float", "Boolean", "String", "Char"):
decl = " const val " + fieldName + ": " + convertedType + " = " + rhsExpr + ";\n"
else:
decl = " val " + fieldName + ": " + convertedType + " = " + rhsExpr + ";\n"
return [self.companionMarker + decl, ""]
def codeVarField_Str(self, convertedType, tSpec, fieldName, RHS, className, tags, typeArgList, indent):
convertedType = self._cleanType(convertedType)
fieldOwner = progSpec.getOwner(tSpec)
if self._rhsExpr(RHS) == "null" and not convertedType.endswith("?"):
convertedType += "?"
if fieldOwner == "we":
decl = self._fieldDecl(" ", "@JvmField var", fieldName, convertedType, RHS, fieldOwner)
return [self.companionMarker + decl, ""]
return [self._fieldDecl(indent, "var", fieldName, convertedType, RHS, fieldOwner), ""]
###################################################### CONSTRUCTORS / FUNCS
def codeConstructors(self, className, ctorArgs, ctorOvrRide, ctorInit, copyCtorArgs, funcBody, callSuper):
if callSuper:
funcBody = callSuper + funcBody
withArgConstructor = ""
if ctorArgs != "" or funcBody != "":
withArgConstructor = " constructor(" + ctorArgs + ") {\n" + funcBody + ctorInit + " }\n"
copyConstructor = " constructor(fromVar: " + className + ") {\n" + copyCtorArgs + " }\n"
noArgConstructor = " constructor() {\n" + funcBody + "\n }\n"
if className in ("ourSubMenu", "GUI", "CanvasView", "APP", "GUI_ZStack"):
return ""
return withArgConstructor + copyConstructor + noArgConstructor
def codeConstructorInit(self, fieldName, count, defaultVal):
return " " + fieldName + " = arg_" + fieldName + ";\n"
def codeConstructorArgText(self, argFieldName, count, argType, defaultVal):
return "arg_" + argFieldName + ": " + self._cleanType(argType)
def codeCopyConstructor(self, fieldName, isTemplateVar):
if isTemplateVar:
return ""
return " " + fieldName + " = fromVar." + fieldName + ";\n"
def codeConstructorCall(self, className):
return " INIT();\n"
def codeSuperConstructorCall(self, parentClassName):
return " super<" + parentClassName + ">.toString();\n"
def codeFuncHeaderStr(self, className, fieldName, field, cvrtType, paramListText, localArgsAlloc, inheritMode, typeArgList, isNested, overRideOper, isStatic, indent):
structCode = "\n"
funcDefCode = ""
globalFuncs = ""
cvrtType = self._cleanType(cvrtType)
if cvrtType == "Unit":
retText = ""
else:
retText = ": " + cvrtType
if fieldName == className:
structCode += indent + "constructor(" + paramListText + ")"
elif className == "GLOBAL" and fieldName == "main":
structCode += indent + "fun main(args: Array<String>)"
localArgsAlloc.append(["args", {"owner": "me", "fieldType": "string", "arraySpec": None, "reqTagList": None, "paramList": None}])
else:
prefix = "abstract " if inheritMode == "pure-virtual" else ""
overrideText = "override " if inheritMode == "override" else ""
structCode += indent + prefix + overrideText + "fun " + fieldName + "(" + paramListText + ")" + retText
if inheritMode == "pure-virtual":
structCode += "\n"
return [structCode, funcDefCode, globalFuncs]
def codeTemplateHeader(self, className, typeArgList):
return "class " + className + "<" + ", ".join(typeArgList) + ">"
def extraCodeForTopOfFuntion(self, paramList):
if paramList is None or len(paramList) == 0 or paramList[0] == "<%":
return ""
S = ""
for param in paramList:
fieldName = param["fieldName"]
S += " var " + fieldName + " = arg_" + fieldName + ";\n"
return S
###################################################### STATEMENTS / MISC
def codeRethrow(self, nativeName):
return 'throw ' + nativeName
def codeTryCatch(self, tryText, catchText, errorName, nativeName, indent):
code = indent + 'try ' + tryText + indent + 'catch (' + nativeName + ': Exception) '
if errorName:
binding = indent + ' var ' + errorName + ': String = ' + nativeName + '.message ?: ' + nativeName + '.toString()\n'
catchText = '{\n' + binding + catchText[2:]
return code + catchText
def codeSetBits(self, LHS_Left, LHS_FieldType, prefix, bitMask, RHS, rhsType):
item = LHS_Left + "flags"
if LHS_FieldType == "flag":
mask = prefix + bitMask
if RHS not in ("true", "false") and progSpec.fieldTypeKeyword(rhsType) != "bool":
RHS += " != 0"
val = "if (" + RHS + ") " + mask + " else 0L"
elif LHS_FieldType == "mode":
mask = prefix + bitMask + "Mask"
offset = prefix + bitMask + "Offset"
if RHS == "false":
RHS = "0"
if RHS == "true":
RHS = "1"
val = "(" + RHS + ".toLong() shl " + offset + ".toInt())"
else:
cdErr("Unknown bit field type: " + LHS_FieldType)
return item + " = " + item + " and " + mask + ".inv(); " + item + " = " + item + " or (" + val + ");\n"
def checkIfSpecialAssignmentFormIsNeeded(self, action, indent, AltIDXFormat, RHS, rhsType, LHS, LHSParentType, LHS_FieldType):
if AltIDXFormat is None:
return ""
assignTag = action["assignTag"]
if not isinstance(assignTag, str):
assignTag = assignTag[0]
baseExpr = AltIDXFormat[0]
idxExpr = AltIDXFormat[2]
indexed = baseExpr + "[" + idxExpr + "]"
if assignTag == "":
if LHSParentType in ("string", "String") and LHS_FieldType in ("char", "Char"):
return indent + baseExpr + " = " + baseExpr + ".substring(0, " + idxExpr + ") + " + RHS + " + " + baseExpr + ".substring(" + idxExpr + " + 1);\n"
return indent + indexed + " = " + RHS + ";\n"
if assignTag == "+":
return indent + indexed + " = " + indexed + " + " + RHS + ";\n"
if assignTag == "-":
return indent + indexed + " = " + indexed + " - " + RHS + ";\n"
return ""
def applyTypecast(self, typeInCodeDog, itemToAlterType):
platformType = self.adjustBaseTypes(typeInCodeDog, False)
if platformType == "Int":
return "(" + itemToAlterType + ").toInt()"
if platformType == "Long":
return "(" + itemToAlterType + ").toLong()"
if platformType == "Double":
return "(" + itemToAlterType + ").toDouble()"
if platformType == "Float":
return "(" + itemToAlterType + ").toFloat()"
if platformType == "Char":
return "(" + itemToAlterType + ").toChar()"
return "(" + itemToAlterType + " as " + platformType + ")"
def codeReadBitField(self, S, connector, prevLen, prefix, segName, fType):
flagsExpr = S[0:prevLen] + connector + "flags"
if fType == "flag":
return "(" + flagsExpr + " and " + prefix + segName + ")"
return "(((" + flagsExpr + " and " + prefix + segName + "Mask) shr " + prefix + segName + "Offset.toInt()).toInt())"
def codeSwitchStmt(self, action, indent, returnType, genericArgs):
[switchKeyExpr, switchKeyTypeSpec] = self.codeGen.codeExpr(action["switchKey"][0], None, None, "RVAL", genericArgs)
S = indent + "when (" + self.codeSwitchExpr(switchKeyExpr, switchKeyTypeSpec) + ") {\n"
for sCases in action["switchCases"]:
caseValues = []
for sCase in sCases[0]:
[caseKeyValue, caseKeyTypeSpec] = self.codeGen.codeExpr(sCase[0], None, None, "RVAL", genericArgs)
caseValues.append(self.codeSwitchCase(caseKeyValue, caseKeyTypeSpec))
S += indent + " " + ", ".join(caseValues) + " -> "
S += self.codeGen.codeActionSeq(sCases[1], indent + " ", returnType, genericArgs)
defaultCase = action["defaultCase"]
if defaultCase and len(defaultCase) > 0:
S += indent + " else -> "
S += self.codeGen.codeActionSeq(defaultCase, indent + " ", returnType, genericArgs)
S += indent + "}\n"
return S
def includeDirective(self, libHdr):
return "import " + libHdr + "\n"
def generateMainFunctionality(self, classes, tags):
runCode = progSpec.fetchTagValue(tags, "runCode")
if runCode is None:
runCode = ""
platform = progSpec.fetchTagValue(tags, "Platform")
if platform == "Android":
mainFuncCode = """
me void: runDogCode() <- {
""" + runCode + """
}
"""
else:
mainFuncCode = """
me void: main() <- {
initialize("")
""" + runCode + """
deinitialize()
}
"""
progSpec.addClass(classes[0], classes[1], "GLOBAL", "struct", "SEQ", ["//^", "Main class"])
codeDogParser.AddToObjectFromText(classes[0], classes[1], progSpec.wrapFieldListInObjectDef("GLOBAL", mainFuncCode), "Kotlin start-up code")
def __init__(self):
self.enumDefaults = {}
self.enumTypes = set()
print("INIT")