-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement.go
More file actions
183 lines (147 loc) · 3.08 KB
/
Copy pathstatement.go
File metadata and controls
183 lines (147 loc) · 3.08 KB
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
package main
import (
"fmt"
"strings"
)
type Stmt interface {
isStmt()
fmt.Stringer // TODO: Standardize implementations or delete the stringer requirement
}
type NoOpStmt struct{}
func (NoOpStmt) isStmt() {}
func (n NoOpStmt) String() string {
return "no-op"
}
type ClassDecl struct {
name Token
methods []Stmt
staticMethods []Stmt
}
func (ClassDecl) isStmt() {}
func (c ClassDecl) String() string {
return c.name.lexeme
}
type VarDecl struct {
name Token
initializer Expr
}
func (VarDecl) isStmt() {}
func (d VarDecl) String() string {
return d.name.lexeme
}
type FunDecl struct {
name Token
params []Token
body Stmt // NOTE: book uses []Stmt, but `body` is always a Block stmt
}
func (FunDecl) isStmt() {}
func (f FunDecl) String() string {
return f.name.lexeme
}
type ExprStmt struct {
expr Expr
}
func (ExprStmt) isStmt() {}
func (e ExprStmt) String() string {
return e.expr.String() + ";"
}
type PrintStmt struct {
expr Expr
}
func (PrintStmt) isStmt() {}
func (p PrintStmt) String() string {
return "print " + p.expr.String() + ";"
}
type BreakStmt struct {
keyword Token
}
func (BreakStmt) isStmt() {}
func (BreakStmt) String() string {
return "break"
}
type CycleStmt struct {
keyword Token
}
func (CycleStmt) isStmt() {}
func (CycleStmt) String() string {
return "cycle"
}
type ReturnStmt struct {
keyword Token
value Expr
}
func (ReturnStmt) isStmt() {}
func (r ReturnStmt) String() string {
if _, ok := r.value.(fmt.Stringer); ok {
return "return " + r.value.String()
}
return "return"
}
type IfStmt struct {
token Token
condition Expr
thenBranch Stmt
elseBranch Stmt
}
func (IfStmt) isStmt() {}
func (i IfStmt) String() string {
var sb strings.Builder
sb.WriteString("if ")
sb.WriteString(i.condition.String())
sb.WriteByte('{')
sb.WriteString(i.thenBranch.String())
sb.WriteByte('}')
if i.elseBranch != nil {
// Technically, this have different logic for "else" and "else if"
// branches. Braces won't be shown correctly on else branches.
// But I won't fix it for now
sb.WriteString("else ")
sb.WriteString(i.elseBranch.String())
}
return sb.String()
}
type WhileStmt struct {
token Token
condition Expr
body Stmt
}
func (WhileStmt) isStmt() {}
func (w WhileStmt) String() string {
var sb strings.Builder
sb.WriteString("while ")
sb.WriteString(w.condition.String())
sb.WriteByte('{')
sb.WriteByte('\n')
sb.WriteString(w.body.String())
sb.WriteByte('}')
return sb.String()
}
type ForStmt struct {
token Token
initializer Stmt
condition Expr
increment Expr // increment clause
body Stmt
}
func (ForStmt) isStmt() {}
func (w ForStmt) String() string {
var sb strings.Builder
sb.WriteString("for ")
sb.WriteString(fmt.Sprintf("%s; %s; %s", w.initializer, w.condition, w.increment))
sb.WriteByte('{')
sb.WriteByte('\n')
sb.WriteString(w.body.String())
sb.WriteByte('}')
return sb.String()
}
type Block struct {
stmts []Stmt
}
func (Block) isStmt() {}
func (b Block) String() string {
s := ""
for _, stmt := range b.stmts {
s += (stmt.String() + "\n")
}
return s
}