adds while statement to vool

This commit is contained in:
Torsten Ruger 2017-04-03 11:49:21 +03:00
parent 5335d08408
commit c545bfdfc6
4 changed files with 56 additions and 11 deletions

View File

@ -118,11 +118,10 @@ module Vool
w
end
def on_while_statement statement
branch_type , condition , statements = *statement
w = WhileStatement.new()
w.branch_type = branch_type
w.condition = process(condition)
def on_while statement
condition , statements = *statement
w = WhileStatement.new( process(condition) )
simplify_condition(w)
w.statements = process(statements)
w
end
@ -130,16 +129,18 @@ module Vool
def on_if statement
# puts "IF #{statement}"
condition , if_true , if_false = *statement
w = IfStatement.new()
w.condition = process(condition)
if(w.condition.is_a?(ScopeStatement) and w.condition.single?)
w.condition = w.condition.first
end
w = IfStatement.new( process(condition) )
simplify_condition(w)
w.if_true = process(if_true)
w.if_false = process(if_false)
w
end
def simplify_condition( cond )
return unless cond.condition.is_a?(ScopeStatement)
cond.condition = cond.condition.first if cond.condition.single?
end
def on_operator_value statement
operator , left_e , right_e = *statement
w = OperatorStatement.new()

View File

@ -1,5 +1,9 @@
module Vool
class WhileStatement < Statement
attr_accessor :branch_type , :condition , :statements
attr_accessor :condition , :statements
def initialize( condition )
@condition = condition
end
end
end

View File

@ -7,3 +7,4 @@ require_relative "test_if_statement"
require_relative "test_method_statement"
require_relative "test_send_statement"
require_relative "test_variables"
require_relative "test_while_statement"

View File

@ -0,0 +1,39 @@
require_relative 'helper'
module Vool
class TestWhileStatement < MiniTest::Test
def basic_while
"while(10 < 12) ; true ; end"
end
def test_while_basic
lst = Compiler.compile( basic_while )
assert_equal WhileStatement , lst.class
end
def test_while_basic_cond
lst = Compiler.compile( basic_while )
assert_equal SendStatement , lst.condition.class
end
def test_while_basic_branches
lst = Compiler.compile( basic_while )
assert_equal TrueStatement , lst.statements.class
end
def reverse_while
"true while(false)"
end
def test_while_reverse_branches
lst = Compiler.compile( reverse_while )
assert_equal WhileStatement , lst.class
end
def test_while_reverse_cond
lst = Compiler.compile( reverse_while )
assert_equal FalseStatement , lst.condition.class
end
def test_while_reverse_branches
lst = Compiler.compile( reverse_while )
assert_equal TrueStatement , lst.statements.class
end
end
end