Class: Chelsy::Iteration Abstract

Inherits:
Stmt show all
Defined in:
lib/chelsy/ast.rb

Overview

This class is abstract.

Subclass to implement a custom iteration class.

Direct Known Subclasses

DoWhile, For, While

Instance Attribute Summary collapse

Attributes inherited from Element

#fragments, #post_fragments

Instance Method Summary collapse

Constructor Details

#initialize(condition_expr = nil, body_stmt = nil, **rest) {|Chelsy::Block| ... } ⇒ Iteration

Initialize iteration statement with its condition and iteration body statement. You can pass an optional code block which takes Block instance can be used to construct iteration body statements.

Parameters:

  • condition_expr (defaults to: nil)

    an expression which express condition

  • body_stmt (defaults to: nil)

    iteration body statement

Yields:

Raises:

  • (ArgumentError)

    Given neither body_stmt nor code block



1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/chelsy/ast.rb', line 1024

def initialize(condition_expr=nil, body_stmt=nil, **rest)
  @condition = Syntax::Expr.ensure(condition_expr) if condition_expr

  if block_given?
    @body = Block.new
    yield @body
  elsif body_stmt
    @body = Syntax::Stmt.ensure(body_stmt)
  else
    raise ArgumentError, "missing body statement"
  end

  super **rest
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



1014
1015
1016
# File 'lib/chelsy/ast.rb', line 1014

def body
  @body
end

#conditionObject (readonly)

Returns the value of attribute condition.



1014
1015
1016
# File 'lib/chelsy/ast.rb', line 1014

def condition
  @condition
end