Skip to content

Commit

Permalink
fixed a deep bug, in which we weren't recursing properly on the the r…
Browse files Browse the repository at this point in the history
…otations. added a little optimization. started on While
  • Loading branch information
maxtaco committed Jan 24, 2012
1 parent f3c82b3 commit 9ba1fe0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
26 changes: 23 additions & 3 deletions lib/coffee-script/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/coffee-script/tame.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 29 additions & 5 deletions src/nodes.coffee
Expand Up @@ -129,7 +129,7 @@ exports.Base = class Base
extras += "C" extras += "C"
if extras.length if extras.length
extras = " (" + extras + ")" extras = " (" + extras + ")"
tree = '\n' + idt + name tree = '\n' + idt + name
tree += '?' if @soak tree += '?' if @soak
tree += extras tree += extras
@eachChild (node) -> tree += node.toString idt + TAB @eachChild (node) -> tree += node.toString idt + TAB
Expand Down Expand Up @@ -173,7 +173,7 @@ exports.Base = class Base
# when considering the children # when considering the children
walkTaming : -> walkTaming : ->
@hasTaming = false @hasTaming = false
for child in @flattenChildren() for child in @flattenChildren()
@hasTaming = true if child.walkTaming() @hasTaming = true if child.walkTaming()
return @hasTaming return @hasTaming


Expand Down Expand Up @@ -276,6 +276,16 @@ exports.Block = class Block extends Base
break break
this this


# Optimization!
# Blocks typically don't need their own cpsCascading. This saves
# wasted code.
compileCps : (o) ->
@gotCpsSplit = true
if @expressions.length > 1
super o
else
@compileNode o

# A **Block** is the only node that can serve as the root. # A **Block** is the only node that can serve as the root.
compile: (o = {}, level) -> compile: (o = {}, level) ->
if o.scope then super o, level else @compileRoot o if o.scope then super o, level else @compileRoot o
Expand Down Expand Up @@ -365,6 +375,7 @@ exports.Block = class Block extends Base


cpsRotate : -> cpsRotate : ->
pivot = null pivot = null
child = null


# If this Block has taming, then we go ahead and look for a pivot # If this Block has taming, then we go ahead and look for a pivot
if @hasTaming if @hasTaming
Expand All @@ -378,6 +389,7 @@ exports.Block = class Block extends Base
# We find a pivot if this node hasTaming, and it's not an Await # We find a pivot if this node hasTaming, and it's not an Await
# itself # itself
if pivot if pivot
console.log("found pvito")
# flood that all children of the pivot need to be CPS-Translated # flood that all children of the pivot need to be CPS-Translated
pivot.floodCpsTranslation() pivot.floodCpsTranslation()
# include the pivot in this slice! # include the pivot in this slice!
Expand All @@ -386,14 +398,22 @@ exports.Block = class Block extends Base
if rest.length if rest.length
child = new Block rest child = new Block rest
pivot.tameNestContinuationBlock child pivot.tameNestContinuationBlock child
# we have to set the taming bit on the new Block
for e in rest
child.hasTaming = true if e.hasTaming
# now recursive apply the transformation to the new child,
# this being especially import in blocks that have multiple
# awaits on the same level
child.cpsRotate()
pivot.callContinuation() pivot.callContinuation()

# After we have pivoted this guy, we still need to walk all of the # After we have pivoted this guy, we still need to walk all of the
# expressions, because maybe the expressions that we left still have # expressions, because maybe the expressions that we left still have
# embedded functions that need the rotation run on them. Thus, # embedded functions that need the rotation run on them. Thus,
# hasTaming will be false, but children might have blocks that still # hasTaming will be false, but children might have blocks that still
# need to be tamed # need to be tamed
super() super()

# return this for chaining # return this for chaining
this this


Expand Down Expand Up @@ -1477,6 +1497,10 @@ exports.While = class While extends Base
return node if node.jumps loop: yes return node if node.jumps loop: yes
no no


callContinuation : ->
k = new Call(new Literal tame.const.k_while, [])
@body.push k

# The main difference from a JavaScript *while* is that the CoffeeScript # 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 # *while* can be used as a part of a larger expression -- while loops may
# return an array containing the computed result of each iteration. # return an array containing the computed result of each iteration.
Expand Down Expand Up @@ -1702,7 +1726,7 @@ exports.Await = class Await extends Base
isStatement: YES isStatement: YES


makeReturn: THIS makeReturn: THIS

compileNode: (o) -> compileNode: (o) ->
o.indent += TAB o.indent += TAB
@body.compile o @body.compile o
Expand Down Expand Up @@ -1990,7 +2014,7 @@ exports.If = class If extends Base
this this


# propogate the closing continuation call down both branches of the if. # propogate the closing continuation call down both branches of the if.
# note this prevents if ...else if... inline chaining, and makes it # note this prevents if ...else if... inline chaining, and makes it
# fully nested if { .. } else { if { } ..} ..'s # fully nested if { .. } else { if { } ..} ..'s
callContinuation : -> callContinuation : ->
code = CALL_CONTINUATION() code = CALL_CONTINUATION()
Expand Down
3 changes: 2 additions & 1 deletion src/tame.coffee
Expand Up @@ -2,7 +2,7 @@
exports.AstTamer = class AstTamer exports.AstTamer = class AstTamer


constructor: (rest...) -> constructor: (rest...) ->

transform: (x) -> transform: (x) ->
x.tameTransform() x.tameTransform()


Expand All @@ -12,3 +12,4 @@ exports.const =
Deferrals : "Deferrals" Deferrals : "Deferrals"
deferrals : "__tame_deferrals" deferrals : "__tame_deferrals"
fulfill : "_fulfill" fulfill : "_fulfill"
k_while : "_kw"

0 comments on commit 9ba1fe0

Please sign in to comment.