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 } }