Class: NScript::Expressions

Inherits:
Node
  • Object
show all
Defined in:
lib/nscript/parser/nodes.rb

Constant Summary collapse

TRAILING_WHITESPACE =
/\s+$/

Constants inherited from Node

Node::TAB

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

children, #children, #compile_closure, #contains?, #idt, statement, #statement?, statement_only, #statement_only?, top_sensitive, #top_sensitive?, #write

Constructor Details

#initialize(*nodes) ⇒ Expressions

Returns a new instance of Expressions.



75
76
77
# File 'lib/nscript/parser/nodes.rb', line 75

def initialize(*nodes)
  @expressions = nodes.flatten
end

Instance Attribute Details

#functionObject

Returns the value of attribute function.



66
67
68
# File 'lib/nscript/parser/nodes.rb', line 66

def function
  @function
end

Class Method Details

.wrap(*nodes) ⇒ Object



70
71
72
73
# File 'lib/nscript/parser/nodes.rb', line 70

def self.wrap(*nodes)
  return nodes[0] if nodes.length == 1 && nodes[0].is_a?(Expressions)
  Expressions.new(*nodes)
end

Instance Method Details

#<<(node) ⇒ Object



79
80
81
82
# File 'lib/nscript/parser/nodes.rb', line 79

def <<(node)
  @expressions << node
  self
end

#compile(o = {}) ⇒ Object



102
103
104
# File 'lib/nscript/parser/nodes.rb', line 102

def compile(o={})
  o[:scope] ? super(o) : compile_root(o)
end

#compile_expression(node, o) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/nscript/parser/nodes.rb', line 129

def compile_expression(node, o)
  @indent = o[:indent]
  stmt    = node.statement?
  # We need to return the result if this is the last node in the expressions body.
  returns = o.delete(:return) && last?(node) && !node.statement_only?
  # Return the regular compile of the node, unless we need to return the result.
  return "#{stmt ? '' : idt}#{node.compile(o.merge(:top => true))}#{stmt ? '' : ';'}" unless returns
  # If it's a statement, the node knows how to return itself.
  return node.compile(o.merge(:return => true)) if node.statement?
  # If it's not part of a constructor, we can just return the value of the expression.
  return "#{idt}return #{node.compile(o)};" unless o[:scope].function && o[:scope].function.constructor?
  # It's the last line of a constructor, add a safety check.
  temp = o[:scope].free_variable
  "#{idt}#{temp} = #{node.compile(o)};\n#{idt}return #{o[:scope].function.name} === this.constructor ? this : #{temp};"
end

#compile_node(options = {}) ⇒ Object



106
107
108
# File 'lib/nscript/parser/nodes.rb', line 106

def compile_node(options={})
  write(@expressions.map {|n| compile_expression(n, options.dup) }.join("\n"))
end

#compile_root(o = {}) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/nscript/parser/nodes.rb', line 110

def compile_root(o={})
  indent = o[:no_wrap] ? '' : TAB
  @indent = indent
  o.merge!(:indent => indent, :scope => Scope.new(nil, self, nil))
  code = o[:globals] ? compile_node(o) : compile_with_declarations(o)
  code.gsub!(TRAILING_WHITESPACE, '')
  write(o[:no_wrap] ? code : "(function(){\n#{code}\n})();")
end

#compile_with_declarations(o = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/nscript/parser/nodes.rb', line 119

def compile_with_declarations(o={})
  code  = compile_node(o)
  args  = self.contains? {|n| n.is_a?(ValueNode) && n.arguments? }
  argv  = args && o[:scope].check('arguments') ? '' : 'var '
  code  = "#{idt}#{argv}arguments = Array.prototype.slice.call(arguments, 0);\n#{code}" if args
  code  = "#{idt}var #{o[:scope].compiled_assignments};\n#{code}" if o[:scope].assignments?(self)
  code  = "#{idt}var #{o[:scope].compiled_declarations};\n#{code}" if o[:scope].declarations?(self)
  write(code)
end

#empty?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/nscript/parser/nodes.rb', line 93

def empty?
  @expressions.empty?
end

#last?(node) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/nscript/parser/nodes.rb', line 97

def last?(node)
  @last_index ||= @expressions.last.is_a?(CommentNode) ? -2 : -1
  node == @expressions[@last_index]
end

#unshift(node) ⇒ Object



84
85
86
87
# File 'lib/nscript/parser/nodes.rb', line 84

def unshift(node)
  @expressions.unshift(node)
  self
end

#unwrapObject



89
90
91
# File 'lib/nscript/parser/nodes.rb', line 89

def unwrap
  @expressions.length == 1 ? @expressions.first : self
end