NichtHaskell/Source/abstractSyntaxTree/Statement/IfStatement.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

50 lines
1.3 KiB
Java

package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
import org.objectweb.asm.*;
public class IfStatement extends AbstractType implements IStatement{
IExpression condition;
//Do we need a block statement here?
IStatement ifStatement;
public IfStatement(IExpression condition, IStatement ifStatement) {
this.condition = condition;
this.ifStatement = ifStatement;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck();
if (!conditionType.equals("boolean")) {
throw new IllegalArgumentException("should be boolean");
}
TypeCheckResult ifStatementType = ifStatement.typeCheck();
result.type = ifStatementType.type;
return result;
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
Label conditionFalse = new Label();
condition.CodeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.CodeGen(mv);
mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed
}
}