-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stmt.java
146 lines (128 loc) · 3.71 KB
/
Stmt.java
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
package SLox;
import java.util.List;
abstract class Stmt {
interface Visitor<R> {
R visitPrintStmt(Print expr);
R visitExpressionStmt(Expression stmt);
R visitBlockStmt(Block stmt);
R visitIfStmt(If stmt);
R visitWhileStmt(While stmt);
R visitVarStmt(Var stmt);
R visitFunctionStmt(Function stmt);
R visitReturnStmt(Return stmt);
R visitClassStmt(Class stmt);
}
static class Print extends Stmt {
Print(Expr expression) {
this.expression = expression;
}
final Expr expression;
@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitPrintStmt(this);
}
}
static class Expression extends Stmt {
Expression(Expr expression) {
this.expression = expression;
}
final Expr expression;
@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitExpressionStmt(this);
}
}
static class Var extends Stmt {
Var(Token name, Expr initializer) {
this.name = name;
this.initializer = initializer;
}
@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitVarStmt(this);
}
final Token name;
final Expr initializer;
}
static class Block extends Stmt {
Block(List<Stmt> statements) {
this.statements = statements;
}
@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitBlockStmt(this);
}
final List<Stmt> statements;
}
static class If extends Stmt {
If(Expr condition, Stmt thenBranch, Stmt elseBranch) {
this.condition = condition;
this.thenBranch = thenBranch;
this.elseBranch = elseBranch;
}
@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitIfStmt(this);
}
final Expr condition;
final Stmt thenBranch;
final Stmt elseBranch;
}
static class While extends Stmt {
While(Expr condition, Stmt body) {
this.condition = condition;
this.body = body;
}
@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitWhileStmt(this);
}
final Expr condition;
final Stmt body;
}
static class Function extends Stmt {
Function(Token name, List<Token> params, List<Stmt> body, boolean isGetter) {
this.name = name;
this.params = params;
this.body = body;
this.isGetter = isGetter;
}
@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitFunctionStmt(this);
}
final Token name;
final List<Token> params;
final List<Stmt> body;
final boolean isGetter;
}
static class Return extends Stmt {
Return(Token keyword, Expr value) {
this.value = value;
this.keyword = keyword;
}
@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitReturnStmt(this);
}
final Expr value;
final Token keyword;
}
static class Class extends Stmt {
Class(Token name,
Expr.Variable superclass,
List<Stmt.Function> methods) {
this.name = name;
this.superclass = superclass;
this.methods = methods;
}
@Override
<R> R accept(Visitor<R> visitor) {
return visitor.visitClassStmt(this);
}
final Token name;
final Expr.Variable superclass;
final List<Stmt.Function> methods;
}
abstract <R> R accept(Visitor<R> visitor);
}