Browse Source

moar pprint functions

pull/21/head
Wesley Kerfoot 12 years ago
parent
commit
28de71cc0a
  1. 32
      parse.js
  2. 13
      representation.js

32
parse.js

@ -345,20 +345,38 @@ function pprintName(ident) {
} }
function pprintFunc(func) { function pprintFunc(func) {
return "lambda " + pprint(func.p) + " -> " + pprint(func.body); if (func.p.exprType === "Name")
return "\\ " + pprint(func.p) + " -> " + pprint(func.body);
else
return "\\ " + func.p.map(pprint).join(" ") + " -> " + pprint(func.body);
} }
function pprintApp(app) { function pprintApp(app) {
if (!app.p || app.p === undefined) if (!app.p || app.p === undefined)
return "((" + pprint(app.func) + "))"; return "(" + pprint(app.func) + ")";
return "((" + pprint(app.func) + ") " + pprint(app.p) + ")"; return "((" + pprint(app.func) + ") " + pprint(app.p) + ")";
} }
function pprintDef(def) {
return "let " + pprint(def.ident) + " = " + pprint(def.val);
}
function pprintIf(ifexp) {
if (ifexp.elseexp)
return "if " + pprint(ifexp.condition) + " then " + pprint(ifexp.thenexp) + " else " + pprint(ifexp.elseexp);
else
return "if " + pprint(ifexp.condition) + " then " + pprint(ifexp.thenexp);
}
function pprint(expr) { function pprint(expr) {
if (expr.exprType === "Name") if (expr.exprType === "Name")
return expr.val; return expr.val;
else if (expr.exprType === "Bool") else if (expr.exprType === "Bool")
return expr.val; if (expr.val)
return "True";
else
return "False";
else if (expr.exprType === "Integer") else if (expr.exprType === "Integer")
return expr.val; return expr.val;
else if (expr.exprType === "Float") else if (expr.exprType === "Float")
@ -369,10 +387,18 @@ function pprint(expr) {
return expr.val; return expr.val;
else if (expr.exprType === "Application") else if (expr.exprType === "Application")
return pprintApp(expr); return pprintApp(expr);
else if (expr.exprType === "Definition")
return pprintDef(expr);
else if (expr.exprType === "If")
return pprintIf(expr);
else if (expr.exprType === "Function")
return pprintFunc(expr);
} }
var input = process.argv.slice(2).reduce(function(acc, x) {return acc + " " + x}, ""); var input = process.argv.slice(2).reduce(function(acc, x) {return acc + " " + x}, "");
var tokenized = tokenize(input).reverse(); var tokenized = tokenize(input).reverse();
//parse(tokenized);
//console.log(parse(tokenized))
console.log(pprint(parse(tokenized))); console.log(pprint(parse(tokenized)));
//console.log(tokenized); //console.log(tokenized);

13
representation.js

@ -118,6 +118,7 @@ function If(condition, thenexp, elseexp) {
return this; return this;
} }
//convenience function to construct binary operators //convenience function to construct binary operators
//assumes that the identifier refers to the name of a primitive //assumes that the identifier refers to the name of a primitive
//operation //operation
@ -140,11 +141,13 @@ function makeApp(name, parameters) {
OPTable = {"+" : makeBin("+")}; OPTable = {"+" : makeBin("+")};
OPInfo = {"+" : [1, "Left"], OPInfo = {"+" : [2, "Left"],
"-" : [1, "Left"], "-" : [2, "Left"],
"*" : [2, "Left"], "*" : [3, "Left"],
"/" : [2, "Left"], "/" : [3, "Left"],
"^" : [3, "Right"]} "^" : [4, "Right"],
"++" : [2, "Left"],
"==" : [1, "Left"]}
module.exports = module.exports =
{ IntT : IntT, { IntT : IntT,

Loading…
Cancel
Save