merge OFTN version #12

Merged
weskerfoot merged 4 commits from master into master 11 years ago
  1. 13
      desugar.js
  2. 1
      errors.js
  3. 4
      example.jl
  4. 3
      parse.js
  5. 43
      representation.js

13
desugar.js

@ -52,11 +52,15 @@ 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);
}
function desugar(stx) {
var typeExpTest;
switch (stx.exprType) {
case "If":
if (stx.elseexp) {
@ -76,8 +80,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;
}

4
example.jl

@ -1,12 +1,14 @@
defop 2 Left (a ++ b)
(a - b)
(qat :: (T -> 2))
(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)

3
parse.js

@ -701,12 +701,13 @@ 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) {

43
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) {
@ -35,25 +37,43 @@ function isIrregularTypeOp(x) {
return (x === "->");
}
function isTypeExprRec(stx, isTypeOp) {
function flattenTypeApp(stx) {
if (isTypeExpr(stx)) {
return true;
}
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([flattenTypeApp(stx.p), flattenTypeApp(stx.func)]);
}
else {
return isTypeExprRec(stx.func);
return _.flatten([flattenTypeApp(stx.func)]);
}
}
if (stx.exprType === "Name") {
/* Check if it might be a type operator that is not capitalized */
return isIrregularTypeOp(stx.ident);
/*
* Either it is a type operator
* or we assume it is a type variable
* since it was not capitalized
*/
return true;
}
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) {
@ -250,8 +270,11 @@ function isTypeExpr(expr) {
}
function TypeApp(expression, type) {
if (isTypeExprRec(expression)) {
throw errors.JInternalError(
if (isTypeExprRec(expression) &&
expression.exprType !== "Name") {
throw errors.JSyntaxError(
expression.linenum,
expression.charnum,
"Left-hand-side of type application must not be in the type language"
);
}

Loading…
Cancel
Save