Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this should be generated by walking of the child classes of visitable
- interface Visitor {
- visitExpression(expression: Expression)
- visitStatement(statement: Statement)
- }
- abstract class Visitable {
- fun accept(visitor: Visitor) {
- // name of the visit function
- val thisKClass = this.javaClass.kotlin
- val name = thisKClass.simpleName
- val functionName = "visit$name"
- // find the function on the visitor
- val vKClass = visitor.javaClass.kotlin
- val functions = vKClass.memberFunctions
- val function = functions.find { it.name == functionName } ?: throw RuntimeError("Could not find visit function called '$functionName' on visitor", null)
- function.call(visitor, this)
- }
- }
- class Expression : Visitable()
- class Statement : Visitable()
- // Another option would be:
- // this should be generated by walking of the child classes of visitable
- interface Visitor {
- visitExpression(expression: Expression)
- visitStatement(statement: Statement)
- }
- interface Visitable {
- fun accept(visitor: Visitor)
- }
- class Expression : Visitable() {
- fun accept(visitor: Visitor) {
- visitor.visitExpression(this)
- }
- }
- class Statement : Visitable() {
- fun accept(visitor: Visitor) {
- visitor.visitStatement(this)
- }
- }
- // But where is the fun in that :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement