Add Bytecodegenerator for LocalVariable

This commit is contained in:
Boolean-True 2024-05-08 22:16:09 +02:00
parent dea4ba1808
commit 918acef610
2 changed files with 19 additions and 3 deletions

View File

@ -1,10 +1,22 @@
package de.maishai.typedast; package de.maishai.typedast;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Data; import org.objectweb.asm.Label;
import lombok.NoArgsConstructor;
import java.util.HashMap;
import java.util.Map;
@NoArgsConstructor @NoArgsConstructor
@Getter
@Setter
@Data @Data
public class MethodContext { public class MethodContext {
private Label startLabel;
private Label endLabel;
private int localVarIndex = 0;
private Map<String, Integer> variableIndex = new HashMap<>();
public void addVariable(String name, int index) {
variableIndex.put(name, index);
}
} }

View File

@ -38,5 +38,9 @@ public final class TypedLocalVariable implements TypedNode {
} }
public void codeGen(MethodVisitor mv, MethodContext ctx) { public void codeGen(MethodVisitor mv, MethodContext ctx) {
int index = ctx.getLocalVarIndex();
ctx.setLocalVarIndex(index + 1);
ctx.addVariable(name, index);
mv.visitLocalVariable(name, type.getDescriptor(), null, ctx.getStartLabel(), ctx.getEndLabel(), index);
} }
} }