forked from glycerine/zygomys
/
func.go
295 lines (247 loc) · 7.24 KB
/
func.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
package zygo
import (
"fmt"
)
func FuncBuilder(env *Zlisp, name string,
args []Sexp) (Sexp, error) {
useName := name
isMethod := false
if name == "method" {
isMethod = true
useName = "method [p: (* StructName)]"
}
use := "use: (" + useName + " funcName [inputs:type ...] [returns:type ...])"
n := len(args)
if n < 1 {
return SexpNull, fmt.Errorf("missing arguments. %s", use)
}
inputsLoc := 1
returnsLoc := 2
bodyLoc := 3
isAnon := false
var symN *SexpSymbol
switch b := args[0].(type) {
case *SexpSymbol:
symN = b
case *SexpPair:
sy, isQuo := isQuotedSymbol(b)
if isQuo {
symN = sy.(*SexpSymbol)
} else {
return SexpNull, fmt.Errorf("bad func name: symbol required")
}
case *SexpArray:
if isMethod {
ok := false
symN, ok = args[1].(*SexpSymbol)
if !ok {
return SexpNull, fmt.Errorf("bad method name: symbol required after receiver array")
}
inputsLoc++
returnsLoc++
bodyLoc++
} else {
// anonymous function
symN = env.GenSymbol("__anon")
isAnon = true
inputsLoc--
returnsLoc--
bodyLoc--
}
default:
return SexpNull, fmt.Errorf("bad func name: symbol required")
}
Q("good: have func name '%v'", symN.name)
funcName := symN.name
builtin, builtTyp := env.IsBuiltinSym(symN)
if builtin {
return SexpNull,
fmt.Errorf("already have %s '%s', refusing to overwrite with defn",
builtTyp, symN.name)
}
if env.HasMacro(symN) {
return SexpNull, fmt.Errorf("Already have macro named '%s': refusing"+
" to define function of same name.", symN.name)
}
if n < inputsLoc+1 {
return SexpNull, fmt.Errorf("func [inputs] array is missing. %s", use)
}
if n < returnsLoc+1 {
return SexpNull, fmt.Errorf("func [returns] array is missing. %s", use)
}
var inputs *SexpArray
switch ar := args[inputsLoc].(type) {
default:
return SexpNull, fmt.Errorf("bad func declaration '%v': "+
"expected array of input declarations after the name. %s", funcName, use)
case *SexpArray:
inputs = ar
inputs.IsFuncDeclTypeArray = true
}
var returns *SexpArray
switch ar := args[returnsLoc].(type) {
default:
return SexpNull, fmt.Errorf("bad func declaration '%v': third argument "+
"must be a array of return declarations. %s", funcName, use)
case *SexpArray:
returns = ar
returns.IsFuncDeclTypeArray = true
}
body := args[bodyLoc:]
Q("in func builder, args = ")
for i := range args {
Q("args[%v] = '%s'", i, args[i].SexpString(nil))
}
Q("in func builder, isAnon = %v", isAnon)
Q("in func builder, inputs = %v", inputs.SexpString(nil))
Q("in func builder, returns = %v", returns.SexpString(nil))
Q("in func builder, body = %v", (&SexpArray{Val: body, Env: env}).SexpString(nil))
inHash, err := GetFuncArgArray(inputs, env, "inputs")
if err != nil {
return SexpNull, fmt.Errorf("inputs array parsing error: %v", err)
}
Q("inHash = '%v'", inHash.SexpString(nil))
retHash, err := GetFuncArgArray(returns, env, "returns")
if err != nil {
return SexpNull, fmt.Errorf("returns array parsing error: %v", err)
}
Q("retHash = '%v'", retHash.SexpString(nil))
env.datastack.PushExpr(SexpNull)
Q("FuncBuilder() about to call buildSexpFun")
// ===================================
// ===================================
//
// from buildSexpFun, adapted
//
// todo: type checking the inputs and handling the returns as well
//
// ===================================
// ===================================
// sfun, err := buildSexpFun(env, symN.name, funcargs, body, orig)
//orig := &SexpArray{Val: args, Env: env}
origa := []Sexp{env.MakeSymbol("func")}
origa = append(origa, args...)
orig := MakeList(origa)
funcargs := inHash.KeyOrder
gen := NewGenerator(env)
gen.Tail = true
gen.funcname = funcName
afsHelper := &AddFuncScopeHelper{}
gen.AddInstruction(AddFuncScopeInstr{Name: "runtime " + gen.funcname, Helper: afsHelper})
argsyms := make([]*SexpSymbol, len(funcargs))
// copy
for i := range funcargs {
argsyms[i] = funcargs[i].(*SexpSymbol)
}
varargs := false
nargs := len(funcargs)
if len(argsyms) >= 2 && argsyms[len(argsyms)-2].name == "&" {
argsyms[len(argsyms)-2] = argsyms[len(argsyms)-1]
argsyms = argsyms[0 : len(argsyms)-1]
varargs = true
nargs = len(argsyms) - 1
}
VPrintf("\n in buildSexpFun(): DumpFunction just before %v args go onto stack\n",
len(argsyms))
if Working {
DumpFunction(ZlispFunction(gen.instructions), -1)
}
for i := len(argsyms) - 1; i >= 0; i-- {
gen.AddInstruction(PopStackPutEnvInstr{argsyms[i]})
}
err = gen.GenerateBegin(body)
if err != nil {
return MissingFunction, err
}
// minimal sanity check that we return the number of arguments
// on the stack that are declared
if len(body) == 0 {
for range retHash.KeyOrder {
gen.AddInstruction(PushInstr{expr: SexpNull})
}
}
gen.AddInstruction(RemoveScopeInstr{})
gen.AddInstruction(ReturnInstr{nil}) // nil is the error returned
newfunc := ZlispFunction(gen.instructions)
sfun := gen.env.MakeFunction(gen.funcname, nargs,
varargs, newfunc, orig)
sfun.inputTypes = inHash
sfun.returnTypes = retHash
// tell the function scope where their function is, to
// provide access to the captured-closure scopes at runtime.
afsHelper.MyFunction = sfun
clos := CreateClosureInstr{sfun}
notePc := env.pc
clos.Execute(env)
invok, err := env.datastack.PopExpr()
panicOn(err) // we just pushed in the clos.Execute(), so this should always be err == nil
env.pc = notePc
err = env.LexicalBindSymbol(symN, invok)
if err != nil {
return SexpNull, fmt.Errorf("internal error: could not bind symN:'%s' into env: %v", symN.name, err)
}
if len(body) > 0 {
invok.(*SexpFunction).hasBody = true
}
return invok, nil
}
func GetFuncArgArray(arr *SexpArray, env *Zlisp, where string) (*SexpHash, error) {
ar := arr.Val
n := len(ar)
hash, err := MakeHash([]Sexp{}, "hash", env)
panicOn(err)
if n == 0 {
return hash, nil
}
if n%2 != 0 {
return nil, fmt.Errorf("func definintion's %s array must have an even number of elements (each name:type pair counts as two)", where)
}
for i := 0; i < n; i += 2 {
name := ar[i]
typ := ar[i+1]
//P("name = %#v", name)
//P("typ = %#v", typ)
var symN *SexpSymbol
switch b := name.(type) {
case *SexpSymbol:
symN = b
case *SexpPair:
sy, isQuo := isQuotedSymbol(b)
if isQuo {
symN = sy.(*SexpSymbol)
} else {
return nil, fmt.Errorf("bad formal parameter name: symbol required in %s array, not a symbol: '%s'",
where, b.SexpString(nil))
}
}
var symTyp *SexpSymbol
switch b := typ.(type) {
case *SexpSymbol:
symTyp = b
case *SexpPair:
sy, isQuo := isQuotedSymbol(b)
if isQuo {
symTyp = sy.(*SexpSymbol)
} else {
return nil, fmt.Errorf("bad formal parameter type: type required in %s array, but found '%s'",
where, b.SexpString(nil))
}
}
//P("here is env.ShowGlobalStack():")
//env.ShowGlobalStack()
//P("symN = '%s'", symN.SexpString(nil))
//P("symTyp = '%s'", symTyp.SexpString(nil))
r, err, _ := env.LexicalLookupSymbol(symTyp, nil)
if err != nil {
return nil, fmt.Errorf("could not identify type %s: %v", symTyp.SexpString(nil), err)
}
switch rt := r.(type) {
case *RegisteredType:
// good, store it
hash.HashSet(symN, rt)
default:
return nil, fmt.Errorf("'%s' is not a known type", symTyp.SexpString(nil))
}
}
return hash, nil
}