diff --git a/lib/ast/expression.rb b/lib/ast/expression.rb index 0465d622..f867dad5 100644 --- a/lib/ast/expression.rb +++ b/lib/ast/expression.rb @@ -34,7 +34,7 @@ end require_relative "basic_expressions" require_relative "compound_expressions" -require_relative "conditional_expression" +require_relative "if_expression" require_relative "while_expression" require_relative "function_expression" require_relative "operator_expressions" diff --git a/lib/ast/conditional_expression.rb b/lib/ast/if_expression.rb similarity index 89% rename from lib/ast/conditional_expression.rb rename to lib/ast/if_expression.rb index 25bdac11..29b30854 100644 --- a/lib/ast/conditional_expression.rb +++ b/lib/ast/if_expression.rb @@ -1,5 +1,5 @@ module Ast - class ConditionalExpression < Expression + class IfExpression < Expression attr_reader :cond, :if_true, :if_false def initialize cond, if_true, if_false @cond, @if_true, @if_false = cond, if_true, if_false diff --git a/lib/parser/transform.rb b/lib/parser/transform.rb index f0242e4a..321eb25b 100644 --- a/lib/parser/transform.rb +++ b/lib/parser/transform.rb @@ -27,7 +27,7 @@ module Parser rule(:if => simple(:if), :conditional => simple(:conditional), :if_true => {:expressions => sequence(:if_true) , :else => simple(:else) }, :if_false => {:expressions => sequence(:if_false) , :end => simple(:e) }) do - Ast::ConditionalExpression.new(conditional, if_true, if_false) + Ast::IfExpression.new(conditional, if_true, if_false) end rule(:while => simple(:while), diff --git a/test/fragments/test_if.rb b/test/fragments/test_if.rb new file mode 100644 index 00000000..34867e09 --- /dev/null +++ b/test/fragments/test_if.rb @@ -0,0 +1,22 @@ +require_relative 'helper' + +class TestIf < MiniTest::Test + include Fragments + + def test_if + @string_input = <"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}} - @transform_output = Ast::ConditionalExpression.new( Ast::IntegerExpression.new(0), + @transform_output = Ast::IfExpression.new( Ast::IntegerExpression.new(0), [Ast::IntegerExpression.new(42)], [Ast::IntegerExpression.new(667)]) @parser = @parser.conditional diff --git a/test/parser/test_function_definition.rb b/test/parser/test_function_definition.rb index f5009ccf..2cc132f3 100644 --- a/test/parser/test_function_definition.rb +++ b/test/parser/test_function_definition.rb @@ -54,7 +54,7 @@ HERE :end=>"end"} @transform_output = Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n")] , - [Ast::ConditionalExpression.new(Ast::IntegerExpression.new(0), + [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::OperatorExpression.new("=", Ast::NameExpression.new("isit"), Ast::IntegerExpression.new(42))], [Ast::OperatorExpression.new("=", Ast::NameExpression.new("maybenot"),Ast::IntegerExpression.new(667))] )] )