Skip to content

Commit

Permalink
closes #151; implemented if-block
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Jan 9, 2013
1 parent 9f81951 commit f2dc5bb
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 79 deletions.
5 changes: 5 additions & 0 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -3030,6 +3030,11 @@ exports.If = If = (function(superclass){
var ref$;
return ((ref$ = this['else']) != null ? ref$[it]() : void 8) && this.then[it]();
});
prototype.maybeUnlessBlock = function(un){
this.un = un;
un && this['else'] instanceof If && this['else'].maybeUnlessBlock(true);
return this;
};
prototype.getJump = function(it){
var ref$;
return this.then.getJump(it) || ((ref$ = this['else']) != null ? ref$.getJump(it) : void 8);
Expand Down
9 changes: 9 additions & 0 deletions lib/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ bnf = {
return L(Fun($3, $6).named($1));
}), o('IF Expression Block Else', function(){
return If($2, $3, $1 === 'unless').addElse($4);
}), o('IF INDENT IfLines DEDENT', function(){
return $3.maybeUnlessBlock($1 === 'unless');
}), o('Expression POST_IF Expression', function(){
return If($3, $1, $2 === 'unless');
}), o('LoopHead Block Else', function(){
Expand Down Expand Up @@ -314,6 +316,13 @@ bnf = {
return new While($2, $1 === 'until', $4);
})
],
IfLines: [
o('Expression Block Else', function(){
return If($1, $2).addElse($3);
}), o('Expression Block NEWLINE IfLines', function(){
return If($1, $2).addElse($4);
})
],
Cases: [
o('CASE Exprs Block', function(){
return [new Case($2, $3)];
Expand Down
146 changes: 76 additions & 70 deletions lib/parser.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/ast.co
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,10 @@ class exports.If extends Node
::delegate <[ isCallable isArray isString isRegex ]> ->
@else?[it]! and @then[it]!

maybeUnlessBlock: (@un) ->
un and @else instanceof If and @else.maybeUnlessBlock true
this

getJump: -> @then.getJump it or @else?getJump it

makeReturn: ->
Expand Down
6 changes: 4 additions & 2 deletions src/command.co
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ default
o.run = not o.compile ||= o.output
process.execPath = argv.0 = argv.1
argv.splice 2 9e9
argv.push ...if o.stdin then $args else
if o.run then $args.splice 1 9e9 else []
argv.push ...if
o.stdin => $args
o.run => $args.splice 1 9e9
else => []
if o.require
({filename} = module)filename = \.
that.forEach require
Expand Down
5 changes: 5 additions & 0 deletions src/grammar.co
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ bnf =

# The full complement of `if` and `unless` expressions
o 'IF Expression Block Else' -> If $2, $3, $1 is \unless .addElse $4
o 'IF INDENT IfLines DEDENT' -> $3.maybeUnlessBlock $1 is \unless
# and their postfix forms.
o 'Expression POST_IF Expression' -> If $3, $1, $2 is \unless

Expand Down Expand Up @@ -301,6 +302,10 @@ bnf =
o 'WHILE Expression' -> new While $2, $1 is \until
o 'WHILE Expression , Expression' -> new While $2, $1 is \until, $4

IfLines:
o 'Expression Block Else' -> If $1, $2 .addElse $3
o 'Expression Block NEWLINE IfLines' -> If $1, $2 .addElse $4

Cases:
o 'CASE Exprs Block' -> [new Case $2, $3]
o 'Cases CASE Exprs Block' -> $1.concat new Case $3, $4
Expand Down
32 changes: 25 additions & 7 deletions test/if.co
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ eq 2, [if 1 then 2 , 3].0
eq 2, [if 0 then 1 else 2, 3].0


# Soaks should not `that`-aware.
a = [0 1]
if 1
eq 1 a?[that]


### `if`-block
if
false
ok false
true
unless
true => ok false
false => ok true
else
ok false

eq 1, if
0 => 0
else 1

# Compile conditonal expression chains neatly.
eq '''
var r;
Expand All @@ -121,8 +142,10 @@ r = a
? d
: e();
''' Coco.compile '''
r = if a then b
else if c then d else e!
r = if
a then b
c then d
else e!
''' {+bare}


Expand All @@ -149,8 +172,3 @@ if 1
5 if that
that if 6?
''', {+bare}

# Soaks should not `that`-aware.
a = [0 1]
if 1
eq 1 a?[that]

0 comments on commit f2dc5bb

Please sign in to comment.