lib/loxxy/back_end/resolver.rb in loxxy-0.1.16 vs lib/loxxy/back_end/resolver.rb in loxxy-0.1.17

- old
+ new

@@ -68,11 +68,12 @@ @current_class = :class define(aClassStmt.name) begin_scope define('this') aClassStmt.body.each do |fun_stmt| - resolve_function(fun_stmt, :method, aVisitor) + mth_type = fun_stmt.name == 'init' ? :initializer : :method + resolve_function(fun_stmt, mth_type, aVisitor) end end_scope @current_class = previous_class end @@ -90,18 +91,23 @@ def after_if_stmt(anIfStmt, aVisitor) anIfStmt.then_stmt.accept(aVisitor) anIfStmt.else_stmt&.accept(aVisitor) end - def before_return_stmt(_returnStmt) + def before_return_stmt(returnStmt) if scopes.size < 2 msg = "Error at 'return': Can't return from top-level code." raise StandardError, msg end if current_function == :none msg = "Error at 'return': Can't return from outside a function." raise StandardError, 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) end end def after_while_stmt(aWhileStmt, aVisitor) aWhileStmt.body.accept(aVisitor)