From 01e4e93b56f8898e89a211be1dd980ec9c9f4e1b Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Tue, 25 Mar 2014 00:39:02 -0400 Subject: [PATCH] fixed some stupid bugs, started a file for testing the parser --- closure_conversion.js | 2 ++ pprint.js | 11 +++++------ tokenize.js | 22 ++++++++++++++++++---- treegen.js | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 treegen.js diff --git a/closure_conversion.js b/closure_conversion.js index 34a26d4..fc6ab07 100644 --- a/closure_conversion.js +++ b/closure_conversion.js @@ -147,6 +147,8 @@ function test(src) { console.log(JSON.stringify(closure_convert_all(ast), null, 4)); } +console.log(pprint.pprint(parser.parse(pprint.pprint(parser.parse("if something then if a then if b then c else d else rtrrt else some_other_thing")[0]))[0])); + module.export = { test : test, closureConvert : closure_convert_all diff --git a/pprint.js b/pprint.js index 023b1f8..e67fc82 100644 --- a/pprint.js +++ b/pprint.js @@ -4,9 +4,9 @@ function pprintName(ident) { function pprintFunc(func) { if (func.p.exprType === "Name") - return "(\\ " + pprint(func.p) + " -> " + pprint(func.body) + ")"; + return "(lambda " + pprint(func.p) + " -> " + pprint(func.body) + ")"; else - return "(\\ " + func.p.map(pprint).join(" ") + " -> " + pprint(func.body) + ")"; + return "(lambda " + func.p.map(pprint).join(" ") + " -> " + pprint(func.body) + ")"; } @@ -21,10 +21,9 @@ function pprintDef(def) { } 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) + ")"; + return ("(if " + pprint(ifexp.condition) + + " then " + pprint(ifexp.thenexp) + + " else " + pprint(ifexp.elseexp) + ")"); } function pprint(expr) { diff --git a/tokenize.js b/tokenize.js index 99b2be3..b03a87d 100755 --- a/tokenize.js +++ b/tokenize.js @@ -10,7 +10,10 @@ function isDigit(a) { if (!a) return false; var code = a.charCodeAt(); - return (46 < code && code < 58 || code < 58 && code > 46); + return (46 < code && + code < 58 || + code < 58 && + code > 46); } function isWhitespace(a) { @@ -18,12 +21,22 @@ function isWhitespace(a) { return true; var code = a.charCodeAt(); - return (code === 9 || code === 32 || code === 10 || code === 13 || code === 11); + return (code === 9 || + code === 32 || + code === 10 || + code === 13 || + code === 11); } function isIdentifier(a) { var code = a.charCodeAt(); - return code !== 41 && code !== 40 && code && 125 && code && 123 && code !== 93 && code !== 91 && code !== 44; + return (code !== 41 && + code !== 40 && + code !== 125 && + code !== 123 && + code !== 93 && + code !== 91 && + code !== 44); } function tokenizeNum(tokstream, charnum, linenum) { @@ -376,4 +389,5 @@ function tokenizeFull(input) { return tokenizeHelp(input, matchop, true); } -module.exports = {tokenize : tokenizeFull}; +module.exports = {tokenize : tokenizeFull, + isIdentifier : isIdentifier}; diff --git a/treegen.js b/treegen.js new file mode 100644 index 0000000..142f0b9 --- /dev/null +++ b/treegen.js @@ -0,0 +1,19 @@ +#! /usr/bin/node + +var parser = require("./parse.js"); +var pprint = require("./pprint.js"); +var repr = require("./representation.js"); +var lex = require("./tokenize.js"); + +var qc = require("quickcheck"); + +function arbIdentifier() { + var st = qc.arbString() + if (lex.isIdentifier(st)) { + return st; + } + else { + return arbIdentifier(); + } +} +