-
Notifications
You must be signed in to change notification settings - Fork 349
Description
Here is a problem
sample.js
{
var a = 1
}
console.log(a)is identical to liftToJS(compile(sample.js))
Yet, if we look at the FuzzIL representation, we see that the Compiler didn't use v1 for console.log but v2 instead, which is unnecessary.
The problem is that the map function maps the identifier a to a FuzzIL variable in the most recently opened scope, i.e. the BlockStatement. Outside of the BlockStatement lookupIdentifier doesn't find the FuzzIL variable anymore and therefore creates a new one.
BeginBlockStatement
v0 <- LoadInteger '1'
v1 <- CreateNamedVariable 'a', 'var', v0
EndBlockStatement
v2 <- CreateNamedVariable 'a', 'none'
v3 <- CreateNamedVariable 'console', 'none'
v4 <- CallMethod v3, 'log', [v2]
I assume that the map function works correctly for variable declarations of the let and const type but not for globals or var. I would propose giving the map function a parameter that controls in which scope on the stack the identifier should be saved instead of just using scopes.top.