-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.go
288 lines (252 loc) · 5.26 KB
/
lambda.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
//
// Copyright (c) 2022-2023 Markku Rossi
//
// All rights reserved.
//
package scheme
import (
"fmt"
"math"
"os"
"strings"
"github.com/markkurossi/scheme/types"
)
// Lambda implements lambda values.
type Lambda struct {
Capture *VMEnvFrame
Impl *LambdaImpl
}
// Scheme returns the value as a Scheme string.
func (v *Lambda) Scheme() string {
return v.String()
}
// Eq tests if the argument value is eq? to this value.
func (v *Lambda) Eq(o Value) bool {
return v == o
}
// Equal tests if the argument value is equal to this value.
func (v *Lambda) Equal(o Value) bool {
ov, ok := o.(*Lambda)
if !ok {
return false
}
return v.Impl.Equal(ov.Impl)
}
// Type implements the Value.Type().
func (v *Lambda) Type() *types.Type {
t := &types.Type{
Enum: types.EnumLambda,
Return: v.Impl.Return,
}
for _, arg := range v.Impl.Args.Fixed {
if arg.Type == nil {
t.Args = append(t.Args, types.Any)
} else {
t.Args = append(t.Args, arg.Type)
}
}
if v.Impl.Args.Rest != nil {
if v.Impl.Args.Rest.Type == nil {
t.Rest = &types.Type{
Enum: types.EnumPair,
Car: types.Unspecified,
Cdr: types.Any,
}
} else {
t.Rest = &types.Type{
Enum: types.EnumPair,
Car: v.Impl.Args.Rest.Type,
Cdr: types.Any,
}
}
}
return t
}
func (v *Lambda) String() string {
return v.Impl.Signature(false)
}
// MapPC maps the program counter value to the source location.
func (v *Lambda) MapPC(pc int) (source string, line int) {
source = v.Impl.Source
if false {
fmt.Printf("MapPC: %v:%v\n", source, pc)
for idx, pm := range v.Impl.PCMap {
fmt.Printf(" - %v\tPC=%v, Line=%v\n", idx, pm.PC, pm.Line)
}
v.Impl.Code.Print(os.Stdout)
}
line = v.Impl.PCMap.MapPC(pc)
return
}
// LambdaImpl implements lambda functions.
type LambdaImpl struct {
Name string
Args Args
Return *types.Type
Captures bool
Capture *VMEnvFrame
Native Native
Source string
Code Code
MaxStack int
PCMap PCMap
Body []AST
}
// Scheme implements the Value.Scheme().
func (v *LambdaImpl) Scheme() string {
return v.Signature(false)
}
// Signature prints the lambda signature with optional lambda body.
func (v *LambdaImpl) Signature(body bool) string {
var str strings.Builder
str.WriteRune('(')
if len(v.Name) != 0 {
str.WriteString(v.Name)
} else {
str.WriteString("lambda")
}
str.WriteString(" ")
str.WriteString(v.Args.String())
if v.Native != nil {
str.WriteString(" {native}")
} else if body && len(v.Body) > 0 {
for _, ast := range v.Body {
str.WriteRune(' ')
str.WriteString(fmt.Sprintf("%v", ast))
}
} else if v.Code != nil {
str.WriteString(" {compiled}")
} else {
str.WriteString(" ...")
}
str.WriteRune(')')
str.WriteString(v.Return.String())
return str.String()
}
// Eq implements the Value.Eq().
func (v *LambdaImpl) Eq(o Value) bool {
return v == o
}
// Equal implements the Value.Equal().
func (v *LambdaImpl) Equal(o Value) bool {
ov, ok := o.(*LambdaImpl)
if !ok {
return false
}
if !v.Args.Equal(ov.Args) {
return false
}
if v.Captures != ov.Captures {
return false
}
if v.Native == nil && ov.Native != nil {
return false
}
if v.Native != nil && ov.Native == nil {
return false
}
if len(v.Body) != len(ov.Body) {
return false
}
for idx, vv := range v.Body {
if !vv.Equal(ov.Body[idx]) {
return false
}
}
return true
}
// Type implements the Value.Type().
func (v *LambdaImpl) Type() *types.Type {
return types.Unspecified
}
// Args specify lambda arguments.
type Args struct {
Min int
Max int
Fixed []*TypedName
Rest *TypedName
}
// Equal tests if the arguments are equal.
func (args Args) Equal(o Args) bool {
if args.Min != o.Min || args.Max != o.Max ||
len(args.Fixed) != len(o.Fixed) {
return false
}
for idx, n := range args.Fixed {
if n.Name != o.Fixed[idx].Name {
return false
}
}
if args.Rest == nil {
if o.Rest != nil {
return false
}
} else if o.Rest == nil {
return false
} else if args.Rest.Name != o.Rest.Name {
return false
}
return true
}
func (args Args) String() string {
if len(args.Fixed) == 0 {
if args.Rest == nil {
return "()"
}
return args.Rest.Name
}
var str strings.Builder
str.WriteRune('(')
for idx, arg := range args.Fixed {
if idx > 0 {
str.WriteRune(' ')
}
str.WriteString(arg.String())
}
if args.Rest != nil {
str.WriteString(" . ")
str.WriteString(args.Rest.String())
}
str.WriteRune(')')
return str.String()
}
// Init initializes argument limits and checks that all argument names
// are unique.
func (args *Args) Init() {
args.Min = len(args.Fixed)
if args.Rest != nil {
args.Max = math.MaxInt
} else {
args.Max = args.Min
}
}
// TypedName defines name with type information.
type TypedName struct {
Name string
Type *types.Type
}
func (tn *TypedName) String() string {
var result = tn.Name
if tn.Type != nil {
result += fmt.Sprintf("<%s>", tn.Type)
}
switch tn.Type.Kind {
case types.Optional:
return "[" + result + "]"
case types.Rest:
return result + "..."
default:
return result
}
}
// Native implements native functions.
type Native func(scm *Scheme, args []Value) (Value, error)
// Builtin defines a built-in native function.
type Builtin struct {
Name string
Aliases []string
Args []string
Return *types.Type
Flags Flags
Native Native
}