From f1b12a26e5b7a1fae24bad1b918c8cb4c94dfe49 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Fri, 16 May 2014 20:43:58 -0400 Subject: [PATCH 1/4] Better error messages for type application errors where something that was not a type operator, type variable, or type application is used --- desugar.js | 11 +++++++++-- errors.js | 3 +-- example.jl | 2 +- parse.js | 3 ++- representation.js | 35 ++++++++++++++++++++++++++++------- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/desugar.js b/desugar.js index 020cc7f..d0a2aa0 100644 --- a/desugar.js +++ b/desugar.js @@ -57,6 +57,8 @@ function sugarTypeApp(stx) { function desugar(stx) { + var typeExpTest; + switch (stx.exprType) { case "If": if (stx.elseexp) { @@ -76,8 +78,13 @@ function desugar(stx) { * In this case we actually *add* syntax here to differentiate type applications * from normal applications */ - if (!typ.isTypeExpr(stx.p)) { - throw errors.JInternalError("Type application error near line " + stx.linenum + " at character #"+stx.charnum); + typeExpTest = typ.isTypeExpr(stx.p); + + if (typeExpTest.failed !== undefined && + typeExpTest.failed) { + throw errors.JInternalError( + "Type application error near line " + stx.linenum + " at character #"+stx.charnum + + "\n"+typeExpTest.stx.exprType+" (" + typeExpTest.stx.val + ") found where a type operator or type application was expected"); } return sugarTypeApp(stx); } diff --git a/errors.js b/errors.js index 0eebc6a..466d800 100644 --- a/errors.js +++ b/errors.js @@ -10,7 +10,7 @@ function JSyntaxError(linenum, charnum, message) { this.charnum = charnum; this.errormessage = message; this.stxerror = function() { - console.log("Syntax Error\n", + console.log("Syntax Error\n", "Line #", this.linenum-2,"\n", "Near character #", this.charnum, "\n", this.errormessage); @@ -28,7 +28,6 @@ function JTypeError(linenum, charnum, token, message) { function JInternalError(message) { this.errormessage = message; - console.log(message); return this; } diff --git a/example.jl b/example.jl index 3f2c11b..3ed8bd3 100644 --- a/example.jl +++ b/example.jl @@ -1,7 +1,7 @@ defop 2 Left (a ++ b) (a - b) -(qat :: (T -> 2)) +(qat :: (T -> B -> "wat")) def qat (lambda a b c -> (a + b)) def (add a b) diff --git a/parse.js b/parse.js index 139a77f..886507b 100755 --- a/parse.js +++ b/parse.js @@ -701,11 +701,12 @@ function parseFull(tokenized) { } catch (e) { if (e.stxerror !== undefined) { e.stxerror(); + process.exit(1); } else { console.log(e.errormessage); + process.exit(1); } - process.exit(1); } } diff --git a/representation.js b/representation.js index c03af68..64f298b 100644 --- a/representation.js +++ b/representation.js @@ -35,27 +35,48 @@ function isIrregularTypeOp(x) { return (x === "->"); } -function isTypeExprRec(stx, isTypeOp) { +function flattenTypeApp(stx) { if (isTypeExpr(stx)) { - return true; + return stx; } if (stx.exprType === "Application") { /* it might be a type application so recursively check it */ if (stx.p !== undefined) { - return (isTypeExprRec(stx.p) && - isTypeExprRec(stx.func)); + return _.flatten([stx, flattenTypeApp(stx.p), flattenTypeApp(stx.func)]); } else { - return isTypeExprRec(stx.func); + return _.flatten([stx, flattenTypeApp(stx.func)]); } } if (stx.exprType === "Name") { /* Check if it might be a type operator that is not capitalized */ - return isIrregularTypeOp(stx.ident); + if (isIrregularTypeOp(stx.ident)) { + return stx; + } + return { + failed : true, + stx : stx + }; } - return false; + return { + failed : true, + stx : stx + }; } +function isTypeExprRec(stx) { + var flattened = flattenTypeApp(stx); + for(var i = 0; i < flattened.length; i++) { + if (flattened[i].failed !== undefined && + flattened[i].failed) { + return flattened[i]; + } + } + return true; +} + + + function App(func, p) { this.func = func; this.exprType = "Application"; -- 2.30.2 From 4c285539eb2b7eba0e95d1a2476d8a8240fa3fe7 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Fri, 16 May 2014 20:49:06 -0400 Subject: [PATCH 2/4] Removed spurious code from experiments --- representation.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/representation.js b/representation.js index 64f298b..ff536ac 100644 --- a/representation.js +++ b/representation.js @@ -37,26 +37,24 @@ function isIrregularTypeOp(x) { function flattenTypeApp(stx) { if (isTypeExpr(stx)) { - return stx; + return true; } if (stx.exprType === "Application") { /* it might be a type application so recursively check it */ if (stx.p !== undefined) { - return _.flatten([stx, flattenTypeApp(stx.p), flattenTypeApp(stx.func)]); + return _.flatten([flattenTypeApp(stx.p), flattenTypeApp(stx.func)]); } else { - return _.flatten([stx, flattenTypeApp(stx.func)]); + return _.flatten([flattenTypeApp(stx.func)]); } } if (stx.exprType === "Name") { - /* Check if it might be a type operator that is not capitalized */ - if (isIrregularTypeOp(stx.ident)) { - return stx; - } - return { - failed : true, - stx : stx - }; + /* + * Either it is a type operator + * or we assume it is a type variable + * since it was not capitalized + */ + return true; } return { failed : true, @@ -66,6 +64,7 @@ function flattenTypeApp(stx) { function isTypeExprRec(stx) { var flattened = flattenTypeApp(stx); + console.log(flattened); for(var i = 0; i < flattened.length; i++) { if (flattened[i].failed !== undefined && flattened[i].failed) { -- 2.30.2 From 2ddb05930c59cd4463d44fc7e95be057c4a3f991 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sat, 17 May 2014 00:13:42 -0400 Subject: [PATCH 3/4] fix false positive when checking lhs of type declarations for being type decls themselves --- example.jl | 4 +++- representation.js | 7 +++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/example.jl b/example.jl index 3ed8bd3..f16bca0 100644 --- a/example.jl +++ b/example.jl @@ -1,12 +1,14 @@ defop 2 Left (a ++ b) (a - b) -(qat :: (T -> B -> "wat")) +(qat :: A -> B) def qat (lambda a b c -> (a + b)) def (add a b) (a + b) +def wat [[1,2,3], [4,5,6]] + def (catstrs strs) (foldr f (head strs) diff --git a/representation.js b/representation.js index ff536ac..1f51aeb 100644 --- a/representation.js +++ b/representation.js @@ -62,9 +62,9 @@ function flattenTypeApp(stx) { }; } + function isTypeExprRec(stx) { var flattened = flattenTypeApp(stx); - console.log(flattened); for(var i = 0; i < flattened.length; i++) { if (flattened[i].failed !== undefined && flattened[i].failed) { @@ -74,8 +74,6 @@ function isTypeExprRec(stx) { return true; } - - function App(func, p) { this.func = func; this.exprType = "Application"; @@ -270,7 +268,8 @@ function isTypeExpr(expr) { } function TypeApp(expression, type) { - if (isTypeExprRec(expression)) { + if (isTypeExprRec(expression) && + expression.exprType !== "Name") { throw errors.JInternalError( "Left-hand-side of type application must not be in the type language" ); -- 2.30.2 From c6ad6e2b17c782e1a4947fb0a68ae8364d6df0b7 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Sat, 17 May 2014 00:22:44 -0400 Subject: [PATCH 4/4] change the type of error for type decl syntax --- desugar.js | 2 ++ example.jl | 2 +- representation.js | 8 ++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/desugar.js b/desugar.js index d0a2aa0..6489e6b 100644 --- a/desugar.js +++ b/desugar.js @@ -52,6 +52,8 @@ function sugarTypeApp(stx) { var expression; type = stx.p; expression = desugar(stx.func.p); + expression.linenum = stx.linenum; + expression.charnum = stx.charnum; return new typ.TypeApp(expression, type); } diff --git a/example.jl b/example.jl index f16bca0..7d6d07a 100644 --- a/example.jl +++ b/example.jl @@ -1,7 +1,7 @@ defop 2 Left (a ++ b) (a - b) -(qat :: A -> B) +(qat :: A -> b) def qat (lambda a b c -> (a + b)) def (add a b) diff --git a/representation.js b/representation.js index 1f51aeb..da1d72f 100644 --- a/representation.js +++ b/representation.js @@ -24,7 +24,9 @@ var TypeExpression = { console.log("Could not unify " + this.expr + " with " + t.expr); } }, - isTypeExpr : true + isTypeExpr : true, + linenum : 0, + charnum : 0 }; function isTypeExpr(x) { @@ -270,7 +272,9 @@ function isTypeExpr(expr) { function TypeApp(expression, type) { if (isTypeExprRec(expression) && expression.exprType !== "Name") { - throw errors.JInternalError( + throw errors.JSyntaxError( + expression.linenum, + expression.charnum, "Left-hand-side of type application must not be in the type language" ); } -- 2.30.2