forked from gracelang/minigrace
-
Notifications
You must be signed in to change notification settings - Fork 3
/
genjs.grace
1840 lines (1756 loc) · 64.1 KB
/
genjs.grace
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
dialect "standard"
import "io" as io
import "sys" as sys
import "ast" as ast
import "util" as util
import "unixFilePath" as filePath
import "xmodule" as xmodule
import "errormessages" as errormessages
import "identifierresolution" as identifierresolution
import "shasum" as shasum
import "regularExpression" as regex
def nodeTally = dictionary.empty
def inBrowser = native "js" code ‹
result = (typeof global === "undefined" ? GraceTrue : GraceFalse);
›
var indent := ""
var verbosity := 30
var pad1 := 1
var auto_count := 0
var constants := list []
var output := list []
var usedvars := list []
var declaredvars := list []
var initializedVars := set ["$dialect", "$module"]
method saveInitializedVars {
// returns the current set of initialized vars,
// to be used in restoreInitializedVars
def result = initializedVars
initializedVars := set ["$dialect", "$module"]
result
}
method restoreInitializedVars(savedState) {
// restores the set of initialized vars to savedState,
// the result of saveInitializedVars
initializedVars := savedState
}
var bblock := "entry"
var outfile
var modname := "main"
var buildtype := "bc"
var inBlock := false
var compilationDepth := 0
def importedModules = set.empty
def topLevelTypes = set.empty
var debugMode := false
var priorLineSeen := 0
var priorLineComment := ""
var priorLineEmitted := 0
var emitTypeChecks := true
var emitUndefinedChecks := true
var emitArgChecks := true
var emitPositions := true
var emod // the name of the module being compiled, escaped
// so that it is a legal identifier
var modNameAsString // the name of the module surrounded by quotes,
// with internal special chars escaped.
var thisModule // the register (string) that will refer to this module
/////////////////////////////////////////////////////////////
//
// Utility methods
//
/////////////////////////////////////////////////////////////
method increaseindent {
indent := indent ++ " "
}
method decreaseindent {
if(indent.size <= 2) then {
indent := ""
} else {
indent := indent.substringFrom 3
}
}
method formatModname(name) {
"gracecode_" ++ escapeident (basename(name))
}
method basename(filepath) {
var bnm := ""
for (filepath) do {c->
if (c == "/") then {
bnm := ""
} else {
bnm := bnm ++ c
}
}
bnm
}
method outerProp(node) { "outer_" ++ emod ++ "_" ++ node.line }
method noteLineNumber(n)comment(c) {
// remember the current line number, so that it can be generated if needed
if (n ≠ 0) then {
priorLineSeen := n
priorLineComment := c
}
}
method clearLineNumber(n) {
// clear and reset the remembered line number.
// Used at the start of a method
priorLineComment := "cleared on line {n}"
priorLineSeen := 0
priorLineEmitted := 0
}
method out(s) {
// output code, but first output code to set the line number
if (emitPositions && (priorLineSeen != priorLineEmitted)) then {
output.push "{indent}setLineNumber({priorLineSeen}); // {priorLineComment}"
priorLineEmitted := priorLineSeen
}
output.push(indent ++ s)
return done
}
method outUnnumbered(s) {
// output code that does not correspond to any source line
output.push(indent ++ s)
}
method emitBufferedOutput {
for (output) do { o ->
util.outprint(o)
}
outfile.close
}
def validIdChars = regex.fromString ‹[a-zA-Z0-9\$]›
method escapeident(vn) {
// escapes characters not legal in an identifer using __«codepoint»__
// must correspond to escapeident in the version of gracelib that will
// be loaded with the code that we are generating — which may be different
// from the version of gracelib that is supporting this execution of the
// compiler. (That's why we don't just call the gracelib version here.)
var result := ""
vn.do { ch ->
if (validIdChars.matches(ch)) then {
result := result ++ ch
} else {
result := result ++ "__{ch.ord}__"
}
}
result
}
method escapestring(s) {
// escapes embedded double-quotes, backslashes, newlines and non-ascii chars
native "js" code ‹return new GraceString(escapestring(var_s._value));›
}
method varf(vn) {
"var_" ++ escapeident(vn)
}
method uidWithPrefix(str) {
def myc = auto_count
auto_count := auto_count + 1
str ++ myc
}
/////////////////////////////////////////////////////////////
//
// Compilation methods for AST nodes
//
/////////////////////////////////////////////////////////////
method compilearray(o) {
def reg = uidWithPrefix "seq"
var vals := list []
for (o.value) do { a -> vals.push(compilenode(a)) }
out "var {reg} = new GraceSequence({literalList(vals)});"
o.register := reg
}
method compileobjouter(o, outerRef) is confidential {
def outerPropName = outerProp(o)
out "this.closureKeys = this.closureKeys || [];"
out "this.closureKeys.push(\"{outerPropName}\");"
out "this.{outerPropName} = {outerRef};"
}
method compileCheckThat(val) called (description)
hasType (expectedType) onLine(lineNumber) {
// Compiles a type check.
// expectedType is an astNode representing the type expression;
// val the register that holds the value to be type-checked;
// description is a String describing the nature of val, such as
// "argument 2 to `foobaz(_)with(_)`", which will be used in the error
// message, and lineNumber the line on which the error occurred (or 0
// if we don't want to emit a setLineNumber command).
if (emitTypeChecks.not) then { return }
if (false == expectedType) then { return }
if ("never returns" == val) then { return } // Charlie on the MTA?
if (expectedType.isUnknownType) then { return }
if ("Done" == expectedType.value) then { return }
def nm_t = compilenode(expectedType)
if (lineNumber ≠ 0) then {
noteLineNumber(lineNumber) comment "typecheck"
}
def typeDesc = withoutDollarPrefix(withoutTypeArgs(expectedType.toGrace 0.quoted))
out "assertTypeOrMsg({val}, {nm_t}, \"{description}\", \"{typeDesc}\");"
}
method withoutDollarPrefix(str) {
if (str.first == "$") then {
def dotIx = str.indexOf "." ifAbsent { 0 }
return str.substringFrom (dotIx + 1)
}
str
}
method withoutTypeArgs(str) {
def leftBracketIx = str.indexOf "⟦" ifAbsent { return str }
str.substringFrom 1 to (leftBracketIx - 1)
}
method compileobjdefdec(o, selfr) {
o.register := "GraceDone"
def val = if (o.isAnnotationDecl) then {
"annotation"
} else {
compilenode(o.value)
}
def oName = o.name.value
def nm = escapeident(oName)
compileCheckThat (val) called "value bound to {escapestring(oName)}"
hasType (o.dtype) onLine (o.line)
out "{selfr}.data.{nm} = {val};"
}
method compileobjvardec(o, selfr) {
o.register := "GraceDone"
def oName = o.name.value
def nm = escapeident(oName)
if (false == o.value) then {
out "{selfr}.data.{nm} = undefined;"
return
}
def val = compilenode(o.value)
compileCheckThat(val) called "value assigned to {escapestring(oName)}"
hasType (o.dtype) onLine (o.line)
out "{selfr}.data.{nm} = {val};"
}
method create (kind) field (o) in (objr) {
// compile code that creates a field, and appropriate
// accessor method(s), in objr, the object under construction
def nm = escapestring(o.name.value)
def nmi = escapeident(o.name.value)
def rFun = uidWithPrefix "reader" ++ "_" ++ nmi
def fieldName = if (o.parentKind == "module") then {
"var_{nmi}" // this var_{nmi} variable must be declared by caller
} else {
out "{objr}.data.{nmi} = undefined;"
"{objr}.data.{nmi}"
}
out "var {rFun} = function() \{ // reader method {nm}"
if (emitUndefinedChecks) then {
out " if ({fieldName} === undefined) raiseUninitializedVariable(\"{nm}\");"
}
out " return {fieldName};"
out "};"
out "{rFun}.is{kind.capitalized} = true;"
if (o.isReadable.not) then {
out "{rFun}.confidential = true;"
}
out "{objr}.methods[\"{nm}\"] = {rFun};"
if (kind == "var") then {
def wFun = uidWithPrefix "writer" ++ "_" ++ nmi
out "var {wFun} = function(argcv, n) \{ // writer method {nm}:=(_)"
increaseindent
compileCheckThat "n" called "argument to {nm}:=(_)"
hasType (o.dtype) onLine 0
out "{fieldName} = n;"
out "return GraceDone;"
decreaseindent
out "\};"
if (o.isWritable.not) then {
out "{wFun}.confidential = true;"
}
out "{objr}.methods[\"{nm}:=(1)\"] = {wFun};"
}
}
method installLocalAttributesOf(o) into (objr) {
var mutable := false
for (o.body) do { e ->
if (e.isMethod) then {
compilemethodnode(e) in (objr)
} elseif { e.kind == "vardec" } then {
create "var" field (e) in (objr)
mutable := true
} elseif { e.kind == "defdec" } then {
if (e.value.isNull.not) then {
create "def" field (e) in (objr)
}
} elseif { e.isTypeDec } then {
compiletypedec(e) in (objr)
}
}
if (mutable) then {
out "{objr}.mutable = true;"
}
}
method compileOwnInitialization(o, selfr) {
o.body.do { e ->
if (e.isFieldDec) then {
if (e.kind == "vardec") then {
compileobjvardec(e, selfr)
} elseif { e.kind == "defdec" } then {
compileobjdefdec(e, selfr)
}
} elseif { e.isObject } then {
compileobject(e, selfr)
} elseif { e.isExecutable } then {
compilenode(e)
}
}
}
method compileBuildAndInitFunctions(o) inMethod (methNode) {
// o is an objectNode. In the compiled code, `this` references the current
// object, which will become the outer object of `ouc`, the object here
// under construction. methNode respresents the method containing o;
// if o is not inside a method, methdNode is false.
// The build function adds the attributes defined by o to `this`,
// and returns as its result the init function, which, when called, will
// initialize `this`
var origInBlock := inBlock
inBlock := false
def ouc = uidWithPrefix "obj"
o.register := ouc
def inheritsStmt = o.superclass
var params := ""
var typeParams := ""
if (false != methNode) then {
params := paramlist(methNode)
typeParams := typeParamlist(methNode)
}
out "var {ouc}_build = function(ignore{params}, outerObj, aliases, exclusions{typeParams}) \{"
// At execution time, `this` will be the object under construction.
// `outerObj` will be the current object, which
// will become the `outer` of the object under construction.
// `aliases` and `exclusions` are JS arrays of aliases and method names,
// ultimately from an `inherit` or `use` statement.
increaseindent
compileobjouter(o, "outerObj")
out "const inheritedExclusions = \{ };"
// this object is used to save methods already in the ouc that
// would be overridden by local methods, aliases or excluded methods, were
// the excluded methods not excluded.
out "for (var eix = 0, eLen = exclusions.length; eix < eLen; eix ++) \{"
out " const exMeth = exclusions[eix];"
out " inheritedExclusions[exMeth] = this.methods[exMeth]; \};"
// some of these methods will be undefined; that's OK
if (false != inheritsStmt) then {
compileInherit(inheritsStmt) forClass (o.nameString)
}
o.usedTraits.do { t ->
compileUse(t) in (o)
}
installLocalAttributesOf(o) into "this"
out "const overridenByAliases = \{ };"
out "for (let aix = 0, aLen = aliases.length; aix < aLen; aix ++) \{"
out " const a = aliases[aix];"
out " const newNm = a.newName;"
out " const oldNm = a.oldName;"
out " overridenByAliases[newNm] = this.methods[newNm];"
out " const m = confidentialVersion(overridenByAliases[oldNm] || this.methods[oldNm], newNm);"
out " m.definitionLine = {o.line};"
out " m.definitionModule = {modNameAsString};"
out " this.methods[newNm] = m;"
out "}"
out "for (let exName in inheritedExclusions) \{"
out " if (inheritedExclusions.hasOwnProperty(exName)) \{"
out " if (inheritedExclusions[exName]) \{"
out " this.methods[exName] = inheritedExclusions[exName];"
out " } else \{"
out " delete this.methods[exName];"
out " }"
out " }"
out "}"
out "var {ouc}_init = function() \{ // init of object on line {o.line}"
// At execution time, `this` will be the object being initialized.
increaseindent
if (false != inheritsStmt) then {
compileSuperInitialization(inheritsStmt)
}
compileOwnInitialization(o, "this")
decreaseindent
out "\};" // end of _init function for object on line {o.line}
out "return {ouc}_init; // from compileBuildAndInitFunctions(_)inMethod(_)"
decreaseindent
out "\};" // end of build function
inBlock := origInBlock
}
method compileobject(o, outerRef) {
// compiles an object constructor, in all contexts except a fresh method.
// Generates two JavaScript functions,
// {o.register}_build, which creates the object and its methods and fields,
// and {o.register}_init, which initializes the fields. The _init function
// is returned from the _build funciton, so that it can close over the context.
// The object constructor itself is implemented by calling these functions
// in sequence, _except_ inside a fresh method, where the _build function may
// instead need to add the object's contents to an existing object.
compileBuildAndInitFunctions(o) inMethod (false)
def objRef = o.register
def objName = "\"" ++ o.name.quoted ++ "\""
out "var {objRef} = emptyGraceObject({objName}, {modNameAsString}, {o.line});"
out "var {objRef}_init = {objRef}_build.call({objRef}, null, {outerRef}, [], []);"
out "{objRef}_init.call({objRef}); // end of compileobject"
objRef
}
method compileGuard(o, paramList) {
if (o.aParametersHasATypeAnnotation) then {
def matchFun = uidWithPrefix "matches"
out "var {matchFun} = function({paramList}) \{"
increaseindent
noteLineNumber(o.line) comment "block matches function"
o.params.do { p ->
if (false ≠ p.dtype) then {
def pName = varf(p.value)
def dtype = compilenode(p.dtype)
out "if (!Grace_isTrue(request({dtype}, \"matches(1)\", [1], {pName})))"
out " return false;"
}
}
out "return true;"
decreaseindent
out "};"
matchFun
} else {
"jsTrue" // the constant true function, defined in gracelib
}
}
method compileblock(o) {
def origInBlock = inBlock
def oldInitializedVars = saveInitializedVars
inBlock := true
def blockId = uidWithPrefix "block"
def nParams = o.params.size
out "var {blockId} = new GraceBlock(this, {o.line}, {nParams});"
var paramList := ""
var paramTypes := list [ ]
var paramsAreTyped := false
var first := true
for (o.params) do { each ->
def dType = each.decType
paramTypes.push(compilenode(dType))
if (dType.isUnknownType.not) then {
paramsAreTyped := true
}
if (first) then {
paramList := varf(each.value)
first := false
} else {
paramList := paramList ++ ", " ++ varf(each.value)
}
}
if (paramsAreTyped) then {
out "{blockId}.paramTypes = {literalList(paramTypes)};"
}
out "{blockId}.guard = {compileGuard(o, paramList)};"
out "{blockId}.real = function {blockId}({paramList}) \{"
increaseindent
priorLineEmitted := 0
noteLineNumber (o.line) comment ("compileBlock")
var ret := "GraceDone"
for (o.body) do {l->
ret := compilenode(l)
}
if ("never returns" ≠ ret) then { out "return {ret};" }
decreaseindent
out "\};"
def applyMeth = blockId.replace "block" with "applyMeth"
def applyMethName = if (nParams == 0) then { "apply" } else { "apply({nParams})" }
if (nParams == 0) then {
out "let {applyMeth} = function apply (argcv) \{"
out " return this.real.apply(this.receiver);"
out "\};"
} else {
out "let {applyMeth} = function apply_{nParams} (argcv, ...args) \{"
out " if (this.guard.apply(this.receiver, args))"
out " return this.real.apply(this.receiver, args);"
out " badBlockArgs.apply(this, args);"
out "\};"
}
compileMetadata(o, applyMeth, applyMethName)
out "{blockId}.methods[\"{applyMethName}\"] = {applyMeth};"
def matchesMeth = applyMeth.replace "applyMeth" with "matchesMeth"
def matchesMethName = "matches({nParams})"
if (nParams > 0) then {
out "let {matchesMeth} = function matches_{nParams} (argcv, ...args) \{"
out " return this.guard.apply(this.receiver, args) ? GraceTrue : GraceFalse;"
out "\};"
compileMetadata(o, matchesMeth, matchesMethName)
out "{blockId}.methods[\"{matchesMethName}\"] = {matchesMeth};"
}
o.register := blockId
restoreInitializedVars(oldInitializedVars)
inBlock := origInBlock
}
method compiletypedec(o) in (obj) {
// We compile type declarations as once methods. This allows the declarations
// to appear in any order. Compiling them as defs won't work for types
// with type parameters, or for types that are used before they are declared
// (as must happen for mutually-recursive types)
def s = o.scope
def originalNode = s.node
var reg := uidWithPrefix "type"
def tName = o.nameString
out "// Type decl {tName}"
// declaredvars.push(escapeident(tName))
def rhs = o.value
if (rhs.isInterface) then { rhs.name := tName }
def typeMethod = ast.methodDec(
[ast.signaturePart(o.nameString) params [].setScope(s)],
typeFunBody (rhs) named (tName), ast.unknownLiteral, []).setScope(s)
// Why do we make the type Unknown, rather than Type? Because the
// latter will compile a check that the return value is indeed a type,
// which causes a circularity when trying to import collections. The
// check is subsumed by a call to `isType` in the body of `setTypeName`
typeMethod.isOnceMethod := true
typeMethod.withTypeParams(o.typeParams)
s.node := originalNode // gct generation will scan the ast after code is generated
def typeFun = compilenode(typeMethod)
o.register := reg
reg
}
method typeFunBody(typeExpr) named (tName) {
if (typeExpr.kind == "op") then {
// this guard prevents us from renaming the rhs in decls like type A⟦T⟧ = B⟦T⟧
[ ast.request(
typeExpr,
[ast.requestPart "setTypeName"
withArgs[ ast.stringLiteral(tName) ] ]
).onSelf ]
} else {
[ typeExpr ]
}
}
method compiletypeliteral(o) in (obj) {
def reg = uidWithPrefix "typeLit"
def escName = escapestring(o.name)
out "// Type literal "
out "var {reg} = new GraceType(\"{escName}\");"
if (o.methods.anySatisfy { each -> each.kind == "ellipsis" }) then {
out "Object.defineProperty({reg}, 'typeMethods', \{ get: ellipsisFun \});"
} else {
out "{reg}.typeMethods = {stringList(o.methods.map { meth -> meth.nameString })};"
}
o.register := reg
reg
}
method paramNames(o) {
def result = list [ ]
o.signatureParts.do { part ->
part.params.do { param ->
result.push(param.nameString)
}
}
result
}
method typeParamNames(o) {
if (false == o.typeParams) then { return list [ ] }
def result = list [ ]
o.typeParams.do { each ->
result.push(each.nameString)
}
result
}
method hasTypedParams(o) {
for (o.signatureParts) do { part ->
for (part.params) do { p->
if (p.dtype != false) then {
if ((p.dtype.isIdentifier) || (p.dtype.isInterface)) then {
return true
}
}
}
}
return false
}
method compileMethodPreamble(o, funcName, name) withParams (p) {
out "var {funcName} = function(argcv{p}) \{ // method {name}, line {o.line}"
increaseindent
out "var returnTarget = invocationCount;"
out "invocationCount++;"
}
method compileMethodPostamble(o, funcName, name) {
decreaseindent
out "\}; // end of method {name}"
if (hasTypedParams(o)) then {
compilemethodtypes(funcName, o)
}
if (o.isConfidential) then {
out "{funcName}.confidential = true;"
}
}
method compileParameterDebugFrame(o, name) {
if (debugMode) then {
out "var myframe = new StackFrame(\"{name}\");"
for (o.signatureParts) do { part ->
for (part.params) do { p ->
def pName = p.nameString
def varName = varf(pName)
out "myframe.addVar(\"{escapestring(pName)}\","
out " function() \{return {varName};});"
}
}
}
}
method compileDefaultsForTypeParameters(o) extraParams (extra) {
def tps = o.typeParams
if (false ≠ tps) then {
o.typeParams.do { g->
def gName = varf(g.value)
out "if (! {gName}) {gName} = type_Unknown;"
}
}
if (emitArgChecks) then {
def correction = if (extra == 0) then { "1" } else { "{extra + 1}" }
// 1 for argcv, and extra for the arguments to $build methods
out "const numArgs = arguments.length - {correction};"
def np = o.numParams
def ntp = o.numTypeParams
def s = if (ntp == 1) then { "" } else { "s" }
out "if ((numArgs > {np}) && (numArgs !== {np + ntp})) \{"
out " raiseTypeArgError(\"{o.canonicalName}\", {ntp}, numArgs - {np});"
out "\}"
}
}
method compileArgumentTypeChecks(o) {
if ( emitTypeChecks.not ) then { return }
if (o.numParams == 1) then {
def p = o.signatureParts.first.params.first
def pName = p.value
compileCheckThat (varf(pName))
called "argument to request of `{o.canonicalName}`"
hasType (p.dtype) onLine 0
} else {
var paramnr := 0
o.parametersDo { p ->
paramnr := paramnr + 1
def pName = p.value
compileCheckThat (varf(pName))
called "argument {paramnr} in request of `{o.canonicalName}`"
hasType (p.dtype) onLine 0
// the line number is 0, because we want the error message to
// refer to the call site, not the method definition.
}
}
}
method debugModePrefix {
if (debugMode) then {
out "stackFrames.push(myframe);"
out("try \{")
increaseindent
}
}
method debugModeSuffix {
if (debugMode) then {
decreaseindent
out "\} finally \{"
out " stackFrames.pop();"
out "\}"
}
}
method compileMethodBodyWithTypecheck(o) {
def ret = compileMethodBody(o)
def ln = if (o.body.isEmpty) then { o.end.line } else { o.resultExpression.line }
compileCheckThat(ret) called "result of method {o.canonicalName}"
hasType (o.decType) onLine (ln)
ret
}
method compileFreshMethod(o, outerRef) {
// compiles the methodNode o in a way that can be used with an `inherit`
// statement. _Two_ methods are generated: one to build the new object,
// and one to initialize it. The build method will also implement
// the statements in the body of this method that preceed the final
// (result) expression.
// The final (result) expression of method o may be of two kinds:
// (1) an object constructor,
// (2) a request on another fresh method (which will return its init function).
def resultExpr = o.resultExpression
if (resultExpr.isObject) then { // case (1)
compileBuildMethodFor(o) withObjCon (resultExpr) inside (outerRef)
} else { // case (2)
compileBuildMethodFor(o) withFreshCall (resultExpr) inside (outerRef)
}
return "GraceDone"
}
method compileMethodBody(methNode) {
// compiles the body of method represented by methNode.
// answers the register containing the result.
var ret := "GraceDone"
methNode.body.do { nd -> ret := compilenode(nd) }
ret
}
method compileMethodBodyWithoutLast(methNode) {
def body = methNode.body
if (body.size > 1) then {
def resultExpr = body.removeLast // remove result object
compileMethodBody(methNode)
body.addLast(resultExpr) // put result object back
}
}
method stringList(strs) {
// answers the contents of the collection strs of strings quoted
// and between brackets.
var res := "["
strs.do { s -> res := res ++ "\"" ++ s.quoted ++ "\""}
separatedBy { res := res ++ ", " }
res ++ "]"
}
method literalList(lits) {
// answers the contents of the collection lits between brackets.
var res := "["
lits.do { n -> res := res ++ n.asString }
separatedBy { res := res ++ ", " }
res ++ "]"
}
method compileMetadata(o, funcName, name) {
out "{funcName}.methodName = \"{name}\";"
out "{funcName}.paramCounts = {literalList(o.parameterCounts)};"
out "{funcName}.paramNames = {stringList(o.parameterNames)};"
if (o.hasTypeParams) then {
out "{funcName}.typeParamNames = {stringList(o.typeParameterNames)};"
}
out "{funcName}.definitionLine = {o.line};"
out "{funcName}.definitionModule = {modNameAsString};"
}
method compilemethodnode(o) in (objref) {
def oldusedvars = usedvars
def olddeclaredvars = declaredvars
def oldInitializedVars = saveInitializedVars
clearLineNumber(o.line)
o.register := uidWithPrefix "func"
if (isSimpleAccessor(o)) then {
compileSimpleAccessor(o)
} elseif { o.isAbstract } then {
compileDummyMethod(o, objref, "abstract")
} elseif { o.isRequired } then {
compileDummyMethod(o, objref, "required")
} elseif { o.isAnnotationDecl } then {
compileDummyAnnotationMethod(o, objref)
} else {
compileNormalMethod(o, objref)
}
usedvars := oldusedvars
declaredvars := olddeclaredvars
restoreInitializedVars(oldInitializedVars)
}
method isSimpleAccessor(o) {
if (o.body.size ≠ 1) then { return false }
if (o.isOnceMethod) then { return false }
if (o.hasTypeParams) then { return false }
if (o.hasParams) then { return false }
if (o.decType.isUnknownType.not) then { return false }
// to ensure that we compile a type check for the method's return type
o.body.first.isIdentifier
}
method compileSimpleAccessor(o) {
def oldEmitPositions = emitPositions
emitPositions := false
def canonicalMethName = o.canonicalName
def funcName = o.register
def name = escapestring(o.nameString)
def ident = o.body.first
def p = paramlist(o) ++ typeParamlist(o)
out "var {funcName} = function(argcv{p}) \{ // accessor method {canonicalMethName}, line {o.line}"
increaseindent
if (emitArgChecks) then {
out "const numArgs = arguments.length - 1;"
def np = o.numParams
out "if (numArgs > {np}) raiseTypeArgError(\"{canonicalMethName}\", 0, numArgs - {np});"
}
out "return {compilenode(ident)};"
compileMethodPostamble(o, funcName, canonicalMethName)
out "this.methods[\"{name}\"] = {funcName};"
compileMetadata(o, funcName, name)
emitPositions := oldEmitPositions
}
method compileNormalMethod(o, selfobj) {
def canonicalMethName = o.canonicalName
def funcName = o.register
priorLineEmitted := 0
usedvars := list []
declaredvars := list []
def name = escapestring(o.nameString)
compileMethodPreamble (o, funcName, canonicalMethName)
withParams (paramlist(o) ++ typeParamlist(o))
compileParameterDebugFrame(o, name)
if (o.isOnceMethod.not) then {
// it o is a once method, we compile the defaults in the wrapper
compileDefaultsForTypeParameters(o) extraParams 0
}
compileArgumentTypeChecks(o)
debugModePrefix
if (o.isFresh) then {
def argList = paramlist(o)
def typeArgList = typeParamlist(o)
out "var ouc = emptyGraceObject(\"{o.ilkName}\", {modNameAsString}, {o.line});"
out "var ouc_init = {selfobj}.methods[\"{name}$build(3)\"].call(this, null{argList}, ouc, [], []{typeArgList});"
out "ouc_init.call(ouc);"
compileCheckThat "ouc" called "object returned from {o.canonicalName}"
hasType (o.dtype) onLine (o.end.line);
out "return ouc;"
} else {
def result = compileMethodBodyWithTypecheck(o)
if ("never returns" ≠ result) then {
out "return {result};"
}
}
debugModeSuffix
compileMethodPostamble(o, funcName, canonicalMethName)
if (o.isOnceMethod) then {
compileOnceWrapper(o, selfobj, name)
} else {
out "{selfobj}.methods[\"{name}\"] = {funcName};"
}
compileMetadata(o, funcName, name)
if (o.isFresh) then {
compileFreshMethod(o, selfobj)
}
}
method compileOnceWrapper(o, selfobj, name) {
def totalParams = o.numParams + o.numTypeParams
def funcName = o.register
def memoFuncName = "memo" ++ funcName
if ( totalParams == 0 ) then {
out "function {memoFuncName}(argcv) \{"
out " if (! this.data[\"memo${name}\"]) // parameterless memo function"
out " this.data[\"memo${name}\"] = {funcName}.call(this, argcv);"
out " return this.data[\"memo${name}\"];"
out "\};"
} else {
def commaParamNames = paramlist(o) ++ typeParamlist(o);
def paramNames = commaParamNames.substringFrom 3
out "function {memoFuncName}(argcv{commaParamNames}) \{"
increaseindent
compileDefaultsForTypeParameters(o) extraParams 0
out "let memoTable = this.data[\"memo${name}\"] ||"
out " ( this.data[\"memo${name}\"] ="
out " request(var_HashMap, 'empty', [0]) );"
if (totalParams == 1) then {
out "let tableKey = {paramNames};"
} else {
out "let tableKey = new GraceSequence([{paramNames}]);"
}
out "let absentBlock = new GraceBlock(this, {o.line}, 0);"
out "absentBlock.guard = jsTrue;"
out "absentBlock.real = function ifAbsentBlock() \{"
out " let newResult = {funcName}.call(this, argcv{commaParamNames});"
out " request(memoTable, 'at(1)put(1)', [1,1], tableKey, newResult);"
out " return newResult;"
out "\};"
out "absentBlock.methods.apply = function apply (argcv) \{"
out " return this.real.apply(this.receiver);"
out "\};"
out "return request(memoTable, 'at(1)ifAbsent(1)', [1,1], tableKey, absentBlock);"
decreaseindent
out "\};"
}
out "{selfobj}.methods[\"{name}\"] = {memoFuncName};"
compileMetadata(o, memoFuncName, name)
}
method compileDummyMethod(o, selfobj, kind) {
def canonicalMethName = o.canonicalName
def funcName = o.register
priorLineEmitted := 0
def name = escapestring(o.nameString)
out "if (! {selfobj}.methods[\"{name}\"]) \{"
increaseindent
compileMethodPreamble (o, funcName, canonicalMethName)
withParams (paramlist(o) ++ typeParamlist(o))
compileParameterDebugFrame(o, name)
compileDefaultsForTypeParameters(o) extraParams 0
compileArgumentTypeChecks(o)
noteLineNumber (o.line) comment "{kind} method"
debugModePrefix
out "throw new GraceExceptionPacket(ProgrammingErrorObject,"
out " new GraceString(\"{kind} method {canonicalMethName} \" +"
out " \"was not supplied for \" + safeJsString(this)));"
debugModeSuffix
compileMethodPostamble(o, funcName, canonicalMethName)
out "{selfobj}.methods[\"{name}\"] = {funcName};"
compileMetadata(o, funcName, name)
decreaseindent
out "\};"
}
method compileDummyAnnotationMethod(o, selfobj) {
def canonicalMethName = o.canonicalName
def funcName = o.register
priorLineEmitted := 0
def name = escapestring(o.nameString)
compileMethodPreamble (o, funcName, canonicalMethName)
withParams (paramlist(o) ++ typeParamlist(o))
compileParameterDebugFrame(o, name)
noteLineNumber (o.line) comment "annotation method"
debugModePrefix
out "raiseException(ProgrammingErrorObject,"
out " \"{canonicalMethName} is an annotation; it cannot be requested as a method\");"
debugModeSuffix
compileMethodPostamble(o, funcName, canonicalMethName)
out "{selfobj}.methods[\"{name}\"] = {funcName};"
compileMetadata(o, funcName, name)
}
method compileBuildMethodFor(methNode) withObjCon (objNode) inside (outerRef) {
// the $build method for a fresh method executes the statements in the
// body of the fresh method, and then calls the build function of the
// object constructor. That build function will return the _init function
// for the object expression that it tail-returns.
def funcName = uidWithPrefix "func"
def name = escapestring(methNode.nameString ++ "$build(3)")
def cName = methNode.canonicalName ++ "$build(_,_,_)"
def params = paramlist(methNode)
def typeParams = typeParamlist(methNode)
compileMethodPreamble (methNode, funcName, cName)
withParams (params ++ ", inheritingObject, aliases, exclusions" ++ typeParams)
compileDefaultsForTypeParameters(methNode) extraParams 3
compileArgumentTypeChecks(methNode)
compileMethodBodyWithoutLast(methNode)
compileBuildAndInitFunctions(objNode) inMethod (methNode)
def objRef = objNode.register
out "var {objRef}_init = {objRef}_build.call(inheritingObject, null{params}, {outerRef}, aliases, exclusions{typeParams});"
out "return {objRef}_init; // from compileBuildMethodFor(_)withObjCon(_)inside(_)"
compileMethodPostamble(methNode, funcName, cName)
out "this.methods[\"{name}\"] = {funcName};"
compileMetadata(methNode, funcName, name)
}
method compileBuildMethodFor(methNode) withFreshCall (callExpr) inside (outerRef) {
// The build method will have three additional parameters:
// `inheritingObject`, `aliases`, and `exclusions`. These
// will be passed to it by the `inherit` statement.
def funcName = uidWithPrefix "func"
def name = escapestring(methNode.nameString ++ "$build(3)")
def cName = escapestring(methNode.canonicalName ++ "$build(_,_,_)")
compileMethodPreamble(methNode, funcName, cName)
withParams( paramlist(methNode) ++ ", ouc, aliases, exclusions")
compileMethodBodyWithoutLast(methNode)
// Now compile the call, with the three aditional arguments.
// TODO: refactor compilecall so that it can handle this case, as well as
// normal calls.
def calltemp = uidWithPrefix "call"
callExpr.register := calltemp
var args := list []
compileNormalArguments(callExpr, args)
args.addAll ["ouc", "aliases", "exclusions"]
compileTypeArguments(callExpr, args)
compileCallToBuildMethod(callExpr) withArgs (args)
compileCheckThat "ouc" called "result of method {methNode.canonicalName}"
hasType (methNode.dtype) onLine (callExpr.line)
out "return {calltemp}; // from compileBuildMethodFor(_)withFreshCall(_)inside(_)"
compileMethodPostamble(methNode, funcName, cName)
out "this.methods[\"{name}\"] = {funcName};"
compileMetadata(methNode, funcName, name)
}
method compileCallToBuildMethod(callExpr) withArgs (args) {
util.setPosition(callExpr.line, callExpr.column)
callExpr.parts.addLast(
ast.requestPart "$build"
withArgs [ast.nullNode, ast.nullNode, ast.nullNode]