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
|
package parser
import (
"errors"
"fmt"
"strings"
"github.com/mvertes/parscan/lang"
"github.com/mvertes/parscan/vm"
)
type typeFlag int
const (
parseTypeIn typeFlag = iota
parseTypeOut
parseTypeVar
parseTypeType
)
// Type parsing error definitions.
var (
ErrFuncType = errors.New("invalid function type")
ErrInvalidType = errors.New("invalid type")
ErrMissingType = errors.New("missing type")
ErrSize = errors.New("invalid size")
ErrSyntax = errors.New("syntax error")
ErrNotImplemented = errors.New("not implemented")
)
func (p *Parser) parseTypeExpr(in Tokens) (typ *vm.Type, err error) {
switch in[0].Tok {
case lang.BracketBlock:
typ, err := p.parseTypeExpr(in[1:])
if err != nil {
return nil, err
}
if b := in[0].Block(); len(b) > 0 {
x, err := p.Scan(b, false)
if err != nil {
return nil, err
}
cval, _, err := p.evalConstExpr(x)
if err != nil {
return nil, err
}
size, ok := constValue(cval).(int)
if !ok {
return nil, ErrSize
}
return vm.ArrayOf(size, typ), nil
}
return vm.SliceOf(typ), nil
case lang.Mul:
typ, err := p.parseTypeExpr(in[1:])
if err != nil {
return nil, err
}
return vm.PointerTo(typ), nil
case lang.Func:
// Get argument and return token positions depending on function pattern:
// method with receiver, named function or anonymous closure.
// TODO: handle variadics
var out Tokens
var indexArgs int
switch l, in1 := len(in), in[1]; {
case l >= 4 && in1.Tok == lang.ParenBlock && in[2].Tok == lang.Ident:
indexArgs, out = 3, in[4:]
case l >= 3 && in1.Tok == lang.Ident:
indexArgs, out = 2, in[3:]
case l >= 2 && in1.Tok == lang.ParenBlock:
indexArgs, out = 1, in[2:]
default:
return nil, ErrFuncType
}
// We can now parse function input and output parameter types.
// Input parameters are always enclosed by parenthesis.
iargs, err := p.Scan(in[indexArgs].Block(), false)
if err != nil {
return nil, err
}
arg, _, err := p.parseParamTypes(iargs, parseTypeIn)
if err != nil {
return nil, err
}
// Output parameters may be empty, or enclosed or not by parenthesis.
if len(out) == 1 && out[0].Tok == lang.ParenBlock {
if out, err = p.Scan(out[0].Block(), false); err != nil {
return nil, err
}
}
ret, _, err := p.parseParamTypes(out, parseTypeOut)
if err != nil {
return nil, err
}
return vm.FuncOf(arg, ret, false), nil
case lang.Ident:
// TODO: selector expression (pkg.type)
s, _, ok := p.getSym(in[0].Str, p.scope)
if !ok || s.kind != symType {
return nil, fmt.Errorf("%w: %s", ErrInvalidType, in[0].Str)
}
return s.typ, nil
case lang.Struct:
if len(in) != 2 || in[1].Tok != lang.BraceBlock {
return nil, fmt.Errorf("%w: %v", ErrSyntax, in)
}
if in, err = p.Scan(in[1].Block(), false); err != nil {
return nil, err
}
var fields []*vm.Type
for _, lt := range in.Split(lang.Semicolon) {
types, names, err := p.parseParamTypes(lt, parseTypeType)
if err != nil {
return nil, err
}
for i, name := range names {
fields = append(fields, &vm.Type{Name: name, PkgPath: p.pkgName, Rtype: types[i].Rtype})
// TODO: handle embedded fields
}
}
return vm.StructOf(fields), nil
default:
return nil, fmt.Errorf("%w: %v", ErrNotImplemented, in[0].Name())
}
}
// parseParamTypes parses a list of comma separated typed parameters and returns a list of
// runtime types. Implicit parameter names and types are supported.
func (p *Parser) parseParamTypes(in Tokens, flag typeFlag) (types []*vm.Type, vars []string, err error) {
// Parse from right to left, to allow multiple comma separated parameters of the same type.
list := in.Split(lang.Comma)
for i := len(list) - 1; i >= 0; i-- {
t := list[i]
if len(t) == 0 {
continue
}
param := ""
local := p.funcScope != ""
if p.hasFirstParam(t) {
param = strings.TrimPrefix(p.scope+"/"+t[0].Str, "/")
t = t[1:]
if len(t) == 0 {
if len(types) == 0 {
return nil, nil, ErrMissingType
}
// Type was omitted, apply the previous one from the right.
types = append([]*vm.Type{types[0]}, types...)
p.addSymVar(i, param, types[0], flag, local)
vars = append(vars, param)
continue
}
}
typ, err := p.parseTypeExpr(t)
if err != nil {
return nil, nil, err
}
if param != "" {
p.addSymVar(i, param, typ, flag, local)
}
types = append([]*vm.Type{typ}, types...)
vars = append(vars, param)
}
return types, vars, err
}
func (p *Parser) addSymVar(index int, name string, typ *vm.Type, flag typeFlag, local bool) {
zv := vm.NewValue(typ)
switch flag {
case parseTypeIn:
p.addSym(-index-2, name, zv, symVar, typ, true)
case parseTypeOut:
p.addSym(p.framelen[p.funcScope], name, zv, symVar, typ, true)
p.framelen[p.funcScope]++
case parseTypeVar:
if !local {
p.addSym(unsetAddr, name, zv, symVar, typ, local)
break
}
p.addSym(p.framelen[p.funcScope], name, zv, symVar, typ, local)
p.framelen[p.funcScope]++
}
}
// hasFirstParam returns true if the first token of a list is a parameter name.
func (p *Parser) hasFirstParam(in Tokens) bool {
if in[0].Tok != lang.Ident {
return false
}
s, _, ok := p.getSym(in[0].Str, p.scope)
return !ok || s.kind != symType
}
// typeStartIndex returns the index of the start of type expression in tokens, or -1.
func (p *Parser) typeStartIndex(in Tokens) int {
index := len(in) - 1
for i := index; i >= 0; i-- {
switch in[i].Tok {
case lang.Ident, lang.Struct, lang.Map, lang.Func, lang.Interface, lang.Mul, lang.BraceBlock, lang.BracketBlock, lang.ParenBlock:
index = i
default:
return index
}
}
return -1
}
|