Browse Source

fixed some bugs related to list parsing

pull/21/head
Wesley Kerfoot 12 years ago
parent
commit
55aab5b3f5
  1. 17
      parse.js
  2. 11
      test.jl

17
parse.js

@ -44,6 +44,7 @@ function parseMany(exprType, valid, tokens) {
}
else {
console.log("Error: unexpected token "+fst(tokens));
console.log("in parseMany," + ", " + tokens);
return;
}
results.push(parsed);
@ -91,6 +92,13 @@ function parseBetween(exprType, between, tokens) {
}
function parseList(tokens) {
if (fst(tokens)[0] === "right_square")
var xs = [];
else if (fst(tokens)[0] === "comma") {
tokens.pop();
var xs = [];
}
else
var xs = parseBetween(function (x) { return true; }, "comma", tokens);
if (fst(tokens)[0] !== "right_square") {
console.log("Error, list must be terminated by ]");
@ -107,7 +115,12 @@ function parseDefFunction(tokens) {
console.log("Error, expected an identifier in function definition");
return undefined;
}
if (fst(tokens)[0] === "right_paren") {
var parameters = [];
}
else {
var parameters = parseMany(validName, validFormPar, tokens);
}
if ((fst(tokens)[0]) !== "right_paren") {
console.log("Error, formal parameters must be followed by )");
return undefined;
@ -333,9 +346,9 @@ function pprintDef(def) {
function pprintIf(ifexp) {
if (ifexp.elseexp)
return "if " + pprint(ifexp.condition) + " then " + pprint(ifexp.thenexp) + " else " + pprint(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) + ")";
}
function pprint(expr) {

11
test.jl

@ -17,8 +17,13 @@ def (mymap f xs)
(: (f (head xs))
(mymap f (tail xs)))
def empty []
def getFile
(readFile "./parse.js")
def main
if (2 < 3)
if False
then
(print (mymap add [1,2,3,4 , 5]))
else (print "")
undefined
else getFile

Loading…
Cancel
Save