forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_eval.go
402 lines (395 loc) · 9.32 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
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
package gno
import (
"fmt"
"math"
"math/big"
"regexp"
"strconv"
"strings"
"github.com/cockroachdb/apd"
)
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:
if matched, _ := regexp.MatchString(`^[0-9\.]+([eE][\-\+]?[0-9]+)?$`, x.Value); matched {
value := x.Value
bd, c, err := apd.NewFromString(value)
if err != nil {
panic(fmt.Sprintf(
"invalid decimal constant: %s",
x.Value))
}
if c.Inexact() {
panic(fmt.Sprintf(
"could not represent decimal exactly: %s",
x.Value))
}
m.PushValue(TypedValue{
T: UntypedBigdecType,
V: BigdecValue{V: bd},
})
return
} else if matched, _ := regexp.MatchString(`^0[xX][0-9a-fA-F\.]+([pP][\-\+]?[0-9a-fA-F]+)?$`, x.Value); matched {
originalInput := x.Value
value := x.Value[2:]
var hexString string
var exp int64
eIndex := strings.IndexAny(value, "Pp")
if eIndex == -1 {
panic("should not happen")
}
//----------------------------------------
// NewFromHexString()
// TODO: move this to another function.
// Step 1 get exp component.
expInt, err := strconv.ParseInt(value[eIndex+1:], 10, 32)
if err != nil {
if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrRange {
panic(fmt.Sprintf(
"can't convert %s to decimal: fractional part too long",
value))
}
panic(fmt.Sprintf(
"can't convert %s to decimal: exponent is not numeric",
value))
}
value = value[:eIndex]
exp = expInt
// Step 2 adjust exp from dot.
pIndex := -1
vLen := len(value)
for i := 0; i < vLen; i++ {
if value[i] == '.' {
if pIndex > -1 {
panic(fmt.Sprintf(
"can't convert %s to decimal: too many .s",
value))
}
pIndex = i
}
}
if pIndex == -1 {
// There is no decimal point, we can just parse the original string as
// a hex
hexString = value
} else {
if pIndex+1 < vLen {
hexString = value[:pIndex] + value[pIndex+1:]
} else {
hexString = value[:pIndex]
}
expInt := -len(value[pIndex+1:])
exp += int64(expInt)
}
bexp := apd.New(0, 0)
_, err = apd.BaseContext.WithPrecision(1024).Pow(
bexp,
apd.New(2, 0),
apd.New(exp, 0))
if err != nil {
panic(fmt.Sprintf("error computing exponent: %v", err))
}
// Step 3 make Decimal from mantissa and exp.
var dValue *big.Int
dValue = new(big.Int)
_, ok := dValue.SetString(hexString, 16)
if !ok {
panic(fmt.Sprintf("can't convert %s to decimal", value))
}
if exp < math.MinInt32 || exp > math.MaxInt32 {
// NOTE(vadim): I doubt a string could realistically be this long
panic(fmt.Sprintf("can't convert %s to decimal: fractional part too long", originalInput))
}
res := apd.New(0, 0)
_, err = apd.BaseContext.WithPrecision(1024).Mul(
res,
apd.NewWithBigInt(dValue, 0),
bexp)
if err != nil {
panic(fmt.Sprintf("canot calculate hexadecimal: %v", err))
}
// NewFromHexString() END
//----------------------------------------
m.PushValue(TypedValue{
T: UntypedBigdecType,
V: BigdecValue{V: res},
})
return
} else {
panic(fmt.Sprintf("unexpected decimal/float format %s", x.Value))
}
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: m.Alloc.NewString(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)
}
// evaluate 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)
// evaluate max
if x.Max != nil {
m.PushExpr(x.Max)
m.PushOp(OpEval)
}
// evaluate high
if x.High != nil {
m.PushExpr(x.High)
m.PushOp(OpEval)
}
// evaluate low
if x.Low != nil {
m.PushExpr(x.Low)
m.PushOp(OpEval)
}
// evaluate 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))
}
}