first rough rough rough draft of kamatsu's closure suggestion -- test.coffee runs, but probably nothing else

This commit is contained in:
Jeremy Ashkenas
2010-01-03 13:59:17 -05:00
parent de4eddcad4
commit 78c4957ba8
3 changed files with 62 additions and 141 deletions

View File

@@ -43,14 +43,14 @@ rule
# All parsing will end in this rule, being the trunk of the AST. # All parsing will end in this rule, being the trunk of the AST.
Root: Root:
/* nothing */ { result = Expressions.new([]) } /* nothing */ { result = Expressions.new }
| Terminator { result = Expressions.new([]) } | Terminator { result = Expressions.new }
| Expressions { result = val[0] } | Expressions { result = val[0] }
; ;
# Any list of expressions or method body, seperated by line breaks or semis. # Any list of expressions or method body, seperated by line breaks or semis.
Expressions: Expressions:
Expression { result = Expressions.new(val) } Expression { result = Expressions.wrap(val) }
| Expressions Terminator Expression { result = val[0] << val[2] } | Expressions Terminator Expression { result = val[0] << val[2] }
| Expressions Terminator { result = val[0] } | Expressions Terminator { result = val[0] }
; ;
@@ -78,7 +78,7 @@ rule
Block: Block:
INDENT Expressions OUTDENT { result = val[1] } INDENT Expressions OUTDENT { result = val[1] }
| INDENT OUTDENT { result = Expressions.new([]) } | INDENT OUTDENT { result = Expressions.new }
; ;
# All tokens that can terminate an expression. # All tokens that can terminate an expression.
@@ -405,8 +405,8 @@ rule
# The full complement of if blocks, including postfix one-liner ifs and unlesses. # The full complement of if blocks, including postfix one-liner ifs and unlesses.
If: If:
IfBlock IfEnd { result = val[0].add_else(val[1]) } IfBlock IfEnd { result = val[0].add_else(val[1]) }
| Expression IF Expression { result = IfNode.new(val[2], Expressions.new([val[0]]), nil, {:statement => true}) } | Expression IF Expression { result = IfNode.new(val[2], Expressions.wrap(val[0]), nil, {:statement => true}) }
| Expression UNLESS Expression { result = IfNode.new(val[2], Expressions.new([val[0]]), nil, {:statement => true, :invert => true}) } | Expression UNLESS Expression { result = IfNode.new(val[2], Expressions.wrap(val[0]), nil, {:statement => true, :invert => true}) }
; ;
end end

View File

