|
|
|
@ -22,6 +22,7 @@ class Parser(private val tokens: List<Token>) {
|
|
|
|
private fun declaration(): Stmt? {
|
|
|
|
private fun declaration(): Stmt? {
|
|
|
|
return try {
|
|
|
|
return try {
|
|
|
|
when {
|
|
|
|
when {
|
|
|
|
|
|
|
|
match(CLASS) -> classDeclaration()
|
|
|
|
match(FUN) -> function("function")
|
|
|
|
match(FUN) -> function("function")
|
|
|
|
match(VAR) -> varDeclaration()
|
|
|
|
match(VAR) -> varDeclaration()
|
|
|
|
else -> statement()
|
|
|
|
else -> statement()
|
|
|
|
@ -32,7 +33,21 @@ class Parser(private val tokens: List<Token>) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun function(kind: String): Stmt {
|
|
|
|
private fun classDeclaration(): ClassStmt {
|
|
|
|
|
|
|
|
val name = consume(IDENTIFIER, "Expect class name.")
|
|
|
|
|
|
|
|
consume(LEFT_BRACE, "Expect '{' before class body.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val methods: MutableList<Function> = ArrayList()
|
|
|
|
|
|
|
|
while (!check(RIGHT_BRACE) && !isAtEnd()) {
|
|
|
|
|
|
|
|
methods.add(function("method"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
consume(RIGHT_BRACE, "Expect '}' after class body.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ClassStmt(name, methods)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun function(kind: String): Function {
|
|
|
|
val name = consume(IDENTIFIER, "Expect $kind name.")
|
|
|
|
val name = consume(IDENTIFIER, "Expect $kind name.")
|
|
|
|
consume(LEFT_PAREN, "Expect '(' after $kind name.")
|
|
|
|
consume(LEFT_PAREN, "Expect '(' after $kind name.")
|
|
|
|
val parameters: MutableList<Token> = ArrayList()
|
|
|
|
val parameters: MutableList<Token> = ArrayList()
|
|
|
|
@ -51,7 +66,7 @@ class Parser(private val tokens: List<Token>) {
|
|
|
|
return Function(name, parameters, body)
|
|
|
|
return Function(name, parameters, body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun varDeclaration(): Stmt {
|
|
|
|
private fun varDeclaration(): Var {
|
|
|
|
val name = consume(IDENTIFIER, "Expect variable name.")
|
|
|
|
val name = consume(IDENTIFIER, "Expect variable name.")
|
|
|
|
|
|
|
|
|
|
|
|
var initializer: Expr? = null
|
|
|
|
var initializer: Expr? = null
|
|
|
|
@ -63,7 +78,7 @@ class Parser(private val tokens: List<Token>) {
|
|
|
|
return Var(name, initializer)
|
|
|
|
return Var(name, initializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun whileStatement(): Stmt {
|
|
|
|
private fun whileStatement(): While {
|
|
|
|
consume(LEFT_PAREN, "Expect '(' after 'while'.")
|
|
|
|
consume(LEFT_PAREN, "Expect '(' after 'while'.")
|
|
|
|
val condition = expression()
|
|
|
|
val condition = expression()
|
|
|
|
consume(RIGHT_PAREN, "Expect ')' after condition.")
|
|
|
|
consume(RIGHT_PAREN, "Expect ')' after condition.")
|
|
|
|
@ -120,7 +135,7 @@ class Parser(private val tokens: List<Token>) {
|
|
|
|
return body
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun ifStatement(): Stmt {
|
|
|
|
private fun ifStatement(): If {
|
|
|
|
consume(LEFT_PAREN, "Expect '(' after 'if'.")
|
|
|
|
consume(LEFT_PAREN, "Expect '(' after 'if'.")
|
|
|
|
val condition = expression()
|
|
|
|
val condition = expression()
|
|
|
|
consume(RIGHT_PAREN, "Expect ')' after if condition.")
|
|
|
|
consume(RIGHT_PAREN, "Expect ')' after if condition.")
|
|
|
|
@ -135,20 +150,20 @@ class Parser(private val tokens: List<Token>) {
|
|
|
|
return If(condition, thenBranch, elseBranch)
|
|
|
|
return If(condition, thenBranch, elseBranch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun printStatement(): Stmt {
|
|
|
|
private fun printStatement(): Print {
|
|
|
|
val value = expression()
|
|
|
|
val value = expression()
|
|
|
|
consume(SEMICOLON, "Expect ';' after value.")
|
|
|
|
consume(SEMICOLON, "Expect ';' after value.")
|
|
|
|
return Print(value)
|
|
|
|
return Print(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun returnStatement(): Stmt {
|
|
|
|
private fun returnStatement(): Return {
|
|
|
|
val keyword = previous()
|
|
|
|
val keyword = previous()
|
|
|
|
val value = if (!check(SEMICOLON)) expression() else null
|
|
|
|
val value = if (!check(SEMICOLON)) expression() else null
|
|
|
|
consume(SEMICOLON, "Expect ';' after return value.")
|
|
|
|
consume(SEMICOLON, "Expect ';' after return value.")
|
|
|
|
return Return(keyword, value)
|
|
|
|
return Return(keyword, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun expressionStatement(): Stmt {
|
|
|
|
private fun expressionStatement(): Expression {
|
|
|
|
val value = expression()
|
|
|
|
val value = expression()
|
|
|
|
consume(SEMICOLON, "Expect ';' after expression.")
|
|
|
|
consume(SEMICOLON, "Expect ';' after expression.")
|
|
|
|
return Expression(value)
|
|
|
|
return Expression(value)
|
|
|
|
|