diff --git a/documentation/coffee/block_comment.coffee b/documentation/coffee/block_comment.coffee
index 3cfd9e95..4c0012b4 100644
--- a/documentation/coffee/block_comment.coffee
+++ b/documentation/coffee/block_comment.coffee
@@ -1,4 +1,4 @@
###
-CoffeeScript Compiler v0.9.4
+CoffeeScript Compiler v0.9.5
Released under the MIT License
###
\ No newline at end of file
diff --git a/documentation/coffee/classes.coffee b/documentation/coffee/classes.coffee
index 7175baf1..6794518a 100644
--- a/documentation/coffee/classes.coffee
+++ b/documentation/coffee/classes.coffee
@@ -1,5 +1,5 @@
class Animal
- (@name) ->
+ constructor: (@name) ->
move: (meters) ->
alert @name + " moved " + meters + "m."
diff --git a/documentation/coffee/overview.coffee b/documentation/coffee/overview.coffee
index 7ab4e716..0db65dd2 100644
--- a/documentation/coffee/overview.coffee
+++ b/documentation/coffee/overview.coffee
@@ -25,4 +25,4 @@ race = (winner, runners...) ->
alert "I knew it!" if elvis?
# Array comprehensions:
-cubes = math.cube num for num in list
+cubes = (math.cube num for num in list)
diff --git a/documentation/coffee/soaks.coffee b/documentation/coffee/soaks.coffee
index 16b2350f..a473f376 100644
--- a/documentation/coffee/soaks.coffee
+++ b/documentation/coffee/soaks.coffee
@@ -1 +1 @@
-lottery.drawWinner?().address?.zipcode
+zip = lottery.drawWinner?().address?.zipcode
diff --git a/documentation/css/docs.css b/documentation/css/docs.css
index b47c1ff5..7bbd01ac 100644
--- a/documentation/css/docs.css
+++ b/documentation/css/docs.css
@@ -2,7 +2,7 @@ body {
font-size: 13px;
line-height: 20px;
color: #191933;
- font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
+ font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica,Arial, sans-serif !important;
}
.container {
width: 950px;
diff --git a/documentation/docs/coffee-script.html b/documentation/docs/coffee-script.html
index 85c3d7f4..01aca8a6 100644
--- a/documentation/docs/coffee-script.html
+++ b/documentation/docs/coffee-script.html
@@ -12,17 +12,20 @@ execute all scripts present in text/coffeescript
tags.
The current CoffeeScript version number.
exports.VERSION = '0.9.4'
Expose helpers for testing.
exports.helpers = require './helpers'
Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison + require.registerExtension '.coffee', (content) -> compile content
The current CoffeeScript version number.
exports.VERSION = '0.9.5'
Expose helpers for testing.
exports.helpers = require './helpers'
Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison compiler.
exports.compile = compile = (code, options = {}) ->
try
(parser.parse lexer.tokenize code).compile options
catch err
err.message = "In #{options.fileName}, #{err.message}" if options.fileName
throw err
Tokenize a string of CoffeeScript code, and return the array of tokens.
exports.tokens = (code, options) ->
- lexer.tokenize code, options
Tokenize and parse a string of CoffeeScript code, and return the AST. You can
-then compile it by calling .compile()
on the root, or traverse it by using
-.traverse()
with a callback.
exports.nodes = (code, options) ->
- parser.parse lexer.tokenize code, options
Compile and execute a string of CoffeeScript (on the server), correctly + lexer.tokenize code, options
Parse a string of CoffeeScript code or an array of lexed tokens, and
+return the AST. You can then compile it by calling .compile()
on the root,
+or traverse it by using .traverse()
with a callback.
exports.nodes = (source, options) ->
+ if typeof source is 'string'
+ parser.parse lexer.tokenize source, options
+ else
+ parser.parse source
Compile and execute a string of CoffeeScript (on the server), correctly
setting __filename
, __dirname
, and relative require()
.
exports.run = (code, options) ->
We want the root module.
root = module
while root.parent
root = root.parent
Set the filename.
root.filename = fs.realpathSync options.fileName or '.'
Clear the module cache.
root.moduleCache = {} if root.moduleCache
Compile.
if path.extname(root.filename) isnt '.coffee' or require.extensions
diff --git a/documentation/docs/grammar.html b/documentation/docs/grammar.html
index 8a538e18..35418bc0 100644
--- a/documentation/docs/grammar.html
+++ b/documentation/docs/grammar.html
@@ -150,14 +150,16 @@ as functions, indexed into, named as a class, etc.
The general group of accessors into an object, by property, by prototype or by array index or slice.
Accessor: [
- o '. Identifier', -> new Accessor $2
- o '?. Identifier', -> new Accessor $2, 'soak'
- o ':: Identifier', -> new Accessor $2, 'proto'
- o '::', -> new Accessor new Literal 'prototype'
+ o '. Identifier', -> new Access $2
+ o '?. Identifier', -> new Access $2, 'soak'
+ o ':: Identifier', -> new Access $2, 'proto'
+ o '::', -> new Access new Literal 'prototype'
o 'Index'
+ o 'Slice', -> new Slice $1
]
Indexing into an object or array using bracket notation.
Index: [
o 'INDEX_START Expression INDEX_END', -> new Index $2
o 'INDEX_SOAK Index', -> extend $2, soak : yes
@@ -198,11 +200,20 @@ and optional references to the superclass.
A reference to a property on this.
ThisProperty: [
- o '@ Identifier', -> new Value new Literal('this'), [new Accessor($2)], 'this'
+ o '@ Identifier', -> new Value new Literal('this'), [new Access($2)], 'this'
]
The array literal.
Array: [
o '[ ]', -> new Arr []
o '[ ArgList OptComma ]', -> new Arr $2
- ]
The ArgList is both the list of objects passed into a function call, + ]
Inclusive and exclusive range dots.
RangeDots: [
+ o '..', -> 'inclusive'
+ o '...', -> 'exclusive'
+ ]
The CoffeeScript range literal.
Range: [
+ o '[ Expression RangeDots Expression ]', -> new Range $2, $4, $3
+ ]
Array slice literals.
Slice: [
+ o 'INDEX_START Expression RangeDots Expression INDEX_END', -> new Range $2, $4, $3
+ o 'INDEX_START Expression RangeDots INDEX_END', -> new Range $2, null, $3
+ o 'INDEX_START RangeDots Expression INDEX_END', -> new Range null, $3, $2
+ ]
The ArgList is both the list of objects passed into a function call, as well as the contents of an array literal (i.e. comma-separated expressions). Newlines work as well.
ArgList: [
o 'Arg', -> [$1]
@@ -210,34 +221,35 @@ as well as the contents of an array literal
o 'ArgList OptComma TERMINATOR Arg', -> $1.concat $4
o 'INDENT ArgList OptComma OUTDENT', -> $2
o 'ArgList OptComma INDENT ArgList OptComma OUTDENT', -> $1.concat $4
- ]
Valid arguments are Expressions or Splats.
Arg: [
+ ]
Valid arguments are Expressions or Splats.
Arg: [
o 'Expression'
o 'Splat'
- ]
Just simple, comma-separated, required arguments (no fancy syntax). We need + ]
Just simple, comma-separated, required arguments (no fancy syntax). We need this to be separate from the ArgList for use in Switch blocks, where having the newlines wouldn't make sense.
SimpleArgs: [
o 'Expression'
o 'SimpleArgs , Expression', -> [].concat $1, $3
- ]
The variants of try/catch/finally exception handling blocks.
Try: [
+ ]
The variants of try/catch/finally exception handling blocks.
Try: [
o 'TRY Block', -> new Try $2
o 'TRY Block Catch', -> new Try $2, $3[0], $3[1]
o 'TRY Block FINALLY Block', -> new Try $2, null, null, $4
o 'TRY Block Catch FINALLY Block', -> new Try $2, $3[0], $3[1], $5
- ]
A catch clause names its error and runs a block of code.
Catch: [
+ ]
A catch clause names its error and runs a block of code.
Catch: [
o 'CATCH Identifier Block', -> [$2, $3]
- ]
Throw an exception object.
Throw: [
+ ]
Throw an exception object.
Throw: [
o 'THROW Expression', -> new Throw $2
- ]
Parenthetical expressions. Note that the Parenthetical is a Value, + ]
Parenthetical expressions. Note that the Parenthetical is a Value, not an Expression, so if you need to use an expression in a place where only values are accepted, wrapping it in parentheses will always do the trick.
Parenthetical: [
- o '( Expression )', -> new Parens $2
- ]
The condition portion of a while loop.
WhileSource: [
+ o '( Body )', -> new Parens $2
+ o '( INDENT Body OUTDENT )', -> new Parens $3
+ ]
The condition portion of a while loop.
WhileSource: [
o 'WHILE Expression', -> new While $2
o 'WHILE Expression WHEN Expression', -> new While $2, guard: $4
o 'UNTIL Expression', -> new While $2, invert: true
o 'UNTIL Expression WHEN Expression', -> new While $2, invert: true, guard: $4
- ]
The while loop can either be normal, with a block of expressions to execute, + ]
The while loop can either be normal, with a block of expressions to execute, or postfix, with a single expression. There is no do..while.
While: [
o 'WhileSource Block', -> $1.addBody $2
o 'Statement WhileSource', -> $2.addBody Expressions.wrap [$1]
@@ -248,46 +260,42 @@ or postfix, with a single expression. There is no do..while. Loop: [
o 'LOOP Block', -> new While(new Literal 'true').addBody $2
o 'LOOP Expression', -> new While(new Literal 'true').addBody Expressions.wrap [$2]
- ]
Array, object, and range comprehensions, at the most generic level. + ]
Array, object, and range comprehensions, at the most generic level. Comprehensions can either be normal, with a block of expressions to execute, or postfix, with a single expression.
For: [
- o 'Statement ForBody', -> new For $1, $2
- o 'Expression ForBody', -> new For $1, $2
- o 'ForBody Block', -> new For $2, $1
- ]
An array of all accepted values for a variable inside the loop. This -enables support for pattern matching.
ForValue: [
+ o 'Statement ForBody', -> new For $1, $2, $2.vars[0], $2.vars[1]
+ o 'Expression ForBody', -> new For $1, $2, $2.vars[0], $2.vars[1]
+ o 'ForBody Block', -> new For $2, $1, $1.vars[0], $1.vars[1]
+ ]
+
+ ForBody: [
+ o 'FOR Range', -> source: new Value($2), vars: []
+ o 'ForStart ForSource', -> $2.raw = $1.raw; $2.vars = $1; $2
+ ]
+
+ ForStart: [
+ o 'FOR ForVariables', -> $2
+ o 'FOR ALL ForVariables', -> $3.raw = yes; $3
+ ]
An array of all accepted values for a variable inside the loop. +This enables support for pattern matching.
ForValue: [
o 'Identifier'
o 'Array', -> new Value $1
o 'Object', -> new Value $1
- ]
-
- ForIn: [
- o 'FORIN Expression', -> source: $2
- o 'FORIN Expression WHEN Expression', -> source: $2, guard: $4
- o 'FORIN Expression BY Expression', -> source: $2, step: $4
- o 'FORIN Expression BY Expression WHEN Expression', -> source: $2, step: $4, guard: $6
- ]
-
- ForOf: [
- o 'FOROF Expression', -> object: on, source: $2
- o 'FOROF Expression WHEN Expression', -> object: on, source: $2, guard: $4
- ]
-
- ForTo: [
- o 'TO Expression', -> to: $2
- o 'TO Expression WHEN Expression', -> to: $2, guard: $4
- o 'TO Expression BY Expression', -> to: $2, step: $4
- o 'TO Expression BY Expression WHEN Expression', -> to: $2, step: $4, guard: $6
- ]
The source of a comprehension is an array or object with an optional guard + ]
An array or range comprehension has variables for the current element +and (optional) reference to the current index. Or, key, value, in the case +of object comprehensions.
ForVariables: [
+ o 'ForValue', -> [$1]
+ o 'ForValue , ForValue', -> [$1, $3]
+ ]
The source of a comprehension is an array or object with an optional guard clause. If it's an array comprehension, you can also choose to step through -in fixed-size increments.
ForBody: [
- o 'FOR ForValue ForIn', -> extend $3, name: $2
- o 'FOR ForValue , Identifier ForIn', -> extend $5, name: $2, index: $4
- o 'FOR Identifier ForOf', -> extend $3, index: $2
- o 'FOR ForValue , ForValue ForOf', -> extend $5, index: $2, name: $4
- o 'FOR ALL Identifier ForOf', -> extend $4, raw: on, index: $3
- o 'FOR ALL Identifier , ForValue ForOf', -> extend $6, raw: on, index: $3, name: $5
- o 'FOR Identifier FROM Expression ForTo', -> extend $5, index: $2, from: $4
+in fixed-size increments.
ForSource: [
+ o 'FORIN Expression', -> source: $2
+ o 'FOROF Expression', -> source: $2, object: yes
+ o 'FORIN Expression WHEN Expression', -> source: $2, guard: $4
+ o 'FOROF Expression WHEN Expression', -> source: $2, guard: $4, object: yes
+ o 'FORIN Expression BY Expression', -> source: $2, step: $4
+ o 'FORIN Expression WHEN Expression BY Expression', -> source: $2, guard: $4, step: $6
+ o 'FORIN Expression BY Expression WHEN Expression', -> source: $2, step: $4, guard: $6
]
Switch: [
@@ -300,24 +308,24 @@ in fixed-size increments.
An individual When clause, with action.
When: [
+ ]
An individual When clause, with action.
When: [
o 'LEADING_WHEN SimpleArgs Block', -> [[$2, $3]]
o 'LEADING_WHEN SimpleArgs Block TERMINATOR', -> [[$2, $3]]
- ]
The most basic form of if is a condition and an action. The following + ]
The most basic form of if is a condition and an action. The following if-related rules are broken up along these lines in order to avoid ambiguity.
IfBlock: [
o 'IF Expression Block', -> new If $2, $3
o 'UNLESS Expression Block', -> new If $2, $3, invert: true
o 'IfBlock ELSE IF Expression Block', -> $1.addElse new If $4, $5
o 'IfBlock ELSE Block', -> $1.addElse $3
- ]
The full complement of if expressions, including postfix one-liner + ]
The full complement of if expressions, including postfix one-liner if and unless.
If: [
o 'IfBlock'
o 'Statement POST_IF Expression', -> new If $3, Expressions.wrap([$1]), statement: true
o 'Expression POST_IF Expression', -> new If $3, Expressions.wrap([$1]), statement: true
o 'Statement POST_UNLESS Expression', -> new If $3, Expressions.wrap([$1]), statement: true, invert: true
o 'Expression POST_UNLESS Expression', -> new If $3, Expressions.wrap([$1]), statement: true, invert: true
- ]
Arithmetic and logical operators, working on one or more operands. + ]
Arithmetic and logical operators, working on one or more operands. Here they are grouped by order of precedence. The actual precedence rules are defined at the bottom of the page. It would be shorter if we could combine most of these rules into a single generic Operand OpSymbol Operand @@ -330,7 +338,7 @@ rules are necessary.
o 'Expression ?', -> new Existence $1
+ o 'SimpleAssignable ++', -> new Op '++', $1, null, true
o 'Expression ?', -> new Existence $1
o 'Expression + Expression', -> new Op '+' , $1, $3
o 'Expression - Expression', -> new Op '-' , $1, $3
@@ -350,7 +358,7 @@ rules are necessary.
Operators at the top of this list have higher precedence than the ones lower + ]
Operators at the top of this list have higher precedence than the ones lower
down. Following these rules is what makes 2 + 3 * 4
parse as:
2 + (3 * 4)
@@ -373,10 +381,10 @@ down. Following these rules is what makes 2 + 3 * 4
parse as:
['left', 'LOGIC']
['nonassoc', 'INDENT', 'OUTDENT']
['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN', 'THROW', 'EXTENDS']
- ['right', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY', 'WHEN']
+ ['right', 'FORIN', 'FOROF', 'BY', 'WHEN']
['right', 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS']
['right', 'POST_IF', 'POST_UNLESS']
-]
Finally, now what we have our grammar and our operators, we can create +]
Finally, now what we have our grammar and our operators, we can create our Jison.Parser. We do this by processing all of our rules, recording all terminals (every symbol which does not appear as the name of a rule above) as "tokens".
tokens = []
@@ -385,7 +393,7 @@ as "tokens".
Initialize the Parser with our list of terminal tokens, our grammar + alt
Initialize the Parser with our list of terminal tokens, our grammar rules, and the name of the root. Reverse the operators because Jison orders precedence from low to high, and we have it high to low (as in Yacc).
exports.parser = new Parser
diff --git a/documentation/docs/lexer.html b/documentation/docs/lexer.html
index 181d64fa..857a2a9c 100644
--- a/documentation/docs/lexer.html
+++ b/documentation/docs/lexer.html
@@ -27,8 +27,7 @@ unless explicitly asked not to.
At every position, run through this list of attempted matches, + @tokens = [] # Stream of parsed tokens in the form `['TYPE', value, line]`.
At every position, run through this list of attempted matches,
short-circuiting if any of them succeed. Their order determines precedence:
@literalToken
is the fallback catch-all.
i = 0
while @chunk = code.slice i
@@ -57,15 +56,6 @@ though is
means ===
otherwise.
balancedString: (str, delimited, options = {}) ->
stack = [delimited[0]]
- for i from 1 to str.length - 1
+ for i in [1...str.length]
switch str.charAt i
when '\\'
i++
@@ -352,10 +342,11 @@ token stream.
Compile to a source/variable pair suitable for looping.
compileLoopReference: (o, name) ->
src = tmp = @compile o, LEVEL_LIST
- unless NUMBER.test(src) or IDENTIFIER.test(src) and o.scope.check(src, yes)
+ unless -Infinity < +src < Infinity or IDENTIFIER.test(src) and o.scope.check(src, yes)
src = "#{ tmp = o.scope.freeVariable name } = #{src}"
[src, tmp]
Construct a node that returns the current node's result. Note that this is overridden for smarter behavior for @@ -114,9 +114,12 @@ it back out.
An Expressions node does not return its entire body, rather it ensures that the final expression is returned.
makeReturn: ->
- for end, idx in @expressions by -1 when end not instanceof Comment
- @expressions[idx] = end.makeReturn()
- break
+ len = @expressions.length
+ while len--
+ expr = @expressions[len]
+ if expr not instanceof Comment
+ @expressions[len] = expr.makeReturn()
+ break
this
An Expressions is the only node that can serve as the root.
compile: (o = {}, level) ->
if o.scope then super o, level else @compileRoot o
Compile all expressions within the Expressions body. If we need to return the result, and it's an expression, simply return it. If it's a @@ -209,6 +212,7 @@ or vanilla.
Add a property access to the list.
push: (prop) ->
@properties.push prop
@@ -231,6 +235,9 @@ or vanilla.
The value can be unwrapped as its inner node, if there are no attached properties.
unwrap: ->
@@ -256,7 +263,7 @@ evaluate anything twice when building the soak chain.
Unfold a soak into an If
: a?.b
-> a.b if a?
unfoldSoak: (o) ->
if ifn = @base.unfoldSoak o
@@ -368,7 +375,7 @@ inner constructor in order to be able to pass the varargs.
goog.inherits
from the
Closure Library. compile: (o) ->
utility 'hasProp'
- new Call(new Value(new Literal utility 'extends'), [@child, @parent]).compile o
Hooks one constructor into another's prototype chain.
exports.Accessor = class Accessor extends Base
+ new Call(new Value(new Literal utility 'extends'), [@child, @parent]).compile o
Hooks one constructor into another's prototype chain.
exports.Access = class Access extends Base
constructor: (@name, tag) ->
@proto = if tag is 'proto' then '.prototype' else ''
@soak = tag is 'soak'
@@ -379,8 +386,8 @@ After goog.inherits
from the
name = @name.compile o
@proto + if IS_STRING.test name then "[#{name}]" else ".#{name}"
- isComplex: NO
A .
accessor into a property of a value, or the ::
shorthand for
-an accessor into the object's prototype.
exports.Index = class Index extends Base
+ isComplex: NO
A .
access into a property of a value, or the ::
shorthand for
+an access into the object's prototype.
exports.Index = class Index extends Base
constructor: (@index) ->
children: ['index']
@@ -389,7 +396,74 @@ an accessor into the object's prototype.
A [ ... ]
indexed accessor into an array or object.
exports.Obj = class Obj extends Base
+ @index.isComplex()
A [ ... ]
indexed access into an array or object.
exports.Range = class Range extends Base
+
+ children: ['from', 'to']
+
+ constructor: (@from, @to, tag) ->
+ @exclusive = tag is 'exclusive'
+ @equals = if @exclusive then '' else '='
A range literal. Ranges can be used to extract portions (slices) of arrays, +to specify a range for comprehensions, or as a value, to be expanded into the +corresponding array of integers at runtime.
compileVariables: (o) ->
+ o = merge(o, top: true)
+ [@from, @fromVar] = @from.cache o, LEVEL_LIST
+ [@to, @toVar] = @to.cache o, LEVEL_LIST
+ [@fromNum, @toNum] = [@fromVar.match(SIMPLENUM), @toVar.match(SIMPLENUM)]
+ parts = []
+ parts.push @from if @from isnt @fromVar
+ parts.push @to if @to isnt @toVar
Compiles the range's source variables -- where it starts and where it ends. +But only if they need to be cached to avoid double evaluation.
compileNode: (o) ->
+ @compileVariables o
+ return @compileArray(o) unless o.index
+ return @compileSimple(o) if @fromNum and @toNum
+ idx = del o, 'index'
+ step = del o, 'step'
+ vars = "#{idx} = #{@from}" + if @to isnt @toVar then ", #{@to}" else ''
+ intro = "(#{@fromVar} <= #{@toVar} ? #{idx}"
+ compare = "#{intro} <#{@equals} #{@toVar} : #{idx} >#{@equals} #{@toVar})"
+ stepPart = if step then step.compile(o) else '1'
+ incr = if step then "#{idx} += #{stepPart}" else "#{intro} += #{stepPart} : #{idx} -= #{stepPart})"
+ "#{vars}; #{compare}; #{incr}"
When compiled normally, the range returns the contents of the for loop +needed to iterate over the values in the range. Used by comprehensions.
compileSimple: (o) ->
+ [from, to] = [+@fromNum, +@toNum]
+ idx = del o, 'index'
+ step = del o, 'step'
+ step and= "#{idx} += #{step.compile(o)}"
+ if from <= to
+ "#{idx} = #{from}; #{idx} <#{@equals} #{to}; #{step or "#{idx}++"}"
+ else
+ "#{idx} = #{from}; #{idx} >#{@equals} #{to}; #{step or "#{idx}--"}"
Compile a simple range comprehension, with integers.
compileArray: (o) ->
+ if @fromNum and @toNum and Math.abs(@fromNum - @toNum) <= 20
+ range = [+@fromNum..+@toNum]
+ range.pop() if @exclusive
+ return "[#{ range.join(', ') }]"
+ idt = @tab + TAB
+ i = o.scope.freeVariable 'i'
+ result = o.scope.freeVariable 'results'
+ pre = "\n#{idt}#{result} = [];"
+ if @fromNum and @toNum
+ o.index = i
+ body = @compileSimple o
+ else
+ vars = "#{i} = #{@from}" + if @to isnt @toVar then ", #{@to}" else ''
+ clause = "#{@fromVar} <= #{@toVar} ?"
+ body = "var #{vars}; #{clause} #{i} <#{@equals} #{@toVar} : #{i} >#{@equals} #{@toVar}; #{clause} #{i} += 1 : #{i} -= 1"
+ post = "{ #{result}.push(#{i}); }\n#{idt}return #{result};\n#{o.indent}"
+ "(function() {#{pre}\n#{idt}for (#{body})#{post}}).call(this)"
When used as a value, expand the range into the equivalent array.
exports.Slice = class Slice extends Base
+
+ children: ['range']
+
+ constructor: (@range) ->
+ super()
+
+ compileNode: (o) ->
+ from = if @range.from then @range.from.compile(o) else '0'
+ to = if @range.to then @range.to.compile(o) else ''
+ to += if not to or @range.exclusive then '' else ' + 1'
+ to = ', ' + to if to
+ ".slice(#{from}#{to})"
An array slice literal. Unlike JavaScript's Array#slice
, the second parameter
+specifies the index of the end of the slice, just as the first parameter
+is the index of the beginning.
exports.Obj = class Obj extends Base
constructor: (props, @generated = false) ->
@objects = @properties = props or []
@@ -447,7 +521,7 @@ an accessor into the object's prototype.
An object literal, nothing fancy.
exports.Arr = class Arr extends Base
+ no
An object literal, nothing fancy.
exports.Arr = class Arr extends Base
constructor: (objs) ->
@objects = objs or []
@@ -465,30 +539,30 @@ an accessor into the object's prototype.
An array literal.
exports.Class = class Class extends Base
+ no
An array literal.
exports.Class = class Class extends Base
constructor: (@variable, @parent, @body = new Expressions) ->
@boundFuncs = []
- children: ['variable', 'parent', 'body']
The CoffeeScript class definition. + children: ['variable', 'parent', 'body']
The CoffeeScript class definition. Initialize a Class with its name, an optional superclass, and a list of prototype property assignments.
determineName: ->
return null unless @variable
decl = if tail = last @variable.properties
- tail instanceof Accessor and tail.name.value
+ tail instanceof Access and tail.name.value
else
@variable.base.value
- decl and= IDENTIFIER.test(decl) and decl
Figure out the appropriate name for the constructor function of this class.
setContext: (name) ->
+ decl and= IDENTIFIER.test(decl) and decl
Figure out the appropriate name for the constructor function of this class.
setContext: (name) ->
@body.traverseChildren false, (node) ->
if node instanceof Literal and node.value is 'this'
node.value = name
else if node instanceof Code
node.klass = name
- node.context = name if node.bound
For all this
-references and bound functions in the class definition,
+ node.context = name if node.bound
For all this
-references and bound functions in the class definition,
this
is the Class being constructed.
addBoundFunctions: (o) ->
if @boundFuncs.length
for bvar in @boundFuncs
bname = bvar.compile o
- @ctor.body.unshift new Literal "this.#{bname} = #{utility 'bind'}(this.#{bname}, this);"
Ensure that all functions bound to the instance are proxied in the + @ctor.body.unshift new Literal "this.#{bname} = #{utility 'bind'}(this.#{bname}, this);"
Ensure that all functions bound to the instance are proxied in the constructor.
addProperties: (node, name) ->
props = node.base.properties.slice 0
while assign = props.shift()
@@ -508,24 +582,24 @@ constructor.
Merge the properties from a top-level object as prototypal properties + assign
Merge the properties from a top-level object as prototypal properties on the class.
walkBody: (name) ->
@traverseChildren false, (child) =>
if child instanceof Expressions
for node, i in exps = child.expressions
if node instanceof Value and node.isObject(true)
exps[i] = compact @addProperties node, name
- child.expressions = exps = compact flatten exps
Walk the body of the class, looking for prototype properties to be converted.
ensureConstructor: (name) ->
+ child.expressions = exps = compact flatten exps
Walk the body of the class, looking for prototype properties to be converted.
ensureConstructor: (name) ->
if not @ctor
@ctor = new Code
@ctor.body.push new Call 'super', [new Splat new Literal 'arguments'] if @parent
@ctor.ctor = @ctor.name = name
@ctor.klass = null
- @ctor.noReturn = yes
Make sure that a constructor is defined for the class, and properly + @ctor.noReturn = yes
Make sure that a constructor is defined for the class, and properly configured.
compileNode: (o) ->
decl = @determineName()
name = decl or @name or '_Class'
@@ -542,10 +616,10 @@ configured.
Instead of generating the JavaScript string directly, we build up the + klass.compile o
Instead of generating the JavaScript string directly, we build up the equivalent syntax tree and compile that, in pieces. You can see the -constructor, property assignments, and inheritance getting built out below.
exports.Assign = class Assign extends Base
- constructor: (@variable, @value, @context) ->
The Assign is used to assign a local variable to value, or to set the +constructor, property assignments, and inheritance getting built out below.
exports.Assign = class Assign extends Base
+ constructor: (@variable, @value, @context) ->
The Assign is used to assign a local variable to value, or to set the property of an object -- including within object literals.
METHOD_DEF: /^(?:(\S+)\.prototype\.|\S+?)?\b([$A-Za-z_][$\w]*)$/
children: ['variable', 'value']
@@ -554,9 +628,10 @@ property of an object -- including within object literals.
Matchers for detecting class/method names
compileNode: (o) ->
+ unfoldSoak o, this, 'variable'
Matchers for detecting class/method names
compileNode: (o) ->
if isValue = @variable instanceof Value
return @compilePatternMatch o if @variable.isArray() or @variable.isObject()
+ return @compileSplice o if @variable.isSplice()
return @compileConditional o if @context in ['||=', '&&=', '?=']
name = @variable.compile o, LEVEL_LIST
if @value instanceof Code and match = @METHOD_DEF.exec name
@@ -569,7 +644,7 @@ property of an object -- including within object literals.
Compile an assignment, delegating to compilePatternMatch
or
+ if o.level <= LEVEL_LIST then val else "(#{val})"
Compile an assignment, delegating to compilePatternMatch
or
compileSplice
if appropriate. Keep track of the name of the base object
we've been assigned to, for correct internal references. If the variable
has not been seen yet within the current scope, declare it.
compilePatternMatch: (o) ->
@@ -578,7 +653,7 @@ has not been seen yet within the current scope, declare it.
Brief implementation of recursive pattern matching, when assigning array or + if top and olen is 1 and (obj = objects[0]) not instanceof Splat
Brief implementation of recursive pattern matching, when assigning array or object literals to a value. Peeks at their properties to assign inner names. See the ECMAScript Harmony Wiki for details.
if obj instanceof Assign
@@ -593,7 +668,7 @@ for details.
Unroll simplest cases: {v} = x
-> v = x.v
idx = i
+ for obj, i in objects
Unroll simplest cases: {v} = x
-> v = x.v
idx = i
if isObject
- if obj instanceof Assign
A regular array pattern-match.
{variable: {base: idx}, value: obj} = obj
- else
A regular object pattern-match.
if obj.base instanceof Parens
+ if obj instanceof Assign
A regular array pattern-match.
{variable: {base: idx}, value: obj} = obj
+ else
A regular object pattern-match.
if obj.base instanceof Parens
[obj, idx] = new Value(obj.unwrapAll()).cacheReference o
else
idx = if obj.this then obj.properties[0].name else obj
@@ -627,15 +702,24 @@ for details.
A shorthand {a, b, @c} = val
pattern-match.
compileConditional: (o) ->
+ if o.level < LEVEL_LIST then code else "(#{code})"
A shorthand {a, b, @c} = val
pattern-match.
compileConditional: (o) ->
[left, rite] = @variable.cacheReference o
- new Op(@context.slice(0, -1), left, new Assign(rite, @value, '=')).compile o
When compiling a conditional assignment, take care to ensure that the + new Op(@context.slice(0, -1), left, new Assign(rite, @value, '=')).compile o
When compiling a conditional assignment, take care to ensure that the operands are only evaluated once, even though we have to reference them -more than once.
exports.Code = class Code extends Base
+more than once.
compileSplice: (o) ->
+ {range} = @variable.properties.pop()
+ name = @variable.compile o
+ plus = if range.exclusive then '' else ' + 1'
+ from = if range.from then range.from.compile(o) else '0'
+ to = if range.to then range.to.compile(o) + ' - ' + from + plus else "#{name}.length"
+ ref = o.scope.freeVariable 'ref'
+ val = @value.compile(o)
+ "([].splice.apply(#{name}, [#{from}, #{to}].concat(#{ref} = #{val})), #{ref})"
Compile the assignment from an array splice literal, using JavaScript's
+Array#splice
method.
exports.Code = class Code extends Base
constructor: (params, body, tag) ->
@params = params or []
@body = body or new Expressions
@@ -644,7 +728,7 @@ more than once.
A function definition. This is the only node that creates a new Scope. + isStatement: -> !!@ctor
A function definition. This is the only node that creates a new Scope. When for the purposes of walking the contents of a function body, the Code has no children -- they're within the inner scope.
compileNode: (o) ->
sharedScope = del o, 'sharedScope'
@@ -684,13 +768,13 @@ has no children -- they're within the inner scope.
Compilation creates a new scope unless explicitly asked to share with the + if @front then "(#{code})" else code
Compilation creates a new scope unless explicitly asked to share with the
outer scope. Handles splat parameters in the parameter list by peeking at
the JavaScript arguments
objects. If the function is bound with the =>
arrow, generates a wrapper that saves the current value of this
through
a closure.
traverseChildren: (crossScope, func) ->
- super(crossScope, func) if crossScope
Short-circuit traverseChildren
method to prevent it from crossing scope boundaries
-unless crossScope
is true
.
exports.Param = class Param extends Base
+ super(crossScope, func) if crossScope
Short-circuit traverseChildren
method to prevent it from crossing scope boundaries
+unless crossScope
is true
.
exports.Param = class Param extends Base
constructor: (@name, @value, @splat) ->
children: ['name', 'value']
@@ -700,15 +784,20 @@ unless crossScope
is true
.
A parameter in a function definition. Beyond a typical Javascript parameter, + @name.isComplex()
A parameter in a function definition. Beyond a typical Javascript parameter, these parameters can also attach themselves to the context of the function, -as well as be a splat, gathering up a group of parameters into an array.
exports.Splat = class Splat extends Base
+as well as be a splat, gathering up a group of parameters into an array.
exports.Splat = class Splat extends Base
children: ['name']
@@ -721,7 +810,7 @@ as well as be a splat, gathering up a group of parameters into an array.
@name.assigns name
compile: (o) ->
- if @index? then @compileParam o else @name.compile o
A splat, either as a parameter to a function, an argument to a call, + if @index? then @compileParam o else @name.compile o
A splat, either as a parameter to a function, an argument to a call, or as part of a destructuring assignment.
@compileSplattedArray: (o, list, apply) ->
index = -1
continue while (node = list[++index]) and node not instanceof Splat
@@ -738,8 +827,8 @@ or as part of a destructuring assignment.
Utility function that converts arbitrary number of elements, mixed with -splats, to a proper array.
exports.While = class While extends Base
+ "[#{ base.join ', ' }].concat(#{ args.join ', ' })"
Utility function that converts arbitrary number of elements, mixed with +splats, to a proper array.
exports.While = class While extends Base
constructor: (condition, options) ->
@condition = if options?.invert then condition.invert() else condition
@guard = options?.guard
@@ -761,7 +850,7 @@ splats, to a proper array.
A while loop, the only sort of low-level loop exposed by CoffeeScript. From + false
A while loop, the only sort of low-level loop exposed by CoffeeScript. From it, all other loops can be manufactured. Useful in cases where you need more flexibility or more speed than a comprehension can provide.
compileNode: (o) ->
o.indent += TAB
@@ -780,9 +869,9 @@ flexibility or more speed than a comprehension can provide.
The main difference from a JavaScript while is that the CoffeeScript + code
The main difference from a JavaScript while is that the CoffeeScript while can be used as a part of a larger expression -- while loops may -return an array containing the computed result of each iteration.
exports.Op = class Op extends Base
+return an array containing the computed result of each iteration.
exports.Op = class Op extends Base
constructor: (op, first, second, flip) ->
return new In first, second if op is 'in'
if op is 'new'
@@ -791,11 +880,12 @@ return an array containing the computed result of each iteration.
@operator = CONVERSIONS[op] or op
@first = first
@second = second
- @flip = !!flip
Simple Arithmetic and logical operations. Performs some conversion from + @flip = !!flip + return this
Simple Arithmetic and logical operations. Performs some conversion from CoffeeScript operations into their JavaScript equivalents.
CONVERSIONS =
'==': '==='
'!=': '!=='
- 'of': 'in'
The map of conversions from CoffeeScript to JavaScript symbols.
INVERSIONS =
+ 'of': 'in'
The map of conversions from CoffeeScript to JavaScript symbols.
INVERSIONS =
'!==': '==='
'===': '!=='
'>': '<='
@@ -806,7 +896,7 @@ CoffeeScript operations into their JavaScript equivalents.
The map of invertible operators.
isChainable: ->
+ not @second
The map of invertible operators.
isChainable: ->
@operator in ['<', '>', '>=', '<=', '===', '!==']
invert: ->
@@ -831,7 +921,7 @@ CoffeeScript operations into their JavaScript equivalents.
Am I capable of + if o.level <= LEVEL_OP then code else "(#{code})"
Am I capable of Python-style comparison chaining?
compileChain: (o) ->
[@first.second, shared] = @first.second.cache o
fst = @first .compile o, LEVEL_OP
@@ -846,7 +936,7 @@ CoffeeScript operations into their JavaScript equivalents.
Mimic Python's chained comparisons when multiple comparison operators are + new Existence(fst).compile(o) + " ? #{ref} : #{ @second.compile o, LEVEL_LIST }"
Mimic Python's chained comparisons when multiple comparison operators are used sequentially. For example:
bin/coffee -e 'console.log 50 < 65 > 10'
@@ -860,7 +950,7 @@ true
parts.join ''
toString: (idt) ->
- super idt, @constructor.name + ' ' + @operator
Compile a unary Op.
exports.In = class In extends Base
+ super idt, @constructor.name + ' ' + @operator
Compile a unary Op.
exports.In = class In extends Base
constructor: (@object, @array) ->
children: ['object', 'array']
@@ -890,7 +980,7 @@ true
if o.level < LEVEL_LIST then code else "(#{code})"
toString: (idt) ->
- super idt, @constructor.name + if @negated then '!' else ''
exports.Try = class Try extends Base
+ super idt, @constructor.name + if @negated then '!' else ''
exports.Try = class Try extends Base
constructor: (@attempt, @error, @recovery, @ensure) ->
children: ['attempt', 'recovery', 'ensure']
@@ -900,7 +990,7 @@ true
makeReturn: ->
@attempt = @attempt .makeReturn() if @attempt
@recovery = @recovery.makeReturn() if @recovery
- this
A classic try/catch/finally block.
compileNode: (o) ->
+ this
A classic try/catch/finally block.
compileNode: (o) ->
o.indent += TAB
errorPart = if @error then " (#{ @error.compile o }) " else ' '
catchPart = if @recovery
@@ -910,18 +1000,18 @@ true
"""
#DIVIDER
- """ + if @ensure then " finally {\n#{ @ensure.compile o, LEVEL_TOP }\n#{@tab}}" else ''
Compilation is more or less as you would expect -- the finally clause -is optional, the catch is not.
{@tab}try { + """ + if @ensure then " finally {\n#{ @ensure.compile o, LEVEL_TOP }\n#{@tab}}" else ''
Compilation is more or less as you would expect -- the finally clause +is optional, the catch is not.
{@tab}try { { @attempt.compile o, LEVEL_TOP } {@tab}}#{ catchPart or '' }
exports.Throw = class Throw extends Base
constructor: (@expression) ->
children: ['expression']
- isStatement: YES
makeReturn: THIS
+ isStatement: YES
makeReturn: THIS
compileNode: (o) ->
- @tab + "throw #{ @expression.compile o };"
Simple node to throw an exception.
A Throw is already a return, of sorts...
exports.Existence = class Existence extends Base
+ @tab + "throw #{ @expression.compile o };"
Simple node to throw an exception.
A Throw is already a return, of sorts...
exports.Existence = class Existence extends Base
constructor: (@expression) ->
children: ['expression']
@@ -938,38 +1028,42 @@ is optional, the catch is not.
Checks a variable for existence -- not null and not undefined. This is + if o.level <= LEVEL_COND then code else "(#{code})"
Checks a variable for existence -- not null and not undefined. This is
similar to .nil?
in Ruby, and avoids having to consult a JavaScript truth
table.
exports.Parens = class Parens extends Base
- constructor: (@expression) ->
+ constructor: (@body) ->
- children: ['expression']
+ children: ['body']
- unwrap : -> @expression
- isComplex : -> @expression.isComplex()
- makeReturn: -> @expression.makeReturn()
+ unwrap : -> @body
+ isComplex : -> @body.isComplex()
+ makeReturn: -> @body.makeReturn()
compileNode: (o) ->
- expr = @expression
+ expr = @body.unwrap()
if expr instanceof Value and expr.isAtomic()
expr.front = @front
return expr.compile o
bare = o.level < LEVEL_OP and (expr instanceof Op or expr instanceof Call)
code = expr.compile o, LEVEL_PAREN
- if bare then code else "(#{code})"
An extra set of parentheses, specified explicitly in the source. At one time + if bare then code else "(#{code})"
An extra set of parentheses, specified explicitly in the source. At one time we tried to clean up the results by detecting and removing redundant parentheses, but no longer -- you can put in as many as you please.
Parentheses are a good way to force any statement to become an expression.
exports.For = class For extends Base
- constructor: (body, head) ->
- if head.index instanceof Value
- throw SyntaxError 'index cannot be a pattern matching expression'
- extend this, head
+ constructor: (body, source, @name, @index) ->
+ {@source, @guard, @step} = source
@body = Expressions.wrap [body]
+ @raw = !!source.raw
+ @object = !!source.object
+ [@name, @index] = [@index, @name] if @object
+ throw SyntaxError 'index cannot be a pattern matching expression' if @index instanceof Value
+ @range = @source instanceof Value and @source.base instanceof Range and not @source.properties.length
@pattern = @name instanceof Value
+ throw SyntaxError 'cannot pattern match a range loop' if @range and @pattern
@returns = false
- children: ['body', 'source', 'guard', 'step', 'from', 'to']
+ children: ['body', 'source', 'guard', 'step']
isStatement: YES
@@ -977,87 +1071,58 @@ parentheses, but no longer -- you can put in as many as you please.
@returns = yes
this
- containsPureStatement: While::containsPureStatement
-
- compileReturnValue: (val, o) ->
- return '\n' + new Return(new Literal val).compile o if @returns
- return '\n' + val if val
- ''
compileNode: (o) ->
- {scope} = o
- {body} = this
- hasCode = @body.contains (node) -> node instanceof Code
- name = not @pattern and @name?.compile o
- index = @index?.compile o
- ivar = if not index then scope.freeVariable 'i' else index
- varPart = guardPart = defPart = retPart = ''
- idt = o.indent + TAB
+ containsPureStatement: While::containsPureStatement
compileNode: (o) ->
+ body = Expressions.wrap [@body]
+ hasCode = body.contains (node) -> node instanceof Code
+ hasPure = last(body.expressions)?.containsPureStatement()
+ source = if @range then @source.base else @source
+ scope = o.scope
+ name = @name and @name.compile o, LEVEL_LIST
+ index = @index and @index.compile o, LEVEL_LIST
unless hasCode
- scope.find(name, yes) if name
- scope.find(index, yes) if index
- [step, pvar] = @step.compileLoopReference o, 'step' if @step
- if @from
- [head, fvar] = @from.compileLoopReference o, 'from'
- [tail, tvar] = @to.compileLoopReference o, 'to'
- vars = ivar + ' = ' + head
- vars += ', ' + tail if tail isnt tvar
- if SIMPLENUM.test(head) and SIMPLENUM.test(tail)
- if +head <= +tail
- cond = "#{ivar} <= #{tail}"
- else
- pvar or= -1
- cond = "#{ivar} >= #{tail}"
- else
- if +pvar
- cond = "#{ivar} #{ if pvar < 0 then '>' else '<' }= #{tvar}"
- else
- intro = "#{fvar} <= #{tvar} ? #{ivar}"
- cond = "#{intro} <= #{tvar} : #{ivar} >= #{tvar}"
- incr = if pvar then "#{ivar} += #{pvar}" else "#{intro}++ : #{ivar}--"
+ scope.find(name, immediate: yes) if name and not @pattern
+ scope.find(index, immediate: yes) if index
+ rvar = scope.freeVariable 'results' if @returns and not hasPure
+ ivar = (if @range then name else index) or scope.freeVariable 'i'
+ varPart = ''
+ guardPart = ''
+ defPart = ''
+ idt1 = @tab + TAB
+ if @range
+ forPart = source.compile merge(o, {index: ivar, @step})
else
- if name or @object and not @raw
- [sourcePart, svar] = @source.compileLoopReference o, 'ref'
- else
- sourcePart = svar = @source.compile o, LEVEL_PAREN
+ svar = @source.compile o, LEVEL_TOP
+ if (name or not @raw) and
+ not (IDENTIFIER.test(svar) and scope.check svar, immediate: on)
+ defPart = "#{@tab}#{ref = scope.freeVariable 'ref'} = #{svar};\n"
+ svar = ref
namePart = if @pattern
new Assign(@name, new Literal "#{svar}[#{ivar}]").compile o, LEVEL_TOP
else if name
"#{name} = #{svar}[#{ivar}]"
unless @object
- if 0 > pvar and (pvar | 0) is +pvar # negative int
- vars = "#{ivar} = #{svar}.length - 1"
- cond = "#{ivar} >= 0"
- else
- lvar = scope.freeVariable 'len'
- vars = "#{ivar} = 0, #{lvar} = #{svar}.length"
- cond = "#{ivar} < #{lvar}"
+ lvar = scope.freeVariable 'len'
+ stepPart = if @step then "#{ivar} += #{ @step.compile(o, LEVEL_OP) }" else "#{ivar}++"
+ forPart = "#{ivar} = 0, #{lvar} = #{svar}.length; #{ivar} < #{lvar}; #{stepPart}"
+ if @returns and not hasPure
+ resultPart = "#{@tab}#{rvar} = [];\n"
+ returnResult = '\n' + (new Return(new Literal(rvar)).compile o, LEVEL_PAREN)
+ body = Push.wrap rvar, body
+ if @guard
+ body = Expressions.wrap [new If @guard, body]
+ if hasCode
+ body = Closure.wrap(body, yes)
+ varPart = "\n#{idt1}#{namePart};" if namePart
if @object
- forPart = ivar + ' in ' + sourcePart
- guardPart = if @raw then '' else
- idt + "if (!#{ utility 'hasProp' }.call(#{svar}, #{ivar})) continue;\n"
- else
- pvar or= 1
- vars += ', ' + step if step and (step isnt pvar)
- defPart = @tab + sourcePart + ';\n' if svar isnt sourcePart
- forPart = vars + "; #{cond}; " + (incr or (ivar + switch +pvar
- when 1 then '++'
- when -1 then '--'
- else (if pvar < 0 then ' -= ' + pvar.slice 1 else ' += ' + pvar)
- ))
- body = Closure.wrap(body, yes) if hasCode
- varPart = idt + namePart + ';\n' if namePart
- defPart += @pluckDirectCall o, body, name, index unless @pattern
- code = guardPart + varPart
- unless body.isEmpty()
- if o.level > LEVEL_TOP or @returns
- rvar = scope.freeVariable 'results'
- defPart += @tab + rvar + ' = [];\n'
- retPart = @compileReturnValue rvar, o
- body = Push.wrap rvar, body
- body = Expressions.wrap [new If @guard, body] if @guard
- o.indent = idt
- code += body.compile o, LEVEL_TOP
- code = '\n' + code + '\n' + @tab if code
- defPart + @tab + "for (#{forPart}) {#{code}}" + retPart
+ forPart = "#{ivar} in #{svar}"
+ guardPart = "\n#{idt1}if (!#{utility('hasProp')}.call(#{svar}, #{ivar})) continue;" unless @raw
+ defPart += @pluckDirectCall o, body, name, index unless @pattern
+ body = body.compile merge(o, indent: idt1), LEVEL_TOP
+ body = '\n' + body + '\n' if body
+ """
+
+#DIVIDER
+ """
pluckDirectCall: (o, body, name, index) ->
defs = ''
@@ -1082,13 +1147,13 @@ parentheses, but no longer -- you can put in as many as you please.
args.unshift new Literal 'this'
body.expressions[idx] = new Call base, args
defs += @tab + new Assign(ref, fn).compile(o, LEVEL_TOP) + ';\n'
- defs
CoffeeScript's replacement for the for loop is our array and object + defs
CoffeeScript's replacement for the for loop is our array and object comprehensions, that compile into for loops here. They also act as an expression, able to return the result of each filtered iteration.
Unlike Python array comprehensions, they can be multi-line, and you can pass the current index of the loop as a second parameter. Unlike Ruby blocks, -you can map and filter in a single pass.
Welcome to the hairiest method in all of CoffeeScript. Handles the inner +you can map and filter in a single pass.
Welcome to the hairiest method in all of CoffeeScript. Handles the inner loop, filtering, stepping, and result saving for array, object, and range comprehensions. Some of the generated code can be shared in common, and some cannot.
exports.Switch = class Switch extends Base
@@ -1117,7 +1182,7 @@ some cannot.
A JavaScript switch statement. Converts into a returnable expression on-demand.
exports.If = class If extends Base
+ code + @tab + '}'
{defPart}#{resultPart or ''}#{@tab}for (#{forPart}) {#{guardPart}#{varPart}#{body}#{@tab}}#{returnResult or ''}
exports.If = class If extends Base
constructor: (condition, @body, options = {}) ->
@condition = if options.invert then condition.invert() else condition
@elseBody = null
@@ -1127,17 +1192,13 @@ some cannot.
addElse: (elseBody) ->
+ elseBodyNode: -> @elseBody?.unwrap()
A JavaScript switch statement. Converts into a returnable expression on-demand.
addElse: (elseBody) ->
if @isChain
@elseBodyNode().addElse elseBody
else
@isChain = elseBody instanceof If
@elseBody = @ensureExpressions elseBody
- this
If/else statements. Acts as an expression by pushing down requested returns -to the last line of each clause.
- -Single-expression Ifs are compiled into conditional operators if possible, -because ternaries are already proper expressions, and don't need conversion.
isStatement: (o) ->
+ this
isStatement: (o) ->
o?.level is LEVEL_TOP or
@bodyNode().isStatement(o) or @elseBodyNode()?.isStatement(o)
@@ -1150,7 +1211,11 @@ because ternaries are already proper expressions, and don't need conversion.
this
ensureExpressions: (node) ->
- if node instanceof Expressions then node else new Expressions [node]
Rewrite a chain of Ifs to add a default case as the final else.
compileStatement: (o) ->
+ if node instanceof Expressions then node else new Expressions [node]
If/else statements. Acts as an expression by pushing down requested returns +to the last line of each clause.
+ +Single-expression Ifs are compiled into conditional operators if possible, +because ternaries are already proper expressions, and don't need conversion.
compileStatement: (o) ->
child = del o, 'chainChild'
cond = @condition.compile o, LEVEL_PAREN
o.indent += TAB
@@ -1164,8 +1229,7 @@ because ternaries are already proper expressions, and don't need conversion.
o.chainChild = yes
@elseBody.unwrap().compile o, LEVEL_TOP
else
- "{\n#{ @elseBody.compile o, LEVEL_TOP }\n#{@tab}}"
The If only compiles into a statement if either of its bodies needs -to be a statement. Otherwise a conditional operator is safe.
compileExpression: (o) ->
+ "{\n#{ @elseBody.compile o, LEVEL_TOP }\n#{@tab}}"
Rewrite a chain of Ifs to add a default case as the final else.
compileExpression: (o) ->
cond = @condition.compile o, LEVEL_COND
body = @bodyNode().compile o, LEVEL_LIST
alt = if @elseBodyNode() then @elseBodyNode().compile(o, LEVEL_LIST) else 'void 0'
@@ -1173,16 +1237,16 @@ to be a statement. Otherwise a conditional operator is safe. if o.level >= LEVEL_COND then "(#{code})" else code
unfoldSoak: ->
- @soak and this
Compile the If as a regular if-else statement. Flattened chains -force inner else bodies into statement form.
Compile the If as a conditional operator.
Faux-nodes are never created by the grammar, but are used during code -generation to generate other combinations of nodes.
Push =
+ @soak and this
The If only compiles into a statement if either of its bodies needs +to be a statement. Otherwise a conditional operator is safe.
Compile the If as a regular if-else statement. Flattened chains +force inner else bodies into statement form.
Compile the If as a conditional operator.
Push =
wrap: (name, exps) ->
return exps if exps.isEmpty() or last(exps.expressions).containsPureStatement()
- exps.push new Call \
- new Value(new Literal(name), [new Accessor new Literal 'push']), [exps.pop()]
The Push creates the tree for array.push(value)
,
-which is helpful for recording the result arrays from comprehensions.
Closure =
wrap: (expressions, statement, noReturn) ->
+ exps.push new Call new Value(new Literal(name), [new Access new Literal 'push']), [exps.pop()]
Faux-nodes are never created by the grammar, but are used during code +generation to generate other combinations of nodes.
Closure =
The Push creates the tree for array.push(value)
,
+which is helpful for recording the result arrays from comprehensions.
wrap: (expressions, statement, noReturn) ->
return expressions if expressions.containsPureStatement()
func = new Parens new Code [], Expressions.wrap [expressions]
args = []
@@ -1191,51 +1255,50 @@ which is helpful for recording the result arrays from comprehensions.
meth = new Literal if mentionsArgs then 'apply' else 'call'
args = [new Literal 'this']
args.push new Literal 'arguments' if mentionsArgs
- func = new Value func, [new Accessor meth]
+ func = new Value func, [new Access meth]
func.noReturn = noReturn
call = new Call func, args
if statement then Expressions.wrap [call] else call
literalArgs: (node) -> node instanceof Literal and node.value is 'arguments'
literalThis: (node) -> node instanceof Literal and node.value is 'this' or
- node instanceof Code and node.bound
A faux-node used to wrap an expressions body in a closure.
unfoldSoak = (o, parent, name) ->
+ node instanceof Code and node.bound
unfoldSoak = (o, parent, name) ->
return unless ifn = parent[name].unfoldSoak o
parent[name] = ifn.body
ifn.body = new Value parent
- ifn
Wrap the expressions body, unless it contains a pure statement, + ifn
A faux-node used to wrap an expressions body in a closure.
UTILITIES =
Wrap the expressions body, unless it contains a pure statement,
in which case, no dice. If the body mentions this
or arguments
,
-then make sure that the closure wrapper preserves the original values.
UTILITIES =
Unfold a node's child if soak, then tuck the node under created If
extends: '''
+then make sure that the closure wrapper preserves the original values.
extends: '''
function(child, parent) {
+ for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
- for (var key in parent) if (__hasProp.call(parent, key)) child[key] = parent[key];
child.__super__ = parent.prototype;
return child;
}
- '''
bind: '''
+ '''
Unfold a node's child if soak, then tuck the node under created If
bind: '''
function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
- '''
Correctly set up a prototype chain for inheritance, including a reference
-to the superclass for super()
calls, and copies of any static properties.
indexOf: '''
+ '''
indexOf: '''
Array.prototype.indexOf || function(item) {
for (var i = 0, l = this.length; i < l; i++) {
if (this[i] === item) return i;
}
return -1;
}
- '''
Create a function bound to the current value of "this".
hasProp: 'Object.prototype.hasOwnProperty'
- slice : 'Array.prototype.slice'
Discover if an item is in an array.
LEVEL_TOP = 1 # ...;
+ '''
Correctly set up a prototype chain for inheritance, including a reference
+to the superclass for super()
calls, and copies of any static properties.
hasProp: 'Object.prototype.hasOwnProperty'
+ slice : 'Array.prototype.slice'
Create a function bound to the current value of "this".
LEVEL_TOP = 1 # ...;
LEVEL_PAREN = 2 # (...)
LEVEL_LIST = 3 # [...]
LEVEL_COND = 4 # ... ? x : y
LEVEL_OP = 5 # !...
-LEVEL_ACCESS = 6 # ...[0]
Shortcuts to speed up the lookup time for native functions.
TAB = ' '
Levels indicates a node's position in the AST. Useful for knowing if -parens are necessary or superfluous.
TRAILING_WHITESPACE = /[ \t]+$/gm
+LEVEL_ACCESS = 6 # ...[0]
Discover if an item is in an array.
TAB = ' '
Shortcuts to speed up the lookup time for native functions.
TRAILING_WHITESPACE = /[ \t]+$/gm
IDENTIFIER = /^[$A-Za-z_][$\w]*$/
-NUMBER = /// ^ -? (?: 0x[\da-f]+ | (?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)? ) $ ///i
-SIMPLENUM = /^[+-]?\d+$/
Tabs are two spaces for pretty printing.
IS_STRING = /^['"]/
Trim out all trailing whitespace, so that the generated code plays nice -with Git.
Is a literal value a string?
utility = (name) ->
+SIMPLENUM = /^[+-]?\d+$/
Levels indicates a node's position in the AST. Useful for knowing if +parens are necessary or superfluous.
IS_STRING = /^['"]/
Tabs are two spaces for pretty printing.
Trim out all trailing whitespace, so that the generated code plays nice +with Git.
utility = (name) ->
ref = "__#{name}"
Scope.root.assign ref, UTILITIES[name]
ref
@@ -1243,4 +1306,4 @@ with Git.
undefined
Helper for ensuring that utility functions are assigned at the top level.
undefined