Skip to content

Commit

Permalink
prevent crash when scope deplete in typechecker
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingdong Huang committed Jan 6, 2020
1 parent 25a35bd commit 55d2885
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/typecheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ function typecheck(

return undefined;
}
function checkscopethis(name) {
function checkscopethis(name, pos) {
assert(
"Scope stack depleted, possibly due to extraneous end-block statement.",
pos,
scope.length
);
return checkscopei(scope.length - 1, name);
}

Expand Down Expand Up @@ -267,7 +272,7 @@ function typecheck(
assert(
`[Type] Variable '${a.names[j]}' redeclared in the same scope`,
a.pos,
!checkscopethis(a.names[j])
!checkscopethis(a.names[j], a.pos)
);

scope[scope.length - 1][a.names[j]] = inittype(a.type);
Expand Down Expand Up @@ -468,6 +473,7 @@ function typecheck(
ty.type = "obj";
ty.fields = {};
ty.fields[a.value[1].slice(1, -1)] = inittype("any");
ty.isarg = true;
}
assert(
`[Type] Property ${a.value[1]} does not exist in obj ${printType(
Expand Down
4 changes: 2 additions & 2 deletions tools/test_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ function runAll(lang, skips = []) {

// runExample("js", "quicksort");
// runExample("js", "../lib/js/畫譜");
// runExample("js", "../lib/列經");
runExample("js", "../lib/曆法");
// runAll("js", ["quine", "quine2", "tree", "tree2", "try"]);
// runAll("js", ["quine"]);

runExample("js", "../../../Downloads/local_test");
// runExample("js", "../../../Downloads/local_test");
// runExample("py", "draw_heart");

0 comments on commit 55d2885

Please sign in to comment.