52 lines
1.5 KiB
Java
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") || type.equals("char")) {
|
|
mv.visitInsn(Opcodes.IRETURN);
|
|
} else {
|
|
mv.visitInsn(Opcodes.ARETURN);
|
|
|
|
}
|
|
} else {
|
|
mv.visitInsn(Opcodes.RETURN);
|
|
}
|
|
}
|
|
}
|