Browse Source

Merge pull request #16 from oftn/master

merge OFTN version
pull/21/head
Wesley Kerfoot 11 years ago
parent
commit
0aedc418d2
  1. 3
      example.jl
  2. 41
      parse.js
  3. 7
      pprint.js
  4. 13
      representation.js

3
example.jl

@ -3,6 +3,9 @@ defop 2 Left (a ++ b)
deftype Foo (A -> B) deftype Foo (A -> B)
deftype (Foo a b)
(a -> b)
(qat :: A -> b) (qat :: A -> b)
def tdeftype (lambda a b c -> (a + b)) def tdeftype (lambda a b c -> (a + b))

41
parse.js

@ -207,7 +207,7 @@ function parseDefFunction(tokens, linenum, charnum) {
fst(tokens)[2], fst(tokens)[2],
fst(tokens)[3]); fst(tokens)[3]);
} }
if ((fst(tokens)[0]) !== "right_paren") { if (!tokens || (fst(tokens)[0]) !== "right_paren") {
throw error.JSyntaxError(fst(tokens)[3], throw error.JSyntaxError(fst(tokens)[3],
fst(tokens)[2], fst(tokens)[2],
"Formal parameters must be followed by )"); "Formal parameters must be followed by )");
@ -353,6 +353,37 @@ function parseLetItem(tokens) {
} }
} }
function parseDataType(tokens, linenum, charnum) {
var typeName = parse(tokens, linenum, charnum);
var typeParams;
var typeBody;
var result;
if (typeName.exprType !== "TypeOperator") {
throw error.JSyntaxError(typeName.linenum,
typeName.charnum,
"Expected a type operator in data type definition");
}
parameters = parseMany(parse,
validName,
validFormPar,
tokens,
charnum,
linenum);
if (!tokens || (fst(tokens)[0]) !== "right_paren") {
throw error.JSyntaxError(fst(tokens)[3],
fst(tokens)[2],
"Data type parameters must be followed by )");
}
tokens.pop();
typeBody = parse(tokens);
result = addSrcPos(new typ.DataType(typeName, parameters, typeBody), tokens, typeBody.linenum, typeBody.charnum);
return result;
}
function parseDefType(tokens, linenum, charnum) { function parseDefType(tokens, linenum, charnum) {
var result; var result;
var rhs; var rhs;
@ -464,7 +495,7 @@ function parseDef(tokens, linenum, charnum) {
charnum, charnum,
"A definition cannot be the value of a binding"); "A definition cannot be the value of a binding");
} }
result = addSrcPos(new typ.Def(identifier, bound), tokens, linenum, charnum); result = addSrcPos(new typ.Def(identifier, bound), tokens, bound.linenum, bound.charnum);
return result; return result;
} }
} }
@ -562,7 +593,7 @@ function parseIf(tokens) {
} }
else { else {
var elseC = parse(tokens); var elseC = parse(tokens);
result = addSrcPos(new typ.If(ifC, thenC, elseC), tokens, linenum, charnum); result = addSrcPos(new typ.If(ifC, thenC, elseC), tokens, elseC.linenum, elseC.charnum);
return result; return result;
} }
} }
@ -599,7 +630,7 @@ function parseLambda(tokens) {
} }
tokens.pop(); tokens.pop();
var body = parse(tokens); var body = parse(tokens);
result = addSrcPos(new typ.FuncT(parameters, body), tokens, linenum, charnum); result = addSrcPos(new typ.FuncT(parameters, body), tokens, body.linenum, body.charnum);
return result; return result;
} }
@ -687,7 +718,7 @@ function parseInfix(tokens, minPrec, lhs, linenum, charnum) {
tokens.pop(); tokens.pop();
/*remove the operator token*/ /*remove the operator token*/
var rhs = parseInfix(tokens, nextMinPrec); var rhs = parseInfix(tokens, nextMinPrec);
lhs = addSrcPos(typ.makeApp(op, [lhs, rhs]), tokens, linenum, charnum); lhs = addSrcPos(typ.makeApp(op, [lhs, rhs]), tokens, rhs.linenum, rhs.charnum);
} }
return lhs; return lhs;
} }

7
pprint.js

@ -30,6 +30,10 @@ function pprintDefType(stx) {
return pprint(stx.lhs) + " = " + pprint(stx.rhs); 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) { function pprint(expr) {
if (expr.exprType === "Name") { if (expr.exprType === "Name") {
return expr.val; return expr.val;
@ -63,6 +67,9 @@ function pprint(expr) {
else if (expr.exprType === "TypeDefinition") { else if (expr.exprType === "TypeDefinition") {
return pprintDefType(expr); return pprintDefType(expr);
} }
else if (expr.exprType === "TypeFuncDefinition") {
return pprintTypeFunc(expr);
}
else if (expr.exprType === "If") { else if (expr.exprType === "If") {
return pprintIf(expr); return pprintIf(expr);
} }

13
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 /* Params is a list of type variables
* type is a type expression * 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); _.each(params, checkName);
if (!isTypeExprRec(type)) { if (!isTypeExprRec(type)) {
throw errors.JSyntaxError( throw errors.JSyntaxError(
@ -330,6 +337,7 @@ function DataType(params, type) {
type.charnum, type.charnum,
"Body of a type definition must be a valid type expression"); "Body of a type definition must be a valid type expression");
} }
this.name = name;
this.params = params; this.params = params;
this.type = type; this.type = type;
this.exprType = "TypeFuncDefinition"; this.exprType = "TypeFuncDefinition";
@ -410,5 +418,6 @@ module.exports =
TypeApp: TypeApp, TypeApp: TypeApp,
Closure : Closure, Closure : Closure,
isTypeExpr : isTypeExprRec, isTypeExpr : isTypeExprRec,
DefType : DefType DefType : DefType,
DataType : DataType
}; };

Loading…
Cancel
Save