From 39403648ecca7c2d827f6c0d42005b500add8485 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sun, 18 May 2014 03:36:43 -0400 Subject: [PATCH 1/3] fix a few bugs, functionality for aliasing type operators now works --- parse.js | 7 ++++++- pprint.js | 7 +++++++ representation.js | 11 +++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/parse.js b/parse.js index b3e8607..5e53364 100755 --- a/parse.js +++ b/parse.js @@ -374,7 +374,7 @@ function parseDefType(tokens, linenum, charnum) { return parseDataType(tokens, linenum, charnum); } - if (notFollowedBy(tokens, ["constructor"]. linenum, charnum)) { + if (notFollowedBy(tokens, ["constructor"], linenum, charnum)) { throw error.JSyntaxError(linenum, charnum, "deftype must be followed by a single constructor" + @@ -400,6 +400,8 @@ function parseDefType(tokens, linenum, charnum) { rhs.charnum, "was expecting an application or type operator on the right-hand side of a type definition"); } + result = new typ.DefType(lhs, rhs); + return result; } } @@ -737,6 +739,9 @@ function parse(tokens) { toktype === "let") { return parseDef(tokens, linenum, charnum); } + else if (toktype === "deftype") { + return parseDefType(tokens, linenum, charnum); + } else if (toktype === "defop") { return parseDefOp(tokens, linenum, charnum); } diff --git a/pprint.js b/pprint.js index 305ea8c..da57019 100644 --- a/pprint.js +++ b/pprint.js @@ -26,6 +26,10 @@ function pprintIf(ifexp) { " else " + pprint(ifexp.elseexp) + ")"); } +function pprintDefType(stx) { + return pprint(stx.lhs) + " = " + pprint(stx.rhs); +} + function pprint(expr) { if (expr.exprType === "Name") { return expr.val; @@ -56,6 +60,9 @@ function pprint(expr) { else if (expr.exprType === "Definition") { return pprintDef(expr); } + else if (expr.exprType === "TypeDefinition") { + return pprintDefType(expr); + } else if (expr.exprType === "If") { return pprintIf(expr); } diff --git a/representation.js b/representation.js index c45e237..e0734f9 100644 --- a/representation.js +++ b/representation.js @@ -295,9 +295,9 @@ function DefType(rhs, lhs) { /* Both rhs and lhs are expected * to be fully desugared already */ - if (!isExprType(rhs) || - !isExprType(lhs)) { - throw erros.JSyntaxError( + if (!isTypeExpr(rhs) || + !isTypeExpr(lhs)) { + throw errors.JSyntaxError( rhs.linenum, rhs.charnum, "Illegal type definition, both sides must be valid type expressions"); @@ -308,6 +308,8 @@ function DefType(rhs, lhs) { return this; } +DefType.prototype = Expression; + //Applies the function ``name'' to the list of parameters function makeApp(name, parameters) { @@ -381,5 +383,6 @@ module.exports = TypeOp : TypeOp, TypeApp: TypeApp, Closure : Closure, - isTypeExpr : isTypeExprRec + isTypeExpr : isTypeExprRec, + DefType : DefType }; From 6b33a7e318acb7aaf426bf17d20042e23ae3febf Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sun, 18 May 2014 03:51:26 -0400 Subject: [PATCH 2/3] fix more bugs --- desugar.js | 10 ++++++++++ example.jl | 2 ++ parse.js | 2 +- representation.js | 10 ++++++++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/desugar.js b/desugar.js index 6489e6b..eb5f998 100644 --- a/desugar.js +++ b/desugar.js @@ -57,6 +57,14 @@ function sugarTypeApp(stx) { return new typ.TypeApp(expression, type); } +function desugarDefType(stx) { + var result; + result = new typ.DefType(desugar(stx.lhs), desugar(stx.rhs)); + result.linenum = stx.linenum; + result.charnum = stx.charnum; + return result; +} + function desugar(stx) { var typeExpTest; @@ -71,6 +79,8 @@ function desugar(stx) { return desugarDefFunc(stx); case "Definition": return new typ.Def(stx.ident, desugar(stx.val)); + case "TypeDefinition": + return desugarDefType(stx); case "Name": return stx; case "Application": diff --git a/example.jl b/example.jl index 89edaad..bc9ccae 100644 --- a/example.jl +++ b/example.jl @@ -1,6 +1,8 @@ defop 2 Left (a ++ b) (a - b) +deftype Foo (A -> B) + (qat :: A -> b) def tdeftype (lambda a b c -> (a + b)) diff --git a/parse.js b/parse.js index 5e53364..84c1959 100755 --- a/parse.js +++ b/parse.js @@ -400,7 +400,7 @@ function parseDefType(tokens, linenum, charnum) { rhs.charnum, "was expecting an application or type operator on the right-hand side of a type definition"); } - result = new typ.DefType(lhs, rhs); + result = addSrcPos(new typ.DefType(lhs, rhs), tokens, linenum, charnum); return result; } } diff --git a/representation.js b/representation.js index e0734f9..95ca437 100644 --- a/representation.js +++ b/representation.js @@ -291,11 +291,11 @@ function TypeApp(expression, type) { TypeApp.prototype = TypeExpression; -function DefType(rhs, lhs) { +function DefType(lhs, rhs) { /* Both rhs and lhs are expected * to be fully desugared already */ - if (!isTypeExpr(rhs) || + if (!isTypeExprRec(rhs) || !isTypeExpr(lhs)) { throw errors.JSyntaxError( rhs.linenum, @@ -310,6 +310,12 @@ function DefType(rhs, lhs) { DefType.prototype = Expression; +function DataType(params, type) { + /* Params is a list of type variables + * type is a type expression + */ +} + //Applies the function ``name'' to the list of parameters function makeApp(name, parameters) { From 0eafd96a7f217d91f2767fb7f4caf8704eeb7246 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sun, 18 May 2014 17:20:40 -0400 Subject: [PATCH 3/3] representation for type definitions with parameters --- representation.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/representation.js b/representation.js index 95ca437..bea8317 100644 --- a/representation.js +++ b/representation.js @@ -310,10 +310,30 @@ function DefType(lhs, rhs) { DefType.prototype = Expression; +function checkName(exp) { + if (exp.exprType !== "Name") { + throw errors.JSyntaxError( + exp.linenum, + exp.charnum, + "Expected a type variable (an identifier starting with a lowercase character), got " + exp.val); + } +} + function DataType(params, type) { /* Params is a list of type variables * type is a type expression */ + _.each(params, checkName); + if (!isTypeExprRec(type)) { + throw errors.JSyntaxError( + type.linenum, + type.charnum, + "Body of a type definition must be a valid type expression"); + } + this.params = params; + this.type = type; + this.exprType = "TypeFuncDefinition"; + return this; }