Skip to content

Commit

Permalink
fix monad syntax bug puffnfresh#160
Browse files Browse the repository at this point in the history
  • Loading branch information
raimohanska committed Dec 2, 2012
1 parent 56e06dc commit 2271215
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,11 @@ var compileNodeWithEnv = function(n, env, opts) {
visitBind: function() {
var init = n.rest.slice(0, n.rest.length - 1);
var last = n.rest[n.rest.length - 1];
var compiledLast = last ? compileNode(last) : "";
return "__monad__.bind(" + compileNode(n.value) +
", function(" + n.name + ") {\n" + pushIndent() +
_.map(init, compileNode).join(";\n" + getIndent()) + "\n" +
getIndent() + "return " + compileNode(last) + "\n" +
getIndent() + "return " + compiledLast + "\n" +
popIndent() + "});";
},
visitDo: function() {
Expand Down
4 changes: 3 additions & 1 deletion src/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ var grammar = {
["doBody TERMINATOR", "$$ = $1;"]
],
"doLine": [
["line", "$$ = $1;"],
["statement", "$$ = $1;"],
["expression", n("$$ = new yy.Bind('_', $1);")],
["COMMENT", n("$$ = new yy.Comment($1);")],
["IDENTIFIER LEFTARROW expression", n("$$ = new yy.Bind($1, $3);")],
["RETURN expression", n("$$ = new yy.Return($2);")]
],
Expand Down
3 changes: 3 additions & 0 deletions test/CompileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ describe('compiler', function(){
it('trace_monad.roy with expected output', function() {
expectExecutionToHaveExpectedOutput('good/trace_monad');
});
it('callbackmonad.roy with expected output', function() {
expectExecutionToHaveExpectedOutput('good/callbackmonad');
});
it('unicode.roy with expected output', function() {
expectExecutionToHaveExpectedOutput('good/unicode');
});
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/good/callbackmonad.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
starting
hello
world
24 changes: 24 additions & 0 deletions test/fixtures/good/callbackmonad.roy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let callbacks = {
return: \x ->
\callback -> callback x
bind: \action1 f ->
\callback ->
let callback1 = \x ->
let action2 = f x
action2 callback
action1 callback1
}

let wait seconds = (\callback -> setTimeout callback (seconds * 1000))
let print x = \callback ->
console.log x
callback ()
let nop _ = _

console.log "starting"
let program = do callbacks
_ <- print "hello"
print "world"

program nop

1 change: 1 addition & 0 deletions test/fixtures/good/trace_monad.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Binding: 1
Binding: 3
Binding: 4
Binding: 5
Return: 10
10
1 change: 1 addition & 0 deletions test/fixtures/good/trace_monad.roy
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ console.log (do traceMonad
let x = 2
y ← 3
z ← 4
5
return w + x + y + z
)

0 comments on commit 2271215

Please sign in to comment.