|
|
|
|
@ -22,6 +22,7 @@ class Parser(private val tokens: List<Token>) {
|
|
|
|
|
private fun declaration(): Stmt? {
|
|
|
|
|
return try {
|
|
|
|
|
when {
|
|
|
|
|
match(FUN) -> function("function")
|
|
|
|
|
match(VAR) -> varDeclaration()
|
|
|
|
|
else -> statement()
|
|
|
|
|
}
|
|
|
|
|
@ -30,7 +31,26 @@ class Parser(private val tokens: List<Token>) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun function(kind: String): Stmt {
|
|
|
|
|
val name = consume(IDENTIFIER, "Expect $kind name.")
|
|
|
|
|
consume(LEFT_PAREN, "Expect '(' after $kind name.")
|
|
|
|
|
val parameters: MutableList<Token> = ArrayList()
|
|
|
|
|
if (!check(RIGHT_PAREN)) {
|
|
|
|
|
do {
|
|
|
|
|
if (parameters.size >= 255) {
|
|
|
|
|
error(peek(), "Cannot have more than 255 parameters.")
|
|
|
|
|
}
|
|
|
|
|
parameters.add(consume(IDENTIFIER, "Expect parameter name."))
|
|
|
|
|
} while (match(COMMA))
|
|
|
|
|
}
|
|
|
|
|
consume(RIGHT_PAREN, "Expect ')' after parameters.")
|
|
|
|
|
|
|
|
|
|
consume(LEFT_BRACE, "Expect '{' before $kind body.")
|
|
|
|
|
val body = blockStatement()
|
|
|
|
|
return Function(name, parameters, body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun varDeclaration(): Stmt {
|
|
|
|
|
val name = consume(IDENTIFIER, "Expect variable name.")
|
|
|
|
|
|
|
|
|
|
|