fix if to_vool
and add (simple) hoisting tests
This commit is contained in:
parent
98788b52d3
commit
9b8bd57db4
@ -14,8 +14,8 @@ module Ruby
|
||||
|
||||
def to_vool
|
||||
cond , rest = *normalize_name(@condition)
|
||||
fals = @if_false ? @if_false.normalize : nil
|
||||
me = IfStatement.new(cond , @if_true.normalize, fals)
|
||||
fals = @if_false ? @if_false.to_vool : nil
|
||||
me = Vool::IfStatement.new(cond , @if_true.to_vool, fals)
|
||||
return me unless rest
|
||||
rest << me
|
||||
rest
|
||||
|
@ -12,7 +12,7 @@ module Ruby
|
||||
if( condition.is_a?(ScopeStatement) and condition.single?)
|
||||
condition = condition.first
|
||||
end
|
||||
return [condition] if condition.is_a?(Named) or condition.is_a?(Constant)
|
||||
return [condition.to_vool] if condition.is_a?(Named) or condition.is_a?(Constant)
|
||||
condition = condition.to_vool
|
||||
local = "tmp_#{object_id}".to_sym
|
||||
assign = Vool::Statements.new [Vool::LocalAssignment.new( local , condition)]
|
||||
|
@ -37,56 +37,5 @@ module Ruby
|
||||
assert_equal TrueConstant , lst.if_true.class
|
||||
assert_equal FalseConstant, lst.if_false.class
|
||||
end
|
||||
|
||||
def reverse_if
|
||||
"true if(false)"
|
||||
end
|
||||
def test_if_reverse
|
||||
lst = compile( reverse_if )
|
||||
assert_equal IfStatement , lst.class
|
||||
end
|
||||
def test_if_reverse_cond
|
||||
lst = compile( reverse_if )
|
||||
assert_equal FalseConstant , lst.condition.class
|
||||
end
|
||||
def test_if_reverse_branches
|
||||
lst = compile( reverse_if )
|
||||
assert_equal TrueConstant , lst.if_true.class
|
||||
assert_nil lst.if_false
|
||||
end
|
||||
|
||||
def reverse_unless
|
||||
"true unless(false)"
|
||||
end
|
||||
def test_if_reverse
|
||||
lst = compile( reverse_unless )
|
||||
assert_equal IfStatement , lst.class
|
||||
end
|
||||
def test_if_reverse_cond
|
||||
lst = compile( reverse_unless )
|
||||
assert_equal ScopeStatement , lst.condition.class
|
||||
end
|
||||
def test_if_reverse_branches
|
||||
lst = compile( reverse_unless )
|
||||
assert_nil lst.if_true
|
||||
assert_equal TrueConstant ,lst.if_false.class
|
||||
end
|
||||
|
||||
def ternary
|
||||
"false ? true : false"
|
||||
end
|
||||
def test_if_ternary
|
||||
lst = compile( ternary )
|
||||
assert_equal IfStatement , lst.class
|
||||
end
|
||||
def test_if_ternary_cond
|
||||
lst = compile( ternary )
|
||||
assert_equal FalseConstant , lst.condition.class
|
||||
end
|
||||
def test_if_ternary_branches
|
||||
lst = compile( ternary )
|
||||
assert_equal TrueConstant , lst.if_true.class
|
||||
assert_equal FalseConstant, lst.if_false.class
|
||||
end
|
||||
end
|
||||
end
|
||||
|
57
test/ruby/test_if_statement1.rb
Normal file
57
test/ruby/test_if_statement1.rb
Normal file
@ -0,0 +1,57 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Ruby
|
||||
class TestIfStatement < MiniTest::Test
|
||||
include RubyTests
|
||||
|
||||
def reverse_if
|
||||
"true if(false)"
|
||||
end
|
||||
def test_if_reverse
|
||||
lst = compile( reverse_if )
|
||||
assert_equal IfStatement , lst.class
|
||||
end
|
||||
def test_if_reverse_cond
|
||||
lst = compile( reverse_if )
|
||||
assert_equal FalseConstant , lst.condition.class
|
||||
end
|
||||
def test_if_reverse_branches
|
||||
lst = compile( reverse_if )
|
||||
assert_equal TrueConstant , lst.if_true.class
|
||||
assert_nil lst.if_false
|
||||
end
|
||||
def reverse_unless
|
||||
"true unless(false)"
|
||||
end
|
||||
def test_if_reverse
|
||||
lst = compile( reverse_unless )
|
||||
assert_equal IfStatement , lst.class
|
||||
end
|
||||
def test_if_reverse_cond
|
||||
lst = compile( reverse_unless )
|
||||
assert_equal ScopeStatement , lst.condition.class
|
||||
end
|
||||
def test_if_reverse_branches
|
||||
lst = compile( reverse_unless )
|
||||
assert_nil lst.if_true
|
||||
assert_equal TrueConstant ,lst.if_false.class
|
||||
end
|
||||
|
||||
def ternary
|
||||
"false ? true : false"
|
||||
end
|
||||
def test_if_ternary
|
||||
lst = compile( ternary )
|
||||
assert_equal IfStatement , lst.class
|
||||
end
|
||||
def test_if_ternary_cond
|
||||
lst = compile( ternary )
|
||||
assert_equal FalseConstant , lst.condition.class
|
||||
end
|
||||
def test_if_ternary_branches
|
||||
lst = compile( ternary )
|
||||
assert_equal TrueConstant , lst.if_true.class
|
||||
assert_equal FalseConstant, lst.if_false.class
|
||||
end
|
||||
end
|
||||
end
|
48
test/ruby/test_if_statement2.rb
Normal file
48
test/ruby/test_if_statement2.rb
Normal file
@ -0,0 +1,48 @@
|
||||
require_relative 'helper'
|
||||
|
||||
module Ruby
|
||||
class TestIfStatementVool < MiniTest::Test
|
||||
include RubyTests
|
||||
|
||||
def setup
|
||||
@lst = compile("if(true) ; a = 1 ; else ; a = 2 ; end").to_vool
|
||||
end
|
||||
def test_class
|
||||
assert_equal Vool::IfStatement , @lst.class
|
||||
end
|
||||
def test_true
|
||||
assert_equal Vool::LocalAssignment , @lst.if_true.class
|
||||
end
|
||||
def test_false
|
||||
assert_equal Vool::LocalAssignment , @lst.if_false.class
|
||||
end
|
||||
def test_condition
|
||||
assert_equal Vool::TrueConstant , @lst.condition.class
|
||||
end
|
||||
end
|
||||
class TestIfStatementVoolHoisted < MiniTest::Test
|
||||
include RubyTests
|
||||
|
||||
def setup
|
||||
@lst = compile("if(a == 1) ; a = 1 end").to_vool
|
||||
end
|
||||
|
||||
def test_class
|
||||
assert_equal Vool::Statements , @lst.class
|
||||
end
|
||||
def test_first_class
|
||||
assert_equal Vool::LocalAssignment , @lst.first.class
|
||||
end
|
||||
def test_last_class
|
||||
assert_equal Vool::IfStatement , @lst.last.class
|
||||
end
|
||||
def test_true
|
||||
assert_equal Vool::LocalAssignment , @lst.last.if_true.class
|
||||
end
|
||||
def test_condition
|
||||
assert_equal Vool::LocalVariable , @lst.last.condition.class
|
||||
assert_equal @lst.first.name , @lst.last.condition.name
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user