diff --git a/doc/parser.md b/doc/parser.md new file mode 100644 index 00000000..b8263623 --- /dev/null +++ b/doc/parser.md @@ -0,0 +1,24 @@ +# Dateien +* sämtliche Datein im Ordner de.dhbwstuttgart.parser gehören zum Parser +* Auch zu beachten: pom.xml + * Ein Teil der pom.xml instruiert maven zur Kompilierung der Java8.g4 Datei + * VORSICHT! Wird nicht zwangsläufig von der IDE ausgeführt + * siehe Kapitel "Grammatik -> Parser" + + +## Unterordner antlr +* Java8.g4 + * die eigentliche Java Grammatik + * hier wurden Anpassungen vorgenommen, damit sie auch typloses Java annimmt +* alle anderen Dateien in diesem Ordner sind vom ANTLR-Parsergenerator autogeneriert + +## Unterordner SyntaxTreeGenerator +* SyntaxTreeGenerator +* TODO + +# Grammatik -> Parser +* Die Antlr-Grammatik (Java8.g4) wandelt +* Folgende Stellen sind relevant + * TODO + + 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/src/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/de/dhbwstuttgart/bytecode/signature/Signature.java index 4daf3ded..60023895 100644 --- a/src/de/dhbwstuttgart/bytecode/signature/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -178,7 +178,6 @@ public class Signature { if(hasTPHs(ref)) createSignatureForParameterizedType(ref); - System.out.println("HAS WC = " + hasWC(ref)); if(hasWC(ref)) createSigForParamTypeWithWC(ref); } @@ -234,7 +233,6 @@ public class Signature { if(p instanceof WildcardType) { if(((WildcardType) p).getInnerType() instanceof GenericRefType) { String name = new TypeToSignature().visit((GenericRefType)((WildcardType) p).getInnerType()); - System.out.println("NAME WC = " + name); if(!genericsAndBoundsMethod.containsKey(name) && !genericsAndBounds.containsKey(name)) { sw.visitFormalTypeParameter(name); sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); diff --git a/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java index 6e73d8ee..7d47406d 100644 --- a/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java @@ -34,9 +34,9 @@ public class TypeToSignature implements TypeVisitor { // params += "L"+param.toString().replace(".", "/"); // } params += param.acceptTV(new TypeToSignature()); - if(!(param instanceof RefType)) + + if(param instanceof TypePlaceholder) params += ";"; -// if(it.hasNext())params += ";"; } params += ">"; } diff --git a/test/bytecode/Tph5Test.java b/test/bytecode/Tph5Test.java index 7d44e5e5..4107cca2 100644 --- a/test/bytecode/Tph5Test.java +++ b/test/bytecode/Tph5Test.java @@ -36,8 +36,9 @@ public class Tph5Test { @Test public void test() throws Exception { - Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class, Object.class); - Object result = m.invoke(instanceOfClass, "xx",2,3); +// Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class, Object.class); + Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class); +// Object result = m.invoke(instanceOfClass, "xx",2,3); //assertEquals(2,result); } diff --git a/test/bytecode/javFiles/Faculty.jav b/test/bytecode/javFiles/Faculty.jav index d2bdcf91..794022ab 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 == 1) { + return x; + } + else { + return x * m(x-1); + } } } diff --git a/test/bytecode/javFiles/Tph5.jav b/test/bytecode/javFiles/Tph5.jav index 1772b7ce..e73cfa2c 100644 --- a/test/bytecode/javFiles/Tph5.jav +++ b/test/bytecode/javFiles/Tph5.jav @@ -1,7 +1,13 @@ public class Tph5 { - m(a,b,c){ - a = c; - b = c; - return a; +// m(a,b,c){ +// a = c; +// b = c; +// return a; +// } + + m(x,y){ + x = m2(y); } + + m2(y) { return y; } }