a start on conditionals
This commit is contained in:
parent
443fd13980
commit
f343ad934c
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
54
test/vool/test_if_statement.rb
Normal file
54
test/vool/test_if_statement.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user