1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/coffee_script/nodes.rb

474 lines
13 KiB
Ruby
Raw Normal View History

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.
class Node
# Tabs are two spaces for pretty-printing.
TAB = ' '
def flatten; self; end
def line_ending; ';'; end
def statement?; false; end
def custom_return?; false; end
def custom_assign?; false; end
2009-12-13 17:07:16 -05:00
def compile(indent='', scope=nil, opts={}); end
end
2009-12-17 22:58:40 -05:00
# A collection of nodes, each one representing an expression.
class Expressions < Node
attr_reader :expressions
2009-12-13 17:07:16 -05:00
def self.wrap(node)
2009-12-17 22:58:40 -05:00
node.is_a?(Expressions) ? node : Expressions.new([node])
end
2009-12-13 17:07:16 -05:00
def initialize(nodes)
2009-12-17 22:58:40 -05:00
@expressions = nodes
end
def <<(node)
2009-12-17 22:58:40 -05:00
@expressions << node
self
end
def flatten
2009-12-17 22:58:40 -05:00
@expressions.length == 1 ? @expressions.first : self
end
def begin_compile
"(function(){\n#{compile(TAB, Scope.new)}\n})();"
end
def statement?
true
end
# Fancy to handle pushing down returns recursively to the final lines of
# inner statements (to make expressions out of them).
def compile(indent='', scope=nil, opts={})
return begin_compile unless scope
2009-12-17 22:58:40 -05:00
@expressions.map { |n|
if opts[:return] && n == @expressions.last
if n.statement? || n.custom_return?
"#{indent}#{n.compile(indent, scope, opts)}#{n.line_ending}"
else
"#{indent}return #{n.compile(indent, scope, opts)}#{n.line_ending}"
end
else
"#{indent}#{n.compile(indent, scope)}#{n.line_ending}"
end
}.join("\n")
end
2009-12-13 17:07:16 -05:00
end
# Literals are static values that have a Ruby representation, eg.: a string, a number,
# true, false, nil, etc.
class LiteralNode < Node
STATEMENTS = ['break', 'continue']
def initialize(value)
@value = value
end
2009-12-13 17:07:16 -05:00
def statement?
STATEMENTS.include?(@value.to_s)
end
def compile(indent, scope, opts={})
@value.to_s
end
2009-12-13 17:07:16 -05:00
end
class ReturnNode < Node
def initialize(expression)
@expression = expression
end
2009-12-13 18:37:29 -05:00
def statement?
true
end
def custom_return?
true
end
def compile(indent, scope, opts={})
return @expression.compile(indent, scope, opts.merge(:return => true)) if @expression.custom_return?
compiled = @expression.compile(indent, scope)
@expression.statement? ? "#{compiled}\n#{indent}return null" : "return #{compiled}"
end
2009-12-13 23:59:12 -05:00
end
# Node of a method call or local variable access, can take any of these forms:
#
# method # this form can also be a local variable
# method(argument1, argument2)
# receiver.method
# receiver.method(argument1, argument2)
#
class CallNode < Node
LEADING_DOT = /\A\./
2009-12-13 17:07:16 -05:00
def initialize(variable, arguments=[])
@variable, @arguments = variable, arguments
end
2009-12-17 09:07:42 -05:00
def new_instance
@new = true
self
end
2009-12-17 09:07:42 -05:00
def super?
@variable == :super
end
2009-12-13 17:07:16 -05:00
def compile(indent, scope, opts={})
args = @arguments.map{|a| a.compile(indent, scope, :no_paren => true) }.join(', ')
return compile_super(args, indent, scope, opts) if super?
prefix = @new ? "new " : ''
"#{prefix}#{@variable.compile(indent, scope)}(#{args})"
end
2009-12-17 09:07:42 -05:00
def compile_super(args, indent, scope, opts)
methname = opts[:last_assign].sub(LEADING_DOT, '')
"this.constructor.prototype.#{methname}.call(this, #{args})"
end
2009-12-13 17:07:16 -05:00
end
class ValueNode < Node
attr_reader :last
2009-12-13 17:07:16 -05:00
def initialize(name, properties=[])
@name, @properties = name, properties
end
2009-12-13 18:37:29 -05:00
def <<(other)
@properties << other
self
2009-12-17 09:07:42 -05:00
end
2009-12-13 20:29:44 -05:00
def properties?
return !@properties.empty?
end
2009-12-13 20:29:44 -05:00
def compile(indent, scope, opts={})
parts = [@name, @properties].flatten.map do |v|
v.respond_to?(:compile) ? v.compile(indent, scope) : v.to_s
end
@last = parts.last
parts.join('')
end
2009-12-13 20:29:44 -05:00
end
class AccessorNode
def initialize(name)
@name = name
end
2009-12-13 20:29:44 -05:00
def compile(indent, scope, opts={})
".#{@name}"
end
2009-12-13 17:07:16 -05:00
end
class IndexNode
def initialize(index)
@index = index
end
2009-12-16 21:43:26 -05:00
def compile(indent, scope, opts={})
"[#{@index.compile(indent, scope)}]"
end
2009-12-16 21:43:26 -05:00
end
class SliceNode
def initialize(from, to)
@from, @to = from, to
end
2009-12-13 17:07:16 -05:00
def compile(indent, scope, opts={})
".slice(#{@from.compile(indent, scope, opts)}, #{@to.compile(indent, scope, opts)} + 1)"
end
end
# Setting the value of a local variable.
class AssignNode < Node
def initialize(variable, value, context=nil)
@variable, @value, @context = variable, value, context
end
def custom_return?
true
end
2009-12-13 17:07:16 -05:00
def statement?
true
end
2009-12-13 17:07:16 -05:00
def compile(indent, scope, opts={})
name = @variable.compile(indent, scope) if @variable.respond_to?(:compile)
last = @variable.respond_to?(:last) ? @variable.last : name
opts = opts.merge({:assign => name, :last_assign => last})
value = @value.compile(indent + TAB, scope, opts)
return "#{@variable}: #{value}" if @context == :object
return "#{name} = #{value}" if @variable.properties?
defined = scope.find(name)
postfix = !defined && opts[:return] ? ";\n#{indent}return #{name}" : ''
def_part = defined ? "" : "var #{name};\n#{indent}"
return def_part + @value.compile(indent, scope, opts) if @value.custom_assign?
def_part = defined ? name : "var #{name}"
"#{def_part} = #{@value.compile(indent, scope, opts)}#{postfix}"
end
2009-12-13 17:07:16 -05:00
end
2009-12-13 20:29:44 -05:00
# Simple Arithmetic and logical operations
class OpNode < Node
CONVERSIONS = {
"==" => "===",
"!=" => "!==",
'and' => '&&',
'or' => '||',
'is' => '===',
"aint" => "!==",
'not' => '!',
}
CONDITIONALS = ['||=', '&&=']
def initialize(operator, first, second=nil)
@first, @second = first, second
@operator = CONVERSIONS[operator] || operator
end
def unary?
@second.nil?
end
2009-12-13 17:07:16 -05:00
def compile(indent, scope, opts={})
return compile_conditional(indent, scope) if CONDITIONALS.include?(@operator)
return compile_unary(indent, scope) if unary?
"#{@first.compile(indent, scope)} #{@operator} #{@second.compile(indent, scope)}"
end
2009-12-13 17:07:16 -05:00
def compile_conditional(indent, scope)
first, second = @first.compile(indent, scope), @second.compile(indent, scope)
sym = @operator[0..1]
"#{first} = #{first} #{sym} #{second}"
end
2009-12-13 17:07:16 -05:00
def compile_unary(indent, scope)
space = @operator == 'delete' ? ' ' : ''
"#{@operator}#{space}#{@first.compile(indent, scope)}"
end
2009-12-13 17:07:16 -05:00
end
# Method definition.
class CodeNode < Node
def initialize(params, body)
@params = params
@body = body
end
2009-12-13 17:07:16 -05:00
def compile(indent, scope, opts={})
scope = Scope.new(scope)
@params.each {|id| scope.find(id) }
opts = opts.merge(:return => true)
code = @body.compile(indent + TAB, scope, opts)
"function(#{@params.join(', ')}) {\n#{code}\n#{indent}}"
end
2009-12-13 17:07:16 -05:00
end
class ObjectNode < Node
def initialize(properties = [])
@properties = properties
end
2009-12-13 17:07:16 -05:00
def compile(indent, scope, opts={})
props = @properties.map {|p| indent + TAB + p.compile(indent, scope) }.join(",\n")
"{\n#{props}\n#{indent}}"
end
end
class ArrayNode < Node
def initialize(objects=[])
@objects = objects
end
def compile(indent, scope, opts={})
objects = @objects.map {|o| o.compile(indent, scope) }.join(', ')
"[#{objects}]"
end
end
class WhileNode < Node
def initialize(condition, body)
@condition, @body = condition, body
end
def line_ending
''
end
def statement?
true
end
def compile(indent, scope, opts={})
"while (#{@condition.compile(indent, scope, :no_paren => true)}) {\n#{@body.compile(indent + TAB, scope)}\n#{indent}}"
end
end
class ForNode < Node
def initialize(body, source, name, index=nil)
@body, @source, @name, @index = body, source, name, index
end
def line_ending
''
end
2009-12-17 10:33:57 -05:00
def custom_return?
true
end
2009-12-13 23:25:00 -05:00
def custom_assign?
true
end
2009-12-14 10:00:31 -05:00
def statement?
true
end
2009-12-14 10:00:31 -05:00
def compile(indent, scope, opts={})
svar = scope.free_variable
ivar = scope.free_variable
lvar = scope.free_variable
name_part = scope.find(@name) ? @name : "var #{@name}"
index_name = @index ? (scope.find(@index) ? @index : "var #{@index}") : nil
source_part = "var #{svar} = #{@source.compile(indent, scope)};"
for_part = "var #{ivar}=0, #{lvar}=#{svar}.length; #{ivar}<#{lvar}; #{ivar}++"
var_part = "\n#{indent + TAB}#{name_part} = #{svar}[#{ivar}];\n"
index_part = @index ? "#{indent + TAB}#{index_name} = #{ivar};\n" : ''
set_result = ''
save_result = ''
return_result = ''
if opts[:return] || opts[:assign]
rvar = scope.free_variable
set_result = "var #{rvar} = [];\n#{indent}"
save_result = "#{rvar}[#{ivar}] = "
return_result = rvar
return_result = "#{opts[:assign]} = #{return_result}" if opts[:assign]
return_result = "return #{return_result}" if opts[:return]
return_result = "\n#{indent}#{return_result}"
end
body = @body.compile(indent + TAB, scope)
"#{source_part}\n#{indent}#{set_result}for (#{for_part}) {#{var_part}#{index_part}#{indent + TAB}#{save_result}#{body};\n#{indent}}#{return_result}"
end
2009-12-14 10:00:31 -05:00
end
class TryNode < Node
def initialize(try, error, recovery, finally=nil)
@try, @error, @recovery, @finally = try, error, recovery, finally
end
2009-12-14 10:00:31 -05:00
def line_ending
''
end
def statement?
true
end
2009-12-14 10:00:31 -05:00
def compile(indent, scope, opts={})
catch_part = @recovery && " catch (#{@error}) {\n#{@recovery.compile(indent + TAB, scope, opts)}\n#{indent}}"
finally_part = @finally && " finally {\n#{@finally.compile(indent + TAB, scope, opts)}\n#{indent}}"
"try {\n#{@try.compile(indent + TAB, scope, opts)}\n#{indent}}#{catch_part}#{finally_part}"
end
2009-12-13 23:25:00 -05:00
end
class ThrowNode < Node
def initialize(expression)
@expression = expression
end
2009-12-17 10:33:57 -05:00
def statement?
true
end
2009-12-17 10:33:57 -05:00
def compile(indent, scope, opts={})
"throw #{@expression.compile(indent, scope)}"
end
2009-12-17 10:33:57 -05:00
end
class ParentheticalNode < Node
def initialize(expressions)
@expressions = expressions
end
2009-12-17 10:33:57 -05:00
def compile(indent, scope, opts={})
compiled = @expressions.flatten.compile(indent, scope)
compiled = compiled[0...-1] if compiled[-1..-1] == ';'
opts[:no_paren] ? compiled : "(#{compiled})"
end
2009-12-17 10:33:57 -05:00
end
# "if-else" control structure. Look at this node if you want to implement other control
# structures like while, for, loop, etc.
class IfNode < Node
def initialize(condition, body, else_body=nil, tag=nil)
@condition = condition
@body = body && body.flatten
@else_body = else_body && else_body.flatten
@condition = OpNode.new("!", @condition) if tag == :invert
end
2009-12-17 10:33:57 -05:00
def <<(else_body)
eb = else_body.flatten
@else_body ? @else_body << eb : @else_body = eb
self
end
2009-12-17 10:33:57 -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
# Rewrite a chain of IfNodes to add a default case as the final else.
def add_else(expressions)
chain? ? @else_body.add_else(expressions) : @else_body = expressions
self
end
def chain?
@chain ||= @else_body && @else_body.is_a?(IfNode)
end
def statement?
@is_statement ||= (@body.statement? || (@else_body && @else_body.statement?))
end
2009-12-17 10:33:57 -05:00
def line_ending
statement? ? '' : ';'
end
def compile(indent, scope, opts={})
statement? ? compile_statement(indent, scope, opts) : compile_ternary(indent, scope)
end
def compile_statement(indent, scope, opts)
2009-12-17 22:58:40 -05:00
if_part = "if (#{@condition.compile(indent, scope, :no_paren => true)}) {\n#{Expressions.wrap(@body).compile(indent + TAB, scope, opts)}\n#{indent}}"
else_part = @else_body ? " else {\n#{Expressions.wrap(@else_body).compile(indent + TAB, scope, opts)}\n#{indent}}" : ''
if_part + else_part
end
def compile_ternary(indent, scope)
if_part = "#{@condition.compile(indent, scope)} ? #{@body.compile(indent, scope)}"
else_part = @else_body ? "#{@else_body.compile(indent, scope)}" : 'null'
"#{if_part} : #{else_part}"
end
2009-12-17 10:33:57 -05:00
end
end