Browse Source

give a better name to type declarations in the IR

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

3
parse.js

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

2
pprint.js

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

22
representation.js

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

Loading…
Cancel
Save