-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.go
200 lines (161 loc) · 4.11 KB
/
stmt.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
package main
import "fmt"
type StmtVisitor interface {
visitPrintStmt(PrintStmt) error
visitExpressionStmt(ExpressionStmt) error
visitVarStmt(VarStmt) error
visitBlockStmt(BlockStmt) error
visitIFStmt(IFStmt) error
visitWhileStmt(WhileStmt) error
visitFunctionStmt(FunctionStmt) error
visitReturnStmt(ReturnStmt) error
visitClassStmt(ClassStmt) error
}
type Stmt interface {
acceptStmtVisitor(StmtVisitor) error
}
type PrintStmt struct {
expr Expr
}
func newPrintStmt(expr Expr) Stmt {
return PrintStmt{expr: expr}
}
func (stmt PrintStmt) acceptStmtVisitor(visitor StmtVisitor) error {
return visitor.visitPrintStmt(stmt)
}
func (stmt PrintStmt) String() string {
return fmt.Sprintf("print stmt, expr: %s", stmt.expr)
}
type ExpressionStmt struct {
expr Expr
}
func newExpressionStmt(expr Expr) Stmt {
return ExpressionStmt{
expr: expr,
}
}
func (stmt ExpressionStmt) acceptStmtVisitor(visitor StmtVisitor) error {
return visitor.visitExpressionStmt(stmt)
}
func (stmt ExpressionStmt) String() string {
return fmt.Sprintf("expression stmt, expr: %s", stmt.expr)
}
type VarStmt struct {
expr Expr // initializer
name token
}
func newVarStmt(name token, expr Expr) Stmt {
return VarStmt{
expr: expr,
name: name,
}
}
func (stmt VarStmt) acceptStmtVisitor(visitor StmtVisitor) error {
return visitor.visitVarStmt(stmt)
}
func (stmt VarStmt) String() string {
if stmt.expr == nil {
return fmt.Sprintf("var stmt, %s ;", stmt.name)
}
return fmt.Sprintf("var stmt, %s = %s;", stmt.name, stmt.expr)
}
type BlockStmt struct {
stmts []Stmt
}
func newBlockStmt(stmts []Stmt) Stmt {
return BlockStmt{
stmts: stmts,
}
}
func (stmt BlockStmt) acceptStmtVisitor(visitor StmtVisitor) error {
return visitor.visitBlockStmt(stmt)
}
func (stmt BlockStmt) String() string {
return fmt.Sprintf("block stmt, { %s }", stmt.stmts)
}
type IFStmt struct {
condition Expr
thenBranch Stmt
elseBranch Stmt
}
func newIFStmt(condition Expr, thenBranch Stmt, elseBranch Stmt) Stmt {
return IFStmt{
condition: condition,
thenBranch: thenBranch,
elseBranch: elseBranch,
}
}
func (stmt IFStmt) acceptStmtVisitor(visitor StmtVisitor) error {
return visitor.visitIFStmt(stmt)
}
func (stmt IFStmt) String() string {
return fmt.Sprintf("if stmt, condition: %s, then: %s, else: %s", stmt.condition, stmt.thenBranch, stmt.elseBranch)
}
type WhileStmt struct {
condition Expr
body Stmt
}
func newWhileStmt(condition Expr, body Stmt) Stmt {
return WhileStmt{
condition: condition,
body: body,
}
}
func (stmt WhileStmt) acceptStmtVisitor(visitor StmtVisitor) error {
return visitor.visitWhileStmt(stmt)
}
func (stmt WhileStmt) String() string {
return fmt.Sprintf("while stmt, condition:(%s), body:{%s}", stmt.condition, stmt.body)
}
type FunctionStmt struct {
name token
params []token
stmts []Stmt // body
}
func newFunctionStmt(name token, params []token, body []Stmt) Stmt {
return FunctionStmt{
name: name,
params: params,
stmts: body,
}
}
func (stmt FunctionStmt) acceptStmtVisitor(visitor StmtVisitor) error {
return visitor.visitFunctionStmt(stmt)
}
func (stmt FunctionStmt) String() string {
return fmt.Sprintf("funtion stmt, name: %s, params: %s, body: %s", stmt.name, stmt.params, stmt.stmts)
}
type ReturnStmt struct {
keyword token
value Expr
}
func newReturnStmt(keyword token, value Expr) Stmt {
return ReturnStmt{
keyword: keyword,
value: value,
}
}
func (stmt ReturnStmt) acceptStmtVisitor(visitor StmtVisitor) error {
return visitor.visitReturnStmt(stmt)
}
func (stmt ReturnStmt) String() string {
return fmt.Sprintf("return stmt, value: %s", stmt.value)
}
type ClassStmt struct {
name token
methods []FunctionStmt
superclass *VarExpr
}
func newClassStmt(name token, superclass *VarExpr, methods []FunctionStmt) Stmt {
return ClassStmt{
name: name,
methods: methods,
superclass: superclass,
}
}
func (stmt ClassStmt) acceptStmtVisitor(visitor StmtVisitor) error {
return visitor.visitClassStmt(stmt)
}
func (stmt ClassStmt) String() string {
return fmt.Sprintf("class stmt, name: %s, superclass:%s functions: %s", stmt.name, stmt.superclass, stmt.methods)
}