2009-12-17 22:54:24 -05:00
|
|
|
module CoffeeScript
|
2009-12-13 18:37:29 -05:00
|
|
|
|
2009-12-17 22:58:40 -05:00
|
|
|
# The abstract base class for all CoffeeScript nodes.
|
2009-12-17 22:54:24 -05:00
|
|
|
class Node
|
|
|
|
# Tabs are two spaces for pretty-printing.
|
|
|
|
TAB = ' '
|
2009-12-15 10:07:10 -05:00
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Tag this node as a statement, meaning that it can't be used directly as
|
|
|
|
# the result of an expression.
|
|
|
|
def self.statement
|
|
|
|
class_eval "def statement?; true; end"
|
|
|
|
end
|
|
|
|
|
2010-01-03 13:59:17 -05:00
|
|
|
# Tag this node as a statement that cannot be transformed into an expression.
|
|
|
|
# (break, continue, etc.) It doesn't make sense to try to transform it.
|
|
|
|
def self.statement_only
|
|
|
|
statement
|
|
|
|
class_eval "def statement_only?; true; end"
|
2009-12-17 23:22:02 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-19 00:33:34 -05:00
|
|
|
def write(code)
|
2009-12-22 10:11:41 -05:00
|
|
|
puts "#{self.class.to_s}:\n#{@options.inspect}\n#{code}\n\n" if ENV['VERBOSE']
|
2009-12-19 00:33:34 -05:00
|
|
|
code
|
|
|
|
end
|
|
|
|
|
2010-01-03 16:32:59 -05:00
|
|
|
# This is extremely important -- we convert JS statements into expressions
|
|
|
|
# by wrapping them in a closure, only if it's possible, and we're not at
|
|
|
|
# the top level of a block (which would be unnecessary), and we haven't
|
|
|
|
# already been asked to return the result.
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
@options = o.dup
|
2010-01-07 20:55:30 -05:00
|
|
|
@indent = o[:indent]
|
2010-01-04 18:57:10 -05:00
|
|
|
top = self.is_a?(ForNode) ? @options[:top] : @options.delete(:top)
|
2010-01-03 16:32:59 -05:00
|
|
|
closure = statement? && !statement_only? && !top && !@options[:return]
|
|
|
|
closure ? compile_closure(@options) : compile_node(@options)
|
2009-12-22 10:11:41 -05:00
|
|
|
end
|
|
|
|
|
2010-01-03 13:59:17 -05:00
|
|
|
def compile_closure(o={})
|
2010-01-07 20:57:23 -05:00
|
|
|
o[:indent] += idt(1)
|
2010-01-07 20:55:30 -05:00
|
|
|
"(function() {\n#{compile_node(o.merge(:return => true))}\n#{idt}})()"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Quick method for the current indentation level, plus tabs out.
|
|
|
|
def idt(tabs=0)
|
|
|
|
@indent + (TAB * tabs)
|
2010-01-03 13:59:17 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Default implementations of the common node methods.
|
2010-01-03 13:59:17 -05:00
|
|
|
def unwrap; self; end
|
|
|
|
def statement?; false; end
|
|
|
|
def statement_only?; false; end
|
2009-12-15 00:27:34 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 22:58:40 -05:00
|
|
|
# A collection of nodes, each one representing an expression.
|
|
|
|
class Expressions < Node
|
2009-12-17 23:22:02 -05:00
|
|
|
statement
|
2009-12-17 22:58:40 -05:00
|
|
|
attr_reader :expressions
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-22 11:27:19 -05:00
|
|
|
STRIP_TRAILING_WHITESPACE = /\s+$/
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Wrap up a node as an Expressions, unless it already is.
|
2010-01-03 13:59:17 -05:00
|
|
|
def self.wrap(*nodes)
|
|
|
|
return nodes[0] if nodes.length == 1 && nodes[0].is_a?(Expressions)
|
|
|
|
Expressions.new(*nodes)
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2010-01-03 13:59:17 -05:00
|
|
|
def initialize(*nodes)
|
|
|
|
@expressions = nodes.flatten
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-14 23:03:51 -05:00
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Tack an expression onto the end of this node.
|
2009-12-17 22:54:24 -05:00
|
|
|
def <<(node)
|
2009-12-17 22:58:40 -05:00
|
|
|
@expressions << node
|
2009-12-17 22:54:24 -05:00
|
|
|
self
|
|
|
|
end
|
2009-12-15 10:07:10 -05:00
|
|
|
|
2009-12-31 17:50:12 -05:00
|
|
|
def unshift(node)
|
|
|
|
@expressions.unshift(node)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# If this Expressions consists of a single node, pull it back out.
|
|
|
|
def unwrap
|
2009-12-17 22:58:40 -05:00
|
|
|
@expressions.length == 1 ? @expressions.first : self
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 11:27:19 -05:00
|
|
|
# Is the node last in this block of expressions.
|
|
|
|
def last?(node)
|
|
|
|
@last_index ||= @expressions.last.is_a?(CommentNode) ? -2 : -1
|
|
|
|
node == @expressions[@last_index]
|
|
|
|
end
|
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile(o={})
|
2010-01-03 15:59:33 -05:00
|
|
|
o[:scope] ? super(o) : compile_root(o)
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
|
|
|
|
2010-01-03 13:59:17 -05:00
|
|
|
# The extra fancy is to handle pushing down returns to the final lines of
|
|
|
|
# inner statements. Variables first defined within the Expressions body
|
|
|
|
# have their declarations pushed up top of the closest scope.
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(options={})
|
2009-12-26 01:57:33 -05:00
|
|
|
compiled = @expressions.map do |node|
|
2010-01-03 15:13:59 -05:00
|
|
|
o = options.dup
|
2010-01-07 20:55:30 -05:00
|
|
|
@indent = o[:indent]
|
2010-01-03 13:59:17 -05:00
|
|
|
returns = o.delete(:return)
|
|
|
|
if last?(node) && returns && !node.statement_only?
|
|
|
|
if node.statement?
|
|
|
|
node.compile(o.merge(:return => true))
|
|
|
|
else
|
2010-01-06 23:47:36 -05:00
|
|
|
if o[:top] && o[:last_assign] && o[:last_assign][0..0][/[A-Z]/]
|
|
|
|
temp = o[:scope].free_variable
|
2010-01-07 20:55:30 -05:00
|
|
|
"#{idt}#{temp} = #{node.compile(o)};\n#{idt}return #{o[:last_assign]} === this.constructor ? this : #{temp};"
|
2010-01-06 23:47:36 -05:00
|
|
|
else
|
2010-01-07 20:55:30 -05:00
|
|
|
"#{idt}return #{node.compile(o)};"
|
2010-01-06 23:47:36 -05:00
|
|
|
end
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-14 23:03:51 -05:00
|
|
|
else
|
2010-01-03 15:59:33 -05:00
|
|
|
ending = node.statement? ? '' : ';'
|
2010-01-07 20:55:30 -05:00
|
|
|
indent = node.statement? ? '' : idt
|
2010-01-03 15:13:59 -05:00
|
|
|
"#{indent}#{node.compile(o.merge(:top => true))}#{ending}"
|
2009-12-14 23:03:51 -05:00
|
|
|
end
|
2009-12-26 01:57:33 -05:00
|
|
|
end
|
2010-01-03 15:13:59 -05:00
|
|
|
write(compiled.join("\n"))
|
|
|
|
end
|
|
|
|
|
|
|
|
# If this is the top-level Expressions, wrap everything in a safety closure.
|
|
|
|
def compile_root(o={})
|
|
|
|
indent = o[:no_wrap] ? '' : TAB
|
2010-01-07 20:55:30 -05:00
|
|
|
@indent = indent
|
2010-01-03 15:13:59 -05:00
|
|
|
o.merge!(:indent => indent, :scope => Scope.new(nil, self))
|
2010-01-07 21:10:25 -05:00
|
|
|
code = o[:globals] ? compile_node(o) : compile_with_declarations(o)
|
2010-01-03 15:13:59 -05:00
|
|
|
code.gsub!(STRIP_TRAILING_WHITESPACE, '')
|
|
|
|
o[:no_wrap] ? code : "(function(){\n#{code}\n})();"
|
|
|
|
end
|
|
|
|
|
|
|
|
def compile_with_declarations(o={})
|
2010-01-03 18:22:10 -05:00
|
|
|
code = compile_node(o)
|
2010-01-03 13:59:17 -05:00
|
|
|
decls = ''
|
2010-01-07 20:55:30 -05:00
|
|
|
decls = "#{idt}var #{o[:scope].declared_variables.join(', ')};\n" if o[:scope].declarations?(self)
|
2010-01-03 18:22:10 -05:00
|
|
|
decls + code
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2010-01-03 15:13:59 -05:00
|
|
|
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Literals are static values that have a Ruby representation, eg.: a string, a number,
|
|
|
|
# true, false, nil, etc.
|
|
|
|
class LiteralNode < Node
|
|
|
|
STATEMENTS = ['break', 'continue']
|
2009-12-17 21:10:49 -05:00
|
|
|
|
2010-01-05 09:10:45 -05:00
|
|
|
CONVERSIONS = {
|
|
|
|
'arguments' => 'Array.prototype.slice.call(arguments, 0)'
|
|
|
|
}
|
|
|
|
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :value
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(value)
|
|
|
|
@value = value
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def statement?
|
|
|
|
STATEMENTS.include?(@value.to_s)
|
|
|
|
end
|
2010-01-03 13:59:17 -05:00
|
|
|
alias_method :statement_only?, :statement?
|
2009-12-21 11:41:45 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2010-01-05 09:10:45 -05:00
|
|
|
val = CONVERSIONS[@value.to_s] || @value.to_s
|
2010-01-07 20:55:30 -05:00
|
|
|
indent = statement? ? idt : ''
|
2010-01-03 16:32:59 -05:00
|
|
|
ending = statement? ? ';' : ''
|
2010-01-05 09:10:45 -05:00
|
|
|
write("#{indent}#{val}#{ending}")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Try to return your expression, or tell it to return itself.
|
2009-12-17 22:54:24 -05:00
|
|
|
class ReturnNode < Node
|
2010-01-03 13:59:17 -05:00
|
|
|
statement_only
|
2009-12-17 23:22:02 -05:00
|
|
|
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :expression
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(expression)
|
|
|
|
@expression = expression
|
|
|
|
end
|
2009-12-13 18:37:29 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
|
|
|
return write(@expression.compile(o.merge(:return => true))) if @expression.statement?
|
2009-12-22 10:11:41 -05:00
|
|
|
compiled = @expression.compile(o)
|
2010-01-07 20:55:30 -05:00
|
|
|
write(@expression.statement? ? "#{compiled}\n#{idt}return null;" : "#{idt}return #{compiled};")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 23:59:12 -05:00
|
|
|
end
|
|
|
|
|
2009-12-22 11:27:19 -05:00
|
|
|
# Pass through CoffeeScript comments into JavaScript comments at the
|
|
|
|
# same position.
|
|
|
|
class CommentNode < Node
|
2010-01-03 13:59:17 -05:00
|
|
|
statement_only
|
2009-12-22 11:27:19 -05:00
|
|
|
|
|
|
|
def initialize(lines)
|
|
|
|
@lines = lines.value
|
|
|
|
end
|
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o={})
|
2010-01-07 20:55:30 -05:00
|
|
|
delimiter = "\n#{idt}//"
|
2009-12-22 11:27:19 -05:00
|
|
|
comment = "#{delimiter}#{@lines.join(delimiter)}"
|
|
|
|
write(comment)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Node for a function invocation. Takes care of converting super() calls into
|
|
|
|
# calls against the prototype's function of the same name.
|
2009-12-17 22:54:24 -05:00
|
|
|
class CallNode < Node
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :variable, :arguments
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(variable, arguments=[])
|
|
|
|
@variable, @arguments = variable, arguments
|
|
|
|
end
|
2009-12-17 09:07:42 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def new_instance
|
|
|
|
@new = true
|
|
|
|
self
|
|
|
|
end
|
2009-12-17 09:07:42 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def super?
|
|
|
|
@variable == :super
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-31 19:52:13 -05:00
|
|
|
def prefix
|
|
|
|
@new ? "new " : ''
|
|
|
|
end
|
|
|
|
|
2009-12-31 20:02:15 -05:00
|
|
|
def splat?
|
|
|
|
@arguments.any? {|a| a.is_a?(ArgSplatNode) }
|
|
|
|
end
|
|
|
|
|
2010-01-03 10:19:39 -05:00
|
|
|
def <<(argument)
|
|
|
|
@arguments << argument
|
|
|
|
end
|
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2009-12-31 20:02:15 -05:00
|
|
|
return write(compile_splat(o)) if splat?
|
2009-12-26 14:55:34 -05:00
|
|
|
args = @arguments.map{|a| a.compile(o) }.join(', ')
|
2009-12-22 10:11:41 -05:00
|
|
|
return write(compile_super(args, o)) if super?
|
|
|
|
write("#{prefix}#{@variable.compile(o)}(#{args})")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-17 09:07:42 -05:00
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile_super(args, o)
|
2009-12-30 23:13:22 -05:00
|
|
|
methname = o[:last_assign]
|
2009-12-24 19:23:23 -05:00
|
|
|
arg_part = args.empty? ? '' : ", #{args}"
|
2010-01-08 10:54:44 -05:00
|
|
|
meth = o[:proto_assign] ? "#{o[:proto_assign]}.__superClass__.#{methname}" :
|
|
|
|
"#{methname}.__superClass__.constructor"
|
|
|
|
"#{meth}.call(this#{arg_part})"
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-31 19:52:13 -05:00
|
|
|
|
|
|
|
def compile_splat(o)
|
|
|
|
meth = @variable.compile(o)
|
|
|
|
obj = @variable.source || 'this'
|
|
|
|
args = @arguments.map do |arg|
|
|
|
|
code = arg.compile(o)
|
2009-12-31 20:02:15 -05:00
|
|
|
code = arg.is_a?(ArgSplatNode) ? code : "[#{code}]"
|
2009-12-31 19:52:13 -05:00
|
|
|
arg.equal?(@arguments.first) ? code : ".concat(#{code})"
|
|
|
|
end
|
|
|
|
"#{prefix}#{meth}.apply(#{obj}, #{args.join('')})"
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-24 19:49:23 -05:00
|
|
|
# Node to extend an object's prototype with an ancestor object.
|
2009-12-25 16:57:47 -05:00
|
|
|
# After goog.inherits from the Closure Library.
|
2009-12-24 19:49:23 -05:00
|
|
|
class ExtendsNode < Node
|
2010-01-03 13:59:17 -05:00
|
|
|
statement
|
2009-12-24 19:49:23 -05:00
|
|
|
attr_reader :sub_object, :super_object
|
|
|
|
|
|
|
|
def initialize(sub_object, super_object)
|
|
|
|
@sub_object, @super_object = sub_object, super_object
|
|
|
|
end
|
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o={})
|
2010-01-08 09:35:02 -05:00
|
|
|
constructor = o[:scope].free_variable
|
2009-12-25 16:57:47 -05:00
|
|
|
sub, sup = @sub_object.compile(o), @super_object.compile(o)
|
2010-01-08 09:35:02 -05:00
|
|
|
"#{idt}#{constructor} = function(){};\n#{idt}" +
|
|
|
|
"#{constructor}.prototype = #{sup}.prototype;\n#{idt}" +
|
|
|
|
"#{sub}.__superClass__ = #{sup}.prototype;\n#{idt}" +
|
|
|
|
"#{sub}.prototype = new #{constructor}();\n#{idt}" +
|
2010-01-03 15:59:33 -05:00
|
|
|
"#{sub}.prototype.constructor = #{sub};"
|
2009-12-24 19:49:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-12-19 22:55:58 -05:00
|
|
|
# A value, indexed or dotted into, or vanilla.
|
2009-12-17 22:54:24 -05:00
|
|
|
class ValueNode < Node
|
2009-12-31 19:52:13 -05:00
|
|
|
attr_reader :literal, :properties, :last, :source
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-18 07:21:59 -05:00
|
|
|
def initialize(literal, properties=[])
|
|
|
|
@literal, @properties = literal, properties
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 18:37:29 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def <<(other)
|
|
|
|
@properties << other
|
|
|
|
self
|
2009-12-17 09:07:42 -05:00
|
|
|
end
|
2009-12-13 20:29:44 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def properties?
|
|
|
|
return !@properties.empty?
|
|
|
|
end
|
2009-12-13 20:29:44 -05:00
|
|
|
|
2009-12-18 22:30:09 -05:00
|
|
|
def statement?
|
|
|
|
@literal.is_a?(Node) && @literal.statement? && !properties?
|
|
|
|
end
|
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2010-01-02 00:20:24 -05:00
|
|
|
only = o.delete(:only_first)
|
|
|
|
props = only ? @properties[0...-1] : @properties
|
|
|
|
parts = [@literal, props].flatten.map do |val|
|
2009-12-22 10:11:41 -05:00
|
|
|
val.respond_to?(:compile) ? val.compile(o) : val.to_s
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
|
|
|
@last = parts.last
|
2009-12-31 19:52:13 -05:00
|
|
|
@source = parts.length > 1 ? parts[0...-1].join('') : nil
|
2009-12-19 00:33:34 -05:00
|
|
|
write(parts.join(''))
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 20:29:44 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# A dotted accessor into a part of a value.
|
2009-12-19 00:33:34 -05:00
|
|
|
class AccessorNode < Node
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :name
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
2009-12-13 20:29:44 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2009-12-19 00:33:34 -05:00
|
|
|
write(".#{@name}")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# An indexed accessor into a part of an array or object.
|
2009-12-19 00:33:34 -05:00
|
|
|
class IndexNode < Node
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :index
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(index)
|
|
|
|
@index = index
|
|
|
|
end
|
2009-12-16 21:43:26 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2009-12-22 10:11:41 -05:00
|
|
|
write("[#{@index.compile(o)}]")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-16 21:43:26 -05:00
|
|
|
end
|
|
|
|
|
2009-12-25 19:20:28 -05:00
|
|
|
# A range literal. Ranges can be used to extract portions (slices) of arrays,
|
|
|
|
# or to specify a range for array comprehensions.
|
2010-01-01 10:55:43 -05:00
|
|
|
class RangeNode < Node
|
2009-12-25 19:20:28 -05:00
|
|
|
attr_reader :from, :to
|
|
|
|
|
|
|
|
def initialize(from, to, exclusive=false)
|
|
|
|
@from, @to, @exclusive = from, to, exclusive
|
|
|
|
end
|
|
|
|
|
|
|
|
def exclusive?
|
|
|
|
@exclusive
|
|
|
|
end
|
|
|
|
|
2009-12-26 23:35:43 -05:00
|
|
|
def less_operator
|
|
|
|
@exclusive ? '<' : '<='
|
|
|
|
end
|
2009-12-26 11:57:13 -05:00
|
|
|
|
2009-12-26 23:35:43 -05:00
|
|
|
def greater_operator
|
|
|
|
@exclusive ? '>' : '>='
|
2009-12-26 11:57:13 -05:00
|
|
|
end
|
|
|
|
|
2010-01-01 10:55:43 -05:00
|
|
|
def compile_variables(o)
|
2010-01-07 20:55:30 -05:00
|
|
|
@indent = o[:indent]
|
2010-01-01 10:55:43 -05:00
|
|
|
@from_var, @to_var = o[:scope].free_variable, o[:scope].free_variable
|
|
|
|
from_val, to_val = @from.compile(o), @to.compile(o)
|
2010-01-07 20:27:26 -05:00
|
|
|
write("#{@from_var} = #{from_val}; #{@to_var} = #{to_val};\n#{idt}")
|
2010-01-01 10:55:43 -05:00
|
|
|
end
|
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
|
|
|
idx, step = o.delete(:index), o.delete(:step)
|
2010-01-05 22:17:09 -05:00
|
|
|
return compile_array(o) unless idx
|
2010-01-01 11:19:57 -05:00
|
|
|
vars = "#{idx}=#{@from_var}"
|
2010-01-01 11:54:59 -05:00
|
|
|
step = step ? step.compile(o) : '1'
|
2010-01-01 11:19:57 -05:00
|
|
|
compare = "(#{@from_var} <= #{@to_var} ? #{idx} #{less_operator} #{@to_var} : #{idx} #{greater_operator} #{@to_var})"
|
2010-01-01 11:54:59 -05:00
|
|
|
incr = "(#{@from_var} <= #{@to_var} ? #{idx} += #{step} : #{idx} -= #{step})"
|
2010-01-01 10:55:43 -05:00
|
|
|
write("#{vars}; #{compare}; #{incr}")
|
2009-12-26 03:16:40 -05:00
|
|
|
end
|
|
|
|
|
2010-01-05 22:17:09 -05:00
|
|
|
# Expand the range into the equivalent array, if it's not being used as
|
|
|
|
# part of a comprehension, slice, or splice.
|
|
|
|
# TODO: This generates pretty ugly code ... shrink it.
|
|
|
|
def compile_array(o)
|
|
|
|
body = Expressions.wrap(LiteralNode.new(Value.new('i')))
|
2010-01-05 22:49:51 -05:00
|
|
|
arr = Expressions.wrap(ForNode.new(body, {:source => ValueNode.new(self)}, Value.new('i')))
|
2010-01-05 22:17:09 -05:00
|
|
|
ParentheticalNode.new(CallNode.new(CodeNode.new([], arr))).compile(o)
|
|
|
|
end
|
|
|
|
|
2009-12-25 19:20:28 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# An array slice literal. Unlike JavaScript's Array#slice, the second parameter
|
|
|
|
# specifies the index of the end of the slice (just like the first parameter)
|
|
|
|
# is the index of the beginning.
|
2009-12-19 00:33:34 -05:00
|
|
|
class SliceNode < Node
|
2009-12-25 19:20:28 -05:00
|
|
|
attr_reader :range
|
2009-12-18 07:21:59 -05:00
|
|
|
|
2009-12-25 19:20:28 -05:00
|
|
|
def initialize(range)
|
|
|
|
@range = range
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2009-12-25 19:20:28 -05:00
|
|
|
from = @range.from.compile(o)
|
|
|
|
to = @range.to.compile(o)
|
|
|
|
plus_part = @range.exclusive? ? '' : ' + 1'
|
|
|
|
write(".slice(#{from}, #{to}#{plus_part})")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-15 10:07:10 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Setting the value of a local variable, or the value of an object property.
|
2009-12-17 22:54:24 -05:00
|
|
|
class AssignNode < Node
|
2009-12-24 19:23:23 -05:00
|
|
|
PROTO_ASSIGN = /\A(\S+)\.prototype/
|
2009-12-30 23:13:22 -05:00
|
|
|
LEADING_DOT = /\A\./
|
2009-12-19 00:33:34 -05:00
|
|
|
|
2009-12-18 07:11:01 -05:00
|
|
|
attr_reader :variable, :value, :context
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(variable, value, context=nil)
|
|
|
|
@variable, @value, @context = variable, value, context
|
|
|
|
end
|
2009-12-17 21:10:49 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2010-01-02 00:20:24 -05:00
|
|
|
return compile_splice(o) if @variable.properties.last.is_a?(SliceNode)
|
2009-12-26 01:57:33 -05:00
|
|
|
name = @variable.compile(o)
|
2009-12-30 23:13:22 -05:00
|
|
|
last = @variable.last.to_s.sub(LEADING_DOT, '')
|
2009-12-24 19:23:23 -05:00
|
|
|
proto = name[PROTO_ASSIGN, 1]
|
2010-01-03 13:59:17 -05:00
|
|
|
o = o.merge(:last_assign => last, :proto_assign => proto)
|
2010-01-01 22:00:34 -05:00
|
|
|
o[:immediate_assign] = last if @value.is_a?(CodeNode) && last.match(Lexer::IDENTIFIER)
|
2009-12-26 01:57:33 -05:00
|
|
|
return write("#{name}: #{@value.compile(o)}") if @context == :object
|
|
|
|
o[:scope].find(name) unless @variable.properties?
|
2010-01-03 15:13:59 -05:00
|
|
|
val = "#{name} = #{@value.compile(o)}"
|
2010-01-07 20:55:30 -05:00
|
|
|
write(o[:return] ? "#{idt}return (#{val})" : val)
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2010-01-02 00:20:24 -05:00
|
|
|
|
|
|
|
def compile_splice(o)
|
|
|
|
var = @variable.compile(o.merge(:only_first => true))
|
|
|
|
range = @variable.properties.last.range
|
|
|
|
plus = range.exclusive? ? '' : ' + 1'
|
|
|
|
from = range.from.compile(o)
|
|
|
|
to = "#{range.to.compile(o)} - #{from}#{plus}"
|
|
|
|
write("#{var}.splice.apply(#{var}, [#{from}, #{to}].concat(#{@value.compile(o)}))")
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
2009-12-13 20:29:44 -05:00
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Simple Arithmetic and logical operations. Performs some conversion from
|
|
|
|
# CoffeeScript operations into their JavaScript equivalents.
|
2009-12-17 22:54:24 -05:00
|
|
|
class OpNode < Node
|
|
|
|
CONVERSIONS = {
|
2009-12-30 13:34:25 -05:00
|
|
|
:== => "===",
|
|
|
|
:'!=' => "!==",
|
|
|
|
:and => '&&',
|
|
|
|
:or => '||',
|
|
|
|
:is => '===',
|
|
|
|
:isnt => "!==",
|
|
|
|
:not => '!'
|
2009-12-17 22:54:24 -05:00
|
|
|
}
|
2009-12-30 13:34:25 -05:00
|
|
|
CONDITIONALS = [:'||=', :'&&=']
|
|
|
|
PREFIX_OPERATORS = [:typeof, :delete]
|
2009-12-17 22:54:24 -05:00
|
|
|
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :operator, :first, :second
|
|
|
|
|
2009-12-24 03:41:12 -05:00
|
|
|
def initialize(operator, first, second=nil, flip=false)
|
|
|
|
@first, @second, @flip = first, second, flip
|
2009-12-30 13:34:25 -05:00
|
|
|
@operator = CONVERSIONS[operator.to_sym] || operator
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-15 00:27:34 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def unary?
|
|
|
|
@second.nil?
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2009-12-30 13:34:25 -05:00
|
|
|
return write(compile_conditional(o)) if CONDITIONALS.include?(@operator.to_sym)
|
2009-12-22 10:11:41 -05:00
|
|
|
return write(compile_unary(o)) if unary?
|
|
|
|
write("#{@first.compile(o)} #{@operator} #{@second.compile(o)}")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile_conditional(o)
|
|
|
|
first, second = @first.compile(o), @second.compile(o)
|
2009-12-17 22:54:24 -05:00
|
|
|
sym = @operator[0..1]
|
|
|
|
"#{first} = #{first} #{sym} #{second}"
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile_unary(o)
|
2009-12-30 13:34:25 -05:00
|
|
|
space = PREFIX_OPERATORS.include?(@operator.to_sym) ? ' ' : ''
|
2009-12-24 03:41:12 -05:00
|
|
|
parts = [@operator.to_s, space, @first.compile(o)]
|
|
|
|
parts.reverse! if @flip
|
|
|
|
parts.join('')
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# A function definition. The only node that creates a new Scope.
|
2009-12-17 22:54:24 -05:00
|
|
|
class CodeNode < Node
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :params, :body
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(params, body)
|
|
|
|
@params = params
|
|
|
|
@body = body
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
|
|
|
shared_scope = o.delete(:shared_scope)
|
|
|
|
o[:scope] = shared_scope || Scope.new(o[:scope], @body)
|
|
|
|
o[:return] = true
|
2010-01-03 16:32:59 -05:00
|
|
|
o[:top] = true
|
2010-01-07 20:57:23 -05:00
|
|
|
o[:indent] = idt(1)
|
2009-12-24 20:37:24 -05:00
|
|
|
o.delete(:no_wrap)
|
2010-01-07 21:10:25 -05:00
|
|
|
o.delete(:globals)
|
2009-12-30 23:13:22 -05:00
|
|
|
name = o.delete(:immediate_assign)
|
2009-12-31 19:52:13 -05:00
|
|
|
if @params.last.is_a?(ParamSplatNode)
|
2009-12-31 17:50:12 -05:00
|
|
|
splat = @params.pop
|
|
|
|
splat.index = @params.length
|
|
|
|
@body.unshift(splat)
|
|
|
|
end
|
2009-12-31 18:03:39 -05:00
|
|
|
@params.each {|id| o[:scope].parameter(id.to_s) }
|
2010-01-03 15:13:59 -05:00
|
|
|
code = @body.compile_with_declarations(o)
|
2009-12-30 23:13:22 -05:00
|
|
|
name_part = name ? " #{name}" : ''
|
2010-01-07 20:55:30 -05:00
|
|
|
write("function#{name_part}(#{@params.join(', ')}) {\n#{code}\n#{idt}}")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
end
|
|
|
|
|
2009-12-31 17:50:12 -05:00
|
|
|
# A parameter splat in a function definition.
|
2009-12-31 19:52:13 -05:00
|
|
|
class ParamSplatNode < Node
|
2009-12-31 17:50:12 -05:00
|
|
|
attr_accessor :index
|
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o={})
|
2009-12-31 18:03:39 -05:00
|
|
|
o[:scope].find(@name)
|
2009-12-31 19:52:13 -05:00
|
|
|
write("#{@name} = Array.prototype.slice.call(arguments, #{@index})")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ArgSplatNode < Node
|
|
|
|
attr_reader :value
|
|
|
|
|
|
|
|
def initialize(value)
|
|
|
|
@value = value
|
2009-12-31 17:50:12 -05:00
|
|
|
end
|
2009-12-31 19:52:13 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o={})
|
2009-12-31 19:52:13 -05:00
|
|
|
write(@value.compile(o))
|
|
|
|
end
|
|
|
|
|
2009-12-31 17:50:12 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# An object literal.
|
2009-12-17 22:54:24 -05:00
|
|
|
class ObjectNode < Node
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :properties
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(properties = [])
|
|
|
|
@properties = properties
|
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-30 20:24:24 -05:00
|
|
|
# All the mucking about with commas is to make sure that CommentNodes and
|
|
|
|
# AssignNodes get interleaved correctly, with no trailing commas or
|
|
|
|
# commas affixed to comments. TODO: Extract this and add it to ArrayNode.
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2010-01-07 20:57:23 -05:00
|
|
|
o[:indent] = idt(1)
|
2009-12-30 20:24:24 -05:00
|
|
|
joins = Hash.new("\n")
|
|
|
|
non_comments = @properties.select {|p| !p.is_a?(CommentNode) }
|
|
|
|
non_comments.each {|p| joins[p] = p == non_comments.last ? "\n" : ",\n" }
|
2010-01-03 18:35:03 -05:00
|
|
|
props = @properties.map { |prop|
|
2009-12-30 20:24:24 -05:00
|
|
|
join = joins[prop]
|
|
|
|
join = '' if prop == @properties.last
|
2010-01-07 20:55:30 -05:00
|
|
|
indent = prop.is_a?(CommentNode) ? '' : idt(1)
|
|
|
|
"#{indent}#{prop.compile(o)}#{join}"
|
2009-12-22 11:50:43 -05:00
|
|
|
}.join('')
|
2010-01-07 20:55:30 -05:00
|
|
|
write("{\n#{props}\n#{idt}}")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-15 00:27:34 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# An array literal.
|
2009-12-17 22:54:24 -05:00
|
|
|
class ArrayNode < Node
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :objects
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(objects=[])
|
|
|
|
@objects = objects
|
|
|
|
end
|
2009-12-15 00:27:34 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2010-01-07 20:57:23 -05:00
|
|
|
o[:indent] = idt(1)
|
2009-12-22 11:50:43 -05:00
|
|
|
objects = @objects.map { |obj|
|
2010-01-03 18:35:03 -05:00
|
|
|
code = obj.compile(o)
|
|
|
|
obj.is_a?(CommentNode) ? "\n#{code}\n#{o[:indent]}" :
|
|
|
|
obj == @objects.last ? code : "#{code}, "
|
2009-12-22 11:50:43 -05:00
|
|
|
}.join('')
|
2010-01-07 20:55:30 -05:00
|
|
|
ending = objects.include?("\n") ? "\n#{idt}]" : ']'
|
2009-12-22 11:50:43 -05:00
|
|
|
write("[#{objects}#{ending}")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-15 00:27:34 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# A while loop, the only sort of low-level loop exposed by CoffeeScript. From
|
|
|
|
# it, all other loops can be manufactured.
|
2009-12-17 22:54:24 -05:00
|
|
|
class WhileNode < Node
|
2009-12-17 23:22:02 -05:00
|
|
|
statement
|
|
|
|
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :condition, :body
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(condition, body)
|
|
|
|
@condition, @body = condition, body
|
|
|
|
end
|
2009-12-15 00:27:34 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2010-01-03 18:58:34 -05:00
|
|
|
returns = o.delete(:return)
|
2010-01-07 20:57:23 -05:00
|
|
|
o[:indent] = idt(1)
|
2010-01-03 15:59:33 -05:00
|
|
|
o[:top] = true
|
|
|
|
cond = @condition.compile(o)
|
2010-01-07 20:55:30 -05:00
|
|
|
post = returns ? "\n#{idt}return null;" : ''
|
2010-01-09 11:58:50 -05:00
|
|
|
return write("#{idt}while (#{cond}) null;#{post}") if @body.nil?
|
2010-01-07 20:55:30 -05:00
|
|
|
write("#{idt}while (#{cond}) {\n#{@body.compile(o)}\n#{idt}}#{post}")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-15 00:27:34 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# The replacement for the for loop is an array comprehension (that compiles)
|
|
|
|
# into a for loop. Also acts as an expression, able to return the result
|
|
|
|
# of the comprehenion. Unlike Python array comprehensions, it's able to pass
|
|
|
|
# the current index of the loop as a second parameter.
|
2009-12-17 22:54:24 -05:00
|
|
|
class ForNode < Node
|
2009-12-17 23:22:02 -05:00
|
|
|
statement
|
2009-12-15 21:30:37 -05:00
|
|
|
|
2010-01-01 11:54:59 -05:00
|
|
|
attr_reader :body, :source, :name, :index, :filter, :step
|
2009-12-18 07:21:59 -05:00
|
|
|
|
2010-01-01 11:54:59 -05:00
|
|
|
def initialize(body, source, name, index=nil)
|
|
|
|
@body, @name, @index = body, name, index
|
|
|
|
@source = source[:source]
|
|
|
|
@filter = source[:filter]
|
|
|
|
@step = source[:step]
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-15 00:27:34 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2010-01-04 18:57:10 -05:00
|
|
|
top_level = o.delete(:top) && !o[:return]
|
2010-01-05 22:49:51 -05:00
|
|
|
range = @source.is_a?(ValueNode) && @source.literal.is_a?(RangeNode) && @source.properties.empty?
|
|
|
|
source = range ? @source.literal : @source
|
2009-12-25 17:18:05 -05:00
|
|
|
scope = o[:scope]
|
|
|
|
index_found = @index && scope.find(@index)
|
2010-01-07 20:55:30 -05:00
|
|
|
body_dent = idt(1)
|
2009-12-25 17:18:05 -05:00
|
|
|
svar = scope.free_variable
|
2009-12-31 14:52:14 -05:00
|
|
|
ivar = range ? name : @index ? @index : scope.free_variable
|
2010-01-04 18:57:10 -05:00
|
|
|
rvar = scope.free_variable unless top_level
|
2010-01-07 20:27:26 -05:00
|
|
|
fvar = scope.free_variable
|
2009-12-26 03:16:40 -05:00
|
|
|
if range
|
2009-12-26 23:35:43 -05:00
|
|
|
index_var = scope.free_variable
|
2010-01-05 22:49:51 -05:00
|
|
|
source_part = source.compile_variables(o)
|
|
|
|
for_part = "#{index_var}=0, #{source.compile(o.merge(:index => ivar, :step => @step))}, #{index_var}++"
|
2009-12-26 03:16:40 -05:00
|
|
|
else
|
2009-12-26 03:27:49 -05:00
|
|
|
index_var = nil
|
2010-01-07 20:55:30 -05:00
|
|
|
source_part = "#{svar} = #{source.compile(o)};\n#{idt}"
|
2010-01-07 20:27:26 -05:00
|
|
|
for_part = "#{ivar}=0; #{ivar}<#{svar}.length; #{ivar}++"
|
2009-12-26 03:16:40 -05:00
|
|
|
end
|
2009-12-25 17:18:05 -05:00
|
|
|
body = @body
|
2010-01-07 20:55:30 -05:00
|
|
|
set_result = rvar ? "#{idt}#{rvar} = []; " : idt
|
2010-01-04 18:57:10 -05:00
|
|
|
return_result = rvar || ''
|
|
|
|
if top_level
|
|
|
|
body = Expressions.wrap(body)
|
|
|
|
else
|
2010-01-07 20:27:26 -05:00
|
|
|
body = Expressions.wrap(CallNode.new(
|
|
|
|
ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [@body.unwrap]
|
|
|
|
))
|
2010-01-04 01:06:31 -05:00
|
|
|
end
|
2010-01-03 13:59:17 -05:00
|
|
|
if o[:return]
|
2009-12-25 17:18:05 -05:00
|
|
|
return_result = "return #{return_result}" if o[:return]
|
2009-12-28 00:50:02 -05:00
|
|
|
o.delete(:return)
|
2009-12-31 16:09:27 -05:00
|
|
|
body = IfNode.new(@filter, body, nil, :statement => true) if @filter
|
2009-12-18 22:30:09 -05:00
|
|
|
elsif @filter
|
2009-12-31 16:50:46 -05:00
|
|
|
body = Expressions.wrap(IfNode.new(@filter, @body))
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-14 23:03:51 -05:00
|
|
|
|
2010-01-07 20:55:30 -05:00
|
|
|
return_result = "\n#{idt}#{return_result};" unless top_level
|
2010-01-03 15:59:33 -05:00
|
|
|
body = body.compile(o.merge(:indent => body_dent, :top => true))
|
2010-01-07 20:27:26 -05:00
|
|
|
vars = range ? @name : "#{@name}, #{ivar}"
|
2010-01-07 20:55:30 -05:00
|
|
|
func = "#{fvar} = function(#{vars}) {\n#{body}\n#{idt}};\n#{idt}"
|
|
|
|
return write(set_result + source_part + func + "for (#{for_part}) #{fvar}(#{ivar});\n#{idt}#{return_result}") if range
|
2010-01-07 20:27:26 -05:00
|
|
|
|
|
|
|
call = "#{fvar}(#{svar}[#{ivar}], #{ivar})"
|
2010-01-07 20:55:30 -05:00
|
|
|
fast = "if (#{svar} instanceof Array) {\n#{idt(1)}for (#{for_part}) #{call};\n#{idt}} else {\n#{idt(1)}"
|
|
|
|
slow = "for (#{ivar} in #{svar}) { if (#{svar}.hasOwnProperty(#{ivar})) #{call}; }\n#{idt}}\n#{idt}#{return_result}"
|
2010-01-07 20:27:26 -05:00
|
|
|
write(set_result + source_part + func + fast + slow)
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-14 10:00:31 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# A try/catch/finally block.
|
2009-12-17 22:54:24 -05:00
|
|
|
class TryNode < Node
|
2009-12-17 23:22:02 -05:00
|
|
|
statement
|
|
|
|
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :try, :error, :recovery, :finally
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(try, error, recovery, finally=nil)
|
|
|
|
@try, @error, @recovery, @finally = try, error, recovery, finally
|
|
|
|
end
|
2009-12-14 10:00:31 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2010-01-07 20:57:23 -05:00
|
|
|
o[:indent] = idt(1)
|
2010-01-03 15:59:33 -05:00
|
|
|
o[:top] = true
|
2009-12-24 02:01:39 -05:00
|
|
|
error_part = @error ? " (#{@error}) " : ' '
|
2010-01-07 20:55:30 -05:00
|
|
|
catch_part = @recovery && " catch#{error_part}{\n#{@recovery.compile(o)}\n#{idt}}"
|
|
|
|
finally_part = @finally && " finally {\n#{@finally.compile(o.merge(:return => nil))}\n#{idt}}"
|
|
|
|
write("#{idt}try {\n#{@try.compile(o)}\n#{idt}}#{catch_part}#{finally_part}")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 23:25:00 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Throw an exception.
|
2009-12-17 22:54:24 -05:00
|
|
|
class ThrowNode < Node
|
2010-01-03 13:59:17 -05:00
|
|
|
statement_only
|
2009-12-17 23:22:02 -05:00
|
|
|
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :expression
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(expression)
|
|
|
|
@expression = expression
|
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2010-01-07 20:55:30 -05:00
|
|
|
write("#{idt}throw #{@expression.compile(o)};")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
end
|
|
|
|
|
2010-01-01 12:31:05 -05:00
|
|
|
# Check an expression for existence (meaning not null or undefined).
|
|
|
|
class ExistenceNode < Node
|
|
|
|
attr_reader :expression
|
|
|
|
|
|
|
|
def initialize(expression)
|
|
|
|
@expression = expression
|
|
|
|
end
|
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
|
|
|
val = @expression.compile(o)
|
2010-01-05 00:34:18 -05:00
|
|
|
write("(typeof #{val} !== \"undefined\" && #{val} !== null)")
|
2010-01-01 12:31:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-25 23:36:22 -05:00
|
|
|
# An extra set of parentheses, supplied by the script source.
|
|
|
|
# You can't wrap parentheses around bits that get compiled into JS statements,
|
|
|
|
# unfortunately.
|
2009-12-17 22:54:24 -05:00
|
|
|
class ParentheticalNode < Node
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :expressions
|
|
|
|
|
2009-12-30 21:44:51 -05:00
|
|
|
def initialize(expressions, line=nil)
|
2009-12-18 22:30:09 -05:00
|
|
|
@expressions = expressions.unwrap
|
2009-12-30 21:44:51 -05:00
|
|
|
@line = line
|
2009-12-18 22:30:09 -05:00
|
|
|
end
|
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2009-12-22 10:11:41 -05:00
|
|
|
compiled = @expressions.compile(o)
|
2009-12-17 22:54:24 -05:00
|
|
|
compiled = compiled[0...-1] if compiled[-1..-1] == ';'
|
2009-12-26 14:55:34 -05:00
|
|
|
write("(#{compiled})")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
end
|
|
|
|
|
2009-12-24 04:33:59 -05:00
|
|
|
# If/else statements. Switch/whens get compiled into these. Acts as an
|
2009-12-17 23:22:02 -05:00
|
|
|
# expression by pushing down requested returns to the expression bodies.
|
|
|
|
# Single-expression IfNodes are compiled into ternary operators if possible,
|
|
|
|
# because ternaries are first-class returnable assignable expressions.
|
2009-12-17 22:54:24 -05:00
|
|
|
class IfNode < Node
|
2009-12-18 07:21:59 -05:00
|
|
|
attr_reader :condition, :body, :else_body
|
|
|
|
|
2009-12-18 23:13:59 -05:00
|
|
|
def initialize(condition, body, else_body=nil, tags={})
|
2009-12-17 22:54:24 -05:00
|
|
|
@condition = condition
|
2009-12-17 23:22:02 -05:00
|
|
|
@body = body && body.unwrap
|
|
|
|
@else_body = else_body && else_body.unwrap
|
2009-12-18 23:13:59 -05:00
|
|
|
@tags = tags
|
2009-12-23 20:24:55 -05:00
|
|
|
@condition = OpNode.new("!", ParentheticalNode.new(@condition)) if @tags[:invert]
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def <<(else_body)
|
2009-12-17 23:22:02 -05:00
|
|
|
eb = else_body.unwrap
|
2009-12-17 22:54:24 -05:00
|
|
|
@else_body ? @else_body << eb : @else_body = eb
|
|
|
|
self
|
|
|
|
end
|
2009-12-31 14:52:14 -05:00
|
|
|
|
2010-01-09 13:25:44 -05:00
|
|
|
def add_comment(comment)
|
|
|
|
@comment = comment
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2009-12-30 18:59:33 -05:00
|
|
|
def force_statement
|
|
|
|
@tags[:statement] = true
|
|
|
|
self
|
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Rewrite a chain of IfNodes with their switch condition for equality.
|
|
|
|
def rewrite_condition(expression)
|
|
|
|
@condition = OpNode.new("is", expression, @condition)
|
|
|
|
@else_body.rewrite_condition(expression) if chain?
|
|
|
|
self
|
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Rewrite a chain of IfNodes to add a default case as the final else.
|
2009-12-30 14:32:59 -05:00
|
|
|
def add_else(exprs)
|
|
|
|
chain? ? @else_body.add_else(exprs) : @else_body = (exprs && exprs.unwrap)
|
2009-12-17 22:54:24 -05:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2009-12-17 23:34:52 -05:00
|
|
|
# If the else_body is an IfNode itself, then we've got an if-else chain.
|
2009-12-17 22:54:24 -05:00
|
|
|
def chain?
|
|
|
|
@chain ||= @else_body && @else_body.is_a?(IfNode)
|
|
|
|
end
|
|
|
|
|
2009-12-17 23:34:52 -05:00
|
|
|
# The IfNode only compiles into a statement if either of the bodies needs
|
|
|
|
# to be a statement.
|
2009-12-17 22:54:24 -05:00
|
|
|
def statement?
|
2010-01-09 13:25:44 -05:00
|
|
|
@is_statement ||= !!(@comment || @tags[:statement] || @body.statement? || (@else_body && @else_body.statement?))
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
|
2010-01-03 15:13:59 -05:00
|
|
|
def compile_node(o)
|
2009-12-22 12:18:27 -05:00
|
|
|
write(statement? ? compile_statement(o) : compile_ternary(o))
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
|
|
|
|
2009-12-18 00:49:23 -05:00
|
|
|
# Compile the IfNode as a regular if-else statement. Flattened chains
|
|
|
|
# force sub-else bodies into statement form.
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile_statement(o)
|
2010-01-07 20:57:23 -05:00
|
|
|
child = o.delete(:chain_child)
|
|
|
|
cond_o = o.dup
|
2009-12-27 00:55:56 -05:00
|
|
|
cond_o.delete(:return)
|
2010-01-07 20:57:23 -05:00
|
|
|
o[:indent] = idt(1)
|
|
|
|
o[:top] = true
|
|
|
|
if_dent = child ? '' : idt
|
2010-01-09 13:25:44 -05:00
|
|
|
com_dent = child ? idt : ''
|
|
|
|
prefix = @comment ? @comment.compile(cond_o) + "\n#{com_dent}" : ''
|
|
|
|
if_part = "#{prefix}#{if_dent}if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{idt}}"
|
2009-12-17 23:34:52 -05:00
|
|
|
return if_part unless @else_body
|
|
|
|
else_part = chain? ?
|
2010-01-07 20:55:30 -05:00
|
|
|
" else #{@else_body.compile(o.merge(:indent => idt, :chain_child => true))}" :
|
|
|
|
" else {\n#{Expressions.wrap(@else_body).compile(o)}\n#{idt}}"
|
2009-12-17 22:54:24 -05:00
|
|
|
if_part + else_part
|
|
|
|
end
|
|
|
|
|
2009-12-17 23:34:52 -05:00
|
|
|
# Compile the IfNode into a ternary operator.
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile_ternary(o)
|
|
|
|
if_part = "#{@condition.compile(o)} ? #{@body.compile(o)}"
|
|
|
|
else_part = @else_body ? "#{@else_body.compile(o)}" : 'null'
|
2009-12-17 22:54:24 -05:00
|
|
|
"#{if_part} : #{else_part}"
|
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
end
|
2009-12-17 22:54:24 -05:00
|
|
|
|
|
|
|
end
|