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
|
|
|
|
|
|
|
|
# Tag this node as having a custom return, meaning that instead of returning
|
|
|
|
# it from the outside, you ask it to return itself, and it obliges.
|
|
|
|
def self.custom_return
|
|
|
|
class_eval "def custom_return?; true; end"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tag this node as having a custom assignment, meaning that instead of
|
|
|
|
# assigning it to a variable name from the outside, you pass it the variable
|
|
|
|
# name and let it take care of it.
|
|
|
|
def self.custom_assign
|
|
|
|
class_eval "def custom_assign?; true; end"
|
|
|
|
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
|
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
@options = o.dup
|
|
|
|
end
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# Default implementations of the common node methods.
|
|
|
|
def unwrap; self; end
|
|
|
|
def line_ending; ';'; end
|
|
|
|
def statement?; false; end
|
|
|
|
def custom_return?; false; end
|
|
|
|
def custom_assign?; 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.
|
2009-12-17 22:54:24 -05:00
|
|
|
def self.wrap(node)
|
2009-12-17 22:58:40 -05:00
|
|
|
node.is_a?(Expressions) ? node : Expressions.new([node])
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-13 17:07:16 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def initialize(nodes)
|
2009-12-17 22:58:40 -05:00
|
|
|
@expressions = nodes
|
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-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
|
|
|
|
|
2009-12-17 23:22:02 -05:00
|
|
|
# If this is the top-level Expressions, wrap everything in a safety closure.
|
2009-12-24 18:31:00 -05:00
|
|
|
def root_compile(o={})
|
|
|
|
indent = o[:no_wrap] ? '' : TAB
|
2009-12-26 01:57:33 -05:00
|
|
|
code = compile(o.merge(:indent => indent, :scope => Scope.new), o[:no_wrap] ? nil : :code)
|
2009-12-22 11:27:19 -05:00
|
|
|
code.gsub!(STRIP_TRAILING_WHITESPACE, '')
|
2009-12-24 18:31:00 -05:00
|
|
|
o[:no_wrap] ? code : "(function(){\n#{code}\n})();"
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
|
|
|
|
2009-12-19 22:55:58 -05:00
|
|
|
# The extra fancy is to handle pushing down returns and assignments
|
|
|
|
# recursively to the final lines of inner statements.
|
2009-12-26 01:57:33 -05:00
|
|
|
# Variables first defined within the Expressions body have their
|
|
|
|
# declarations pushed up to the top scope.
|
|
|
|
def compile(options={}, parent=nil)
|
2009-12-24 18:31:00 -05:00
|
|
|
return root_compile(options) unless options[:scope]
|
2009-12-26 01:57:33 -05:00
|
|
|
compiled = @expressions.map do |node|
|
2009-12-22 10:11:41 -05:00
|
|
|
o = super(options)
|
2009-12-22 11:27:19 -05:00
|
|
|
if last?(node) && (o[:return] || o[:assign])
|
2009-12-22 10:11:41 -05:00
|
|
|
if o[:return]
|
|
|
|
if node.statement? || node.custom_return?
|
|
|
|
"#{o[:indent]}#{node.compile(o)}#{node.line_ending}"
|
2009-12-19 00:33:34 -05:00
|
|
|
else
|
2009-12-27 01:24:21 -05:00
|
|
|
o.delete(:return)
|
2009-12-22 10:11:41 -05:00
|
|
|
"#{o[:indent]}return #{node.compile(o)}#{node.line_ending}"
|
2009-12-19 00:33:34 -05:00
|
|
|
end
|
2009-12-22 10:11:41 -05:00
|
|
|
elsif o[:assign]
|
|
|
|
if node.statement? || node.custom_assign?
|
|
|
|
"#{o[:indent]}#{node.compile(o)}#{node.line_ending}"
|
2009-12-19 00:33:34 -05:00
|
|
|
else
|
2009-12-26 01:57:33 -05:00
|
|
|
"#{o[:indent]}#{AssignNode.new(o[:assign], node).compile(o)};"
|
2009-12-19 00:33:34 -05:00
|
|
|
end
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-14 23:03:51 -05:00
|
|
|
else
|
2009-12-22 10:11:41 -05:00
|
|
|
o.delete(:return) and o.delete(:assign)
|
|
|
|
"#{o[:indent]}#{node.compile(o)}#{node.line_ending}"
|
2009-12-14 23:03:51 -05:00
|
|
|
end
|
2009-12-26 01:57:33 -05:00
|
|
|
end
|
|
|
|
scope = options[:scope]
|
|
|
|
declarations = scope.any_declared? && parent == :code ? "#{options[:indent]}var #{scope.declared_variables.join(', ')};\n" : ''
|
|
|
|
code = declarations + compiled.join("\n")
|
2009-12-19 00:33:34 -05:00
|
|
|
write(code)
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
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
|
|
|
|
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
|
2009-12-17 21:10:49 -05:00
|
|
|
|
2009-12-21 11:41:45 -05:00
|
|
|
def line_ending
|
|
|
|
@value.to_s[-1..-1] == ';' ? '' : ';'
|
|
|
|
end
|
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
|
|
|
write(@value.to_s)
|
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
|
2009-12-17 23:22:02 -05:00
|
|
|
statement
|
|
|
|
custom_return
|
|
|
|
|
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
|
|
|
|
2009-12-18 23:13:59 -05:00
|
|
|
def line_ending
|
|
|
|
@expression.custom_return? ? '' : ';'
|
|
|
|
end
|
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
|
|
|
return write(@expression.compile(o.merge(:return => true))) if @expression.custom_return?
|
|
|
|
compiled = @expression.compile(o)
|
2009-12-19 00:33:34 -05:00
|
|
|
write(@expression.statement? ? "#{compiled}\n#{indent}return null" : "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
|
|
|
|
statement
|
|
|
|
|
|
|
|
def initialize(lines)
|
|
|
|
@lines = lines.value
|
|
|
|
end
|
|
|
|
|
|
|
|
def line_ending
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
def compile(o={})
|
|
|
|
delimiter = "\n#{o[:indent]}//"
|
|
|
|
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-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
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?
|
2009-12-17 22:54:24 -05:00
|
|
|
prefix = @new ? "new " : ''
|
2009-12-22 10:11:41 -05:00
|
|
|
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}"
|
2009-12-25 16:57:47 -05:00
|
|
|
"#{o[:proto_assign]}.__superClass__.#{methname}.call(this#{arg_part})"
|
2009-12-17 22:54:24 -05:00
|
|
|
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
|
|
|
|
attr_reader :sub_object, :super_object
|
|
|
|
|
|
|
|
def initialize(sub_object, super_object)
|
|
|
|
@sub_object, @super_object = sub_object, super_object
|
|
|
|
end
|
|
|
|
|
|
|
|
def compile(o={})
|
2009-12-25 16:57:47 -05:00
|
|
|
sub, sup = @sub_object.compile(o), @super_object.compile(o)
|
|
|
|
"#{sub}.__superClass__ = #{sup}.prototype;\n#{o[:indent]}" +
|
|
|
|
"#{sub}.prototype = new #{sup}();\n#{o[:indent]}" +
|
|
|
|
"#{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-18 07:21:59 -05:00
|
|
|
attr_reader :literal, :properties, :last
|
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
|
|
|
|
|
|
|
|
def custom_assign?
|
|
|
|
@literal.is_a?(Node) && @literal.custom_assign? && !properties?
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_return?
|
|
|
|
@literal.is_a?(Node) && @literal.custom_return? && !properties?
|
|
|
|
end
|
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
|
|
|
parts = [@literal, @properties].flatten.map do |val|
|
|
|
|
val.respond_to?(:compile) ? val.compile(o) : val.to_s
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
|
|
|
@last = parts.last
|
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
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(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
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
|
|
|
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.
|
|
|
|
class RangeNode
|
|
|
|
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
|
|
|
|
|
2009-12-31 14:52:14 -05:00
|
|
|
def compile(o, fv)
|
2009-12-26 23:35:43 -05:00
|
|
|
fvv, tvv = @from.compile(o), @to.compile(o)
|
2009-12-31 14:52:14 -05:00
|
|
|
vars = "#{fv}=#{fvv}"
|
|
|
|
compare = "(#{fvv} <= #{tvv} ? #{fv} #{less_operator} #{tvv} : #{fv} #{greater_operator} #{tvv})"
|
2009-12-26 23:35:43 -05:00
|
|
|
incr = "(#{fvv} <= #{tvv} ? #{fv} += 1 : #{fv} -= 1)"
|
|
|
|
"#{vars}; #{compare}; #{incr}"
|
2009-12-26 03:16:40 -05:00
|
|
|
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
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
2009-12-25 19:20:28 -05:00
|
|
|
o = super(o)
|
|
|
|
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-17 23:22:02 -05:00
|
|
|
custom_return
|
|
|
|
|
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
|
|
|
|
2009-12-19 00:33:34 -05:00
|
|
|
def line_ending
|
|
|
|
@value.custom_assign? ? '' : ';'
|
|
|
|
end
|
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
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]
|
2009-12-26 01:57:33 -05:00
|
|
|
o = o.merge(:assign => @variable, :last_assign => last, :proto_assign => proto)
|
2009-12-30 23:13:22 -05:00
|
|
|
o[:immediate_assign] = last if @value.is_a?(CodeNode)
|
2009-12-26 01:57:33 -05:00
|
|
|
return write("#{name}: #{@value.compile(o)}") if @context == :object
|
|
|
|
o[:scope].find(name) unless @variable.properties?
|
|
|
|
return write(@value.compile(o)) if @value.custom_assign?
|
2009-12-27 00:55:56 -05:00
|
|
|
val = "#{name} = #{@value.compile(o)}"
|
|
|
|
write(o[:return] && !@value.custom_return? ? "return (#{val})" : val)
|
2009-12-17 22:54:24 -05:00
|
|
|
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
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(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
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
|
|
|
o[:scope] = Scope.new(o[:scope])
|
|
|
|
o[:return] = true
|
|
|
|
indent = o[:indent]
|
|
|
|
o[:indent] += TAB
|
|
|
|
o.delete(:assign)
|
2009-12-24 20:37:24 -05:00
|
|
|
o.delete(:no_wrap)
|
2009-12-30 23:13:22 -05:00
|
|
|
name = o.delete(:immediate_assign)
|
2009-12-26 01:57:33 -05:00
|
|
|
@params.each {|id| o[:scope].parameter(id.to_s) }
|
|
|
|
code = @body.compile(o, :code)
|
2009-12-30 23:13:22 -05:00
|
|
|
name_part = name ? " #{name}" : ''
|
|
|
|
write("function#{name_part}(#{@params.join(', ')}) {\n#{code}\n#{indent}}")
|
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 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.
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
|
|
|
indent = o[:indent]
|
|
|
|
o[:indent] += TAB
|
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" }
|
2009-12-22 11:50:43 -05:00
|
|
|
props = @properties.map { |prop|
|
2009-12-30 20:24:24 -05:00
|
|
|
join = joins[prop]
|
|
|
|
join = '' if prop == @properties.last
|
|
|
|
o[:indent] + prop.compile(o) + join
|
2009-12-22 11:50:43 -05:00
|
|
|
}.join('')
|
2009-12-19 00:33:34 -05:00
|
|
|
write("{\n#{props}\n#{indent}}")
|
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
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
2009-12-22 11:50:43 -05:00
|
|
|
objects = @objects.map { |obj|
|
|
|
|
joiner = obj.is_a?(CommentNode) ? "\n#{o[:indent] + TAB}" : obj == @objects.last ? '' : ', '
|
|
|
|
obj.compile(o.merge(:indent => o[:indent] + TAB)) + joiner
|
|
|
|
}.join('')
|
|
|
|
ending = objects.include?("\n") ? "\n#{o[:indent]}]" : ']'
|
|
|
|
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
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def line_ending
|
|
|
|
''
|
|
|
|
end
|
2009-12-15 00:27:34 -05:00
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
2009-12-25 02:09:24 -05:00
|
|
|
o.delete(:return)
|
2009-12-22 10:11:41 -05:00
|
|
|
indent = o[:indent] + TAB
|
2009-12-26 14:55:34 -05:00
|
|
|
cond = @condition.compile(o)
|
2009-12-22 10:11:41 -05:00
|
|
|
write("while (#{cond}) {\n#{@body.compile(o.merge(:indent => indent))}\n#{o[:indent]}}")
|
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
|
|
|
|
custom_return
|
|
|
|
custom_assign
|
2009-12-15 21:30:37 -05:00
|
|
|
|
2009-12-18 22:30:09 -05:00
|
|
|
attr_reader :body, :source, :name, :filter, :index
|
2009-12-18 07:21:59 -05:00
|
|
|
|
2009-12-18 22:30:09 -05:00
|
|
|
def initialize(body, source, name, filter, index=nil)
|
|
|
|
@body, @source, @name, @filter, @index = body, source, name, filter, index
|
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 line_ending
|
|
|
|
''
|
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
2009-12-26 03:16:40 -05:00
|
|
|
range = @source.is_a?(RangeNode)
|
2009-12-25 17:18:05 -05:00
|
|
|
scope = o[:scope]
|
|
|
|
name_found = scope.find(@name)
|
|
|
|
index_found = @index && scope.find(@index)
|
|
|
|
svar = scope.free_variable
|
2009-12-31 14:52:14 -05:00
|
|
|
ivar = range ? name : @index ? @index : scope.free_variable
|
2009-12-25 17:18:05 -05:00
|
|
|
rvar = scope.free_variable
|
2009-12-26 03:16:40 -05:00
|
|
|
if range
|
2009-12-31 14:52:14 -05:00
|
|
|
body_dent = o[:indent] + TAB
|
|
|
|
source_part, var_part = '', '', ''
|
|
|
|
pre_cond, post_cond = '', ''
|
2009-12-26 23:35:43 -05:00
|
|
|
index_var = scope.free_variable
|
2009-12-31 14:52:14 -05:00
|
|
|
for_part = "#{index_var}=0, #{@source.compile(o, ivar)}, #{index_var}++"
|
2009-12-26 03:16:40 -05:00
|
|
|
else
|
2009-12-26 03:27:49 -05:00
|
|
|
index_var = nil
|
2009-12-31 14:52:14 -05:00
|
|
|
body_dent = o[:indent] + TAB + TAB
|
2009-12-26 03:16:40 -05:00
|
|
|
source_part = "#{svar} = #{@source.compile(o)};\n#{o[:indent]}"
|
2009-12-31 14:52:14 -05:00
|
|
|
for_part = "#{ivar} in #{svar}"
|
|
|
|
pre_cond = "\n#{o[:indent] + TAB}if (#{svar}.hasOwnProperty(#{ivar})) {"
|
|
|
|
var_part = "\n#{body_dent}#{@name} = #{svar}[#{ivar}];"
|
|
|
|
post_cond = "\n#{o[:indent] + TAB}}"
|
2009-12-26 03:16:40 -05:00
|
|
|
end
|
2009-12-25 17:18:05 -05:00
|
|
|
body = @body
|
|
|
|
suffix = ';'
|
2009-12-26 01:57:33 -05:00
|
|
|
set_result = "#{rvar} = [];\n#{o[:indent]}"
|
2009-12-25 17:18:05 -05:00
|
|
|
return_result = rvar
|
2009-12-31 15:03:32 -05:00
|
|
|
body = CallNode.new(ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [@body])
|
2009-12-25 17:18:05 -05:00
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
if o[:return] || o[:assign]
|
2009-12-26 01:57:33 -05:00
|
|
|
return_result = "#{o[:assign].compile(o)} = #{return_result}" if o[:assign]
|
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(:assign)
|
|
|
|
o.delete(:return)
|
2009-12-18 22:30:09 -05:00
|
|
|
if @filter
|
2009-12-24 03:12:07 -05:00
|
|
|
body = IfNode.new(@filter, body, nil, :statement => true)
|
2009-12-18 22:30:09 -05:00
|
|
|
suffix = ''
|
|
|
|
end
|
|
|
|
elsif @filter
|
|
|
|
body = IfNode.new(@filter, @body)
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-14 23:03:51 -05:00
|
|
|
|
2009-12-25 17:18:05 -05:00
|
|
|
return_result = "\n#{o[:indent]}#{return_result};"
|
2009-12-31 14:52:14 -05:00
|
|
|
body = body.compile(o.merge(:indent => body_dent))
|
2009-12-31 15:03:32 -05:00
|
|
|
write("#{source_part}#{set_result}for (#{for_part}) {#{pre_cond}#{var_part}\n#{body_dent}#{body}#{suffix}#{post_cond}\n#{o[:indent]}}#{return_result}")
|
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-23 20:57:35 -05:00
|
|
|
custom_return
|
|
|
|
custom_assign
|
2009-12-17 23:22:02 -05:00
|
|
|
|
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
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def line_ending
|
|
|
|
''
|
|
|
|
end
|
2009-12-17 21:10:49 -05:00
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
|
|
|
indent = o[:indent]
|
|
|
|
o[:indent] += TAB
|
2009-12-24 02:01:39 -05:00
|
|
|
error_part = @error ? " (#{@error}) " : ' '
|
|
|
|
catch_part = @recovery && " catch#{error_part}{\n#{@recovery.compile(o)}\n#{indent}}"
|
2009-12-23 20:57:35 -05:00
|
|
|
finally_part = @finally && " finally {\n#{@finally.compile(o.merge(:assign => nil, :return => nil))}\n#{indent}}"
|
2009-12-22 10:11:41 -05:00
|
|
|
write("try {\n#{@try.compile(o)}\n#{indent}}#{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
|
2009-12-17 23:22:02 -05:00
|
|
|
statement
|
|
|
|
|
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
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(o)
|
|
|
|
write("throw #{@expression.compile(o)}")
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
def statement?
|
2009-12-25 23:36:22 -05:00
|
|
|
@expressions.unwrap.statement?
|
2009-12-18 22:30:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def custom_assign?
|
|
|
|
@expressions.custom_assign?
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_return?
|
|
|
|
@expressions.custom_return?
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-17 10:33:57 -05:00
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
2009-12-30 21:44:51 -05:00
|
|
|
raise SyntaxError, "line #{@line}: parentheses can't be wrapped around a statement" if statement?
|
2009-12-22 10:11:41 -05:00
|
|
|
o = super(o)
|
|
|
|
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
|
|
|
|
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?
|
2009-12-18 23:13:59 -05:00
|
|
|
@is_statement ||= !!(@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
|
|
|
|
2009-12-18 00:49:23 -05:00
|
|
|
def custom_return?
|
|
|
|
statement?
|
|
|
|
end
|
|
|
|
|
2009-12-19 00:33:34 -05:00
|
|
|
def custom_assign?
|
|
|
|
statement?
|
|
|
|
end
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
def line_ending
|
|
|
|
statement? ? '' : ';'
|
|
|
|
end
|
|
|
|
|
2009-12-22 10:11:41 -05:00
|
|
|
def compile(o={})
|
|
|
|
o = super(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)
|
|
|
|
indent = o[:indent]
|
2009-12-27 00:55:56 -05:00
|
|
|
cond_o = o.dup
|
|
|
|
cond_o.delete(:assign)
|
|
|
|
cond_o.delete(:return)
|
2009-12-22 10:11:41 -05:00
|
|
|
o[:indent] += TAB
|
2009-12-27 00:55:56 -05:00
|
|
|
if_part = "if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{indent}}"
|
2009-12-17 23:34:52 -05:00
|
|
|
return if_part unless @else_body
|
|
|
|
else_part = chain? ?
|
2009-12-22 12:18:27 -05:00
|
|
|
" else #{@else_body.compile(o.merge(:indent => indent))}" :
|
2009-12-22 10:11:41 -05:00
|
|
|
" else {\n#{Expressions.wrap(@else_body).compile(o)}\n#{indent}}"
|
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
|