add rewriting of operator assignment

foo += 1 becomes foo = foo + 1 in vool
This commit is contained in:
Torsten Ruger 2018-06-25 16:32:20 +03:00
parent 70d7e654c4
commit 67a6ef9f67
6 changed files with 56 additions and 3 deletions

View File

@ -22,6 +22,9 @@ guard :minitest , all_on_start: false do # with Minitest::Unit
#parfait type tests have a whole directory
watch(%r{^lib/parfait/type.rb}) { Dir["test/parfait/type/test_*.rb"] }
# ruby compiler tests have a whole directory
watch(%r{^lib/vool/ruby_compiler.rb}) { Dir["test/vool/ruby_compiler/test_*.rb"] }
# Vool to_mom compile process + # Ruby to vool compile process
watch(%r{^lib/vool/statements/(.+)_statement.rb}) { |m|
[ Dir["test/vool/to_mom/test_#{m[1]}*.rb"] , "test/vool/statements/test_#{m[1]}.rb"] }

View File

@ -10,6 +10,7 @@ module Vool
# The next step is then to normalize the code and then finally to compile
# to the next level down, MOM (Minimal Object Machine)
class RubyCompiler < AST::Processor
include AST::Sexp
def self.compile(input)
ast = Parser::Ruby22.parse( input )
@ -132,6 +133,16 @@ module Vool
IvarAssignment.new(instance_name(name),value)
end
def on_op_asgn(expression)
ass , op , exp = *expression
name = ass.children[0]
a_type = ass.type.to_s[0,3]
rewrite = s( a_type + "sgn" ,
name ,
s(:send , s( a_type + "r" , name ) , op , exp ) )
process(rewrite)
end
def on_return statement
return_value = process(statement.children.first)
ReturnStatement.new( return_value )

View File

@ -14,7 +14,7 @@ module Rubyx
result = a + b
a = b
b = result
i+= 1
i += 1
end
return result
end

View File

@ -1,7 +1,7 @@
require_relative "helper"
module Vool
class TestAssignment < MiniTest::Test
class TestIvarAssignment < MiniTest::Test
def test_local
lst = RubyCompiler.compile( "foo = bar")

View File

@ -1,7 +1,7 @@
require_relative "helper"
module Vool
class TestLocal < MiniTest::Test
class TestLocalAssignment < MiniTest::Test
def test_local
lst = RubyCompiler.compile( "foo = bar")

View File

@ -0,0 +1,39 @@
require_relative "helper"
module Vool
module OpAss
def test_local_name
assert_equal :foo , @lst.name
end
def test_local_value
assert_equal SendStatement , @lst.value.class
end
def test_local_method
assert_equal :+ , @lst.value.name
end
def test_local_receiver
assert_equal :foo , @lst.value.receiver.name
end
def test_local_receiver
assert_equal 5 , @lst.value.arguments.first.value
end
end
class TestLocalOpAssign < MiniTest::Test
include OpAss
def setup
@lst = RubyCompiler.compile( "foo += 5")
end
def test_local_ass
assert_equal LocalAssignment , @lst.class
end
end
class TestIvarOpAssign < MiniTest::Test
include OpAss
def setup
@lst = RubyCompiler.compile( "@foo += 5")
end
def test_ivar_ass
assert_equal IvarAssignment , @lst.class
end
end
end