finishes local assignments to_mom
This commit is contained in:
parent
9c499c7a19
commit
21e426be71
@ -1,6 +1,46 @@
|
|||||||
module Mom
|
module Mom
|
||||||
|
|
||||||
|
# SlotLoad is an abstract base class for moving data into a slot
|
||||||
|
# A Slot is the basically an instance variable, but it must be of known type
|
||||||
|
#
|
||||||
|
# The value loaded can be a constant (SlotConstant) or come from another Slot (SlotMove)
|
||||||
|
#
|
||||||
|
# The Slot is the left hand side, the right hand side being determined by the subclass.
|
||||||
|
# The only known object (*) for the left side is the current message, which is a bit like
|
||||||
|
# the oo version of a PC (program Counter)
|
||||||
|
# (* off course all class objects are global, and so they are allowed too)
|
||||||
|
#
|
||||||
|
# A maybe not immediately obvious corrolar of this design is the total absence of
|
||||||
|
# general purpose instance variable accessors. Ie only inside an object's functions
|
||||||
|
# can a method access instance variables, because only inside the method is the type
|
||||||
|
# guaranteed.
|
||||||
|
# From the outside a send is neccessary, both for get and set, (which goes through the method
|
||||||
|
# resolution and guarantees the correct method for a type), in other words perfect data hiding.
|
||||||
|
#
|
||||||
|
# @left: is an array of symbols, that specifies the first the object, and then the Slot.
|
||||||
|
# The first element is either a known type name (Capitalized symbol of the class name) ,
|
||||||
|
# or the symbol :message
|
||||||
|
# Ant subsequent symbols must be instance variables on the previous type.
|
||||||
|
# Examples: [:message , :self] or [:Space : :next_message]
|
||||||
|
#
|
||||||
|
# @right: depends on the derived Class
|
||||||
|
#
|
||||||
class SlotLoad < Instruction
|
class SlotLoad < Instruction
|
||||||
|
attr_reader :left , :right
|
||||||
|
def initialize(left , right)
|
||||||
|
@left , @right = left , right
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A SlotConstant moves a constant into a know Slot.
|
||||||
|
# Eg when you write a = 5 , the 5 becomes a constant, and so the right side
|
||||||
|
# the a is an instance variable on the current frame, and the frame is an instance
|
||||||
|
# of the current message, so the effect is something like message.frame.a = 5
|
||||||
|
# @left: See SlotLoad, and array of symbols
|
||||||
|
# @right: A Constant from parse, ie an instance of classes in basc_value, like TrueStatement
|
||||||
|
class SlotConstant < SlotLoad
|
||||||
|
end
|
||||||
|
|
||||||
|
class SlotMove < SlotLoad
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -14,10 +14,12 @@ module Vool
|
|||||||
def add_local( array )
|
def add_local( array )
|
||||||
array << @name
|
array << @name
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_mom( method )
|
def to_mom( method )
|
||||||
Mom::SlotLoad.new
|
Mom::SlotConstant.new([:message , :self , @name] , @value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class InstanceAssignment < Assignment
|
class InstanceAssignment < Assignment
|
||||||
# used to collect type information
|
# used to collect type information
|
||||||
def add_ivar( array )
|
def add_ivar( array )
|
||||||
|
@ -6,6 +6,7 @@ module Vool
|
|||||||
|
|
||||||
def setup
|
def setup
|
||||||
Risc.machine.boot
|
Risc.machine.boot
|
||||||
|
@method = compile_first_method( "a = 5")
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile_first_method input
|
def compile_first_method input
|
||||||
@ -17,8 +18,25 @@ module Vool
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_class_compiles
|
def test_class_compiles
|
||||||
meth = compile_first_method( "a = 5")
|
assert_equal Mom::SlotConstant , @method.first.class , @method
|
||||||
assert_equal Mom::SlotLoad , meth.first.class , meth
|
end
|
||||||
|
def test_slot_is_set
|
||||||
|
assert @method.first.left
|
||||||
|
end
|
||||||
|
def test_slot_starts_at_message
|
||||||
|
assert_equal :message , @method.first.left[0]
|
||||||
|
end
|
||||||
|
def test_slot_gets_self
|
||||||
|
assert_equal :self , @method.first.left[1]
|
||||||
|
end
|
||||||
|
def test_slot_assigns_to_local
|
||||||
|
assert_equal :a , @method.first.left[-1]
|
||||||
|
end
|
||||||
|
def test_slot_assigns_something
|
||||||
|
assert @method.first.right
|
||||||
|
end
|
||||||
|
def test_slot_assigns_int
|
||||||
|
assert_equal IntegerStatement , @method.first.right.class
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user