forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go2gno.go
787 lines (753 loc) · 18.3 KB
/
go2gno.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
package gno
/*
This package helps parse Go code into GNO ast. It uses go/parser, which
may imply that the internal gc tooling found in
`golang/src/cmd/compile/internal/gc|syntax` may not be useful for
type-checking and static analysis.
from `golang/src/cmd/compile/README.md`:
> Note that the `go/*` family of packages, such as `go/parser` and
> `go/types`, have no relation to the compiler. Since the compiler was
> initially written in C, the `go/*` packages were developed to enable
> writing tools working with Go code, such as `gofmt` and `vet`.
The interpreter is written first and foremost such that we *could* use Go's
`golang/src/cmd/compile/*` logic for type-checking. In other words, the
interpreter is lax and execution may (or may not!) fail if the code run is
invalid Go code.
Initially we will use the `go/parser` package to parse Go code and by
default configure the Machine to perform run-time assertions such as
type-checking. Code parsed from `go/parser` that would fail static
analysis in Go should fail at run-time with the configuration but otherwise
behave identically.
This lets us extend the langauge (e.g. a new kind like the Type kind may
become available in the Gno langauge), and helps us plan to transition to
the final implementation of the Gno parser which should be written in pure
Gno. Callers of the interpreter have the option of using the
`golang/src/cmd/compile/*` package for vetting code correctness.
*/
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"reflect"
"strconv"
"github.com/davecgh/go-spew/spew"
"github.com/gnolang/gno/pkgs/errors"
)
func MustReadFile(path string) *FileNode {
n, err := ReadFile(path)
if err != nil {
panic(err)
}
return n
}
func MustParseFile(filename string, body string) *FileNode {
n, err := ParseFile(filename, body)
if err != nil {
panic(err)
}
return n
}
func ReadFile(path string) (*FileNode, error) {
bz, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return ParseFile(path, string(bz))
}
func ParseExpr(expr string) (retx Expr, err error) {
x, err := parser.ParseExpr(expr)
if err != nil {
return nil, err
}
// recover from Go2Gno.
// NOTE: Go2Gno is best implemented with panics due to inlined toXYZ() calls.
defer func() {
if r := recover(); r != nil {
if rerr, ok := r.(error); ok {
err = rerr
} else {
err = errors.New(fmt.Sprintf("%v", r))
}
return
}
}()
// parse with Go2Gno.
return Go2Gno(nil, x).(Expr), nil
}
func MustParseExpr(expr string) Expr {
x, err := ParseExpr(expr)
if err != nil {
panic(err)
}
return x
}
// filename must not include the path.
func ParseFile(filename string, body string) (fn *FileNode, err error) {
// Parse src but stop after processing the imports.
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, filename, body, parser.ParseComments|parser.DeclarationErrors)
if err != nil {
return nil, err
}
// Print the imports from the file's AST.
// spew.Dump(f)
// recover from Go2Gno.
// NOTE: Go2Gno is best implemented with panics due to inlined toXYZ() calls.
defer func() {
if r := recover(); r != nil {
if rerr, ok := r.(error); ok {
err = rerr
} else {
err = errors.New(fmt.Sprintf("%v", r))
}
return
}
}()
// parse with Go2Gno.
fn = Go2Gno(fs, f).(*FileNode)
fn.Name = Name(filename)
return fn, nil
}
func setLoc(fs *token.FileSet, pos token.Pos, n Node) Node {
posn := fs.Position(pos)
n.SetLine(posn.Line)
return n
}
// If gon is a *ast.File, the name must be filled later.
func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) {
if gon == nil {
return nil
}
if fs != nil {
defer func() {
if n != nil {
setLoc(fs, gon.Pos(), n)
}
}()
}
switch gon := gon.(type) {
case *ast.ParenExpr:
return toExpr(fs, gon.X)
case *ast.Ident:
return Nx(toName(gon))
case *ast.BasicLit:
if gon == nil {
return nil
}
return &BasicLitExpr{
Kind: toWord(gon.Kind),
Value: gon.Value,
}
case *ast.BinaryExpr:
return &BinaryExpr{
Left: toExpr(fs, gon.X),
Op: toWord(gon.Op),
Right: toExpr(fs, gon.Y),
}
case *ast.CallExpr:
return &CallExpr{
Func: toExpr(fs, gon.Fun),
Args: toExprs(fs, gon.Args),
Varg: gon.Ellipsis.IsValid(),
}
case *ast.IndexExpr:
return &IndexExpr{
X: toExpr(fs, gon.X),
Index: toExpr(fs, gon.Index),
}
case *ast.SelectorExpr:
return &SelectorExpr{
X: toExpr(fs, gon.X),
Sel: toName(gon.Sel),
}
case *ast.SliceExpr:
return &SliceExpr{
X: toExpr(fs, gon.X),
Low: toExpr(fs, gon.Low),
High: toExpr(fs, gon.High),
Max: toExpr(fs, gon.Max),
}
case *ast.StarExpr:
return &StarExpr{
X: toExpr(fs, gon.X),
}
case *ast.TypeAssertExpr:
return &TypeAssertExpr{
X: toExpr(fs, gon.X),
Type: toExpr(fs, gon.Type),
}
case *ast.UnaryExpr:
if gon.Op == token.AND {
return &RefExpr{
X: toExpr(fs, gon.X),
}
} else {
return &UnaryExpr{
X: toExpr(fs, gon.X),
Op: toWord(gon.Op),
}
}
case *ast.CompositeLit:
// If ArrayType with ellipsis for length,
// just figure out the length here.
return &CompositeLitExpr{
Type: toExpr(fs, gon.Type),
Elts: toKeyValueExprs(fs, gon.Elts),
}
case *ast.KeyValueExpr:
return &KeyValueExpr{
Key: toExpr(fs, gon.Key),
Value: toExpr(fs, gon.Value),
}
case *ast.FuncLit:
type_ := Go2Gno(fs, gon.Type).(*FuncTypeExpr)
return &FuncLitExpr{
Type: *type_,
Body: toBody(fs, gon.Body),
}
case *ast.Field:
if len(gon.Names) == 0 {
return &FieldTypeExpr{
Name: "",
Type: toExpr(fs, gon.Type),
Tag: toExpr(fs, gon.Tag),
}
} else if len(gon.Names) == 1 {
return &FieldTypeExpr{
Name: toName(gon.Names[0]),
Type: toExpr(fs, gon.Type),
Tag: toExpr(fs, gon.Tag),
}
} else {
panic(fmt.Sprintf(
"expected a Go Field with 1 name but got %v.\n"+
"maybe call toFields",
gon.Names))
}
case *ast.ArrayType:
if _, ok := gon.Len.(*ast.Ellipsis); ok {
return &ArrayTypeExpr{
Len: nil,
Elt: toExpr(fs, gon.Elt),
}
} else if gon.Len == nil {
return &SliceTypeExpr{
Elt: toExpr(fs, gon.Elt),
Vrd: false,
}
} else {
return &ArrayTypeExpr{
Len: toExpr(fs, gon.Len),
Elt: toExpr(fs, gon.Elt),
}
}
case *ast.Ellipsis:
return &SliceTypeExpr{
Elt: toExpr(fs, gon.Elt),
Vrd: true,
}
case *ast.InterfaceType:
return &InterfaceTypeExpr{
Methods: toFieldsFromList(fs, gon.Methods),
}
case *ast.ChanType:
var dir ChanDir
if gon.Dir&ast.SEND > 0 {
dir |= SEND
}
if gon.Dir&ast.RECV > 0 {
dir |= RECV
}
return &ChanTypeExpr{
Dir: dir,
Value: toExpr(fs, gon.Value),
}
case *ast.FuncType:
return &FuncTypeExpr{
Params: toFieldsFromList(fs, gon.Params),
Results: toFieldsFromList(fs, gon.Results),
}
case *ast.MapType:
return &MapTypeExpr{
Key: toExpr(fs, gon.Key),
Value: toExpr(fs, gon.Value),
}
case *ast.StructType:
return &StructTypeExpr{
Fields: toFieldsFromList(fs, gon.Fields),
}
case *ast.AssignStmt:
return &AssignStmt{
Lhs: toExprs(fs, gon.Lhs),
Op: toWord(gon.Tok),
Rhs: toExprs(fs, gon.Rhs),
}
case *ast.BlockStmt:
return &BlockStmt{
Body: toStmts(fs, gon.List),
}
case *ast.BranchStmt:
return &BranchStmt{
Op: toWord(gon.Tok),
Label: toName(gon.Label),
}
case *ast.DeclStmt:
return &DeclStmt{
Body: toSimpleDeclStmts(fs, gon.Decl.(*ast.GenDecl)),
}
case *ast.DeferStmt:
cx := toExpr(fs, gon.Call).(*CallExpr)
return &DeferStmt{
Call: *cx,
}
case *ast.ExprStmt:
if cx, ok := gon.X.(*ast.CallExpr); ok {
if ix, ok := cx.Fun.(*ast.Ident); ok && ix.Name == "panic" {
if len(cx.Args) != 1 {
panic("expected panic statement to have single exception value")
}
return &PanicStmt{
Exception: toExpr(fs, cx.Args[0]),
}
}
}
return &ExprStmt{
X: toExpr(fs, gon.X),
}
case *ast.ForStmt:
return &ForStmt{
Init: toSimp(fs, gon.Init),
Cond: toExpr(fs, gon.Cond),
Post: toSimp(fs, gon.Post),
Body: toStmts(fs, gon.Body.List),
}
case *ast.IfStmt:
thenStmt := IfCaseStmt{
Body: toStmts(fs, gon.Body.List),
}
setLoc(fs, gon.Body.Pos(), &thenStmt)
ess := []Stmt(nil)
if gon.Else != nil {
if _, ok := gon.Else.(*ast.BlockStmt); ok {
ess = Go2Gno(fs, gon.Else).(*BlockStmt).Body
} else {
ess = []Stmt{toStmt(fs, gon.Else)}
}
}
elseStmt := IfCaseStmt{
Body: ess,
}
if gon.Else != nil {
setLoc(fs, gon.Else.Pos(), &elseStmt)
}
return &IfStmt{
Init: toSimp(fs, gon.Init),
Cond: toExpr(fs, gon.Cond),
Then: thenStmt,
Else: elseStmt,
}
case *ast.IncDecStmt:
return &IncDecStmt{
X: toExpr(fs, gon.X),
Op: toWord(gon.Tok),
}
case *ast.LabeledStmt:
stmt := toStmt(fs, gon.Stmt)
stmt.SetLabel(toName(gon.Label))
return stmt
case *ast.RangeStmt:
return &RangeStmt{
X: toExpr(fs, gon.X),
Key: toExpr(fs, gon.Key),
Value: toExpr(fs, gon.Value),
Op: toWord(gon.Tok),
Body: toBody(fs, gon.Body),
}
case *ast.ReturnStmt:
return &ReturnStmt{
Results: toExprs(fs, gon.Results),
}
case *ast.TypeSwitchStmt:
switch as := gon.Assign.(type) {
case *ast.AssignStmt:
return &SwitchStmt{
Init: toStmt(fs, gon.Init),
X: toExpr(fs, as.Rhs[0].(*ast.TypeAssertExpr).X),
IsTypeSwitch: true,
Clauses: toClauses(fs, gon.Body.List),
VarName: toName(as.Lhs[0].(*ast.Ident)),
}
case *ast.ExprStmt:
return &SwitchStmt{
Init: toStmt(fs, gon.Init),
X: toExpr(fs, as.X.(*ast.TypeAssertExpr).X),
IsTypeSwitch: true,
Clauses: toClauses(fs, gon.Body.List),
VarName: "",
}
default:
panic(fmt.Sprintf(
"unexpected *ast.TypeSwitchStmt.Assign type %s",
reflect.TypeOf(gon.Assign).String()))
}
case *ast.SwitchStmt:
x := toExpr(fs, gon.Tag)
if x == nil {
// if tag is nil, default to "true"
x = Nx(Name("true"))
}
return &SwitchStmt{
Init: toStmt(fs, gon.Init),
X: x,
IsTypeSwitch: false,
Clauses: toClauses(fs, gon.Body.List),
}
case *ast.FuncDecl:
isMethod := gon.Recv != nil
recv := FieldTypeExpr{}
if isMethod {
if len(gon.Recv.List) > 1 {
panic("*ast.FuncDecl cannot have multiple receivers")
}
recv = *Go2Gno(fs, gon.Recv.List[0]).(*FieldTypeExpr)
}
name := toName(gon.Name)
type_ := Go2Gno(fs, gon.Type).(*FuncTypeExpr)
body := Go2Gno(fs, gon.Body).(*BlockStmt).Body
return &FuncDecl{
IsMethod: isMethod,
Recv: recv,
NameExpr: NameExpr{Name: name},
Type: *type_,
Body: body,
}
case *ast.GenDecl:
panic("unexpected *ast.GenDecl; use toDecls(fs,) instead")
case *ast.File:
pkgName := Name(gon.Name.Name)
decls := make([]Decl, 0, len(gon.Decls))
for _, d := range gon.Decls {
if gd, ok := d.(*ast.GenDecl); ok {
decls = append(decls, toDecls(fs, gd)...)
} else {
decls = append(decls, toDecl(fs, d))
}
}
return &FileNode{
Name: "", // filled later.
PkgName: pkgName,
Decls: decls,
}
default:
panic(fmt.Sprintf("unknown Go type %v: %s\n",
reflect.TypeOf(gon),
spew.Sdump(gon),
))
}
}
//----------------------------------------
// utility methods
func toName(name *ast.Ident) Name {
if name == nil {
return Name("")
} else {
return Name(name.Name)
}
}
var token2word = map[token.Token]Word{
token.ILLEGAL: ILLEGAL,
token.IDENT: NAME,
token.INT: INT,
token.FLOAT: FLOAT,
token.IMAG: IMAG,
token.CHAR: CHAR,
token.STRING: STRING,
token.ADD: ADD,
token.SUB: SUB,
token.MUL: MUL,
token.QUO: QUO,
token.REM: REM,
token.AND: BAND,
token.OR: BOR,
token.XOR: XOR,
token.SHL: SHL,
token.SHR: SHR,
token.AND_NOT: BAND_NOT,
token.ADD_ASSIGN: ADD_ASSIGN,
token.SUB_ASSIGN: SUB_ASSIGN,
token.MUL_ASSIGN: MUL_ASSIGN,
token.QUO_ASSIGN: QUO_ASSIGN,
token.REM_ASSIGN: REM_ASSIGN,
token.AND_ASSIGN: BAND_ASSIGN,
token.OR_ASSIGN: BOR_ASSIGN,
token.XOR_ASSIGN: XOR_ASSIGN,
token.SHL_ASSIGN: SHL_ASSIGN,
token.SHR_ASSIGN: SHR_ASSIGN,
token.AND_NOT_ASSIGN: BAND_NOT_ASSIGN,
token.LAND: LAND,
token.LOR: LOR,
token.ARROW: ARROW,
token.INC: INC,
token.DEC: DEC,
token.EQL: EQL,
token.LSS: LSS,
token.GTR: GTR,
token.ASSIGN: ASSIGN,
token.NOT: NOT,
token.NEQ: NEQ,
token.LEQ: LEQ,
token.GEQ: GEQ,
token.DEFINE: DEFINE,
token.BREAK: BREAK,
token.CASE: CASE,
token.CHAN: CHAN,
token.CONST: CONST,
token.CONTINUE: CONTINUE,
token.DEFAULT: DEFAULT,
token.DEFER: DEFER,
token.ELSE: ELSE,
token.FALLTHROUGH: FALLTHROUGH,
token.FOR: FOR,
token.FUNC: FUNC,
token.GO: GO,
token.GOTO: GOTO,
token.IF: IF,
token.IMPORT: IMPORT,
token.RETURN: RETURN,
token.SELECT: SELECT,
token.STRUCT: STRUCT,
token.TYPE: TYPE,
token.VAR: VAR,
}
func toWord(tok token.Token) Word {
return token2word[tok]
}
func toExpr(fs *token.FileSet, gox ast.Expr) Expr {
// TODO: could the language handle this?
gnox := Go2Gno(fs, gox)
if gnox == nil {
return nil
} else {
return gnox.(Expr)
}
}
func toExprs(fs *token.FileSet, goxs []ast.Expr) (gnoxs Exprs) {
if len(goxs) == 0 {
return nil
}
gnoxs = make([]Expr, len(goxs))
for i, x := range goxs {
gnoxs[i] = toExpr(fs, x)
}
return
}
func toStmt(fs *token.FileSet, gos ast.Stmt) Stmt {
gnos := Go2Gno(fs, gos)
if gnos == nil {
return nil
} else {
return gnos.(Stmt)
}
}
func toStmts(fs *token.FileSet, goss []ast.Stmt) (gnoss Body) {
gnoss = make([]Stmt, len(goss))
for i, x := range goss {
gnoss[i] = toStmt(fs, x)
}
return
}
func toBody(fs *token.FileSet, body *ast.BlockStmt) Body {
if body == nil {
return nil
}
return toStmts(fs, body.List)
}
func toSimp(fs *token.FileSet, gos ast.Stmt) Stmt {
gnos := Go2Gno(fs, gos)
if gnos == nil {
return nil
} else {
return gnos.(SimpleStmt).(Stmt)
}
}
func toDecl(fs *token.FileSet, god ast.Decl) Decl {
gnod := Go2Gno(fs, god)
if gnod == nil {
return nil
} else {
return gnod.(Decl)
}
}
func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) {
ds = make([]Decl, 0, len(gd.Specs))
/*
Within a parenthesized const declaration list the
expression list may be omitted from any but the
first ConstSpec. Such an empty list is equivalent
to the textual substitution of the first preceding
non-empty expression list and its type if any.
*/
var lastValues Exprs // (see Go iota spec above)
var lastType Expr // (see Go iota spec above)
for si, s := range gd.Specs {
switch s := s.(type) {
case *ast.TypeSpec:
name := toName(s.Name)
tipe := toExpr(fs, s.Type)
alias := s.Assign != 0
ds = append(ds, &TypeDecl{
NameExpr: NameExpr{Name: name},
Type: tipe,
IsAlias: alias,
})
case *ast.ValueSpec:
if gd.Tok == token.CONST {
var names []NameExpr
var tipe Expr
var values Exprs
for _, id := range s.Names {
names = append(names, *Nx(toName(id)))
}
if s.Type == nil {
tipe = lastType
} else {
tipe = toExpr(fs, s.Type)
lastType = tipe
}
if s.Values == nil {
values = copyExprs(lastValues)
} else {
values = toExprs(fs, s.Values)
lastValues = values
}
cd := &ValueDecl{
NameExprs: names,
Type: tipe,
Values: values,
Const: true,
}
cd.SetAttribute(ATTR_IOTA, si)
ds = append(ds, cd)
} else {
var names []NameExpr
var tipe Expr
var values Exprs
for _, id := range s.Names {
names = append(names, *Nx(toName(id)))
}
tipe = toExpr(fs, s.Type)
if s.Values != nil {
values = toExprs(fs, s.Values)
}
vd := &ValueDecl{
NameExprs: names,
Type: tipe,
Values: values,
Const: false,
}
ds = append(ds, vd)
}
case *ast.ImportSpec:
path, err := strconv.Unquote(s.Path.Value)
if err != nil {
panic("unexpected import spec path type")
}
ds = append(ds, &ImportDecl{
NameExpr: *Nx(toName(s.Name)),
PkgPath: path,
})
default:
panic(fmt.Sprintf(
"unexpected decl spec %v",
reflect.TypeOf(s)))
}
}
return ds
}
func toSimpleDeclStmts(fs *token.FileSet, gd *ast.GenDecl) (sds []Stmt) {
ds := toDecls(fs, gd)
sds = make([]Stmt, len(ds))
for i, d := range ds {
sds[i] = d.(SimpleDeclStmt).(Stmt)
}
return
}
func toFieldsFromList(fs *token.FileSet, fl *ast.FieldList) (ftxs []FieldTypeExpr) {
if fl == nil {
return nil
} else {
ftxs = toFields(fs, fl.List...)
return
}
}
func toFields(fs *token.FileSet, fields ...*ast.Field) (ftxs []FieldTypeExpr) {
if len(fields) == 0 {
return nil
}
ftxs = make([]FieldTypeExpr, 0, len(fields)) // may grow longer
for _, f := range fields {
if len(f.Names) == 0 {
// a single unnamed field w/ type
ftxs = append(ftxs, FieldTypeExpr{
Name: "",
Type: toExpr(fs, f.Type),
Tag: toExpr(fs, f.Tag),
})
} else {
// one or more named fields
for _, n := range f.Names {
ftxs = append(ftxs, FieldTypeExpr{
Name: toName(n),
Type: toExpr(fs, f.Type),
Tag: toExpr(fs, f.Tag),
})
}
}
}
return
}
func toKeyValueExprs(fs *token.FileSet, elts []ast.Expr) (kvxs KeyValueExprs) {
kvxs = make([]KeyValueExpr, len(elts))
for i, x := range elts {
if kvx, ok := x.(*ast.KeyValueExpr); ok {
kvxs[i] = *Go2Gno(fs, kvx).(*KeyValueExpr)
} else {
kvxs[i] = KeyValueExpr{
Key: nil,
Value: toExpr(fs, x),
}
}
}
return
}
// NOTE: moves the default clause to last.
func toClauses(fs *token.FileSet, csz []ast.Stmt) []SwitchClauseStmt {
res := make([]SwitchClauseStmt, 0, len(csz))
var dclause *SwitchClauseStmt
for _, cs := range csz {
clause := toSwitchClauseStmt(fs, cs.(*ast.CaseClause))
if len(clause.Cases) == 0 {
if dclause != nil {
panic("duplicate default clause")
}
dclause = &clause
} else {
res = append(res, clause)
}
}
if dclause != nil {
res = append(res, *dclause)
}
if len(res) != len(csz) {
panic("should not happen")
}
return res
}
func toSwitchClauseStmt(fs *token.FileSet, cc *ast.CaseClause) SwitchClauseStmt {
return SwitchClauseStmt{
Cases: toExprs(fs, cc.List),
Body: toStmts(fs, cc.Body),
}
}