adds mom while
much like the if, difference will show later in the jump arrangement
This commit is contained in:
parent
76a87dd418
commit
af85cb7c67
@ -24,6 +24,7 @@ end
|
||||
|
||||
require_relative "simple_call"
|
||||
require_relative "if_statement"
|
||||
require_relative "while_statement"
|
||||
require_relative "truth_check"
|
||||
require_relative "jump"
|
||||
require_relative "slot_load"
|
||||
|
14
lib/mom/while_statement.rb
Normal file
14
lib/mom/while_statement.rb
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
module Mom
|
||||
class WhileStatement < Instruction
|
||||
attr_reader :condition , :statements
|
||||
|
||||
attr_accessor :hoisted
|
||||
|
||||
def initialize( cond , statements)
|
||||
@condition = cond
|
||||
@statements = statements
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -1,5 +1,8 @@
|
||||
require_relative "hoister"
|
||||
|
||||
module Vool
|
||||
class WhileStatement < Statement
|
||||
include Hoister
|
||||
attr_reader :condition , :statements
|
||||
|
||||
def initialize( condition , statements )
|
||||
@ -8,6 +11,15 @@ module Vool
|
||||
simplify_condition
|
||||
end
|
||||
|
||||
def to_mom( method )
|
||||
statements = @statements.to_mom( method )
|
||||
condition , hoisted = hoist_condition( method )
|
||||
cond = Mom::TruthCheck.new(condition.to_mom(method))
|
||||
check = Mom::WhileStatement.new( cond , statements )
|
||||
check.hoisted = hoisted.to_mom(method) if hoisted
|
||||
check
|
||||
end
|
||||
|
||||
def simplify_condition
|
||||
return unless @condition.is_a?(ScopeStatement)
|
||||
@condition = @condition.first if @condition.single?
|
||||
|
27
test/vool/to_mom/test_while_condition.rb
Normal file
27
test/vool/to_mom/test_while_condition.rb
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
require_relative "helper"
|
||||
|
||||
module Vool
|
||||
class TestWhileConditionMom < MiniTest::Test
|
||||
include MomCompile
|
||||
|
||||
def setup
|
||||
Risc.machine.boot
|
||||
@stats = compile_first_method( "while(@a == 5) ; 5.mod4 ; end")
|
||||
@first = @stats.first
|
||||
end
|
||||
|
||||
def test_if_compiles_as_array
|
||||
assert_equal Mom::WhileStatement , @first.class , @stats
|
||||
end
|
||||
def test_condition_compiles_to_slot
|
||||
assert_equal Mom::TruthCheck , @first.condition.class
|
||||
end
|
||||
def test_condition_is_slot
|
||||
assert_equal Mom::SlotDefinition , @first.condition.condition.class , @stats
|
||||
end
|
||||
def test_hoisetd
|
||||
assert_equal Mom::SlotConstant , @first.hoisted.class
|
||||
end
|
||||
end
|
||||
end
|
27
test/vool/to_mom/test_while_simple.rb
Normal file
27
test/vool/to_mom/test_while_simple.rb
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
require_relative "helper"
|
||||
|
||||
module Vool
|
||||
class TestSimpleWhileMom < MiniTest::Test
|
||||
include MomCompile
|
||||
|
||||
def setup
|
||||
Risc.machine.boot
|
||||
@stats = compile_first_method( "while(@a) ; 5.mod4 ; end")
|
||||
@first = @stats.first
|
||||
end
|
||||
|
||||
def test_if_compiles_as_array
|
||||
assert_equal Mom::WhileStatement , @first.class , @stats
|
||||
end
|
||||
def test_condition_compiles_to_check
|
||||
assert_equal Mom::TruthCheck , @first.condition.class , @stats
|
||||
end
|
||||
def test_condition_is_slot
|
||||
assert_equal Mom::SlotDefinition , @first.condition.condition.class , @stats
|
||||
end
|
||||
def test_nothing_hoisted
|
||||
assert_nil @first.hoisted , @stats
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user