Skip to content

Commit

Permalink
added quick map: for xs => & as short for for x of xs => x (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Nov 19, 2012
1 parent b5ad42d commit e31e877
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 187 deletions.
38 changes: 21 additions & 17 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -2737,6 +2737,9 @@ exports.For = For = (function(superclass){
? idx + " " + '<>'.charAt(pvar < 0) + eq + " " + tvar
: pvar + " < 0 ? " + idx + " >" + eq + " " + tvar + " : " + idx + " <" + eq + " " + tvar;
} else {
if (this.pipe) {
this.item = Var(o.scope.temporary('x'));
}
if (this.item || this.object && this.own) {
ref$ = this.source.compileLoopReference(o, 'ref', !this.object), svar = ref$[0], srcPart = ref$[1];
svar === srcPart || temps.push(svar);
Expand Down Expand Up @@ -2776,7 +2779,9 @@ exports.For = For = (function(superclass){
head += ') {';
this.infuseIIFE();
o.indent += TAB;
if (this.item && !this.item.isEmpty()) {
if (this.pipe) {
this.body = Block((ref$ = Pipe(JS(svar + "[" + idx + "]"), this.body), ref$.ref = this.item.value, ref$.map = true, ref$));
} else if (this.item && !this.item.isEmpty()) {
head += '\n' + o.indent + Assign(this.item, JS(svar + "[" + idx + "]")).compile(o, LEVEL_TOP) + ';';
}
body = this.compileBody(o);
Expand Down Expand Up @@ -3128,19 +3133,20 @@ exports.Label = Label = (function(superclass){
exports.Pipe = Pipe = (function(superclass){
Pipe.displayName = 'Pipe';
var prototype = extend$(Pipe, superclass).prototype, constructor = Pipe;
function Pipe(input, output, cascade, implicit){
this.input = input;
this.output = output;
this.cascade = cascade;
this.implicit = implicit;
}
function Pipe(input, output, prog1){
var this$ = this instanceof ctor$ ? this : new ctor$;
this$.input = input;
this$.output = output;
this$.prog1 = prog1;
return this$;
} function ctor$(){} ctor$.prototype = prototype;
prototype.show = function(){
return this.cascade && '=>';
return this.prog1;
};
prototype.children = ['input', 'output'];
prototype.terminator = '';
prototype.delegate(['isCallable', 'isArray', 'isString', 'isRegex'], function(it){
return this.output[it]();
return this[this.prog1 ? 'input' : 'output'][it]();
});
prototype.getJump = function(it){
return this.output.getJump(it);
Expand All @@ -3150,31 +3156,29 @@ exports.Pipe = Pipe = (function(superclass){
return this;
};
prototype.compileNode = function(o){
var level, input, output, ref, x$, code, out;
var level, input, output, prog1, ref, x$, code, out;
level = o.level;
input = this.input, output = this.output, ref = this.ref;
if (this.cascade && ('ret' in this || level && !this['void'])) {
input = this.input, output = this.output, prog1 = this.prog1, ref = this.ref;
if (prog1 && ('ret' in this || level && !this['void'])) {
output.add((x$ = Literal('&'), x$.cascadee = true, x$));
}
if ('ret' in this) {
output = output.makeReturn(this.ret);
}
if (ref) {
output = Assign(this.cascade
? Arr()
: JS(ref), output);
prog1 || this.map || (output = Assign(JS(ref), output));
} else {
ref = o.scope.temporary('x');
}
if (input instanceof Pipe) {
input.ref = ref;
} else {
input = Assign(JS(ref), input);
input && (input = Assign(JS(ref), input));
}
o.level && (o.level = LEVEL_PAREN);
code = input.compile(o);
out = Block(output).compile((o.ref = new String(ref), o));
if (this.implicit && !o.ref.erred) {
if (prog1 === 'cascade' && !o.ref.erred) {
this.carp("unreferred cascadee");
}
if (!level) {
Expand Down
12 changes: 9 additions & 3 deletions lib/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ bnf = {
}), o('LET CALL( ArgList OptComma )CALL Block', function(){
return Chain(Call['let']($3, $6));
}), o('WITH Expression Block', function(){
return Chain(new Pipe($2, $3, true));
return Chain(Pipe($2, $3, 'with'));
}), o('FOR Expression Block', function(){
return Chain(new For({
source: $2,
body: $3,
pipe: true
}));
})
],
List: [
Expand Down Expand Up @@ -77,7 +83,7 @@ bnf = {
],
Line: [
o('Expression'), o('Expression Block', function(){
return new Pipe($1, $2, true, true);
return Pipe($1, $2, 'cascade');
}), o('PARAM( ArgList OptComma )PARAM <- Expression', function(){
return Call.back($2, $6, $5 === '<~');
}), o('COMMENT', function(){
Expand Down Expand Up @@ -121,7 +127,7 @@ bnf = {
? Binary($2.slice(1), $1, $3).invert()
: Binary($2, $1, $3);
}), o('Expression |> Expression', function(){
return new Pipe($1, $3);
return Pipe($1, $3);
}), o('Chain !?', function(){
return Existence($1.unwrap(), true);
}), o('PARAM( ArgList OptComma )PARAM -> Block', function(){
Expand Down
Loading

0 comments on commit e31e877

Please sign in to comment.