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 w
end end
def on_while_statement statement def on_while statement
branch_type , condition , statements = *statement condition , statements = *statement
w = WhileStatement.new() w = WhileStatement.new( process(condition) )
w.branch_type = branch_type simplify_condition(w)
w.condition = process(condition)
w.statements = process(statements) w.statements = process(statements)
w w
end end
@ -130,16 +129,18 @@ module Vool
def on_if statement def on_if statement
# puts "IF #{statement}" # puts "IF #{statement}"
condition , if_true , if_false = *statement condition , if_true , if_false = *statement
w = IfStatement.new() w = IfStatement.new( process(condition) )
w.condition = process(condition) simplify_condition(w)
if(w.condition.is_a?(ScopeStatement) and w.condition.single?)
w.condition = w.condition.first
end
w.if_true = process(if_true) w.if_true = process(if_true)
w.if_false = process(if_false) w.if_false = process(if_false)
w w
end 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 def on_operator_value statement
operator , left_e , right_e = *statement operator , left_e , right_e = *statement
w = OperatorStatement.new() w = OperatorStatement.new()

View File

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

View File

@ -7,3 +7,4 @@ require_relative "test_if_statement"
require_relative "test_method_statement" require_relative "test_method_statement"
require_relative "test_send_statement" require_relative "test_send_statement"
require_relative "test_variables" 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