Permalink
Browse files

Started adding do notation to hiji

  • Loading branch information...
Mikael Bung
Mikael Bung committed May 25, 2010
1 parent 76d0b16 commit 90f040e334a031edda15a91860ff28ad4c8d40a3
Showing with 27 additions and 56 deletions.
  1. +13 −51 haskell.hiji.js
  2. +11 −4 haskell.parser.js
  3. +2 −1 haskell.primitives.js
  4. +1 −0 hiji.html
View
@@ -24,7 +24,13 @@ commands[":type"] = "TYPE";
return "Syntax Error";
}
console.log("%o", ast);
- return haskell.interpreter.eval(ast, env);
+ if (ast.type == "DoExpr") {
+ ast = new haskell.ast.DoExpr(new haskell.ast.Application(new haskell.ast.VariableLookup("hijiOutputLine#"), ast.expr));
+ }
+ var doexpr = new haskell.ast.Do([ast,
+ new haskell.ast.DoExpr(new haskell.ast.VariableLookup("hijiContinuation#"))
+ ]);
+ return haskell.interpreter.eval(doexpr, env);
};
var makeModules = function(modules){
return "<ul class='modules'><li>" + modules.join("</li><li>") + "</li></ul>";
@@ -64,6 +70,7 @@ commands[":type"] = "TYPE";
var env = new haskell.interpreter.RootEnv();
haskell.primitives.init(env);
+ haskell.primitives.initHiji(env);
load_module('hs/Prelude.hs');
@@ -99,57 +106,12 @@ commands[":type"] = "TYPE";
{
try {
var newLine = makeEntered(modules, line);
+ // Global variable:
+ printArea = $("ol", this);
+ env = evaluateHaskell(line, env);
- var showResult = function(result) {
- if (result.type == "Data") {
- var str = result.identifier;
- var op = " ";
-
- if (str == "I#") {
- str = "";
- } else if (str == ":") {
- str = "";
- op = ",";
- }
-
- if (result.ptrs) {
- var first = true;
- for (var i = 0; i < result.ptrs.length; i++) {
- if (str.length == 0 && first) {
- str = showResult(result.ptrs[i].dereference());
- if (typeof str.str != "undefined")
- str = str.str;
- first = false;
- } else {
- var res = showResult(result.ptrs[i].dereference());
- if (typeof res.str != "undefined")
- res = res.str;
- str = str + op + res;
- }
- }
- }
-
- return { str: str, isList: op == "," };
- } if (result.force) {
- return result.force();
- } else if (result.ptrs) {
- return result.ptrs[0].dereference();
- } else {
- return result;
- }
- }
-
- var result = showResult(evaluateHaskell(line, env));
- if (result.isList) {
- result = result.str;
- result = result.substring(0, result.length - 3);
- result = "[" + result + "]";
- } else if (typeof result.str != "undefined") {
- result = result.str;
- }
-
- var output = makeOutput(result);
- $('.input', this).after(output).replaceWith(newLine);
+ // var output = makeOutput(result);
+ // $('.input', this).after(output).replaceWith(newLine);
$("ol",this).append(makeInput(modules));
}
catch(e) {
View
@@ -283,6 +283,8 @@
// Redefined later with the proper definition
var decls = function(state) { return decls(state); };
+ var exp = function(state) { return exp(state); };
+
var stmt_exp_action = function(p) {
return action(p, function(ast) {
return new haskell.ast.DoExpr(ast);
@@ -303,8 +305,8 @@
var infixexp = function(state) { return infixexp(state); };
- var stmt = choice( stmt_bind_action(sequence(ws(pat), expect(ws("<-")), ws(infixexp))),
- stmt_exp_action(ws(infixexp)),
+ var stmt = choice( stmt_bind_action(sequence(ws(pat), expect(ws("<-")), ws(exp))),
+ stmt_exp_action(ws(exp)),
stmt_let_action(sequence(expect(ws("let")), ws(decls)))
);
@@ -319,7 +321,6 @@
});
};
- var exp = function(state) { return exp(state); };
var alt = sequence(ws(pat), expect(ws("->")), ws(exp));
@@ -1089,7 +1090,13 @@
toplevel_exp = action(toplevel_exp, function(ast) {
return ast[0];
});
- var program = action(sequence(choice(toplevel_exp, module), ws(end_p)), function(ast) { return ast[0]; });
+
+ var toplevel_stmt = choice(sequence(expect(ws('{')), ws(stmt), expect(ws('}'))), stmt);
+ toplevel_stmt = action(toplevel_stmt, function(ast) {
+ return ast[0];
+ });
+
+ var program = action(sequence(choice(toplevel_stmt, module), ws(end_p)), function(ast) { return ast[0]; });
// Pragma macro parser
var pragmaId = join_action(repeat1(negate(choice('\t', ' ', '\r', '\n', "#-}"))), "");
View
@@ -195,7 +195,7 @@
return new interpreter.HeapPtr(new interpreter.Closure(env, expr));
};
- function createPrimitive(env, numArgs, func) {
+ var createPrimitive = function(env, numArgs, func) {
if (numArgs.length != undefined) {
numArgs = numArgs.length; // KLUDGE: createPrimitive should take a number instead of an argument list
}
@@ -208,6 +208,7 @@
};
return parameterCollector(env, numArgs, prim);
};
+ primitives.createPrimitive = createPrimitive;
function createDataConstructor(env, ident, num) {
View
@@ -11,6 +11,7 @@
<script src="haskell.ast.js" type="text/javascript"></script>
<script src="haskell.parser.js" type="text/javascript"></script>
<script src="haskell.primitives.js" type="text/javascript"></script>
+ <script src="haskell.hiji.primitives.js" type="text/javascript"></script>
<script src="haskell.interpreter.js" type="text/javascript"></script>
<script src="lib/jquery.js" type="text/javascript"></script>
<script src="haskell.hiji.js" type="text/javascript"></script>

0 comments on commit 90f040e

Please sign in to comment.