lib/loxxy/back_end/resolver.rb in loxxy-0.2.05 vs lib/loxxy/back_end/resolver.rb in loxxy-0.2.06

- old
+ new

@@ -67,11 +67,11 @@ previous_class = current_class @current_class = :class define(aClassStmt.name) if aClassStmt.superclass if aClassStmt.name == aClassStmt.superclass.name - raise StandardError, "'A class can't inherit from itself." + raise Loxxy::RuntimeError, "'A class can't inherit from itself." end @current_class = :subclass aClassStmt.superclass.accept(aVisitor) begin_scope @@ -94,21 +94,21 @@ end def before_return_stmt(returnStmt) if scopes.size < 2 msg = "Error at 'return': Can't return from top-level code." - raise StandardError, msg + raise Loxxy::RuntimeError, msg end if current_function == :none msg = "Error at 'return': Can't return from outside a function." - raise StandardError, msg + raise Loxxy::RuntimeError, msg end if current_function == :initializer msg = "Error at 'return': Can't return a value from an initializer." - raise StandardError, msg unless returnStmt.subnodes[0].kind_of?(Datatype::Nil) + raise Loxxy::RuntimeError, msg unless returnStmt.subnodes[0].kind_of?(Datatype::Nil) end end def after_while_stmt(aWhileStmt, aVisitor) aWhileStmt.body.accept(aVisitor) @@ -158,11 +158,11 @@ end def before_this_expr(_thisExpr) if current_class == :none msg = "Error at 'this': Can't use 'this' outside of a class." - raise StandardError, msg + raise Loxxy::RuntimeError, msg end end def after_this_expr(aThisExpr, aVisitor) # 'this' behaves closely to a local variable @@ -173,15 +173,15 @@ # rubocop: disable Style/StringConcatenation def after_super_expr(aSuperExpr, aVisitor) msg_prefix = "Error at 'super': Can't use 'super' " if current_class == :none err_msg = msg_prefix + 'outside of a class.' - raise StandardError, err_msg + raise Loxxy::RuntimeError, err_msg elsif current_class == :class err_msg = msg_prefix + 'in a class without superclass.' - raise StandardError, err_msg + raise Loxxy::RuntimeError, err_msg end # 'super' behaves closely to a local variable resolve_local(aSuperExpr, aVisitor) end @@ -203,17 +203,22 @@ def end_scope scopes.pop end + # rubocop: disable Style/SoleNestedConditional def declare(aVarName) return if scopes.empty? curr_scope = scopes.last - if curr_scope.include?(aVarName) - msg = "Error at '#{aVarName}': Already variable with this name in this scope." - raise StandardError, msg + if scopes.size > 1 # Not at top-level? + # Oddly enough, Lox allows variable re-declaration at top-level + if curr_scope.include?(aVarName) + msg = "Error at '#{aVarName}': Already variable with this name in this scope." + raise Loxxy::RuntimeError, msg + end end + # rubocop: enable Style/SoleNestedConditional # The initializer is not yet processed. # Mark the variable as 'not yet ready' = exists but may not be referenced yet curr_scope[aVarName] = false end