Skip to content

Commit

Permalink
gave |> a lower precedence and an ability to take block to the right (
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Jan 9, 2013
1 parent 36cb3f2 commit 3a04b80
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 109 deletions.
5 changes: 3 additions & 2 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -3157,17 +3157,18 @@ exports.Pipe = Pipe = (function(superclass){
return this;
};
prototype.compileNode = function(o){
var level, input, output, prog1, ref, x$, code, out;
var level, input, output, prog1, ref, x$, y$, code, out;
level = o.level;
input = this.input, output = this.output, prog1 = this.prog1, ref = this.ref;
output = Block(output).chomp();
if (prog1 && ('ret' in this || level && !this['void'])) {
output.add((x$ = Literal('&'), x$.cascadee = true, x$));
}
if ('ret' in this) {
output = output.makeReturn(this.ret);
}
if (ref) {
prog1 || (output = Assign(Var(ref), output));
prog1 || (y$ = output.lines, y$.push(Assign(Var(ref), y$.pop())));
} else {
ref = o.scope.temporary('x');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ bnf = {
: Binary($2, $1, $3);
}), o('Expression |> Expression', function(){
return Pipe($1, $3);
}), o('Chain !?', function(){
}), o('Expression |> Block', ditto), o('Chain !?', function(){
return Existence($1.unwrap(), true);
}), o('PARAM( ArgList OptComma )PARAM -> Block', function(){
return L(Fun($2, $6, $5 === '~>'));
Expand Down Expand Up @@ -336,7 +336,7 @@ bnf = {
})
]
};
operators = [['left', '|>', 'POST_IF', 'FOR', 'WHILE'], ['right', ',', 'ASSIGN', 'HURL', 'EXTENDS', 'INDENT', 'SWITCH', 'CASE', 'TO', 'BY', 'LABEL'], ['right', 'LOGIC'], ['left', '^', 'BITWISE'], ['right', 'COMPARE'], ['left', 'RELATION'], ['left', 'SHIFT', 'IMPORT'], ['left', '+-'], ['left', 'MATH'], ['right', 'UNARY'], ['nonassoc', 'CREMENT']];
operators = [['left', '|>'], ['left', 'POST_IF', 'FOR', 'WHILE'], ['right', ',', 'ASSIGN', 'HURL', 'EXTENDS', 'INDENT', 'SWITCH', 'CASE', 'TO', 'BY', 'LABEL'], ['right', 'LOGIC'], ['left', '^', 'BITWISE'], ['right', 'COMPARE'], ['left', 'RELATION'], ['left', 'SHIFT', 'IMPORT'], ['left', '+-'], ['left', 'MATH'], ['right', 'UNARY'], ['nonassoc', 'CREMENT']];
tokens = (function(){
var ref$, i$, ref1$, len$, j$, ref2$, len1$, results$ = [];
for (name in ref$ = bnf) {
Expand Down
2 changes: 1 addition & 1 deletion lib/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ exports.doLine = function(code, index){
this.newline();
} else {
tag = last[0], val = last[1];
if (tag === 'ASSIGN' && ((ref$ = val + '') !== '=' && ref$ !== ':=' && ref$ !== '+=') || tag === 'BITWISE' && val !== '&' || (tag === '+-' || tag === '|>' || tag === 'DOT' || tag === 'LOGIC' || tag === 'MATH' || tag === 'COMPARE' || tag === 'RELATION' || tag === 'SHIFT' || tag === 'IN' || tag === 'OF' || tag === 'TO' || tag === 'BY' || tag === 'FROM' || tag === 'EXTENDS' || tag === 'IMPLEMENTS')) {
if (tag === 'ASSIGN' && ((ref$ = val + '') !== '=' && ref$ !== ':=' && ref$ !== '+=') || tag === 'BITWISE' && val !== '&' || (tag === '+-' || tag === 'DOT' || tag === 'LOGIC' || tag === 'MATH' || tag === 'COMPARE' || tag === 'RELATION' || tag === 'SHIFT' || tag === 'IN' || tag === 'OF' || tag === 'TO' || tag === 'BY' || tag === 'FROM' || tag === 'EXTENDS' || tag === 'IMPLEMENTS')) {
return length;
}
if (delta) {
Expand Down
147 changes: 74 additions & 73 deletions lib/parser.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/ast.co
Original file line number Diff line number Diff line change
Expand Up @@ -1938,12 +1938,13 @@ class exports.Pipe extends Node

compileNode: ({level}:o) ->
{input, output, prog1, ref} = this
output = Block output .chomp!
if prog1 and (\ret in this or level and not @void)
output.add (Literal \& => &cascadee = true)
if \ret in this
output.=makeReturn @ret
if ref
then prog1 or output = Assign Var(ref), output
then prog1 or with output.lines => &push Assign Var(ref), &pop!
else ref = o.scope.temporary \x
if input instanceof Pipe
then input <<< {ref}
Expand Down
4 changes: 3 additions & 1 deletion src/grammar.co
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ bnf =
else Binary $2 , $1, $3

o 'Expression |> Expression' -> Pipe $1, $3
o 'Expression |> Block' ditto

o 'Chain !?' -> Existence $1.unwrap!, true

Expand Down Expand Up @@ -319,7 +320,8 @@ bnf =
# and `x = y = z` `x = (y = z)` (not `(x = y) = z`).
operators =
# Listed from lower precedence.
<[ left |> POST_IF FOR WHILE ]>
<[ left |> ]>
<[ left POST_IF FOR WHILE ]>
<[ right , ASSIGN HURL EXTENDS INDENT SWITCH CASE TO BY LABEL ]>
<[ right LOGIC ]>
<[ left ^ BITWISE ]>
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.co
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ exports import
[tag, val] = last
if tag is \ASSIGN and val+'' not of <[ = := += ]>
or tag is \BITWISE and val is not \&
or tag of <[ +- |> DOT LOGIC MATH COMPARE RELATION SHIFT
or tag of <[ +- DOT LOGIC MATH COMPARE RELATION SHIFT
IN OF TO BY FROM EXTENDS IMPLEMENTS ]>
return length
if delta then @indent delta else @newline!
Expand Down
23 changes: 3 additions & 20 deletions test/literal.co
Original file line number Diff line number Diff line change
Expand Up @@ -437,21 +437,10 @@ eq 1, n & -n
compileThrows 'stray reference' 2 '\n&'

#### Pipe
Array 0 |> &concat 1, 2
|> & <<< {3: 4}
|> eq '1,2,,4' "#&"
# => [operator](#operator)

String 0
|> if & then &+& else &*&
|> eq \00 &

eq void,
-> |> & & |> &

funs =
2 |> -> &
3 |> -> &
eq 6, funs.0! * funs.1!
#### Quick Map
# => [loop](#loop)

#### `with`
a = with [2 7 1 8 2]
Expand All @@ -469,9 +458,3 @@ eq
ok &valueOf!

compileThrows 'unreferred cascadee' 1 'a => b'

#### Quick Map
eq \2718,
for [1 6 0 7]
& + 1
.join ''
22 changes: 14 additions & 8 deletions test/loop.co
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,6 @@ continue for, o.count!_ in [1 2]
eq o.i, 4


# [#195](https://github.com/satyr/coco/issues/195)
for [0]
ok 0 in {0}
for [1] then ok 1 of [1]
for [2] => ok 2 of [2]
ok 3 not in []


### Line folding before/after `for` prepositions
for x
in {2}
Expand Down Expand Up @@ -335,3 +327,17 @@ eq 9 r.0
eq i, 0 for , i of [0]
eq v, 1 for , v in {1}
eq v, 2 for own, v in {2}


### Quick Map
eq \2718,
for [1 6 0 7]
& + 1
.join ''

# [#195](https://github.com/satyr/coco/issues/195)
for [0]
ok 0 in {0}
for [1] then ok 1 of [1]
for [2] => ok 2 of [2]
ok 3 not in []
22 changes: 22 additions & 0 deletions test/operator.co
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,28 @@ ok o.2 is o.5 is void
eq '8,9' ''+ -~[7 8]


#### Pipe
Array 0 |> &concat 1, 2
|> & <<< {3: 4}
|> x+5 for x of &
|> eq '6,7,NaN,9' "#&"

String 0
|>
if &
then &+&
else &*&
|> eq \00 &

eq void,
-> |> & & |> &

funs =
2 |> -> &
3 |> -> &
eq 6, funs.0! * funs.1!


### Overloaded
a = b = [0 1]

Expand Down

0 comments on commit 3a04b80

Please sign in to comment.