-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
1000 lines (838 loc) · 18.7 KB
/
parse.go
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
package main
import (
"fmt"
)
func advance() {
tokens = tokens[1:]
}
func peek(s string) bool {
return len(tokens) > 0 && tokens[0].val == s
}
func consume(s string) bool {
if peek(s) {
advance()
return true
}
return false
}
func expect(s string) {
if consume(s) {
return
}
panic(fmt.Sprintf("Unexpected token: %+v. want: %s", tokens[0], s))
}
func consumeToken(tk tokenKind) *token {
if len(tokens) > 0 && tokens[0].kind == tk {
tok := tokens[0]
advance()
return tok
}
return nil
}
type program struct {
funcs []*function
}
type function struct {
name string
body statement
params []*obj
results []*obj
locals []*obj
stackSize int
paramsSize int
resultsSize int
}
func (f *function) assignLVarOffsets() {
offset := 8
for i := range f.params {
lv := f.params[i]
offset += lv.ty.size
lv.offset = alignTo(offset, 8)
}
f.paramsSize = alignTo(offset-8, 8)
for i := len(f.results) - 1; i >= 0; i-- {
lv := f.results[i]
offset += lv.ty.size
lv.offset = alignTo(offset, 8)
}
f.resultsSize = alignTo(offset-f.paramsSize-8, 8)
offset = 0
for i := len(f.locals) - 1; i >= 0; i-- {
if f.locals[i].offset != 0 {
continue
}
lv := f.locals[i]
offset += lv.ty.size
f.locals[i].offset = -alignTo(offset, lv.ty.size)
}
f.stackSize = alignTo(offset, 16)
}
// Statement
type statement interface {
aStmt()
}
type returnStmt struct {
statement
ty *typ
child statement
}
func (s *returnStmt) getType() *typ { return s.ty }
func (s *returnStmt) setType(ty *typ) { s.ty = ty }
type blockStmt struct {
statement
ty *typ
stmts []statement
}
func (s *blockStmt) getType() *typ { return s.ty }
func (s *blockStmt) setType(ty *typ) { s.ty = ty }
type ifStmt struct {
statement
ty *typ
init statement
cond expression
then statement
els statement
}
func (s *ifStmt) getType() *typ { return s.ty }
func (s *ifStmt) setType(ty *typ) { s.ty = ty }
type forStmt struct {
statement
ty *typ
cond expression
init statement
post statement
body statement
}
func (s *forStmt) getType() *typ { return s.ty }
func (s *forStmt) setType(ty *typ) { s.ty = ty }
type expressionStmt struct {
statement
ty *typ
child expression
}
func (s *expressionStmt) getType() *typ { return s.ty }
func (s *expressionStmt) setType(ty *typ) { s.ty = ty }
type assignment struct {
statement
ty *typ
lhs []expression
rhs expressionList
}
func (s *assignment) getType() *typ { return s.ty }
func (s *assignment) setType(ty *typ) { s.ty = ty }
// Expressions
type expression interface {
anExpr()
getType() *typ
setType(ty *typ)
}
type singleMultiValuedExpression interface {
multiValues() []expression
}
type expressionList []expression
func (es expressionList) convertSingleMultiValuedExpression() singleMultiValuedExpression {
if len(es) == 1 {
if e, ok := es[0].(singleMultiValuedExpression); ok {
return e
}
}
return nil
}
type intLit struct {
expression
ty *typ
val int
}
func (e *intLit) getType() *typ { return e.ty }
func (e *intLit) setType(ty *typ) { e.ty = ty }
type compositeLit struct {
expression
ty *typ
elems []expression
}
func (e *compositeLit) getType() *typ { return e.ty }
func (e *compositeLit) setType(ty *typ) { e.ty = ty }
type memberRef struct {
expression
ty *typ
member *member
child expression
}
func (e *memberRef) getType() *typ { return e.ty }
func (e *memberRef) setType(ty *typ) { e.ty = ty }
type binary struct {
expression
ty *typ
op string
lhs expression
rhs expression
}
func (e *binary) getType() *typ { return e.ty }
func (e *binary) setType(ty *typ) { e.ty = ty }
type obj struct {
expression
ty *typ
name string
offset int
}
func (e *obj) getType() *typ { return e.ty }
func (e *obj) setType(ty *typ) {
if e.ty != nil {
return
}
e.ty = ty
}
type deref struct {
expression
ty *typ
child expression
}
func (e *deref) getType() *typ { return e.ty }
func (e *deref) setType(ty *typ) { e.ty = ty }
type addr struct {
expression
ty *typ
child expression
}
func (e *addr) getType() *typ { return e.ty }
func (e *addr) setType(ty *typ) { e.ty = ty }
type funcCall struct {
expression
ty *typ
name string
args []expression
target *function
}
func (e *funcCall) multiValues() []expression {
ret := make([]expression, len(e.target.results))
for i, res := range e.target.results {
ret[i] = res
}
return ret
}
func (e *funcCall) getType() *typ { return e.ty }
func (e *funcCall) setType(ty *typ) { e.ty = ty }
// temporary sets
var locals []*obj
var results []*obj
var callees []*funcCall
var uniqueID = 0
func newUniqueName() string {
s := fmt.Sprintf(".L..%d", uniqueID)
uniqueID++
return s
}
func createLocalVar(name string) *obj {
lv := &obj{
name: name,
}
locals = append(locals, lv)
return lv
}
func findLocalVar(name string) *obj {
for i := range locals {
lv := locals[i]
if lv.name == name {
return lv
}
}
return nil
}
// TopLevelDecl = FunctionDecl .
func parse() *program {
mFuncs := make(map[string]*function)
ret := &program{
funcs: make([]*function, 0),
}
for len(tokens) > 0 {
expect("func")
f := parseFunction()
ret.funcs = append(ret.funcs, f)
mFuncs[f.name] = f
expect(";")
}
for _, c := range callees {
f := mFuncs[c.name]
c.target = f
}
for _, f := range ret.funcs {
addType(f.body)
f.assignLVarOffsets()
}
return ret
}
// VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
func parseVarDecl() statement {
if !consume("(") {
return parseVarSpec()
}
ret := &blockStmt{
stmts: []statement{},
}
for !consume(")") {
ret.stmts = append(ret.stmts, parseVarSpec())
expect(";")
}
return ret
}
// VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
func parseVarSpec() statement {
ids := parseIdentifierList()
if consume("=") {
lhs := make([]expression, len(ids))
for i, id := range ids {
lhs[i] = createLocalVar(id)
}
rhs := parseExpressionList()
return &assignment{lhs: lhs, rhs: rhs}
}
ty := parseType()
stmts := make([]statement, len(ids))
for i, id := range ids {
lv := createLocalVar(id)
lv.ty = ty
stmts[i] = initializer(lv)
}
return &blockStmt{stmts: stmts}
}
func initializer(expr expression) statement {
switch ty := expr.getType(); ty.kind {
case typeKindStruct:
stmts := make([]statement, len(ty.members))
for i, mem := range ty.members {
lhs := &memberRef{child: expr, member: mem, ty: mem.ty}
stmts[i] = initializer(lhs)
}
return &blockStmt{
stmts: stmts,
}
case typeKindArray:
lhs := make([]expression, ty.length)
rhs := make([]expression, ty.length)
for i := 0; i < ty.length; i++ {
lhs[i] = &deref{child: addBinary(expr, &intLit{val: i})}
rhs[i] = zeroValueMap[ty.base.kind]
}
return &assignment{lhs: lhs, rhs: rhs}
default:
return &assignment{lhs: expressionList{expr}, rhs: expressionList{zeroValueMap[ty.kind]}}
}
}
// FunctionDecl = "func" FunctionName Signature [ FunctionBody ] .
// FunctionName = identifier .
// FunctionBody = Block .
func parseFunction() *function {
locals = []*obj{}
results = []*obj{}
tok := consumeToken(tokenKindIdentifier)
if tok == nil {
panic("must be an identifier")
}
ret := &function{name: tok.val}
expect("(")
// Signature = Parameters [ Type ] .
ret.params, ret.results = parseSignature()
expect("{")
ret.body = parseBlockStmt()
ret.locals = locals
return ret
}
func parseSignature() ([]*obj, []*obj) {
params := parseParameters()
if consume("(") {
// multiple results
for i := 0; !consume(")"); i++ {
if i > 0 {
expect(",")
}
// TODO: identifier
tok := consumeToken(tokenKindType)
if tok == nil {
panic(fmt.Sprintf("Expected a type: %+v", tokens[0]))
}
lv := createLocalVar(tok.val)
lv.ty = newLiteralType(tok.val)
results = append(results, lv)
}
return params, results
}
if tok := consumeToken(tokenKindType); tok != nil {
// TODO: identifier
lv := createLocalVar(tok.val)
lv.ty = newLiteralType(tok.val)
results = append(results, lv)
return params, results
}
return params, results
}
// Parameters = "(" [ ParameterList ] ")" .
func parseParameters() []*obj {
var params []*obj
if consume(")") {
return params
}
params = parseParameterList()
expect(")")
return params
}
// ParameterList = ParameterDecl { "," ParameterDecl } .
func parseParameterList() []*obj {
var ret []*obj
for i := 0; i == 0 || consume(","); i++ {
ret = append(ret, parseParameterDecl()...)
}
return ret
}
// ParameterDecl = [ IdentifierList ] Type .
func parseParameterDecl() []*obj {
ids := parseIdentifierList()
ty := parseType()
ret := make([]*obj, len(ids))
for i, id := range ids {
ret[i] = createLocalVar(id)
ret[i].ty = ty
}
return ret
}
func parseType() *typ {
tok := consumeToken(tokenKindType)
if tok == nil {
tok = consumeToken(tokenKindOperator)
}
if tok == nil {
panic(fmt.Sprintf("Expected a type: %+v", tokens[0]))
}
if tok.val == "struct" {
return parseStructDecl()
}
if tok.val == "[" {
return parseArrayType()
}
return newLiteralType(tok.val)
}
// Statement = Declaration | ReturnStmt | SimpleStmt .
// Declaration = VarDecl .
func parseStatement() statement {
// varDeclaration
if consume("var") {
return parseVarDecl()
}
// return
if consume("return") {
// ReturnStmt = "return" [ ExpressionList ] .
if peek("}") {
return &returnStmt{}
}
lhs := make([]expression, len(results))
for i, v := range results {
lhs[i] = v
}
rhs := parseExpressionList()
return &returnStmt{child: &assignment{lhs: lhs, rhs: rhs}}
}
// block
if consume("{") {
return parseBlockStmt()
}
// if
if consume("if") {
return parseIfStmt()
}
// for
if consume("for") {
return parseForStmt()
}
return parseSimpleStmt()
}
// Block = "{" StatementList "}" .
func parseBlockStmt() statement {
var stmts []statement
for !consume("}") {
stmts = append(stmts, parseStatement())
}
return &blockStmt{stmts: stmts}
}
// IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
func parseIfStmt() statement {
var cond expression
var init statement
tmp := parseSimpleStmt()
if t, ok := tmp.(*expressionStmt); ok {
cond = t.child
} else {
init = tmp
expect(";")
cond = parseExpression()
}
expect("{")
then := parseBlockStmt()
ret := &ifStmt{
init: init,
cond: cond,
then: then,
}
if !consume("else") {
return ret
}
if consume("{") {
ret.els = parseBlockStmt()
} else if consume("if") {
ret.els = parseIfStmt()
}
return ret
}
// ForStmt = "for" [ Condition | ForClause ] Block .
// Condition = Expression .
// ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
// InitStmt = SimpleStmt .
// PostStmt = SimpleStmt .
func parseForStmt() statement {
if consume("{") {
return &forStmt{body: parseBlockStmt()}
}
var cond expression
var init statement
var post statement
if !consume(";") {
tmp := parseSimpleStmt()
if t, ok := tmp.(*expressionStmt); ok {
cond = t.child
expect("{")
return &forStmt{
cond: cond,
body: parseBlockStmt(),
}
}
init = tmp
expect(";")
}
if !consume(";") {
cond = parseExpression()
expect(";")
}
if !consume("{") {
post = parseSimpleStmt()
expect("{")
}
return &forStmt{
cond: cond,
init: init,
post: post,
body: parseBlockStmt(),
}
}
func parseSimpleStmt() statement {
expr := parseExpressionList()
if consume("=") {
// Assignment
for i, l := range expr {
switch l := l.(type) {
case *obj:
lv := findLocalVar(l.name)
if lv == nil {
panic(fmt.Sprintf("a local variable not declared: %s", l.name))
}
expr[i] = lv
}
}
return &assignment{lhs: expr, rhs: parseExpressionList()}
}
if consume(":=") {
// ShortVarDecl
rhs := parseExpressionList()
ret := &assignment{lhs: expr, rhs: rhs}
if se := rhs.convertSingleMultiValuedExpression(); se == nil {
addType(ret)
ret = &assignment{lhs: expandExpressionList(expr), rhs: expandExpressionList(rhs)}
}
return ret
}
return &expressionStmt{child: expr[0]}
}
func expandExpressionList(exprs []expression) []expression {
if len(exprs) == 0 {
return nil
}
expr, remain := exprs[0], exprs[1:]
expanded := []expression{expr}
ty := expr.getType()
if c, ok := expr.(*compositeLit); ok {
expanded = c.elems
} else if ty.kind == typeKindArray {
expanded = make([]expression, ty.length)
for i := 0; i < ty.length; i++ {
expanded[i] = &deref{child: addBinary(expr, &intLit{val: i})}
}
}
expandedRemain := expandExpressionList(remain)
if expandedRemain != nil {
return append(expanded, expandedRemain...)
}
return expanded
}
// ExpressionList = Expression { "," Expression } .
func parseExpressionList() expressionList {
ret := expressionList{parseExpression()}
for consume(",") {
ret = append(ret, parseExpression())
}
return ret
}
// IdentifierList = identifier { "," identifier } .
func parseIdentifierList() []string {
var ret []string
tok := consumeToken(tokenKindIdentifier)
if tok == nil {
return ret
}
ret = append(ret, tok.val)
for consume(",") {
tok := consumeToken(tokenKindIdentifier)
if tok == nil {
panic(fmt.Sprintf("Expect an identifier: %+v", tokens[0]))
}
ret = append(ret, tok.val)
}
return ret
}
func parseExpression() expression {
return parseRel()
}
// rel = add ((">=" | "<=" | "==" | "!=") add)*
func parseRel() expression {
ret := parseAdd()
for {
switch {
case consume("<"):
ret = &binary{op: "<", lhs: ret, rhs: parseAdd()}
case consume(">"):
ret = &binary{op: "<", lhs: parseAdd(), rhs: ret}
case consume("<="):
ret = &binary{op: "<=", lhs: ret, rhs: parseAdd()}
case consume(">="):
ret = &binary{op: "<=", lhs: parseAdd(), rhs: ret}
case consume("=="):
ret = &binary{op: "==", lhs: ret, rhs: parseAdd()}
case consume("!="):
ret = &binary{op: "!=", lhs: ret, rhs: parseAdd()}
default:
return ret
}
}
}
// add = mul (("*" | "/") mul)*
func parseAdd() expression {
ret := parseMul()
for {
switch {
case consume("+"):
ret = &binary{op: "+", lhs: ret, rhs: parseMul()}
case consume("-"):
ret = &binary{op: "-", lhs: ret, rhs: parseMul()}
default:
return ret
}
}
}
// mul = unary (("*" | "/") unary)*
func parseMul() expression {
ret := parseUnary()
for {
switch {
case consume("*"):
ret = &binary{op: "*", lhs: ret, rhs: parseUnary()}
case consume("/"):
ret = &binary{op: "/", lhs: ret, rhs: parseUnary()}
default:
return ret
}
}
}
// unary = ("+" | "-" | "*" | "&")? unary | primary
func parseUnary() expression {
switch {
case consume("+"):
return parseUnary()
case consume("-"):
return &binary{op: "-", lhs: &intLit{val: 0}, rhs: parseUnary()}
case consume("*"):
return &deref{child: parseUnary()}
case consume("&"):
return &addr{child: parseUnary()}
case consume("!"):
return &binary{op: "==", rhs: parseUnary(), lhs: &intLit{val: 0}}
default:
return parsePrimary()
}
}
// PrimaryExpr = Operand
// | PrimaryExpr Selector .
// | PrimaryExpr Index .
func parsePrimary() expression {
expr := parseOperand()
for {
if consume(".") {
expr = parseSelector(expr)
continue
}
if consume("[") {
expr = parseIndex(expr)
continue
}
return expr
}
}
// Selector = "." identifier .
func parseSelector(expr expression) expression {
ty := expr.getType()
if ty.kind != typeKindStruct {
panic("expected struct type")
}
tok := consumeToken(tokenKindIdentifier)
if tok == nil {
panic(fmt.Sprintf("Expected an identifier: %+v", tokens[0]))
}
var mem *member
for i := range ty.members {
m := ty.members[i]
if m.name == tok.val {
mem = m
}
}
return &memberRef{child: expr, member: mem}
}
// Index = "[" Expression "]" .
func parseIndex(expr expression) expression {
index := parseExpression()
expect("]")
return &deref{child: addBinary(expr, index)}
}
// Operand = Literal | identifier [ Arguments ] | "(" Expression ")" .
func parseOperand() expression {
if consume("(") {
ret := parseExpression()
expect(")")
return ret
}
// identifier
if tok := consumeToken(tokenKindIdentifier); tok != nil {
if consume("(") {
return parseArguments(tok.val)
}
lv := findLocalVar(tok.val)
if lv == nil {
lv = createLocalVar(tok.val)
}
return lv
}
// Literal
return parseLiteral()
}
// Arguments = "(" [ ExpressionList [ "..." ] [ "," ] ] ")" .
func parseArguments(name string) expression {
ret := &funcCall{name: name}
callees = append(callees, ret)
if consume(")") {
return ret
}
ret.args = parseExpressionList()
expect(")")
return ret
}
func parseLiteral() expression {
if consume("struct") {
ty := parseStructDecl()
expect("{")
tmp := parseStructLiteral(ty)
return tmp
}
if consume("[") {
ty := parseArrayType()
expect("{")
return parseArrayLiteral(ty)
}
return parseIntLit()
}
// ArrayType = "[" ArrayLength "]" ElementType .
// ArrayLength = Expression .
// ElementType = Type .
func parseArrayType() *typ {
length := parseNum()
expect("]")
base := parseType()
return arrayOf(base, length)
}
// StructType = "struct" "{" { FieldDecl ";" } "}" .
// FieldDecl = (IdentifierList Type) .
func parseStructDecl() *typ {
expect("{")
var members []*member
for !consume("}") {
ids := parseIdentifierList()
ty := parseType()
for _, id := range ids {
members = append(members, &member{
name: id,
ty: ty,
})
}
expect(";")
}
return newStructType(members)
}
// LiteralValue = "{" [ ElementList [ "," ] ] "}" .
// ElementList = KeyedElement { "," KeyedElement } .
// KeyedElement = [ Key ":" ] Element .
// Key = FieldName | Expression | LiteralValue .
// FieldName = identifier .
// Element = Expression | LiteralValue .
// LiteralValue = "{" [ ElementList [ "," ] ] "}" .
// ElementList = { "," Element } .
// Element = Expression | LiteralValue .
func parseArrayLiteral(ty *typ) expression {
ret := &compositeLit{
ty: ty,
elems: []expression{},
}
for i := 0; !consume("}"); i++ {
if i > 0 {
expect(",")
}
ret.elems = append(ret.elems, parseExpression())
}
return ret
}
// LiteralValue = "{" [ ElementList [ "," ] ] "}" .
// ElementList = KeyedElement { "," KeyedElement } .
// KeyedElement = [ Key ":" ] Element .
// Key = identifier .
// Element = Expression | LiteralValue .
func parseStructLiteral(ty *typ) *obj {
v := createLocalVar(newUniqueName())
for i := 0; !consume("}"); i++ {
// TODO:
panic("unimplemented")
}
v.ty = ty
return v
}
func parseIntLit() expression {
return &intLit{
val: parseNum(),
}
}
func parseNum() int {
tok := tokens[0]
advance()
return tok.num
}
func addBinary(lhs, rhs expression) expression {
addType(lhs)
addType(rhs)
// ptr + num
if lhs.getType().base != nil && rhs.getType().kind == typeKindInt {
rhs = &binary{op: "*", lhs: &intLit{val: lhs.getType().base.size}, rhs: rhs}
return &binary{op: "+", lhs: lhs, rhs: rhs}
}
return &binary{op: "+", lhs: lhs, rhs: rhs}
}