mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
first major rework of the nodes -- still need more comments and templatish cleanup, but character tagging is all settled
This commit is contained in:
parent
83944950ac
commit
1395b05d36
1 changed files with 82 additions and 71 deletions
|
@ -5,19 +5,40 @@ module CoffeeScript
|
||||||
# Tabs are two spaces for pretty-printing.
|
# Tabs are two spaces for pretty-printing.
|
||||||
TAB = ' '
|
TAB = ' '
|
||||||
|
|
||||||
def flatten; self; end
|
# 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
|
||||||
|
|
||||||
|
# Default implementations of the common node methods.
|
||||||
|
def unwrap; self; end
|
||||||
def line_ending; ';'; end
|
def line_ending; ';'; end
|
||||||
def statement?; false; end
|
def statement?; false; end
|
||||||
def custom_return?; false; end
|
def custom_return?; false; end
|
||||||
def custom_assign?; false; end
|
def custom_assign?; false; end
|
||||||
|
|
||||||
def compile(indent='', scope=nil, opts={}); end
|
def compile(indent='', scope=nil, opts={}); end
|
||||||
end
|
end
|
||||||
|
|
||||||
# A collection of nodes, each one representing an expression.
|
# A collection of nodes, each one representing an expression.
|
||||||
class Expressions < Node
|
class Expressions < Node
|
||||||
|
statement
|
||||||
attr_reader :expressions
|
attr_reader :expressions
|
||||||
|
|
||||||
|
# Wrap up a node as an Expressions, unless it already is.
|
||||||
def self.wrap(node)
|
def self.wrap(node)
|
||||||
node.is_a?(Expressions) ? node : Expressions.new([node])
|
node.is_a?(Expressions) ? node : Expressions.new([node])
|
||||||
end
|
end
|
||||||
|
@ -26,27 +47,26 @@ module CoffeeScript
|
||||||
@expressions = nodes
|
@expressions = nodes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Tack an expression onto the end of this node.
|
||||||
def <<(node)
|
def <<(node)
|
||||||
@expressions << node
|
@expressions << node
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def flatten
|
# If this Expressions consists of a single node, pull it back out.
|
||||||
|
def unwrap
|
||||||
@expressions.length == 1 ? @expressions.first : self
|
@expressions.length == 1 ? @expressions.first : self
|
||||||
end
|
end
|
||||||
|
|
||||||
def begin_compile
|
# If this is the top-level Expressions, wrap everything in a safety closure.
|
||||||
|
def root_compile
|
||||||
"(function(){\n#{compile(TAB, Scope.new)}\n})();"
|
"(function(){\n#{compile(TAB, Scope.new)}\n})();"
|
||||||
end
|
end
|
||||||
|
|
||||||
def statement?
|
# The extra fancy is to handle pushing down returns recursively to the
|
||||||
true
|
# final lines of inner statements (so as to make expressions out of them).
|
||||||
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={})
|
def compile(indent='', scope=nil, opts={})
|
||||||
return begin_compile unless scope
|
return root_compile unless scope
|
||||||
@expressions.map { |n|
|
@expressions.map { |n|
|
||||||
if opts[:return] && n == @expressions.last
|
if opts[:return] && n == @expressions.last
|
||||||
if n.statement? || n.custom_return?
|
if n.statement? || n.custom_return?
|
||||||
|
@ -79,19 +99,15 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Try to return your expression, or tell it to return itself.
|
||||||
class ReturnNode < Node
|
class ReturnNode < Node
|
||||||
|
statement
|
||||||
|
custom_return
|
||||||
|
|
||||||
def initialize(expression)
|
def initialize(expression)
|
||||||
@expression = expression
|
@expression = expression
|
||||||
end
|
end
|
||||||
|
|
||||||
def statement?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def custom_return?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile(indent, scope, opts={})
|
def compile(indent, scope, opts={})
|
||||||
return @expression.compile(indent, scope, opts.merge(:return => true)) if @expression.custom_return?
|
return @expression.compile(indent, scope, opts.merge(:return => true)) if @expression.custom_return?
|
||||||
compiled = @expression.compile(indent, scope)
|
compiled = @expression.compile(indent, scope)
|
||||||
|
@ -99,13 +115,8 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Node of a method call or local variable access, can take any of these forms:
|
# Node for a function invocation. Takes care of converting super() calls into
|
||||||
#
|
# calls against the prototype's function of the same name.
|
||||||
# method # this form can also be a local variable
|
|
||||||
# method(argument1, argument2)
|
|
||||||
# receiver.method
|
|
||||||
# receiver.method(argument1, argument2)
|
|
||||||
#
|
|
||||||
class CallNode < Node
|
class CallNode < Node
|
||||||
LEADING_DOT = /\A\./
|
LEADING_DOT = /\A\./
|
||||||
|
|
||||||
|
@ -135,6 +146,7 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A value, indexed or dotted into or vanilla.
|
||||||
class ValueNode < Node
|
class ValueNode < Node
|
||||||
attr_reader :last
|
attr_reader :last
|
||||||
|
|
||||||
|
@ -160,6 +172,7 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A dotted accessor into a part of a value.
|
||||||
class AccessorNode
|
class AccessorNode
|
||||||
def initialize(name)
|
def initialize(name)
|
||||||
@name = name
|
@name = name
|
||||||
|
@ -170,6 +183,7 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# An indexed accessor into a part of an array or object.
|
||||||
class IndexNode
|
class IndexNode
|
||||||
def initialize(index)
|
def initialize(index)
|
||||||
@index = index
|
@index = index
|
||||||
|
@ -180,6 +194,9 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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.
|
||||||
class SliceNode
|
class SliceNode
|
||||||
def initialize(from, to)
|
def initialize(from, to)
|
||||||
@from, @to = from, to
|
@from, @to = from, to
|
||||||
|
@ -190,20 +207,15 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Setting the value of a local variable.
|
# Setting the value of a local variable, or the value of an object property.
|
||||||
class AssignNode < Node
|
class AssignNode < Node
|
||||||
|
statement
|
||||||
|
custom_return
|
||||||
|
|
||||||
def initialize(variable, value, context=nil)
|
def initialize(variable, value, context=nil)
|
||||||
@variable, @value, @context = variable, value, context
|
@variable, @value, @context = variable, value, context
|
||||||
end
|
end
|
||||||
|
|
||||||
def custom_return?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def statement?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile(indent, scope, opts={})
|
def compile(indent, scope, opts={})
|
||||||
name = @variable.compile(indent, scope) if @variable.respond_to?(:compile)
|
name = @variable.compile(indent, scope) if @variable.respond_to?(:compile)
|
||||||
last = @variable.respond_to?(:last) ? @variable.last : name
|
last = @variable.respond_to?(:last) ? @variable.last : name
|
||||||
|
@ -220,7 +232,8 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Simple Arithmetic and logical operations
|
# Simple Arithmetic and logical operations. Performs some conversion from
|
||||||
|
# CoffeeScript operations into their JavaScript equivalents.
|
||||||
class OpNode < Node
|
class OpNode < Node
|
||||||
CONVERSIONS = {
|
CONVERSIONS = {
|
||||||
"==" => "===",
|
"==" => "===",
|
||||||
|
@ -260,7 +273,7 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Method definition.
|
# A function definition. The only node that creates a new Scope.
|
||||||
class CodeNode < Node
|
class CodeNode < Node
|
||||||
def initialize(params, body)
|
def initialize(params, body)
|
||||||
@params = params
|
@params = params
|
||||||
|
@ -276,6 +289,7 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# An object literal.
|
||||||
class ObjectNode < Node
|
class ObjectNode < Node
|
||||||
def initialize(properties = [])
|
def initialize(properties = [])
|
||||||
@properties = properties
|
@properties = properties
|
||||||
|
@ -287,6 +301,7 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# An array literal.
|
||||||
class ArrayNode < Node
|
class ArrayNode < Node
|
||||||
def initialize(objects=[])
|
def initialize(objects=[])
|
||||||
@objects = objects
|
@objects = objects
|
||||||
|
@ -298,7 +313,11 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A while loop, the only sort of low-level loop exposed by CoffeeScript. From
|
||||||
|
# it, all other loops can be manufactured.
|
||||||
class WhileNode < Node
|
class WhileNode < Node
|
||||||
|
statement
|
||||||
|
|
||||||
def initialize(condition, body)
|
def initialize(condition, body)
|
||||||
@condition, @body = condition, body
|
@condition, @body = condition, body
|
||||||
end
|
end
|
||||||
|
@ -307,16 +326,19 @@ module CoffeeScript
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
||||||
def statement?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile(indent, scope, opts={})
|
def compile(indent, scope, opts={})
|
||||||
"while (#{@condition.compile(indent, scope, :no_paren => true)}) {\n#{@body.compile(indent + TAB, scope)}\n#{indent}}"
|
"while (#{@condition.compile(indent, scope, :no_paren => true)}) {\n#{@body.compile(indent + TAB, scope)}\n#{indent}}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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.
|
||||||
class ForNode < Node
|
class ForNode < Node
|
||||||
|
statement
|
||||||
|
custom_return
|
||||||
|
custom_assign
|
||||||
|
|
||||||
def initialize(body, source, name, index=nil)
|
def initialize(body, source, name, index=nil)
|
||||||
@body, @source, @name, @index = body, source, name, index
|
@body, @source, @name, @index = body, source, name, index
|
||||||
|
@ -326,18 +348,6 @@ module CoffeeScript
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
||||||
def custom_return?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def custom_assign?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def statement?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile(indent, scope, opts={})
|
def compile(indent, scope, opts={})
|
||||||
svar = scope.free_variable
|
svar = scope.free_variable
|
||||||
ivar = scope.free_variable
|
ivar = scope.free_variable
|
||||||
|
@ -367,7 +377,10 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A try/catch/finally block.
|
||||||
class TryNode < Node
|
class TryNode < Node
|
||||||
|
statement
|
||||||
|
|
||||||
def initialize(try, error, recovery, finally=nil)
|
def initialize(try, error, recovery, finally=nil)
|
||||||
@try, @error, @recovery, @finally = try, error, recovery, finally
|
@try, @error, @recovery, @finally = try, error, recovery, finally
|
||||||
end
|
end
|
||||||
|
@ -376,10 +389,6 @@ module CoffeeScript
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
||||||
def statement?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile(indent, scope, opts={})
|
def compile(indent, scope, opts={})
|
||||||
catch_part = @recovery && " catch (#{@error}) {\n#{@recovery.compile(indent + TAB, scope, opts)}\n#{indent}}"
|
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}}"
|
finally_part = @finally && " finally {\n#{@finally.compile(indent + TAB, scope, opts)}\n#{indent}}"
|
||||||
|
@ -387,44 +396,46 @@ module CoffeeScript
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Throw an exception.
|
||||||
class ThrowNode < Node
|
class ThrowNode < Node
|
||||||
|
statement
|
||||||
|
|
||||||
def initialize(expression)
|
def initialize(expression)
|
||||||
@expression = expression
|
@expression = expression
|
||||||
end
|
end
|
||||||
|
|
||||||
def statement?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile(indent, scope, opts={})
|
def compile(indent, scope, opts={})
|
||||||
"throw #{@expression.compile(indent, scope)}"
|
"throw #{@expression.compile(indent, scope)}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# An extra set of parenthesis, supplied by the script source.
|
||||||
class ParentheticalNode < Node
|
class ParentheticalNode < Node
|
||||||
def initialize(expressions)
|
def initialize(expressions)
|
||||||
@expressions = expressions
|
@expressions = expressions
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile(indent, scope, opts={})
|
def compile(indent, scope, opts={})
|
||||||
compiled = @expressions.flatten.compile(indent, scope)
|
compiled = @expressions.unwrap.compile(indent, scope)
|
||||||
compiled = compiled[0...-1] if compiled[-1..-1] == ';'
|
compiled = compiled[0...-1] if compiled[-1..-1] == ';'
|
||||||
opts[:no_paren] ? compiled : "(#{compiled})"
|
opts[:no_paren] ? compiled : "(#{compiled})"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# "if-else" control structure. Look at this node if you want to implement other control
|
# If/else statements. Switch/cases get compiled into these. Acts as an
|
||||||
# structures like while, for, loop, etc.
|
# 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.
|
||||||
class IfNode < Node
|
class IfNode < Node
|
||||||
def initialize(condition, body, else_body=nil, tag=nil)
|
def initialize(condition, body, else_body=nil, tag=nil)
|
||||||
@condition = condition
|
@condition = condition
|
||||||
@body = body && body.flatten
|
@body = body && body.unwrap
|
||||||
@else_body = else_body && else_body.flatten
|
@else_body = else_body && else_body.unwrap
|
||||||
@condition = OpNode.new("!", @condition) if tag == :invert
|
@condition = OpNode.new("!", @condition) if tag == :invert
|
||||||
end
|
end
|
||||||
|
|
||||||
def <<(else_body)
|
def <<(else_body)
|
||||||
eb = else_body.flatten
|
eb = else_body.unwrap
|
||||||
@else_body ? @else_body << eb : @else_body = eb
|
@else_body ? @else_body << eb : @else_body = eb
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue