From f343ad934c7b34f78ee421e670a64f162d63e89f Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 2 Apr 2017 19:12:42 +0300 Subject: [PATCH] a start on conditionals --- lib/vool/compiler.rb | 9 ++++-- lib/vool/if_statement.rb | 16 +++++++++- lib/vool/statements.rb | 9 ++++++ test/vool/test_if_statement.rb | 54 ++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 test/vool/test_if_statement.rb diff --git a/lib/vool/compiler.rb b/lib/vool/compiler.rb index 55a4d445..2989e095 100644 --- a/lib/vool/compiler.rb +++ b/lib/vool/compiler.rb @@ -127,11 +127,14 @@ module Vool w end - def on_if_statement statement - branch_type , condition , if_true , if_false = *statement + def on_if statement +# puts "IF #{statement}" + condition , if_true , if_false = *statement w = IfStatement.new() - w.branch_type = branch_type w.condition = process(condition) + if(w.condition.is_a?(ScopeStatement) and w.condition.single?) + w.condition = w.condition.first + end w.if_true = process(if_true) w.if_false = process(if_false) w diff --git a/lib/vool/if_statement.rb b/lib/vool/if_statement.rb index bec83227..8d71845d 100644 --- a/lib/vool/if_statement.rb +++ b/lib/vool/if_statement.rb @@ -1,5 +1,19 @@ module Vool class IfStatement < Statement - attr_accessor :branch_type , :condition , :if_true , :if_false + attr_accessor :condition , :if_true , :if_false + + def initialize( cond = nil) + @condition = cond + @if_true = [] + @if_false = [] + end + + def has_false? + @if_false != nil + end + + def has_true? + @if_true != nil + end end end diff --git a/lib/vool/statements.rb b/lib/vool/statements.rb index f7281801..462ffa46 100644 --- a/lib/vool/statements.rb +++ b/lib/vool/statements.rb @@ -4,6 +4,15 @@ module Vool def initialize(statements) @statements = statements end + def empty? + @statements.empty? + end + def single? + @statements.length == 1 + end + def first + @statements.first + end end class ScopeStatement < Statements diff --git a/test/vool/test_if_statement.rb b/test/vool/test_if_statement.rb new file mode 100644 index 00000000..aeeba399 --- /dev/null +++ b/test/vool/test_if_statement.rb @@ -0,0 +1,54 @@ +require_relative 'helper' + +module Vool + class TestIfStatement < MiniTest::Test + + def basic_if + "if(10 < 12) ; true ; end" + end + + def test_if_basic + lst = Compiler.compile( basic_if ) + assert_equal IfStatement , lst.class + end + + def test_if_basic_cond + lst = Compiler.compile( basic_if ) + assert_equal SendStatement , lst.condition.class + end + def test_if_basic_branches + lst = Compiler.compile( basic_if ) + assert_equal TrueStatement , lst.if_true.class + assert_nil lst.if_false + end + + def pest_if_basicr + @input = s(:statements, s(:if_statement, :plus, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))), s(:true_statements, s(:return, s(:int, 3))), s(:false_statements, s(:return, s(:int, 4))))) + + @expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsPlus, LoadConstant , + RegToSlot, Branch, Label, LoadConstant, RegToSlot, Label , + LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn] + assert_nil msg = check_nil , msg + end + + + def pest_if_small_minus + @input = s(:statements, s(:if_statement, :minus, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))), s(:true_statements, s(:return, s(:int, 3))), s(:false_statements, nil))) + + @expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsMinus, Branch , + Label, LoadConstant, RegToSlot, Label, LoadConstant, SlotToReg , + RegToSlot, Label, FunctionReturn] + assert_nil msg = check_nil , msg + end + + + def pest_if_small_zero + @input = s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))), s(:true_statements, s(:return, s(:int, 3))), s(:false_statements, nil))) + + @expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsZero, Branch , + Label, LoadConstant, RegToSlot, Label, LoadConstant, SlotToReg , + RegToSlot, Label, FunctionReturn] + assert_nil msg = check_nil , msg + end + end +end