Browse Source

started on the desugarer

pull/21/head
Wesley Kerfoot 12 years ago
parent
commit
0742d95bc4
  1. 11
      desugar.js
  2. 21
      parse.js
  3. 6
      representation.js

11
desugar.js

@ -0,0 +1,11 @@
/*
* This module takes a parse tree in a surface format
* and transforms it into the "core" language which is
* much simpler and easier to type-check, optimize, and evaluate
*/
var typ = require("./representation.js");
// Lists get desugared to nested function calls
// i.e. (cons (cons (cons ...)))
function desugarList(lst) {

21
parse.js

@ -1,3 +1,5 @@
#! /usr/bin/node
var typ = require("./representation.js"); var typ = require("./representation.js");
var tool = require("./tools.js"); var tool = require("./tools.js");
@ -174,7 +176,12 @@ function parseDef(tokens) {
return undefined; return undefined;
} }
else { else {
return new typ.Def(parse(tokens), parse(tokens)); var identifier = parse(tokens);
if (!notFollowedBy(tokens, ["def", "comma", "left_paren", "arrow", "right_brace", "right_square"])) {
console.log("Error: def " + identifier.val + " must not be followed by " + fst(tokens)[0]);
return;
}
return new typ.Def(identifier, parse(tokens));
} }
} }
@ -251,8 +258,16 @@ function parse(tokens) {
return checkParse(parseDef(tokens)); return checkParse(parseDef(tokens));
else if (toktype === "ifexp") else if (toktype === "ifexp")
return checkParse(parseIf(tokens)); return checkParse(parseIf(tokens));
else if (toktype === "left_paren") else if (toktype === "left_paren") {
if (fst(tokens)[0] === "lambda") {
tokens.pop();
var parsed = checkParse(parseLambda(tokens));
tokens.pop();
return parsed;
}
else
return computeApp(tokens); return computeApp(tokens);
}
else { else {
console.log("Unexpected token: " + toktype); console.log("Unexpected token: " + toktype);
process.exit(code=1); process.exit(code=1);
@ -334,8 +349,6 @@ function parseInfix(tokens, minPrec, lhs) {
//remove the operator token //remove the operator token
var rhs = parseInfix(tokens, nextMinPrec); var rhs = parseInfix(tokens, nextMinPrec);
lhs = typ.makeApp(op, [lhs, rhs]); lhs = typ.makeApp(op, [lhs, rhs]);
} }
return lhs; return lhs;
} }

6
representation.js

@ -58,7 +58,7 @@ BoolT.prototype = Expression;
function ListT(x, xs) { function ListT(x, xs) {
this.x = x; this.x = x;
this.rest = xs; this.rest = xs;
this.val = [x,xs]; this.val = xs;
this.exprType = "List"; this.exprType = "List";
return this; return this;
} }
@ -112,7 +112,7 @@ function Def(ident, exp) {
function If(condition, thenexp, elseexp) { function If(condition, thenexp, elseexp) {
this.condition = condition; this.condition = condition;
this.thenexp = thenexp; this.thenexp = thenexp;
if (elseexp) if (elseexp)
this.elseexp = elseexp; this.elseexp = elseexp;
this.exprType = "If"; this.exprType = "If";
return this; return this;
@ -139,8 +139,6 @@ function makeApp(name, parameters) {
} }
OPTable = {"+" : makeBin("+")};
OPInfo = {"+" : [2, "Left"], OPInfo = {"+" : [2, "Left"],
"-" : [2, "Left"], "-" : [2, "Left"],
"*" : [3, "Left"], "*" : [3, "Left"],

Loading…
Cancel
Save