diff --git a/parse.js b/parse.js index e993f36..780f9d7 100755 --- a/parse.js +++ b/parse.js @@ -377,7 +377,7 @@ function parseDataType(tokens, linenum, charnum) { } tokens.pop(); typeBody = parse(tokens); - result = addSrcPos(new typ.DataType(parameters, typeBody), tokens, typeBody.linenum, typeBody.charnum); + result = addSrcPos(new typ.DataType(typeName, parameters, typeBody), tokens, typeBody.linenum, typeBody.charnum); return result; } diff --git a/pprint.js b/pprint.js index da57019..6986b0c 100644 --- a/pprint.js +++ b/pprint.js @@ -30,6 +30,10 @@ function pprintDefType(stx) { return pprint(stx.lhs) + " = " + pprint(stx.rhs); } +function pprintTypeFunc(stx) { + return "(" + stx.name.name + " " + stx.params.map(pprint).join(" ") + ") = " + pprint(stx.type); +} + function pprint(expr) { if (expr.exprType === "Name") { return expr.val; @@ -63,6 +67,9 @@ function pprint(expr) { else if (expr.exprType === "TypeDefinition") { return pprintDefType(expr); } + else if (expr.exprType === "TypeFuncDefinition") { + return pprintTypeFunc(expr); + } else if (expr.exprType === "If") { return pprintIf(expr); } diff --git a/representation.js b/representation.js index ea87fec..2b83669 100644 --- a/representation.js +++ b/representation.js @@ -319,10 +319,17 @@ function checkName(exp) { } } -function DataType(params, type) { +function DataType(name, params, type) { /* Params is a list of type variables * type is a type expression */ + if (name.exprType !== "TypeOperator") { + throw errors.JSyntaxError( + name.linenum, + name.charnum, + "First element in a data type definition must be its name " + + "which is a type operator"); + } _.each(params, checkName); if (!isTypeExprRec(type)) { throw errors.JSyntaxError( @@ -330,6 +337,7 @@ function DataType(params, type) { type.charnum, "Body of a type definition must be a valid type expression"); } + this.name = name; this.params = params; this.type = type; this.exprType = "TypeFuncDefinition";