-
Notifications
You must be signed in to change notification settings - Fork 0
/
AstPrinter.cs
136 lines (112 loc) · 3.97 KB
/
AstPrinter.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpLox
{
public class AstPrinter : IVisitor<string>
{
public static string Print(Expr expr) => expr.AcceptVisitor(new AstPrinter());
private string Parenthesize(string name, params Expr[] exprs)
{
var sb = new StringBuilder();
sb.Append('(').Append(name);
foreach (Expr expr in exprs)
{
sb.Append(' ');
sb.Append(expr.AcceptVisitor(this));
}
sb.Append(')');
return sb.ToString();
}
public string Visit(Variable expr)
{
return expr.Name.Lexeme;
}
public string Visit(ClassDeclaration stmnt)
{
return $"class {stmnt.Name}{(stmnt.Superclass != null ? $" < {stmnt.Superclass.AcceptVisitor(this)}" : "" )}\n{{\n{string.Join("\n\n", stmnt.Methods.Select(meth => meth.AcceptVisitor(this)))}\n}}\n";
}
public string Visit(BlockStatement stmnt)
{
return $"\n{{\n{string.Join("", stmnt.Statements.Select(st => st.AcceptVisitor(this)))}}}\n";
}
public string Visit(Function stmnt)
{
return $"fun {stmnt.Name.Lexeme} ({string.Join(",", stmnt.Parameters.Select(p => p.Lexeme))})\n{{\n{string.Join("", stmnt.Body.Select(st => st.AcceptVisitor(this)))}}}\n";
}
public string Visit(IfStatement stmnt)
{
return $"if ({stmnt.Condition.AcceptVisitor(this)}) {stmnt.ThenBranch}{(stmnt.ElseBranch != null ? $" else {stmnt.ElseBranch}" : "")}";
}
public string Visit(WhileStatement stmnt)
{
return $"While ({stmnt.Condition.AcceptVisitor(this)}) {stmnt.Body}";
}
public string Visit(Assign expr)
{
return Parenthesize($"= {expr.Name.Lexeme}", expr.Value);
}
public string Visit(ExpressionStatement stmnt)
{
return $"{stmnt.Expression.AcceptVisitor(this)};\n";
}
public string Visit(PrintStatement stmnt)
{
return $"print {stmnt.Expression.AcceptVisitor(this)};\n";
}
public string Visit(ReturnStatement stmnt)
{
return $"{stmnt.Keyword.Lexeme} {stmnt.Value.AcceptVisitor(this)};\n";
}
public string Visit(VarStatement stmnt)
{
return $"var {stmnt.Name.Lexeme}{(stmnt.Initializer != null ? $" = {stmnt.Initializer}" : "")};\n";
}
public string Visit(Binary expr)
{
return Parenthesize(expr.Op.Lexeme, expr.Left, expr.Right);
}
public string Visit(Call expr)
{
return $"{expr.Callee.AcceptVisitor(this)}({string.Join(",", expr.Args.Select(arg => arg.AcceptVisitor(this)))})";
}
public string Visit(Get expr)
{
return $"(. {expr.Obj.AcceptVisitor(this)} {expr.Name.Lexeme})";
}
public string Visit(Grouping expr)
{
return Parenthesize("grouping", expr.Expression);
}
public string Visit(Literal expr)
{
return expr.Value is null ? "nil" : expr.Value.ToString();
}
public string Visit(Logical expr)
{
return Parenthesize(expr.Op.Lexeme, expr.Left, expr.Right);
}
public string Visit(SetExpr expr)
{
throw new NotImplementedException();
}
public string Visit(Super expr)
{
return $"{expr.Keyword.Lexeme}.{expr.Method.Lexeme}";
}
public string Visit(This expr)
{
return expr.Keyword.Lexeme;
}
public string Visit(Unary expr)
{
return Parenthesize(expr.Op.Lexeme, expr.Right);
}
public string Visit(Ternary expr)
{
return Parenthesize("?:", expr.Left, expr.Middle, expr.Right);
}
}
}