adds a mom version of basic values
This commit is contained in:
parent
45bfe118d1
commit
b297650b78
55
lib/mom/basic_values.rb
Normal file
55
lib/mom/basic_values.rb
Normal file
@ -0,0 +1,55 @@
|
||||
module Mom
|
||||
# just name scoping the same stuff to mom
|
||||
# so we know we are on the way down, keeping our layers seperated
|
||||
# and we can put constant adding into the to_risc methods (instead of on vool classes)
|
||||
class Constant
|
||||
end
|
||||
|
||||
class IntegerConstant < Constant
|
||||
attr_reader :value
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
def ct_type
|
||||
Parfait.object_space.get_class_by_name(:Integer).instance_type
|
||||
end
|
||||
end
|
||||
class FloatConstant < Constant
|
||||
attr_reader :value
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
def ct_type
|
||||
true
|
||||
end
|
||||
end
|
||||
class TrueConstant < Constant
|
||||
def ct_type
|
||||
Parfait.object_space.get_class_by_name(:True).instance_type
|
||||
end
|
||||
end
|
||||
class FalseConstant < Constant
|
||||
def ct_type
|
||||
Parfait.object_space.get_class_by_name(:False).instance_type
|
||||
end
|
||||
end
|
||||
class NilConstant < Constant
|
||||
def ct_type
|
||||
Parfait.object_space.get_class_by_name(:Nil).instance_type
|
||||
end
|
||||
end
|
||||
class StringConstant < Constant
|
||||
attr_reader :value
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
def ct_type
|
||||
Parfait.object_space.get_class_by_name(:Word).instance_type
|
||||
end
|
||||
end
|
||||
class SymbolConstant < String
|
||||
def ct_type
|
||||
Parfait.object_space.get_class_by_name(:Word).instance_type
|
||||
end
|
||||
end
|
||||
end
|
@ -19,6 +19,7 @@ module Mom
|
||||
end
|
||||
end
|
||||
|
||||
require_relative "basic_values"
|
||||
require_relative "simple_call"
|
||||
require_relative "dynamic_call"
|
||||
require_relative "truth_check"
|
||||
|
@ -30,7 +30,8 @@ module Mom
|
||||
def initialize(left , right)
|
||||
left = SlotDefinition.new(left.shift , left) if left.is_a? Array
|
||||
@left , @right = left , right
|
||||
raise "right not SlotDefinition, #{left}" unless left.is_a? SlotDefinition
|
||||
raise "left not SlotDefinition, #{left}" unless left.is_a? SlotDefinition
|
||||
# raise "right not Mom, #{right.to_rxf}" unless right.class.name.include?("Mom")
|
||||
end
|
||||
end
|
||||
|
||||
@ -44,7 +45,7 @@ module Mom
|
||||
|
||||
def initialize(left , right)
|
||||
super
|
||||
raise "right not constant, #{right}" unless right.is_a? Vool::ConstantStatement
|
||||
raise "right not constant, #{right}" unless right.is_a? Mom::Constant
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1,11 +1,10 @@
|
||||
|
||||
# Integer class for representing maths on Integers
|
||||
# Integers are Values (not Objects),
|
||||
# Integers are Objects, spcifically DataObjects
|
||||
# - they have fixed value
|
||||
# - they are immutable
|
||||
# you can *not* assign instance variables or methods
|
||||
|
||||
# TODO how this idea works with Numeric ?
|
||||
# (both by implementation, not design.
|
||||
# Ie it would be possible to change the value, we just don't support that)
|
||||
|
||||
module Parfait
|
||||
class Integer
|
||||
|
@ -15,6 +15,9 @@ module Vool
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
def to_mom(method)
|
||||
return Mom::IntegerConstant.new(@value)
|
||||
end
|
||||
def ct_type
|
||||
Parfait.object_space.get_class_by_name(:Integer).instance_type
|
||||
end
|
||||
@ -45,10 +48,12 @@ module Vool
|
||||
end
|
||||
class SelfStatement < Statement
|
||||
attr_reader :clazz
|
||||
|
||||
def set_class(clazz)
|
||||
@clazz = clazz
|
||||
end
|
||||
def to_mom(in_method)
|
||||
Mom::SlotDefinition.new(:message , [:self])
|
||||
end
|
||||
def ct_type
|
||||
@clazz.instance_type
|
||||
end
|
||||
@ -60,6 +65,9 @@ module Vool
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
def to_mom(method)
|
||||
return Mom::StringConstant.new(@value)
|
||||
end
|
||||
def ct_type
|
||||
Parfait.object_space.get_class_by_name(:Word).instance_type
|
||||
end
|
||||
|
@ -18,7 +18,7 @@ module Vool
|
||||
end
|
||||
|
||||
def to_mom( method )
|
||||
@value.slot_class.new([:message , :self , @name] , @value)
|
||||
@value.slot_class.new([:message , :self , @name] , @value.to_mom(method))
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -12,7 +12,7 @@ module Vool
|
||||
else
|
||||
type = :frame
|
||||
end
|
||||
@value.slot_class.new(Mom::SlotDefinition.new(:message , [type , @name]) , @value)
|
||||
@value.slot_class.new(Mom::SlotDefinition.new(:message , [type , @name]) , @value.to_mom(method))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -11,11 +11,11 @@ module Vool
|
||||
super
|
||||
end
|
||||
|
||||
# To return form a method in mom instructions we need to do three things:
|
||||
# To return form a method in mom instructions we need to do two things:
|
||||
# - store the given return value, this is a SlotMove / SlotConstant
|
||||
# - activate return sequence (reinstantiate old message and jump to return address)
|
||||
def to_mom( method )
|
||||
move = @return_value.slot_class.new( [:message , :return_value] , @return_value)
|
||||
move = @return_value.slot_class.new( [:message , :return_value] , @return_value.to_mom(method))
|
||||
Mom::Statements.new [move , Mom::ReturnSequence.new]
|
||||
end
|
||||
|
||||
|
@ -43,11 +43,11 @@ module Vool
|
||||
|
||||
def message_setup(in_method)
|
||||
setup = [Mom::MessageSetup.new(in_method)]
|
||||
receiver = @receiver.slot_class.new([:message , :next_message , :receiver] , @receiver)
|
||||
receiver = @receiver.slot_class.new([:message , :next_message , :receiver] , @receiver.to_mom(in_method))
|
||||
arg_target = [:message , :next_message , :arguments]
|
||||
args = []
|
||||
@arguments.each_with_index do |arg , index|
|
||||
args << arg.slot_class.new( arg_target + [index] , arg)
|
||||
args << arg.slot_class.new( arg_target + [index] , arg.to_mom(in_method))
|
||||
end
|
||||
setup << Mom::ArgumentTransfer.new( receiver , args )
|
||||
end
|
||||
|
@ -9,8 +9,8 @@ module Vool
|
||||
statements
|
||||
end
|
||||
def self.ruby_to_mom(source)
|
||||
statements = elf.ruby_to_vool(source)
|
||||
statements.to_mom
|
||||
statements = self.ruby_to_vool(source)
|
||||
statements.to_mom(nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -16,7 +16,7 @@ module Mom
|
||||
assert_equal 1 , @stats.length
|
||||
end
|
||||
def test_assigns_class
|
||||
assert_equal Vool::IntegerStatement , @stats.right.class , @stats.inspect
|
||||
assert_equal Mom::IntegerConstant , @stats.right.class , @stats.inspect
|
||||
end
|
||||
def test_assigns_value
|
||||
assert_equal 5 , @stats.right.value , @stats.inspect
|
||||
@ -49,7 +49,7 @@ module Mom
|
||||
assert_equal 1 , @stats.length
|
||||
end
|
||||
def test_assigns_class
|
||||
assert_equal Vool::InstanceVariable , @stats.right.class , @stats.inspect
|
||||
assert_equal Mom::SlotDefinition , @stats.right.class , @stats.inspect
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -13,8 +13,8 @@ module Mom
|
||||
def test_if_compiles
|
||||
assert_equal @stats.last.class , SimpleCall , @stats
|
||||
end
|
||||
def test_length
|
||||
assert_equal 3 , @stats.length
|
||||
def test_method_name
|
||||
assert_equal @stats.last.method.name , :mod4 , @stats.last.to_rxf
|
||||
end
|
||||
def test_array
|
||||
check_array [MessageSetup,ArgumentTransfer,SimpleCall] , @stats
|
||||
|
@ -16,7 +16,7 @@ module Vool
|
||||
end
|
||||
|
||||
def test_receiver
|
||||
assert_equal SelfStatement, @stats[1].receiver.right.class
|
||||
assert_equal Mom::SlotDefinition, @stats[1].receiver.right.class
|
||||
end
|
||||
|
||||
def test_arg_one
|
||||
|
@ -12,7 +12,7 @@ module Vool
|
||||
@first = @stats.first
|
||||
end
|
||||
def receiver
|
||||
[IntegerStatement , 5]
|
||||
[Mom::IntegerConstant , 5]
|
||||
end
|
||||
def test_call_has_right_method
|
||||
assert_equal :mod4, @stats[2].method.name
|
||||
|
@ -12,14 +12,14 @@ module Vool
|
||||
end
|
||||
|
||||
def receiver
|
||||
[IntegerStatement , 5]
|
||||
[Mom::IntegerConstant , 5]
|
||||
end
|
||||
def test_args_two_move
|
||||
assert_equal :next_message, @stats[1].arguments[1].left.slots[0]
|
||||
assert_equal :arguments, @stats[1].arguments[1].left.slots[1]
|
||||
end
|
||||
def test_args_two_str
|
||||
assert_equal IntegerStatement, @stats[1].arguments[1].right.class
|
||||
assert_equal Mom::IntegerConstant, @stats[1].arguments[1].right.class
|
||||
assert_equal 2, @stats[1].arguments[1].right.value
|
||||
end
|
||||
end
|
||||
|
@ -12,7 +12,7 @@ module Vool
|
||||
@first = @stats.first
|
||||
end
|
||||
def receiver
|
||||
[StringStatement , "5"]
|
||||
[Mom::StringConstant , "5"]
|
||||
end
|
||||
|
||||
def test_args_one_move
|
||||
@ -20,7 +20,7 @@ module Vool
|
||||
assert_equal :arguments, @stats[1].arguments[0].left.slots[1]
|
||||
end
|
||||
def test_args_one_str
|
||||
assert_equal IntegerStatement, @stats[1].arguments[0].right.class
|
||||
assert_equal Mom::IntegerConstant, @stats[1].arguments[0].right.class
|
||||
assert_equal 1, @stats[1].arguments[0].right.value
|
||||
end
|
||||
end
|
||||
|
@ -29,7 +29,7 @@ module Vool
|
||||
assert @stats.first.right
|
||||
end
|
||||
def test_slot_assigns_int
|
||||
assert_equal IntegerStatement , @first.right.class
|
||||
assert_equal Mom::IntegerConstant , @first.right.class
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -31,7 +31,7 @@ module Vool
|
||||
assert @method.first.right
|
||||
end
|
||||
def test_slot_assigns_int
|
||||
assert_equal IntegerStatement , @method.first.right.class
|
||||
assert_equal Mom::IntegerConstant , @method.first.right.class
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -32,7 +32,7 @@ module Vool
|
||||
assert @first.right
|
||||
end
|
||||
def test_slot_assigns_int
|
||||
assert_equal IntegerStatement , @first.right.class
|
||||
assert_equal Mom::IntegerConstant , @first.right.class
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -34,7 +34,7 @@ module Vool
|
||||
assert @stats.first.right
|
||||
end
|
||||
def test_slot_assigns_int
|
||||
assert_equal IntegerStatement , @stats.first.right.class
|
||||
assert_equal Mom::IntegerConstant , @stats.first.right.class
|
||||
end
|
||||
end
|
||||
class TestReturnSendMom < MiniTest::Test
|
||||
@ -42,7 +42,7 @@ module Vool
|
||||
|
||||
def setup
|
||||
Risc.machine.boot
|
||||
@stats = compile_first_method( "return foo").first
|
||||
@stats = compile_first_method( "return 5.mod4").first
|
||||
end
|
||||
|
||||
def test_two_instructions_are_returned
|
||||
|
Loading…
Reference in New Issue
Block a user