diff --git a/desugar.js b/desugar.js new file mode 100644 index 0000000..c4eaed9 --- /dev/null +++ b/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) { diff --git a/parse.js b/parse.js old mode 100644 new mode 100755 index 410d8fd..39aa335 --- a/parse.js +++ b/parse.js @@ -1,3 +1,5 @@ +#! /usr/bin/node + var typ = require("./representation.js"); var tool = require("./tools.js"); @@ -174,7 +176,12 @@ function parseDef(tokens) { return undefined; } 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)); else if (toktype === "ifexp") return checkParse(parseIf(tokens)); - else if (toktype === "left_paren") - return computeApp(tokens); + 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); + } else { console.log("Unexpected token: " + toktype); process.exit(code=1); @@ -334,8 +349,6 @@ function parseInfix(tokens, minPrec, lhs) { //remove the operator token var rhs = parseInfix(tokens, nextMinPrec); lhs = typ.makeApp(op, [lhs, rhs]); - - } return lhs; } diff --git a/representation.js b/representation.js index 2b27389..4d43c65 100644 --- a/representation.js +++ b/representation.js @@ -58,7 +58,7 @@ BoolT.prototype = Expression; function ListT(x, xs) { this.x = x; this.rest = xs; - this.val = [x,xs]; + this.val = xs; this.exprType = "List"; return this; } @@ -112,7 +112,7 @@ function Def(ident, exp) { function If(condition, thenexp, elseexp) { this.condition = condition; this.thenexp = thenexp; - if (elseexp) +if (elseexp) this.elseexp = elseexp; this.exprType = "If"; return this; @@ -139,8 +139,6 @@ function makeApp(name, parameters) { } -OPTable = {"+" : makeBin("+")}; - OPInfo = {"+" : [2, "Left"], "-" : [2, "Left"], "*" : [3, "Left"],