forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_eval.go
334 lines (332 loc) · 7.46 KB
/
op_eval.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
package gno
import (
"fmt"
"math/big"
"regexp"
"strconv"
"strings"
)
func (m *Machine) doOpEval() {
x := m.PeekExpr(1)
if debug {
debug.Printf("EVAL: %v\n", x)
//fmt.Println(m.String())
}
// This case moved out of switch for performance.
// TODO: understand this better.
if nx, ok := x.(*NameExpr); ok {
m.PopExpr()
if nx.Path.Depth == 0 {
// Name is in uverse (global).
gv := Uverse().GetBlock(nil).GetPointerTo(nil, nx.Path)
m.PushValue(gv.Deref())
return
} else {
// Get value from scope.
lb := m.LastBlock()
// Push value, done.
ptr := lb.GetPointerTo(m.Store, nx.Path)
m.PushValue(ptr.Deref())
return
}
}
switch x := x.(type) {
// case NameExpr: handled above
case *BasicLitExpr:
m.PopExpr()
switch x.Kind {
case INT:
// temporary optimization
bi := big.NewInt(0)
// TODO optimize.
// TODO deal with base.
if len(x.Value) > 2 && x.Value[0] == '0' {
var ok bool = false
switch x.Value[1] {
case 'b', 'B':
_, ok = bi.SetString(x.Value[2:], 2)
case 'o', 'O':
_, ok = bi.SetString(x.Value[2:], 8)
case 'x', 'X':
_, ok = bi.SetString(x.Value[2:], 16)
default:
ok = false
}
if !ok {
panic(fmt.Sprintf(
"invalid integer constant: %s",
x.Value))
}
} else {
_, ok := bi.SetString(x.Value, 10)
if !ok {
panic(fmt.Sprintf(
"invalid integer constant: %s",
x.Value))
}
}
m.PushValue(TypedValue{
T: UntypedBigintType,
V: BigintValue{V: bi},
})
case FLOAT:
// Special case if ieee notation of integer type.
if matched, _ := regexp.MatchString(`[\-\+]?[0-9]+e[0-9]+`, x.Value); matched {
value := x.Value
isNeg := false
if x.Value[0] == '-' {
isNeg = true
value = x.Value[1:]
} else if x.Value[0] == '+' {
isNeg = false
value = x.Value[1:]
}
parts := strings.SplitN(value, "e", 2)
if len(parts) != 2 {
panic(fmt.Sprintf(
"invalid integer constant: %s",
x.Value))
}
first, err := strconv.Atoi(parts[0])
if err != nil {
panic(fmt.Sprintf(
"invalid integer constant: %s",
x.Value))
}
second, err := strconv.Atoi(parts[1])
if err != nil {
panic(fmt.Sprintf(
"invalid integer constant: %s",
x.Value))
}
bi := big.NewInt(0)
bi = bi.Exp(big.NewInt(10), big.NewInt(int64(second)), nil)
bi = bi.Mul(bi, big.NewInt(int64(first)))
if isNeg {
bi = bi.Mul(bi, big.NewInt(-1))
}
m.PushValue(TypedValue{
T: UntypedBigintType,
V: BigintValue{V: bi},
})
return
} else {
// NOTE: I suspect we won't get hardware-level
// consistency (determinism) in floating point numbers
// yet, so hold off on this until we master this.
panic(fmt.Sprintf("floats are not supported: %v", x.String()))
}
case IMAG:
// NOTE: this is a syntax and grammar problem, not an
// AST one. Imaginaries should get evaluated as a
// type like any other. See
// github.com/Quasilyte/go-complex-nums-emulation
// and github.com/golang/go/issues/19921
panic("imaginaries are not supported")
case CHAR:
cstr, err := strconv.Unquote(x.Value)
if err != nil {
panic("error in parsing character literal: " + err.Error())
}
runes := []rune(cstr)
if len(runes) != 1 {
panic(fmt.Sprintf("error in parsing character literal: 1 rune expected, but got %v (%s)", len(runes), cstr))
}
tv := TypedValue{T: UntypedRuneType}
tv.SetInt32(int32(rune(runes[0])))
m.PushValue(tv)
case STRING:
m.PushValue(TypedValue{
T: UntypedStringType,
V: StringValue(x.GetString()),
})
default:
panic(fmt.Sprintf("unexpected lit kind %v", x.Kind))
}
case *BinaryExpr:
switch x.Op {
case LAND, LOR:
m.PushOp(OpBinary1)
// evaluate left
m.PushExpr(x.Left)
m.PushOp(OpEval)
default:
op := word2BinaryOp(x.Op)
m.PushOp(op)
// alt: m.PushOp(OpBinary2)
// evaluate right
m.PushExpr(x.Right)
m.PushOp(OpEval)
// evaluate left
m.PushExpr(x.Left)
m.PushOp(OpEval)
}
case *CallExpr:
m.PushOp(OpPrecall)
// Eval args.
args := x.Args
for i := len(args) - 1; 0 <= i; i-- {
m.PushExpr(args[i])
m.PushOp(OpEval)
}
// evaluate func
m.PushExpr(x.Func)
m.PushOp(OpEval)
case *IndexExpr:
if x.HasOK {
m.PushOp(OpIndex2)
} else {
m.PushOp(OpIndex1)
}
// evalaute index
m.PushExpr(x.Index)
m.PushOp(OpEval)
// evaluate x
m.PushExpr(x.X)
m.PushOp(OpEval)
case *SelectorExpr:
m.PushOp(OpSelector)
// evaluate x
m.PushExpr(x.X)
m.PushOp(OpEval)
case *SliceExpr:
m.PushOp(OpSlice)
// evalaute max
if x.Max != nil {
m.PushExpr(x.Max)
m.PushOp(OpEval)
}
// evalaute high
if x.High != nil {
m.PushExpr(x.High)
m.PushOp(OpEval)
}
// evalaute low
if x.Low != nil {
m.PushExpr(x.Low)
m.PushOp(OpEval)
}
// evalaute x
m.PushExpr(x.X)
m.PushOp(OpEval)
case *StarExpr:
m.PopExpr()
m.PushOp(OpStar)
// evaluate x.
m.PushExpr(x.X)
m.PushOp(OpEval)
case *RefExpr:
m.PushOp(OpRef)
// evaluate x
m.PushForPointer(x.X)
case *UnaryExpr:
op := word2UnaryOp(x.Op)
m.PushOp(op)
// evaluate x
m.PushExpr(x.X)
m.PushOp(OpEval)
case *CompositeLitExpr:
m.PushOp(OpCompositeLit)
// evaluate type
m.PushExpr(x.Type)
m.PushOp(OpEval)
case *FuncLitExpr:
m.PushOp(OpFuncLit)
// evaluate func type
m.PushExpr(&x.Type)
m.PushOp(OpEval)
case *ConstExpr:
m.PopExpr()
// push preprocessed value
m.PushValue(x.TypedValue)
case *constTypeExpr:
m.PopExpr()
// push preprocessed type as value
m.PushValue(asValue(x.Type))
case *FieldTypeExpr:
m.PushOp(OpFieldType)
// evaluate field type
m.PushExpr(x.Type)
m.PushOp(OpEval)
// evaluate tag?
if x.Tag != nil {
m.PushExpr(x.Tag)
m.PushOp(OpEval)
}
case *ArrayTypeExpr:
m.PushOp(OpArrayType)
// evaluate length if set
if x.Len != nil {
m.PushExpr(x.Len)
m.PushOp(OpEval) // OpEvalPrimitive?
}
// evaluate elem type
m.PushExpr(x.Elt)
m.PushOp(OpEval) // OpEvalType?
case *SliceTypeExpr:
m.PushOp(OpSliceType)
// evaluate elem type
m.PushExpr(x.Elt)
m.PushOp(OpEval) // OpEvalType?
case *InterfaceTypeExpr:
m.PushOp(OpInterfaceType)
// evaluate methods
for i := len(x.Methods) - 1; 0 <= i; i-- {
m.PushExpr(&x.Methods[i])
m.PushOp(OpEval)
}
case *FuncTypeExpr:
// NOTE params and results are evaluated in
// the parent scope.
m.PushOp(OpFuncType)
// evaluate results (after params)
for i := len(x.Results) - 1; 0 <= i; i-- {
m.PushExpr(&x.Results[i])
m.PushOp(OpEval)
}
// evaluate params
for i := len(x.Params) - 1; 0 <= i; i-- {
m.PushExpr(&x.Params[i])
m.PushOp(OpEval)
}
case *MapTypeExpr:
m.PopExpr()
m.PushOp(OpMapType)
// evaluate value type
m.PushExpr(x.Value)
m.PushOp(OpEval) // OpEvalType?
// evaluate key type
m.PushExpr(x.Key)
m.PushOp(OpEval) // OpEvalType?
case *StructTypeExpr:
m.PushOp(OpStructType)
// evaluate fields
for i := len(x.Fields) - 1; 0 <= i; i-- {
m.PushExpr(&x.Fields[i])
m.PushOp(OpEval)
}
case *TypeAssertExpr:
if x.HasOK {
m.PushOp(OpTypeAssert2)
} else {
m.PushOp(OpTypeAssert1)
}
// evaluate type
m.PushExpr(x.Type)
m.PushOp(OpEval)
// evaluate x
m.PushExpr(x.X)
m.PushOp(OpEval)
case *ChanTypeExpr:
m.PushOp(OpChanType)
m.PushExpr(x.Value)
m.PushOp(OpEval) // OpEvalType?
case *MaybeNativeTypeExpr:
m.PopExpr()
m.PushOp(OpMaybeNativeType)
m.PushExpr(x.Type)
m.PushOp(OpEval)
default:
panic(fmt.Sprintf("unexpected expression %#v", x))
}
}