From d0e2af5da61cf9546e3fe1722d3712f120529ce6 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 23 Jun 2024 16:07:40 +0200 Subject: [PATCH] add codeGen for Main-Method --- .../maishai/typedast/typedclass/TypedClass.java | 12 ++++++++---- .../maishai/typedast/typedclass/TypedMain.java | 7 +++++-- .../typedast/typedclass/TypedMethod.java | 17 +++++++++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index da842a0..3cd2c72 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -1,13 +1,13 @@ package de.maishai.typedast.typedclass; -import de.maishai.ast.Operator; -import de.maishai.ast.records.*; import de.maishai.ast.records.Class; +import de.maishai.ast.records.Constructor; +import de.maishai.ast.records.Declaration; +import de.maishai.ast.records.Method; import de.maishai.typedast.ClassContext; -import de.maishai.typedast.CodeGenUtils; import de.maishai.typedast.Type; -import lombok.AllArgsConstructor; import de.maishai.typedast.TypedNode; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.objectweb.asm.ClassWriter; @@ -218,6 +218,10 @@ public class TypedClass implements TypedNode { m.codeGen(ctx); } + if (typedMain != null) { + typedMain.codeGen(ctx); + } + return cw.toByteArray(); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMain.java b/src/main/java/de/maishai/typedast/typedclass/TypedMain.java index da981c0..67c4671 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMain.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMain.java @@ -1,13 +1,12 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Class; +import de.maishai.typedast.ClassContext; import de.maishai.typedast.Type; import de.maishai.typedast.TypedNode; import lombok.AllArgsConstructor; import lombok.Data; -import lombok.NoArgsConstructor; -import java.util.ArrayList; import java.util.List; @Data @@ -31,4 +30,8 @@ public class TypedMain implements TypedNode { public Type typeCheck(TypedProgram typedProgram) { return type; } + + public void codeGen(ClassContext ctx) { + typedMethod.codeGenMain(ctx); + } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index 3acbf7f..5a66a31 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -124,11 +124,20 @@ public class TypedMethod implements TypedNode { int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok MethodVisitor mv = ctx.getCw().visitMethod(accessModifier, name, CodeGenUtils.generateDescriptor(typedParameters.stream().map(TypedParameter::getType).toList(), returnType), null, null); - MethodContext context = new MethodContext(ctx, mv); - typedParameters.forEach(param -> context.registerVariable(param.getParaName(), param.getType())); + MethodContext mctx = new MethodContext(ctx, mv); + typedParameters.forEach(param -> mctx.registerVariable(param.getParaName(), param.getType())); - typedBlock.codeGen(context); - context.wrapUp(); + typedBlock.codeGen(mctx); + mctx.wrapUp(); } + public void codeGenMain(ClassContext ctx) { + MethodVisitor mv = ctx.getCw().visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, + name, "([Ljava/lang/String;)V", null, null); + MethodContext mctx = new MethodContext(ctx, mv); + typedParameters.forEach(param -> mctx.registerVariable(param.getParaName(), param.getType())); + + typedBlock.codeGen(mctx); + mctx.wrapUp(); + } }