# HG changeset patch # User Mike Pavone # Date 1343371256 25200 # Node ID 59a94f3ad56f47157ef6c4ea5421a98830b2a5fb # Parent 84b65ee8b78b0c6e8110638ff547f96b3dd95e3d Added short-circuit && and || operators diff -r 84b65ee8b78b -r 59a94f3ad56f cbackend.js --- a/cbackend.js Thu Jul 26 18:54:42 2012 -0700 +++ b/cbackend.js Thu Jul 26 23:40:56 2012 -0700 @@ -13,7 +13,7 @@ } op.prototype.toC = function(isReceiver) { - var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_', '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_', '.': 'CAT_'}; + var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_', '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_', '.': 'CAT_', '&&':'if', '||':'ifnot'}; var method = optoMeth[this.op]; return 'mcall(' + getMethodId(method) + '/* ' + method + ' */, 2, (object *)' + this.left.toC() + ', ' + this.right.toC() + ')\n'; }; diff -r 84b65ee8b78b -r 59a94f3ad56f compiler.js --- a/compiler.js Thu Jul 26 18:54:42 2012 -0700 +++ b/compiler.js Thu Jul 26 23:40:56 2012 -0700 @@ -296,6 +296,12 @@ op.prototype.populateSymbols = function(symbols, isReceiver) { this.left.populateSymbols(symbols); + if (this.op == '&&' || this.op == '||') { + //&& and || are syntactic sugar for if and ifnot with + //the second argument transformed into a lambda to + //achieve short-circuit evalutation + this.right = new lambda([], [this.right]); + } this.right.populateSymbols(symbols); }; diff -r 84b65ee8b78b -r 59a94f3ad56f modules/false.tp --- a/modules/false.tp Thu Jul 26 18:54:42 2012 -0700 +++ b/modules/false.tp Thu Jul 26 23:40:56 2012 -0700 @@ -2,6 +2,9 @@ if <- :self trueblock { self } + ifnot <- :self falseblock { + falseblock: + } if:else <- :self trueblock :elseblock { elseblock: } diff -r 84b65ee8b78b -r 59a94f3ad56f modules/true.tp --- a/modules/true.tp Thu Jul 26 18:54:42 2012 -0700 +++ b/modules/true.tp Thu Jul 26 23:40:56 2012 -0700 @@ -2,6 +2,9 @@ if <- :self trueblock { trueblock: } + ifnot <- :self falseblock { + self + } if:else <- :self trueblock :elseblock { trueblock: } diff -r 84b65ee8b78b -r 59a94f3ad56f samples/logical.tp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/logical.tp Thu Jul 26 23:40:56 2012 -0700 @@ -0,0 +1,28 @@ +#{ + + foo <- { + print: "foo\n" + true + } + + bar <- { + print: "bar\n" + false + } + + baz <- { + print: "baz\n" + true + } + + qux <- { + print: "shouldn't be printed\n" + true + } + + + main <- { + foo && bar || (baz || qux) + } + +}