-
Notifications
You must be signed in to change notification settings - Fork 1
/
object.go
166 lines (136 loc) · 3.15 KB
/
object.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
// Object and ObjectBase is an abstract class for all scheme expressions.
// A return value of a method which returns scheme object is Object.
// And ObjectBase has Object's implementation of String().
package scheme
type Object interface {
Parent() Object
Bounder() *Variable
setParent(Object)
setBounder(*Variable)
Eval() Object
String() string
isNumber() bool
isBoolean() bool
isClosure() bool
isProcedure() bool
isNull() bool
isPair() bool
isList() bool
isSymbol() bool
isSyntax() bool
isString() bool
isVariable() bool
isApplication() bool
define(string, Object)
set(string, Object)
scopedBinding() Binding
binding() Binding
boundedObject(string) Object
}
type Binding map[string]Object
type ObjectBase struct {
parent Object
bounder *Variable // Variable.Eval() sets itself into this
}
func (o *ObjectBase) Eval() Object {
runtimeError("This object's Eval() is not implemented yet.")
return nil
}
func (o *ObjectBase) String() string {
runtimeError("This object's String() is not implemented yet.")
return ""
}
func (o *ObjectBase) isNumber() bool {
return false
}
func (o *ObjectBase) isBoolean() bool {
return false
}
func (o *ObjectBase) isClosure() bool {
return false
}
func (o *ObjectBase) isProcedure() bool {
return false
}
func (o *ObjectBase) isNull() bool {
return false
}
func (o *ObjectBase) isPair() bool {
return false
}
func (o *ObjectBase) isList() bool {
return false
}
func (o *ObjectBase) isSymbol() bool {
return false
}
func (o *ObjectBase) isSyntax() bool {
return false
}
func (o *ObjectBase) isString() bool {
return false
}
func (o *ObjectBase) isVariable() bool {
return false
}
func (o *ObjectBase) isApplication() bool {
return false
}
func (o *ObjectBase) binding() Binding {
return Binding{}
}
func (o *ObjectBase) Parent() Object {
return o.parent
}
func (o *ObjectBase) Bounder() *Variable {
return o.bounder
}
func (o *ObjectBase) setParent(parent Object) {
o.parent = parent
}
func (o *ObjectBase) setBounder(bounder *Variable) {
o.bounder = bounder
}
func (o *ObjectBase) scopedBinding() (scopedBinding Binding) {
scopedBinding = make(Binding)
parent := o.Parent()
for parent != nil {
for identifier, object := range parent.binding() {
if scopedBinding[identifier] == nil {
scopedBinding[identifier] = object
}
}
parent = parent.Parent()
}
return
}
// Define variable in the most inner closure
func (o *ObjectBase) define(identifier string, object Object) {
if o.parent == nil {
runtimeError("Bind called for object whose parent is nil")
} else {
o.parent.define(identifier, object)
}
}
// This is for set! syntax.
// Update the variable's value when it is defined.
func (o *ObjectBase) set(identifier string, object Object) {
if o.parent == nil {
runtimeError("symbol not defined")
} else {
o.parent.set(identifier, object)
}
}
func (o *ObjectBase) boundedObject(identifier string) Object {
scopedBinding := make(Binding)
parent := o.Parent()
for parent != nil {
for identifier, object := range parent.binding() {
if scopedBinding[identifier] == nil {
scopedBinding[identifier] = object
}
}
parent = parent.Parent()
}
return scopedBinding[identifier]
}