Add support for unless

ifs without a true branch would crash before
Somewhat more verbose but unified
This commit is contained in:
Torsten Rüger 2019-08-14 22:24:35 +03:00
parent d6c38d15ba
commit 31ae0a9670
7 changed files with 101 additions and 25 deletions

4
.reek
View File

@ -13,13 +13,15 @@ FeatureEnvy:
- "Arm::Translator"
- "Vm::ToCode"
TooManyMethods:
max_methods: 30
exclude:
- "Vool::Compiler"
- "RubyX::RubyCompiler"
- "Ruby::RubyCompiler"
- "Risc::Interpreter"
- "Risc::TextWriter"
- "Arm::Translator"
- "Vm::ToCode"
- "Util::List"
UtilityFunction:
exclude:
- "Vool::Compiler"

View File

@ -1,10 +1,11 @@
before_install:
- sudo apt-get install -y qemu
language: ruby
sudo: false
before_install: gem install bundler --version '2.0.1'
script:
- ruby test/test_all.rb
- bundle exec codeclimate-test-reporter
rvm:
- 2.4.5
- 2.5.3
- 2.6.1
- 2.4.6
- 2.5.5
- 2.6.3

View File

@ -11,36 +11,23 @@ module Vool
end
def to_mom( compiler )
if_false ? full_if(compiler) : simple_if(compiler)
end
def simple_if(compiler)
true_label = Mom::Label.new( self,"true_label_#{object_id.to_s(16)}")
merge_label = Mom::Label.new( self,"merge_label_#{object_id.to_s(16)}")
head = Mom::TruthCheck.new(condition.slot_definition(compiler) , merge_label)
head << true_label
head << if_true.to_mom(compiler)
head << merge_label
end
def full_if(compiler)
true_label = Mom::Label.new( self , "true_label_#{object_id.to_s(16)}")
false_label = Mom::Label.new( self , "false_label_#{object_id.to_s(16)}")
merge_label = Mom::Label.new( self , "merge_label_#{object_id.to_s(16)}")
head = Mom::TruthCheck.new(condition.slot_definition(compiler) , false_label)
head << true_label
head << if_true.to_mom(compiler)
head << Mom::Jump.new(merge_label)
head << if_true.to_mom(compiler) if @if_true
head << Mom::Jump.new(merge_label) if @if_false
head << false_label
head << if_false.to_mom(compiler)
head << merge_label
head << if_false.to_mom(compiler) if @if_false
head << merge_label if @if_false
head
end
def each(&block)
block.call(condition)
@if_true.each(&block)
@if_true.each(&block) if @if_true
@if_false.each(&block) if @if_false
end

View File

@ -24,7 +24,6 @@ module Risc
produced = produce_body
check = produced.next(4)
assert_equal IsZero , check.class
assert check.label.name.start_with?("merge_label") , check.label.name
end
def test_false_label
produced = produce_body

56
test/mom/test_if_no_if.rb Normal file
View File

@ -0,0 +1,56 @@
require_relative "helper"
module Risc
class TestIfNoIf < MiniTest::Test
include Statements
def setup
super
@input = "unless(@a) ; arg = 5 ; end"
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero, #4
LoadConstant, OperatorInstruction, IsZero, Label, Branch, #9
Label, LoadConstant, SlotToReg, RegToSlot, Label] #14
end
def test_if_instructions
assert_nil msg = check_nil , msg
end
def test_false_load
produced = produce_body
assert_equal Parfait::FalseClass , produced.next(2).constant.class
end
def test_isnotzero
produced = produce_body
check = produced.next(4)
assert_equal IsZero , check.class
assert check.label.name.start_with?("false_") , check.label.name
end
def test_false_label
produced = produce_body
assert_equal Label , produced.next(10).class
end
def test_false_check
produced = produce_body
assert_equal produced.next(10).name , produced.next(4).label.name
end
def test_nil_load
produced = produce_body
assert_equal Parfait::NilClass , produced.next(5).constant.class
end
def est_nil_check
produced = produce_body
assert_equal Label , produced.next(4).label.class
assert_equal produced.next(12) , produced.next(4).label
end
def test_true_label
produced = produce_body
assert produced.next(8).name.start_with?("true_label")
end
def test_merge_label
produced = produce_body
assert produced.next(14).name.start_with?("merge_label")
end
end
end

View File

@ -0,0 +1,31 @@
require_relative "helper"
module Vool
class TestIfNoIf < MiniTest::Test
include VoolCompile
def setup
Parfait.boot!(Parfait.default_test_options)
@compiler = compile_first_method( "unless(@a) ; @a = 5 ; end")
@ins = @compiler.mom_instructions.next
end
def test_condition_compiles_to_check
assert_equal TruthCheck , @ins.class , @ins
end
def test_condition_is_slot
assert_equal SlotDefinition , @ins.condition.class , @ins
end
def test_label_after_check
assert_equal Label , @ins.next.class , @ins
end
def test_label_last
assert_equal Label , @ins.last.class , @ins
end
def test_array
check_array [TruthCheck, Label, Jump, Label, SlotLoad ,
Label, Label, ReturnSequence, Label] , @ins
end
end
end