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);
}
//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();
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);
}
}
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);
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";

6
tokenize.js

@ -308,9 +308,15 @@ function tokenize(tokstream) {
}
function tokenizeFull(input) {
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);
}
}

Loading…
Cancel
Save