From ea2aa91761d0ce2dabdc99ec5f0c18b994d20f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=BCtterling=2C=20Franziska=20=28WWI2012D=29?= Date: Thu, 25 Jun 2015 16:01:24 +0200 Subject: [PATCH] GenCode: DoubleLiteral implementiert. Aufgrund von Typinferenz-Problemen kann ein erfolgreicher Test-Run nicht konsisten repliziert werden. --- .../syntaxtree/statement/Assign.java | 16 +++++-- .../syntaxtree/statement/DoubleLiteral.java | 19 +++++++- test/bytecode/DoubleLiteral.jav | 6 +++ test/bytecode/DoubleLiteral.java | 45 +++++++++++++++++++ 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 test/bytecode/DoubleLiteral.jav create mode 100644 test/bytecode/DoubleLiteral.java diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index 47d682168..a992d209c 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -8,6 +8,7 @@ import java.util.Iterator; import org.apache.bcel.generic.ASTORE; import org.apache.bcel.generic.ClassGen; +import org.apache.bcel.generic.DSTORE; import org.apache.bcel.generic.ISTORE; import org.apache.bcel.generic.InstructionFactory; import org.apache.bcel.generic.InstructionList; @@ -185,11 +186,18 @@ public class Assign extends Expr String expr2Type = expr2.getType().get_Name().toString(); switch(expr2Type){//welche rückgabewerte entstehen hier??? Integer und String scheinen nicht zu stimmen - case "java.lang.Integer": il.append(new ISTORE(counterAssign)); - break; + case "java.lang.Integer": + il.append(new ISTORE(counterAssign)); + break; - case "java.lang.String": il.append(new ASTORE(counterAssign)); - break; + case "java.lang.String": + il.append(new ASTORE(counterAssign)); + break; + + case "java.lang.Double": + il.append(new DSTORE(counterAssign)); + break; + } return il; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.java b/src/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.java index 5cd0aa5d0..3c8f8ee71 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.java @@ -6,8 +6,11 @@ import java.util.Hashtable; import org.apache.bcel.generic.BIPUSH; import org.apache.bcel.generic.ClassGen; +import org.apache.bcel.generic.ConstantPoolGen; import org.apache.bcel.generic.InstructionFactory; import org.apache.bcel.generic.InstructionList; +import org.apache.bcel.generic.LDC; +import org.apache.bcel.generic.LDC2_W; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.logger.Logger; @@ -131,12 +134,26 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) { return ret; } - @Override + //alter Double-Versuch vermutlich inkorrekt + /*@Override public InstructionList genByteCode(ClassGen cg) { InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool()); InstructionList il = new InstructionList(); il.append(new BIPUSH(new Double(get_Double()).byteValue())); return il; + }*/ + + @Override + public InstructionList genByteCode(ClassGen cg) { + ConstantPoolGen cp = cg.getConstantPool(); + //InstructionFactory _factory = new InstructionFactory(cg, cp); + InstructionList il = new InstructionList(); + + cp.addDouble(get_Double()); + il.append(new LDC2_W(cp.getSize()-1)); + return il; + } + } // ino.end diff --git a/test/bytecode/DoubleLiteral.jav b/test/bytecode/DoubleLiteral.jav new file mode 100644 index 000000000..7813d2628 --- /dev/null +++ b/test/bytecode/DoubleLiteral.jav @@ -0,0 +1,6 @@ +class DoubleLiteral{ + +void method() {a; a = 20.0d;} + + +} \ No newline at end of file diff --git a/test/bytecode/DoubleLiteral.java b/test/bytecode/DoubleLiteral.java new file mode 100644 index 000000000..e9470c5fd --- /dev/null +++ b/test/bytecode/DoubleLiteral.java @@ -0,0 +1,45 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +import org.junit.Test; + +import plugindevelopment.TypeInsertTester; +import de.dhbwstuttgart.core.MyCompiler; +import de.dhbwstuttgart.core.MyCompilerAPI; +import de.dhbwstuttgart.logger.LoggerConfiguration; +import de.dhbwstuttgart.logger.Section; +import de.dhbwstuttgart.parser.JavaParser.yyException; +import de.dhbwstuttgart.typeinference.ByteCodeResult; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; +import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet; + +public class DoubleLiteral { + + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public final static String testFile = "DoubleLiteral.jav"; + public final static String outputFile = "DoubleLiteral.class"; + + @Test + public void test() { + LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out); + MyCompilerAPI compiler = MyCompiler.getAPI(logConfig); + try { + compiler.parse(new File(rootDirectory + testFile)); + compiler.typeReconstruction(); + ByteCodeResult bytecode = compiler.generateBytecode(); + System.out.println(bytecode); + bytecode.getByteCode().getJavaClass().dump(new File(rootDirectory + outputFile)); + } catch (IOException | yyException e) { + e.printStackTrace(); + TestCase.fail(); + } + } + +}