-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfunction.go
188 lines (160 loc) · 5.02 KB
/
function.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
// Copyright 2014 SteelSeries ApS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file implements user defined functions.
package golisp
import (
"errors"
"fmt"
"sync/atomic"
"unsafe"
)
type Function struct {
Name string
Params *Data
VarArgs bool
RequiredArgCount int
Body *Data
Env *SymbolTableFrame
DebugOnEntry bool
SlotFunction int32
ParentProcess *Process
}
func computeRequiredArgumentCount(args *Data) (requiredArgumentCount int, varArgs bool) {
requiredArgumentCount = 0
varArgs = false
for a := args; NotNilP(a); a = Cdr(a) {
if SymbolP(a) {
varArgs = true
return
} else {
requiredArgumentCount += 1
}
}
return
}
func MakeFunction(name string, params *Data, body *Data, parentEnv *SymbolTableFrame) *Function {
requiredArgs, varArgs := computeRequiredArgumentCount(params)
return &Function{Name: name, Params: params, VarArgs: varArgs, RequiredArgCount: requiredArgs, Body: body, Env: parentEnv, SlotFunction: 0}
}
func (self *Function) String() string {
return fmt.Sprintf("<func: %s>", self.Name)
}
func (self *Function) makeLocalBindings(args *Data, argEnv *SymbolTableFrame, localEnv *SymbolTableFrame, eval bool) (err error) {
if self.VarArgs {
if Length(args) < self.RequiredArgCount {
return errors.New(fmt.Sprintf("%s expected at least %d parameters, received %d.", self.Name, self.RequiredArgCount, Length(args)))
}
} else {
if Length(args) != self.RequiredArgCount {
return errors.New(fmt.Sprintf("%s expected %d parameters, received %d.", self.Name, self.RequiredArgCount, Length(args)))
}
}
var argValue *Data
var accumulatingParam *Data = nil
accumulatedArgs := make([]*Data, 0)
for p, a := self.Params, args; NotNilP(a); a = Cdr(a) {
if eval {
argValue, err = Eval(Car(a), argEnv)
if err != nil {
return
}
} else {
argValue = Car(a)
}
if accumulatingParam != nil {
accumulatedArgs = append(accumulatedArgs, argValue)
} else {
_, err = localEnv.BindLocallyTo(Car(p), argValue)
if err != nil {
return
}
}
if accumulatingParam == nil {
p = Cdr(p)
}
if SymbolP(p) {
accumulatingParam = p
}
}
if accumulatingParam != nil {
_, err = localEnv.BindLocallyTo(accumulatingParam, ArrayToList(accumulatedArgs))
if err != nil {
return
}
}
return nil
}
func (self *Function) internalApply(args *Data, argEnv *SymbolTableFrame, frame *FrameMap, eval bool) (result *Data, err error) {
localEnv := NewSymbolTableFrameBelowWithFrame(self.Env, frame, self.Name)
localEnv.Previous = argEnv
selfSym := Intern("self")
if frame != nil {
_, err = localEnv.BindLocallyTo(selfSym, FrameWithValue(frame))
if err != nil {
return
}
} else if atomic.LoadInt32(&self.SlotFunction) == 1 {
selfBinding, found := argEnv.findBindingInLocalFrameFor(selfSym)
if found {
_, err = localEnv.BindLocallyTo(selfSym, selfBinding.Val)
if err != nil {
return
}
}
}
parentProcSym := Intern("parentProcess")
if self.ParentProcess != nil {
procObj := ObjectWithTypeAndValue("Process", unsafe.Pointer(self.ParentProcess))
_, err = localEnv.BindLocallyTo(parentProcSym, procObj)
if err != nil {
return
}
}
err = self.makeLocalBindings(args, argEnv, localEnv, eval)
if err != nil {
return
}
localGuid := atomic.AddInt64(&ProfileGUID, 1) - 1
ProfileEnter("func", self.Name, localGuid)
for s := self.Body; NotNilP(s); s = Cdr(s) {
result, err = Eval(Car(s), localEnv)
if err != nil {
result, err = nil, errors.New(fmt.Sprintf("In '%s': %s", self.Name, err))
break
}
}
ProfileExit("func", self.Name, localGuid)
return
}
func (self *Function) Apply(args *Data, argEnv *SymbolTableFrame) (result *Data, err error) {
return self.internalApply(args, argEnv, nil, true)
}
func (self *Function) ApplyWithFrame(args *Data, argEnv *SymbolTableFrame, frame *FrameMap) (result *Data, err error) {
return self.internalApply(args, argEnv, frame, true)
}
func (self *Function) ApplyWithoutEval(args *Data, argEnv *SymbolTableFrame) (result *Data, err error) {
return self.internalApply(args, argEnv, nil, false)
}
func (self *Function) ApplyWithoutEvalWithFrame(args *Data, argEnv *SymbolTableFrame, frame *FrameMap) (result *Data, err error) {
return self.internalApply(args, argEnv, frame, false)
}
func (self *Function) ApplyOveriddingEnvironment(args *Data, argEnv *SymbolTableFrame) (result *Data, err error) {
localEnv := NewSymbolTableFrameBelow(argEnv, self.Name)
err = self.makeLocalBindings(args, argEnv, localEnv, true)
if err != nil {
return
}
localGuid := atomic.AddInt64(&ProfileGUID, 1) - 1
ProfileEnter("func", self.Name, localGuid)
for s := self.Body; NotNilP(s); s = Cdr(s) {
result, err = Eval(Car(s), localEnv)
if err != nil {
result, err = nil, errors.New(fmt.Sprintf("In '%s': %s", self.Name, err))
break
}
}
ProfileExit("func", self.Name, localGuid)
return
}