Skip to content

Commit 2eaded1

Browse files
committed
add while loops
1 parent 4570327 commit 2eaded1

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ declaration -> varDecl | statement ;
1111
1212
varDecl -> "var" IDENTIFIER ( "=" expression )? ";" ;
1313
14-
statement -> exprStmt | ifStmt | printStmt | block;
14+
statement -> exprStmt | ifStmt | printStmt | whileStmt | block;
1515
1616
exprStmt -> expression ";" ;
1717
ifStmt -> "if" "(" expression ")" statement ( "else" statement )? ;
1818
printStmt -> "print" expression ";" ;
19+
whileStmt -> "while "(" expression ")" statement ;
1920
block -> "{" declaration* "}" ;
2021
2122
expression -> assignment ;

src/com/boxfort/jlox/Interpreter.java

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ public Void visitVarStmt(Stmt.Var stmt) {
8484
return null;
8585
}
8686

87+
@Override
88+
public Void visitWhileStmt(Stmt.While stmt) {
89+
while(isTruthy(evaluate(stmt.condition))) {
90+
execute(stmt.body);
91+
}
92+
return null;
93+
}
94+
8795
@Override
8896
public Object visitAssignExpr(Expr.Assign expr) {
8997
Object value = evaluate(expr.value);

src/com/boxfort/jlox/Parser.java

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private Stmt varDeclaration() {
5252
private Stmt statement() {
5353
if (match(IF)) return ifStatement();
5454
if (match(PRINT)) return printStatement();
55+
if (match(WHILE)) return whileStatement();
5556
if (match(LEFT_BRACE)) return new Stmt.Block(block());
5657

5758
return expressionStatement();
@@ -78,6 +79,15 @@ private Stmt printStatement() {
7879
return new Stmt.Print(value);
7980
}
8081

82+
private Stmt whileStatement() {
83+
consume(LEFT_PAREN, "Expect '(' after 'While'.");
84+
Expr condition = expression();
85+
consume(RIGHT_PAREN, "Expect ')' after condition.");
86+
Stmt body = statement();
87+
88+
return new Stmt.While(condition, body);
89+
}
90+
8191
private Stmt expressionStatement() {
8292
Expr expr = expression();
8393
consume(SEMICOLON, "Expect ';' after expression.");

src/com/boxfort/jlox/Stmt.java

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface Visitor<R> {
99
R visitIfStmt(If stmt);
1010
R visitPrintStmt(Print stmt);
1111
R visitVarStmt(Var stmt);
12+
R visitWhileStmt(While stmt);
1213
}
1314

1415
static class Block extends Stmt {
@@ -82,5 +83,20 @@ <R> R accept(Visitor<R> visitor) {
8283
final Expr initializer;
8384
}
8485

86+
static class While extends Stmt {
87+
While(Expr condition, Stmt body) {
88+
this.condition = condition;
89+
this.body = body;
90+
}
91+
92+
@Override
93+
<R> R accept(Visitor<R> visitor) {
94+
return visitor.visitWhileStmt(this);
95+
}
96+
97+
final Expr condition;
98+
final Stmt body;
99+
}
100+
85101
abstract <R> R accept(Visitor<R> visitor);
86102
}

src/com/boxfort/tool/GenerateAst.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static void main(String[] args) throws IOException {
2828
"Expression : Expr expression",
2929
"If : Expr condition, Stmt thenBranch, Stmt elseBranch",
3030
"Print : Expr expression",
31-
"Var : Token name, Expr initializer"
31+
"Var : Token name, Expr initializer",
32+
"While : Expr condition, Stmt body"
3233
));
3334
}
3435

0 commit comments

Comments
 (0)