From 37c79418d4aca6795f627acb5caa9d3583f12780 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Thu, 12 Dec 2013 15:01:37 -0500 Subject: [PATCH] started to add proper errors --- errors.js | 35 +++++++++++++++++++++++++++++++++++ parse.js | 23 ++++++++++++++++------- representation.js | 2 ++ tokenize.js | 12 +++++++++--- 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 errors.js diff --git a/errors.js b/errors.js new file mode 100644 index 0000000..1a9b08c --- /dev/null +++ b/errors.js @@ -0,0 +1,35 @@ +/* + * This file defines common error objects + * for reporting on syntax errors, type errors, + * and perhaps runtime exceptions although I have + * not thought about how that will work much + */ + +var JLException = { + stxerror : + function () { + console.log("There was an error\n", + "Line #",this.linenum,"\n", + "Character #", this.charnum,"\n", + this.errormessage); + }, + type_error : + function () { + return; + } +} + +function SyntaxError(linenum, charnum, message) { + this.linenum = linenum; + this.charnum = charnum; + this.errormessage = message; + return this; +} + +function TypeError(linenum, charnum, token, message) { + this.linenum = linenum; + this.charnum = charnum; + this.errormessage = message; + this.token = token; + return this; +} diff --git a/parse.js b/parse.js index 709bde9..6b10954 100755 --- a/parse.js +++ b/parse.js @@ -192,6 +192,9 @@ function parseLambda(tokens) { return new typ.FuncT(parameters, body); } +//function parseLet(tokens) { + + var invalidArguments = ["def", "comma", "right_paren", "right_square", "right_brace", "left_brace", "right_brace"]; var validArgument = tool.compose(tool.not, makeChecker(invalidArguments)); var validArgTypes = tool.compose(tool.not, makeChecker(["Definition"])); @@ -298,9 +301,9 @@ function parse(tokens) { else return computeApp(tokens); } - /*else if (toktype === "let") { - return parseLet(tokens); - }*/ +// else if (toktype === "let") { +// return parseLet(tokens); +// } else { throw "Error: Unexpected token: " + toktype; } @@ -309,11 +312,17 @@ function parse(tokens) { var istr = fs.readFileSync('/dev/stdin').toString(); function parseFull(tokenized) { var ast = new Array(); - while (tokenized.length > 0) { - var parsed = desugarer.desugar(parse(tokenized)); - ast.push(parsed); + try { + while (tokenized.length > 0) { + var parsed = desugarer.desugar(parse(tokenized)); + ast.push(parsed); + } + return ast; + } catch (e) { + print("An exception occured, could not finish parsing"); + print(e); + process.exit(1); } - return ast; } console.log(parseFull(tokenizer.tokenize(istr)).map(pprint.pprint).join("\n")); diff --git a/representation.js b/representation.js index 7151338..0521183 100644 --- a/representation.js +++ b/representation.js @@ -30,6 +30,7 @@ function LetExp(pairs) { this.val = tool.dict(pairs); return this; } +LetExp.prototype = Expression; function UnaryOp(op, v) { this.exprType = "Unary"; @@ -37,6 +38,7 @@ function UnaryOp(op, v) { this.op = op; return this; } +UnaryOp.prototype = Expression; function IntT(v) { this.exprType = "Integer"; diff --git a/tokenize.js b/tokenize.js index 28f96e9..1a7c04f 100755 --- a/tokenize.js +++ b/tokenize.js @@ -308,9 +308,15 @@ function tokenize(tokstream) { } function tokenizeFull(input) { - return tokenize(input).reverse().filter(function(x) { - return x[0] !== "whitespace"; - }); + try { + return tokenize(input).reverse().filter(function(x) { + return x[0] !== "whitespace"; + }); + } catch (e) { + console.log("An error occured during tokenization"); + console.log(e); + process.exit(1); + } }