-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.go
410 lines (388 loc) · 9.75 KB
/
class.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package gmnlisp
import (
"context"
"errors"
"fmt"
"io"
"strings"
)
type _SlotSpec struct {
identifier Symbol
reader Symbol
writer Symbol
accessor Symbol
boundp Symbol
initform func() (Node, error)
initarg Symbol
}
type _Getter struct {
Symbol
class map[Symbol]func(*_Receiver) (Node, error)
}
func registerGetter(w *World, getterName, className Symbol, getter func(*_Receiver) (Node, error)) error {
if _acc, err := w.Get(getterName); err == nil {
if acc, ok := _acc.(*_Getter); ok {
acc.class[className] = getter
} else {
return fmt.Errorf("%v: already defined as not accessor", getterName)
}
} else {
w.DefineGlobal(getterName, &_Getter{
Symbol: getterName,
class: map[Symbol]func(*_Receiver) (Node, error){
className: getter,
},
})
}
return nil
}
func (acc *_Getter) findClass(class *_Class) func(*_Receiver) (Node, error) {
if f, ok := acc.class[class.Symbol]; ok {
return f
}
for _, super := range class.Super {
if f := acc.findClass(super); f != nil {
return f
}
}
return nil
}
func (acc *_Getter) Call(ctx context.Context, w *World, node Node) (Node, error) {
_this, _, err := w.ShiftAndEvalCar(ctx, node)
if err != nil {
return nil, err
}
reciever, ok := _this.(*_Receiver)
if !ok {
return nil, errors.New("Expect Class Instance")
}
f := acc.findClass(reciever._Class)
if f == nil {
return nil, fmt.Errorf("reciever %v not found in %v", acc.Symbol.String(), reciever._Class.Symbol.String())
}
return f(reciever)
}
type _Setter struct {
Symbol
class map[Symbol]func(*_Receiver, Node)
}
func registerSetter(w *World, setterName, className Symbol, setter func(*_Receiver, Node)) error {
if _acc, err := w.Get(setterName); err == nil {
// println("accessor is found", spec.accessor.String())
if acc, ok := _acc.(*_Setter); ok {
acc.class[className] = setter
} else {
return fmt.Errorf("%v: already defined as not accessor", setterName.String())
}
} else {
// println("accessor not found", spec.accessor.String())
w.DefineGlobal(setterName, &_Setter{
Symbol: setterName,
class: map[Symbol]func(*_Receiver, Node){
className: setter,
},
})
}
return nil
}
func (acc *_Setter) findClass(class *_Class) func(*_Receiver, Node) {
if f := acc.class[class.Symbol]; f != nil {
return f
}
for _, super := range class.Super {
if f := acc.findClass(super); f != nil {
return f
}
}
return nil
}
func (acc *_Setter) Call(ctx context.Context, w *World, node Node) (Node, error) {
value, node, err := w.ShiftAndEvalCar(ctx, node)
if err != nil {
return nil, err
}
_instance, _, err := w.ShiftAndEvalCar(ctx, node)
if err != nil {
return nil, err
}
reciever, ok := _instance.(*_Receiver)
if !ok {
return nil, errors.New("Expect Class Instance")
}
f := acc.findClass(reciever._Class)
if f == nil {
return nil, errors.New("reciever not found")
}
f(reciever, value)
return acc, nil
}
func readSlotSpec(ctx context.Context, w *World, list Node) (*_SlotSpec, error) {
cons, ok := list.(*Cons)
if !ok {
return nil, fmt.Errorf("[1] %w: %#v", ErrExpectedCons, list)
}
identifier, ok := cons.Car.(Symbol)
if !ok {
return nil, fmt.Errorf("[1] %w: %#v", ErrExpectedSymbol, cons.Car)
}
slotSpec := &_SlotSpec{identifier: identifier}
list = cons.Cdr
count := 1
for IsSome(list) {
count++
keywordCons, ok := list.(*Cons)
if !ok {
return nil, fmt.Errorf("[%d][1] %w: %#v", count, ErrExpectedCons, list)
}
keyword, ok := keywordCons.Car.(Keyword)
if !ok {
return nil, fmt.Errorf("[%d][2] %w: %#v", count, ErrExpectedKeyword, keywordCons.Car)
}
valueCons, ok := keywordCons.Cdr.(*Cons)
if !ok {
return nil, fmt.Errorf("[%d][3] %w: %#v", count, ErrExpectedCons, keywordCons.Cdr)
}
var err error
value := valueCons.Car
switch keyword {
case NewKeyword(":reader"):
slotSpec.reader, ok = value.(Symbol)
case NewKeyword(":writer"):
slotSpec.writer, ok = value.(Symbol)
case NewKeyword(":accessor"):
slotSpec.accessor, ok = value.(Symbol)
// println("accessor=", slotSpec.accessor.String())
case NewKeyword(":boundp"):
slotSpec.boundp, ok = value.(Symbol)
case NewKeyword(":initform"):
slotSpec.initform = func() (Node, error) { return value.Eval(ctx, w) }
case NewKeyword(":initarg"):
slotSpec.initarg, ok = value.(Symbol)
default:
return nil, fmt.Errorf("invalid keyword %v", keyword)
}
if err != nil {
return nil, fmt.Errorf("[%d][4] %w: %#v", count, err, value)
}
if !ok {
return nil, fmt.Errorf("[%d][5] Domain error: %#v", count, valueCons.Car)
}
list = valueCons.Cdr
}
return slotSpec, nil
}
type _Class struct {
Symbol
Super map[Symbol]*_Class
Slot map[Symbol]*_SlotSpec
}
func cmdDefClass(ctx context.Context, w *World, args Node) (Node, error) {
// (defclass class-name (sc-name*) (slot-spec*) class-opt*)
// class-name
_className, args, err := Shift(args)
if err != nil {
return nil, fmt.Errorf("[1] %w", err)
}
className, ok := _className.(Symbol)
if !ok {
return nil, fmt.Errorf("[1] %w: %#v", ErrExpectedSymbol, _className)
}
class := &_Class{
Symbol: className,
Super: make(map[Symbol]*_Class),
Slot: make(map[Symbol]*_SlotSpec),
}
if w.shared.classes == nil {
w.shared.classes = make(map[Symbol]*_Class)
}
if IsNone(args) {
w.shared.classes[className] = class
return className, nil
}
// (sc-name*) ... super class list
_scNames, args, err := Shift(args)
if err != nil {
return nil, fmt.Errorf("[2] %w", err)
}
if IsSome(_scNames) {
scNames, ok := _scNames.(*Cons)
if !ok {
return nil, fmt.Errorf("[2] %w: %#v", ErrExpectedCons, _scNames)
}
// println(scNames.GoString())
for p, ok := scNames, true; ok && IsSome(p); p, ok = p.Cdr.(*Cons) {
name, ok := p.Car.(Symbol)
if !ok {
return nil, fmt.Errorf("[2][%d] %w: %#v", 1+len(class.Super), ErrExpectedSymbol, p.Car)
}
//println("class", className, "inherits", name)
class.Super[name] = w.shared.classes[name]
}
}
if IsNone(args) {
w.shared.classes[className] = class
return className, nil
}
// (slot-spec*)
_slotSpecs, args, err := Shift(args)
if err != nil {
return nil, err
}
slotSpecs, ok := _slotSpecs.(*Cons)
if !ok {
return nil, fmt.Errorf("[3] %w: %#v", ErrExpectedCons, _slotSpecs)
}
slotCount := 0
for p, ok := slotSpecs, true; ok && IsSome(p); p, ok = p.Cdr.(*Cons) {
slotCount++
spec, err := readSlotSpec(ctx, w, p.Car)
if err != nil {
return nil, fmt.Errorf("[3][%d] %w", slotCount, err)
}
class.Slot[spec.identifier] = spec
getter := func(r *_Receiver) (Node, error) {
return r.Slot[spec.identifier], nil
}
setter := func(this *_Receiver, value Node) {
this.Slot[spec.identifier] = value
}
if IsSome(spec.reader) {
if err := registerGetter(w, spec.reader, className, getter); err != nil {
return nil, err
}
}
if IsSome(spec.writer) {
if err := registerSetter(w, spec.writer, className, setter); err != nil {
return nil, err
}
}
if IsSome(spec.accessor) {
if err := registerGetter(w, spec.accessor, className, getter); err != nil {
return nil, err
}
setterName := NewSymbol("set-" + spec.accessor.String())
if err := registerSetter(w, setterName, className, setter); err != nil {
return nil, err
}
}
}
if w.shared.classes == nil {
w.shared.classes = make(map[Symbol]*_Class)
}
w.shared.classes[className] = class
return className, nil
}
type _Receiver struct {
*_Class
Slot map[Symbol]Node
}
func (c *_Receiver) PrintTo(w io.Writer, mode PrintMode) (int, error) {
n, err := c._Class.Symbol.PrintTo(w, mode)
if err != nil {
return n, err
}
delim := []byte{'{'}
for key, val := range c.Slot {
n1, err := w.Write(delim)
n += n1
if err != nil {
return n, err
}
n1, err = key.PrintTo(w, mode)
n += n1
if err != nil {
return n, err
}
n1, err = w.Write([]byte{':'})
n += n1
if err != nil {
return n, err
}
n1, err = val.PrintTo(w, mode)
n += n1
if err != nil {
return n, err
}
delim = []byte{','}
}
n1, err := io.WriteString(w, "}")
n += n1
return n, err
}
func (c *_Receiver) String() string {
var buffer strings.Builder
c.PrintTo(&buffer, PRINC)
return buffer.String()
}
func (c *_Receiver) GoString() string {
var buffer strings.Builder
c.PrintTo(&buffer, PRINT)
return buffer.String()
}
func (reciever *_Receiver) callInitForm(classDef *_Class) error {
for _, super := range classDef.Super {
if err := reciever.callInitForm(super); err != nil {
return err
}
}
for name, slot1 := range classDef.Slot {
if _, ok := reciever.Slot[name]; !ok && slot1.initform != nil {
var err error
reciever.Slot[name], err = slot1.initform()
if err != nil {
return err
}
}
}
return nil
}
func (reciever *_Receiver) callInitArg(classDef *_Class, initArg Symbol, initVal Node) bool {
for name, slot1 := range classDef.Slot {
if slot1.initarg == initArg {
reciever.Slot[name] = initVal
return true
}
}
for _, super := range classDef.Super {
if reciever.callInitArg(super, initArg, initVal) {
return true
}
}
return false
}
func cmdCreate(ctx context.Context, w *World, args Node) (Node, error) {
_className, args, err := Shift(args)
if err != nil {
return nil, err
}
className, ok := _className.(Symbol)
if !ok {
return nil, ErrExpectedSymbol
}
classDef, ok := w.shared.classes[className]
if !ok {
return nil, fmt.Errorf("class %v undefined", className)
}
this := &_Receiver{
_Class: classDef,
Slot: map[Symbol]Node{},
}
for IsSome(args) {
var _initArg Node
_initArg, args, err = w.ShiftAndEvalCar(ctx, args)
if err != nil {
return nil, err
}
initArg, ok := _initArg.(Symbol)
if !ok {
return nil, ErrExpectedSymbol
}
var initVal Node
initVal, args, err = w.ShiftAndEvalCar(ctx, args)
if err != nil {
return nil, err
}
this.callInitArg(classDef, initArg, initVal)
}
return this, this.callInitForm(classDef)
}