Browse Source

started to add proper errors

pull/1/head
Wesley Kerfoot 12 years ago
parent
commit
37c79418d4
  1. 35
      errors.js
  2. 15
      parse.js
  3. 2
      representation.js
  4. 6
      tokenize.js

35
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;
}

15
parse.js

@ -192,6 +192,9 @@ function parseLambda(tokens) {
return new typ.FuncT(parameters, body); return new typ.FuncT(parameters, body);
} }
//function parseLet(tokens) {
var invalidArguments = ["def", "comma", "right_paren", "right_square", "right_brace", "left_brace", "right_brace"]; var invalidArguments = ["def", "comma", "right_paren", "right_square", "right_brace", "left_brace", "right_brace"];
var validArgument = tool.compose(tool.not, makeChecker(invalidArguments)); var validArgument = tool.compose(tool.not, makeChecker(invalidArguments));
var validArgTypes = tool.compose(tool.not, makeChecker(["Definition"])); var validArgTypes = tool.compose(tool.not, makeChecker(["Definition"]));
@ -298,9 +301,9 @@ function parse(tokens) {
else else
return computeApp(tokens); return computeApp(tokens);
} }
/*else if (toktype === "let") { // else if (toktype === "let") {
return parseLet(tokens); // return parseLet(tokens);
}*/ // }
else { else {
throw "Error: Unexpected token: " + toktype; throw "Error: Unexpected token: " + toktype;
} }
@ -309,11 +312,17 @@ function parse(tokens) {
var istr = 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();
try {
while (tokenized.length > 0) { while (tokenized.length > 0) {
var parsed = desugarer.desugar(parse(tokenized)); var parsed = desugarer.desugar(parse(tokenized));
ast.push(parsed); ast.push(parsed);
} }
return ast; return ast;
} catch (e) {
print("An exception occured, could not finish parsing");
print(e);
process.exit(1);
}
} }
console.log(parseFull(tokenizer.tokenize(istr)).map(pprint.pprint).join("\n")); console.log(parseFull(tokenizer.tokenize(istr)).map(pprint.pprint).join("\n"));

2
representation.js

@ -30,6 +30,7 @@ function LetExp(pairs) {
this.val = tool.dict(pairs); this.val = tool.dict(pairs);
return this; return this;
} }
LetExp.prototype = Expression;
function UnaryOp(op, v) { function UnaryOp(op, v) {
this.exprType = "Unary"; this.exprType = "Unary";
@ -37,6 +38,7 @@ function UnaryOp(op, v) {
this.op = op; this.op = op;
return this; return this;
} }
UnaryOp.prototype = Expression;
function IntT(v) { function IntT(v) {
this.exprType = "Integer"; this.exprType = "Integer";

6
tokenize.js

@ -308,9 +308,15 @@ function tokenize(tokstream) {
} }
function tokenizeFull(input) { function tokenizeFull(input) {
try {
return tokenize(input).reverse().filter(function(x) { return tokenize(input).reverse().filter(function(x) {
return x[0] !== "whitespace"; return x[0] !== "whitespace";
}); });
} catch (e) {
console.log("An error occured during tokenization");
console.log(e);
process.exit(1);
}
} }

Loading…
Cancel
Save