source-academy/js-slang

Env Viz: Avoid unnecessary block statements

Closed this issue · 1 comments

Before we push a block statement on the agenda, we should check if the block has a declaration. If not, we should avoid pushing the block, and instead just push the body of the block. Example:

This statement

let x = 1;
{
    x = 2;   
}
x;

should lead to:

x;
POP
x = 2;
let x = 1;

instead of

x;
POP
{ x = 2; }
let x = 1;

This is in line with the parse function:

display_list(parse("let x = 1; { x = 2; } x;"));

gives:

list("sequence",
     list(list("variable_declaration", list("name", "x"), list("literal", 1)),
          list("assignment", list("name", "x"), list("literal", 2)),
          list("name", "x")))

Implementation here:
js-slang/src/stdlib/parser.ts

Look out for the functions
makeSequenceIfNeeded
and
makeBlockIfNeeded.

Closed as completed in #1454