-
Notifications
You must be signed in to change notification settings - Fork 7
/
evalbuiltin.go
195 lines (174 loc) · 4.89 KB
/
evalbuiltin.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
package eval
import (
"reflect"
)
func evalCallBuiltinExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
ident := call.Fun.(*Ident)
switch ident.Name {
case "complex":
return evalBuiltinComplexExpr(call, env)
case "real":
return evalBuiltinRealExpr(call, env)
case "imag":
return evalBuiltinImagExpr(call, env)
case "new":
return evalBuiltinNewExpr(call, env)
case "make":
return evalBuiltinMakeExpr(call, env)
case "len":
return evalBuiltinLenExpr(call, env)
case "cap":
return evalBuiltinCapExpr(call, env)
case "append":
return evalBuiltinAppendExpr(call, env)
case "copy":
return evalBuiltinCopyExpr(call, env)
case "delete":
return evalBuiltinDeleteExpr(call, env)
case "panic":
return evalBuiltinPanicExpr(call, env)
default:
panic("eval: unimplemented builtin " + ident.Name)
}
}
func evalBuiltinComplexExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
var err error
resT := call.KnownType()[0]
argT := knownType{comprisingFloatType(resT)}
var re, im []reflect.Value
if re, err = evalTypedExpr(call.Args[0], argT, env); err != nil {
return nil, err
} else if im, err = evalTypedExpr(call.Args[1], argT, env); err != nil {
return nil, err
}
cplx := builtinComplex(re[0], im[0])
return []reflect.Value{cplx}, nil
}
func evalBuiltinRealExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
var err error
resT := call.KnownType()[0]
argT := knownType{comprisingFloatType(resT)}
var cplx []reflect.Value
if cplx, err = evalTypedExpr(call.Args[0], argT, env); err != nil {
return nil, err
}
re := builtinReal(cplx[0])
return []reflect.Value{re}, nil
}
func evalBuiltinImagExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
var err error
resT := call.KnownType()[0]
argT := knownType{comprisingFloatType(resT)}
var cplx []reflect.Value
if cplx, err = evalTypedExpr(call.Args[0], argT, env); err != nil {
return nil, err
}
im := builtinImag(cplx[0])
return []reflect.Value{im}, nil
}
func evalBuiltinNewExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
resT := call.KnownType()[0]
ptr := builtinNew(resT.Elem())
return []reflect.Value{ptr}, nil
}
func evalBuiltinMakeExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
resT := call.KnownType()[0]
length, capacity := 0, 0
var err error
if len(call.Args) > 1 {
if length, err = evalInteger(call.Args[1], env); err != nil {
return nil, err
}
}
if len(call.Args) > 2 {
if capacity, err = evalInteger(call.Args[2], env); err != nil {
return nil, err
}
}
var res reflect.Value
switch resT.Kind() {
case reflect.Slice:
res = reflect.MakeSlice(resT, length, capacity)
case reflect.Map:
res = reflect.MakeMap(resT)
case reflect.Chan:
res = reflect.MakeChan(resT, length)
default:
panic(dytc("make(bad type)"))
}
return []reflect.Value{res}, nil
}
func evalBuiltinLenExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
if arg0, err := EvalExpr(call.Args[0], env); err != nil {
return nil, err
} else {
length := builtinLen(arg0[0])
return []reflect.Value{length}, nil
}
}
func evalBuiltinCapExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
if arg0, err := EvalExpr(call.Args[0], env); err != nil {
return nil, err
} else {
capacity := builtinCap(arg0[0])
return []reflect.Value{capacity}, nil
}
}
func evalBuiltinAppendExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
sliceT := call.KnownType()
head, err := evalTypedExpr(call.Args[0], sliceT, env)
if err != nil {
return nil, err
}
var tail reflect.Value
if call.argNEllipsis {
xs, err := EvalExpr(call.Args[1], env)
if err != nil {
return nil, err
}
tail = xs[0]
} else {
numXs := len(call.Args) - 1
tail = reflect.MakeSlice(sliceT[0], numXs, numXs)
xT := knownType{sliceT[0].Elem()}
for i := 1; i < len(call.Args); i += 1 {
if x, err := evalTypedExpr(call.Args[i], xT, env); err != nil {
return nil, err
} else {
tail.Index(i-1).Set(x[0])
}
}
}
res := builtinAppend(head[0], tail)
return []reflect.Value{res}, nil
}
func evalBuiltinCopyExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
if x, err := EvalExpr(call.Args[0], env); err != nil {
return nil, err
} else if y, err := EvalExpr(call.Args[1], env); err != nil {
return nil, err
} else {
n := builtinCopy(x[0], y[0])
return []reflect.Value{n}, nil
}
}
func evalBuiltinDeleteExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
m := call.Args[0]
mT := m.KnownType()[0]
if x, err := EvalExpr(m, env); err != nil {
return nil, err
} else if y, err := evalTypedExpr(call.Args[1], knownType{mT.Key()}, env); err != nil {
return nil, err
} else {
builtinDelete(x[0], y[0])
return []reflect.Value{}, nil
}
}
func evalBuiltinPanicExpr(call *CallExpr, env Env) ([]reflect.Value, error) {
if arg0, err := evalTypedExpr(call.Args[0], knownType{emptyInterface}, env); err != nil {
return nil, err
} else {
err := builtinPanic(arg0[0])
return []reflect.Value{}, err
}
}