-
Notifications
You must be signed in to change notification settings - Fork 0
/
Golly.go
367 lines (343 loc) · 12.4 KB
/
Golly.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
package Golly
import (
"Golly/parser"
"fmt"
"strconv"
)
type FunctionObj struct {
Parems []string
Body []ListCell
Pure bool
GoFunc bool
FuncType goFuncType
}
func (aFunc *FunctionObj) Call(params []ListCell, env Environment)([]*ListCell, error) {
if aFunc.GoFunc{
returnedVals, err := CallGoFunc(aFunc.FuncType, params)
if err != nil{
return nil, err
}else{
return returnedVals, nil
}
}else{
returnedVals, err := Eval(aFunc.Body, env)
if err != nil{
return nil, err
}else{
return returnedVals, nil
}
}
}
func makeSysFunc(funcType goFuncType)ListCell{
return ListCell{TypeName: FUNCTION_TYPE_NAME,
Value: FunctionObj{Pure: true, GoFunc: true, FuncType: funcType},
Mutable: false}
}
func CreateSystemFuncs() *SysEnvironment{
sysBindings := make(map[string]EnvBinding)
sysBindings["if"] = EnvBinding{Binding: makeSysFunc(GoIfT)}
sysBindings["+"] = EnvBinding{Binding: makeSysFunc(GoAddT)}
sysBindings["-"] = EnvBinding{Binding: makeSysFunc(GoSubtractT)}
sysBindings["*"] = EnvBinding{Binding: makeSysFunc(GoMultiplyT)}
sysBindings["/"] = EnvBinding{Binding: makeSysFunc(GoDivideT)}
env := SysEnvironment{Bindings: sysBindings}
return &env
}
type singleType struct {
Inputs []string
Outputs []string
}
type TypeObj struct {
Types []singleType
}
func (firstType *TypeObj) EqualTo(secondType *TypeObj) bool {
return false
}
func TypesEqualP(first *ListCell, second *ListCell, lineNum int, caller *string) bool {
if firstType, ok := first.Value.(TypeObj); ok {
if secondType, ok := second.Value.(TypeObj); ok {
return firstType.EqualTo(&secondType)
} else {
errMsg := fmt.Sprintf("Error: cell claiming to be a type actually contains something else, in %v at line %v.\n", caller, lineNum)
panic(errMsg)
}
} else {
errMsg := fmt.Sprintf("Error: cell claiming to be a type actually contains something else, in %v at line %v.\n", caller, lineNum)
panic(errMsg)
}
}
type EnvBinding struct {
Name string
Binding ListCell
}
type SysEnvironment struct {
Bindings map[string]EnvBinding
}
type Environment struct {
Bindings []EnvBinding
Parent *Environment
System *SysEnvironment
}
func (env Environment) findBinding(name string, recur, checkSystem bool) *EnvBinding {
if checkSystem {
if binding, ok := env.System.Bindings[name]; ok {
return &binding
}
}
for i, binding := range env.Bindings {
if binding.Name == name {
return &env.Bindings[i]
}
}
if recur {
if env.Parent == nil {
return nil
} else {
return env.Parent.findBinding(name, true, false)
}
}
return nil
}
func (env *Environment) addBinding(recur bool) *EnvBinding {
if recur {
if env.Parent == nil {
env.Bindings = append(env.Bindings, EnvBinding{})
return &((env.Bindings)[len(env.Bindings)])
} else {
return env.Parent.addBinding(true)
}
} else {
env.Bindings = append(env.Bindings, EnvBinding{})
return &((env.Bindings)[len(env.Bindings)])
}
}
type ListCell struct {
TypeName string
Value interface{}
Mutable bool
}
type CellList struct {
Cells []ListCell
Env Environment
}
func evalLitToken(num *Parser.Token, lineNum int, caller *string) ListCell {
newValue := ListCell{}
switch (*num).LitType {
case Parser.FloNum:
floatval, err := strconv.ParseFloat((*num).Value, 32)
if err != nil {
errMsg := fmt.Sprintf("Error: cannot parse string %v to float in %v at line %v.\n", (*num).Value, caller, lineNum)
panic(errMsg)
} else {
newValue.Value = floatval
newValue.TypeName = "float"
}
case Parser.FixNum:
intval, err := strconv.Atoi((*num).Value)
if err != nil {
errMsg := fmt.Sprintf("Error: cannot parse string %v to int in %v at line %v.\n", (*num).Value, caller, lineNum)
panic(errMsg)
} else {
newValue.Value = intval
newValue.TypeName = "int"
}
default:
errMsg := fmt.Sprintf("Error: unhandled literal type for %v in %v at line %v.\n", (*num).Value, caller, lineNum)
panic(errMsg)
}
return newValue
}
func evalIdToken(identifierName *Parser.Token, env *Environment, lineNum int, caller *string) interface{} {
var newValue interface{}
valueReferenced := env.findBinding((*identifierName).Value, true, true)
if valueReferenced == nil {
errMsg := fmt.Sprintf("Error: attempting to evalute var %v in %v at line %v, but that var is unbound.\n", (*identifierName).Value, caller, lineNum)
panic(errMsg)
} else {
newValue = valueReferenced.Binding
}
return newValue
}
func parseType(identifierToBindTo, potentialType *Parser.Token, env *Environment, lineNum int, caller *string) (*ListCell, string) {
newTypeName := ""
typeLiteralFound := false
var newType *ListCell
switch (*potentialType).Type {
case Parser.LiteralToken:
errMsg := fmt.Sprintf("Error: attempting use a numeric literal as the type for %v in %v at line %v.\n", identifierToBindTo.Value, caller, lineNum)
panic(errMsg)
case Parser.DefToken:
errMsg := fmt.Sprintf("Error: attempting use a reserved name as the type for %v in %v at line %v.\n", identifierToBindTo.Value, caller, lineNum)
panic(errMsg)
case Parser.IdToken:
potentialNewTypeValue := env.findBinding(potentialType.Value, true, true)
if potentialNewTypeValue != nil {
if potentialNewTypeValue.Binding.TypeName != "type" {
errMsg := fmt.Sprintf("Error: attempting to assign something that is not a type, but a %v, to %v in %v at line %v.\n", potentialNewTypeValue.Binding.TypeName, identifierToBindTo.Value, caller, lineNum)
panic(errMsg)
} else {
newTypeName = potentialType.Value
newType = &potentialNewTypeValue.Binding
}
//Handle var containing quoted type here
} else {
errMsg := fmt.Sprintf("Error: attempting to assign identifier %v to %v in %v at line %v, but %v is unbound.\n", (*potentialType).Value, (*identifierToBindTo).Value, caller, lineNum, (*potentialType).Value)
panic(errMsg)
}
case Parser.ListToken:
potentialNewType := evalListToken(potentialType)
if newType.TypeName != "type" {
errMsg := fmt.Sprintf("Error: attempting to assign something that is not a type, but a %v, to %v in %v at line %v.\n", newType.TypeName, (*identifierToBindTo).Value, caller, lineNum)
panic(errMsg)
} else {
typeLiteralFound = true
newType = &potentialNewType
}
//Handle function returning quoted type here
case Parser.TypeAnnToken:
errMsg := fmt.Sprintf("Error: misplaced type annotation marker in %v at line %v.\n", caller, lineNum)
panic(errMsg)
default:
errMsg := fmt.Sprintf("Error: unhandled token type in %v at line %v.\n", caller, lineNum)
panic(errMsg)
}
if typeLiteralFound {
if _, ok := newType.Value.(TypeObj); !ok {
errMsg := fmt.Sprintf("Error: cell claiming to be a type actually contains something else, in %v at line %v.\n", caller, lineNum)
panic(errMsg)
}
}
return newType, newTypeName
}
func parseNewIdentifier(val *Parser.Token, env *Environment, global bool, lineNum int, caller *string) *EnvBinding {
if val.Type != Parser.IdToken {
errMsg := fmt.Sprintf("Error: attempting to assign to a non-identifier in %v at line %v.\n", caller, lineNum)
panic(errMsg)
}
prevBinding := env.findBinding(val.Value, false, true)
var newBinding *EnvBinding
if prevBinding != nil {
if !(*prevBinding).Binding.Mutable {
errMsg := fmt.Sprintf("Error: attempting to assign to an immutable identifier in %v at line %v.\n", caller, lineNum)
panic(errMsg)
} else {
newBinding = prevBinding
}
} else {
newBinding = env.addBinding(global)
}
return newBinding
}
func parseIdentifierToBeBound(identifierToBeBoundTo, identifierToBind *Parser.Token, env *Environment, global bool, lineNum int, caller *string) *ListCell {
newValue := ListCell{TypeName: "undecided"}
switch (*identifierToBind).Type {
case Parser.LiteralToken:
newValue = evalLitToken(identifierToBind, lineNum, caller)
case Parser.DefToken:
errMsg := fmt.Sprintf("Error: attempting to assign reserved name %v to %v in %v at line %v.\n", identifierToBind.Value, identifierToBeBoundTo.Value, caller, lineNum)
panic(errMsg)
case Parser.IdToken:
potentialNewValue := env.findBinding(identifierToBind.Value, true, true)
if potentialNewValue != nil {
newValue.Value = potentialNewValue
} else {
errMsg := fmt.Sprintf("Error: attempting to assign identifier %v to %v in %v at line %v, but %v is unbound.\n", identifierToBind.Value, identifierToBeBoundTo.Value, caller, lineNum, identifierToBind.Value)
panic(errMsg)
}
case Parser.ListToken:
newValue.Value = evalListToken(identifierToBind)
case Parser.TypeAnnToken:
errMsg := fmt.Sprintf("Error: expected identifier to %v in %v at line %v, but got type annotation token \":\".\n", identifierToBeBoundTo.Value, caller, lineNum)
panic(errMsg)
default:
errMsg := fmt.Sprintf("Error: unhandled token type for %v in %v at line %v.\n", identifierToBind.Value, caller, lineNum)
panic(errMsg)
}
return &newValue
}
func bindVars(list *Parser.Token, env Environment, lineNum int, global, mut bool, caller *string) Environment {
for i := 0; i < len(list.ListVals); i++ {
howManyIndicesToJumpForward := 1
isTypeAnnotated := false
firstListItem := &list.ListVals[i]
newBinding := parseNewIdentifier(firstListItem, &env, global, lineNum, caller)
newBinding.Name = firstListItem.Value
if i >= len(list.ListVals) {
errMsg := fmt.Sprintf("Error: nothing to assign to %v in %v at line %v.\n", firstListItem.Value, caller, lineNum)
panic(errMsg)
}
var potentialNewValue *ListCell
annotatedTypeName := ""
var annotatedTypeValue *ListCell
nextListItem := &list.ListVals[i+1]
if nextListItem.Type == Parser.TypeAnnToken {
if i+2 >= len(list.ListVals) {
errMsg := fmt.Sprintf("Error: no type and/or value provided in assignment to %v in %v at line %v.\n", firstListItem.Value, caller, lineNum)
panic(errMsg)
} else {
potentialTypeItem := &list.ListVals[i+2]
annotatedTypeValue, annotatedTypeName = parseType(firstListItem, potentialTypeItem, &env, lineNum, caller)
potentialNewValueItem := &list.ListVals[i+3]
potentialNewValue = parseIdentifierToBeBound(firstListItem, potentialNewValueItem, &env, global, lineNum, caller)
howManyIndicesToJumpForward = 3
isTypeAnnotated = true
}
} else {
potentialNewValue = parseIdentifierToBeBound(firstListItem, nextListItem, &env, global, lineNum, caller)
}
if isTypeAnnotated {
if (annotatedTypeName == "" && TypesEqualP(annotatedTypeValue, &env.findBinding(potentialNewValue.TypeName, true, true).Binding, lineNum, caller)) ||
(potentialNewValue.TypeName == "undecided" && potentialNewValue.TypeName == annotatedTypeName) {
if annotatedTypeName != "" {
potentialNewValue.TypeName = annotatedTypeName
}
} else {
errMsg := fmt.Sprintf("Error: attempting to assign type %v to %v in %v at line %v, but it is already of type %v.\n", annotatedTypeName, firstListItem.Value, caller, lineNum, potentialNewValue.TypeName)
panic(errMsg)
}
}
potentialNewValue.Mutable = mut
newBinding.Binding = *potentialNewValue
i += howManyIndicesToJumpForward
}
return env
}
func evalListToken(list *Parser.Token) ListCell {
firstVal := &list.ListVals[0]
// initCellList := CellList{}
initEnvironment := Environment{}
switch firstVal.Type {
case Parser.LiteralToken:
errMsg := fmt.Sprintf("Error: attempting to evaluate a literal, %v, at line %v.\n", firstVal.Value, firstVal.LineNum)
panic(errMsg)
case Parser.DefToken:
defKind := &firstVal.Value
if len(list.ListVals) < 3 {
errMsg := fmt.Sprintf("Error: too few arguments to %v at line %v.\n", defKind, firstVal.LineNum)
panic(errMsg)
} else if list.ListVals[1].Type != Parser.ListToken {
errMsg := fmt.Sprintf("Error: first argument (%v) to %v at line %v is not a list.\n", list.ListVals[1].Value, defKind, firstVal.LineNum)
panic(errMsg)
} else if list.ListVals[2].Type != Parser.ListToken {
errMsg := fmt.Sprintf("Error: second argument (%v) to %v at line %v is not a list.\n", list.ListVals[2].Value, defKind, firstVal.LineNum)
panic(errMsg)
} else if len(list.ListVals) > 3 {
errMsg := fmt.Sprintf("Error: too many arguments to %v at line %v.\n", defKind, firstVal.LineNum)
panic(errMsg)
}
tmpCallerName := "let"
initEnvironment = bindVars(&list.ListVals[1], initEnvironment, firstVal.LineNum, true, true, &tmpCallerName)
default:
errMsg := fmt.Sprintf("Error: unhandled token type for %v at line %v.\n", firstVal.Value, firstVal.LineNum)
panic(errMsg)
}
return ListCell{}
}
func Initialise(input string)ListCell{
res := Parser.Lex(&input)
tokens := Parser.ParseList(res, 0)
for _, tok := range tokens.ListVals {
fmt.Println(tok)
}
return evalListToken(&tokens.ListVals[0])
}