-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfunction.go
207 lines (181 loc) · 4.21 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package object
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/risor-io/risor/compiler"
"github.com/risor-io/risor/errz"
"github.com/risor-io/risor/op"
)
// Function is a function that has been compiled to bytecode.
type Function struct {
*base
name string
parameters []string
defaults []Object
defaultsCount int
code *compiler.Code
fn *compiler.Function
instructions []op.Code
freeVars []*Cell
}
func (f *Function) Type() Type {
return FUNCTION
}
func (f *Function) Name() string {
return f.name
}
func (f *Function) Inspect() string {
var out bytes.Buffer
parameters := make([]string, 0)
for i, name := range f.parameters {
if def := f.defaults[i]; def != nil {
name += "=" + def.Inspect()
}
parameters = append(parameters, name)
}
out.WriteString("func")
if f.name != "" {
out.WriteString(" " + f.name)
}
out.WriteString("(")
out.WriteString(strings.Join(parameters, ", "))
out.WriteString(") {")
lines := strings.Split(f.code.Source(), "\n")
if len(lines) == 1 {
out.WriteString(" " + lines[0] + " }")
} else if len(lines) == 0 {
out.WriteString(" }")
} else {
for _, line := range lines {
out.WriteString("\n " + line)
}
out.WriteString("\n}")
}
return out.String()
}
func (f *Function) String() string {
if f.name != "" {
return fmt.Sprintf("func %s() { ... }", f.name)
}
return "func() { ... }"
}
func (f *Function) Interface() interface{} {
return nil
}
func (f *Function) GetAttr(name string) (Object, bool) {
switch name {
case "spawn":
return &Builtin{
name: "function.spawn",
fn: func(ctx context.Context, args ...Object) Object {
thread, err := Spawn(ctx, f, args)
if err != nil {
return NewError(err)
}
return thread
},
}, true
}
return nil, false
}
func (f *Function) RunOperation(opType op.BinaryOpType, right Object) Object {
return TypeErrorf("type error: unsupported operation for function: %v", opType)
}
func (f *Function) Equals(other Object) Object {
if f == other {
return True
}
return False
}
func (f *Function) Instructions() []op.Code {
if f.instructions == nil {
count := f.code.InstructionCount()
f.instructions = make([]op.Code, count)
for i := 0; i < count; i++ {
f.instructions[i] = f.code.Instruction(i)
}
}
return f.instructions
}
func (f *Function) FreeVars() []*Cell {
return f.freeVars
}
func (f *Function) Code() *compiler.Code {
return f.code
}
func (f *Function) Function() *compiler.Function {
return f.fn
}
func (f *Function) Parameters() []string {
return f.parameters
}
func (f *Function) Defaults() []Object {
return f.defaults
}
func (f *Function) RequiredArgsCount() int {
return len(f.parameters) - f.defaultsCount
}
func (f *Function) LocalsCount() int {
return int(f.code.LocalsCount())
}
func (f *Function) MarshalJSON() ([]byte, error) {
return nil, errz.TypeErrorf("type error: unable to marshal function")
}
func (f *Function) Call(ctx context.Context, args ...Object) Object {
callFunc, found := GetCallFunc(ctx)
if !found {
return Errorf("eval error: context did not contain a call function")
}
result, err := callFunc(ctx, f, args)
if err != nil {
return NewError(err)
}
return result
}
type FunctionOpts struct {
Name string
ParameterNames []string
Defaults []Object
Code *compiler.Code
}
func NewFunction(fn *compiler.Function) *Function {
// Parameter defaults
var defaults []Object
var defaultsCount int
for i := 0; i < fn.DefaultsCount(); i++ {
value := fn.Default(i)
if value != nil {
defaultsCount++
defaults = append(defaults, FromGoType(value))
} else {
defaults = append(defaults, nil)
}
}
// Parameter names
var parameters []string
for i := 0; i < fn.ParametersCount(); i++ {
parameters = append(parameters, fn.Parameter(i))
}
return &Function{
name: fn.Name(),
code: fn.Code(),
parameters: parameters,
defaults: defaults,
defaultsCount: defaultsCount,
}
}
func NewClosure(
fn *Function,
freeVars []*Cell,
) *Function {
return &Function{
name: fn.name,
parameters: fn.parameters,
defaults: fn.defaults,
defaultsCount: fn.defaultsCount,
code: fn.Code(),
freeVars: freeVars,
}
}