Skip to content
Snippets Groups Projects
Commit 023c5230 authored by Thomas Wood's avatar Thomas Wood
Browse files

Add tuple type checking

parent 24e96186
No related branches found
No related tags found
No related merge requests found
...@@ -148,6 +148,12 @@ function typecheckAST(ast) { ...@@ -148,6 +148,12 @@ function typecheckAST(ast) {
return type; return type;
} }
// Test for tuple types
if (type.endsWith(')')) {
type = type.substring(1, type.length - 1);
return type.split('*').map(s => s.trim());
}
// Test for poly type // Test for poly type
var i = type.lastIndexOf(' '); var i = type.lastIndexOf(' ');
if (i >= 0) { if (i >= 0) {
...@@ -170,14 +176,13 @@ function typecheckAST(ast) { ...@@ -170,14 +176,13 @@ function typecheckAST(ast) {
} }
}; };
var errorMsg = function (value, msg) {
return _ => esprimaToAST.toString(value, 1) + " " + msg;
};
var typecheck = function(type, value) { var typecheck = function(type, value) {
var t = getType(type); var t = getType(type);
if (isBaseType(t)) { if (isBaseType(t)) {
assert(t === typeof value, errorMsg(value, "was expected to have type of "+t)); assert(t === typeof value, errorMsg(value, "was expected to have type of "+t));
} else if (t instanceof Array) {
assert.instanceOf(value, Array);
t.forEach((type, index) => typecheck(type, value[index]));
} else { } else {
assert(value.hasOwnProperty("type"), errorMsg(value, "doesn't have a type property")); assert(value.hasOwnProperty("type"), errorMsg(value, "doesn't have a type property"));
assert.strictEqual(t._typeName, value.type); assert.strictEqual(t._typeName, value.type);
...@@ -194,6 +199,10 @@ function typecheckAST(ast) { ...@@ -194,6 +199,10 @@ function typecheckAST(ast) {
} }
}; };
var errorMsg = function (value, msg) {
return _ => esprimaToAST.toString(value, 3) + " " + msg;
};
return typecheck("prog", ast); return typecheck("prog", ast);
} }
...@@ -241,6 +250,7 @@ walk(test262path) ...@@ -241,6 +250,7 @@ walk(test262path)
} catch (e) { } catch (e) {
if (e instanceof esprimaToAST.UnsupportedSyntaxError) { if (e instanceof esprimaToAST.UnsupportedSyntaxError) {
} else { } else {
console.log(JSON.stringify(ast, null, 2));
throw e; throw e;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment