class NScript::Expressions

Constants

TRAILING_WHITESPACE

Attributes

function[RW]

Public Class Methods

new(*nodes) click to toggle source
# File lib/nscript/parser/nodes.rb, line 75
def initialize(*nodes)
  @expressions = nodes.flatten
end
wrap(*nodes) click to toggle source
# 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

Public Instance Methods

<<(node) click to toggle source
# File lib/nscript/parser/nodes.rb, line 79
def <<(node)
  @expressions << node
  self
end
compile(o={}) click to toggle source
Calls superclass method NScript::Node#compile
# File lib/nscript/parser/nodes.rb, line 102
def compile(o={})
  o[:scope] ? super(o) : compile_root(o)
end
compile_expression(node, o) click to toggle source
# 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={}) click to toggle source
# 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={}) click to toggle source
# 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={}) click to toggle source
# 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?() click to toggle source
# File lib/nscript/parser/nodes.rb, line 93
def empty?
  @expressions.empty?
end
last?(node) click to toggle source
# 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) click to toggle source
# File lib/nscript/parser/nodes.rb, line 84
def unshift(node)
  @expressions.unshift(node)
  self
end
unwrap() click to toggle source
# File lib/nscript/parser/nodes.rb, line 89
def unwrap
  @expressions.length == 1 ? @expressions.first : self
end