Added TODOS for changes

This commit is contained in:
Jochen Seyfried 2024-05-08 13:51:20 +02:00
parent ce399626da
commit 93514e4f08
5 changed files with 18 additions and 5 deletions

View File

@ -11,6 +11,7 @@ public class MethodDecl implements IClass {
private HashMap<String, HashMap<String, HashMap<String, String>>> methodContext; private HashMap<String, HashMap<String, HashMap<String, String>>> methodContext;
private HashMap<String, HashMap<String, String>> typeContext; private HashMap<String, HashMap<String, String>> typeContext;
//TODO: Move this into the typeCheck
private HashMap<String, String> localVars; // (type, identifier) // add content here private HashMap<String, String> localVars; // (type, identifier) // add content here
public MethodDecl(HashMap<String, HashMap<String, HashMap<String, String>>> methodContext, HashMap<String, HashMap<String, String>> typeContext){ public MethodDecl(HashMap<String, HashMap<String, HashMap<String, String>>> methodContext, HashMap<String, HashMap<String, String>> typeContext){

View File

@ -3,7 +3,9 @@ package abstractSyntaxTree;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Datatype.RefType; import abstractSyntaxTree.Datatype.RefType;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -11,7 +13,10 @@ import java.util.List;
public class Program { public class Program {
public List<RefType> classes; public List<RefType> classes;
//TODO: Rename typeContext to attributeContext or something
public HashMap<String, HashMap<String, String>> typeContext; // (class, (type, identifier)) public HashMap<String, HashMap<String, String>> typeContext; // (class, (type, identifier))
//TODO: Change Data structure and make parameter a list
public HashMap<String, HashMap<String, HashMap<String, String>>> methodContext; // (class, (returntype, (identifier, parameter))) public HashMap<String, HashMap<String, HashMap<String, String>>> methodContext; // (class, (returntype, (identifier, parameter)))
public TypeCheckResult typeCheck() throws Exception{ public TypeCheckResult typeCheck() throws Exception{
@ -26,7 +31,14 @@ public class Program {
public void codeGen() throws Exception{ public void codeGen() throws Exception{
for(RefType oneClass : classes){ for(RefType oneClass : classes){
oneClass.codeGen(); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "oneClass.className",
null, "java/lang/Object", null);
oneClass.codeGen(cw);
cw.visitEnd();
byte[] bytecode = cw.toByteArray();
//Write the bytecode to a .class file
} }
} }
} }

View File

@ -43,7 +43,7 @@ public class IfElseStatement extends AbstractType implements IStatement{
Label conditionFalse = new Label(); Label conditionFalse = new Label();
Label statementEnd = new Label(); Label statementEnd = new Label();
condition.CodeGen(mv); condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0) mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.codeGen(mv); //If the condition is true, execute the ifBlock ifStatement.codeGen(mv); //If the condition is true, execute the ifBlock

View File

@ -35,7 +35,7 @@ public class IfStatement extends AbstractType implements IStatement{
Label conditionFalse = new Label(); Label conditionFalse = new Label();
condition.CodeGen(mv); condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0) mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.codeGen(mv); ifStatement.codeGen(mv);

View File

@ -34,11 +34,11 @@ public class ReturnStatement extends AbstractType implements IStatement{
public void codeGen(MethodVisitor mv) throws Exception { public void codeGen(MethodVisitor mv) throws Exception {
if (expression != null) { if (expression != null) {
expression.CodeGen(mv); expression.codeGen(mv);
//Get the Type of the expression //Get the Type of the expression
String type = expression.typeCheck().type; String type = expression.typeCheck().type;
if (type.equals("int") || type.equals("bool")) { if (type.equals("int") || type.equals("bool") || type.equals("char")) {
mv.visitInsn(Opcodes.IRETURN); mv.visitInsn(Opcodes.IRETURN);
} else { } else {
mv.visitInsn(Opcodes.ARETURN); mv.visitInsn(Opcodes.ARETURN);