Browse Source

Merge pull request #8 from oftn/master

merge OFTN version
pull/21/head
Wesley Kerfoot 11 years ago
parent
commit
61c07701c8
  1. 2
      closure_conversion.js
  2. 21
      desugar.js
  3. 11
      example.jl
  4. 12
      pprint.js
  5. 49
      representation.js

2
closure_conversion.js

@ -151,7 +151,7 @@ function test(src) {
console.log(JSON.stringify(closure_convert_all(ast), null, 4)); console.log(JSON.stringify(closure_convert_all(ast), null, 4));
} }
//console.log(test(pprint.pprint(parser.parse(pprint.pprint(parser.parse("if something then if a then if b then c else d else rtrrt else some_other_thing")[0]))[0]))); //console.log(test("if something then if a then if b then c else d else rtrrt else some_other_thing"));
module.export = { module.export = {
test : test, test : test,
closureConvert : closure_convert_all closureConvert : closure_convert_all

21
desugar.js

@ -26,7 +26,6 @@ function desugarDefFunc(def) {
def.body)); def.body));
} }
function curryFunc(ps, body) { function curryFunc(ps, body) {
if (_.isEmpty(ps)) { if (_.isEmpty(ps)) {
return desugar(body); return desugar(body);
@ -43,6 +42,15 @@ function desugarLet(stx) {
return new typ.LetExp(values, desugar(stx.body)); return new typ.LetExp(values, desugar(stx.body));
} }
function sugarTypeApp(stx) {
var type;
var expression;
type = stx.p;
expression = desugar(stx.func.p);
return new typ.TypeApp(expression, type);
}
function desugar(stx) { function desugar(stx) {
switch (stx.exprType) { switch (stx.exprType) {
case "If": case "If":
@ -56,10 +64,15 @@ function desugar(stx) {
case "Name": case "Name":
return stx; return stx;
case "Application": case "Application":
if (stx.func.ident === "::") { if ((stx.func.func != undefined ) &&
//It's a type binding (stx.func.func.ident === "::")) {
return desugarTypeBinding(stx); /* It's a type application probably (will be verified later)
* In this case we actually *add* syntax here to differentiate type applications
* from normal applications
*/
return sugarTypeApp(stx);
} }
if ((stx.func.ident === "-" || if ((stx.func.ident === "-" ||
stx.func.ident === "+") && stx.func.ident === "+") &&
stx.p) { stx.p) {

11
example.jl

@ -1,8 +1,9 @@
defop 2 Left (a ## b) defop 2 Left (a ++#$ b)
(a - b) (a - b)
def (f a b)
(a ++ b) (qat :: A)
def qat (lambda a b c -> (a + b))
def (add a b) def (add a b)
(a + b) (a + b)
@ -63,6 +64,6 @@ def main
if False if False
then undefined then undefined
else else
(unary >> (unary +
fileLines >> fileLines +
(print splitted)) (print splitted))

12
pprint.js

@ -30,9 +30,6 @@ function pprint(expr) {
if (expr.exprType === "Name") { if (expr.exprType === "Name") {
return expr.val; return expr.val;
} }
else if (expr.exprType === "TypeOperator") {
return expr.val;
}
else if (expr.exprType === "Bool") { else if (expr.exprType === "Bool") {
if (expr.val) { if (expr.val) {
return "True"; return "True";
@ -77,6 +74,15 @@ function pprint(expr) {
return pprint(v); return pprint(v);
}).join(" ; ") + "} in " + pprint(expr.body); }).join(" ; ") + "} in " + pprint(expr.body);
} }
else if (expr.exprType === "TypeOperator") {
return "("+expr.val+")";
}
else if (expr.exprType === "TypeVar") {
return "("+expr.name+")";
}
else if (expr.exprType === "TypeApplication") {
return "( " + pprint(expr.expression) + " :: " + pprint(expr.type) + " )";
}
} }
module.exports = {pprint : pprint}; module.exports = {pprint : pprint};

49
representation.js

@ -41,7 +41,9 @@ function LetExp(pairs, body) {
return (x.exprType === "Definition" || return (x.exprType === "Definition" ||
x.exprType === "FunctionDefinition"); x.exprType === "FunctionDefinition");
})) { })) {
throw "let can only be used to bind things to names or functions"; throw Errors.JInternalError(
"let can only be used to bind things to names or functions"
);
} }
this.exprType = "Let"; this.exprType = "Let";
this.val = [pairs, body]; this.val = [pairs, body];
@ -174,34 +176,48 @@ function If(condition, thenexp, elseexp) {
function TypeVar(name) { function TypeVar(name) {
this.exprtype = "TypeVar"; this.exprtype = "TypeVar";
this.name = name; this.name = name;
this.exprType = "TypeVar";
return this; return this;
} }
TypeVar.prototype = TypeExpression; TypeVar.prototype = TypeExpression;
function TypeOp(name, params, body) { function TypeOp(name) {
if (!_.every(params, _.compose(
_.partial(_.isEqual, "TypeVar"),
_.property("exprtype")))) {
throw errors.JInternalError(
"Parameters to a type operator must be type variables"
);
}
this.name = name; this.name = name;
this.params = params; this.val = name;
this.body = body; this.exprType = "TypeOperator";
return this; return this;
} }
function isTypeExpr(expr) {
if (!expr.exprType) {
throw errors.JInternalError(expr);
}
return ((expr.exprType === "TypeOperator") ||
(expr.exprType === "TypeVar") ||
(expr.exprType === "TypeApplication"));
}
TypeOp.prototype = TypeExpression; TypeOp.prototype = TypeExpression;
function TypeBinding(expression, type) { function TypeApp(expression, type) {
this.expr = expression; if (isTypeExpr(expression)) {
throw errors.JInternalError(
"Left-hand-side of type application must not be in the type language"
);
}
if (!isTypeExpr(type)) {
throw errors.JInternalError(
"Right-hand-side of type application must be a type expression"
);
}
this.expression = expression;
this.type = type; this.type = type;
this.exprType = "TypeApplication";
return this; return this;
} }
TypeBinding.prototype = TypeExpression; TypeApp.prototype = TypeExpression;
//Applies the function ``name'' to the list of parameters //Applies the function ``name'' to the list of parameters
function makeApp(name, parameters) { function makeApp(name, parameters) {
@ -246,7 +262,8 @@ OPInfo = {"+" : [3, "Left"],
">>=" : [1, "Left"], ">>=" : [1, "Left"],
"<$>" : [1, "Left"], "<$>" : [1, "Left"],
"." : [1, "Left"], "." : [1, "Left"],
"," : [1, "Left"]}; "," : [1, "Left"],
"->" : [1, "Right"]};
module.exports = module.exports =
{ {
@ -270,6 +287,6 @@ module.exports =
gensym : gensym, gensym : gensym,
TypeVar : TypeVar, TypeVar : TypeVar,
TypeOp : TypeOp, TypeOp : TypeOp,
TypeBinding : TypeBinding, TypeApp: TypeApp,
Closure : Closure Closure : Closure
}; };

Loading…
Cancel
Save