-
Notifications
You must be signed in to change notification settings - Fork 0
/
AstPrinter.cs
82 lines (65 loc) · 1.82 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
using System.Text;
namespace SharpLox;
public class AstPrinter : Expr.Visitor<string>
{
public string Print(Expr expr) => expr.Accept(this);
public string VisitAssignExpr(Expr.Assign expr)
{
throw new NotImplementedException();
}
public string VisitBinaryExpr(Expr.Binary expr)
{
return Parenthesize(expr.Operator.Lexeme, expr.Left, expr.Right);
}
public string VisitCallExpr(Expr.Call expr)
{
throw new NotImplementedException();
}
public string VisitGetExpr(Expr.Get expr)
{
throw new NotImplementedException();
}
public string VisitGroupingExpr(Expr.Grouping expr)
{
return Parenthesize("group", expr.Expression);
}
public string VisitLiteralExpr(Expr.Literal expr)
{
return expr.Value?.ToString() ?? "nil";
}
public string VisitLogicalExpr(Expr.Logical expr)
{
throw new NotImplementedException();
}
public string VisitSetExpr(Expr.Set expr)
{
throw new NotImplementedException();
}
public string VisitSuperExpr(Expr.Super expr)
{
throw new NotImplementedException();
}
public string VisitThisExpr(Expr.This expr)
{
throw new NotImplementedException();
}
public string VisitUnaryExpr(Expr.Unary expr)
{
return Parenthesize(expr.Operator.Lexeme, expr.Right);
}
public string VisitVariableExpr(Expr.Variable expr)
{
throw new NotImplementedException();
}
private string Parenthesize(string name, params Expr[] exprs)
{
var builder = new StringBuilder();
builder.Append('(').Append(name);
foreach (var expr in exprs)
{
builder.Append(' ').Append(expr.Accept(this));
}
builder.Append(')');
return builder.ToString();
}
}