diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 643e98d5..2714965b 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -223,7 +223,7 @@ public class BytecodeGenMethod implements StatementVisitor { } Label endLabel = new Label(); - // this case for while loops + // this case for while loops and If statements if (statement instanceof LoopStmt) mv.visitLabel(endLabel); @@ -270,6 +270,8 @@ public class BytecodeGenMethod implements StatementVisitor { case LESSEQUAL: case BIGGERTHAN: case BIGGEREQUAL: + case EQUAL: + case NOTEQUAL: Label branchLabel = new Label(); doVisitRelOpInsn(op, largerType, branchLabel, endLabel); break; @@ -644,7 +646,10 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(IfStmt ifStmt) { - System.out.println("If"); + statement = new IfStatement(ifStmt.expr, ifStmt.then_block, ifStmt.else_block); + isBinaryExp = statement.isExprBinary(); + ifStmt.expr.accept(this); + statement = null; } @Override diff --git a/src/de/dhbwstuttgart/bytecode/IfStatement.java b/src/de/dhbwstuttgart/bytecode/IfStatement.java new file mode 100644 index 00000000..6b11cc96 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/IfStatement.java @@ -0,0 +1,31 @@ +package de.dhbwstuttgart.bytecode; + +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.statement.Statement; + +public class IfStatement extends AStatement{ + + private Statement then_block; + private Statement else_block; + + public IfStatement(Expression expr, Statement then_block, Statement else_block) { + super(expr); + this.then_block = then_block; + this.else_block = else_block; + } + + @Override + public void genBCForRelOp(MethodVisitor mv,Label branchLabel, Label endLabel, BytecodeGenMethod bytecodeGenMethod) { + bytecodeGenMethod.isBinary(false); + this.then_block.accept(bytecodeGenMethod); + + mv.visitLabel(branchLabel); + this.else_block.accept(bytecodeGenMethod); +// mv.visitLabel(endLabel); +// mv.visitJumpInsn(Opcodes.GOTO, endLabel); + } +} diff --git a/test/bytecode/javFiles/Faculty.jav b/test/bytecode/javFiles/Faculty.jav index d2bdcf91..1bcddc51 100644 --- a/test/bytecode/javFiles/Faculty.jav +++ b/test/bytecode/javFiles/Faculty.jav @@ -1,17 +1,32 @@ import java.lang.Integer; -class Faculty { +public class Faculty { - m () { + m (x) { - var fact = (x) -> { - if (x == 1) { - return x; - } - else { - return x * (fact.apply(x-1)); - } - }; - return fact; +// var fact = (x) -> { +// if (x == 1) { +// return x; +// } +// else { +// return x * (fact.apply(x-1)); +// } +// }; +// return fact; +// var x = 13; +// if(x>22) { +// return 0; +// }else if(x <1){ +// return x; +// }else { +// return 1; +// } + + if (x < 2) { + return x; + } + else { + return x * m(x-1); + } } }