From b72cc89b6ff52c9c48635edd9ef6515669e35059 Mon Sep 17 00:00:00 2001 From: romanman Date: Wed, 14 May 2014 19:45:02 +0300 Subject: [PATCH] More testing on Serpent compiler, some operand order fixes: --- .../org/ethereum/gui/ConnectionConsole.java | 1 + .../java/org/ethereum/gui/SerpentEditor.java | 13 +- .../serpent/SerpentToAssemblyCompiler.java | 40 +- .../ethereum/serpent/SerpentCompileTest.java | 733 ++++++++++++++---- .../test/java/org/ethereum/util/RLPTest.java | 2 +- 5 files changed, 596 insertions(+), 193 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/ConnectionConsole.java b/ethereumj-core/src/main/java/org/ethereum/gui/ConnectionConsole.java index 8c13ae5f..baeadf45 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/ConnectionConsole.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/ConnectionConsole.java @@ -61,6 +61,7 @@ public class ConnectionConsole extends JFrame implements PeerListener{ public void componentShown(ComponentEvent e) { Thread t = new Thread() { public void run() { + new ClientPeer(thisConsole).connect("54.201.28.117", 30303); // new ClientPeer(thisConsole).connect("82.217.72.169", 30303); // new ClientPeer(thisConsole).connect("54.204.10.41", 30303); diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java b/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java index ad9f0cd7..4c7d7b14 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java @@ -103,10 +103,19 @@ public class SerpentEditor extends JFrame { // todo: integrate new compiler when avail asmResult = SerpentCompiler.compile(codeArea.getText()); - } catch (Throwable th) {th.printStackTrace();} + } catch (Throwable th) { + th.printStackTrace(); + splitPanel.setDividerLocation(0.7); + result.setVisible(true); + result.setText(th.getMessage()); + result.setForeground(Color.RED); + return; + + } + + result.setForeground(Color.BLUE); splitPanel.setDividerLocation(0.7); - result.setVisible(true); result.setText(asmResult); } diff --git a/ethereumj-core/src/main/java/org/ethereum/serpent/SerpentToAssemblyCompiler.java b/ethereumj-core/src/main/java/org/ethereum/serpent/SerpentToAssemblyCompiler.java index 2e3d4f45..87303894 100644 --- a/ethereumj-core/src/main/java/org/ethereum/serpent/SerpentToAssemblyCompiler.java +++ b/ethereumj-core/src/main/java/org/ethereum/serpent/SerpentToAssemblyCompiler.java @@ -193,8 +193,8 @@ public class SerpentToAssemblyCompiler extends SerpentBaseVisitor { public String visitMul_expr(@NotNull SerpentParser.Mul_exprContext ctx) { if (ctx.mul_expr() == null) return visit(ctx.int_val()); - String operand0 = visit(ctx.mul_expr()); - String operand1 = visit(ctx.int_val()); + String operand0 = visit(ctx.int_val()); + String operand1 = visit(ctx.mul_expr()); switch (ctx.OP_MUL().getText().toLowerCase()) { case "*": return operand0 + " " + operand1 + " MUL"; @@ -211,8 +211,8 @@ public class SerpentToAssemblyCompiler extends SerpentBaseVisitor { if (ctx.add_expr() == null) return visit(ctx.mul_expr()); - String operand0 = visit(ctx.add_expr()); - String operand1 = visit(ctx.mul_expr()); + String operand0 = visit(ctx.mul_expr()); + String operand1 = visit(ctx.add_expr()); switch (ctx.OP_ADD().getText().toLowerCase()) { case "+": return operand0 + " " + operand1 + " ADD"; @@ -230,10 +230,10 @@ public class SerpentToAssemblyCompiler extends SerpentBaseVisitor { String operand1 = visit(ctx.add_expr()); switch (ctx.OP_REL().getText().toLowerCase()) { - case "<": return operand0 + " " + operand1 + " LT"; - case ">": return operand0 + " " + operand1 + " GT"; - case ">=": return operand0 + " " + operand1 + " LT NOT"; - case "<=": return operand0 + " " + operand1 + " GT NOT"; + case "<": return operand1 + " " + operand0 + " LT"; + case ">": return operand1 + " " + operand0 + " GT"; + case ">=": return operand1 + " " + operand0 + " LT NOT"; + case "<=": return operand1 + " " + operand0 + " GT NOT"; default: throw new UnknownOperandException(ctx.OP_REL().getText()); } } @@ -243,8 +243,8 @@ public class SerpentToAssemblyCompiler extends SerpentBaseVisitor { if (ctx.eq_exp() == null) return visit(ctx.rel_exp()); - String operand0 = visit(ctx.eq_exp()); - String operand1 = visit(ctx.rel_exp()); + String operand0 = visit(ctx.rel_exp()); + String operand1 = visit(ctx.eq_exp()); switch (ctx.OP_EQ().getText().toLowerCase()) { case "==": return operand0 + " " + operand1 + " EQ"; @@ -258,8 +258,8 @@ public class SerpentToAssemblyCompiler extends SerpentBaseVisitor { if (ctx.and_exp() == null) return visit(ctx.eq_exp()); - String operand0 = visit(ctx.and_exp()); - String operand1 = visit(ctx.eq_exp()); + String operand0 = visit(ctx.eq_exp()); + String operand1 = visit(ctx.and_exp()); switch (ctx.OP_AND().getText().toLowerCase()) { case "&": return operand0 + " " + operand1 + " AND"; @@ -272,8 +272,8 @@ public class SerpentToAssemblyCompiler extends SerpentBaseVisitor { if (ctx.ex_or_exp() == null) return visit(ctx.and_exp()); - String operand0 = visit(ctx.ex_or_exp()); - String operand1 = visit(ctx.and_exp()); + String operand0 = visit(ctx.and_exp()); + String operand1 = visit(ctx.ex_or_exp()); switch (ctx.OP_EX_OR().getText().toLowerCase()) { case "xor": return operand0 + " " + operand1 + " XOR"; @@ -286,8 +286,8 @@ public class SerpentToAssemblyCompiler extends SerpentBaseVisitor { if (ctx.in_or_exp() == null) return visit(ctx.ex_or_exp()); - String operand0 = visit(ctx.in_or_exp()); - String operand1 = visit(ctx.ex_or_exp()); + String operand0 = visit(ctx.ex_or_exp()); + String operand1 = visit(ctx.in_or_exp()); switch (ctx.OP_IN_OR().getText().toLowerCase()) { case "|": return operand0 + " " + operand1 + " OR"; @@ -300,8 +300,8 @@ public class SerpentToAssemblyCompiler extends SerpentBaseVisitor { if (ctx.log_and_exp() == null) return visit(ctx.in_or_exp()); - String operand0 = visit(ctx.log_and_exp()); - String operand1 = visit(ctx.in_or_exp()); + String operand0 = visit(ctx.in_or_exp()); + String operand1 = visit(ctx.log_and_exp()); switch (ctx.OP_LOG_AND().getText().toLowerCase()) { case "and": return operand0 + " " + operand1 + " NOT NOT MUL"; @@ -315,8 +315,8 @@ public class SerpentToAssemblyCompiler extends SerpentBaseVisitor { if (ctx.log_or_exp() == null) return visit(ctx.log_and_exp()); - String operand0 = visit(ctx.log_or_exp()); - String operand1 = visit(ctx.log_and_exp()); + String operand0 = visit(ctx.log_and_exp()); + String operand1 = visit(ctx.log_or_exp()); switch (ctx.OP_LOG_OR().getText().toLowerCase()) { case "||": return operand0 + " " + operand1 + " DUP 4 PC ADD JUMPI POP SWAP POP"; diff --git a/ethereumj-core/src/test/java/org/ethereum/serpent/SerpentCompileTest.java b/ethereumj-core/src/test/java/org/ethereum/serpent/SerpentCompileTest.java index e5861c60..034cb7ae 100644 --- a/ethereumj-core/src/test/java/org/ethereum/serpent/SerpentCompileTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/serpent/SerpentCompileTest.java @@ -26,7 +26,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // assign test 2 @@ -44,7 +44,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // assign test 3 @@ -63,7 +63,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @@ -84,7 +84,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // assign test 5 @@ -130,7 +130,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // expression test 2 @@ -147,14 +147,14 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // expression test 3 public void test8(){ String code = "a = 2 | 2 xor 2 * 2"; - String expected = "2 2 2 2 MUL XOR OR 0 MSTORE"; + String expected = "2 2 MUL 2 XOR 2 OR 0 MSTORE"; SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -164,14 +164,14 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // expression test 4 public void test9(){ String code = "a = (2 | 2) xor (2 * 2)"; - String expected = "2 2 OR 2 2 MUL XOR 0 MSTORE"; + String expected = "2 2 MUL 2 2 OR XOR 0 MSTORE"; SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -181,7 +181,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @@ -189,7 +189,7 @@ public class SerpentCompileTest { public void test10(){ String code = "a = !(2 | 2 xor 2 * 2)"; - String expected = "2 2 2 2 MUL XOR OR NOT 0 MSTORE"; + String expected = "2 2 MUL 2 XOR 2 OR NOT 0 MSTORE"; SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -199,14 +199,14 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // expression test 6 public void test11(){ String code = "a = 2 + 2 * 2 + 2"; - String expected = "2 2 2 MUL ADD 2 ADD 0 MSTORE"; + String expected = "2 2 2 MUL 2 ADD ADD 0 MSTORE"; SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -216,14 +216,14 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // expression test 7 public void test12(){ - String code = "a = 2 / 2 * 2 + 2"; - String expected = "2 2 DIV 2 MUL 2 ADD 0 MSTORE"; + String code = "a = 2 / 1 * 2 + 2"; + String expected = "2 2 1 2 DIV MUL ADD 0 MSTORE"; SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -233,14 +233,14 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // expression test 8 public void test13(){ String code = "a = 2 - 0x1a * 5 + 0xA"; - String expected = "2 26 5 MUL SUB 10 ADD 0 MSTORE"; + String expected = "10 5 26 MUL 2 SUB ADD 0 MSTORE"; SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -250,14 +250,14 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // expression test 9 public void test14(){ String code = "a = 1 > 2 > 3 > 4"; - String expected = "1 2 GT 3 GT 4 GT 0 MSTORE"; + String expected = "4 3 2 1 GT GT GT 0 MSTORE"; SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -267,14 +267,14 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // expression test 10 public void test15(){ String code = "a = not ( 1 + 2 * 9 | 8 == 2)"; - String expected = "1 2 9 MUL ADD 8 2 EQ OR NOT 0 MSTORE"; + String expected = "2 8 EQ 9 2 MUL 1 ADD OR NOT 0 MSTORE"; SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -284,7 +284,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 1 @@ -292,14 +292,15 @@ public class SerpentCompileTest { String code = "if 1>2: \n" + " a=2"; - String expected = "1 2 GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 LABEL_0"; + String expected = "2 1 GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 LABEL_0"; /** - 1 2 GT NOT REF_1 JUMPI - 2 0 MSTORE - REF_0 JUMP - LABEL_1 - LABEL_0 + + 2 1 GT NOT REF_1 JUMPI + 2 0 MSTORE + REF_0 JUMP + LABEL_1 LABEL_0 + */ SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -309,7 +310,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 2 @@ -317,15 +318,14 @@ public class SerpentCompileTest { String code = "if 10 > 2 + 5: \n" + " a=2"; - String expected = "10 2 5 ADD GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 LABEL_0"; + String expected = "5 2 ADD 10 GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 LABEL_0"; /** - 10 2 5 ADD GT NOT REF_1 JUMPI - 2 0 MSTORE - REF_0 JUMP - LABEL_1 - LABEL_0 + 5 2 ADD 10 GT NOT REF_1 JUMPI + 2 0 MSTORE + REF_0 JUMP + LABEL_1 LABEL_0 */ @@ -337,7 +337,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 3 @@ -347,14 +347,15 @@ public class SerpentCompileTest { " a=2\n" + "else: \n" + " c=3\n"; - String expected = "10 2 5 ADD GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 3 32 MSTORE LABEL_0"; + String expected = "5 2 ADD 10 GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 3 32 MSTORE LABEL_0"; /** - 10 2 5 ADD GT NOT REF_1 JUMPI - 2 0 MSTORE REF_0 JUMP - LABEL_1 + 5 2 ADD 10 GT NOT REF_1 JUMPI + 2 0 MSTORE + REF_0 JUMP + LABEL_1 3 32 MSTORE - LABEL_0 + LABEL_0 */ SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, @@ -365,7 +366,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 4 @@ -376,15 +377,16 @@ public class SerpentCompileTest { "else: \n" + " c=123\n" + " d=0xFFAA"; - String expected = "10 2 5 ADD GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 123 32 MSTORE 65450 64 MSTORE LABEL_0"; + String expected = "5 2 ADD 10 GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 123 32 MSTORE 65450 64 MSTORE LABEL_0"; /** - 10 2 5 ADD GT NOT REF_1 JUMPI - 2 0 MSTORE REF_0 JUMP - LABEL_1 - 123 32 MSTORE - 65450 64 MSTORE - LABEL_0 + 5 2 ADD 10 GT NOT REF_1 JUMPI + 2 0 MSTORE + REF_0 JUMP + LABEL_1 + 123 32 MSTORE + 65450 64 MSTORE + LABEL_0 */ SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, @@ -395,7 +397,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 5 @@ -408,18 +410,20 @@ public class SerpentCompileTest { "else: \n" + " c=123\n" + " d=0xFFAA"; - String expected = "10 2 5 ADD GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 2 2 MUL 4 EQ NOT REF_2 JUMPI 3 0 MSTORE REF_0 JUMP LABEL_2 123 32 MSTORE 65450 64 MSTORE LABEL_0"; + String expected = "5 2 ADD 10 GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 4 2 2 MUL EQ NOT REF_2 JUMPI 3 0 MSTORE REF_0 JUMP LABEL_2 123 32 MSTORE 65450 64 MSTORE LABEL_0"; /** - 10 2 5 ADD GT NOT REF_1 JUMPI - 2 0 MSTORE REF_0 JUMP - LABEL_1 - 2 2 MUL 4 EQ NOT REF_2 JUMPI - 3 0 MSTORE REF_0 JUMP - LABEL_2 - 123 32 MSTORE - 65450 64 MSTORE - LABEL_0 + 5 2 ADD 10 GT NOT REF_1 JUMPI + 2 0 MSTORE + REF_0 JUMP + LABEL_1 + 4 2 2 MUL EQ NOT REF_2 JUMPI + 3 0 MSTORE + REF_0 JUMP + LABEL_2 + 123 32 MSTORE + 65450 64 MSTORE + LABEL_0 */ SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, @@ -430,13 +434,13 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 6 public void test21(){ - String code = "if 10 > 2 + 5: \n" + + String code = "if 10 < 2 + 5: \n" + " a=2\n" + "elif 2*2==4: \n" + " a=3\n" + @@ -447,26 +451,29 @@ public class SerpentCompileTest { "else: \n" + " c=123\n" + " d=0xFFAA"; - String expected = "10 2 5 ADD GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 2 2 MUL 4 EQ NOT REF_2 JUMPI 3 0 MSTORE REF_0 JUMP LABEL_2 2 2 MUL 10 ADD 40 EQ NOT REF_3 JUMPI 3 0 MSTORE 9 0 MSTORE 21 0 MSTORE REF_0 JUMP LABEL_3 123 32 MSTORE 65450 64 MSTORE LABEL_0"; + String expected = "5 2 ADD 10 LT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 4 2 2 MUL EQ NOT REF_2 JUMPI 3 0 MSTORE REF_0 JUMP LABEL_2 40 10 2 2 MUL ADD EQ NOT REF_3 JUMPI 3 0 MSTORE 9 0 MSTORE 21 0 MSTORE REF_0 JUMP LABEL_3 123 32 MSTORE 65450 64 MSTORE LABEL_0"; /** - 10 2 5 ADD GT NOT REF_1 JUMPI - 2 0 MSTORE REF_0 JUMP + 5 2 ADD 10 LT NOT REF_1 JUMPI + 2 0 MSTORE + REF_0 JUMP LABEL_1 - 2 2 MUL 4 EQ NOT REF_2 JUMPI + 4 2 2 MUL EQ NOT REF_2 JUMPI 3 0 MSTORE REF_0 JUMP LABEL_2 - 2 2 MUL 10 ADD 40 EQ NOT REF_3 JUMPI + 40 10 2 2 MUL ADD EQ NOT REF_3 JUMPI 3 0 MSTORE 9 0 MSTORE 21 0 MSTORE REF_0 JUMP LABEL_3 - 123 32 MSTORE - 65450 64 MSTORE - LABEL_0 */ + 123 32 MSTORE + 65450 64 MSTORE + LABEL_0 + + */ SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, code); @@ -476,7 +483,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @@ -496,31 +503,31 @@ public class SerpentCompileTest { "else: \n" + " c=123\n" + " d=0xFFAA"; - String expected = "10 2 5 ADD GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 2 2 MUL 4 EQ NOT REF_2 JUMPI 3 0 MSTORE 0 MLOAD 3 EQ NOT REF_4 JUMPI 123 32 MSTORE REF_3 JUMP LABEL_4 LABEL_3 REF_0 JUMP LABEL_2 2 2 MUL 10 ADD 40 EQ NOT REF_5 JUMPI 3 0 MSTORE 9 0 MSTORE 21 0 MSTORE REF_0 JUMP LABEL_5 123 64 MSTORE 65450 96 MSTORE LABEL_0"; + String expected = "5 2 ADD 10 GT NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 4 2 2 MUL EQ NOT REF_2 JUMPI 3 0 MSTORE 3 0 MLOAD EQ NOT REF_4 JUMPI 123 32 MSTORE REF_3 JUMP LABEL_4 LABEL_3 REF_0 JUMP LABEL_2 40 10 2 2 MUL ADD EQ NOT REF_5 JUMPI 3 0 MSTORE 9 0 MSTORE 21 0 MSTORE REF_0 JUMP LABEL_5 123 64 MSTORE 65450 96 MSTORE LABEL_0"; /** - 10 2 5 ADD GT NOT REF_1 JUMPI - 2 0 MSTORE - REF_0 JUMP - LABEL_1 - 2 2 MUL 4 EQ NOT REF_2 JUMPI - 3 0 MSTORE - 0 MLOAD 3 EQ NOT REF_4 JUMPI - 123 32 MSTORE - REF_3 JUMP - LABEL_4 - LABEL_3 - REF_0 JUMP - LABEL_2 - 2 2 MUL 10 ADD 40 EQ NOT REF_5 JUMPI - 3 0 MSTORE - 9 0 MSTORE - 21 0 MSTORE - REF_0 JUMP - LABEL_5 - 123 64 MSTORE - 65450 96 MSTORE - LABEL_0 + 5 2 ADD 10 GT NOT REF_1 JUMPI + 2 0 MSTORE + REF_0 JUMP + LABEL_1 + 4 2 2 MUL EQ NOT REF_2 JUMPI + 3 0 MSTORE + 3 0 MLOAD EQ NOT REF_4 JUMPI + 123 32 MSTORE + REF_3 JUMP + LABEL_4 + LABEL_3 + REF_0 + JUMP LABEL_2 + 40 10 2 2 MUL ADD EQ NOT REF_5 JUMPI + 3 0 MSTORE + 9 0 MSTORE + 21 0 MSTORE + REF_0 JUMP + LABEL_5 + 123 64 MSTORE + 65450 96 MSTORE + LABEL_0 */ SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, @@ -531,13 +538,13 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 8 public void test23(){ - String code = "if (10 > 2 + 5) && (2 * 7 > 96): \n" + + String code = "if (10 >= 2 + 5) && (2 * 7 > 96): \n" + " a=2\n" + "elif 2*2==4: \n" + " a=3\n" + @@ -550,32 +557,31 @@ public class SerpentCompileTest { "else: \n" + " c=123\n" + " d=0xFFAA"; - String expected = "10 2 5 ADD GT 2 7 MUL 96 GT NOT NOT MUL NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 2 2 MUL 4 EQ NOT REF_2 JUMPI 3 0 MSTORE 0 MLOAD 3 EQ NOT REF_4 JUMPI 123 32 MSTORE REF_3 JUMP LABEL_4 LABEL_3 REF_0 JUMP LABEL_2 2 2 MUL 10 ADD 40 EQ NOT REF_5 JUMPI 3 0 MSTORE 9 0 MSTORE 21 0 MSTORE REF_0 JUMP LABEL_5 123 64 MSTORE 65450 96 MSTORE LABEL_0"; + String expected = "96 7 2 MUL GT 5 2 ADD 10 LT NOT NOT NOT MUL NOT REF_1 JUMPI 2 0 MSTORE REF_0 JUMP LABEL_1 4 2 2 MUL EQ NOT REF_2 JUMPI 3 0 MSTORE 3 0 MLOAD EQ NOT REF_4 JUMPI 123 32 MSTORE REF_3 JUMP LABEL_4 LABEL_3 REF_0 JUMP LABEL_2 40 10 2 2 MUL ADD EQ NOT REF_5 JUMPI 3 0 MSTORE 9 0 MSTORE 21 0 MSTORE REF_0 JUMP LABEL_5 123 64 MSTORE 65450 96 MSTORE LABEL_0"; /** - 10 2 5 ADD GT 2 7 MUL 96 GT NOT NOT MUL NOT REF_1 JUMPI - 2 0 MSTORE - REF_0 JUMP - LABEL_1 - 2 2 MUL 4 EQ NOT REF_2 JUMPI + 96 7 2 MUL GT 5 2 ADD 10 LT NOT NOT NOT MUL NOT REF_1 JUMPI + 2 0 MSTORE + REF_0 JUMP + LABEL_1 + 4 2 2 MUL EQ NOT REF_2 JUMPI 3 0 MSTORE - 0 MLOAD 3 EQ NOT REF_4 JUMPI - 123 32 MSTORE REF_3 JUMP - LABEL_4 - LABEL_3 + 3 0 MLOAD EQ NOT REF_4 JUMPI + 123 32 MSTORE + REF_3 JUMP + LABEL_4 + LABEL_3 REF_0 JUMP LABEL_2 - 2 2 MUL 10 ADD 40 EQ NOT REF_5 JUMPI + 40 10 2 2 MUL ADD EQ NOT REF_5 JUMPI 3 0 MSTORE 9 0 MSTORE 21 0 MSTORE REF_0 JUMP LABEL_5 - 123 64 MSTORE - 65450 96 MSTORE - - LABEL_0 - + 123 64 MSTORE + 65450 96 MSTORE + LABEL_0 */ SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, @@ -586,7 +592,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 9 @@ -598,22 +604,20 @@ public class SerpentCompileTest { " a = 30\n" + "if b == 40: \n" + " b = 50\n"; - String expected = "20 0 MSTORE 40 32 MSTORE 0 MLOAD 20 EQ NOT REF_1 JUMPI 30 0 MSTORE REF_0 JUMP LABEL_1 LABEL_0 32 MLOAD 40 EQ NOT REF_3 JUMPI 50 32 MSTORE REF_2 JUMP LABEL_3 LABEL_2"; + String expected = "20 0 MSTORE 40 32 MSTORE 20 0 MLOAD EQ NOT REF_1 JUMPI 30 0 MSTORE REF_0 JUMP LABEL_1 LABEL_0 40 32 MLOAD EQ NOT REF_3 JUMPI 50 32 MSTORE REF_2 JUMP LABEL_3 LABEL_2"; /** - 20 0 MSTORE - 40 32 MSTORE - 0 MLOAD 20 EQ NOT REF_1 JUMPI - 30 0 MSTORE - REF_0 JUMP - LABEL_1 - LABEL_0 - 32 MLOAD 40 EQ NOT REF_3 JUMPI - 50 32 MSTORE - REF_2 JUMP - LABEL_3 - LABEL_2 + 20 0 MSTORE + 40 32 MSTORE + 20 0 MLOAD EQ NOT REF_1 JUMPI + 30 0 MSTORE REF_0 JUMP + LABEL_1 + LABEL_0 + 40 32 MLOAD EQ NOT REF_3 JUMPI + 50 32 MSTORE + REF_2 JUMP + LABEL_3 LABEL_2 */ @@ -625,7 +629,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 10 @@ -638,23 +642,22 @@ public class SerpentCompileTest { "a = 70\n" + "if b == 40: \n" + " b = 50\n"; - String expected = "20 0 MSTORE 40 32 MSTORE 0 MLOAD 20 EQ NOT REF_1 JUMPI 30 0 MSTORE REF_0 JUMP LABEL_1 LABEL_0 70 0 MSTORE 32 MLOAD 40 EQ NOT REF_3 JUMPI 50 32 MSTORE REF_2 JUMP LABEL_3 LABEL_2"; + String expected = "20 0 MSTORE 40 32 MSTORE 20 0 MLOAD EQ NOT REF_1 JUMPI 30 0 MSTORE REF_0 JUMP LABEL_1 LABEL_0 70 0 MSTORE 40 32 MLOAD EQ NOT REF_3 JUMPI 50 32 MSTORE REF_2 JUMP LABEL_3 LABEL_2"; /** 20 0 MSTORE 40 32 MSTORE - 0 MLOAD 20 EQ NOT REF_1 JUMPI - 30 0 MSTORE - REF_0 JUMP - LABEL_1 - LABEL_0 + 20 0 MLOAD EQ NOT REF_1 JUMPI + 30 0 MSTORE + REF_0 JUMP + LABEL_1 + LABEL_0 70 0 MSTORE - 32 MLOAD 40 EQ NOT REF_3 JUMPI - 50 32 MSTORE - REF_2 JUMP - LABEL_3 - LABEL_2 + 40 32 MLOAD EQ NOT REF_3 JUMPI + 50 32 MSTORE + REF_2 JUMP + LABEL_3 LABEL_2 */ @@ -666,7 +669,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 11 @@ -677,14 +680,14 @@ public class SerpentCompileTest { " if 4>3:\n" + " if 5>4:\n" + " a = 10\n"; - String expected = "2 1 GT NOT REF_7 JUMPI 3 2 GT NOT REF_6 JUMPI 4 3 GT NOT REF_5 JUMPI 5 4 GT NOT REF_4 JUMPI 10 0 MSTORE REF_3 JUMP LABEL_4 LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0"; + String expected = "1 2 GT NOT REF_7 JUMPI 2 3 GT NOT REF_6 JUMPI 3 4 GT NOT REF_5 JUMPI 4 5 GT NOT REF_4 JUMPI 10 0 MSTORE REF_3 JUMP LABEL_4 LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0"; /** - 2 1 GT NOT REF_7 JUMPI - 3 2 GT NOT REF_6 JUMPI - 4 3 GT NOT REF_5 JUMPI - 5 4 GT NOT REF_4 JUMPI + 1 2 GT NOT REF_7 JUMPI + 2 3 GT NOT REF_6 JUMPI + 3 4 GT NOT REF_5 JUMPI + 4 5 GT NOT REF_4 JUMPI 10 0 MSTORE REF_3 JUMP LABEL_4 LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0 @@ -699,7 +702,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @Test // if elif else test 12 @@ -710,18 +713,17 @@ public class SerpentCompileTest { " if 4>3:\n" + " if 5>4:\n" + " a = 10\n"; - String expected = "2 1 GT NOT REF_7 JUMPI 3 2 GT NOT REF_6 JUMPI 4 3 GT NOT REF_5 JUMPI 5 4 GT NOT REF_4 JUMPI 10 0 MSTORE REF_3 JUMP LABEL_4 LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0"; + String expected = "1 2 GT NOT REF_7 JUMPI 2 3 GT NOT REF_6 JUMPI 3 4 GT NOT REF_5 JUMPI 4 5 GT NOT REF_4 JUMPI 10 0 MSTORE REF_3 JUMP LABEL_4 LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0"; /** - 2 1 GT NOT REF_7 JUMPI - 3 2 GT NOT REF_6 JUMPI - 4 3 GT NOT REF_5 JUMPI - 5 4 GT NOT REF_4 JUMPI - 10 0 MSTORE - REF_3 JUMP - LABEL_4 LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0 - + 1 2 GT NOT REF_7 JUMPI + 2 3 GT NOT REF_6 JUMPI + 3 4 GT NOT REF_5 JUMPI + 4 5 GT NOT REF_4 JUMPI + 10 0 MSTORE + REF_3 JUMP + LABEL_4 LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0 */ SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, @@ -732,7 +734,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @@ -746,19 +748,20 @@ public class SerpentCompileTest { " a = 10\n" + " else:\n" + " b = 20\n"; - String expected = "2 1 GT NOT REF_7 JUMPI 3 2 GT NOT REF_6 JUMPI 4 3 GT NOT REF_5 JUMPI 5 4 GT NOT REF_4 JUMPI 10 0 MSTORE REF_3 JUMP LABEL_4 20 32 MSTORE LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0"; + String expected = "1 2 GT NOT REF_7 JUMPI 2 3 GT NOT REF_6 JUMPI 3 4 GT NOT REF_5 JUMPI 4 5 GT NOT REF_4 JUMPI 10 0 MSTORE REF_3 JUMP LABEL_4 20 32 MSTORE LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0"; /** - 2 1 GT NOT REF_7 JUMPI - 3 2 GT NOT REF_6 JUMPI - 4 3 GT NOT REF_5 JUMPI - 5 4 GT NOT REF_4 JUMPI - 10 0 MSTORE REF_3 JUMP - LABEL_4 - 20 32 MSTORE - LABEL_3 - REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0 + 1 2 GT NOT REF_7 JUMPI + 2 3 GT NOT REF_6 JUMPI + 3 4 GT NOT REF_5 JUMPI + 4 5 GT NOT REF_4 JUMPI + 10 0 MSTORE + REF_3 JUMP + LABEL_4 + 20 32 MSTORE + LABEL_3 REF_2 JUMP LABEL_5 LABEL_2 REF_1 JUMP LABEL_6 LABEL_1 REF_0 JUMP LABEL_7 LABEL_0 + */ SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, @@ -769,7 +772,7 @@ public class SerpentCompileTest { result = result.replaceAll("\\s+", " "); result = result.trim(); - Assert.assertEquals(result, expected); + Assert.assertEquals(expected, result); } @@ -848,9 +851,399 @@ public class SerpentCompileTest { Assert.fail("Should be indent error thrown"); } + @Test // if elif else test 17 + public void test32(){ + + String code = "if 2*2==4: \n" + + " if 3*3==9: \n" + + " if 4*4==16: \n" + + " a=20 \n" + + " else: \n" + + " b=20 \n"; + String expected = "4 2 2 MUL EQ NOT REF_5 JUMPI 9 3 3 MUL EQ NOT REF_4 JUMPI 16 4 4 MUL EQ NOT REF_3 JUMPI 20 0 MSTORE REF_2 JUMP LABEL_3 LABEL_2 REF_1 JUMP LABEL_4 20 32 MSTORE LABEL_1 REF_0 JUMP LABEL_5 LABEL_0"; + + /** + + 4 2 2 MUL EQ NOT REF_5 JUMPI + 9 3 3 MUL EQ NOT REF_4 JUMPI + 16 4 4 MUL EQ NOT REF_3 JUMPI + 20 0 MSTORE + REF_2 JUMP + LABEL_3 LABEL_2 REF_1 JUMP LABEL_4 + 20 32 MSTORE + LABEL_1 REF_0 JUMP LABEL_5 LABEL_0 + */ + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + ParseTree tree = parser.parse(); + + String result = new SerpentToAssemblyCompiler().visit(tree); + result = result.replaceAll("\\s+", " "); + result = result.trim(); + + Assert.assertEquals(expected, result); + } + + + + @Test // if elif else test 18 + public void test33(){ + + String code = "if 2*2==4: \n" + + " if 3*3==9: \n" + + " if 4*4==16: \n" + + " a=20 \n" + + " else: \n" + + " \n"; + String expected = ""; + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + + ParseTree tree = null; + try { + tree = parser.parse(); + } catch (Throwable e) { + + Assert.assertTrue(e instanceof ParserUtils.AntlrParseException); + return; + } + + Assert.fail("Should be indent error thrown"); + } + + + @Test // if elif else test 19 + public void test34(){ + + String code = "if 2>1: \n" + + " if 3>2: \n" + + " if 4>3: \n" + + " if 5>4: \n" + + " a = 10\n" + + " else: \n" + + " b = 20\n" + + " elif 2*2 != 4: \n" + + " a = 15\n"; + String expected = "1 2 GT NOT REF_8 JUMPI 2 3 GT NOT REF_7 JUMPI 3 4 GT NOT REF_5 JUMPI 4 5 GT NOT REF_4 JUMPI 10 0 MSTORE REF_3 JUMP LABEL_4 20 32 MSTORE LABEL_3 REF_2 JUMP LABEL_5 4 2 2 MUL EQ NOT NOT REF_6 JUMPI 15 0 MSTORE REF_2 JUMP LABEL_6 LABEL_2 REF_1 JUMP LABEL_7 LABEL_1 REF_0 JUMP LABEL_8 LABEL_0"; + + /** + + 1 2 GT NOT REF_8 JUMPI + 2 3 GT NOT REF_7 JUMPI + 3 4 GT NOT REF_5 JUMPI + 4 5 GT NOT REF_4 JUMPI + 10 0 MSTORE + REF_3 JUMP + LABEL_4 + 20 32 MSTORE + LABEL_3 REF_2 JUMP + LABEL_5 + 4 2 2 MUL EQ NOT NOT REF_6 JUMPI + 15 0 MSTORE + REF_2 JUMP LABEL_6 LABEL_2 REF_1 JUMP LABEL_7 LABEL_1 REF_0 JUMP LABEL_8 LABEL_0 + + */ + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + ParseTree tree = parser.parse(); + + String result = new SerpentToAssemblyCompiler().visit(tree); + result = result.replaceAll("\\s+", " "); + result = result.trim(); + + Assert.assertEquals(expected, result); + } + + + + @Test // if elif else test 20 + public void test35(){ + + String code = "if 2>1: \n" + + " a=20 \n" + + "elif 5<1: \n" + + " a=30 \n" + + "elif 6>6: \n" + + " a=40 \n" + + "else: \n" + + " a=50 \n" ; + + String expected = "1 2 GT NOT REF_1 JUMPI 20 0 MSTORE REF_0 JUMP LABEL_1 1 5 LT NOT REF_2 JUMPI 30 0 MSTORE REF_0 JUMP LABEL_2 6 6 GT NOT REF_3 JUMPI 40 0 MSTORE REF_0 JUMP LABEL_3 50 0 MSTORE LABEL_0"; + + /** + + 1 2 GT NOT REF_1 JUMPI + 20 0 MSTORE + REF_0 JUMP + LABEL_1 + 1 5 LT NOT REF_2 JUMPI + 30 0 MSTORE + REF_0 JUMP + LABEL_2 + 6 6 GT NOT REF_3 JUMPI + 40 0 MSTORE + REF_0 JUMP + LABEL_3 + 50 0 MSTORE + LABEL_0 + + */ + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + ParseTree tree = parser.parse(); + + String result = new SerpentToAssemblyCompiler().visit(tree); + result = result.replaceAll("\\s+", " "); + result = result.trim(); + + Assert.assertEquals(expected, result); + } + + + @Test // while test 1 + public void test36(){ + + String code = "a = 20 \n" + + "while a>0: \n" + + " a = a - 1 \n" ; + + String expected = "20 0 MSTORE LABEL_0 0 0 MLOAD GT EQ NOT REF_1 JUMPI 1 0 MLOAD SUB 0 MSTORE REF_0 JUMP LABEL_1"; + + /** + + 20 0 MSTORE + LABEL_0 + 0 0 MLOAD GT EQ NOT REF_1 JUMPI + 1 0 MLOAD SUB 0 MSTORE + REF_0 JUMP + LABEL_1 + + */ + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + ParseTree tree = parser.parse(); + + String result = new SerpentToAssemblyCompiler().visit(tree); + result = result.replaceAll("\\s+", " "); + result = result.trim(); + + Assert.assertEquals(expected, result); + } + + @Test // while test 2 + public void test37(){ + + String code = "x = 248 \n" + + "while x > 1: \n" + + " if (x % 2) == 0: \n" + + " x = x / 2 \n" + + " else: \n " + + " x = 3 * x + 1 \n" ; + + String expected = "248 0 MSTORE LABEL_0 1 0 MLOAD GT EQ NOT REF_1 JUMPI 0 2 0 MLOAD MOD EQ NOT REF_3 JUMPI 2 0 MLOAD DIV 0 MSTORE REF_2 JUMP LABEL_3 1 0 MLOAD 3 MUL ADD 0 MSTORE LABEL_2 REF_0 JUMP LABEL_1"; + + /** + + 248 0 MSTORE + LABEL_0 + 1 0 MLOAD GT EQ NOT REF_1 JUMPI + 0 2 0 MLOAD MOD EQ NOT REF_3 JUMPI + 2 0 MLOAD DIV 0 MSTORE + REF_2 JUMP + LABEL_3 + 1 0 MLOAD 3 MUL ADD 0 MSTORE + LABEL_2 + + REF_0 JUMP + LABEL_1 + + */ + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + ParseTree tree = parser.parse(); + + String result = new SerpentToAssemblyCompiler().visit(tree); + result = result.replaceAll("\\s+", " "); + result = result.trim(); + + Assert.assertEquals(expected, result); + } + + + @Test // while test 3 + public void test38(){ + + String code = "x = 0xFF \n" + + "while x > 1: \n" + + " if (x % 2) == 0: \n" + + " x = x / 2 \n" + + "x = x +2 \n" + + "x = 3 * x + 1 \n" ; + + String expected = "255 0 MSTORE LABEL_0 1 0 MLOAD GT EQ NOT REF_1 JUMPI 0 2 0 MLOAD MOD EQ NOT REF_3 JUMPI 2 0 MLOAD DIV 0 MSTORE REF_2 JUMP LABEL_3 LABEL_2 REF_0 JUMP LABEL_1 2 0 MLOAD ADD 0 MSTORE 1 0 MLOAD 3 MUL ADD 0 MSTORE"; + + /** + + 255 0 MSTORE + LABEL_0 + 1 0 MLOAD GT EQ NOT REF_1 JUMPI + 0 2 0 MLOAD MOD EQ NOT REF_3 JUMPI + 2 0 MLOAD DIV 0 MSTORE REF_2 JUMP + LABEL_3 LABEL_2 + REF_0 JUMP + LABEL_1 + 2 0 MLOAD ADD 0 MSTORE + 1 0 MLOAD 3 MUL ADD 0 MSTORE + + */ + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + ParseTree tree = parser.parse(); + + String result = new SerpentToAssemblyCompiler().visit(tree); + result = result.replaceAll("\\s+", " "); + result = result.trim(); + + Assert.assertEquals(expected, result); + } + + @Test // while test 4 + public void test39(){ + + String code = "x = 0xFF\n" + + "while x > 1:\n" + + "x = x +2\n" + + "x = 3 * x + 1" ; + + String expected = ""; + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + + ParseTree tree = null; + try { + tree = parser.parse(); + } catch (Throwable e) { + + Assert.assertTrue(e instanceof ParserUtils.AntlrParseException); + return; + } + + Assert.fail("Should be indent error thrown"); + } + + + @Test // while test 5 + public void test40(){ + + String code = "x = 0xFF\n" + + "while (x > 1) && (x > 2) && (x > 3) && (2 <9):\n" + + " x = x -2\n" ; + + String expected = "255 0 MSTORE LABEL_0 9 2 LT 3 0 MLOAD GT 2 0 MLOAD GT 1 0 MLOAD GT NOT NOT MUL NOT NOT MUL NOT NOT MUL EQ NOT REF_1 JUMPI 2 0 MLOAD SUB 0 MSTORE REF_0 JUMP LABEL_1"; + + /** + + 255 0 MSTORE + LABEL_0 + 9 2 LT 3 0 MLOAD GT 2 0 MLOAD GT 1 0 MLOAD GT NOT NOT MUL NOT NOT MUL NOT NOT MUL EQ NOT REF_1 JUMPI + 2 0 MLOAD SUB 0 MSTORE + REF_0 JUMP + LABEL_1 + + */ + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + ParseTree tree = parser.parse(); + + String result = new SerpentToAssemblyCompiler().visit(tree); + result = result.replaceAll("\\s+", " "); + result = result.trim(); + + Assert.assertEquals(expected, result); + } + + @Test // special functions test 1 + public void test41(){ + + String code = "a = msg.datasize\n" + + "b = msg.sender\n" + + "c = msg.value\n" + + "d = tx.gasprice\n" + + "e = tx.origin\n" + + "f = tx.gas\n" + + "g = contract.balance\n" + + "h = block.prevhash\n" + + "i = block.coinbase\n" + + "j = block.timestamp\n" + + "k = block.number\n" + + "l = block.difficulty\n" + + "m = block.gaslimit\n" ; + + String expected = "32 CALLDATASIZE DIV 0 MSTORE CALLER 32 MSTORE CALLVALUE 64 MSTORE GASPRICE 96 MSTORE ORIGIN 128 MSTORE GAS 160 MSTORE BALANCE 192 MSTORE PREVHASH 224 MSTORE COINBASE 256 MSTORE TIMESTAMP 288 MSTORE NUMBER 320 MSTORE DIFFICULTY 352 MSTORE GASLIMIT 384 MSTORE"; + + /** + + 32 CALLDATASIZE DIV 0 MSTORE + CALLER 32 MSTORE + CALLVALUE 64 MSTORE + GASPRICE 96 MSTORE + ORIGIN 128 MSTORE + GAS 160 MSTORE + BALANCE 192 MSTORE + PREVHASH 224 MSTORE + COINBASE 256 MSTORE + TIMESTAMP 288 MSTORE + NUMBER 320 MSTORE + DIFFICULTY 352 MSTORE + GASLIMIT 384 MSTORE + + */ + + SerpentParser parser = ParserUtils.getParser(SerpentLexer.class, SerpentParser.class, + code); + ParseTree tree = parser.parse(); + + String result = new SerpentToAssemblyCompiler().visit(tree); + result = result.replaceAll("\\s+", " "); + result = result.trim(); + + Assert.assertEquals(expected, result); + } + + + +/* + +a = msg.datasize +b = msg.sender +c = msg.value +d = tx.gasprice +e = tx.origin +f = tx.gas +g = contract.balance +h = block.prevhash +i = block.coinbase +j = block.timestamp +k = block.number +l = block.difficulty +m = block.gaslimit + +*/ + /** - * todo: more testing for if-elif-else 4 tests - * todo: more testing for special functions 10 - * todo: more testing for while function 10 + * + * todo: return testing + * todo: memory_storage testing + * todo: message access testing + * */ } diff --git a/ethereumj-core/src/test/java/org/ethereum/util/RLPTest.java b/ethereumj-core/src/test/java/org/ethereum/util/RLPTest.java index 08fe9777..75f7f267 100644 --- a/ethereumj-core/src/test/java/org/ethereum/util/RLPTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/util/RLPTest.java @@ -688,7 +688,7 @@ public class RLPTest { String blockRaw = "f8cbf8c7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a02f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817da00000000000000000000000000000000000000000000000000000000000000000834000008080830f4240808080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829c0c0"; byte[] payload = Hex.decode(blockRaw); - final int ITERATIONS = 10000000; + final int ITERATIONS = 1; RLPList list = null; DecodeResult result = null; System.out.println("Starting " + ITERATIONS + " decoding iterations...");