Updated CodeGen of FiledDecl and MethodDecl and Assign

This commit is contained in:
Jochen Seyfried 2024-05-22 10:05:20 +02:00
parent 3be8d9854a
commit 108b8f63ea
6 changed files with 17 additions and 9 deletions

View File

@ -41,7 +41,7 @@ public class FieldDecl extends AbstractType implements Node {
}
public void codeGen(ClassWriter cw) {
//TODO: Do we have fields with initial values?
//TODO: Do we have fields with initial values? --> No dont think so --> assign
FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC, identifier, getFieldDescriptor(), null, null);
fv.visitEnd();
}
@ -51,7 +51,7 @@ public class FieldDecl extends AbstractType implements Node {
switch (type) {
case "int":
return "I";
case "boolean":
case "bool":
return "Z";
case "char":
return "C";

View File

@ -116,7 +116,7 @@ public class MethodDecl implements Node {
for (Parameter param : parameters.parameterList) {
switch (param.type) {
case "int" -> descriptor.append("I");
case "boolean" -> descriptor.append("Z");
case "bool" -> descriptor.append("Z");
case "char" -> descriptor.append("C");
case "void" -> descriptor.append("V");
default -> {
@ -136,7 +136,7 @@ public class MethodDecl implements Node {
} else {
switch (returnType) {
case "int" -> descriptor.append("I");
case "boolean" -> descriptor.append("Z");
case "bool" -> descriptor.append("Z");
case "char" -> descriptor.append("C");
case "void" -> descriptor.append("V");
default -> {

View File

@ -85,7 +85,7 @@ public class RefType extends AbstractType implements Node {
}
for (MethodDecl method : methodDecls) {
// method.codeGen(cw);
method.codeGen(cw, methodContext);
}
cw.visitEnd();
}

View File

@ -4,6 +4,7 @@ import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.ArrayList;
import java.util.Arrays;
@ -30,6 +31,6 @@ public class LocalVarDecl implements IStatement{
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
localVars.put(identifier, type);
}
}

View File

@ -7,7 +7,6 @@ import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.*;
import java.util.HashMap;
import java.util.List;
public class ReturnStatement extends AbstractType implements IStatement{
IExpression expression;
@ -39,7 +38,8 @@ public class ReturnStatement extends AbstractType implements IStatement{
if (expression != null) {
expression.codeGen(mv);
//Get the Type of the expression
String type = expression.typeCheck().type;
//TODO: Resolve how do we get the type of the expression
String type = expression.typeCheck(??).type;
if (type.equals("int") || type.equals("bool") || type.equals("char")) {
mv.visitInsn(Opcodes.IRETURN);

View File

@ -8,6 +8,7 @@ import abstractSyntaxTree.Expression.InstVarExpression;
import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.Statement.LocalVarDecl;
import org.objectweb.asm.*;
import java.util.HashMap;
@ -51,7 +52,13 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
if (left instanceof VarRefExpression varRef) {
if (left instanceof LocalVarIdentifier) {
LocalVarIdentifier localVar = (LocalVarIdentifier) left;
String varName = localVar.getIdentifier();
//Get the index of the local variable
int varIndex = localVars.get(varName)
}