|
|
|
|
@ -43,11 +43,20 @@ class Parser(private val tokens: List<Token>) {
|
|
|
|
|
return Var(name, initializer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun whileStatement(): Stmt {
|
|
|
|
|
consume(LEFT_PAREN, "Expect '(' after 'while'.")
|
|
|
|
|
val condition = expression()
|
|
|
|
|
consume(RIGHT_PAREN, "Expect ')' after condition.")
|
|
|
|
|
val body = statement()
|
|
|
|
|
|
|
|
|
|
return While(condition, body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun statement(): Stmt {
|
|
|
|
|
return when {
|
|
|
|
|
match(IF) -> ifStatement()
|
|
|
|
|
match(PRINT) -> printStatement()
|
|
|
|
|
match(WHILE) -> whileStatement()
|
|
|
|
|
match(LEFT_BRACE) -> Block(blockStatement())
|
|
|
|
|
else -> expressionStatement()
|
|
|
|
|
}
|
|
|
|
|
|