lib/code/node/if.rb in code-ruby-0.4.0 vs lib/code/node/if.rb in code-ruby-0.5.0
- old
+ new
@@ -1,50 +1,54 @@
class Code
class Node
class If < Node
IF_KEYWORD = "if"
UNLESS_KEYWORD = "unless"
+ ELSIF_KEYWORD = "elsif"
+ ELSE_KEYWORD = "else"
- class Else
- def initialize(else_parsed)
- if else_parsed.key?(:operator)
- @operator = else_parsed.fetch(:operator)
- @statement =
- ::Code::Node::Statement.new(else_parsed.fetch(:statement))
- end
+ class Else < Node
+ attr_reader :operator, :statement, :body
- @body = ::Code::Node::Code.new(else_parsed.fetch(:body))
- end
+ def initialize(parsed)
+ @operator = parsed.delete(:operator)
+ @body = Node::Code.new(parsed.delete(:body))
- attr_reader :operator, :body, :statement
+ if parsed.key?(:statement)
+ @statement = Node::Statement.new(parsed.delete(:statement))
+ end
+ end
end
- def initialize(if_parsed)
- @if_operator = if_parsed.fetch(:if_operator)
- @if_statement =
- ::Code::Node::Statement.new(if_parsed.fetch(:if_statement))
- @if_body = ::Code::Node::Code.new(if_parsed.fetch(:if_body))
- @elses = if_parsed.fetch(:elses, [])
- @elses.map! { |else_parsed| ::Code::Node::If::Else.new(else_parsed) }
+ def initialize(parsed)
+ @first_operator = parsed.delete(:first_operator)
+ @first_statement = Node::Statement.new(parsed.delete(:first_statement))
+ @first_body = Node::Code.new(parsed.delete(:first_body))
+ @elses =
+ parsed.delete(:elses) { [] }.map { |elses| Node::If::Else.new(elses) }
+ super(parsed)
end
def evaluate(**args)
- if_object = @if_statement.evaluate(**args)
-
- if @if_operator == IF_KEYWORD && if_object.truthy?
- @if_body.evaluate(**args)
- elsif @if_operator == UNLESS_KEYWORD && if_object.falsy?
- @if_body.evaluate(**args)
+ if @first_operator == IF_KEYWORD &&
+ @first_statement.evaluate(**args).truthy?
+ @first_body.evaluate(**args)
+ elsif @first_operator == UNLESS_KEYWORD &&
+ @first_statement.evaluate(**args).falsy?
+ @first_body.evaluate(**args)
else
- @elses.each do |else_node|
- if else_node.operator == IF_KEYWORD
- else_object = else_node.statement.evaluate(**args)
- return else_node.body.evaluate(**args) if else_object.truthy?
- elsif else_node.operator == UNLESS_KEYWORD
- else_object = else_node.statement.evaluate(**args)
- return else_node.body.evaluate(**args) if else_object.falsy?
- elsif else_node.operator.nil?
- return else_node.body.evaluate(**args)
+ @elses.each do |elses|
+ if elses.operator == ELSIF_KEYWORD &&
+ elses.statement.evaluate(**args).truthy?
+ return elses.body.evaluate(**args)
+ elsif elses.operator == IF_KEYWORD &&
+ elses.statement.evaluate(**args).truthy?
+ return elses.body.evaluate(**args)
+ elsif elses.operator == UNLESS_KEYWORD &&
+ elses.statement.evaluate(**args).falsy?
+ return elses.body.evaluate(**args)
+ elsif elses.operator == ELSE_KEYWORD
+ return elses.body.evaluate(**args)
end
end
::Code::Object::Nothing.new
end