A good start on the macro idea

I call it macro because it lets you insert basically arbitrary risc code into the ruby level. The way it works:
Reserve namespace X
map any X.some_call to a Mom instruction
by the name SomeCall
which must take the same args in constructor as given
And obviously produce whatever risc it wants
Hoping to rewrite builtin around this idea (with the existing Mom builtn instructions)
This commit is contained in:
2019-08-25 14:40:59 +03:00
parent c0a3c9b65c
commit b9bdc55059
10 changed files with 158 additions and 8 deletions

View File

@ -44,7 +44,7 @@ module Mains
run_all
assert_equal ::Integer , get_return.class , " "
assert_equal 4 , get_return , " "
assert_equal "hi" , @interpreter.stdout
assert_equal "11111" , @interpreter.stdout
end
end

View File

@ -2,12 +2,20 @@ require_relative "../helper"
module Ruby
module RubyTests
include ScopeHelper
def setup
Parfait.boot!(Parfait.default_test_options)
end
def compile(input)
RubyCompiler.compile(input)
end
def compile_main(input)
RubyCompiler.compile(as_main(input))
end
def compile_main_vool(input)
xcompiler = RubyX::RubyXCompiler.new(RubyX.default_test_options)
xcompiler.ruby_to_vool(as_main(input))
end
def assert_raises_muted &block
orig_stdout = $stdout

View File

@ -0,0 +1,65 @@
require_relative "helper"
module Ruby
class TestPlusEqualsRuby < Minitest::Test
include RubyTests
def setup
@lst = compile_main( "X.plus_equals(1)")
end
def method_body
@lst.body.first.body
end
def test_class
assert_equal Ruby::ClassStatement , @lst.class
assert_equal Ruby::Statements , @lst.body.class
end
def test_method
assert_equal Ruby::MethodStatement , @lst.body.first.class
assert_equal Ruby::SendStatement , method_body.class
end
def test_send
assert_equal :X , method_body.receiver.name
assert_equal :plus_equals , method_body.name
end
end
class TestPlusEquals < Minitest::Test
include RubyTests
def setup
@lst = compile( "X.plus_equals(1)").to_vool
end
def test_class
assert_equal Vool::MacroExpression , @lst.class
end
def test_arg1
assert_equal Vool::IntegerConstant , @lst.arguments.first.class
end
def test_name
assert_equal :plus_equals , @lst.name
end
end
class TestPlusEqualsX < Minitest::Test
include RubyTests
def setup
@lst = compile_main( "X.plus_equals(arg,1)").to_vool
end
def method_body
@lst.body.first.body
end
def test_class
assert_equal Vool::ClassExpression , @lst.class
assert_equal Vool::MethodExpression , @lst.body.first.class
end
def test_macro_class
assert_equal Vool::ReturnStatement , method_body.class
assert_equal Vool::MacroExpression , method_body.return_value.class
end
def test_args
assert_equal Vool::LocalVariable , method_body.return_value.arguments.first.class
assert_equal Vool::IntegerConstant , method_body.return_value.arguments.last.class
end
def test_name
assert_equal :plus_equals , method_body.return_value.name
end
end
end

View File

@ -0,0 +1,35 @@
require_relative "helper"
module Mom
class PlusEquals < Instruction
attr_reader :a , :b
def initialize(source , arg , b)
super(source)
@a = arg
@b = b
end
end
end
module Vool
class TestMacroMom < MiniTest::Test
include VoolCompile
def setup
@compiler = compile_first_method( "X.plus_equals(arg,1)")
@ins = @compiler.mom_instructions.next
end
def test_class_compiles
assert_equal Mom::PlusEquals , @ins.class , @ins
end
def test_arg1
assert_equal Vool::LocalVariable , @ins.a.class
assert_equal :arg , @ins.a.name
end
def test_arg2
assert_equal Vool::IntegerConstant , @ins.b.class
assert_equal 1 , @ins.b.value
end
end
end