forked from chrislusf/gelo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
287 lines (261 loc) · 5.82 KB
/
api.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
package gelo
//This type is merely a proxy for the vm to give the API methods a namespace
//distinct from the other vm methods. Pretty, but useless.
type api struct {
vm *VM
}
func (*api) Trace(message ...interface{}) {
alien_trace(message...)
}
func (p *api) Halt(info *List) {
panic(halt_control_code(info))
}
func (p *api) Recv() Word {
return p.vm.io.Recv()
}
func (p *api) Send(w Word) {
p.vm.io.Send(w)
}
//If string, attempt to convert
func (p *api) NumberOrElse(w Word) *Number {
n, ok := w.(*Number)
if !ok {
n, ok = NewNumberFromBytes(w.Ser().Bytes())
if !ok {
TypeMismatch(p.vm, "number", w.Type())
}
}
return n
}
func (p *api) QuoteOrElse(w Word) Quote {
q, ok := w.(Quote)
if !ok {
TypeMismatch(p.vm, "quote", w.Type())
}
return q
}
func (p *api) ListOrElse(w Word) *List {
if l, ok := w.(*List); ok {
return l
}
l, ok := UnserializeListFrom(w)
if !ok {
TypeMismatch(p.vm, "list", w.Type())
}
return l
}
func (p *api) DictOrElse(w Word) *Dict {
if d, ok := w.(*Dict); ok {
return d
}
d, ok := UnserializeDictFrom(w)
if !ok {
TypeMismatch(p.vm, "dict", w.Type())
}
return d
}
func (p *api) PortOrElse(w Word) Port {
port, ok := w.(Port)
if !ok {
TypeMismatch(p.vm, "port", w.Type())
}
return port
}
func (p *api) ChanOrElse(w Word) Port {
c, ok := w.(*Chan)
if !ok {
TypeMismatch(p.vm, "chan", w.Type())
}
return c
}
func (p *api) BoolOrElse(w Word) Bool {
b, ok := w.(Bool)
if !ok {
TypeMismatch(p.vm, "bool", w.Type())
}
return b
}
func (p *api) SymbolOrElse(w Word) Symbol {
s, ok := w.(Symbol)
if !ok {
TypeMismatch(p.vm, "symbol", w.Type())
}
return s
}
func (p *api) AlienOrElse(w Word) Alien {
a, ok := w.(Alien)
if !ok {
TypeMismatch(p.vm, "alien", w.Type())
}
return a
}
/*
* Returns the bytes of a symbol or quote.
* We do not check that the quote is in fact literal as there are many possible
* texts that could just happen to parse, this comment being an example of one.
*/
func (p *api) LiteralOrElse(w Word) []byte {
s, ok := w.(Symbol)
if ok {
return s.Bytes()
}
q, ok := w.(Quote)
if !ok {
TypeMismatch(p.vm, "symbol or quote", w.Type())
}
return q.Ser().Bytes()
}
func (p *api) InvokableOrElse(w Word) Word {
i, ok := p.IsInvokable(w)
if !ok {
TypeMismatch(p.vm, "invokable", w.Type())
}
return i
}
//If w is a Symbol, dereference and see if it is a code Quote or an Alien, if
//so return the derefed invokable and true. Otherwise, checks if w is
//a code Quote or Alien and, if so, return it and true. In all other cases,
//return (nil, false)
func (p *api) IsInvokable(w Word) (Word, bool) {
if s, ok := w.(Symbol); ok {
w, ok = p.vm.Ns.Lookup(s)
if !ok {
return nil, false
}
}
if q, ok := w.(Quote); ok {
if _, ok := q.unprotect().fcode(); ok {
return w, true
}
return nil, false
}
if _, ok := w.(Alien); ok {
return w, true
}
if _, ok := w.(*defert); ok {
return w, true
}
return nil, false
}
func (p *api) InvokeOrElse(args *List) (ret Word) {
//simulate the Noop, {}
if args == nil {
return Null
}
w, c, args := p.vm.peval(args, uint(args.Len()-1))
if _, is_defer := w.(*defert); is_defer {
RuntimeError(p.vm, "Cannot register a defer via Invoke*")
return
}
if c != nil {
ret = p.vm.eval(c, args)
} else {
ret = w
}
return
}
func (p *api) Invoke(args *List) (ret Word, err Error) {
if args == nil {
return Null, nil
}
defer func() {
if x := recover(); x != nil {
switch t := x.(type) {
default:
//note that we are implicitly allowing halt_control_code
//to bubble
panic(x)
case Error:
ret, err = nil, t
}
}
}()
ret = p.InvokeOrElse(args)
return
}
//The TailInvoke* family is only to be called when the result is to be
//returned from the callee.
func (*api) TailInvoke(args *List) Word {
if args == nil {
return Null
}
return build_quote_from_list(args)
}
func (p *api) InvokeCmd(w Word, args *List) (Word, Error) {
return p.Invoke(&List{w, args})
}
func (p *api) InvokeCmdOrElse(w Word, args *List) Word {
return p.InvokeOrElse(&List{w, args})
}
func (p *api) TailInvokeCmd(w Word, args *List) Word {
return p.TailInvoke(&List{w, args})
}
func (p *api) InvokeWordOrReturn(w Word) (ret Word, err Error) {
i, ok := p.IsInvokable(w)
if !ok {
return w, nil
}
return p.Invoke(AsList(i))
}
func (p *api) TailInvokeWordOrReturn(w Word) Word {
i, ok := p.IsInvokable(w)
if !ok {
return w
}
return p.TailInvoke(AsList(i))
}
//return a list of lists of symbols and quotes, only evaluates $@[]
func (p *api) PartialEval(q Quote) (*List, bool) {
cmds, ok := q.unprotect().fcode()
if !ok { //cannot partially eval what we cannot fully eval
return nil, false
}
if cmds == nil {
//noop, return singleton *containing* empty list
return NewList(EmptyList), true
}
var ghead, gtail *List
for c := cmds; c != nil; c = c.next {
var head, tail *List
for s := c.cmd; s != nil; s = s.next {
var w Word
switch s.tag {
case synLiteral, synQuote:
w = s.val.(Word)
default:
//this is ugly but no clean way to extract a rewrite1
//out of rewrite without increasing the complexity or
//compromising its performance in the face of a splice.
//fortunately this is the only place we call it where
//it wasn't meant to be. Since we handle quote separately,
//we don't need to worry about it getting unprotected.
l, _ := p.vm.rewrite(&sNode{s.tag, s.val, nil})
if s.tag == synSplice {
if l == nil {
continue //so we don't write nil to the list
}
for ; l.Next != nil; l = l.Next {
}
w = l.Next
} else {
w = l.Value
}
}
if head != nil {
tail.Next = &List{w, nil}
tail = tail.Next
} else {
head = &List{w, nil}
tail = head
}
}
if ghead != nil {
gtail.Next = &List{head, nil}
gtail = gtail.Next
} else {
ghead = &List{head, nil}
gtail = ghead
}
}
return ghead, true
}