/
AstBuilder.ooc
1216 lines (983 loc) · 39.8 KB
/
AstBuilder.ooc
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
import io/File, text/[EscapeSequence]
import structs/[ArrayList, List, Stack, HashMap]
import ../utils/FileUtils
import ../frontend/[Token, BuildParams]
import ../middle/tinker/Errors
import ../middle/[FunctionDecl, VariableDecl, TypeDecl, ClassDecl, CoverDecl,
FunctionCall, StringLiteral, Node, Module, Statement, Include, Import,
Type, TypeList, Expression, Return, VariableAccess, Cast, If, Else,
ControlStatement, Comparison, IntLiteral, FloatLiteral, Ternary,
BinaryOp, BoolLiteral, NullLiteral, Argument, Parenthesis, AddressOf,
Dereference, Foreach, OperatorDecl, RangeLiteral, UnaryOp, ArrayAccess,
Match, FlowControl, While, CharLiteral, InterfaceDecl, NamespaceDecl,
Version, Use, Block, ArrayLiteral, EnumDecl, BaseType, FuncType,
Declaration, PropertyDecl, CallChain, Tuple, Addon]
nq_parse: extern proto func (AstBuilder, CString) -> Int
// reserved C99 keywords
reservedWords := ["auto", "int", "long", "char", "register", "short", "do",
"sizeof", "double", "struct", "switch", "typedef", "union",
"unsigned", "signed", "goto", "enum", "const"]
reservedHashs := computeReservedHashs(reservedWords)
ReservedKeywordError: class extends Error {
init: super func ~tokenMessage
}
computeReservedHashs: func (words: String[]) -> ArrayList<Int> {
list := ArrayList<Int> new()
for(i in 0..words length) {
word := words[i]
list add(ac_X31_hash(word))
}
list
}
AstBuilder: class {
cache := static HashMap<String, Module> new()
langImports : List<String>
params: BuildParams
modulePath: String
module: Module
stack: Stack<Object>
versionStack: Stack<VersionSpec>
tokenPos : Int*
init: func (=modulePath, =module, =params) {
first := static true
clearline := " \r"
if(params verbose) {
if(!first) clearline print()
"Parsing " print()
modulePath print()
}
cache put(File new(modulePath) getAbsolutePath(), module)
stack = Stack<Object> new()
stack push(module)
versionStack = Stack<VersionSpec> new()
if(params includeLang && !module fullName startsWith?("/")) {
addLangImports()
}
first = false
result := nq_parse(this, modulePath toCString())
if(result == -1) {
Exception new(This, "File " +modulePath + " not found") throw()
}
}
addLangImports: func {
langImports : static List<String> = null
if(!langImports) {
langImports = ArrayList<String> new()
paths := params sourcePath getRelativePaths("lang")
paths filterEach(|p| p endsWith?(".ooc"),
|p|
impName := p substring(0, p length() - 4) replaceAll(File separator, '/')
langImports add(impName)
)
}
langImports filterEach(|impName| impName != module fullName,
|impName| module addImport(Import new(impName, module token))
)
}
/**
* Turn import paths like "../frontend/AstBuilder" into "/opt/ooc/rock/source/rock/frontend/AstBuilder"
*/
getRealImportPath: static func (imp: Import, module: Module, params: BuildParams, path: String@, impPath, impElement: File@) -> File {
path = FileUtils resolveRedundancies(imp path + ".ooc")
impElement = params sourcePath getElement(path)
impPath = params sourcePath getFile(path)
if(impPath == null) {
parent := File new(module getPath()) parent()
if(parent != null) {
path = FileUtils resolveRedundancies(parent path + File separator + imp path + ".ooc")
impElement = params sourcePath getElement(path)
impPath = params sourcePath getFile(path)
}
}
return impPath
}
printCache: static func {
"==== Cache ====" println()
cache getKeys() each(|key|
"cache %s = %s" format(key toCString(), cache get(key) fullName toCString()) println()
)
"=" times(14) println()
}
error: func (errorID: Int, message: String, index: Int) {
params errorHandler onError(SyntaxError new(Token new(index, 1, module), message))
}
onUse: unmangled(nq_onUse) func (name: CString) {
module addUse(Use new(name toString(), params, token()))
}
onInclude: unmangled(nq_onInclude) func (cpath: CString) {
path := cpath toString()
mode: IncludeMode
if(path startsWith?("./")) {
mode = IncludeModes LOCAL
path = path substring(2) // remove ./ from path
}
else {
mode = IncludeModes PATHY
}
inc := Include new(path, mode)
module addInclude(inc)
inc setVersion(getVersion())
}
onIncludeDefine: unmangled(nq_onIncludeDefine) func (name, value: CString) {
module includes last() addDefine(Define new(name toString(), value toString()))
}
onImport: unmangled(nq_onImport) func (path, name: CString) {
namestr := name toString()
output : String = ((path == null) || (path@ == '\0')) ? namestr : path toString() + namestr
module addImport(Import new( output , token()))
}
onImportNamespace: unmangled(nq_onImportNamespace) func (cnamespace: CString, quantity: Int) {
if(params veryVerbose) printf("nq_importnamespace %s\n", cnamespace)
namespace := cnamespace toString()
nDecl: NamespaceDecl
if(!module hasNamespace(namespace)) {
nDecl = NamespaceDecl new(namespace)
module addNamespace(nDecl)
} else {
nDecl = module getNamespace(namespace)
}
peek(Module) // ensure we're a at root level
quantity times(||
nDecl addImport(module getGlobalImports() last())
module getGlobalImports() removeAt(module getGlobalImports() lastIndex()) // no longer a global import
)
}
/*
* Addons
*/
onExtendStart: unmangled(nq_onExtendStart) func (baseType: Type, doc: CString) {
addon := Addon new(baseType, token())
addon doc = doc toString()
stack push(addon)
}
onExtendEnd: unmangled(nq_onExtendEnd) func -> Addon {
module addAddon(pop(Addon))
}
/*
* Covers
*/
onCoverStart: unmangled(nq_onCoverStart) func (name, doc: CString) {
cDecl := CoverDecl new(name toString(), token())
cDecl setVersion(getVersion())
cDecl doc = doc toString()
cDecl module = module
module addType(cDecl)
stack push(cDecl)
}
onCoverExtern: unmangled(nq_onCoverExtern) func (externName: CString) {
peek(CoverDecl) setExternName(externName toString())
}
onCoverFromType: unmangled(nq_onCoverFromType) func (type: Type) {
peek(CoverDecl) setFromType(type)
}
onCoverExtends: unmangled(nq_onCoverExtends) func (superType: Type) {
peek(CoverDecl) setSuperType(superType)
}
onCoverImplements: unmangled(nq_onCoverImplements) func (interfaceType: Type) {
peek(CoverDecl) addInterface(interfaceType)
}
onCoverEnd: unmangled(nq_onCoverEnd) func {
pop(CoverDecl)
}
/*
* Enums
*/
onEnumStart: unmangled(nq_onEnumStart) func (name, doc: CString) {
eDecl := EnumDecl new(name toString(), token())
eDecl setVersion(getVersion())
eDecl module = module
eDecl doc = doc toString()
module addType(eDecl)
stack push(eDecl)
}
onEnumExtern: unmangled(nq_onEnumExtern) func (externName: CString) {
peek(EnumDecl) setExternName(externName toString())
}
onEnumFromType: unmangled(nq_onEnumFromType) func (fromType: Type) {
peek(EnumDecl) setFromType(fromType)
}
onEnumIncrementExpr: unmangled(nq_onEnumIncrementExpr) func (oper: Char, step: IntLiteral) {
peek(EnumDecl) setIncrement(oper, step value)
}
onEnumElementStart: unmangled(nq_onEnumElementStart) func (name, doc: CString) {
element := EnumElement new(peek(EnumDecl) getInstanceType(),name toString(), token())
element doc = doc toString()
stack push(element)
}
onEnumElementValue: unmangled(nq_onEnumElementValue) func (value: Expression) {
peek(EnumElement) setValue(value)
}
onEnumElementExtern: unmangled(nq_onEnumElementExtern) func (externName: CString) {
peek(EnumElement) setExternName(externName toString())
}
onEnumElementEnd: unmangled(nq_onEnumElementEnd) func {
element := pop(EnumElement)
peek(EnumDecl) addElement(element)
}
onEnumEnd: unmangled(nq_onEnumEnd) func {
pop(EnumDecl)
}
/*
* Classes
*/
onClassStart: unmangled(nq_onClassStart) func (name, doc: CString) {
cDecl := ClassDecl new(name toString(), token())
cDecl setVersion(getVersion())
cDecl doc = doc toString()
cDecl module = module
module addType(cDecl)
stack push(cDecl)
}
onClassExtends: unmangled(nq_onClassExtends) func (superType: Type) {
peek(ClassDecl) setSuperType(superType)
}
onClassImplements: unmangled(nq_onClassImplements) func (interfaceType: Type) {
peek(ClassDecl) addInterface(interfaceType)
}
onClassAbstract: unmangled(nq_onClassAbstract) func {
peek(ClassDecl) isAbstract = true
}
onClassFinal: unmangled(nq_onClassFinal) func {
peek(ClassDecl) isFinal = true
}
onClassBody: unmangled(nq_onClassBody) func {
peek(ClassDecl) addDefaultInit() }
onClassEnd: unmangled(nq_onClassEnd) func {
pop(ClassDecl)
}
/*
* Version blocks
*/
onVersionName: unmangled(nq_onVersionName) func (name: CString) -> VersionSpec {
VersionName new(name toString(), token())
}
onVersionNegation: unmangled(nq_onVersionNegation) func (spec: VersionSpec) -> VersionSpec {
VersionNegation new(spec, token())
}
onVersionAnd: unmangled(nq_onVersionAnd) func (specLeft, specRight: VersionSpec) -> VersionSpec {
VersionAnd new(specLeft, specRight, token())
}
onVersionOr: unmangled(nq_onVersionOr) func (specLeft, specRight: VersionSpec) -> VersionSpec {
VersionOr new(specLeft, specRight, token())
}
onVersionStart: unmangled(nq_onVersionStart) func (spec: VersionSpec) {
object := peek(Object)
if(object instanceOf?(Module)) {
versionStack push(spec)
} else {
vb := VersionBlock new(spec, token())
stack push(vb)
}
}
onVersionEnd: unmangled(nq_onVersionEnd) func -> VersionBlock {
object := peek(Object)
if(object instanceOf?(Module)) {
versionStack pop()
} else {
vb := pop(VersionBlock)
return vb
}
return null
}
/*
* Interfaces
*/
onInterfaceStart: unmangled(nq_onInterfaceStart) func (name, doc: CString) {
iDecl := InterfaceDecl new(name toString(), token())
iDecl setVersion(getVersion())
iDecl doc = doc toString()
iDecl module = module
module addType(iDecl)
stack push(iDecl)
}
onInterfaceExtends: unmangled(nq_onInterfaceExtends) func (superType: Type) {
peek(InterfaceDecl) setSuperType(superType)
}
onInterfaceEnd: unmangled(nq_onInterfaceEnd) func {
pop(InterfaceDecl)
}
onTypeAccess: unmangled(nq_onTypeAccess) func (t: Type) -> TypeAccess {
TypeAccess new(t, t token)
}
/*
* Variable declarations
*/
onVarDeclStart: unmangled(nq_onVarDeclStart) func {
stack push(Stack<VariableDecl> new())
}
onVarDeclName: unmangled(nq_onVarDeclName) func (name, doc: CString) {
vDecl := VariableDecl new(null, name toString(), token())
vDecl doc = doc toString()
peek(Stack<VariableDecl>) push(vDecl)
}
onVarDeclTuple: unmangled(nq_onVarDeclTuple) func (tuple: Tuple) {
peek(Stack<VariableDecl>) push(VariableDeclTuple new(null as Type, tuple, token()))
}
onVarDeclExtern: unmangled(nq_onVarDeclExtern) func (cexternName: CString) {
externName := cexternName toString()
vars := peek(Stack<VariableDecl>)
if(externName empty?()) {
vars each(|var| var setExternName(""))
} else {
if(vars size() != 1) {
params errorHandler onError(SyntaxError new(token(), "Trying to set an extern name on several variables at once!"))
}
vars peek() setExternName(externName)
}
}
onVarDeclUnmangled: unmangled(nq_onVarDeclUnmangled) func (cunmangledName: CString) {
unmangledName :=cunmangledName toString()
vars := peek(Stack<VariableDecl>)
if(unmangledName empty?()) {
vars each(|var| var setUnmangledName(""))
} else {
if(vars size() != 1) {
params errorHandler onError(SyntaxError new(token(), "Trying to set an unmangled name on several variables at once!"))
}
vars peek() setUnmangledName(unmangledName)
}
}
onVarDeclExpr: unmangled(nq_onVarDeclExpr) func (expr: Expression) {
peek(Stack<VariableDecl>) peek() setExpr(expr)
}
onVarDeclStatic: unmangled(nq_onVarDeclStatic) func {
peek(Stack<VariableDecl>) each(|vd| vd setStatic(true))
}
onVarDeclProto: unmangled(nq_onVarDeclProto) func {
peek(Stack<VariableDecl>) each(|vd| vd setProto(true))
}
onVarDeclConst: unmangled(nq_onVarDeclConst) func {
peek(Stack<VariableDecl>) each(|vd| vd setConst(true))
}
onVarDeclType: unmangled(nq_onVarDeclType) func (type: Type) {
peek(Stack<VariableDecl>) each(|vd| vd type = type)
}
onVarDeclEnd: unmangled(nq_onVarDeclEnd) func -> Object {
stack := pop(Stack<VariableDecl>)
if(stack size() == 1) return stack peek() as Object
// FIXME: Better detection to avoid 'stack' being passed as a Statement to, say, an If
return stack as Object
}
gotVarDecl: func (vd: VariableDecl) {
hash := ac_X31_hash(vd getName())
idx := reservedHashs indexOf(hash)
if(idx != -1) {
// same hash? compare length and then full-string comparison
word := reservedWords[idx]
if(word length() == vd getName() length() && word == vd getName()) {
params errorHandler onError(ReservedKeywordError new(vd token, "%s is a reserved C99 keyword, you can't use it in a variable declaration" format(vd getName() toCString())))
}
}
node : Node = stack peek()
//printf("[gotVarDecl] Got variable decl %s, and parent is a %s\n", vd toString(), node class name)
if(node instanceOf?(TypeDecl)) {
tDecl := node as TypeDecl
tDecl addVariable(vd)
} else if(node instanceOf?(List)) {
vd isArg = true
node as List<Node> add(vd)
} else {
//printf("[gotVarDecl] Parent is a %s, don't know what to do, calling gotStatement()\n", node class name)
gotStatement(vd)
}
}
/*
* Properties
*/
onPropertyDeclStart: unmangled(nq_onPropertyDeclStart) func (name: CString) {
stack push(PropertyDecl new(null, name toString(), token()))
}
onPropertyDeclStatic: unmangled(nq_onPropertyDeclStatic) func {
peek(PropertyDecl) setStatic(true)
}
onPropertyDeclType: unmangled(nq_onPropertyDeclType) func (type: Type) {
peek(PropertyDecl) type = type
}
onPropertyDeclGetterStart: unmangled(nq_onPropertyDeclGetterStart) func {
getter := FunctionDecl new("", token())
stack push(getter)
}
onPropertyDeclGetterEnd: unmangled(nq_onPropertyDeclGetterEnd) func {
getter := pop(FunctionDecl)
// getter has 0 statements and isn't extern? use default getter
if(getter body size() == 0 && !getter isExtern()) {
peek(PropertyDecl) setDefaultGetter()
} else {
peek(PropertyDecl) setGetter(getter)
}
}
onPropertyDeclSetterStart: unmangled(nq_onPropertyDeclSetterStart) func {
setter := FunctionDecl new("", token())
stack push(setter)
}
onPropertyDeclSetterArgument: unmangled(nq_onPropertyDeclSetterArgument) func (cname: CString, conventional: Bool) {
name := cname toString()
arg: Argument = match conventional {
case true => Argument new(null, name clone(), token())
case false => AssArg new(name clone(), token())
}
peek(FunctionDecl) args add(arg)
}
onPropertyDeclSetterEnd: unmangled(nq_onPropertyDeclSetterEnd) func {
setter := pop(FunctionDecl)
// setter has 0 statements and isn't extern? use default setter
if(setter body size() == 0 && !setter isExtern()) {
peek(PropertyDecl) setDefaultSetter()
} else {
peek(PropertyDecl) setSetter(setter)
}
}
onPropertyDeclEnd: unmangled(nq_onPropertyDeclEnd) func -> PropertyDecl {
decl := pop(PropertyDecl)
node := peek(TypeDecl)
node addVariable(decl)
decl
}
/*
* Types
*/
onTypeNew: unmangled(nq_onTypeNew) func (name: CString) -> Type {
BaseType new(name toString() trim(), token())
}
onTypePointer: unmangled(nq_onTypePointer) func (type: Type) -> Type {
PointerType new(type, token())
}
onTypeReference: unmangled(nq_onTypeReference) func (type: Type) -> Type {
ReferenceType new(type, token())
}
onTypeBrackets: unmangled(nq_onTypeBrackets) func (type: Type, inner: Expression) -> Type {
ArrayType new(type, inner, token())
}
onTypeGenericArgument: unmangled(nq_onTypeGenericArgument) func (type: Type, typeInner: Type) {
type addTypeArg(VariableAccess new(typeInner, token()))
}
onTypeList: unmangled(nq_onTypeList) func -> TypeList {
TypeList new(token())
}
onTypeListElement: unmangled(nq_onTypeListElement) func (list: TypeList, element: Type) {
list types add(element)
}
/*
* Function types
*/
onFuncTypeNew: unmangled(nq_onFuncTypeNew) func -> FuncType {
f := FuncType new(token())
f isClosure = true
f
}
onFuncTypeArgument: unmangled(nq_onFuncTypeArgument) func (f: FuncType, argType: Type) {
f argTypes add(argType)
}
onFuncTypeVarArg: unmangled(nq_onFuncTypeVarArg) func (f: FuncType) {
f varArg = true
}
onFuncTypeReturnType: unmangled(nq_onFuncTypeReturnType) func (f: FuncType, returnType: Type) {
f returnType = returnType
}
/*
* Operator overloads
*/
onOperatorStart: unmangled(nq_onOperatorStart) func (symbol: CString) {
oDecl := OperatorDecl new(symbol toString() trim(), token())
fDecl := FunctionDecl new("", token())
oDecl setFunctionDecl(fDecl)
stack push(oDecl)
stack push(fDecl)
}
onOperatorEnd: unmangled(nq_onOperatorEnd) func {
oDecl := pop(OperatorDecl)
peek(Module) addOperator(oDecl)
}
/*
* Functions
*/
onFunctionStart: unmangled(nq_onFunctionStart) func (name, doc: CString) {
fDecl := FunctionDecl new(name toString(), token())
fDecl setVersion(getVersion())
fDecl doc = doc toString()
stack push(fDecl)
}
onFunctionExtern: unmangled(nq_onFunctionExtern) func (externName: CString) {
peek(FunctionDecl) setExternName(externName toString())
}
onFunctionUnmangled: unmangled(nq_onFunctionUnmangled) func (unmangledName: CString) {
peek(FunctionDecl) setUnmangledName(unmangledName toString())
}
onFunctionAbstract: unmangled(nq_onFunctionAbstract) func {
peek(FunctionDecl) isAbstract = true
}
onFunctionThisRef: unmangled(nq_onFunctionThisRef) func {
peek(FunctionDecl) isThisRef = true
}
onFunctionStatic: unmangled(nq_onFunctionStatic) func {
peek(FunctionDecl) isStatic = true
}
onFunctionInline: unmangled(nq_onFunctionInline) func {
peek(FunctionDecl) isInline = true
}
onFunctionFinal: unmangled(nq_onFunctionFinal) func {
peek(FunctionDecl) isFinal = true
}
onFunctionProto: unmangled(nq_onFunctionProto) func {
peek(FunctionDecl) isProto = true
}
onFunctionSuper: unmangled(nq_onFunctionSuper) func {
peek(FunctionDecl) isSuper = true
}
onFunctionSuffix: unmangled(nq_onFunctionSuffix) func (suffix: CString) {
peek(FunctionDecl) suffix = suffix toString()
}
onFunctionArgsStart: unmangled(nq_onFunctionArgsStart) func {
stack push(peek(FunctionDecl) args)
}
onFunctionArgsEnd: unmangled(nq_onFunctionArgsEnd) func {
pop(ArrayList<Argument>)
}
onFunctionReturnType: unmangled(nq_onFunctionReturnType) func (type: Type) {
peek(FunctionDecl) returnType = type
}
onFunctionBody: unmangled(nq_onFunctionBody) func {
peek(FunctionDecl) hasBody = true
}
onFunctionEnd: unmangled(nq_onFunctionEnd) func -> FunctionDecl {
fDecl := pop(FunctionDecl)
node : Node = stack peek()
if(node == module) {
module addFunction(fDecl)
} else if(node instanceOf?(TypeDecl)) {
tDecl := node as TypeDecl
tDecl addFunction(fDecl)
} else if(node instanceOf?(Addon)) {
addon := node as Addon
addon addFunction(fDecl)
} else {
//printf("^^^^^^^^ Unexpected function %s (peek is a %s)\n", fDecl name toCString(), node class name toCString())
}
return fDecl
}
/*
* Function calls
*/
onFunctionCallStart: unmangled(nq_onFunctionCallStart) func (name: CString) {
stack push(FunctionCall new(name toString(), token()))
}
onFunctionCallSuffix: unmangled(nq_onFunctionCallSuffix) func (suffix: CString) {
peek(FunctionCall) setSuffix(suffix toString())
}
onFunctionCallArg: unmangled(nq_onFunctionCallArg) func (expr: Expression) {
peek(FunctionCall) args add(expr)
}
onFunctionCallEnd: unmangled(nq_onFunctionCallEnd) func -> FunctionCall {
pop(FunctionCall)
}
onFunctionCallExpr: unmangled(nq_onFunctionCallExpr) func (call: FunctionCall, expr: Expression) {
call expr = expr
}
onFunctionCallCombo: unmangled(nq_onFunctionCallCombo) func (call: FunctionCall, expr: Expression) {
name := call generateTempName("comboRoot")
call setName(name)
vDecl := VariableDecl new(null, name, expr, expr token)
onStatement(vDecl)
}
onFunctionCallChain: unmangled(nq_onFunctionCallChain) func (expr: Expression, call: FunctionCall) -> CallChain {
if(expr instanceOf?(CallChain)) {
chain := expr as CallChain
//printf("Adding %s to existing callchain %s\n", call toString(), chain toString())
chain calls add(call)
return chain
} else {
//printf("Creating new call chain with expr %s and call %s\n", expr toString(), call toString())
return CallChain new(expr, call)
}
}
/*
* Literals
*/
onArrayLiteralStart: unmangled(nq_onArrayLiteralStart) func {
stack push(ArrayLiteral new(token()))
}
onArrayLiteralEnd: unmangled(nq_onArrayLiteralEnd) func -> ArrayLiteral {
pop(ArrayLiteral)
}
onTupleStart: unmangled(nq_onTupleStart) func {
stack push(Tuple new(token()))
}
onTupleEnd: unmangled(nq_onTupleEnd) func -> Tuple {
pop(Tuple)
}
onStringLiteral: unmangled(nq_onStringLiteral) func (text: CString) -> StringLiteral {
StringLiteral new(text toString() replaceAll("\n", "\\n") replaceAll("\t", "\\t"), token())
}
onCharLiteral: unmangled(nq_onCharLiteral) func (value: CString) -> CharLiteral {
CharLiteral new(value toString(), token())
}
// statement
onStatement: unmangled(nq_onStatement) func (stmt: Statement) {
if(stmt instanceOf?(VariableDecl)) {
//printf("[onStatement] stmt %s is a VariableDecl, calling gotVarDecl\n", stmt toString())
gotVarDecl(stmt as VariableDecl)
return
} else if(stmt instanceOf?(Stack<VariableDecl>)) {
stack : Stack<VariableDecl> = stmt
if(stack T inheritsFrom?(VariableDecl)) {
//printf("[onStatement] stmt is a Stack<VariableDecl>, calling gotVarDecl on each of'em\n")
for(vd in stack) {
//printf("[onStatement] among em, %s\n", vd toString())
gotVarDecl(vd)
}
return
}
}
gotStatement(stmt)
}
gotStatement: func (stmt: Statement) {
node := peek(Node)
match {
case node instanceOf?(FunctionDecl) =>
fDecl := node as FunctionDecl
fDecl body add(stmt)
case node instanceOf?(ControlStatement) =>
cStmt := node as ControlStatement
cStmt body add(stmt)
case node instanceOf?(ArrayAccess) =>
aa := node as ArrayAccess
if(!stmt instanceOf?(Expression)) {
params errorHandler onError(SyntaxError new(stmt token, "Expected an expression here, not a statement!"))
}
aa indices add(stmt as Expression)
case node instanceOf?(Module) =>
if(stmt instanceOf?(VariableDecl)) {
vd := stmt as VariableDecl
vd setGlobal(true)
}
module := node as Module
spec := getVersion()
if(spec != null) {
vb := VersionBlock new(spec, token())
vb getBody() add(stmt)
module body add(vb)
} else {
module body add(stmt)
}
case node instanceOf?(ClassDecl) =>
cDecl := node as ClassDecl
fDecl := cDecl lookupFunction(ClassDecl DEFAULTS_FUNC_NAME, "")
if(fDecl == null) {
fDecl = FunctionDecl new(ClassDecl DEFAULTS_FUNC_NAME, cDecl token)
cDecl addFunction(fDecl)
}
fDecl getBody() add(stmt)
case node instanceOf?(ArrayLiteral) =>
arrayLit := node as ArrayLiteral
if(!stmt instanceOf?(Expression)) {
params errorHandler onError(SyntaxError new(stmt token, "Expected an expression here, not a statement!"))
}
arrayLit getElements() add(stmt as Expression)
case node instanceOf?(Tuple) =>
tuple := node as Tuple
if(!stmt instanceOf?(Expression)) {
params errorHandler onError(SyntaxError new(stmt token, "Expected an expression here, not a statement!"))
}
tuple getElements() add(stmt as Expression)
case =>
printf("[gotStatement] Got a %s, don't know what to do with it, parent = %s\n", stmt toString() toCString(), node class name toCString())
}
}
onArrayAccessStart: unmangled(nq_onArrayAccessStart) func (array: Expression) {
stack push(ArrayAccess new(array, token()))
}
onArrayAccessEnd: unmangled(nq_onArrayAccessEnd) func () -> ArrayAccess {
pop(ArrayAccess)
}
// return
onReturn: unmangled(nq_onReturn) func (expr: Expression) -> Return {
Return new(expr, token())
}
// variable access
onVarAccess: unmangled(nq_onVarAccess) func (expr: Expression, name: CString) -> VariableAccess {
return VariableAccess new(expr, name toString(), token())
}
// cast
onCast: unmangled(nq_onCast) func (expr: Expression, type: Type) -> Cast {
return Cast new(expr, type, token())
}
// block {}
onBlockStart: unmangled(nq_onBlockStart) func {
stack push(Block new(token()))
}
onBlockEnd: unmangled(nq_onBlockEnd) func -> Block {
pop(Block)
}
// if
onIfStart: unmangled(nq_onIfStart) func (condition: Expression) {
stack push(If new(condition, token()))
}
onIfEnd: unmangled(nq_onIfEnd) func -> If {
pop(If)
}
// else
onElseStart: unmangled(nq_onElseStart) func {
stack push(Else new(token()))
}
onElseEnd: unmangled(nq_onElseEnd) func -> Else {
pop(Else)
}
// foreach
onForeachStart: unmangled(nq_onForeachStart) func (decl, collec: Expression) {
if(decl instanceOf?(Stack)) {
decl = decl as Stack<VariableDecl> pop()
}
stack push(Foreach new(decl, collec, token()))
}
onForeachEnd: unmangled(nq_onForeachEnd) func -> Foreach {
pop(Foreach)
}
// while
onWhileStart: unmangled(nq_onWhileStart) func (condition: Expression) {
stack push(While new(condition, token()))
}
onWhileEnd: unmangled(nq_onWhileEnd) func -> While {
pop(While)
}
/*
* Arguments
*/
onVarArg: unmangled(nq_onVarArg) func {
peek(List<Node>) add(VarArg new(token()))
}
onTypeArg: unmangled(nq_onTypeArg) func (type: Type) {
// TODO: add check for extern function (TypeArgs are illegal in non-extern functions.)
peek(List<Node>) add(Argument new(type, "", token()))
}
onDotArg: unmangled(nq_onDotArg) func (name: CString) {
// TODO: add check for member function
peek(List<Node>) add(DotArg new(name toString(), token()))
}
onAssArg: unmangled(nq_onAssArg) func (name: CString) {
// TODO: add check for member function
peek(List<Node>) add(AssArg new(name toString(), token()))
}
/*
* Match & case
*/
onMatchStart: unmangled(nq_onMatchStart) func {
stack push(Match new(token()))
}
onMatchExpr: unmangled(nq_onMatchExpr) func (v:Expression) {
peek(Match) setExpr(v)
}
onMatchEnd: unmangled(nq_onMatchEnd) func -> Match {
pop(Match)
}
onCaseStart: unmangled(nq_onCaseStart) func {
stack push(Case new(token()))
}
onCaseExpr: unmangled(nq_onCaseExpr) func (v:Expression) {
peek(Case) setExpr(v)
}
onCaseEnd: unmangled(nq_onCaseEnd) func {
caze := pop(Case)
peek(Match) addCase(caze)
}
onBreak: unmangled(nq_onBreak) func -> FlowControl {
FlowControl new(FlowAction _break, token())
}
onContinue: unmangled(nq_onContinue) func -> FlowControl {
FlowControl new(FlowAction _continue, token())
}
onEquals: unmangled(nq_onEquals) func (left, right: Expression) -> Comparison {
Comparison new(left, right, CompType equal, token())
}
onNotEquals: unmangled(nq_onNotEquals) func (left, right: Expression) -> Comparison {
Comparison new(left, right, CompType notEqual, token())
}
onLessThan: unmangled(nq_onLessThan) func (left, right: Expression) -> Comparison {
Comparison new(left, right, CompType smallerThan, token())
}
onMoreThan: unmangled(nq_onMoreThan) func (left, right: Expression) -> Comparison {
Comparison new(left, right, CompType greaterThan, token())
}
onCmp: unmangled(nq_onCmp) func (left, right: Expression) -> Comparison {
Comparison new(left, right, CompType compare, token())
}
onLessThanOrEqual: unmangled(nq_onLessThanOrEqual) func (left, right: Expression) -> Comparison {
Comparison new(left, right, CompType smallerOrEqual, token())
}
onMoreThanOrEqual: unmangled(nq_onMoreThanOrEqual) func (left, right: Expression) -> Comparison {
Comparison new(left, right, CompType greaterOrEqual, token())
}
onDecLiteral: unmangled(nq_onDecLiteral) func (value: CString) -> IntLiteral {
IntLiteral new(value toString() replaceAll("_", "") toLLong(), token())
}
onOctLiteral: unmangled(nq_onOctLiteral) func (value: CString) -> IntLiteral {
IntLiteral new(value toString() replaceAll("_", "") substring(2) toLLong(8), token())
}
onBinLiteral: unmangled(nq_onBinLiteral) func (value: CString) -> IntLiteral {
IntLiteral new(value toString() replaceAll("_", "") substring(2) toLLong(2), token())
}