lib/loxxy/back_end/resolver.rb in loxxy-0.1.08 vs lib/loxxy/back_end/resolver.rb in loxxy-0.1.09

- old
+ new

@@ -62,10 +62,17 @@ def after_if_stmt(anIfStmt, aVisitor) anIfStmt.then_stmt.accept(aVisitor) anIfStmt.else_stmt&.accept(aVisitor) end + def before_return_stmt(_returnStmt) + if scopes.size < 2 + msg = "Error at 'return': Can't return from top-level code." + raise StandardError, msg + end + end + def after_while_stmt(aWhileStmt, aVisitor) aWhileStmt.body.accept(aVisitor) aWhileStmt.condition.accept(aVisitor) end @@ -120,9 +127,13 @@ 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 + end # 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