rename conditional to if expression

This commit is contained in:
Torsten Ruger 2014-05-24 10:18:54 +03:00
parent 6eeefc5617
commit 00d85156da
6 changed files with 27 additions and 5 deletions

View File

@ -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"

View File

@ -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

View File

@ -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),

22
test/fragments/test_if.rb Normal file
View File

@ -0,0 +1,22 @@
require_relative 'helper'
class TestIf < MiniTest::Test
include Fragments
def test_if
@string_input = <<HERE
def itest(n)
if( n < 12)
putstring("then")
else
putstring("else")
end
end
itest(10)
HERE
@should = [0x0,0xb0,0xa0,0xe3,0xa,0x0,0xa0,0xe3,0x1,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x40,0x2d,0xe9,0x0,0x10,0xa0,0xe3,0x1,0x20,0xa0,0xe3,0x1,0x0,0x50,0xe3,0x6,0x0,0x0,0xda,0x1,0x30,0xa0,0xe1,0x2,0x10,0xa0,0xe1,0x2,0x40,0x83,0xe0,0x4,0x20,0xa0,0xe1,0x1,0x50,0x40,0xe2,0x5,0x0,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x3f,0x0,0x2d,0xe9,0x2,0x0,0xa0,0xe1,0x12,0x0,0x0,0xeb,0x3f,0x0,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x20,0x41,0xe2,0x21,0x11,0x41,0xe0,0x21,0x12,0x81,0xe0,0x21,0x14,0x81,0xe0,0x21,0x18,0x81,0xe0,0xa1,0x11,0xa0,0xe1,0x1,0x31,0x81,0xe0,0x83,0x20,0x52,0xe0,0x1,0x10,0x81,0x52,0xa,0x20,0x82,0x42,0x30,0x20,0x82,0xe2,0x0,0x20,0xc0,0xe5,0x1,0x0,0x40,0xe2,0x0,0x0,0x51,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x0,0x10,0xa0,0xe1,0x24,0x0,0x8f,0xe2,0x9,0x0,0x80,0xe2,0xe9,0xff,0xff,0xeb,0x18,0x0,0x8f,0xe2,0xc,0x10,0xa0,0xe3,0x1,0x20,0xa0,0xe1,0x0,0x10,0xa0,0xe1,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0]
parse
write "if"
end
end

View File

@ -23,7 +23,7 @@ HERE
@parse_output = {:if=>"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

View File

@ -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))] )] )