Class: AmberVM::Interpreter::Return

Inherits:
Element show all
Defined in:
lib/amber/interpreter.rb

Overview

Represents the return statement, it throws a ReturnException which can be caught to have the function or block return what they wish.

Instance Attribute Summary

Attributes inherited from Element

#pos

Instance Method Summary collapse

Constructor Details

#initialize(val, pos = nil) ⇒ Return

The Return is initialized wiht 1 to 2 parameters.

val

The value that will be returned, aka the part after ‘return’.

pos

The position within the source code of the definition - for deugging purpose.



1069
1070
1071
1072
# File 'lib/amber/interpreter.rb', line 1069

def initialize val, pos = nil
  super(pos)
  @val = val
end

Instance Method Details

#data_typeObject

The data type of a return statement is any, as it does not return anything at all, after all it jumps out of a block.



1081
1082
1083
# File 'lib/amber/interpreter.rb', line 1081

def data_type
  :any
end

#execute(env) ⇒ Object

When executed the Return executed the value and then raises a ReturnException.



1087
1088
1089
1090
1091
# File 'lib/amber/interpreter.rb', line 1087

def execute env
  v = @val.execute(env)
  AmberVM::debug "Return reached, returning: #{v}" if $DEBUG
  throw :return, ReturnData.new(v)
end

#optimize(variables = {}) ⇒ Object



1093
1094
1095
1096
# File 'lib/amber/interpreter.rb', line 1093

def optimize variables = {}
  val = @val.optimize variables
  Return.new(val, @pos)
end

#pretty_print(q) ⇒ Object



1074
1075
1076
1077
# File 'lib/amber/interpreter.rb', line 1074

def pretty_print(q)
  q.text "return "
  q.pp @val
end