Skip to content

Commit

Permalink
little cleanup, but that's still SO dirty
Browse files Browse the repository at this point in the history
  • Loading branch information
vendethiel committed Feb 8, 2013
1 parent d3223ce commit 44a0c90
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
13 changes: 8 additions & 5 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -2720,7 +2720,7 @@ exports.While = While = (function(superclass){
}
return code + ret;
};
prototype.setLet = function(){
prototype.makeLet = function(){
throw Error('unimplemented');
};
return While;
Expand All @@ -2742,14 +2742,14 @@ exports.For = For = (function(superclass){
prototype.compileNode = function(o){
var temps, idx, ref$, pvar, step, tvar, tail, fvar, vars, eq, cond, svar, srcPart, lvar, head, that, body;
o.loop = true;
temps = this.temps = [];
if (this['let']) {
this.capture = [];
}
temps = this.temps = [];
if (idx = this.idx = this.index) {
o.scope.declare(idx, this);
} else {
temps.push(idx = this.idx = o.scope.temporary('i'));
temps.push(idx = o.scope.temporary('i'));
}
if (!this.object) {
ref$ = (this.step || Literal(1)).compileLoopReference(o, 'step'), pvar = ref$[0], step = ref$[1];
Expand Down Expand Up @@ -2798,6 +2798,9 @@ exports.For = For = (function(superclass){
head += that + " = true, ";
}
if (this.object) {
if (this['let']) {
this.capture.push(idx);
}
head += srcPart;
} else {
step === pvar || (vars += ', ' + step);
Expand All @@ -2813,7 +2816,7 @@ exports.For = For = (function(superclass){
o.indent += TAB;
if (this.item && !this.item.isEmpty()) {
if (this['let']) {
this.capture = this.item.compile(o, LEVEL_PAREN);
this.capture.push(this.item.compile(o, LEVEL_PAREN));
}
head += '\n' + o.indent + Assign(this.item, JS(svar + "[" + idx + "]")).compile(o, LEVEL_TOP) + ';';
}
Expand All @@ -2826,7 +2829,7 @@ exports.For = For = (function(superclass){
}
return head + body;
};
prototype.setLet = function(){
prototype.makeLet = function(){
this['let'] = true;
return this;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bnf = {
}), o('LET CALL( ArgList OptComma )CALL Block', function(){
return Chain(Call['let']($3, $6));
}), o('LET LoopHead Block', function(){
return $2.setLet().addBody($3);
return $2.makeLet().addBody($3);
}), o('WITH Expression Block', function(){
return Chain(Pipe($2, $3, 'with'));
}), o('FOR Expression Block', function(){
Expand Down
2 changes: 1 addition & 1 deletion lib/parser.js

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

11 changes: 6 additions & 5 deletions src/ast.co
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ class exports.While extends Node
o.scope.free yet
code + ret

setLet: -> ...
makeLet: -> ...

#### For
# Coco's replacements for the `for` loop are array, object or range iterators.
Expand All @@ -1691,11 +1691,11 @@ class exports.For extends While

compileNode: (o) ->
o.loop = true
temps = @temps = []
@capture = [] if @let
temps = @temps = []
if idx = @idx = @index
then o.scope.declare idx, this
else temps.push idx = @idx = o.scope.temporary \i
else temps.push idx = o.scope.temporary \i
unless @object
[pvar, step] = (@step or Literal 1)compileLoopReference o, \step
pvar is step or temps.push pvar
Expand Down Expand Up @@ -1731,6 +1731,7 @@ class exports.For extends While
head += "#idx in " if @object
head += "#that = true, " if @yet
if @object
@capture.push idx if @let
head += srcPart
else
step is pvar or vars += ', ' + step
Expand All @@ -1745,15 +1746,15 @@ class exports.For extends While
@infuseIIFE!
o.indent += TAB
if @item and not @item.isEmpty!
@capture = @item.compile o, LEVEL_PAREN if @let
@capture.push @item.compile o, LEVEL_PAREN if @let
head += \\n + o.indent +
Assign(@item, JS "#svar[#idx]")compile(o, LEVEL_TOP) + \;
o.ref = @item.value if @ref
body = @compileBody o
head += \\n + @tab if @item and \} is body.charAt 0
head + body

setLet: ->
makeLet: ->
@let = true
this

Expand Down
2 changes: 1 addition & 1 deletion src/grammar.co
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bnf =
o 'Chain ?' -> Chain Existence $1.unwrap!

o 'LET CALL( ArgList OptComma )CALL Block' -> Chain Call.let $3, $6
o 'LET LoopHead Block' -> $2.setLet!addBody $3
o 'LET LoopHead Block' -> $2.makeLet!addBody $3

o 'WITH Expression Block' -> Chain Pipe $2, $3, \with
o 'FOR Expression Block' -> Chain new For source: $2, body: $3, ref: true
Expand Down

0 comments on commit 44a0c90

Please sign in to comment.