-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStmt.cs
177 lines (147 loc) · 4.57 KB
/
Stmt.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
using System.Collections.Generic;
// group members: Peter Zhang, Madeline Moore, Cara Cannarozzi
// Crafting Interpreters book by Robert Nystrom used as a reference
// https://craftinginterpreters.com/contents.html
namespace LoxInterpreter
{
/// <summary>
/// statement class that holds all types of statements
/// </summary>
public abstract class Stmt
{
public abstract T Accept<T>(IVisitor<T> visitor);
// IVisitor interface with all relevant statement types
public interface IVisitor<T>
{
T VisitBlockStmt(Block stmt);
T VisitExpressionStmt(Expression stmt);
T VisitFunctionStmt(Function stmt);
T VisitIfStmt(If stmt);
T VisitPrintStmt(Print stmt);
T VisitReturnStmt(Return stmt);
T VisitVarStmt(Var stmt);
T VisitWhileStmt(While stmt);
}
// defines block statements
public class Block : Stmt
{
public List<Stmt> statements;
public Block(List<Stmt> statements)
{
this.statements = statements;
}
public override T Accept<T>(IVisitor<T> visitor)
{
return visitor.VisitBlockStmt(this);
}
}
// defines expression statements
public class Expression : Stmt
{
public Expr expression;
public Expression(Expr expression)
{
this.expression = expression;
}
public override T Accept<T>(IVisitor<T> visitor)
{
return visitor.VisitExpressionStmt(this);
}
}
// defines function statements
public class Function : Stmt
{
public List<Stmt> body;
public Token name;
public List<Token> parms;
public Function(Token name, List<Token> parms, List<Stmt> body)
{
this.name = name;
this.parms = parms;
this.body = body;
}
public override
T Accept<T>(IVisitor<T> visitor)
{
return visitor.VisitFunctionStmt(this);
}
}
// defines if statements
public class If : Stmt
{
public Expr condition;
public Stmt elseBranch;
public Stmt thenBranch;
public If(Expr condition, Stmt thenBranch, Stmt elseBranch)
{
this.condition = condition;
this.thenBranch = thenBranch;
this.elseBranch = elseBranch;
}
public override T Accept<T>(IVisitor<T> visitor)
{
return visitor.VisitIfStmt(this);
}
}
// defines print statements
public class Print : Stmt
{
public Expr expression;
public Print(Expr expression)
{
this.expression = expression;
}
public override T Accept<T>(IVisitor<T> visitor)
{
return visitor.VisitPrintStmt(this);
}
}
// defines return statements
public class Return : Stmt
{
public readonly Token Keyword;
public readonly Expr Value;
public Return(Token keyword, Expr value)
{
this.Keyword = keyword;
this.Value = value;
}
public override T Accept<T>(IVisitor<T> visitor)
{
return visitor.VisitReturnStmt(this);
}
}
// defines variable statements
public class Var : Stmt
{
public Expr initializer;
public Token name;
public Var(Token name, Expr initializer)
{
this.name = name;
this.initializer = initializer;
}
public override
T Accept<T>(IVisitor<T> visitor)
{
return visitor.VisitVarStmt(this);
}
}
// defines while statements
public class While : Stmt
{
public Stmt body;
public Expr condition;
public While(Expr condition, Stmt body)
{
this.condition = condition;
this.body = body;
}
public override
T Accept<T>(IVisitor<T> visitor)
{
return visitor.VisitWhileStmt(this);
}
}
}
}