@@ -11,17 +11,11 @@ module CoffeeScript
class_eval "def statement?; true; end" class_eval "def statement?; true; end"
end end
# Tag this node as having a custom return, meaning that instead of returning # Tag this node as a statement that cannot be transformed into an expression.
# it from the outside, you ask it to return itself, and it obliges. # (break, continue, etc.) It doesn't make sense to try to transform it.
def self.custom_return def self.statement_only
class_eval "def custom_return?; true; end" statement
end class_eval "def statement_only?; true; 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 end
def write(code) def write(code)
@@ -33,29 +27,31 @@ module CoffeeScript
@options = o.dup @options = o.dup
end end
def compile_closure(o={})
"(#{CodeNode.new([], Expressions.wrap(self)).compile(o, o[:scope])})()"
end
# Default implementations of the common node methods. # Default implementations of the common node methods.
def unwrap; self; end def unwrap; self; end
def line_ending; ';'; end def statement?; false; end
def statement?; false; end def statement_only?; false; end
def custom_return?; false; end
def custom_assign?; false; 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 statement
custom_assign
attr_reader :expressions attr_reader :expressions
STRIP_TRAILING_WHITESPACE = /\s+$/ STRIP_TRAILING_WHITESPACE = /\s+$/
# Wrap up a node as an Expressions, unless it already is. # Wrap up a node as an Expressions, unless it already is.
def self.wrap(node) def self.wrap(*nodes)
node.is_a?(Expressions) ? node : Expressions.new([node]) return nodes[0] if nodes.length == 1 && nodes[0].is_a?(Expressions)
Expressions.new(*nodes)
end end
def initialize(nodes) def initialize(*nodes)
@expressions = nodes @expressions = nodes.flatten
end end
# Tack an expression onto the end of this node. # Tack an expression onto the end of this node.
@@ -83,43 +79,36 @@ module CoffeeScript
# If this is the top-level Expressions, wrap everything in a safety closure. # If this is the top-level Expressions, wrap everything in a safety closure.
def root_compile(o={}) def root_compile(o={})
indent = o[:no_wrap] ? '' : TAB indent = o[:no_wrap] ? '' : TAB
code = compile(o.merge(:indent => indent, :scope => Scope.new), o[:no_wrap] ? nil : :code) code = compile(o.merge(:indent => indent, :scope => Scope.new(nil, self)), o[:no_wrap] ? nil : :code)
code.gsub!(STRIP_TRAILING_WHITESPACE, '') code.gsub!(STRIP_TRAILING_WHITESPACE, '')
o[:no_wrap] ? code : "(function(){\n#{code}\n})();" o[:no_wrap] ? code : "(function(){\n#{code}\n})();"
end end
# The extra fancy is to handle pushing down returns and assignments # The extra fancy is to handle pushing down returns to the final lines of
# recursively to the final lines of inner statements. # inner statements. Variables first defined within the Expressions body
# Variables first defined within the Expressions body have their # have their declarations pushed up top of the closest scope.
# declarations pushed up to the top scope.
def compile(options={}, parent=nil) def compile(options={}, parent=nil)
return root_compile(options) unless options[:scope] return root_compile(options) unless options[:scope]
compiled = @expressions.map do |node| compiled = @expressions.map do |node|
o = super(options) o = super(options)
if last?(node) && (o[:return] || o[:assign]) returns = o.delete(:return)
if o[:return] code = node.compile(o)
if node.statement? || node.custom_return? if last?(node) && returns && !node.statement_only?
"#{node.compile(o)}#{node.line_ending}" if node.statement?
else node.compile(o.merge(:return => true))
o.delete(:return) else
"#{o[:indent]}return #{node.compile(o)}#{node.line_ending}" "#{o[:indent]}return #{node.compile(o)};"
end
elsif o[:assign]
if node.statement? || node.custom_assign?
"#{node.compile(o)}#{node.line_ending}"
else
"#{o[:indent]}#{AssignNode.new(o[:assign], node).compile(o)};"
end
end end
else else
o.delete(:return) and o.delete(:assign) ending = node.statement_only? ? '' : ';'
indent = node.unwrap.statement? ? '' : o[:indent] indent = node.statement? ? '' : o[:indent]
"#{indent}#{node.compile(o)}#{node.line_ending}" "#{indent}#{node.compile(o)}#{ending}"
end end
end end
scope = options[:scope] scope = options[:scope]
declarations = scope.any_declared? && parent == :code ? "#{options[:indent]}var #{scope.declared_variables.join(', ')};\n" : '' decls = ''
code = declarations + compiled.join("\n") decls = "#{options[:indent]}var #{scope.declared_variables.join(', ')};\n" if parent == :code && scope.declarations?(self)
code = decls + compiled.join("\n")
write(code) write(code)
end end
end end
@@ -138,10 +127,7 @@ module CoffeeScript
def statement? def statement?
STATEMENTS.include?(@value.to_s) STATEMENTS.include?(@value.to_s)
end end
alias_method :statement_only?, :statement?
def line_ending
@value.to_s[-1..-1] == ';' ? '' : ';'
end
def compile(o={}) def compile(o={})
o = super(o) o = super(o)
@@ -152,8 +138,7 @@ module CoffeeScript
# Try to return your expression, or tell it to return itself. # Try to return your expression, or tell it to return itself.
class ReturnNode < Node class ReturnNode < Node
statement statement_only
custom_return
attr_reader :expression attr_reader :expression
@@ -161,10 +146,6 @@ module CoffeeScript
@expression = expression @expression = expression
end end
def line_ending
@expression.custom_return? ? '' : ';'
end
def compile(o={}) def compile(o={})
o = super(o) o = super(o)
return write(@expression.compile(o.merge(:return => true))) if @expression.custom_return? return write(@expression.compile(o.merge(:return => true))) if @expression.custom_return?
@@ -176,16 +157,12 @@ module CoffeeScript
# Pass through CoffeeScript comments into JavaScript comments at the # Pass through CoffeeScript comments into JavaScript comments at the
# same position. # same position.
class CommentNode < Node class CommentNode < Node
statement statement_only
def initialize(lines) def initialize(lines)
@lines = lines.value @lines = lines.value
end end
def line_ending
''
end
def compile(o={}) def compile(o={})
delimiter = "\n#{o[:indent]}//" delimiter = "\n#{o[:indent]}//"
comment = "#{delimiter}#{@lines.join(delimiter)}" comment = "#{delimiter}#{@lines.join(delimiter)}"
@@ -253,6 +230,7 @@ module CoffeeScript
# Node to extend an object's prototype with an ancestor object. # Node to extend an object's prototype with an ancestor object.
# After goog.inherits from the Closure Library. # After goog.inherits from the Closure Library.
class ExtendsNode < Node class ExtendsNode < Node
statement
attr_reader :sub_object, :super_object attr_reader :sub_object, :super_object
def initialize(sub_object, super_object) def initialize(sub_object, super_object)
@@ -289,14 +267,6 @@ module CoffeeScript
@literal.is_a?(Node) && @literal.statement? && !properties? @literal.is_a?(Node) && @literal.statement? && !properties?
end 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
def compile(o={}) def compile(o={})
o = super(o) o = super(o)
only = o.delete(:only_first) only = o.delete(:only_first)
@@ -401,35 +371,25 @@ module CoffeeScript
PROTO_ASSIGN = /\A(\S+)\.prototype/ PROTO_ASSIGN = /\A(\S+)\.prototype/
LEADING_DOT = /\A\./ LEADING_DOT = /\A\./
custom_return
attr_reader :variable, :value, :context attr_reader :variable, :value, :context
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 line_ending
@value.custom_assign? ? '' : ';'
end
def statement?
@value.custom_assign?
end
def compile(o={}) def compile(o={})
o = super(o) o = super(o)
return compile_splice(o) if @variable.properties.last.is_a?(SliceNode) return compile_splice(o) if @variable.properties.last.is_a?(SliceNode)
name = @variable.compile(o) name = @variable.compile(o)
last = @variable.last.to_s.sub(LEADING_DOT, '') last = @variable.last.to_s.sub(LEADING_DOT, '')
proto = name[PROTO_ASSIGN, 1] proto = name[PROTO_ASSIGN, 1]
o = o.merge(:assign => @variable, :last_assign => last, :proto_assign => proto) o = o.merge(:last_assign => last, :proto_assign => proto)
o[:immediate_assign] = last if @value.is_a?(CodeNode) && last.match(Lexer::IDENTIFIER) o[:immediate_assign] = last if @value.is_a?(CodeNode) && last.match(Lexer::IDENTIFIER)
return write("#{name}: #{@value.compile(o)}") if @context == :object return write("#{name}: #{@value.compile(o)}") if @context == :object
o[:scope].find(name) unless @variable.properties? o[:scope].find(name) unless @variable.properties?
return write(@value.compile(o)) if @value.custom_assign? code = @value.statement? ? @value.compile_closure(o) : @value.compile(o)
val = "#{name} = #{@value.compile(o)}" val = "#{name} = #{code}"
write(o[:return] && !@value.custom_return? ? "#{o[:indent]}return (#{val})" : val) write(o[:return] ? "#{o[:indent]}return (#{val})" : val)
end end
def compile_splice(o) def compile_splice(o)
@@ -498,13 +458,12 @@ module CoffeeScript
@body = body @body = body
end end
def compile(o={}) def compile(o={}, shared_scope=nil)
o = super(o) o = super(o)
o[:scope] = Scope.new(o[:scope]) o[:scope] = shared_scope || Scope.new(o[:scope], @body)
o[:return] = true o[:return] = true
indent = o[:indent] indent = o[:indent]
o[:indent] += TAB o[:indent] += TAB
o.delete(:assign)
o.delete(:no_wrap) o.delete(:no_wrap)
name = o.delete(:immediate_assign) name = o.delete(:immediate_assign)
if @params.last.is_a?(ParamSplatNode) if @params.last.is_a?(ParamSplatNode)
@@ -604,10 +563,6 @@ module CoffeeScript
@condition, @body = condition, body @condition, @body = condition, body
end end
def line_ending
''
end
def compile(o={}) def compile(o={})
o = super(o) o = super(o)
o.delete(:return) o.delete(:return)
@@ -623,8 +578,6 @@ module CoffeeScript
# the current index of the loop as a second parameter. # the current index of the loop as a second parameter.
class ForNode < Node class ForNode < Node
statement statement
custom_return
custom_assign
attr_reader :body, :source, :name, :index, :filter, :step attr_reader :body, :source, :name, :index, :filter, :step
@@ -635,10 +588,6 @@ module CoffeeScript
@step = source[:step] @step = source[:step]
end end
def line_ending
''
end
def compile(o={}) def compile(o={})
o = super(o) o = super(o)
range = @source.is_a?(RangeNode) range = @source.is_a?(RangeNode)
@@ -668,14 +617,12 @@ module CoffeeScript
set_result = "#{rvar} = [];\n#{o[:indent]}" set_result = "#{rvar} = [];\n#{o[:indent]}"
return_result = rvar return_result = rvar
temp_var = ValueNode.new(LiteralNode.new(tvar)) temp_var = ValueNode.new(LiteralNode.new(tvar))
body = Expressions.new([ body = Expressions.wrap(
AssignNode.new(temp_var, @body), AssignNode.new(temp_var, @body),
CallNode.new(ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [temp_var]) CallNode.new(ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [temp_var])
]) )
if o[:return] || o[:assign] if o[:return]
return_result = "#{o[:assign].compile(o)} = #{return_result}" if o[:assign]
return_result = "return #{return_result}" if o[:return] return_result = "return #{return_result}" if o[:return]
o.delete(:assign)
o.delete(:return) o.delete(:return)
body = IfNode.new(@filter, body, nil, :statement => true) if @filter body = IfNode.new(@filter, body, nil, :statement => true) if @filter
elsif @filter elsif @filter
@@ -691,8 +638,6 @@ module CoffeeScript
# A try/catch/finally block. # A try/catch/finally block.
class TryNode < Node class TryNode < Node
statement statement
custom_return
custom_assign
attr_reader :try, :error, :recovery, :finally attr_reader :try, :error, :recovery, :finally
@@ -700,24 +645,20 @@ module CoffeeScript
@try, @error, @recovery, @finally = try, error, recovery, finally @try, @error, @recovery, @finally = try, error, recovery, finally
end end
def line_ending
''
end
def compile(o={}) def compile(o={})
o = super(o) o = super(o)
indent = o[:indent] indent = o[:indent]
o[:indent] += TAB o[:indent] += TAB
error_part = @error ? " (#{@error}) " : ' ' error_part = @error ? " (#{@error}) " : ' '
catch_part = @recovery && " catch#{error_part}{\n#{@recovery.compile(o)}\n#{indent}}" catch_part = @recovery && " catch#{error_part}{\n#{@recovery.compile(o)}\n#{indent}}"
finally_part = @finally && " finally {\n#{@finally.compile(o.merge(:assign => nil, :return => nil))}\n#{indent}}" finally_part = @finally && " finally {\n#{@finally.compile(o.merge(:return => nil))}\n#{indent}}"
write("#{indent}try {\n#{@try.compile(o)}\n#{indent}}#{catch_part}#{finally_part}") write("#{indent}try {\n#{@try.compile(o)}\n#{indent}}#{catch_part}#{finally_part}")
end end
end end
# Throw an exception. # Throw an exception.
class ThrowNode < Node class ThrowNode < Node
statement statement_only
attr_reader :expression attr_reader :expression
@@ -760,14 +701,6 @@ module CoffeeScript
@expressions.unwrap.statement? @expressions.unwrap.statement?
end end
def custom_assign?
@expressions.custom_assign?
end
def custom_return?
@expressions.custom_return?
end
def compile(o={}) def compile(o={})
raise SyntaxError, "line #{@line}: parentheses can't be wrapped around a statement" if statement? raise SyntaxError, "line #{@line}: parentheses can't be wrapped around a statement" if statement?
o = super(o) o = super(o)
@@ -827,18 +760,6 @@ module CoffeeScript
@is_statement ||= !!(@tags[:statement] || @body.statement? || (@else_body && @else_body.statement?)) @is_statement ||= !!(@tags[:statement] || @body.statement? || (@else_body && @else_body.statement?))
end end
def custom_return?
statement?
end
def custom_assign?
statement?
end
def line_ending
statement? ? '' : ';'
end
def compile(o={}) def compile(o={})
o = super(o) o = super(o)
write(statement? ? compile_statement(o) : compile_ternary(o)) write(statement? ? compile_statement(o) : compile_ternary(o))
@@ -850,7 +771,6 @@ module CoffeeScript
indent = o[:indent] indent = o[:indent]
child = o.delete(:chain_child) child = o.delete(:chain_child)
cond_o = o.dup cond_o = o.dup
cond_o.delete(:assign)
cond_o.delete(:return) cond_o.delete(:return)
o[:indent] += TAB o[:indent] += TAB
if_dent = child ? '' : indent if_dent = child ? '' : indent

View File

@@ -5,11 +5,12 @@ module CoffeeScript
# whether a variable has been seen before or if it needs to be declared. # whether a variable has been seen before or if it needs to be declared.
class Scope class Scope
attr_reader :parent, :variables, :temp_variable attr_reader :parent, :expressions, :variables, :temp_variable
# Initialize a scope with its parent, for lookups up the chain. # Initialize a scope with its parent, for lookups up the chain,
def initialize(parent=nil) # as well as the Expressions body where it should declare its variables.
@parent = parent def initialize(parent, expressions)
@parent, @expressions = parent, expressions
@variables = {} @variables = {}
@temp_variable = @parent ? @parent.temp_variable : '__a' @temp_variable = @parent ? @parent.temp_variable : '__a'
end end
@@ -46,8 +47,8 @@ module CoffeeScript
@temp_variable.dup @temp_variable.dup
end end
def any_declared? def declarations?(body)
!declared_variables.empty? !declared_variables.empty? && body == @expressions
end end
# Return the list of variables first declared in current scope. # Return the list of variables first declared in current scope.