-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
118 lines (98 loc) · 3.01 KB
/
ast.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
package minimalisp
// Expression is the interface all types of expressions must fulfil.
type Expression interface {
Accept(visitor visitor) (interface{}, error)
}
// Visitor is the interface which an interpreter has to fulfil.
type visitor interface {
visitLiteralExpr(literalExpr *LiteralExpr) (interface{}, error)
visitDefvarExpr(defvarExpr *DefvarExpr) (interface{}, error)
visitVarExpr(varExpr *VarExpr) (interface{}, error)
visitIfExpr(ifExpr *IfExpr) (interface{}, error)
visitDefunExpr(defunExpr *DefunExpr) (interface{}, error)
visitFuncCallExpr(funcCallExpr *FuncCallExpr) (interface{}, error)
visitListExpr(listExpr *ListExpr) (interface{}, error)
visitLetExpr(letExpr *LetExpr) (interface{}, error)
visitLambdaExpr(lambdaExpr *LambdaExpr) (interface{}, error)
}
// LiteralExpr is a literal such as a string or a number.
type LiteralExpr struct {
Value interface{}
}
// Accept visits the literal expression.
func (e *LiteralExpr) Accept(visitor visitor) (interface{}, error) {
return visitor.visitLiteralExpr(e)
}
// DefvarExpr is a definition of a variable.
type DefvarExpr struct {
Name Token
Initializer Expression
}
// Accept visits the defvar expression.
func (e *DefvarExpr) Accept(visitor visitor) (interface{}, error) {
return visitor.visitDefvarExpr(e)
}
// VarExpr is a reference to a variable.
type VarExpr struct {
Name Token
}
// Accept visits the var expression.
func (e *VarExpr) Accept(visitor visitor) (interface{}, error) {
return visitor.visitVarExpr(e)
}
// IfExpr is an if expression :).
type IfExpr struct {
Condition Expression
ThenBranch Expression
ElseBranch Expression
}
// Accept visits the if expression.
func (e *IfExpr) Accept(visitor visitor) (interface{}, error) {
return visitor.visitIfExpr(e)
}
// DefunExpr is a definition of a function.
type DefunExpr struct {
Name Token
Params []Token
Body Expression
}
// Accept visits the function definition.
func (e *DefunExpr) Accept(visitor visitor) (interface{}, error) {
return visitor.visitDefunExpr(e)
}
// FuncCallExpr is a function call.
type FuncCallExpr struct {
Name Token
Arguments []Expression
}
// Accept visits the function call.
func (e *FuncCallExpr) Accept(visitor visitor) (interface{}, error) {
return visitor.visitFuncCallExpr(e)
}
// ListExpr represents a list collection.
type ListExpr struct {
Elements []Expression
}
// Accept visits the list expression.
func (e *ListExpr) Accept(visitor visitor) (interface{}, error) {
return visitor.visitListExpr(e)
}
// LetExpr is a let expression to define local variables.
type LetExpr struct {
Names []Token
Values []Expression
Body Expression
}
// Accept visits the let expression.
func (e *LetExpr) Accept(visitor visitor) (interface{}, error) {
return visitor.visitLetExpr(e)
}
// LambdaExpr is a lambda expression to define anonymous functions.
type LambdaExpr struct {
Params []Token
Body Expression
}
// Accept visits the lambda expression.
func (e *LambdaExpr) Accept(visitor visitor) (interface{}, error) {
return visitor.visitLambdaExpr(e)
}