2017-01-14 18:28:44 +01:00
|
|
|
module Vm
|
2016-12-09 11:13:33 +01:00
|
|
|
module NameExpression
|
2015-09-19 15:28:41 +02:00
|
|
|
|
|
|
|
# attr_reader :name
|
2016-03-07 10:55:28 +01:00
|
|
|
# compiling name needs to check if it's a local variable
|
|
|
|
# or an argument
|
2015-09-19 15:28:41 +02:00
|
|
|
# whichever way this goes the result is stored in the return slot (as all compiles)
|
2016-03-07 10:55:28 +01:00
|
|
|
def on_NameExpression statement
|
|
|
|
name = statement.name
|
2016-12-09 11:13:33 +01:00
|
|
|
[:self , :space , :message].each do |special|
|
2016-12-28 20:10:14 +01:00
|
|
|
return send(:"load_special_#{special}" , statement ) if name == special
|
2015-11-09 22:29:14 +01:00
|
|
|
end
|
2016-12-28 20:10:14 +01:00
|
|
|
return load_argument(statement) if( @method.has_arg(name))
|
|
|
|
load_local(statement)
|
2016-12-09 11:13:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-28 20:10:14 +01:00
|
|
|
def load_argument(statement)
|
|
|
|
name = statement.name
|
|
|
|
index = @method.has_arg(name)
|
|
|
|
named_list = use_reg :NamedList
|
|
|
|
ret = use_reg @method.argument_type(index)
|
|
|
|
#puts "For #{name} at #{index} got #{@method.arguments.inspect}"
|
|
|
|
add_slot_to_reg("#{statement} load args" , :message , :arguments, named_list )
|
|
|
|
add_slot_to_reg("#{statement} load #{name}" , named_list , index + 1, ret )
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_local( statement )
|
2016-12-27 19:39:39 +01:00
|
|
|
name = statement.name
|
|
|
|
index = @method.has_local( name )
|
|
|
|
raise "must define variable '#{name}' before using it" unless index
|
2016-12-21 17:51:22 +01:00
|
|
|
named_list = use_reg :NamedList
|
2016-12-28 19:37:54 +01:00
|
|
|
add_slot_to_reg("#{name} load locals" , :message , :locals , named_list )
|
2016-12-13 18:18:17 +01:00
|
|
|
ret = use_reg @method.locals_type( index )
|
2016-12-28 19:37:54 +01:00
|
|
|
add_slot_to_reg("#{name} load from locals" , named_list , index + 1, ret )
|
2016-12-09 11:13:33 +01:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2016-12-28 20:10:14 +01:00
|
|
|
def load_special_self(statement)
|
2016-12-14 12:24:42 +01:00
|
|
|
ret = use_reg @type
|
2016-12-28 19:37:54 +01:00
|
|
|
add_slot_to_reg("#{statement} load self" , :message , :receiver , ret )
|
2016-12-09 11:13:33 +01:00
|
|
|
return ret
|
2015-09-19 15:28:41 +02:00
|
|
|
end
|
|
|
|
|
2016-12-28 20:10:14 +01:00
|
|
|
def load_special_space(statement)
|
2016-12-30 13:10:49 +01:00
|
|
|
space = Parfait.object_space
|
2016-12-09 11:13:33 +01:00
|
|
|
reg = use_reg :Space , space
|
2016-12-28 19:37:54 +01:00
|
|
|
add_load_constant( "#{statement} load space", space , reg )
|
2016-12-09 11:13:33 +01:00
|
|
|
return reg
|
|
|
|
end
|
2016-12-27 19:39:39 +01:00
|
|
|
|
2016-12-28 20:10:14 +01:00
|
|
|
def load_special_message(statement)
|
2016-12-09 11:13:33 +01:00
|
|
|
reg = use_reg :Message
|
2016-12-28 19:37:54 +01:00
|
|
|
add_transfer( "#{statement} load message", Register.message_reg , reg )
|
2016-12-09 11:13:33 +01:00
|
|
|
return reg
|
|
|
|
end
|
2015-09-19 15:28:41 +02:00
|
|
|
end #module
|
|
|
|
end
|