11package com.aymanetech.resolver
22
33import com.aymanetech.interpreter.Interpreter
4- import com.aymanetech.Lox
4+ import com.aymanetech.runtime.errors.ErrorHandler
55import com.aymanetech.ast.Expr
66import com.aymanetech.ast.Stmt
77import com.aymanetech.lexer.Token
88import java.util.Stack
99import kotlin.collections.forEach
1010
11- class Resolver (private val interpreter : Interpreter ) : Expr.Visitor<Unit>, Stmt.Visitor<Unit> {
11+ class Resolver (
12+ private val interpreter : Interpreter ,
13+ private val errorHandler : ErrorHandler
14+ ) : Expr.Visitor<Unit>, Stmt.Visitor<Unit> {
1215 private val scopes: Stack <MutableMap <String , Boolean >> = Stack ()
1316 private var currentFunction = FunctionType .NONE
1417 private var currentClass = ClassType .NONE
@@ -43,7 +46,7 @@ class Resolver(private val interpreter: Interpreter) : Expr.Visitor<Unit>, Stmt.
4346
4447 override fun visit (expr : Expr .Variable ) {
4548 if (scopes.isNotEmpty() && scopes.peek()[expr.name.lexeme] == false )
46- Lox .error (expr.name, " Can't read local variable in it's own initializer" )
49+ errorHandler.reportError (expr.name, " Can't read local variable in it's own initializer" )
4750
4851 resolveLocal(expr, expr.name)
4952 }
@@ -60,16 +63,16 @@ class Resolver(private val interpreter: Interpreter) : Expr.Visitor<Unit>, Stmt.
6063
6164 override fun visit (expr : Expr .Super ) {
6265 if (currentClass == ClassType .NONE )
63- Lox .error (expr.keyword, " Can't use 'super' outside of a class." )
66+ errorHandler.reportError (expr.keyword, " Can't use 'super' outside of a class." )
6467 else if (currentClass != ClassType .SUBCLASS )
65- Lox .error (expr.keyword, " Can't use 'super' in a class without a superclass." )
68+ errorHandler.reportError (expr.keyword, " Can't use 'super' in a class without a superclass." )
6669
6770 resolveLocal(expr, expr.keyword)
6871 }
6972
7073 override fun visit (expr : Expr .This ) {
7174 if (currentClass == ClassType .NONE ) {
72- Lox .error (expr.keyword, " Can't use 'this' outside of a method" )
75+ errorHandler.reportError (expr.keyword, " Can't use 'this' outside of a method" )
7376 return
7477 }
7578 resolveLocal(expr, expr.keyword)
@@ -128,10 +131,10 @@ class Resolver(private val interpreter: Interpreter) : Expr.Visitor<Unit>, Stmt.
128131
129132 override fun visit (stmt : Stmt .Return ) {
130133 if (currentFunction == FunctionType .NONE )
131- Lox .error (stmt.token, " Can't return from top-level code" )
134+ errorHandler.reportError (stmt.token, " Can't return from top-level code" )
132135
133136 if (stmt.value != null && currentFunction == FunctionType .INITIALIZER )
134- Lox .error (stmt.token, " Can't return from a constructor" )
137+ errorHandler.reportError (stmt.token, " Can't return from a constructor" )
135138
136139 stmt.value?.let { resolve(it) }
137140 }
@@ -142,7 +145,7 @@ class Resolver(private val interpreter: Interpreter) : Expr.Visitor<Unit>, Stmt.
142145 declare(stmt.name)
143146 define(stmt.name)
144147 if (stmt.superClass != null && stmt.name.lexeme.equals(stmt.superClass.name.lexeme))
145- Lox .error (stmt.superClass.name, " A class can't inherit from itself" )
148+ errorHandler.reportError (stmt.superClass.name, " A class can't inherit from itself" )
146149
147150 if (stmt.superClass != null ){
148151 currentClass = ClassType .SUBCLASS
@@ -206,7 +209,7 @@ class Resolver(private val interpreter: Interpreter) : Expr.Visitor<Unit>, Stmt.
206209 if (scopes.isEmpty()) return
207210 val scope = scopes.peek()
208211 if (scope.containsKey(name.lexeme))
209- Lox .error (name, " Already variable with this name in this scope" )
212+ errorHandler.reportError (name, " Already variable with this name in this scope" )
210213 scope[name.lexeme] = false
211214
212215 }
0 commit comments