Browse Source

continuing syntax support for type declarations

pull/8/head
nisstyre56 11 years ago
parent
commit
8167fcf1ba
  1. 18
      desugar.js
  2. 5
      example.jl
  3. 1
      parse.js
  4. 12
      pprint.js
  5. 16
      representation.js

18
desugar.js

@ -42,6 +42,15 @@ function desugarLet(stx) {
return new typ.LetExp(values, desugar(stx.body)); return new typ.LetExp(values, desugar(stx.body));
} }
function sugarTypeApp(stx) {
var type;
var expression;
type = stx.p;
expression = desugar(stx.func.p);
return new typ.TypeApp(expression, type);
}
function desugar(stx) { function desugar(stx) {
switch (stx.exprType) { switch (stx.exprType) {
case "If": case "If":
@ -55,6 +64,15 @@ function desugar(stx) {
case "Name": case "Name":
return stx; return stx;
case "Application": 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
*/
return sugarTypeApp(stx);
}
if ((stx.func.ident === "-" || if ((stx.func.ident === "-" ||
stx.func.ident === "+") && stx.func.ident === "+") &&
stx.p) { stx.p) {

5
example.jl

@ -1,11 +1,8 @@
defop 2 Left (a ++#$ b) defop 2 Left (a ++#$ b)
(a - b) (a - b)
def (f a b)
(a ++ b)
(qat :: A)
(qat :: (Int -> Int -> Int -> Int))
def qat (lambda a b c -> (a + b)) def qat (lambda a b c -> (a + b))
def (add a b) def (add a b)

1
parse.js

@ -458,7 +458,6 @@ function parseLambda(tokens) {
tokens, tokens,
charnum, charnum,
linenum); linenum);
console.log(tokens);
if (fst(tokens)[1] !== "->") { if (fst(tokens)[1] !== "->") {
throw error.JSyntaxError(fst(tokens)[3], throw error.JSyntaxError(fst(tokens)[3],
fst(tokens)[2], fst(tokens)[2],

12
pprint.js

@ -30,9 +30,6 @@ function pprint(expr) {
if (expr.exprType === "Name") { if (expr.exprType === "Name") {
return expr.val; return expr.val;
} }
else if (expr.exprType === "TypeOperator") {
return expr.val;
}
else if (expr.exprType === "Bool") { else if (expr.exprType === "Bool") {
if (expr.val) { if (expr.val) {
return "True"; return "True";
@ -77,6 +74,15 @@ function pprint(expr) {
return pprint(v); return pprint(v);
}).join(" ; ") + "} in " + pprint(expr.body); }).join(" ; ") + "} in " + pprint(expr.body);
} }
else if (expr.exprType === "TypeOperator") {
return "("+expr.val+")";
}
else if (expr.exprType === "TypeVar") {
return "("+expr.name+")";
}
else if (expr.exprType === "TypeApplication") {
return "( " + pprint(expr.expression) + " :: " + pprint(expr.type) + " )";
}
} }
module.exports = {pprint : pprint}; module.exports = {pprint : pprint};

16
representation.js

@ -176,6 +176,7 @@ function If(condition, thenexp, elseexp) {
function TypeVar(name) { function TypeVar(name) {
this.exprtype = "TypeVar"; this.exprtype = "TypeVar";
this.name = name; this.name = name;
this.exprType = "TypeVar";
return this; return this;
} }
@ -188,20 +189,29 @@ function TypeOp(name) {
return this; return this;
} }
function isTypeExpr(expr) {
if (!expr.exprType) {
throw errors.JInternalError(expr);
}
return ((expr.exprType === "TypeOperator") ||
(expr.exprType === "TypeVar") ||
(expr.exprType === "TypeApplication"));
}
TypeOp.prototype = TypeExpression; TypeOp.prototype = TypeExpression;
function TypeApp(expression, type) { function TypeApp(expression, type) {
if (expression.prototype.isTypeExpr) { if (isTypeExpr(expression)) {
throw errors.JInternalError( throw errors.JInternalError(
"Left-hand-side of type application must not be in the type language" "Left-hand-side of type application must not be in the type language"
); );
} }
if (!type.prototype.isTypeExpr) { if (!isTypeExpr(type)) {
throw errors.JInternalError( throw errors.JInternalError(
"Right-hand-side of type application must be a type expression" "Right-hand-side of type application must be a type expression"
); );
} }
this.expr = expression; this.expression = expression;
this.type = type; this.type = type;
this.exprType = "TypeApplication"; this.exprType = "TypeApplication";
return this; return this;

Loading…
Cancel
Save