From b54b2616e0b38982880bf778956db078b604b397 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Wed, 8 May 2024 20:33:58 +0200 Subject: [PATCH 1/2] add initialisation in method --- .../maishai/typedast/typedclass/TypedMethod.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index 6526e89..ea27c4f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -22,12 +22,17 @@ public class TypedMethod implements TypedNode { public TypedMethod unTypedMethodToTypedMethod(Method unTypedMethod) { TypedMethod typedMethod = new TypedMethod(); typedMethod.setName(unTypedMethod.methodName()); + typedMethod.setTypedParameters(new ArrayList<>()); - for(Parameter parameter : unTypedMethod.params()){ - TypedParameter typedParameter = new TypedParameter(); - typedParameter.setParaName(parameter.name()); - typedParameter.setType(parameter.type()); - typedParameters.add(typedParameter); + if(unTypedMethod.params().isEmpty()){ + typedMethod.setTypedParameters(List.of()); + }else { + for (Parameter parameter : unTypedMethod.params()) { + TypedParameter typedParameter = new TypedParameter(); + typedParameter.setParaName(parameter.name()); + typedParameter.setType(parameter.type()); + typedParameters.add(typedParameter); + } } TypedBlock block = new TypedBlock(); typedMethod.setTypedBlock(block.unTypedBlockToTypedBlock(unTypedMethod.block())); From 918acef610eb9b1f58e9087c905eef527ad57944 Mon Sep 17 00:00:00 2001 From: Boolean-True Date: Wed, 8 May 2024 22:16:09 +0200 Subject: [PATCH 2/2] Add Bytecodegenerator for LocalVariable --- .../de/maishai/typedast/MethodContext.java | 18 +++++++++++++++--- .../typedclass/TypedLocalVariable.java | 4 ++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/maishai/typedast/MethodContext.java b/src/main/java/de/maishai/typedast/MethodContext.java index b1b78f1..392f4de 100644 --- a/src/main/java/de/maishai/typedast/MethodContext.java +++ b/src/main/java/de/maishai/typedast/MethodContext.java @@ -1,10 +1,22 @@ package de.maishai.typedast; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; +import org.objectweb.asm.Label; + +import java.util.HashMap; +import java.util.Map; @NoArgsConstructor +@Getter +@Setter @Data public class MethodContext { + private Label startLabel; + private Label endLabel; + private int localVarIndex = 0; + private Map variableIndex = new HashMap<>(); + + public void addVariable(String name, int index) { + variableIndex.put(name, index); + } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index bb5b483..5177987 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -38,5 +38,9 @@ public final class TypedLocalVariable implements TypedNode { } 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); } }