Skip to content

Instantly share code, notes, and snippets.

@StanAngeloff
Created July 9, 2010 16:31
Show Gist options
  • Save StanAngeloff/469684 to your computer and use it in GitHub Desktop.
Save StanAngeloff/469684 to your computer and use it in GitHub Desktop.
diff --git a/src/grammar.coffee b/src/grammar.coffee
index 9602555..d4c70fc 100644
--- a/src/grammar.coffee
+++ b/src/grammar.coffee
@@ -87,6 +87,7 @@ grammar: {
Expression: [
o "Value"
o "Call"
+ o "RedirectedCall"
o "Code"
o "Operation"
o "Assign"
@@ -298,6 +299,12 @@ grammar: {
o "NEW Value", -> (new CallNode($2, [])).newInstance()
]
+ # Unix-like pipe for chained function calls.
+ RedirectedCall: [
+ o "Expression ~ Invocation", -> new RedirectedCallNode $1, $3
+ o "Expression ~ SimpleAssignable", -> new RedirectedCallNode $1, new CallNode $3, []
+ ]
+
# Extending an object by setting its prototype chain to reference a parent
# object.
Extends: [
diff --git a/src/nodes.coffee b/src/nodes.coffee
index 894526d..d4755ad 100644
--- a/src/nodes.coffee
+++ b/src/nodes.coffee
@@ -1380,6 +1380,21 @@ exports.IfNode: class IfNode extends BaseNode
elsePart: if @elseBody then @elseBodyNode().compile(o) else 'null'
"$ifPart : $elsePart"
+#### RedirectedCallNode
+
+# Unix-like piping. Allows chaining of function calls where every succeeding
+# call receives as first argument the result of all preceding expressions.
+exports.RedirectedCallNode: class RedirectedCallNode extends BaseNode
+ class: 'RedirectedCall'
+ children: ['left', 'right']
+
+ constructor: (left, right) ->
+ @children: [@left: left, @right: right]
+
+ compileNode: (o) ->
+ @right.args.unshift @left
+ @right.compile o
+
# Faux-Nodes
# ----------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment