fix: do variable inits properly

This commit is contained in:
TopchetoEU 2024-11-24 12:48:30 +02:00
parent 7f6df49fc5
commit 39eb6ffac5
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
6 changed files with 9 additions and 15 deletions

View File

@ -38,7 +38,7 @@ public abstract class FunctionNode extends Node {
var index = scope.define(param.name); var index = scope.define(param.name);
target.add(Instruction.loadArg(i++)); target.add(Instruction.loadArg(i++));
target.add(index.index().toInit()); target.add(index.index().toSet(false));
} }
// if (selfName != null && !scope.has(selfName, false)) { // if (selfName != null && !scope.has(selfName, false)) {

View File

@ -27,7 +27,7 @@ public class VariableDeclareNode extends Node {
for (var entry : values) { for (var entry : values) {
if (entry.value != null) { if (entry.value != null) {
entry.value.compile(target, true); entry.value.compile(target, true);
target.add(VariableNode.toInit(target, loc(), entry.var.name)); target.add(VariableNode.toSet(target, loc(), entry.var.name, false, true));
} }
} }

View File

@ -34,7 +34,7 @@ public class ForInNode extends Node {
int mid = target.temp(); int mid = target.temp();
target.add(Instruction.loadMember("value")).setLocation(binding.loc()); target.add(Instruction.loadMember("value")).setLocation(binding.loc());
target.add(VariableNode.toInit(target, loc(), binding.name)); target.add(VariableNode.toSet(target, loc(), binding.name, false, true));
target.setLocationAndDebug(object.loc(), BreakpointType.STEP_OVER); target.setLocationAndDebug(object.loc(), BreakpointType.STEP_OVER);

View File

@ -43,7 +43,7 @@ public class TryNode extends Node {
if (captureName != null) { if (captureName != null) {
var catchVar = target.scope.defineCatch(captureName); var catchVar = target.scope.defineCatch(captureName);
target.add(Instruction.loadError()); target.add(Instruction.loadError());
target.add(catchVar.index().toInit()); target.add(catchVar.index().toSet(false));
catchBody.compile(target, false); catchBody.compile(target, false);
target.scope.undefineCatch(); target.scope.undefineCatch();
} }

View File

@ -20,7 +20,7 @@ public class VariableNode extends Node implements ChangeTarget {
} }
@Override public void afterAssign(CompileResult target, boolean pollute) { @Override public void afterAssign(CompileResult target, boolean pollute) {
target.add(VariableNode.toSet(target, loc(), name, pollute)); target.add(VariableNode.toSet(target, loc(), name, pollute, false));
} }
@Override public void compile(CompileResult target, boolean pollute) { @Override public void compile(CompileResult target, boolean pollute) {
@ -40,17 +40,11 @@ public class VariableNode extends Node implements ChangeTarget {
return toGet(target, loc, name, true, false); return toGet(target, loc, name, true, false);
} }
public static Instruction toInit(CompileResult target, Location loc, String name) { public static Instruction toSet(CompileResult target, Location loc, String name, boolean keep, boolean init) {
var i = target.scope.get(name, false);
if (i != null) return i.index().toInit();
else return Instruction.globSet(name, false, true);
}
public static Instruction toSet(CompileResult target, Location loc, String name, boolean keep) {
var i = target.scope.get(name, false); var i = target.scope.get(name, false);
if (i != null) return i.index().toSet(keep); if (i != null) return i.index().toSet(keep);
else return Instruction.globSet(name, keep, false); else return Instruction.globSet(name, keep, init);
} }
public VariableNode(Location loc, String name) { public VariableNode(Location loc, String name) {

View File

@ -18,11 +18,11 @@ public class VariableAssignNode extends Node {
target.add(VariableNode.toGet(target, loc(), name)); target.add(VariableNode.toGet(target, loc(), name));
FunctionNode.compileWithName(value, target, true, name); FunctionNode.compileWithName(value, target, true, name);
target.add(Instruction.operation(operation)); target.add(Instruction.operation(operation));
target.add(VariableNode.toSet(target, loc(), name, pollute)); target.add(VariableNode.toSet(target, loc(), name, pollute, false));
} }
else { else {
FunctionNode.compileWithName(value, target, true, name); FunctionNode.compileWithName(value, target, true, name);
target.add(VariableNode.toSet(target, loc(), name, pollute)); target.add(VariableNode.toSet(target, loc(), name, pollute, false));
} }
} }