using new macro approach for builtin, testing first
This commit is contained in:
parent
b9bdc55059
commit
160d860db2
@ -1,6 +1,6 @@
|
|||||||
module Mom
|
module Mom
|
||||||
module Builtin
|
module Builtin
|
||||||
class Operator < ::Mom::Instruction
|
class Operator < Instruction
|
||||||
attr_reader :operator
|
attr_reader :operator
|
||||||
def initialize(name , operator)
|
def initialize(name , operator)
|
||||||
super(name)
|
super(name)
|
||||||
@ -14,7 +14,7 @@ module Mom
|
|||||||
builder.build do
|
builder.build do
|
||||||
integer! << message[:receiver]
|
integer! << message[:receiver]
|
||||||
integer.reduce_int
|
integer.reduce_int
|
||||||
integer_reg! << message[:arg1] #"other"
|
integer_reg! << message[:arg1] #"other"
|
||||||
integer_reg.reduce_int
|
integer_reg.reduce_int
|
||||||
integer.op operator , integer_reg
|
integer.op operator , integer_reg
|
||||||
integer_tmp[Parfait::Integer.integer_index] << integer
|
integer_tmp[Parfait::Integer.integer_index] << integer
|
||||||
@ -23,5 +23,29 @@ module Mom
|
|||||||
return compiler
|
return compiler
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
class IntOperator < 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[:arg1] #"other"
|
||||||
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,5 +8,6 @@ require_relative "mom/mom"
|
|||||||
require_relative "arm/arm_machine"
|
require_relative "arm/arm_machine"
|
||||||
require_relative "arm/arm_platform"
|
require_relative "arm/arm_platform"
|
||||||
require_relative "vool/statement"
|
require_relative "vool/statement"
|
||||||
|
require_relative "vool/builtin"
|
||||||
require_relative "ruby"
|
require_relative "ruby"
|
||||||
require_relative "rubyx/rubyx_compiler"
|
require_relative "rubyx/rubyx_compiler"
|
||||||
|
13
lib/vool/builtin.rb
Normal file
13
lib/vool/builtin.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
module Vool
|
||||||
|
module Builtin
|
||||||
|
def self.boot_methods(options)
|
||||||
|
return if options[:boot_methods] == false
|
||||||
|
load_builtin( :int_plus )
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.load_builtin(name)
|
||||||
|
fname = "./builtin/#{name}.rb"
|
||||||
|
File.read File.expand_path(fname, File.dirname(__FILE__))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
5
lib/vool/builtin/int_plus.rb
Normal file
5
lib/vool/builtin/int_plus.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class Integer < DataObject4
|
||||||
|
def +(other)
|
||||||
|
return X.int_operator( :+ )
|
||||||
|
end
|
||||||
|
end
|
47
test/vool/test_builtin.rb
Normal file
47
test/vool/test_builtin.rb
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
module Vool
|
||||||
|
class TestBuiltin < MiniTest::Test
|
||||||
|
include VoolCompile
|
||||||
|
|
||||||
|
def setup
|
||||||
|
Parfait.boot!(Parfait.default_test_options)
|
||||||
|
@code = Builtin.boot_methods({})
|
||||||
|
end
|
||||||
|
def as_ruby
|
||||||
|
@ruby = Ruby::RubyCompiler.compile(@code)
|
||||||
|
end
|
||||||
|
def test_boot
|
||||||
|
assert_equal String , @code.class
|
||||||
|
assert @code.include?("Integer")
|
||||||
|
end
|
||||||
|
def test_compile_ruby
|
||||||
|
assert_equal Ruby::ClassStatement , as_ruby.class
|
||||||
|
assert_equal Ruby::MethodStatement , @ruby.body.first.class
|
||||||
|
assert_equal :+ , @ruby.body.first.name
|
||||||
|
end
|
||||||
|
def test_compile_vool
|
||||||
|
vool = as_ruby.to_vool
|
||||||
|
assert_equal Vool::ClassExpression , vool.class
|
||||||
|
assert_equal Vool::MethodExpression , vool.body.first.class
|
||||||
|
end
|
||||||
|
def test_vool_method
|
||||||
|
vool = as_ruby.to_vool
|
||||||
|
assert_equal :+ , vool.body.first.name
|
||||||
|
assert_equal Vool::ReturnStatement , vool.body.first.body.class
|
||||||
|
assert_equal Vool::MacroExpression , vool.body.first.body.return_value.class
|
||||||
|
end
|
||||||
|
def test_mom_basic
|
||||||
|
mom = as_ruby.to_vool.to_mom(nil)
|
||||||
|
assert_equal Mom::MomCollection , mom.class
|
||||||
|
assert_equal Mom::MethodCompiler , mom.method_compilers.first.class
|
||||||
|
end
|
||||||
|
def test_mom_instructions
|
||||||
|
mom_compiler = as_ruby.to_vool.to_mom(nil).method_compilers.first
|
||||||
|
assert_equal Mom::Label , mom_compiler.mom_instructions.class
|
||||||
|
assert_equal Mom::IntOperator , mom_compiler.mom_instructions.next.class
|
||||||
|
assert_equal Mom::SlotLoad , mom_compiler.mom_instructions.next(2).class
|
||||||
|
assert_equal Mom::ReturnJump , mom_compiler.mom_instructions.next(3).class
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user