progress on the builder front

mainly tests for now
This commit is contained in:
Torsten Ruger 2018-04-06 14:54:24 +03:00
parent e396807ee5
commit 44d661fe56
4 changed files with 76 additions and 6 deletions

View File

@ -2,6 +2,8 @@ module Risc
class Builder class Builder
attr_reader :built
def initialize(compiler) def initialize(compiler)
@compiler = compiler @compiler = compiler
end end
@ -11,15 +13,28 @@ module Risc
name = args[0].to_s.capitalize.to_sym name = args[0].to_s.capitalize.to_sym
type = Risc.resolve_type(name , @compiler) type = Risc.resolve_type(name , @compiler)
reg = @compiler.use_reg( type ) reg = @compiler.use_reg( type )
reg.builder = self
puts reg puts reg
reg reg
end end
def build(&block)
instance_eval(&block)
return built
end
def add_instruction(ins)
if(built)
built << ins
else
@built = ins
end
end
end end
class RValue class RValue
end end
def self.build(compiler, &block) def self.build(compiler, &block)
Builder.new(compiler).instance_eval( &block ) Builder.new(compiler).build( &block )
end end
# if a symbol is given, it may be the message or the new_message. # if a symbol is given, it may be the message or the new_message.

View File

@ -4,9 +4,11 @@ module Risc
class RiscValue class RiscValue
attr_accessor :symbol , :type , :value attr_reader :symbol , :type , :value
def initialize r , type , value = nil attr_accessor :builder
def initialize( r , type , value = nil)
raise "wrong type for register init #{r}" unless r.is_a? Symbol raise "wrong type for register init #{r}" unless r.is_a? Symbol
raise "double r #{r}" if r.to_s[0,1] == "rr" raise "double r #{r}" if r.to_s[0,1] == "rr"
raise "not reg #{r}" unless self.class.look_like_reg r raise "not reg #{r}" unless self.class.look_like_reg r
@ -62,10 +64,14 @@ module Risc
when RValue when RValue
raise "not yet" raise "not yet"
when Parfait::Object when Parfait::Object
Risc.load_constant("#{load.class} to #{self.type}" , load , self) ins = Risc.load_constant("#{load.class} to #{self.type}" , load , self)
when RiscValue
ins = Risc.transfer("#{load.type} to #{self.type}" , load , self)
else else
raise "not implemented" raise "not implemented"
end end
builder.add_instruction(ins) if builder
return ins
end end
end end

View File

@ -2,5 +2,23 @@ require_relative "../helper"
module Risc module Risc
class TestBuilder < MiniTest::Test class TestBuilder < MiniTest::Test
def setup
Risc.machine.boot
init = Parfait.object_space.get_init
compiler = Risc::MethodCompiler.new( init )
@builder = Builder.new(compiler)
end
def test_has_build
assert_nil @builder.build{ }
end
def test_has_build_and_returns_built
r1 = RiscValue.new(:r1 , :Space)
built = @builder.build{ space << r1 }
assert_equal Transfer , built.class
end
def test_has_attribute
assert_nil @builder.built
end
end end
end end

View File

@ -1,6 +1,37 @@
require_relative "../helper" require_relative "../helper"
class FakeBuilder
attr_reader :built
def add_instruction(ins)
@built = ins
end
end
module Risc module Risc
class TestRegisterValue < MiniTest::Test class TestRegisterValue < MiniTest::Test
def setup
Risc.machine.boot
@r0 = RiscValue.new(:r0 , :Message)
@r1 = RiscValue.new(:r1 , :Space)
end
def test_r0
assert_equal :r0 , @r0.symbol
end
def test_load_space
move = @r0 << Parfait.object_space
assert_equal LoadConstant , move.class
end
def test_transfer
transfer = @r0 << @r1
assert_equal Transfer , transfer.class
end
def test_calls_builder
builder = FakeBuilder.new
@r0.builder = builder
@r0 << @r1
assert_equal Transfer , builder.built.class
end
end end
end end