mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
first rough rough rough draft of kamatsu's closure suggestion -- test.coffee runs, but probably nothing else
This commit is contained in:
parent
de4eddcad4
commit
78c4957ba8
3 changed files with 62 additions and 141 deletions
|
@ -43,14 +43,14 @@ rule
|
|||
|
||||
# All parsing will end in this rule, being the trunk of the AST.
|
||||
Root:
|
||||
/* nothing */ { result = Expressions.new([]) }
|
||||
| Terminator { result = Expressions.new([]) }
|
||||
/* nothing */ { result = Expressions.new }
|
||||
| Terminator { result = Expressions.new }
|
||||
| Expressions { result = val[0] }
|
||||
;
|
||||
|
||||
# Any list of expressions or method body, seperated by line breaks or semis.
|
||||
Expressions:
|
||||
Expression { result = Expressions.new(val) }
|
||||
Expression { result = Expressions.wrap(val) }
|
||||
| Expressions Terminator Expression { result = val[0] << val[2] }
|
||||
| Expressions Terminator { result = val[0] }
|
||||
;
|
||||
|
@ -78,7 +78,7 @@ rule
|
|||
|
||||
Block:
|
||||
INDENT Expressions OUTDENT { result = val[1] }
|
||||
| INDENT OUTDENT { result = Expressions.new([]) }
|
||||
| INDENT OUTDENT { result = Expressions.new }
|
||||
;
|
||||
|
||||
# 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.
|
||||
If:
|
||||
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 UNLESS Expression { result = IfNode.new(val[2], Expressions.new([val[0]]), nil, {:statement => true, :invert => 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.wrap(val[0]), nil, {:statement => true, :invert => true}) }
|
||||
;
|
||||
|
||||
end
|
||||
|
|
|
@ -11,17 +11,11 @@ module CoffeeScript
|
|||
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"
|
||||
# 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"
|
||||
end
|
||||
|
||||
def write(code)
|
||||
|
@ -33,29 +27,31 @@ module CoffeeScript
|
|||
@options = o.dup
|
||||
end
|
||||
|
||||
def compile_closure(o={})
|
||||
"(#{CodeNode.new([], Expressions.wrap(self)).compile(o, o[:scope])})()"
|
||||
end
|
||||
|
||||
# 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
|
||||
def unwrap; self; end
|
||||
def statement?; false; end
|
||||
def statement_only?; false; end
|
||||
end
|
||||
|
||||
# A collection of nodes, each one representing an expression.
|
||||
class Expressions < Node
|
||||
statement
|
||||
custom_assign
|
||||
attr_reader :expressions
|
||||
|
||||
STRIP_TRAILING_WHITESPACE = /\s+$/
|
||||
|
||||
# Wrap up a node as an Expressions, unless it already is.
|
||||
def self.wrap(node)
|
||||
node.is_a?(Expressions) ? node : Expressions.new([node])
|
||||
def self.wrap(*nodes)
|
||||
return nodes[0] if nodes.length == 1 && nodes[0].is_a?(Expressions)
|
||||
Expressions.new(*nodes)
|
||||
end
|
||||
|
||||
def initialize(nodes)
|
||||
@expressions = nodes
|
||||
def initialize(*nodes)
|
||||
@expressions = nodes.flatten
|
||||
end
|
||||
|
||||
# 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.
|
||||
def root_compile(o={})
|
||||
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, '')
|
||||
o[:no_wrap] ? code : "(function(){\n#{code}\n})();"
|
||||
end
|
||||
|
||||
# The extra fancy is to handle pushing down returns and assignments
|
||||
# recursively to the final lines of inner statements.
|
||||
# Variables first defined within the Expressions body have their
|
||||
# declarations pushed up to the top scope.
|
||||
# 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.
|
||||
def compile(options={}, parent=nil)
|
||||
return root_compile(options) unless options[:scope]
|
||||
compiled = @expressions.map do |node|
|
||||
o = super(options)
|
||||
if last?(node) && (o[:return] || o[:assign])
|
||||
if o[:return]
|
||||
if node.statement? || node.custom_return?
|
||||
"#{node.compile(o)}#{node.line_ending}"
|
||||
else
|
||||
o.delete(:return)
|
||||
"#{o[:indent]}return #{node.compile(o)}#{node.line_ending}"
|
||||
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
|
||||
returns = o.delete(:return)
|
||||
code = node.compile(o)
|
||||
if last?(node) && returns && !node.statement_only?
|
||||
if node.statement?
|
||||
node.compile(o.merge(:return => true))
|
||||
else
|
||||
"#{o[:indent]}return #{node.compile(o)};"
|
||||
end
|
||||
else
|
||||
o.delete(:return) and o.delete(:assign)
|
||||
indent = node.unwrap.statement? ? '' : o[:indent]
|
||||
"#{indent}#{node.compile(o)}#{node.line_ending}"
|
||||
ending = node.statement_only? ? '' : ';'
|
||||
indent = node.statement? ? '' : o[:indent]
|
||||
"#{indent}#{node.compile(o)}#{ending}"
|
||||
end
|
||||
end
|
||||
scope = options[:scope]
|
||||
declarations = scope.any_declared? && parent == :code ? "#{options[:indent]}var #{scope.declared_variables.join(', ')};\n" : ''
|
||||
code = declarations + compiled.join("\n")
|
||||
decls = ''
|
||||
decls = "#{options[:indent]}var #{scope.declared_variables.join(', ')};\n" if parent == :code && scope.declarations?(self)
|
||||
code = decls + compiled.join("\n")
|
||||
write(code)
|
||||
end
|
||||
end
|
||||
|
@ -138,10 +127,7 @@ module CoffeeScript
|
|||
def statement?
|
||||
STATEMENTS.include?(@value.to_s)
|
||||
end
|
||||
|
||||
def line_ending
|
||||
@value.to_s[-1..-1] == ';' ? '' : ';'
|
||||
end
|
||||
alias_method :statement_only?, :statement?
|
||||
|
||||
def compile(o={})
|
||||
o = super(o)
|
||||
|
@ -152,8 +138,7 @@ module CoffeeScript
|
|||
|
||||
# Try to return your expression, or tell it to return itself.
|
||||
class ReturnNode < Node
|
||||
statement
|
||||
custom_return
|
||||
statement_only
|
||||
|
||||
attr_reader :expression
|
||||
|
||||
|
@ -161,10 +146,6 @@ module CoffeeScript
|
|||
@expression = expression
|
||||
end
|
||||
|
||||
def line_ending
|
||||
@expression.custom_return? ? '' : ';'
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
o = super(o)
|
||||
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
|
||||
# same position.
|
||||
class CommentNode < Node
|
||||
statement
|
||||
statement_only
|
||||
|
||||
def initialize(lines)
|
||||
@lines = lines.value
|
||||
end
|
||||
|
||||
def line_ending
|
||||
''
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
delimiter = "\n#{o[:indent]}//"
|
||||
comment = "#{delimiter}#{@lines.join(delimiter)}"
|
||||
|
@ -253,6 +230,7 @@ module CoffeeScript
|
|||
# Node to extend an object's prototype with an ancestor object.
|
||||
# After goog.inherits from the Closure Library.
|
||||
class ExtendsNode < Node
|
||||
statement
|
||||
attr_reader :sub_object, :super_object
|
||||
|
||||
def initialize(sub_object, super_object)
|
||||
|
@ -289,14 +267,6 @@ module CoffeeScript
|
|||
@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
|
||||
|
||||
def compile(o={})
|
||||
o = super(o)
|
||||
only = o.delete(:only_first)
|
||||
|
@ -401,35 +371,25 @@ module CoffeeScript
|
|||
PROTO_ASSIGN = /\A(\S+)\.prototype/
|
||||
LEADING_DOT = /\A\./
|
||||
|
||||
custom_return
|
||||
|
||||
attr_reader :variable, :value, :context
|
||||
|
||||
def initialize(variable, value, context=nil)
|
||||
@variable, @value, @context = variable, value, context
|
||||
end
|
||||
|
||||
def line_ending
|
||||
@value.custom_assign? ? '' : ';'
|
||||
end
|
||||
|
||||
def statement?
|
||||
@value.custom_assign?
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
o = super(o)
|
||||
return compile_splice(o) if @variable.properties.last.is_a?(SliceNode)
|
||||
name = @variable.compile(o)
|
||||
last = @variable.last.to_s.sub(LEADING_DOT, '')
|
||||
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)
|
||||
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?
|
||||
val = "#{name} = #{@value.compile(o)}"
|
||||
write(o[:return] && !@value.custom_return? ? "#{o[:indent]}return (#{val})" : val)
|
||||
code = @value.statement? ? @value.compile_closure(o) : @value.compile(o)
|
||||
val = "#{name} = #{code}"
|
||||
write(o[:return] ? "#{o[:indent]}return (#{val})" : val)
|
||||
end
|
||||
|
||||
def compile_splice(o)
|
||||
|
@ -498,13 +458,12 @@ module CoffeeScript
|
|||
@body = body
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
def compile(o={}, shared_scope=nil)
|
||||
o = super(o)
|
||||
o[:scope] = Scope.new(o[:scope])
|
||||
o[:scope] = shared_scope || Scope.new(o[:scope], @body)
|
||||
o[:return] = true
|
||||
indent = o[:indent]
|
||||
o[:indent] += TAB
|
||||
o.delete(:assign)
|
||||
o.delete(:no_wrap)
|
||||
name = o.delete(:immediate_assign)
|
||||
if @params.last.is_a?(ParamSplatNode)
|
||||
|
@ -604,10 +563,6 @@ module CoffeeScript
|
|||
@condition, @body = condition, body
|
||||
end
|
||||
|
||||
def line_ending
|
||||
''
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
o = super(o)
|
||||
o.delete(:return)
|
||||
|
@ -623,8 +578,6 @@ module CoffeeScript
|
|||
# the current index of the loop as a second parameter.
|
||||
class ForNode < Node
|
||||
statement
|
||||
custom_return
|
||||
custom_assign
|
||||
|
||||
attr_reader :body, :source, :name, :index, :filter, :step
|
||||
|
||||
|
@ -635,10 +588,6 @@ module CoffeeScript
|
|||
@step = source[:step]
|
||||
end
|
||||
|
||||
def line_ending
|
||||
''
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
o = super(o)
|
||||
range = @source.is_a?(RangeNode)
|
||||
|
@ -668,14 +617,12 @@ module CoffeeScript
|
|||
set_result = "#{rvar} = [];\n#{o[:indent]}"
|
||||
return_result = rvar
|
||||
temp_var = ValueNode.new(LiteralNode.new(tvar))
|
||||
body = Expressions.new([
|
||||
body = Expressions.wrap(
|
||||
AssignNode.new(temp_var, @body),
|
||||
CallNode.new(ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [temp_var])
|
||||
])
|
||||
if o[:return] || o[:assign]
|
||||
return_result = "#{o[:assign].compile(o)} = #{return_result}" if o[:assign]
|
||||
)
|
||||
if o[:return]
|
||||
return_result = "return #{return_result}" if o[:return]
|
||||
o.delete(:assign)
|
||||
o.delete(:return)
|
||||
body = IfNode.new(@filter, body, nil, :statement => true) if @filter
|
||||
elsif @filter
|
||||
|
@ -691,8 +638,6 @@ module CoffeeScript
|
|||
# A try/catch/finally block.
|
||||
class TryNode < Node
|
||||
statement
|
||||
custom_return
|
||||
custom_assign
|
||||
|
||||
attr_reader :try, :error, :recovery, :finally
|
||||
|
||||
|
@ -700,24 +645,20 @@ module CoffeeScript
|
|||
@try, @error, @recovery, @finally = try, error, recovery, finally
|
||||
end
|
||||
|
||||
def line_ending
|
||||
''
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
o = super(o)
|
||||
indent = o[:indent]
|
||||
o[:indent] += TAB
|
||||
error_part = @error ? " (#{@error}) " : ' '
|
||||
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}")
|
||||
end
|
||||
end
|
||||
|
||||
# Throw an exception.
|
||||
class ThrowNode < Node
|
||||
statement
|
||||
statement_only
|
||||
|
||||
attr_reader :expression
|
||||
|
||||
|
@ -760,14 +701,6 @@ module CoffeeScript
|
|||
@expressions.unwrap.statement?
|
||||
end
|
||||
|
||||
def custom_assign?
|
||||
@expressions.custom_assign?
|
||||
end
|
||||
|
||||
def custom_return?
|
||||
@expressions.custom_return?
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
raise SyntaxError, "line #{@line}: parentheses can't be wrapped around a statement" if statement?
|
||||
o = super(o)
|
||||
|
@ -827,18 +760,6 @@ module CoffeeScript
|
|||
@is_statement ||= !!(@tags[:statement] || @body.statement? || (@else_body && @else_body.statement?))
|
||||
end
|
||||
|
||||
def custom_return?
|
||||
statement?
|
||||
end
|
||||
|
||||
def custom_assign?
|
||||
statement?
|
||||
end
|
||||
|
||||
def line_ending
|
||||
statement? ? '' : ';'
|
||||
end
|
||||
|
||||
def compile(o={})
|
||||
o = super(o)
|
||||
write(statement? ? compile_statement(o) : compile_ternary(o))
|
||||
|
@ -850,7 +771,6 @@ module CoffeeScript
|
|||
indent = o[:indent]
|
||||
child = o.delete(:chain_child)
|
||||
cond_o = o.dup
|
||||
cond_o.delete(:assign)
|
||||
cond_o.delete(:return)
|
||||
o[:indent] += TAB
|
||||
if_dent = child ? '' : indent
|
||||
|
|
|
@ -5,11 +5,12 @@ module CoffeeScript
|
|||
# whether a variable has been seen before or if it needs to be declared.
|
||||
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.
|
||||
def initialize(parent=nil)
|
||||
@parent = parent
|
||||
# Initialize a scope with its parent, for lookups up the chain,
|
||||
# as well as the Expressions body where it should declare its variables.
|
||||
def initialize(parent, expressions)
|
||||
@parent, @expressions = parent, expressions
|
||||
@variables = {}
|
||||
@temp_variable = @parent ? @parent.temp_variable : '__a'
|
||||
end
|
||||
|
@ -46,8 +47,8 @@ module CoffeeScript
|
|||
@temp_variable.dup
|
||||
end
|
||||
|
||||
def any_declared?
|
||||
!declared_variables.empty?
|
||||
def declarations?(body)
|
||||
!declared_variables.empty? && body == @expressions
|
||||
end
|
||||
|
||||
# Return the list of variables first declared in current scope.
|
||||
|
|
Loading…
Reference in a new issue