add codeGen for Main-Method

This commit is contained in:
simon 2024-06-23 16:07:40 +02:00
parent 9cda095a0b
commit d0e2af5da6
3 changed files with 26 additions and 10 deletions

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}