-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.cs
210 lines (181 loc) · 6.06 KB
/
Interpreter.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
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
using System.Collections.Generic;
class Interpreter : ExprNamespace.Expr.Visitor<object>,
StmtNamespace.Stmt.Visitor<object>
{
private Environment environment = new Environment();
public void interpret(List<StmtNamespace.Stmt> statements)
{
try
{
foreach (StmtNamespace.Stmt statement in statements)
{
execute(statement);
}
}
catch (Errors.RuntimeError error)
{
LoxSharp.Program.runtimeError(error);
}
}
public object visitLiteralExpr(ExprNamespace.Expr.Literal expr)
{
return expr.value;
}
public object visitUnaryExpr(ExprNamespace.Expr.Unary expr)
{
object right = evaluate(expr.right);
switch (expr.op.type)
{
case LoxSharp.TokenType.BANG:
return !isTruthy(right);
case LoxSharp.TokenType.MINUS:
checkNumberOperand(expr.op, right);
return -(double)right;
}
return null; // Unreachable
}
public object visitVariableExpr(ExprNamespace.Expr.Variable expr)
{
return environment.get(expr.name);
}
private void checkNumberOperand(LoxSharp.Token op, object operand)
{
if (operand.GetType() == typeof(double)) return;
throw new Errors.RuntimeError(op, "Operand must be a number.");
}
private void checkNumberOperands(LoxSharp.Token op, object left, object right)
{
if (left.GetType() == typeof(double) && right.GetType() == typeof(double)) return;
throw new Errors.RuntimeError(op, "Operands must be numbers.");
}
private bool isTruthy(object obj)
{
if (obj == null) return false;
if (obj.GetType() == typeof(bool)) return (bool)obj;
return true;
}
private bool isEqual(object a, object b)
{
if (a == null && b == null) return true;
if (a == null) return false;
return a.Equals(b);
}
private string stringify(object obj)
{
if (obj == null) return "nil";
if (obj.GetType() == typeof(double))
{
string text = obj.ToString();
if (text.EndsWith(".0"))
{
text = text.Substring(0, text.Length - 3);
}
return text;
}
return obj.ToString();
}
public object visitGroupingExpr(ExprNamespace.Expr.Grouping expr)
{
return evaluate(expr.expression);
}
private object evaluate(ExprNamespace.Expr expr)
{
return expr.accept(this);
}
private void execute(StmtNamespace.Stmt stmt)
{
stmt.accept(this);
}
private void executeBlock(List<StmtNamespace.Stmt> statements, Environment environment)
{
Environment previous = this.environment;
try
{
this.environment = environment;
foreach (StmtNamespace.Stmt statement in statements)
{
execute(statement);
}
}
finally
{
this.environment = previous;
}
}
public object visitBlockStmt(StmtNamespace.Stmt.Block stmt)
{
executeBlock(stmt.statements, new Environment(environment));
return null;
}
public object visitExpressionStmt(StmtNamespace.Stmt.Expression stmt)
{
evaluate(stmt.expression);
return null;
}
public object visitPrintStmt(StmtNamespace.Stmt.Print stmt)
{
object value = evaluate(stmt.expression);
System.Console.WriteLine(stringify(value));
return null;
}
public object visitVarStmt(StmtNamespace.Stmt.Var stmt)
{
object value = null;
if (stmt.initializer != null)
{
value = evaluate(stmt.initializer);
}
environment.define(stmt.name.lexeme, value);
return null;
}
public object visitAssignExpr(ExprNamespace.Expr.Assign expr)
{
object value = evaluate(expr.value);
environment.assign(expr.name, value);
return value;
}
public object visitBinaryExpr(ExprNamespace.Expr.Binary expr)
{
object left = evaluate(expr.left);
object right = evaluate(expr.right);
switch (expr.op.type)
{
case LoxSharp.TokenType.GREATER:
checkNumberOperands(expr.op, left, right);
return (double)left > (double)right;
case LoxSharp.TokenType.GREATER_EQUAL:
checkNumberOperands(expr.op, left, right);
return (double)left >= (double)right;
case LoxSharp.TokenType.LESS:
checkNumberOperands(expr.op, left, right);
return (double)left < (double)right;
case LoxSharp.TokenType.LESS_EQUAL:
checkNumberOperands(expr.op, left, right);
return (double)left <= (double)right;
case LoxSharp.TokenType.MINUS:
checkNumberOperands(expr.op, left, right);
return (double)left - (double)right;
case LoxSharp.TokenType.PLUS:
if (left.GetType() == typeof(double) && right.GetType() == typeof(double))
{
return (double)left + (double)right;
}
if (left.GetType() == typeof(string) && right.GetType() == typeof(string))
{
return (string)left + (string)right;
}
throw new Errors.RuntimeError(expr.op, "Operands must be two numbers or two strings.");
case LoxSharp.TokenType.SLASH:
checkNumberOperands(expr.op, left, right);
return (double)left / (double)right;
case LoxSharp.TokenType.STAR:
checkNumberOperands(expr.op, left, right);
return (double)left * (double)right;
case LoxSharp.TokenType.BANG:
return !isEqual(left, right);
case LoxSharp.TokenType.BANG_EQUAL:
return !isEqual(left, right);
}
return null; // Unreachable
}
}