-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.go
61 lines (51 loc) · 1.26 KB
/
application.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
// Application is a type to express scheme procedure application.
// Application has a procedure and its argument as list which consists
// of Pair.
package scheme
type Application struct {
ObjectBase
procedure Object
arguments Object
}
type Invoker interface {
Invoke(Object) Object
}
func NewApplication(parent Object) *Application {
return &Application{
ObjectBase: ObjectBase{parent: parent},
}
}
func (a *Application) Eval() Object {
evaledObject := a.procedure.Eval()
switch evaledObject.(type) {
case Invoker:
return evaledObject.(Invoker).Invoke(a.arguments)
default:
runtimeError("invalid application")
return nil
}
}
func (a *Application) String() string {
// Exceptional handling for special form: quote
list := a.toList()
firstObject := list.ElementAt(0)
if firstObject.isVariable() && firstObject.(*Variable).content() == builtinSyntaxes["quote"] {
if a.arguments.isNull() {
return "(quote)"
} else {
return "'" + a.arguments.(*Pair).ElementAt(0).String()
}
}
return list.String()
}
func (a *Application) toList() *Pair {
list := NewPair(a.Parent())
list.Car = a.procedure
list.Car.setParent(list)
list.Cdr = a.arguments
list.Cdr.setParent(list)
return list
}
func (a *Application) isApplication() bool {
return true
}