Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished do notation in hiji

  • Loading branch information...
commit fab3fcfc2774a4bf4b2e7d33f45497e80a8d8e81 1 parent b0ef75f
Mikael Bung authored
View
44 haskell.ast.js
@@ -1,4 +1,4 @@
-(function(ast, interpreter, utilities, typechecker) {
+(function(ast, interpreter, utilities) {
expectType = utilities.expectType;
expectTypeOf = utilities.expectTypeOf;
@@ -27,6 +27,42 @@
Import = Import Identifier VisibleNames
| ImportQualified Identifer VisibleNames Identifier
*/
+
+ /*
+ data Type = TypeVariable Identifier
+ | TypeConstructor Identifier
+ | TypeApplication Type Type
+ | TypeTupple Int
+ */
+
+ ast.Type = function() {};
+
+ ast.TypeVariable = function(name) {
+ expectTypeOf(name, "string");
+ this.name = name;
+ };
+ ast.TypeVariable.prototype = new ast.Type();
+
+ ast.TypeConstructor = function(name) {
+ expectTypeOf(name, "string");
+ this.name = name;
+ };
+ ast.TypeConstructor.prototype = new ast.Type();
+
+ ast.TypeApplication = function(t1, t2) {
+ expectType(t1, ast.Type);
+ expectType(t2, ast.Type);
+ this.t1 = t1;
+ this.t2 = t2;
+ };
+ ast.TypeApplication.prototype = new ast.Type();
+
+ ast.TypeTupple = function(size) {
+ expectTypeOf(size, "number");
+ this.size = size;
+ };
+ ast.TypeTupple.prototype = new ast.Type();
+
/*
data Expression = Constant Value
@@ -441,7 +477,7 @@
ast.Data = function(identifier, tvars, constructors) {
expectTypeOf(identifier, "string");
- expectTypeArray(tvars, typechecker.TVar);
+ expectTypeArray(tvars, ast.TypeVariable);
expectTypeArray(constructors, ast.Constructor);
this.type = "Data";
this.identifier = identifier;
@@ -493,7 +529,7 @@
ast.Constructor = function(identifier, types) {
expectTypeOf(identifier, "string");
- expectTypeArray(t, "number");
+ expectTypeArray(types, ast.Type);
this.type = "Constructor";
this.identifier = identifier;
this.types = types;
@@ -608,4 +644,4 @@
ast.Combined.prototype = new ast.Pattern();
ast.ConstantPattern.prototype = new ast.Pattern();
ast.Wildcard.prototype = new ast.Pattern();
-})(haskell.ast,haskell.interpreter, haskell.utilities, haskell.typechecker);
+})(haskell.ast,haskell.interpreter, haskell.utilities);
View
14 haskell.hiji.js
@@ -23,14 +23,18 @@ commands[":type"] = "TYPE";
if (ast == undefined){
return "Syntax Error";
}
- console.log("%o", ast);
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);
+ new haskell.ast.DoExpr(new haskell.ast.Primitive(
+ function(env) {
+ return new haskell.interpreter.Data("IO", [new haskell.interpreter.HeapPtr(env)]);
+ }
+ ))]);
+ console.log("%o", doexpr);
+ var res = haskell.interpreter.eval(doexpr, env);
+ return res.ptrs[0].dereference();
};
var makeModules = function(modules){
return "<ul class='modules'><li>" + modules.join("</li><li>") + "</li></ul>";
@@ -109,7 +113,7 @@ commands[":type"] = "TYPE";
// Global variable:
printArea = $("ol", this);
env = evaluateHaskell(line, env);
-
+ console.log("%o", env);
// var output = makeOutput(result);
// $('.input', this).after(output).replaceWith(newLine);
$("ol",this).append(makeInput(modules));
View
5 haskell.hiji.primitives.js
@@ -41,9 +41,9 @@
primitives.initHiji = function(env) {
- env.bind("hijiContinuation#", primitives.createPrimitive(env, 1,
+ env.bind("hijiContinuation#", primitives.createPrimitive(env, 0,
function(env, args) {
- return env;
+ return new interpreter.Data("IO", [new interpreter.HeapPtr(env)]);
}));
// hijiOutputLine# :: a -> IO ()
@@ -59,6 +59,7 @@
result = result.str;
}
printArea.append($("<li class='output'></li>").text(result.toString()));
+ return new interpreter.Data("IO", [new interpreter.HeapPtr(new interpreter.Closure(env, new ast.VariableLookup("()")))]);
}));
};
})(haskell.primitives, haskell.ast, haskell.interpreter);
View
115 haskell.parser.js
@@ -1,6 +1,6 @@
// The parser
-(function(parser, ast, typechecker) {
+(function(parser, ast) {
parser.lastInternalName = 0;
parser.generateInternalName = function() {
@@ -798,8 +798,14 @@
return action(p, function(ast) {
return ast;
});
+ };
+
+ var fix_sequence_action = function(p) {
+ return action(p, function(ast) {
+ return ast[1];
+ });
};
-
+
var constr = choice(constr_action(sequence(ws(con), repeat0(fix_sequence_action(sequence(optional(ws('!')), ws(atype)))))),
constr_op_action(sequence(choice(ws(btype), sequence(optional(ws('!')), ws(atype))), ws(conop), choice(ws(btype), sequence(optional(ws('!')), ws(atype))))),
constr_fielddecl_action(sequence(ws(con), expectws('{'), list(ws(fielddecl), ws(',')), expectws('}')))
@@ -832,37 +838,109 @@
var scontext = choice( scontext_one_action(simpleclass),
scontext_many_action(sequence(expectws('('), list(ws(simpleclass), ws(',')), expectws(')')))
);
-
- // redefinition
- gtycon = choice(qtycon,
- sequence(repeat1(ws(var_)), repeat0(ws(apat))),
- "()",
- "[]",
- "(->)",
- sequence(ws('('), repeat1(ws(',')), ws(')'))
+
+ var gtycon_qtycon_action = function(p) {
+ return action(p, function(ast) {
+ return new haskell.ast.TypeConstructor(ast);
+ });
+ };
+
+ var gtycon_qtycon_action = function(p) {
+ return action(p, function(ast) {
+ return new haskell.ast.TypeConstructor(ast);
+ });
+ };
+
+ var gtycon_unit_action = function(p) {
+ return action(p, function(ast) {
+ return new haskell.ast.TypeConstructor("()");
+ });
+ };
+
+ var gtycon_list_action = function(p) {
+ return action(p, function(ast) {
+ return new haskell.ast.TypeConstructor("([])");
+ });
+ };
+
+ var gtycon_arrow_action = function(p) {
+ return action(p, function(ast) {
+ return new haskell.ast.TypeConstructor("(->)");
+ });
+ };
+
+ var gtycon_tupple_action = function(p) {
+ return action(p, function(ast) {
+ var size = ast[0].length+1;
+ var constructor = "(" + new Array(size).join(",") + ")";
+ return new haskell.ast.TypeTupple(constructor, size);
+ });
};
+
+ // redefinition
+ gtycon = choice(gtycon_qtycon_action(qtycon),
+ // sequence(repeat1(ws(var_)), repeat0(ws(apat))), // TODO: WTF? A pattern in a type? :/
+ gtycon_unit_action("()"),
+ gtycon_list_action("[]"),
+ gtycon_arrow_action("(->)"),
+ gtycon_tupple_action(sequence(ws('('), repeat1(ws(',')), ws(')')))
);
+
+ var atype_tyvar_action = function(p) {
+ return action(p, function(ast) {
+ return new haskell.ast.TypeVariable(ast);
+ });
};
+
+ var atype_tupple_action = function(p) {
+ return action(p, function(ast) {
+ var size = ast[0].length;
+ var constructor = "(" + new Array(size).join(",") + ")";
+ var type = new haskell.ast.TypeTupple(constructor, size);
+ for (var i in ast[0]) {
+ type = new haskell.ast.TypeApplication(type, ast[0][i]);
+ };
+ return type;
+ });
};
var atype_list_action = function(p) {
- return action(p, function(ast) {
+ return action(p, function(ast) {
return new haskell.ast.TypeApplication(new haskell.ast.TypeConstructor("([])"), ast[0]);
});
};
-
// redefinition
- atype = choice( gtycon,
- tyvar,
+ atype = choice( gtycon,
+ atype_tyvar_action(tyvar),
atype_tupple_action(sequence(expectws('('), list(ws(type), ws(',')), expectws(')'))),
atype_list_action(sequence(expectws('['), ws(type), expectws(']'))),
sequence(expectws('('), ws(type), expectws(')'))
+ );
+
+ var btype_action = function(p) {
+ return action(p, function(ast) {
+ var type = ast[0];
+ for (var i = 1; i < ast.length; i++) {
+ type = new haskell.ast.TypeApplication(type, ast[i]);
+ }
+ return type;
+ });
};
-
+
+ var btype = btype_action(repeat1(ws(atype)));
+
+ var type_action = function(p) {
+ return action(p, function(ast) {
+ var type = ast[0];
+ for (var i = 1; i < ast.length; i++) {
+ type = new haskell.ast.TypeApplication(new haskell.ast.TypeApplication(new haskell.ast.TypeConstructor("(->)"), type), ast[i]);
+ }
+ return type;
+ });
};
- // redefinition
+ // redefinition
type = type_action(list(ws(btype), ws("->")));
var fixity = choice(ws("infixl"), ws("infixr"), ws("infix"));
@@ -996,7 +1074,8 @@
var data_action = function(p) {
return action(p, function(ast) {
var ident = ast[1][0];
- var constructors = ast[2];
+ var constructors = ast[2];
+ // TODO: type variables
return new haskell.ast.Data(ident, [], constructors);
});
};
@@ -1363,4 +1442,4 @@
var result = grammar(ps(contextFree));
return result;
- };
+ };
View
1  hiji.html
@@ -10,6 +10,7 @@
<script src="haskell.utilities.js" type="text/javascript"></script>
<script src="haskell.ast.js" type="text/javascript"></script>
<script src="haskell.parser.js" type="text/javascript"></script>
+ <script src="haskell.typechecker.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>
View
8 hs/Prelude.hs
@@ -3,6 +3,8 @@ module Prelude where
data Bool = True | False
+data IO a = IO a
+
infixl 6 +
infixl 6 -
infixl 7 *
@@ -103,12 +105,12 @@ data Maybe a = Just a | Nothing
concatMap f xs = concat (map f xs)
--- List monad -_-
+-- IO monad -_-
(>>) m a = m >>= (\_ -> a)
-(>>=) m f = concatMap f m
+(>>=) (IO v) f = f v
-return a = [a]
+return a = IO a
fail _ = []

0 comments on commit fab3fcf

Please sign in to comment.
Something went wrong with that request. Please try again.