Browse Source

fixed yet another bug, might refactor that function later

pull/21/head
Wesley Kerfoot 12 years ago
parent
commit
9d735e5789
  1. 28
      parse.js
  2. 10
      test.js
  3. 1
      tokenize.js

28
parse.js

@ -1,11 +1,11 @@
#! /usr/bin/node #! /usr/bin/node
var fs = require("fs");
var typ = require("./representation.js"); var typ = require("./representation.js");
var tool = require("./tools.js"); var tool = require("./tools.js");
var tokenizer = require("./tokenize.js"); var tokenizer = require("./tokenize.js");
var desugarer = require("./desugar.js"); var desugarer = require("./desugar.js");
var fs = require("fs"); var pprint = require("./pprint.js");
var pprint = require("./pprint.js").pprint;
var print = console.log; var print = console.log;
@ -37,7 +37,7 @@ function makeChecker(props) {
/*Tries to parse until the prediction ``valid'' fails or the wrong type is parsed /*Tries to parse until the prediction ``valid'' fails or the wrong type is parsed
Collects the results into an array and returns it*/ Collects the results into an array and returns it*/
function parseMany(exprType, valid, tokens) { function parseMany(exprType, valid, tokens) {
var current = fst(tokens)[0]; var current = fst(tokens)[0];
var results = []; var results = [];
var parsed; var parsed;
@ -50,13 +50,15 @@ function parseMany(exprType, valid, tokens) {
console.log("in parseMany," + ", " + tokens); console.log("in parseMany," + ", " + tokens);
return; return;
} }
results.push(parsed); results.push(parsed);
//make sure there are at least 2 tokens to parse //make sure there are at least 2 tokens to parse
if (tokens.length > 1 && valid(fst(tokens)[0])) { if (tokens.length > 1 && valid(fst(tokens)[0])) {
while (valid(snd(tokens)[0])) { while (valid(snd(tokens)[0])) {
results.push(parse(tokens)); if (!(valid(fst(tokens)[0])))
//console.log(results); break;
//print(valid(fst(tokens)[0]), tokens);
results.push(parse(tokens));
if (!exprType(fst(results).exprType)) if (!exprType(fst(results).exprType))
break; break;
//console.log(results); //console.log(results);
@ -68,10 +70,10 @@ function parseMany(exprType, valid, tokens) {
//do the same validity check as before and in the loop //do the same validity check as before and in the loop
if (valid(fst(tokens)[0])) if (valid(fst(tokens)[0]))
results.push(parse(tokens)); results.push(parse(tokens));
//console.log(tokens);
return results; return results;
} }
/* Tries to parse exprType separated by the token between /* Tries to parse exprType separated by the token between
* e.g. <identifier>,<identifier>,... * e.g. <identifier>,<identifier>,...
*/ */
@ -95,14 +97,16 @@ function parseBetween(exprType, between, tokens) {
} }
function parseList(tokens) { function parseList(tokens) {
if (fst(tokens)[0] === "right_square") if (fst(tokens)[0] === "right_square") {
var xs = []; var xs = [];
}
else if (fst(tokens)[0] === "comma") { else if (fst(tokens)[0] === "comma") {
tokens.pop(); tokens.pop();
var xs = []; var xs = [];
} }
else else {
var xs = parseBetween(function (x) { return true; }, "comma", tokens); var xs = parseBetween(function (x) { return true; }, "comma", tokens);
}
if (fst(tokens)[0] !== "right_square") { if (fst(tokens)[0] !== "right_square") {
console.log("Error, list must be terminated by ]"); console.log("Error, list must be terminated by ]");
return undefined; return undefined;
@ -237,6 +241,7 @@ function computeApp(tokens) {
} }
else { else {
//it's a prefix application //it's a prefix application
var parameters = parseMany(validArgTypes, validArgument, tokens); var parameters = parseMany(validArgTypes, validArgument, tokens);
//console.log(parameters); //console.log(parameters);
if (fst(tokens)[0] !== "right_paren") { if (fst(tokens)[0] !== "right_paren") {
@ -325,7 +330,7 @@ function parse(tokens) {
} }
} }
//var tokenized = tokenizer.tokenize(fs.readFileSync('/dev/stdin').toString()); var istr = fs.readFileSync('/dev/stdin').toString();
function parseFull(tokenized) { function parseFull(tokenized) {
var ast = new Array(); var ast = new Array();
while (tokenized.length > 0) { while (tokenized.length > 0) {
@ -334,5 +339,6 @@ function parseFull(tokenized) {
} }
return ast; return ast;
} }
console.log(parseFull(tokenizer.tokenize(istr))[0].func.p.p.val);
module.exports = {parse : tool.compose(parseFull, tokenizer.tokenize) }; //module.exports = {parse : tool.compose(parseFull, tokenizer.tokenize) };

10
test.js

@ -0,0 +1,10 @@
#! /usr/bin/node
var p = require("./parse.js");
var pp = require("./pprint.js");
var tools = require("./tools.js");
var parse = tools.compose(pp.pprint, p.parse);
//console.log(parse("((map g [1,2,3]) >> (print 34))"));
//p.parse("((f [1,2,3,4,5]) >> (* 2 3))");
//p.parse("[1] 45");
//p.parse("(+ 2 3 4)");

1
tokenize.js

@ -1,6 +1,5 @@
#! /usr/bin/node #! /usr/bin/node
var fs = require("fs");
var rep = require("./representation.js"); var rep = require("./representation.js");
var tools = require("./tools.js"); var tools = require("./tools.js");
var operators = Object.keys(rep.OPInfo); var operators = Object.keys(rep.OPInfo);

Loading…
Cancel
Save