Class: Minjs::ECMA262::StBlock

Inherits:
Statement show all
Defined in:
lib/minjs/ecma262/statement.rb

Overview

Base class of ECMA262 Block element.

See Also:

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Statement

#priority

Methods inherited from Base

#add_remove_paren, #concat

Constructor Details

#initialize(statement_list) ⇒ StBlock

Returns a new instance of StBlock.

Parameters:



35
36
37
38
39
40
41
# File 'lib/minjs/ecma262/statement.rb', line 35

def initialize(statement_list)
  if statement_list.kind_of? Array
    @statement_list = StatementList.new(statement_list)
  else
    @statement_list = statement_list
  end
end

Instance Attribute Details

#statement_listObject (readonly)

Returns the value of attribute statement_list.



32
33
34
# File 'lib/minjs/ecma262/statement.rb', line 32

def statement_list
  @statement_list
end

Instance Method Details

#==(obj) ⇒ Object

compare object



66
67
68
69
# File 'lib/minjs/ecma262/statement.rb', line 66

def ==(obj)
  self.class == obj.class and
    @statement_list == obj.statement_list
end

#[](i) ⇒ Statement

Returns the statement at index

Parameters:

  • i

    index

Returns:



133
134
135
# File 'lib/minjs/ecma262/statement.rb', line 133

def [](i)
  @statement_list[i]
end

#deep_dupObject

duplicate object

See Also:



45
46
47
# File 'lib/minjs/ecma262/statement.rb', line 45

def deep_dup
  self.class.new(@statement_list.deep_dup)
end

#empty?Boolean

Returns true if block is empty.

Returns:

  • (Boolean)

    true if block is empty



124
125
126
127
128
# File 'lib/minjs/ecma262/statement.rb', line 124

def empty?
  @statement_list.statement_list.select{|s|
    s.class != StEmpty
  }.length == 0
end

#remove_empty_statementObject

Removes empty statement in this block.



138
139
140
# File 'lib/minjs/ecma262/statement.rb', line 138

def remove_empty_statement
  statement_list.remove_empty_statement
end

#replace(from, to) ⇒ Object

Replaces children object.

See Also:



51
52
53
54
55
# File 'lib/minjs/ecma262/statement.rb', line 51

def replace(from, to)
  if from == @statement_list
    @statement_list = to
  end
end

#to_exp(options = {}) ⇒ Object

Converts block to expression and returns it.



86
87
88
89
90
# File 'lib/minjs/ecma262/statement.rb', line 86

def to_exp(options = {})
  @statement_list.statement_list.select{|s|
    s.class != StEmpty
  }[0].to_exp.deep_dup
end

#to_exp?Boolean

true if block can convert to expression

Returns:



78
79
80
81
82
83
# File 'lib/minjs/ecma262/statement.rb', line 78

def to_exp?
  t = @statement_list.statement_list.select{|s|
    s.class != StEmpty
  }
  t.length == 1 and t[0].to_exp?
end

#to_js(options = {}) ⇒ Object

Returns a ECMAScript string containg the representation of element.

See Also:



73
74
75
# File 'lib/minjs/ecma262/statement.rb', line 73

def to_js(options = {})
  concat(options, "{", @statement_list, "}")
end

#to_returnObject

Converts block to ‘return statement’ and returns it



119
120
121
# File 'lib/minjs/ecma262/statement.rb', line 119

def to_return
  to_statement.to_return
end

#to_return?Boolean

Returns true if block can convert to ‘return statement’.

Returns:

  • (Boolean)

    true if block can convert to ‘return statement’



114
115
116
# File 'lib/minjs/ecma262/statement.rb', line 114

def to_return?
  to_statement? and to_statement.to_return?
end

#to_statementObject

Converts block to single statement and return it.



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/minjs/ecma262/statement.rb', line 101

def to_statement
  t = @statement_list.statement_list.select{|s|
    s.class != StEmpty
  }

  if t[0]
    t[0].deep_dup
  else
    StEmpty.new
  end
end

#to_statement?Boolean

Returns true if block can convert to single statement.

Returns:

  • (Boolean)

    true if block can convert to single statement.



93
94
95
96
97
98
# File 'lib/minjs/ecma262/statement.rb', line 93

def to_statement?
  t = @statement_list.statement_list.select{|s|
    s.class != StEmpty
  }
  t.length == 1 || t.length == 0
end

#traverse(parent) {|parent, _self| ... } ⇒ Object

Traverses this children and itself with given block.

Yields:

Yield Parameters:

See Also:



60
61
62
63
# File 'lib/minjs/ecma262/statement.rb', line 60

def traverse(parent, &block)
  @statement_list.traverse(self, &block)
  yield parent, self
end