Clean up and improve PaperScript code a bit.

This commit is contained in:
Jürg Lehni 2012-11-02 15:58:41 -07:00
parent c227d589ca
commit ee65ee1026

View file

@ -37,7 +37,7 @@ var PaperScript = this.PaperScript = new function() {
var handler = operators[operator]; var handler = operators[operator];
if (left && left[handler]) { if (left && left[handler]) {
var res = left[handler](right); var res = left[handler](right);
return operator == '!=' ? !res : res; return operator === '!=' ? !res : res;
} }
switch (operator) { switch (operator) {
case '+': return left + right; case '+': return left + right;
@ -50,7 +50,7 @@ var PaperScript = this.PaperScript = new function() {
default: default:
throw new Error('Implement Operator: ' + operator); throw new Error('Implement Operator: ' + operator);
} }
}; }
// Sign Operators // Sign Operators
@ -109,8 +109,8 @@ var PaperScript = this.PaperScript = new function() {
// Handle simple mathematical operators here: // Handle simple mathematical operators here:
return handleOperator(operator, left = walk(left), return handleOperator(operator, left = walk(left),
right = walk(right)) right = walk(right))
// Always return something since we're walking left and // Always return a new AST for this node, since we have
// right for the handleOperator() call already. // processed left and right int he call above!
|| [this[0], operator, left, right]; || [this[0], operator, left, right];
}, },
@ -121,10 +121,10 @@ var PaperScript = this.PaperScript = new function() {
// of handleOperator on the right hand side. // of handleOperator on the right hand side.
var res = handleOperator(operator, left = walk(left), var res = handleOperator(operator, left = walk(left),
right = walk(right)); right = walk(right));
if (res) return res
return [this[0], true, left, res]; ? [this[0], true, left, res]
// Always return something for the same reason as in binary // Always return a new AST for the same reason as in binary
return [this[0], operator, left, right]; : [this[0], operator, left, right];
}, },
'unary-prefix': function(operator, exp) { 'unary-prefix': function(operator, exp) {