removing the special case for | or

This commit is contained in:
Jeremy Ashkenas 2010-03-27 10:28:08 -04:00
parent 7ee10e06be
commit 9763839ed1
6 changed files with 127 additions and 127 deletions

View File

@ -276,8 +276,6 @@
// operations.
Pipe: [o("Expression | Expression", function() {
return new PipeNode($1, $3);
}), o("Expression | || Expression", function() {
return new PipeNode($1, $4, true);
})
],
// The three flavors of function call: normal, object instantiation with `new`,

View File

@ -1715,19 +1715,21 @@
// Unix-like piping. Allows chaining of function calls where every succeeding
// call receives as first argument the result of all preceding expressions.
exports.PipeNode = (function() {
PipeNode = function PipeNode(left, right, fallback) {
PipeNode = function PipeNode(left, right) {
this.children = [(this.left = left), (this.right = right)];
this.fallback = fallback;
return this;
};
__extends(PipeNode, BaseNode);
PipeNode.prototype.type = 'Pipe';
PipeNode.prototype.compile_bitwise_or = function compile_bitwise_or(o) {
return new OpNode('|', this.left, this.right).compile(o);
};
PipeNode.prototype.compile_node = function compile_node(o) {
if (this.right instanceof CallNode && !(this.right.is_new || this.right.is_super)) {
this.right.args.unshift(this.left);
return this.right.compile(o);
if (!(this.right instanceof CallNode && !(this.right.is_new || this.right.is_super))) {
return compile_bitwise_or(o);
}
return new OpNode((this.fallback && this.left instanceof PipeNode ? '||' : '|'), this.left, this.right).compile(o);
this.right.args.unshift(this.left);
return this.right.compile(o);
};
return PipeNode;
}).call(this);

File diff suppressed because one or more lines are too long

View File

@ -264,7 +264,6 @@ grammar: {
# operations.
Pipe: [
o "Expression | Expression", -> new PipeNode $1, $3
o "Expression | || Expression", -> new PipeNode $1, $4, true
]
# The three flavors of function call: normal, object instantiation with `new`,

View File

@ -1284,15 +1284,16 @@ exports.IfNode: class IfNode extends BaseNode
exports.PipeNode: class PipeNode extends BaseNode
type: 'Pipe'
constructor: (left, right, fallback) ->
constructor: (left, right) ->
@children: [@left: left, @right: right]
@fallback: fallback
compile_bitwise_or: (o) ->
new OpNode('|', @left, @right).compile o
compile_node: (o) ->
if @right instanceof CallNode and not (@right.is_new or @right.is_super)
@right.args.unshift @left
return @right.compile o
new OpNode((if @fallback and @left instanceof PipeNode then '||' else '|'), @left, @right).compile o
return compile_bitwise_or o unless @right instanceof CallNode and not (@right.is_new or @right.is_super)
@right.args.unshift @left
@right.compile o
# Faux-Nodes
# ----------

View File

@ -1,7 +1,7 @@
map: (list, fn) -> fn item for item in list
map: (list, fn) -> fn item for item in list
first: (list, fn) -> (return item) for item in list when fn item
add: (x, y) -> x + y
add: (x, y) -> x + y
times: (x, y) -> x * y
y: 1 | add(8) | times(8)
@ -10,6 +10,7 @@ z: [1, 2, 3, 4] | map((x) -> x * x) | first (x) -> x > 10
ok y is 8 * (8 + 1)
ok z is 16
grep: (list, regex) ->
matches: item for item in list when item.match regex
if matches.length then matches else null
@ -20,15 +21,16 @@ deepEqual results, ['Hello', 'How do you do?']
reversed: [1, 2, 3, 4] | Array::reverse.call()
deepEqual reversed, [4, 3, 2, 1]
results: ['a', 'b', 'c', 'd'] | grep(/\d/) | or 'GREP empty.'
results: ['a', 'b', 'c', 'd'] | grep(/\d/) or 'GREP empty.'
ok results is 'GREP empty.'
y: 1 | add(8) | times(0) | or 2 | times 2
y: 1 | add(8) | times(0) or 2 | times 2
ok y is 4
z: [1, 2, 3, 4] |
map((x) -> x * x) |
Array::reverse.call() |
or [1, 2, 3, 4] |
first (x) -> x > 10
ok z is 16