Now booting all original methods

Just need to refactor now
This commit is contained in:
Torsten Rüger 2019-08-12 11:31:47 +03:00
parent 91ddae2251
commit 2326081161
4 changed files with 59 additions and 18 deletions

View File

@ -49,7 +49,7 @@ module Risc
int_type = space.get_type_by_class_name(:Integer) int_type = space.get_type_by_class_name(:Integer)
Risc.operators.each do |op| Risc.operators.each do |op|
#compilers << operator_compiler( int_type , op) compilers << operator_compiler( int_type , op)
end end
[ :div4, :<,:<= , :>=, :> , :div10 ].each do |f| #div4 is just a forward declaration [ :div4, :<,:<= , :>=, :> , :div10 ].each do |f| #div4 is just a forward declaration
compilers << compiler_for( int_type , Integer , f) compilers << compiler_for( int_type , Integer , f)

View File

@ -65,20 +65,22 @@ module Risc
class Comparison < ::Mom::Instruction class Comparison < ::Mom::Instruction
attr_reader :operator attr_reader :operator
def initialize(name , operator) def initialize(name , operator)
super(name)
@operator = operator @operator = operator
end end
def to_risc(compiler) def to_risc(compiler)
builder = compiler.builder(compiler.source) builder = compiler.builder(compiler.source)
operator = @operator # make accessible in block
builder.build do builder.build do
integer! << message[:receiver] integer! << message[:receiver]
integer.reduce_int integer.reduce_int
integer_reg! << message[:arguments] integer_reg! << message[:arguments]
integer_reg << integer_reg[Parfait::NamedList.type_length + 0] #"other" is at index 0 integer_reg << integer_reg[Parfait::NamedList.type_length + 0] #"other" is at index 0
integer_reg.reduce_int integer_reg.reduce_int
swap_names(:integer , :integer_reg) if(@operator.to_s.start_with?('<') ) swap_names(:integer , :integer_reg) if(operator.to_s.start_with?('<') )
integer.op :- , integer_reg integer.op :- , integer_reg
if_minus false_label if_minus false_label
if_zero( false_label ) if @operator.to_s.length == 1 if_zero( false_label ) if operator.to_s.length == 1
object! << Parfait.object_space.true_object object! << Parfait.object_space.true_object
branch merge_label branch merge_label
add_code false_label add_code false_label
@ -98,21 +100,33 @@ module Risc
# - returns the new int # - returns the new int
def operator_method( op_sym ) def operator_method( op_sym )
compiler = compiler_for(:Integer, op_sym ,{other: :Integer }) compiler = compiler_for(:Integer, op_sym ,{other: :Integer })
builder = compiler.builder(compiler.source) compiler.add_code OperatorInstruction.new("operator" , op_sym)
integer_tmp = builder.allocate_int
builder.build do
integer! << message[:receiver]
integer.reduce_int
integer_reg! << message[:arguments]
integer_reg << integer_reg[Parfait::NamedList.type_length + 0] #"other" is at index 0
integer_reg.reduce_int
integer.op op_sym , integer_reg
integer_tmp[Parfait::Integer.integer_index] << integer
message[:return_value] << integer_tmp
end
compiler.add_mom( Mom::ReturnSequence.new)
return compiler return compiler
end end
class OperatorInstruction < ::Mom::Instruction
attr_reader :operator
def initialize(name , operator)
super(name)
@operator = operator
end
def to_risc(compiler)
builder = compiler.builder(compiler.source)
integer_tmp = builder.allocate_int
operator = @operator # make accessible in block
builder.build do
integer! << message[:receiver]
integer.reduce_int
integer_reg! << message[:arguments]
integer_reg << integer_reg[Parfait::NamedList.type_length + 0] #"other" is at index 0
integer_reg.reduce_int
integer.op operator , integer_reg
integer_tmp[Parfait::Integer.integer_index] << integer
message[:return_value] << integer_tmp
end
return compiler
end
end
# as the name suggests, this devides the integer (self) by ten # as the name suggests, this devides the integer (self) by ten
# #

View File

@ -53,7 +53,7 @@ module Risc
assert_equal Risc::MethodCompiler , @method.to_risc.class assert_equal Risc::MethodCompiler , @method.to_risc.class
end end
def test_risc_length def test_risc_length
assert_equal 27 , @method.to_risc.risc_instructions.length assert_equal 28 , @method.to_risc.risc_instructions.length
end end
end end
class TestIntComp2 < BootTest class TestIntComp2 < BootTest
@ -74,5 +74,32 @@ module Risc
assert_equal 27 , @method.to_risc.risc_instructions.length assert_equal 27 , @method.to_risc.risc_instructions.length
end end
end end
class TestIntOperators < BootTest
def setup
super
end
def each_method &block
Risc.operators.each do |name|
method = get_compiler(name)
block.yield(method)
end
end
def test_has_get_internal
each_method do |method|
assert_equal Mom::MethodCompiler , method.class
assert_equal 5 , method.mom_instructions.length
end
end
def test_compile
each_method do |method|
assert_equal Risc::MethodCompiler , method.to_risc.class
end
end
def test_risc_length
each_method do |method|
assert_equal 49 , method.to_risc.risc_instructions.length
end
end
end
end end
end end

View File

@ -14,7 +14,7 @@ module Risc
assert_equal Array, @functions.class assert_equal Array, @functions.class
end end
def test_boot_function_length def test_boot_function_length
assert_equal 15, @functions.length assert_equal 22, @functions.length
end end
def test_boot_function_first def test_boot_function_first
assert_equal Mom::MethodCompiler, @functions.first.class assert_equal Mom::MethodCompiler, @functions.first.class