Browse Source

Better error messages for type application errors where something that was not a type operator, type variable, or type application is used

pull/12/head
nisstyre56 11 years ago
parent
commit
f1b12a26e5
  1. 11
      desugar.js
  2. 1
      errors.js
  3. 2
      example.jl
  4. 3
      parse.js
  5. 35
      representation.js

11
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);
}

1
errors.js

@ -28,7 +28,6 @@ function JTypeError(linenum, charnum, token, message) {
function JInternalError(message) {
this.errormessage = message;
console.log(message);
return this;
}

2
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)

3
parse.js

@ -701,13 +701,14 @@ function parseFull(tokenized) {
} catch (e) {
if (e.stxerror !== undefined) {
e.stxerror();
process.exit(1);
}
else {
console.log(e.errormessage);
}
process.exit(1);
}
}
}
module.exports = { parse : function(str) {
return parseFull(tokenizer.tokenize(str));

35
representation.js

@ -35,26 +35,47 @@ 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;

Loading…
Cancel
Save