fix: scope issues

This commit is contained in:
TopchetoEU 2024-11-24 12:47:51 +02:00
parent 61c5df5003
commit 7f6df49fc5
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
3 changed files with 13 additions and 17 deletions

View File

@ -5,8 +5,7 @@ import java.util.HashMap;
public final class FunctionScope {
protected final VariableList locals = new VariableList(VariableIndex.IndexType.LOCALS);
protected final VariableList catches = new VariableList(VariableIndex.IndexType.LOCALS, this.locals);
protected final VariableList capturables = new VariableList(VariableIndex.IndexType.CAPTURABLES, this.catches);
protected final VariableList capturables = new VariableList(VariableIndex.IndexType.CAPTURABLES, this.locals);
private final VariableList captures = new VariableList(VariableIndex.IndexType.CAPTURES);
private final HashMap<String, Variable> localsMap = new HashMap<>();
@ -38,8 +37,10 @@ public final class FunctionScope {
public Variable define(Variable var) {
if (passthrough) return null;
else {
var old = get(var.name, false);
if (old != null) return old;
var catchVar = getCatchVar(var.name);
if (catchVar != null) return catchVar;
if (localsMap.containsKey(var.name)) return localsMap.get(var.name);
if (capturesMap.containsKey(var.name)) throw new RuntimeException("HEY!");
localsMap.put(var.name, var);
return locals.add(var);
@ -58,7 +59,7 @@ public final class FunctionScope {
*/
public Variable defineCatch(String name) {
var var = new Variable(name, false);
this.catches.add(var);
this.locals.add(var);
this.catchesMap.add(var);
return var;
}
@ -87,7 +88,7 @@ public final class FunctionScope {
var parentVar = parent.get(name, true);
if (parentVar == null) return null;
var childVar = captures.add(parentVar.clone());
var childVar = captures.add(parentVar.clone().setIndexSupplier(null));
capturesMap.put(childVar.name, childVar);
childToParent.put(childVar, parentVar);
parentToChild.put(parentVar, childVar);
@ -103,7 +104,6 @@ public final class FunctionScope {
* @param capture Whether or not to execute this capturing logic
*/
public Variable get(Variable var, boolean capture) {
if (catches.has(var)) return addCaptured(var, capture);
if (captures.has(var)) return addCaptured(var, capture);
if (locals.has(var)) return addCaptured(var, capture);

View File

@ -45,14 +45,6 @@ public final class VariableIndex {
default: throw new UnsupportedOperationException("Unknown index type " + type);
}
}
public final Instruction toInit() {
switch (type) {
case CAPTURES: throw new UnsupportedOperationException("Unknown index type " + type);
case CAPTURABLES: return Instruction.storeVar(index, false, true);
case LOCALS: return Instruction.storeVar(index, false, true);
default: throw new UnsupportedOperationException("Unknown index type " + type);
}
}
public VariableIndex(VariableIndex.IndexType type, int index) {
this.type = type;

View File

@ -13,7 +13,7 @@ public final class VariableList implements Iterable<Variable> {
public VariableNode next;
public VariableNode prev;
public int index;
public int indexIteration;
public int indexIteration = -1;
public VariableList list() { return VariableList.this; }
@ -120,6 +120,7 @@ public final class VariableList implements Iterable<Variable> {
node.prev = null;
varMap.remove(node.var);
node.var.setIndexSupplier(null);
return node.var;
}
@ -178,7 +179,10 @@ public final class VariableList implements Iterable<Variable> {
*/
public VariableList(IndexType type, VariableList prev) {
this.indexType = type;
this.offset = prev::size;
this.offset = () -> {
if (prev.offset != null) return prev.offset.getAsInt() + prev.size();
else return prev.size();
};
}
public VariableList(IndexType type) {
this.indexType = type;