-
Notifications
You must be signed in to change notification settings - Fork 5
/
ast.py
988 lines (921 loc) · 32 KB
/
ast.py
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
# Tampio Compiler
# Copyright (C) 2018 Iikka Hauhio
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from collections import namedtuple
from itertools import chain
from inflect import CASES_ABRV
from fatal_error import fatalError, typeError, notfoundError, warning, TampioError
from hierarchy import Class, classes, getClass, isClass, classSet, getFunctions, getFields
# kääntäjän tila
def initializeCompiler(include_file):
global global_variables, aliases, includeFile, options, block_frame, tokens
options = {}
block_frame = BlockData(None, set(), False, None)
global_variables = {}
aliases = {}
includeFile = include_file
tokens = None
class CompilerFrame:
def __init__(self, tl):
self.tokens = tl
def __enter__(self):
global options, block_frame, tokens
self.prev_options = options
self.prev_block_frame = block_frame
self.prev_tokens = tokens
options = {
"kohdekoodi": False,
"käyttömäärittelyt": False
}
block_frame = BlockData(None, {}, False, None)
tokens = self.tokens
def __exit__(self, *args):
global options, block_frame, tokens
options = self.prev_options
block_frame = self.prev_block_frame
tokens = self.prev_tokens
BlockData = namedtuple("BlockData", "variables backreferences block_mode self_type")
class BlockFrame:
def __init__(self, new_frame):
self.new_frame = new_frame
def __enter__(self):
global block_frame
self.prev_frame = block_frame
block_frame = self.new_frame
def __exit__(self, *args):
global block_frame
block_frame = self.prev_frame
# apufunktiot
def formAbrv(form):
if form in CASES_ABRV:
return CASES_ABRV[form]
else:
return form[0].upper() + form[1:].lower()
def typeToJs(typename):
while typename in aliases:
typename = aliases[typename]
return escapeIdentifier(typename)
def escapeIdentifier(identifier):
return identifier.replace("-", "_")
# moduulin käätäminen
def compileModule(declarations, on_error, tokens):
with CompilerFrame(tokens):
for decl in declarations:
try:
decl.buildHierarchy()
except TampioError as e:
on_error(e)
with CompilerFrame(tokens):
ans = ""
additional_statements = ""
for decl in declarations:
try:
decl.validateTree()
ans += decl.compile() + "\n"
additional_statements += decl.compileAdditionalStatements()
except TampioError as e:
on_error(e)
return ans + additional_statements
# lohkon kääntäminen
def compileBlock(statements, indent, parameters):
bcs = set()
for stmt in statements:
bcs.update(set(stmt.backreferences()))
ans = ""
for bc in bcs:
ans += " "*indent + "var se_" + escapeIdentifier(bc) + " = null;\n"
prev_variables = block_frame.variables or {**global_variables}
variables = {**prev_variables, **parameters}
with BlockFrame(BlockData(variables, bcs, True, block_frame.self_type)):
for stmt in statements:
# eksplisiittisesti luodut uudet muuttujat
variables.update(stmt.createdVariables())
# uusi-avainsanalla luodut uudet muuttujat
new_vars = stmt.newVariables()
for name, vtype in new_vars.items():
ans += " "*indent + "var " + escapeIdentifier(name) + " = null;\n"
variables.update(new_vars)
# vielä mainitsemattomat muuttujat ovat luodaan (poisluetaan väliaikaismuuttujat)
if options["käyttömäärittelyt"]:
new_vars = stmt.variables()
tmp_vars = stmt.temporaryVariables()
for name, vtype in new_vars.items():
if name not in variables and name not in tmp_vars:
warning("autodeclaration of " + name + " as " + vtype)
ans += " "*indent + "var " + escapeIdentifier(name) + " = new " + escapeIdentifier(vtype) + "({});\n"
variables.update(new_vars)
ans += stmt.compile(indent=indent)
return ans
# määrittelyjen kääntäminen
class Decl:
def __init__(self, statements):
self.statements = statements
def compile(self):
return self.compileDecl()
def compileAdditionalStatements(self):
if self.statements:
return ";(function() {\n" + compileBlock(self.statements, 0, {}) + "})();\n"
else:
return ""
def buildHierarchy(self):
pass
def validateTree(self):
pass
class SetOptionDecl(Decl):
def __init__(self, positive, option):
self.positive = positive
self.option = option
self.statements = []
def compileDecl(self):
options[self.option] = self.positive
return ""
def buildHierarchy(self):
options[self.option] = self.positive
return ""
class TargetCodeDecl(Decl):
def __init__(self, code, stmts):
Decl.__init__(self, stmts)
self.code = code
def compileDecl(self):
return self.code
class IncludeTargetCodeFileDecl(Decl):
def __init__(self, filename, stmts):
Decl.__init__(self, stmts)
self.file = filename
def compileDecl(self):
with open(self.file) as f:
return f.read()
class IncludeFileDecl(Decl):
def __init__(self, filename, stmts):
Decl.__init__(self, stmts)
self.file = filename
def compileDecl(self):
return ""
def buildHierarchy(self):
includeFile(self.file)
class VariableDecl(Decl):
def __init__(self, var, vtype, value, stmts):
Decl.__init__(self, stmts)
self.var = var
self.type = vtype
self.value = value
def compileDecl(self):
return "var " + escapeIdentifier(self.var) + " = " + self.value.compile(0) + ";"
def compileAdditionalStatements(self):
if self.statements:
return ";(function() {\n" + compileBlock(self.statements, 1, {}) + "}).call(" + escapeIdentifier(self.var) + ");\n"
else:
return ""
def buildHierarchy(self):
global_variables[self.var] = self.value.inferType()
def validateTree(self):
self.value.validateTree()
class ProcedureDecl(Decl):
def __init__(self, signature, body, stmts):
Decl.__init__(self, stmts)
self.signature = signature
self.body = body
def compileDecl(self):
if isinstance(self.signature, ProcedureCallStatement):
return ("function " + self.signature.compile(semicolon=False)
+ " {\n" + compileBlock(self.body, 1, self.signature.variables()) + "};")
elif isinstance(self.signature, MethodCallStatement):
with BlockFrame(block_frame._replace(self_type=self.signature.obj.type)):
return (
typeToJs(self.signature.obj.type) + ".prototype."
+ self.signature.compileName()
+ " = function" + self.signature.compileArgs(indent=0) + " {\n"
+ " var " + escapeIdentifier(self.signature.obj.name) + " = this;\n"
+ compileBlock(self.body, 1, self.signature.variables())
+ "};")
else:
fatalError("TODO")
def buildHierarchy(self):
if isinstance(self.signature, MethodCallStatement):
getClass(self.signature.obj.type).addMethod(self.signature.name, sorted(self.signature.args.keys()))
def validateTree(self):
if isinstance(self.signature, MethodCallStatement):
with BlockFrame(block_frame._replace(self_type=self.signature.obj.type)):
for s in self.body:
s.validateTree()
else:
for s in self.body:
s.validateTree()
class ClassDecl(Decl):
def __init__(self, name, fields, stmts, super_type=None):
Decl.__init__(self, stmts)
self.name = name
self.fields = fields
self.super = super_type
def compile(self):
class_name = escapeIdentifier(self.name)
ans = "function " + class_name + "(vals) {\n"
if self.super:
ans += " " + escapeIdentifier(self.super) + ".call(this, vals);\n"
for name, _, number, _, _, def_val in self.fields:
field = "this." + escapeIdentifier(name)
ans += " if (\"" + name + "\" in vals) " + field + " = vals[\"" + name + "\"];\n"
if def_val:
ans += " else " + field + " = " + def_val.compile(0) + ";\n"
elif number == "plural":
ans += " else " + field + " = [];\n"
ans += " if (" + class_name + ".prototype.syntyä_A__N) " + class_name + ".prototype.syntyä_A__N.call(this);\n"
ans += "};"
if self.super:
ans += "\n" + class_name + ".prototype = Object.create(" + escapeIdentifier(self.super) + ".prototype);"
ans += "\n" + class_name + ".prototype.constructor = " + class_name + ";"
for name, _, _, _, _, _ in self.fields:
ans += "\n" + class_name + ".prototype.f_" + escapeIdentifier(name) + " = function() { return this." + escapeIdentifier(name) + "; };"
return ans
def buildHierarchy(self):
cl = Class(self.name, self.super)
classes[self.name] = cl
for name, _, number, _, _, _ in self.fields:
cl.addField(name, number)
class TargetCodeClassDecl(Decl):
def __init__(self, name, tc_name, stmts):
Decl.__init__(self, stmts)
self.name = name
self.tc_name = tc_name
def compileDecl(self):
return ""
def buildHierarchy(self):
cl = Class(self.name, None)
classes[self.name] = cl
if self.name != self.tc_name:
aliases[self.name] = self.tc_name
class AliasClassDecl(Decl):
def __init__(self, alias_name, real_name, stmts):
Decl.__init__(self, stmts)
self.alias_name = alias_name
self.real_name = real_name
def compileDecl(self):
return ""
def buildHierarchy(self):
classes[self.alias_name] = getClass(self.real_name)
aliases[self.alias_name] = self.real_name
class Whereable:
def compileWheres(self, indent=1):
return "".join([" "*indent + "var "+escapeIdentifier(name)+" = "+val.compile(indent)+";\n" for name, _, val in self.wheres])
def whereSubexpressions(self):
ans = []
for _, _, val in self.wheres:
ans += val.subexpressions()
return ans
def whereCreatedVariables(self):
ans = {}
for name, vtype, val in self.wheres:
ans[name] = val.inferType()
return ans
def validateWheres(self):
for _, _, val in self.wheres:
val.validateTree()
class FunctionDecl(Whereable,Decl):
def __init__(self, vtype, field, self_param, param, param_case, body, wheres, memoize, stmts):
Decl.__init__(self, stmts)
self.type = vtype
self.field = field
self.self_param = self_param
self.param = param
self.param_case = param_case
self.body = body
self.wheres = wheres
self.memoize = memoize
def compile(self):
ans = typeToJs(self.type) + ".prototype.f_" + escapeIdentifier(self.field)
if self.param_case:
ans += "_" + formAbrv(self.param_case)
ans += " = function("
if self.param:
ans += escapeIdentifier(self.param)
ans += ") {\n"
if self.memoize:
ans += " if (this." + escapeIdentifier(self.field) + " !== undefined) return this." + escapeIdentifier(self.field) + ";\n"
if self.self_param != "":
ans += " var " + escapeIdentifier(self.self_param) + " = this;\n"
with BlockFrame(block_frame._replace(self_type=self.type)):
ans += self.compileWheres()
if self.memoize:
ans += " this." + escapeIdentifier(self.field) + " = " + self.body.compile(0) + ";\n"
ans += " return this." + escapeIdentifier(self.field) + ";\n};"
else:
ans += " return " + self.body.compile(0) + ";\n};"
return ans
def buildHierarchy(self):
getClass(self.type).addFunction(self.field, self.param_case, self.body)
def validateTree(self):
with BlockFrame(block_frame._replace(self_type=self.type)):
self.validateWheres()
self.body.validateTree()
class CondFunctionDecl(Whereable,Decl):
def __init__(self, vtype, signature, condition, wheres, stmts):
Decl.__init__(self, stmts)
self.type = vtype
self.signature = signature
self.condition = condition
self.wheres = wheres
def compileDecl(self):
ans = typeToJs(self.type) + ".prototype." + self.signature.compileName() + " = function"
ans += self.signature.compileArgs(indent=0)
ans += " {\n"
if self.signature.getSelfArg().name:
ans += " var " + escapeIdentifier(self.signature.getSelfArg().name) + " = this;\n"
ans += self.compileWheres()
ans += " return " + self.condition.compile(0) + ";\n};"
return ans
def buildHierarchy(self):
getClass(self.type).addComparisonOperator(self.signature.compileName())
def validateTree(self):
with BlockFrame(block_frame._replace(self_type=self.type)):
self.validateWheres()
self.condition.validateTree()
# lauseiden kääntäminen
class Recursive:
def subexpressions(self):
return [self]
def search(self, f):
ans = []
for subexpr in self.subexpressions():
if subexpr is not self:
ans += f(subexpr)
return ans
def searchDict(self, f):
ans = {}
for subexpr in self.subexpressions():
if subexpr is not self:
ans.update(f(subexpr))
return ans
def backreferences(self):
return self.search(lambda e: e.backreferences())
# kaikki muuttujat
def variables(self):
return self.searchDict(lambda e: e.variables())
# lausekkeissa luodut muuttujat (muuttujaluonti käännetään lausekkeen yhteydessä)
def createdVariables(self):
return self.searchDict(lambda e: e.createdVariables())
# lausekkeissa luodut muuttujat (muuttujaluonti käännetään ennen lauseketta)
def newVariables(self):
return self.searchDict(lambda e: e.newVariables())
# väliaikaismuuttujat
def temporaryVariables(self):
return self.searchDict(lambda e: e.temporaryVariables())
# syntaksipuun validoiminen
def validateTree(self):
self.validate()
for e in self.subexpressions():
if e is not self:
e.validateTree()
def validate(self):
pass
class ForStatement(Recursive):
def __init__(self, var, expr, stmt):
self.var = var
self.expr = expr
self.stmt = stmt
def subexpressions(self):
return self.expr.subexpressions() + self.stmt.subexpressions()
def compile(self, indent=0):
return (" "*indent
+ "for (const " + escapeIdentifier(self.var)
+ " of " + self.expr.compile(indent)
+ ") {\n" + self.stmt.compile(indent=indent+1)
+ " "*indent + "}\n")
class IfStatement(Recursive):
def __init__(self, condition, block, is_else):
self.condition = condition
self.block = block
self.is_else = is_else
def subexpressions(self):
ans = []
ans += self.condition.subexpressions()
for s in self.block:
ans += s.subexpressions()
return ans
def compile(self, indent=0):
ans = ""
self.condition.compileWheres()
return ans + (" "*indent
+ ("else " if self.is_else else "")
+ "if (" + self.condition.compile(indent+1) + ") {\n"
+ "".join([s.compile(indent=indent+1) for s in self.block])
+ " "*indent + "}\n")
class QuantifierCondExpr(Whereable,Recursive):
def __init__(self, quant, var, expr, cond, wheres=[]):
self.quantifier = quant
self.var = var
self.expr = expr
self.cond = cond
self.wheres = wheres
def subexpressions(self):
return self.expr.subexpressions() + self.cond.subexpressions() + self.whereSubexpressions()
def createdVariables(self):
return {**super().createdVariables(), **self.whereCreatedVariables()}
def compile(self, indent):
return self.expr.compile(indent) + (".every(" if self.quantifier == "jokainen" else ".some(") + self.var + " => " + self.cond.compile(indent) + ")"
def validate(self):
self.validateWheres()
class CondConjunctionExpr(Whereable,Recursive):
def __init__(self, op, exprs, wheres=[]):
self.op = op
self.exprs = exprs
self.wheres = []
def subexpressions(self):
ans = []
for expr in self.exprs:
ans += expr.subexpressions()
return ans
def createdVariables(self):
return {**super().createdVariables(), **self.whereCreatedVariables()}
def compile(self, indent):
return "((" + (") " + self.op + " (").join([e.compile(indent) for e in self.exprs]) + "))"
def validate(self):
self.validateWheres()
class CondOperatorExpr(Whereable,Recursive):
def __init__(self, negation, operator, left, right, wheres=[]):
self.negation = negation
self.operator = operator
self.left = left
self.right = right
self.wheres = wheres
def subexpressions(self):
return self.left.subexpressions() + (self.right.subexpressions() if self.right else []) + self.whereSubexpressions()
def createdVariables(self):
return {**super().createdVariables(), **self.whereCreatedVariables()}
def getSelfArg(self):
return self.left
def compileName(self):
"""Tätä saa käyttää vain, jos operaattori ei ole JavaScript-operaattori"""
assert self.operator[0] == "."
return escapeIdentifier(self.operator[1:])
def compileArgs(self, indent):
return "(" + (self.right.compile(indent) if self.right else "") + ")"
def compile(self, indent):
ans = self.left.compile(indent) + escapeIdentifier(self.operator)
if self.operator[0] == ".":
ans += "("
if self.right:
ans += self.right.compile(indent)
if self.operator[0] == ".":
ans += ")"
if self.negation:
return "!(" + ans + ")"
else:
return ans
def validate(self):
self.validateWheres()
class ArgContainer:
def compileName(self):
keys = sorted(self.args.keys())
return escapeIdentifier(self.name) + "_" + "".join([formAbrv(form) for form in keys])
def compileArgs(self, indent):
keys = sorted(self.args.keys())
return "(" + ", ".join([self.args[key].compile(indent) for key in keys]) + ")"
class CondFunctionExpr(Whereable,Recursive,ArgContainer):
def __init__(self, negation, func_name, self_obj, args, wheres=[]):
self.negation = negation
self.name = func_name
self.self_obj = self_obj
self.args = args
self.wheres = wheres
def subexpressions(self):
ans = self.self_obj.subexpressions()
for arg in self.args.values():
ans += arg.subexpressions()
return ans + self.whereSubexpressions()
def createdVariables(self):
return {**super().createdVariables(), **self.whereCreatedVariables()}
def getSelfArg(self):
return self.self_obj
def compileName(self):
return "b_" + super().compileName()
def compile(self, indent):
ans = self.self_obj.compile(indent) + "." + self.compileName() + self.compileArgs(indent)
if self.negation:
return "!(" + ans + ")"
else:
return ans
def validate(self):
self.validateWheres()
class BlockStatement(Whereable,Recursive):
def __init__(self, stmts, wheres):
self.stmts = stmts
self.wheres = wheres
def subexpressions(self):
ans = []
for s in self.stmts:
ans += s.subexpressions()
return ans + self.whereSubexpressions()
def createdVariables(self):
return {**super().createdVariables(), **self.whereCreatedVariables()}
def compile(self, semicolon=True, indent=0):
return self.compileWheres(indent) + "".join([s.compile(indent=indent) for s in self.stmts])
def validate(self):
self.validateWheres()
class CallStatement(Recursive,ArgContainer):
def __init__(self, name, args, output_var, async_block):
self.name = name
self.args = args
self.output_var = output_var
self.async_block = async_block
def subexpressions(self):
ans = [self]
for v in self.args.values():
ans += v.subexpressions()
for _, _, _, s in self.async_block:
ans += s.subexpressions()
return ans
def variables(self):
return super().variables()
def createdVariables(self):
if self.output_var:
return dict([self.output_var])
else:
return {}
def temporaryVariables(self):
ans = []
for _, param, ptype, _ in self.async_block:
ans += [(param, ptype)]
return ans
def compileAssignment(self):
if self.output_var:
return "var " + escapeIdentifier(self.output_var[0]) + " = "
else:
return ""
def compileAsync(self, indent):
ans = ""
for mname, param, ptype, stmt in self.async_block:
with BlockFrame(block_frame._replace(variables={**block_frame.variables, **dict([(param, ptype)])})):
ans += ("." + escapeIdentifier(mname)
+ "(" + escapeIdentifier(param) + " =>\n"
+ stmt.compile(indent=indent+1, semicolon=False)
+ " "*indent + ")")
return ans
def compile(self, semicolon=True, indent=0):
ans = " "*indent + self.compileAssignment() + self.compileName() + self.compileArgs(indent) + self.compileAsync(indent)
if semicolon:
ans += ";\n"
return ans
class ProcedureCallStatement(CallStatement):
def compile(self, semicolon=True, indent=0):
if self.name == "suorittaa!" and list(self.args.keys()) == ["nimento"] and isinstance(self.args["nimento"], StrExpr) and options["kohdekoodi"]:
return " "*indent + self.args["nimento"].str + ("\n" if semicolon else "")
else:
return super().compile(semicolon=semicolon, indent=indent)
BUILTIN_ASSIGN_METHODS = [
("asettaa_P", "tulento", "nimento", lambda lval, arg: lval + " = " + arg),
("olla_A", "nimento", "nimento", lambda lval, arg: lval + " = " + arg),
("kasvattaa_P", "osanto", "ulkoolento", lambda lval, arg: lval + " += " + arg)
]
class MethodCallStatement(CallStatement,Recursive):
def __init__(self, obj, obj_case, name, args, output_var, async_block):
self.obj = obj
self.obj_case = obj_case
self.name = name
self.args = args
self.output_var = output_var
self.async_block = async_block
def subexpressions(self):
return super().subexpressions() + self.obj.subexpressions()
def compileName(self):
return super().compileName() + "_" + formAbrv(self.obj_case)
def compile(self, semicolon=True, indent=0):
ans = " "*indent
is_lval = isinstance(self.obj, VariableExpr) or isinstance(self.obj, SubscriptExpr) or isinstance(self.obj, FieldExpr)
def compileLval():
if isinstance(self.obj, FieldExpr):
return self.obj.obj.compile(indent) + "." + escapeIdentifier(self.obj.field)
else:
return self.obj.compile(indent)
for mname, ocase, acase, f in BUILTIN_ASSIGN_METHODS:
if is_lval and self.name == mname and self.obj_case == ocase and list(self.args.keys()) == [acase]:
ans += f(compileLval(), self.args[acase].compile(indent))
break
else:
if self.name == "palauttaa_P" and self.obj_case == "nimento" and len(self.args) == 0:
ans += "return " + self.obj.compile(indent)
else:
ans += self.compileAssignment() + self.obj.compile(indent) + "." + self.compileName() + self.compileArgs(indent) + self.compileAsync(indent)
if semicolon:
ans += ";\n"
return ans
class MethodAssignmentStatement(Recursive):
def __init__(self, obj, obj_case, method, params, body):
self.obj = obj
self.obj_case = obj_case
self.method = method
self.params = params
self.body = body
def subexpressions(self):
return self.obj.subexpressions() # ei palauta vartalon sisältämiä alalausekkeita (kuten ei lambdakaan)
def compileName(self):
keys = sorted(self.params.keys())
return escapeIdentifier(self.method) + "_" + "".join([formAbrv(form) for form in keys]) + "_" + formAbrv(self.obj_case)
def compileParams(self, indent):
keys = sorted(self.params.keys())
with BlockFrame(block_frame._replace(block_mode=False)):
ans = "(" + ", ".join([self.params[key].compile(indent) for key in keys]) + ")"
return ans
def compile(self, semicolon=True, indent=0):
ans = " "*indent
ans += self.obj.compile(indent)
ans += ".t_assign(\"" + self.compileName()
ans += "\", "
ans += self.compileParams(indent)
ans += " => {\n"
ans += compileBlock(self.body, indent+1, dict(chain.from_iterable([self.params[key].variables().items() for key in self.params])))
ans += " "*indent + "});"
if semicolon:
ans += ";\n"
return ans
# lausekkeiden kääntäminen
class Expr:
def __init__(self):
self.type_cache = None
def inferType(self, expected_types=[]):
if self.type_cache is None:
self.type_cache = self.infer(expected_types)
return self.type_cache
class VariableExpr(Expr,Recursive):
def __init__(self, name, vtype=None, initial_value=None, place=None):
Expr.__init__(self)
self.name = name
self.type = vtype
self.initial_value = initial_value
self.place = place
def subexpressions(self):
if self.initial_value:
return [self] + self.initial_value.subexpressions()
else:
return [self]
def newVariables(self):
if self.type and self.initial_value:
return {self.name: self.type}
else:
return {}
def variables(self):
if self.type:
return {self.name: self.type}
else:
return {}
def compile(self, indent):
ans = escapeIdentifier(self.name)
if self.initial_value:
ans = "(" + ans + "=" + self.initial_value.compile(indent) + ")"
if self.type in block_frame.backreferences:
ans = "(se_" + escapeIdentifier(self.type) + "=" + ans + ")"
return ans
def infer(self, expected_types):
if block_frame.block_mode and self.name in block_frame.variables:
ans = block_frame.variables[self.name]
elif self.name in global_variables:
ans = global_variables[self.name]
elif self.type:
ans = set()
else:
return classSet()
if not ans:
ans = set()
for i in range(len(self.type)):
if isClass(self.type[i:]):
ans.add(getClass(self.type[i:]))
return ans
def validate(self):
if block_frame.block_mode and self.type and not self.initial_value:
if self.name not in block_frame.variables:
if self.place:
notfoundError("variable not found: " + self.name, tokens, self.place)
else:
notfoundError("variable not found: " + self.name)
#if self.type and len(self.infer([])) == 0:
# warning("class not found: " + self.type, tokens, self.place)
class BackreferenceExpr(Expr,Recursive):
def __init__(self, name, may_be_field=False):
Expr.__init__(self)
self.name = name
self.may_be_field = may_be_field
def backreferences(self):
return [self.name]
def compile(self, indent):
name = escapeIdentifier(self.name)
if self.may_be_field:
return "(this.f_" + name + "!==undefined ? this.f_" + name + " : se_" + name + ")"
else:
return "se_" + name
def infer(self, expected_types):
# TODO
return classSet()
ARI_OPERATORS = {
"lisätty_E": ("sisatulento", "+", ["luku", "merkkijono"]),
"ynnätty_E": ("sisatulento", "+", ["luku", "merkkijono"]),
"kasvatettu_E": ("ulkoolento", "+", ["luku", "merkkijono"]),
"yhdistetty_E": ("sisatulento", ".concat", []),
"vähennetty_E": ("ulkoolento", "-", ["luku"]),
"kerrottu_E": ("ulkoolento", "*", ["luku"]),
"jaettu_E": ("ulkoolento", "/", ["luku"]),
"rajattu_E": ("sisatulento", "%", ["luku"])
}
class FieldExpr(Expr,Recursive):
def __init__(self, obj, field, arg_case=None, arg=None, place=None):
Expr.__init__(self)
self.obj = obj
self.field = field
self.arg_case = arg_case
self.arg = arg
self.place = place
def subexpressions(self):
return [self] + self.obj.subexpressions() + (self.arg.subexpressions() if self.arg else [])
def isArithmetic(self):
return self.field in ARI_OPERATORS and self.arg_case == ARI_OPERATORS[self.field][0]
def isTargetCode(self):
return self.field == "kohdekoodi_E" and isinstance(self.obj, StrExpr) and options["kohdekoodi"]
def compile(self, indent):
if self.isArithmetic():
return self.compileArithmetic(indent)
elif self.isTargetCode():
return "(" + self.obj.str + ")"
ans = self.obj.compile(indent) + ".f_" + escapeIdentifier(self.field)
if self.arg_case:
ans += "_" + formAbrv(self.arg_case)
ans += "("
if self.arg:
ans += self.arg.compile(indent)
return ans + ")"
def compileArithmetic(self, indent):
operator = ARI_OPERATORS[self.field][1]
if operator[0] == ".":
return self.obj.compile(indent) + operator + "(" + self.arg.compile(indent) + ")"
else:
return "(" + self.obj.compile(indent) + operator + self.arg.compile(indent) + ")"
def infer(self, expected_types):
if self.isArithmetic():
return set(map(getClass, ARI_OPERATORS[self.field][2])) or classSet()
elif self.isTargetCode():
return classSet()
functions = [
f
for f in getFunctions(self.field+"_"+str(self.arg_case))
if not expected_types or not f.inferType().isdisjoint(expected_types)
]
possible_obj_types = set()
for f in functions:
possible_obj_types.update(f.classes())
fields = getFields(self.field) if not self.arg_case else []
if fields:
for f in fields:
possible_obj_types.update(f.classes())
obj_types = self.obj.inferType(possible_obj_types)
for f in fields:
if not f.classes().isdisjoint(obj_types):
return classSet()
ret_types = set()
for f in functions:
if not f.classes().isdisjoint(obj_types):
ret_types.update(f.inferType())
if not ret_types:
warning("unsuccessful inference of " + self.field + ", argument type is illegal: "
+ "expected argument to be one of: {" + ", ".join([cl.name for cl in possible_obj_types])
+ "}, but it is one of: {" + ", ".join([cl.name for cl in obj_types]) + "}",
tokens, self.place, severity="Note")
return ret_types
def validate(self):
if len(self.inferType()) == 0:
if self.place:
typeError("cannot infer the type of expression", tokens, self.place)
else:
typeError("cannot infer the type of expression")
# erikoistapaukset
if self.isArithmetic() or self.isTargetCode():
pass
# päätapaus
elif len(getFunctions(self.field+"_"+str(self.arg_case)) + (getFields(self.field) if not self.arg_case else [])) == 0:
if self.place:
typeError("member not found", tokens, self.place)
else:
typeError("member not found")
class SubscriptExpr(Expr,Recursive):
def __init__(self, obj, index, is_end_index=False):
Expr.__init__(self)
self.obj = obj
self.index = index
self.is_end_index = is_end_index
def subexpressions(self):
return [self] + self.obj.subexpressions()
def compile(self, indent):
if self.is_end_index:
return self.obj.compile(indent) + ".nth_last(" + self.index.compile(indent) + ")"
else:
return self.obj.compile(indent) + "[" + self.index.compile(indent) + "-1]"
def infer(self, expected_types):
return classSet()
class SliceExpr(Expr,Recursive):
def __init__(self, obj, start, end):
Expr.__init__(self)
self.obj = obj
self.start = start
self.end = end
def subexpressions(self):
return [self] + self.obj.subexpressions()
def compile(self, indent):
ans = self.obj.compile(indent) + ".slice("
ans += self.start.compile(indent) + "-1"
if self.end:
ans += ", " + self.end.compile(indent)
ans += ")"
return ans
def infer(self, expected_types):
return set([getClass("kohdekoodilista")])
class NumExpr(Expr,Recursive):
def __init__(self, num):
Expr.__init__(self)
self.num = num
def compile(self, indent):
return "(" + str(self.num) + ")"
def infer(self, expected_types):
return set([getClass("luku")])
class StrExpr(Expr,Recursive):
def __init__(self, string):
Expr.__init__(self)
self.str = string
def compile(self, indent):
return repr(self.str)
def infer(self, expected_types):
return set([getClass("merkkijono")])
class NewExpr(Expr,Recursive):
def __init__(self, typename, args, variable=None):
Expr.__init__(self)
self.type = typename
self.args = args
self.variable = variable
def subexpressions(self):
ans = []
for arg in self.args:
ans += arg.value.subexpressions()
return [self] + ans
def newVariables(self):
if self.variable:
return {self.variable: self.type}
else:
return {}
def compile(self, indent):
ans = "new " + typeToJs(self.type) + "({" + ", ".join(["\"" + arg.field + "\": " + arg.value.compile(indent) for arg in self.args]) + "})"
if self.type in block_frame.backreferences:
ans = "(se_" + escapeIdentifier(self.type) + "=" + ans + ")"
if self.variable:
ans = "(" + escapeIdentifier(self.variable) + "=" + ans + ")"
return ans
def infer(self, expected_types):
return set([getClass(self.type)])
class CtorArgExpr:
def __init__(self, field, value):
self.field = field
self.value = value
class ListExpr(Expr,Recursive):
def __init__(self, values):
Expr.__init__(self)
self.values = values
def subexpressions(self):
ans = []
for val in self.values:
ans += val.subexpressions()
return [self] + ans
def compile(self, indent):
return "[" + ", ".join([value.compile(indent) for value in self.values]) + "]"
def infer(self, expected_types):
return set([getClass("kohdekoodilista")])
class LambdaExpr(Expr,Recursive):
def __init__(self, body):
Expr.__init__(self)
self.body = body
def compile(self, indent):
return "() => {\n" + compileBlock(self.body, indent+1, {}) + " "*indent + "}"
def infer(self, expected_types):
return set([getClass("kohdekoodifunktio")])
# ei alalausekkeita
class TernaryExpr(Expr,Recursive):
def __init__(self, condition, then, otherwise):
Expr.__init__(self)
self.condition = condition
self.then = then
self.otherwise = otherwise
def subexpressions(self):
ans = []
ans += self.condition.subexpressions()
return [self] + ans + self.then.subexpressions() + self.otherwise.subexpressions()
def compile(self, indent):
return ("((" + self.condition.compile(indent)
+ ") ? (" + self.then.compile(indent)
+ ") : (" + self.otherwise.compile(indent) + "))")
def infer(self, expected_types):
return self.then.inferType(expected_types) | self.otherwise.inferType(expected_types)