-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast_printer.go
226 lines (189 loc) · 5.78 KB
/
ast_printer.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
package tlps
import (
"bytes"
"fmt"
"strings"
)
// AstPrinter is struct of ast printer
type AstPrinter struct{}
// NewAstPrinter is constructor of AstPrinter
func NewAstPrinter() *AstPrinter {
return &AstPrinter{}
}
// Print prints given statements ast
func (ap *AstPrinter) Print(stmts []Stmt) (string, error) {
vals := make([]string, 0)
for _, stmt := range stmts {
val, err := stmt.Accept(ap)
if err != nil {
return "", err
}
vals = append(vals, val.(string))
}
return strings.Join(vals, "\n"), nil
}
func (ap *AstPrinter) visitBinaryExpr(expr *Binary) (interface{}, error) {
return ap.parenthesizeExpr(expr.Operator.Lexeme, expr.Left, expr.Right)
}
func (ap *AstPrinter) visitCallExpr(expr *Call) (interface{}, error) {
callee, _ := ap.parenthesizeExpr("callee", expr.Callee)
args := make([]string, 0)
for _, v := range expr.Arguments {
arg, _ := ap.parenthesizeExpr("arg", v)
args = append(args, arg)
}
return callee + "(args " + strings.Join(args, "))"), nil
}
func (ap *AstPrinter) visitGetExpr(expr *Get) (interface{}, error) {
object, err := ap.parenthesizeExpr("object", expr.Object)
if err != nil {
return "", err
}
return "(get " + object + " (property " + expr.Name.Lexeme + ")", nil
}
func (ap *AstPrinter) visitGroupingExpr(expr *Grouping) (interface{}, error) {
return ap.parenthesizeExpr("group", expr.Expression)
}
func (ap *AstPrinter) visitLiteralExpr(expr *Literal) (interface{}, error) {
if expr.Value == nil {
return "nil", nil
}
return fmt.Sprintf("%v", expr.Value), nil
}
func (ap *AstPrinter) visitLogicalExpr(expr *Logical) (interface{}, error) {
return ap.parenthesizeExpr(expr.Operator.Lexeme, expr.Left, expr.Right)
}
func (ap *AstPrinter) visitSetExpr(expr *Set) (interface{}, error) {
object, err := ap.parenthesizeExpr("object", expr.Object)
if err != nil {
return "", err
}
value, err := ap.parenthesizeExpr("value", expr.Value)
if err != nil {
return "", err
}
return "(set " + object + "(name " + expr.Name.Lexeme + ")" + value + ")", nil
}
func (ap *AstPrinter) visitSuperExpr(expr *Super) (interface{}, error) {
return "(super " + expr.Keyword.Lexeme + " " + expr.Method.Lexeme + ")", nil
}
func (ap *AstPrinter) visitThisExpr(expr *This) (interface{}, error) {
return "(this)", nil
}
func (ap *AstPrinter) visitUnaryExpr(expr *Unary) (interface{}, error) {
return ap.parenthesizeExpr(expr.Operator.Lexeme, expr.Right)
}
func (ap *AstPrinter) visitAssignExpr(expr *Assign) (interface{}, error) {
return ap.parenthesizeExpr("assign "+expr.Name.Lexeme, expr.Value)
}
func (ap *AstPrinter) visitVariableExpr(expr *Variable) (interface{}, error) {
return ap.parenthesizeExpr("variable", NewLiteral(expr.Name.Lexeme))
}
func (ap *AstPrinter) visitBlockStmt(b *Block) (interface{}, error) {
body := make([]string, 0)
for _, stmt := range b.Statements {
s, err := ap.parenthesizeStmt("block body", stmt)
if err != nil {
return "", err
}
body = append(body, s)
}
return "(block " + strings.Join(body, " ") + ")", nil
}
func (ap *AstPrinter) visitClassStmt(c *Class) (interface{}, error) {
fns := make([]string, 0)
for _, method := range c.Methods {
fn, err := method.Accept(ap)
if err != nil {
return "", err
}
fns = append(fns, fn.(string))
}
return "(class " + c.Name.Lexeme + " " + strings.Join(fns, " ") + ")", nil
}
func (ap *AstPrinter) visitExpressionStmt(e *Expression) (interface{}, error) {
return e.Expression.Accept(ap)
}
func (ap *AstPrinter) visitFunctionStmt(f *Function) (interface{}, error) {
params := make([]string, 0)
for _, v := range f.Params {
params = append(params, v.Lexeme)
}
stmts := make([]string, 0)
for _, stmt := range f.Body {
s, err := stmt.Accept(ap)
if err != nil {
return "", err
}
stmts = append(stmts, "("+s.(string)+")")
}
return "(function " + f.Name.Lexeme + " (args (" + strings.Join(params, ", ") + ")) (body " + strings.Join(stmts, " ") + "))", nil
}
func (ap *AstPrinter) visitIfStmt(i *If) (interface{}, error) {
cond, err := ap.parenthesizeExpr("cond", i.Condition)
if err != nil {
return "", nil
}
thenBranch, err := ap.parenthesizeStmt("thenBranch", i.ThenBranch)
if err != nil {
return "", nil
}
var elseBranch string
if i.ElseBranch != nil {
elseBranch, err = ap.parenthesizeStmt("elseBranch", i.ElseBranch)
if err != nil {
return "", err
}
}
return "(if " + cond + " " + thenBranch + " " + elseBranch + ")", nil
}
func (ap *AstPrinter) visitIncludeStmt(i *Include) (interface{}, error) {
return "(include " + i.Path.Lexeme + ")", nil
}
func (ap *AstPrinter) visitReturnStmt(r *Return) (interface{}, error) {
expr, err := ap.parenthesizeExpr(r.Keyword.Lexeme, r.Value)
if err != nil {
return "", err
}
return expr, nil
}
func (ap *AstPrinter) visitWhileStmt(p *While) (interface{}, error) {
cond, err := ap.parenthesizeExpr("cond", p.Condition)
if err != nil {
return "", err
}
body, err := ap.parenthesizeStmt("body", p.Body)
if err != nil {
return "", nil
}
return "(while " + cond + " " + body + ")", nil
}
func (ap *AstPrinter) visitVarStmt(v *Var) (interface{}, error) {
initializer, err := ap.parenthesizeExpr("initializer", v.Initializer)
if err != nil {
return "", nil
}
return "(declare " + v.Name.Lexeme + " " + initializer + ")", nil
}
func (ap *AstPrinter) parenthesizeExpr(name string, exprs ...Expr) (string, error) {
buf := bytes.Buffer{}
buf.WriteString("(" + name)
for _, expr := range exprs {
buf.WriteString(" ")
s, _ := expr.Accept(ap)
buf.WriteString(s.(string))
}
buf.WriteString(")")
return buf.String(), nil
}
func (ap *AstPrinter) parenthesizeStmt(name string, stmts ...Stmt) (string, error) {
buf := bytes.Buffer{}
buf.WriteString("(" + name)
for _, stmt := range stmts {
buf.WriteString(" ")
s, _ := stmt.Accept(ap)
buf.WriteString(s.(string))
}
buf.WriteString(")")
return buf.String(), nil
}