You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.3 KiB
Kotlin
103 lines
2.3 KiB
Kotlin
package fr.celticinfo.lox
|
|
|
|
/**
|
|
* The StmtVisitor interface is used to visit the different types of statements that can be parsed by the Parser.
|
|
*/
|
|
interface StmtVisitor<R> {
|
|
fun visitBlock(stmt: Block): R
|
|
fun visitClassStmt(stmt: ClassStmt): R
|
|
fun visitExpression(stmt: Expression): R
|
|
fun visitFunction(stmt: Function): R
|
|
fun visitIf(stmt: If): R
|
|
fun visitPrint(stmt: Print): R
|
|
fun visitReturn(stmt: Return): R
|
|
fun visitVar(stmt: Var): R
|
|
fun visitWhile(stmt: While): R
|
|
}
|
|
|
|
/**
|
|
* The Stmt class represents the different types of statements that can be parsed by the Parser.
|
|
*/
|
|
sealed class Stmt {
|
|
abstract fun <R> accept(visitor: StmtVisitor<R>): R
|
|
}
|
|
|
|
data class Block(
|
|
val statements: List<Stmt?>
|
|
) : Stmt() {
|
|
override fun <R> accept(visitor: StmtVisitor<R>): R {
|
|
return visitor.visitBlock(this)
|
|
}
|
|
}
|
|
|
|
data class ClassStmt(
|
|
val name: Token,
|
|
val superClass: Variable?,
|
|
val methods: List<Function>
|
|
) : Stmt() {
|
|
override fun <R> accept(visitor: StmtVisitor<R>): R {
|
|
return visitor.visitClassStmt(this)
|
|
}
|
|
}
|
|
|
|
data class Expression(
|
|
val expression: Expr
|
|
) : Stmt() {
|
|
override fun <R> accept(visitor: StmtVisitor<R>): R {
|
|
return visitor.visitExpression(this)
|
|
}
|
|
}
|
|
|
|
data class Function(
|
|
val name: Token,
|
|
val params: List<Token>,
|
|
val body: List<Stmt?>
|
|
) : Stmt() {
|
|
override fun <R> accept(visitor: StmtVisitor<R>): R {
|
|
return visitor.visitFunction(this)
|
|
}
|
|
}
|
|
|
|
data class If(
|
|
val condition: Expr,
|
|
val thenBranch: Stmt,
|
|
val elseBranch: Stmt?
|
|
) : Stmt() {
|
|
override fun <R> accept(visitor: StmtVisitor<R>): R {
|
|
return visitor.visitIf(this)
|
|
}
|
|
}
|
|
data class Print(
|
|
val expression: Expr
|
|
) : Stmt() {
|
|
override fun <R> accept(visitor: StmtVisitor<R>): R {
|
|
return visitor.visitPrint(this)
|
|
}
|
|
}
|
|
|
|
data class Return(
|
|
val keyword: Token,
|
|
val value: Expr?
|
|
) : Stmt() {
|
|
override fun <R> accept(visitor: StmtVisitor<R>): R {
|
|
return visitor.visitReturn(this)
|
|
}
|
|
}
|
|
|
|
data class Var(
|
|
val name: Token,
|
|
val initializer: Expr?
|
|
) : Stmt() {
|
|
override fun <R> accept(visitor: StmtVisitor<R>): R {
|
|
return visitor.visitVar(this)
|
|
}
|
|
}
|
|
|
|
data class While(
|
|
val condition: Expr,
|
|
val body: Stmt
|
|
) : Stmt() {
|
|
override fun <R> accept(visitor: StmtVisitor<R>): R {
|
|
return visitor.visitWhile(this)
|
|
}
|
|
} |