Browse Source

give a better name to type declarations in the IR

pull/18/head
nisstyre56 10 years ago
parent
commit
05742335bb
  1. 14
      desugar.js
  2. 3
      parse.js
  3. 2
      pprint.js
  4. 22
      representation.js

14
desugar.js

@ -47,14 +47,14 @@ function desugarLet(stx) {
return new typ.LetExp(values, desugar(stx.body));
}
function sugarTypeApp(stx) {
function sugarTypeDecl(stx) {
var type;
var expression;
type = stx.p;
expression = desugar(stx.func.p);
expression.linenum = stx.linenum;
expression.charnum = stx.charnum;
return new typ.TypeApp(expression, type);
return new typ.TypeDecl(expression, type);
}
function desugarDefType(stx) {
@ -86,19 +86,19 @@ function desugar(stx) {
case "Application":
if ((stx.func.func !== undefined ) &&
(stx.func.func.ident === "::")) {
/* 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
/* It's a type declaration probably (will be verified later)
* In this case we actually *add* syntax here to differentiate type declarations
* from normal function application
*/
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 +
"Type declaration 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);
return sugarTypeDecl(stx);
}
if ((stx.func.ident === "-" ||

3
parse.js

@ -801,8 +801,7 @@ function parseFull(tokenized) {
var ast = [];
try {
while (tokenized.length > 0) {
var parsed = desugarer.desugar(parse(tokenized));
ast.push(parsed);
ast.push(desugarer.desugar(parse(tokenized)));
}
return ast;
} catch (e) {

2
pprint.js

@ -94,7 +94,7 @@ function pprint(expr) {
else if (expr.exprType === "TypeVar") {
return "("+expr.name+")";
}
else if (expr.exprType === "TypeApplication") {
else if (expr.exprType === "TypeDeclaration") {
return "( " + pprint(expr.expression) + " :: " + pprint(expr.type) + " )";
}
}

22
representation.js

@ -37,17 +37,17 @@ function isIrregularTypeOp(x) {
return (x === "->");
}
function flattenTypeApp(stx) {
function flattenTypeDecl(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 _.flatten([flattenTypeApp(stx.p), flattenTypeApp(stx.func)]);
return _.flatten([flattenTypeDecl(stx.p), flattenTypeDecl(stx.func)]);
}
else {
return _.flatten([flattenTypeApp(stx.func)]);
return _.flatten([flattenTypeDecl(stx.func)]);
}
}
if (stx.exprType === "Name") {
@ -66,7 +66,7 @@ function flattenTypeApp(stx) {
function isTypeExprRec(stx) {
var flattened = flattenTypeApp(stx);
var flattened = flattenTypeDecl(stx);
for(var i = 0; i < flattened.length; i++) {
if (flattened[i].failed !== undefined &&
flattened[i].failed) {
@ -266,30 +266,30 @@ function isTypeExpr(expr) {
}
return ((expr.exprType === "TypeOperator") ||
(expr.exprType === "TypeVar") ||
(expr.exprType === "TypeApplication"));
(expr.exprType === "TypeDeclaration"));
}
function TypeApp(expression, type) {
function TypeDecl(expression, type) {
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"
"Left-hand-side of type declaration must not be in the type language"
);
}
if (!isTypeExprRec(type)) {
throw errors.JInternalError(
"Right-hand-side of type application must be a type expression"
"Right-hand-side of type declaration must be a type expression"
);
}
this.expression = expression;
this.type = type;
this.exprType = "TypeApplication";
this.exprType = "TypeDeclaration";
return this;
}
TypeApp.prototype = TypeExpression;
TypeDecl.prototype = TypeExpression;
function DefType(lhs, rhs) {
/* Both rhs and lhs are expected
@ -416,7 +416,7 @@ module.exports =
gensym : gensym,
TypeVar : TypeVar,
TypeOp : TypeOp,
TypeApp: TypeApp,
TypeDecl : TypeDecl,
Closure : Closure,
isTypeExpr : isTypeExprRec,
DefType : DefType,

Loading…
Cancel
Save