NichtHaskell/Source/abstractSyntaxTree/Statement/ReturnStatement.java
Jochen Seyfried 2527d15467 Added Bytecodegeneration to the missing classes
Also included some TODOs in areas where parameters and some connections are missing
2024-05-07 13:50:51 +02:00

52 lines
1.5 KiB
Java

package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
import org.objectweb.asm.*;
public class ReturnStatement extends AbstractType implements IStatement{
IExpression expression;
public ReturnStatement(IExpression expression) {
this.expression = expression;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
if (expression == null) {
result.type = "void";
} else {
TypeCheckResult typedExpression = expression.typeCheck();
result.type = typedExpression.type;
}
return result;
}
//TODO: We do not differentiate between primitive types and reference types
// This is a problem at "BinaryExpression" and here because we need to know the type to return
// At this point in time we can either return reference types or have an error message
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
if (expression != null) {
expression.CodeGen(mv);
//Get the Type of the expression
String type = expression.typeCheck().type;
if (type.equals("int") || type.equals("bool")) {
mv.visitInsn(Opcodes.IRETURN);
} else {
mv.visitInsn(Opcodes.ARETURN);
}
} else {
mv.visitInsn(Opcodes.RETURN);
}
}
}