Browse Source

fixed some stupid bugs, started a file for testing the parser

pull/3/head
nisstyre56 11 years ago
parent
commit
01e4e93b56
  1. 2
      closure_conversion.js
  2. 11
      pprint.js
  3. 22
      tokenize.js
  4. 19
      treegen.js

2
closure_conversion.js

@ -147,6 +147,8 @@ function test(src) {
console.log(JSON.stringify(closure_convert_all(ast), null, 4)); 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 = { module.export = {
test : test, test : test,
closureConvert : closure_convert_all closureConvert : closure_convert_all

11
pprint.js

@ -4,9 +4,9 @@ function pprintName(ident) {
function pprintFunc(func) { function pprintFunc(func) {
if (func.p.exprType === "Name") if (func.p.exprType === "Name")
return "(\\ " + pprint(func.p) + " -> " + pprint(func.body) + ")"; return "(lambda " + pprint(func.p) + " -> " + pprint(func.body) + ")";
else 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) { function pprintIf(ifexp) {
if (ifexp.elseexp) return ("(if " + pprint(ifexp.condition) +
return "(if " + pprint(ifexp.condition) + " then " + pprint(ifexp.thenexp) + " else " + pprint(ifexp.elseexp) + ")"; " then " + pprint(ifexp.thenexp) +
else " else " + pprint(ifexp.elseexp) + ")");
return "(if " + pprint(ifexp.condition) + " then " + pprint(ifexp.thenexp) + ")";
} }
function pprint(expr) { function pprint(expr) {

22
tokenize.js

@ -10,7 +10,10 @@ function isDigit(a) {
if (!a) if (!a)
return false; return false;
var code = a.charCodeAt(); 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) { function isWhitespace(a) {
@ -18,12 +21,22 @@ function isWhitespace(a) {
return true; return true;
var code = a.charCodeAt(); 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) { function isIdentifier(a) {
var code = a.charCodeAt(); 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) { function tokenizeNum(tokstream, charnum, linenum) {
@ -376,4 +389,5 @@ function tokenizeFull(input) {
return tokenizeHelp(input, matchop, true); return tokenizeHelp(input, matchop, true);
} }
module.exports = {tokenize : tokenizeFull}; module.exports = {tokenize : tokenizeFull,
isIdentifier : isIdentifier};

19
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();
}
}
Loading…
Cancel
Save