-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathstatements.go
237 lines (188 loc) · 5.18 KB
/
statements.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
package ast
import (
"bytes"
)
// ClassStatement represents a class node in AST
type ClassStatement struct {
*BaseNode
Name *Constant
Body *BlockStatement
SuperClass Expression
SuperClassName string
}
func (cs *ClassStatement) statementNode() {}
// TokenLiteral is a polymorphic function to return a token literal
func (cs *ClassStatement) TokenLiteral() string {
return cs.Token.Literal
}
func (cs *ClassStatement) String() string {
var out bytes.Buffer
out.WriteString("class ")
out.WriteString(cs.Name.TokenLiteral())
out.WriteString(" {\n")
out.WriteString(cs.Body.String())
out.WriteString("\n}")
return out.String()
}
// ModuleStatement represents a module node in AST
type ModuleStatement struct {
*BaseNode
Name *Constant
Body *BlockStatement
SuperClass *Constant
}
func (ms *ModuleStatement) statementNode() {}
// TokenLiteral is a polymorphic function to return a token literal
func (ms *ModuleStatement) TokenLiteral() string {
return ms.Token.Literal
}
func (ms *ModuleStatement) String() string {
var out bytes.Buffer
out.WriteString("module ")
out.WriteString(ms.Name.TokenLiteral())
out.WriteString(" {\n")
out.WriteString(ms.Body.String())
out.WriteString("\n}")
return out.String()
}
// ReturnStatement represents an expression as a return value
type ReturnStatement struct {
*BaseNode
ReturnValue Expression
}
func (rs *ReturnStatement) statementNode() {}
// TokenLiteral is a polymorphic function to return a token literal
func (rs *ReturnStatement) TokenLiteral() string {
return rs.Token.Literal
}
func (rs *ReturnStatement) String() string {
var out bytes.Buffer
out.WriteString(rs.TokenLiteral() + " ")
if rs.ReturnValue != nil {
out.WriteString(rs.ReturnValue.String())
}
out.WriteString(";")
return out.String()
}
// ExpressionStatement represents an expression statement
type ExpressionStatement struct {
*BaseNode
Expression Expression
}
func (es *ExpressionStatement) statementNode() {}
// TokenLiteral is a polymorphic function to return a token literal
func (es *ExpressionStatement) TokenLiteral() string {
return es.Token.Literal
}
func (es *ExpressionStatement) String() string {
if es.Expression != nil {
return es.Expression.String()
}
return ""
}
// DefStatement represents a "def" keyword with block
type DefStatement struct {
*BaseNode
Name *Identifier
Receiver Expression
Parameters []Expression
BlockStatement *BlockStatement
}
func (tds *DefStatement) statementNode() {}
// TokenLiteral is a polymorphic function to return a token literal
func (tds *DefStatement) TokenLiteral() string {
return tds.Token.Literal
}
func (tds *DefStatement) String() string {
var out bytes.Buffer
out.WriteString("def ")
out.WriteString(tds.Name.TokenLiteral())
out.WriteString("(")
for i, param := range tds.Parameters {
out.WriteString(param.String())
if i != len(tds.Parameters)-1 {
out.WriteString(", ")
}
}
out.WriteString(") ")
out.WriteString("{\n")
out.WriteString(tds.BlockStatement.String())
out.WriteString("\n}")
return out.String()
}
// NextStatement represents "next" keyword
type NextStatement struct {
*BaseNode
}
func (ns *NextStatement) statementNode() {}
// TokenLiteral is a polymorphic function to return a token literal
func (ns *NextStatement) TokenLiteral() string {
return ns.Token.Literal
}
func (ns *NextStatement) String() string {
return "next"
}
// BreakStatement represents "break" keyword
type BreakStatement struct {
*BaseNode
}
func (bs *BreakStatement) statementNode() {}
// TokenLiteral is a polymorphic function to return a token literal
func (bs *BreakStatement) TokenLiteral() string {
return bs.Token.Literal
}
func (bs *BreakStatement) String() string {
return bs.TokenLiteral()
}
// WhileStatement represents a "while" keyword with a block
type WhileStatement struct {
*BaseNode
Condition Expression
Body *BlockStatement
}
func (ws *WhileStatement) statementNode() {}
// TokenLiteral is a polymorphic function to return a token literal
func (ws *WhileStatement) TokenLiteral() string {
return ws.Token.Literal
}
func (ws *WhileStatement) String() string {
var out bytes.Buffer
out.WriteString("while ")
out.WriteString(ws.Condition.String())
out.WriteString(" do\n")
out.WriteString(ws.Body.String())
out.WriteString("\nend")
return out.String()
}
// BlockStatement represents a block statement
type BlockStatement struct {
*BaseNode
Statements []Statement
}
func (bs *BlockStatement) statementNode() {}
// TokenLiteral is a polymorphic function to return a token literal
func (bs *BlockStatement) TokenLiteral() string {
return bs.Token.Literal
}
func (bs *BlockStatement) String() string {
var out bytes.Buffer
for _, stmt := range bs.Statements {
out.WriteString(stmt.String())
}
return out.String()
}
// IsEmpty checks if the statements in the block is empty
func (bs *BlockStatement) IsEmpty() bool {
return len(bs.Statements) == 0
}
// KeepLastValue prevents block's last expression statement to be popped.
func (bs *BlockStatement) KeepLastValue() {
if len(bs.Statements) == 0 {
return
}
stmt := bs.Statements[len(bs.Statements)-1]
expStmt, ok := stmt.(*ExpressionStatement)
if ok {
expStmt.Expression.MarkAsExp()
}
}