-
Notifications
You must be signed in to change notification settings - Fork 8
/
builtin.go
172 lines (157 loc) · 5.04 KB
/
builtin.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
// Copyright Suneido Software Corp. All rights reserved.
// Governed by the MIT license found in the LICENSE file.
package builtin
import (
"reflect"
"runtime"
"strings"
"github.com/apmckinlay/gsuneido/compile"
. "github.com/apmckinlay/gsuneido/core"
"github.com/apmckinlay/gsuneido/util/assert"
"github.com/apmckinlay/gsuneido/util/str"
)
// builtin function names can end in Q or X for ? or !
func builtin(f any, p string) any {
name := funcName(f)
name = str.Capitalize(name)
Global.Builtin(name, builtinVal(name, f, p))
return nil
}
func builtinVal(name string, f any, p string) Value {
ps := params(p)
ps.Name = name
switch f := f.(type) {
case func() Value:
assert.That(ps.Nparams == 0)
return &SuBuiltin0{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(Value) Value:
assert.That(ps.Nparams == 1)
return &SuBuiltin1{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(Value, Value) Value:
assert.That(ps.Nparams == 2)
return &SuBuiltin2{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(Value, Value, Value) Value:
assert.That(ps.Nparams == 3)
return &SuBuiltin3{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(Value, Value, Value, Value) Value:
assert.That(ps.Nparams == 4)
return &SuBuiltin4{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(Value, Value, Value, Value, Value) Value:
assert.That(ps.Nparams == 5)
return &SuBuiltin5{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(Value, Value, Value, Value, Value, Value) Value:
assert.That(ps.Nparams == 6)
return &SuBuiltin6{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(Value, Value, Value, Value, Value, Value, Value) Value:
assert.That(ps.Nparams == 7)
return &SuBuiltin7{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(th *Thread, args []Value) Value:
return &SuBuiltin{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(th *Thread, as *ArgSpec, args []Value) Value:
// params are just for documentation
return &SuBuiltinRaw{Fn: f, BuiltinParams: BuiltinParams{ParamSpec: ps}}
default:
panic("invalid builtin function: " + name)
}
}
// curMethods and curPrefix are only used during initialization
// which is single threaded, so no locking is required
var (
curMethods Methods
curPrefix string
)
func methods() Methods {
curMethods = Methods{}
curPrefix = ""
return curMethods
}
func exportMethods(m *Methods) any {
*m = methods()
return nil
}
// staticMethod adds to curMethods, like method,
// but creates a standalone function like builtin
func staticMethod(f any, p string) any {
name := methodName(f)
fn := builtinVal(name, f, p)
curMethods[name] = fn
return nil
}
// method adds to curMethods, which is set by methods.
// method function names must start with a prefix e.g. xyz_
func method(f any, p string) any {
name := methodName(f)
ps := params(p)
ps.Name = name
switch f := f.(type) {
case func(this Value) Value:
assert.That(ps.Nparams == 0)
curMethods[name] = &SuBuiltinMethod0{SuBuiltin1: SuBuiltin1{Fn: f,
BuiltinParams: BuiltinParams{ParamSpec: ps}}}
case func(this, a Value) Value:
assert.That(ps.Nparams == 1)
curMethods[name] = &SuBuiltinMethod1{SuBuiltin2: SuBuiltin2{Fn: f,
BuiltinParams: BuiltinParams{ParamSpec: ps}}}
case func(this, a, b Value) Value:
assert.That(ps.Nparams == 2)
curMethods[name] = &SuBuiltinMethod2{SuBuiltin3: SuBuiltin3{Fn: f,
BuiltinParams: BuiltinParams{ParamSpec: ps}}}
case func(this, a, b, c Value) Value:
assert.That(ps.Nparams == 3)
curMethods[name] = &SuBuiltinMethod3{SuBuiltin4: SuBuiltin4{Fn: f,
BuiltinParams: BuiltinParams{ParamSpec: ps}}}
case func(th *Thread, this Value, args []Value) Value:
curMethods[name] = &SuBuiltinMethod{Fn: f,
BuiltinParams: BuiltinParams{ParamSpec: ps}}
case func(th *Thread, as *ArgSpec, this Value, args []Value) Value:
// params are just for documentation
curMethods[name] = &SuBuiltinMethodRaw{Fn: f, ParamSpec: params(p)}
default:
Fatal("invalid builtin function", name)
}
return nil
}
func methodName(f any) string {
fname := funcName(f)
prefix, name, _ := strings.Cut(fname, "_")
if name == "" {
Fatal("method missing prefix:", fname)
}
if curPrefix == "" {
curPrefix = prefix
} else if prefix != curPrefix {
Fatal("inconsistent prefix:", fname)
}
if _, ok := curMethods[name]; ok {
Fatal("duplicate method:", fname)
}
return name
}
func funcName(f any) string {
s := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
s = str.AfterFirst(s, "builtin.")
switch s[len(s)-1] {
case 'Q':
s = s[:len(s)-1] + "?"
case 'X':
s = s[:len(s)-1] + "!"
}
return s
}
// params builds a ParamSpec from a string like (a, b) or (@args)
func params(s string) ParamSpec {
s = strings.ReplaceAll(s, "nil", "'nil'")
fn := compile.Constant("function " + s + " {}").(*SuFunc)
for i := 0; i < int(fn.ParamSpec.Ndefaults); i++ {
if fn.Values[i].Equal(SuStr("nil")) {
fn.Values[i] = nil
}
}
return fn.ParamSpec
}
type staticClass[E any] struct {
ValueBase[E]
}
func (*staticClass[E]) SetConcurrent() {
// read-only so ok
}