From 673c249b680b9720014e0cc1d853400a0c0e9f97 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 19 Sep 2018 15:52:29 +0200 Subject: [PATCH] =?UTF-8?q?=09modified:=20=20=20src/de/dhbwstuttgart/bytec?= =?UTF-8?q?ode/BytecodeGenMethod.java=20=09new=20file:=20=20=20src/de/dhbw?= =?UTF-8?q?stuttgart/bytecode/IfStatement.java=20Bytecode=20f=C3=BCr=20If?= =?UTF-8?q?=20statement=20wir=20erzeugt=20aber=20noch=20nicht=20vollst?= =?UTF-8?q?=C3=A4ndig.=20=09modified:=20=20=20test/bytecode/javFiles/Facul?= =?UTF-8?q?ty.jav=20Test=20angepasst.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bytecode/BytecodeGenMethod.java | 9 ++++- .../dhbwstuttgart/bytecode/IfStatement.java | 31 ++++++++++++++++ test/bytecode/javFiles/Faculty.jav | 37 +++++++++++++------ 3 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 src/de/dhbwstuttgart/bytecode/IfStatement.java 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); + } } }