-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhileNode.java
29 lines (26 loc) · 952 Bytes
/
WhileNode.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
import java.util.Scanner;
/**
* Record responsible for representing a WHILE node that will loop over a block of code while some condition is met.
*/
record WhileNode(CondNode condition, BlockNode blockNode) implements ProgramNode {
/**
* Parses in and returns a StatementNode containing a while loop.
*/
static StatementNode parse(Scanner s) {
Parser.require(Parser.OPEN_PAREN_PAT, "Missing open parenthesis.", s);
var cond = CondNode.parse(s);
Parser.require(Parser.CLOSE_PAREN_PAT, "Missing open parenthesis.", s);
var block = BlockNode.parse(s);
return StatementNode.of(new WhileNode(cond, block));
}
@Override
public void execute(Robot robot) {
while (condition.asBool(robot)) {
blockNode().execute(robot);
}
}
@Override
public String toString() {
return "while(" + condition.toString() + ")" + blockNode.toString();
}
}