-
Notifications
You must be signed in to change notification settings - Fork 28
/
compiler.go
224 lines (212 loc) · 5.5 KB
/
compiler.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
// Copyright 2021 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package slang
import (
"fmt"
"go/scanner"
"go/token"
"math"
"reflect"
"strconv"
"time"
)
func (scr *Script) compile(stmts statements) error {
if scr.symbols == nil {
scr.symbols = newSymbolTable()
}
scr.invocations = make([]invocation, 0, len(stmts))
errs := scanner.ErrorList{}
for _, stmt := range stmts {
inv, err := compileStatement(scr.symbols, stmt)
if err != nil {
if err.Pos == stmt.pos {
errs.Add(stmt.pos, err.Msg)
} else {
errs.Add(stmt.pos, err.Error())
}
continue
}
scr.invocations = append(scr.invocations, inv)
}
return errs.Err()
}
func parseBool(v string) (bool, error) {
switch v {
case "true":
return true, nil
case "false":
return false, nil
}
return false, fmt.Errorf("must be one of 'true' or 'false'")
}
func unquote(s string) string {
uq, err := strconv.Unquote(s)
if err != nil {
return s
}
return uq
}
func handleLiteral(lit tokPos, tt reflect.Type) (reflect.Value, error) {
switch lit.tok {
case token.INT:
iv, err := strconv.ParseInt(lit.lit, 10, 64)
if err != nil {
// The go parser has already made sure this token is an int.
panic(fmt.Sprintf("%v: not an int: %v", lit.lit, err))
}
if iv > math.MaxInt {
return reflect.ValueOf(iv), nil
}
return reflect.ValueOf(int(iv)), nil
case token.IDENT:
bv, err := parseBool(lit.lit)
if err == nil {
return reflect.ValueOf(bv), nil
}
if tt.Kind() == reflect.Bool {
return reflect.Value{}, fmt.Errorf("not a bool: must be one of 'true' or 'false'")
}
// Turn function names into strings.
if _, ok := supportedVerbs[lit.lit]; ok {
return reflect.ValueOf(lit.lit), nil
}
return reflect.Value{}, fmt.Errorf("invalid identifier")
case token.STRING:
uq := unquote(lit.lit)
if tt == reflect.TypeOf(time.Hour) {
d, err := time.ParseDuration(uq)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(d), nil
}
return reflect.ValueOf(uq), nil
}
panic(fmt.Sprintf("unsupported literal type: expected %v", tt))
}
func compileResults(symbols *symbolTable, td reflect.Type, results []tokPos, assignment bool) ([]value, *scanner.Error) {
resultValues := []value{}
for i, r := range results {
name := r.lit
et, existing := symbols.types[name]
if existing && symbols.constants[name] {
return nil, &scanner.Error{
Pos: r.pos,
Msg: fmt.Sprintf("cannot assign or define result '%v' which is a constant", name),
}
}
tt := td.Out(i)
if !existing && assignment {
return nil, &scanner.Error{
Pos: r.pos,
Msg: fmt.Sprintf("result '%v' is not defined", name),
}
}
if existing && !assignment {
if et != tt {
return nil, &scanner.Error{
Pos: r.pos,
Msg: fmt.Sprintf("result '%v' is redefined with a new type %v, previous type %v", name, tt, et),
}
}
return nil, &scanner.Error{
Pos: r.pos,
Msg: fmt.Sprintf("result '%v' is redefined", name),
}
}
if !assignment {
symbols.types[name] = tt
}
resultValues = append(resultValues, variableValue{name: name, hasEllipsis: false})
}
return resultValues, nil
}
func compileArguments(symbols *symbolTable, td reflect.Type, args []tokPos) ([]value, *scanner.Error) {
argValues := []value{}
for i, a := range args {
name := a.lit
et, existing := symbols.types[name]
var tt reflect.Type
if td.IsVariadic() && i+1 >= td.NumIn()-1 {
tt = td.In(td.NumIn() - 1)
if !a.ellipsis {
tt = tt.Elem()
}
} else {
tt = td.In(i + 1)
}
if existing {
if !et.AssignableTo(tt) {
return nil, &scanner.Error{
Pos: a.pos,
Msg: fmt.Sprintf("arg '%v' is of the wrong type %v, should be %v", name, et, tt),
}
}
argValues = append(argValues, variableValue{name: name, hasEllipsis: a.ellipsis})
continue
}
// Must be a literal.
val, err := handleLiteral(a, tt)
if err != nil {
return nil, &scanner.Error{
Pos: a.pos,
Msg: fmt.Sprintf("arg '%v' is an invalid literal: %v", name, err),
}
}
if !val.Type().AssignableTo(tt) {
return nil, &scanner.Error{
Pos: a.pos,
Msg: fmt.Sprintf("literal arg '%v' of type %v is not assignable to %v", name, val.Type(), tt),
}
}
argValues = append(argValues, literalValue(val))
}
return argValues, nil
}
func compileStatement(symbols *symbolTable, stmt statement) (invocation, *scanner.Error) {
inv := invocation{pos: stmt.pos}
v, ok := supportedVerbs[stmt.verbName.lit]
if !ok {
return inv, &scanner.Error{
Pos: inv.pos,
Msg: fmt.Sprintf("unrecognised function name: %s", stmt.verbName.lit),
}
}
td := v.protoype
if len(stmt.results)+1 != td.NumOut() {
return inv, &scanner.Error{
Pos: inv.pos,
Msg: fmt.Sprintf("wrong # of results for %v", v),
}
}
if len(stmt.args)+1 != td.NumIn() {
if !td.IsVariadic() {
return inv, &scanner.Error{
Pos: inv.pos,
Msg: fmt.Sprintf("wrong # of arguments for %v", v),
}
}
if len(stmt.args)+1 < td.NumIn()-1 {
return inv, &scanner.Error{
Pos: inv.pos,
Msg: fmt.Sprintf("need at least %v arguments for variadic function %v", td.NumIn()-2, v),
}
}
}
resultValues, err := compileResults(symbols, td, stmt.results, stmt.assign)
if err != nil {
return inv, err
}
argValues, err := compileArguments(symbols, td, stmt.args)
if err != nil {
return inv, err
}
return invocation{
pos: stmt.pos,
verb: v,
results: resultValues,
args: argValues,
assignment: stmt.assign,
}, nil
}