From 160d860db2f79b7ff188a084b9faedb38dff9ace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Mon, 26 Aug 2019 09:24:06 +0300 Subject: [PATCH] using new macro approach for builtin, testing first --- lib/mom/builtin/operator.rb | 28 +++++++++++++++++++-- lib/rubyx.rb | 1 + lib/vool/builtin.rb | 13 ++++++++++ lib/vool/builtin/int_plus.rb | 5 ++++ test/vool/test_builtin.rb | 47 ++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 lib/vool/builtin.rb create mode 100644 lib/vool/builtin/int_plus.rb create mode 100644 test/vool/test_builtin.rb diff --git a/lib/mom/builtin/operator.rb b/lib/mom/builtin/operator.rb index a67c5183..4c9be8ee 100644 --- a/lib/mom/builtin/operator.rb +++ b/lib/mom/builtin/operator.rb @@ -1,6 +1,6 @@ module Mom module Builtin - class Operator < ::Mom::Instruction + class Operator < Instruction attr_reader :operator def initialize(name , operator) super(name) @@ -14,7 +14,7 @@ module Mom builder.build do integer! << message[:receiver] integer.reduce_int - integer_reg! << message[:arg1] #"other" + integer_reg! << message[:arg1] #"other" integer_reg.reduce_int integer.op operator , integer_reg integer_tmp[Parfait::Integer.integer_index] << integer @@ -23,5 +23,29 @@ module Mom return compiler 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 diff --git a/lib/rubyx.rb b/lib/rubyx.rb index 1e849175..4b6221c2 100644 --- a/lib/rubyx.rb +++ b/lib/rubyx.rb @@ -8,5 +8,6 @@ require_relative "mom/mom" require_relative "arm/arm_machine" require_relative "arm/arm_platform" require_relative "vool/statement" +require_relative "vool/builtin" require_relative "ruby" require_relative "rubyx/rubyx_compiler" diff --git a/lib/vool/builtin.rb b/lib/vool/builtin.rb new file mode 100644 index 00000000..d1010be3 --- /dev/null +++ b/lib/vool/builtin.rb @@ -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 diff --git a/lib/vool/builtin/int_plus.rb b/lib/vool/builtin/int_plus.rb new file mode 100644 index 00000000..e90423b3 --- /dev/null +++ b/lib/vool/builtin/int_plus.rb @@ -0,0 +1,5 @@ +class Integer < DataObject4 + def +(other) + return X.int_operator( :+ ) + end +end diff --git a/test/vool/test_builtin.rb b/test/vool/test_builtin.rb new file mode 100644 index 00000000..8e7766a7 --- /dev/null +++ b/test/vool/test_builtin.rb @@ -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