Skip to content

Commit

Permalink
closes #72; implemented cascade
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Jul 26, 2012
1 parent 7c5176d commit f921b24
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 112 deletions.
43 changes: 32 additions & 11 deletions lib/ast.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Node, Negatable, Block, Atom, Literal, Var, Key, Index, Chain, Call, List, Obj, Prop, Arr, Unary, Binary, Assign, Import, Of, Existence, Fun, Class, Super, Parens, Splat, Jump, Throw, Return, While, For, Try, Switch, Case, If, Label, JS, Util, Vars, DECLS, ref$, UTILS, LEVEL_TOP, LEVEL_PAREN, LEVEL_LIST, LEVEL_COND, LEVEL_OP, LEVEL_CALL, PREC, TAB, ID, SIMPLENUM, slice$ = [].slice;
var Node, Negatable, Block, Atom, Literal, Var, Key, Index, Chain, Call, List, Obj, Prop, Arr, Unary, Binary, Assign, Import, Of, Existence, Fun, Class, Super, Parens, Splat, Jump, Throw, Return, While, For, Try, Switch, Case, If, Label, Cascade, JS, Util, Vars, DECLS, ref$, UTILS, LEVEL_TOP, LEVEL_PAREN, LEVEL_LIST, LEVEL_COND, LEVEL_OP, LEVEL_CALL, PREC, TAB, ID, SIMPLENUM, slice$ = [].slice;
(Node = function(){
throw Error('unimplemented');
}).prototype = {
Expand Down Expand Up @@ -468,18 +468,12 @@ exports.Literal = Literal = (function(superclass){
return this$;
} function ctor$(){} ctor$.prototype = prototype;
prototype.isEmpty = function(){
switch (this.value) {
case 'void':
case 'null':
return true;
}
var ref$;
return (ref$ = this.value) === 'null' || ref$ === 'void';
};
prototype.isCallable = function(){
switch (this.value) {
case 'this':
case 'eval':
return true;
}
var ref$;
return (ref$ = this.value) === 'this' || ref$ === 'eval' || ref$ === '&';
};
prototype.isString = function(){
return 0 <= '\'"'.indexOf((this.value + "").charAt());
Expand Down Expand Up @@ -521,6 +515,11 @@ exports.Literal = Literal = (function(superclass){
break;
case '*':
this.carp('stray star');
break;
case '&':
if (!(val = o.cascadee)) {
this.carp('stray cascadee');
}
}
return val;
};
Expand Down Expand Up @@ -3112,6 +3111,28 @@ exports.Label = Label = (function(superclass){
};
return Label;
}(Node));
exports.Cascade = Cascade = (function(superclass){
Cascade.displayName = 'Cascade';
var prototype = extend$(Cascade, superclass).prototype, constructor = Cascade;
function Cascade(target, block){
this.target = target;
this.block = block;
}
prototype.children = ['target', 'block'];
prototype.terminator = '';
prototype.compileNode = function(o){
var ref, t, b;
this.temps = [ref = o.scope.temporary('x')];
t = ref + ' = ' + this.target.compile(o, LEVEL_LIST);
b = this.block.compile((o.cascadee = ref, o));
if (o.level) {
return "(" + t + ", " + b + ")";
} else {
return t + ";\n" + b;
}
};
return Cascade;
}(Node));
exports.JS = JS = (function(superclass){
JS.displayName = 'JS';
var prototype = extend$(JS, superclass).prototype, constructor = JS;
Expand Down
4 changes: 3 additions & 1 deletion lib/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ bnf = {
}), o('Lines NEWLINE')
],
Line: [
o('Expression'), o('PARAM( ArgList OptComma )PARAM <- Expression', function(){
o('Expression'), o('Expression Block', function(){
return new Cascade($1, $2);
}), o('PARAM( ArgList OptComma )PARAM <- Expression', function(){
return Call.back($2, $6, $5 === '<~');
}), o('COMMENT', function(){
return L(JS($1, true, true));
Expand Down
13 changes: 9 additions & 4 deletions lib/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,6 @@ exports.doLiteral = function(code, index){
case '<<<<':
tag = 'IMPORT';
break;
case '&':
case '|':
tag = 'BITWISE';
break;
case ';':
tag = 'NEWLINE';
this.wantBy = false;
Expand Down Expand Up @@ -654,6 +650,15 @@ exports.doLiteral = function(code, index){
}
tag = 'UNARY';
break;
case '&':
if (!able(this.tokens)) {
tag = 'LITERAL';
break;
}
// fallthrough
case '|':
tag = 'BITWISE';
break;
case '~':
if (this.dotcat(val)) {
return 1;
Expand Down
Loading

0 comments on commit f921b24

Please sign in to comment.