Deleting old Serpent compiler implementation

This commit is contained in:
romanman 2014-05-13 19:31:02 +03:00
parent 53e456556e
commit f1d292a2b4
8 changed files with 3 additions and 4559 deletions

View File

@ -1,6 +1,5 @@
package org.ethereum.gui;
import org.ethereum.serpent.SerpentCompiler;
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
@ -83,7 +82,9 @@ public class SerpentEditor extends JFrame {
String asmResult = "";
try {
asmResult = SerpentCompiler.compile(codeArea.getText());
// todo: integrate new compiler when avail
//asmResult = SerpentCompiler.compile(codeArea.getText());
} catch (Throwable th) {th.printStackTrace();}
splitPanel.setDividerLocation(0.7);

View File

@ -1,19 +0,0 @@
package org.ethereum.serpent;
/**
* www.ethereumJ.com
* User: Roman Mandeleil
* Created on: 25/04/14 17:06
*/
public class GenParser {
public static void main(String args[]){
String userDir = System.getProperty("user.dir");
org.antlr.Tool.main(new String[]{userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent.g"});
// org.antlr.Tool.main(new String[]{userDir + "\\src\\main\\java\\samples\\antlr\\PyEsque.g"});
}
}

View File

@ -1,248 +0,0 @@
/*******************************************************************************
* Ethereum high level language grammar definition
*******************************************************************************/
grammar Serpent;
options {
//language = Java;
//output = AST;
output = template;
}
@header {
/* (!!!) Do not update this file manually ,
* It was auto generated from the Serpent.g
* grammar file.
*/
package org.ethereum.serpent;
import org.ethereum.util.Utils;
}
@lexer::header {
/* (!!!) Do not update this file manually ,
* It was auto generated from the Serpent.g
* grammar file.
*/
package org.ethereum.serpent;
}
@members {
private java.util.ArrayList<String> globals = new java.util.ArrayList<String>();
private int labelIndex = 0;
}
program
: gen_body
{
String conVars = "";
if (globals.size() > 0){
conVars = "0 " + (globals.size() * 32 - 1) + " MSTORE8";
}
}
-> concat(left={conVars}, right={$gen_body.st})
;
test_1
:
(
set_var -> concat(left={$test_1.st}, right={$set_var.st})
| get_var -> concat(left={$test_1.st}, right={$get_var.st}) )*
;
/*
test_2
:
(
set_var -> concat(left={$test_2.st}, right={$set_var.st})
)*
(
if_stmt -> concat(left={$test_2.st}, right={$if_stmt.st})
| if_else_stmt -> concat(left={$test_2.st}, right={$if_else_stmt.st})
)
;
*/
gen_body
:
(
set_var -> concat(left={$gen_body.st}, right={$set_var.st})
| storage_save -> concat(left={$gen_body.st}, right={$storage_save.st})
| return_stmt -> concat(left={$gen_body.st}, right={$return_stmt.st})
)* ( if_else_stmt -> concat(left={$gen_body.st}, right={$if_else_stmt.st}))?
;
// [if a==10:\n b=20 \n]
// [10, 0, 'MLOAD', 'EQ', 'NOT', 'REF_0', 'JUMPI', 20, 32, 'MSTORE', 'LABEL_0']
if_stmt
:
'if' unr_expr ':' gen_body
// (!!!)RECURSION ON if_stmt ( 'elif' cond_expr ':' elif_body=set_var )*
-> ifStmt(cond={$unr_expr.st}, body={$gen_body.st}, index={labelIndex++})
;
// [if a==10:\n b=20 \nelse: \n b=30]
// [10, 0, 'MLOAD', 'EQ', 'NOT', 'REF_1', 'JUMPI', 20, 32, 'MSTORE', 'REF_0', 'JUMP', 'LABEL_1', 30, 32, 'MSTORE', 'LABEL_0']
// a 10 EQ NOT REF_01 JUMPI 30 0 MSTORE REF_0 JUMP LABEL_01 20 0 MSTORE LABEL_0
if_else_stmt
:
'if' unr_expr ':' if_body=gen_body
// (!!!)RECURSION ON if_stmt ( 'elif' unr_expr ':' elif_body=set_var )*
'else' ':' else_body=gen_body -> ifElseStmt(cond={$unr_expr.st}, if_body={$if_body.st},
else_body={$else_body.st}, if_index={labelIndex++}, else_index={labelIndex++}) ;
// [multi_set_var] (a=set_var->compile(left={$program.st}, right={$a.st}))*
set_var
:
{int varIndex = -1;}
(a=var { // TODO: change it from atom to something else
// check if that variable already defined
// if it didn't add it to list
// if it did use the index * 32 for memory address
varIndex = globals.indexOf($a.st.toString());
if (varIndex == -1 ) {globals.add($a.st.toString()); varIndex = globals.size() - 1; }
}
'=' b=bin_expr) -> set_var(param_a={$b.st}, param_b={32 * varIndex})
;
get_var
:
{int varIndex = -1;}
(a=var {
// If there is no such var throw exception
varIndex = globals.indexOf($a.st.toString());
if (varIndex == -1 ) {
Error err = new Error("var undefined: " + $a.st.toString());
throw err;
}
;}
-> get_var(varIndex={32 * varIndex})
)
;
unr_expr
:
{boolean negative = false;}
('!' {negative = !negative;} )* a=cond_expr -> not(param={negative? $a.st : $a.st + " NOT"})
;
cond_expr
: a=bin_expr
(
'==' b=bin_expr -> equals(left={$a.st},right={$b.st}) |
'<' b=bin_expr -> lessThan(left={$a.st},right={$b.st}) |
'<=' b=bin_expr -> lessEqThan(left={$a.st},right={$b.st}) |
'>=' b=bin_expr -> greatEqThan(left={$a.st},right={$b.st}) |
'>' b=bin_expr -> greatThan(left={$a.st},right={$b.st})
| -> {$a.st}
)
;
storage_save
: 'contract.storage['index=bin_expr']' '=' assignment=bin_expr -> ssave(index={$index.st}, data={$assignment.st})
;
bin_expr
: (a=atom -> {$a.st})
(( '+' b=atom -> add(left={$bin_expr.st}, right={$b.st}) |
'-' b=atom -> sub(left={$bin_expr.st}, right={$b.st}) |
'*' b=atom -> mul(left={$bin_expr.st}, right={$b.st}) |
'/' b=atom -> div(left={$bin_expr.st}, right={$b.st}) |
'^' b=atom -> exp(left={$bin_expr.st}, right={$b.st}) |
'%' b=atom -> mod(left={$bin_expr.st}, right={$b.st}) |
'#/' b=atom -> sdiv(left={$bin_expr.st}, right={$b.st}) |
'#%' b=atom -> smod(left={$bin_expr.st}, right={$b.st})
)*)
;
// "if !a==10:\n b=20 \nelse: \n b=30"
atom
:
storage_load -> iconst(value={$storage_load.st})
| msg_sender -> iconst(value={$msg_sender.st})
| msg_datasize -> iconst(value={$msg_datasize.st})
| msg_load -> iconst(value={$msg_load.st})
| get_var -> iconst(value={$get_var.st})
| INTEGER -> iconst(value={$INTEGER.text})
| hex_num -> iconst(value={$hex_num.st})
;
hex_num
:
HEX_NUMBER
{
String dec_num = Utils.hexStringToDecimalString($HEX_NUMBER.text);
}
-> iconst(value={dec_num})
;
var
: IDENT -> refVar(id={$IDENT.text} )
;
storage_load
: 'contract.storage['bin_expr']' -> sload(index={$bin_expr.st})
;
msg_load
: 'msg.data['bin_expr']' -> calldataload(index={$bin_expr.st})
;
msg_sender
: 'msg.sender' -> msdSender()
;
msg_datasize
: 'msg.datasize' -> msgDatasize()
;
return_stmt
: 'return('bin_expr')' -> returnStmt(index={$bin_expr.st})
;
fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
INTEGER : DIGIT+ ;
IDENT : LETTER (LETTER | DIGIT)*;
fragment HEX_DIGIT : ('0'..'9' | 'a'..'f' | 'A'..'F');
HEX_NUMBER : ('0x' | '0X' )HEX_DIGIT+;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};
COMMENT : '//' .* ('\n'|'\r') {$channel = HIDDEN;};

View File

@ -1,116 +0,0 @@
group Serpent2Asm;
/** https://github.com/rollxx/antlr-php-runtime/blob/master/examples/cminus/Bytecode.stg */
// [10, 0, 'MLOAD', 'EQ', 'NOT', 'REF_0', 'JUMPI', 20, 32, 'MSTORE', 'LABEL_0']
ifStmt(cond, body, index) ::=<<
<cond> NOT REF_<index> JUMPI <body> LABEL_<index>
>>
// [10, 0, MLOAD, EQ, NOT, REF_1, JUMPI, 20, 32, MSTORE, REF_0, JUMP, LABEL_1, 30, 32, MSTORE, LABEL_0]
ifElseStmt(cond, if_body, else_body, if_index, else_index) ::=<<
<cond> NOT REF_<if_index> JUMPI <else_body> REF_<else_index> JUMP LABEL_<if_index> <if_body> LABEL_<else_index>
>>
set_var(param_a, param_b) ::= <<
<param_a> <param_b> MSTORE
>>
get_var(varIndex) ::= <<
<varIndex> MLOAD
>>
compile(left, right) ::= <<
<left> <right>
>>
add(left,right) ::= <<
<left> <right> ADD
>>
sub(left,right) ::= <<
<left> <right> SUB
>>
mul(left,right) ::= <<
<left> <right> MUL
>>
div(left,right) ::= <<
<left> <right> DIV
>>
exp(left,right) ::= <<
<left> <right> EXP
>>
mod(left,right) ::= <<
<left> <right> MOD
>>
sdiv(left,right) ::= <<
<left> <right> SDIV
>>
smod(left,right) ::= <<
<left> <right> SMOD
>>
equals(left,right) ::= <<
<left> <right> EQ
>>
lessThan(left,right) ::= <<
<left> <right> LT
>>
lessEqThan(left,right) ::= <<
<left> <right> GT NOT
>>
greatThan(left,right) ::= <<
<left> <right> GT
>>
greatEqThan(left,right) ::= <<
<left> <right> LT NOT
>>
not(param) ::= <<
<param> NOT
>>
sload(index) ::= <<
<index> SLOAD
>>
ssave(index, data) ::= <<
<data> <index> SSTORE
>>
calldataload(index) ::=<<
<index> 32 MUL CALLDATALOAD
>>
msdSender() ::=<<
CALLER
>>
msgDatasize() ::=<<
32 CALLDATASIZE DIV
>>
returnStmt(index) ::= <<
<index> MSIZE SWAP MSIZE MSTORE 32 SWAP RETURN
>>
concat(left, right) ::= <<
<left> <right>
>>
refVar(id) ::= "<id>"
iconst(value) ::= "<value>"

View File

@ -1,41 +0,0 @@
package org.ethereum.serpent;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
import java.io.FileNotFoundException;
import java.io.FileReader;
/**
* www.ethereumJ.com
* User: Roman Mandeleil
* Created on: 29/04/14 12:34
*/
public class SerpentCompiler {
public static String compile(String code) throws FileNotFoundException,
RecognitionException {
CharStream stream = new ANTLRStringStream(code);
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir
+ "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(
templateFileName), AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.program_return retVal = parser.program();
return retVal.getTemplate().toString().trim();
}
}

View File

@ -1,991 +0,0 @@
package org.ethereum.serpent;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
import org.ethereum.util.Utils;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import static org.junit.Assert.*;
public class TestCompile {
/* bin_expr
['+', 2, 1, ['<1>', '<0>', 'ADD']], V
['-', 2, 1, ['<1>', '<0>', 'SUB']], V
['*', 2, 1, ['<1>', '<0>', 'MUL']], V
['/', 2, 1, ['<1>', '<0>', 'DIV']], V
['^', 2, 1, ['<1>', '<0>', 'EXP']], V
['%', 2, 1, ['<1>', '<0>', 'MOD']], V
['#/', 2, 1, ['<1>', '<0>', 'SDIV']], V
['#%', 2, 1, ['<1>', '<0>', 'SMOD']], V
*/
@Test /* Test one symbol */
public void test0() throws FileNotFoundException, RecognitionException {
CharStream stream = new ANTLRStringStream("" + "A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A", retVal.getTemplate().toString());
}
@Test /* Test ADD 1*/
public void test1() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A + B");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B ADD", retVal.getTemplate().toString());
}
@Test /* Test ADD 2*/
public void test2() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A + B + C1");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B ADD C1 ADD", retVal.getTemplate().toString());
}
@Test /* Test SUB 1*/
public void test3() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A - B");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B SUB", retVal.getTemplate().toString());
}
@Test /* Test MUL 1*/
public void test4() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A * B");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B MUL", retVal.getTemplate().toString());
}
@Test /* Test DIV 1*/
public void test5() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A / B");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B DIV", retVal.getTemplate().toString());
}
@Test /* Test EXP 1*/
public void test6() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A ^ B");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B EXP", retVal.getTemplate().toString());
}
@Test /* Test MOD 1*/
public void test7() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A % B");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B MOD", retVal.getTemplate().toString());
}
@Test /* Test SDIV 1*/
public void test8() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A #/ B");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B SDIV", retVal.getTemplate().toString());
}
@Test /* Test SMOD 1*/
public void test9() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A #% B");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B SMOD", retVal.getTemplate().toString());
}
@Test /* Test multi binary operators 1*/
public void test10() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A / B - C + D * ET");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B DIV C SUB D ADD ET MUL", retVal.getTemplate().toString());
}
@Test /* Test multi binary operators 2*/
public void test11() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A / B - C + D * ET % ET2 ^ RO + RO2 #/ COOL #% HOT");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("A B DIV C SUB D ADD ET MUL ET2 MOD RO EXP RO2 ADD COOL SDIV HOT SMOD",
retVal.getTemplate().toString());
}
/*
['==', 2, 1, ['<1>', '<0>', 'EQ']], V
['<', 2, 1, ['<1>', '<0>', 'LT']], V
['<=', 2, 1, ['<1>', '<0>', 'GT', 'NOT']], V
['>', 2, 1, ['<1>', '<0>', 'GT']], V
['>=', 2, 1, ['<1>', '<0>', 'LT', 'NOT']], V
['!', 1, 1, ['<0>', 'NOT']], V
*/
@Test /* Test '==' 1*/
public void test12() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A + B == B + A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.cond_expr_return retVal = parser.cond_expr();
assertEquals("A B ADD B A ADD EQ",
retVal.getTemplate().toString());
}
@Test /* Test '<' 1*/
public void test13() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A + C < C + A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.cond_expr_return retVal = parser.cond_expr();
assertEquals("A C ADD C A ADD LT",
retVal.getTemplate().toString());
}
@Test /* Test '<=' 1*/
public void test14() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A + C <= C + A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.cond_expr_return retVal = parser.cond_expr();
assertEquals("A C ADD C A ADD GT NOT",
retVal.getTemplate().toString());
}
@Test /* Test '>' 1*/
public void test15() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A + C > C + A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.cond_expr_return retVal = parser.cond_expr();
assertEquals("A C ADD C A ADD GT",
retVal.getTemplate().toString());
}
@Test /* Test '>=' 1*/
public void test16() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A + C >= C + A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.cond_expr_return retVal = parser.cond_expr();
assertEquals("A C ADD C A ADD LT NOT",
retVal.getTemplate().toString());
}
@Test /* Test '!' 1 */
public void test17() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"!A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.unr_expr_return retVal = parser.unr_expr();
assertEquals("A NOT",
retVal.getTemplate().toString());
}
@Test /* Test '!' 2 */
public void test18() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"!!A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.unr_expr_return retVal = parser.unr_expr();
assertEquals("A NOT NOT",
retVal.getTemplate().toString());
}
@Test /* Test '!' 3 */
public void test19() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.unr_expr_return retVal = parser.unr_expr();
assertEquals("A NOT NOT",
retVal.getTemplate().toString());
}
@Test /* Test '!' 4 */
public void test20() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"!!!!!!!!!A");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.unr_expr_return retVal = parser.unr_expr();
assertEquals("A NOT",
retVal.getTemplate().toString());
}
@Test /* Test set var '=' 1 */
public void test21() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A=10 \n B=20 \n C=30");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.program_return retVal = parser.program();
assertEquals("10 0 MSTORE 20 32 MSTORE 30 64 MSTORE",
retVal.getTemplate().toString().trim());
}
@Test /* Test set var '=' 2 */
public void test22() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"A=10 \n B=20 \n A=30 \n B=40");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.program_return retVal = parser.program();
assertEquals("10 0 MSTORE 20 32 MSTORE 30 0 MSTORE 40 32 MSTORE",
retVal.getTemplate().toString().trim());
}
@Test /* Test if stmt 1 */
public void test23() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"if a==10:\n b=20 \nelse: \n b=30");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.if_else_stmt_return retVal = parser.if_else_stmt();
assertEquals("",
retVal.getTemplate().toString().trim());
}
@Test /* Test contract.storage[x] 1 */
public void test24() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"contract.storage[0]");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.storage_load_return retVal = parser.storage_load();
assertEquals("0 SLOAD",
retVal.getTemplate().toString().trim());
}
@Test /* Test contract.storage[x] 2 */
public void test25() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"contract.storage[100]");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.storage_load_return retVal = parser.storage_load();
assertEquals("100 SLOAD",
retVal.getTemplate().toString().trim());
}
@Test /* Test contract.storage[x]=y 1 */
public void test26() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"contract.storage[100]=200");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.storage_save_return retVal = parser.storage_save();
assertEquals("200 100 SSTORE",
retVal.getTemplate().toString().trim());
}
@Test /* Test contract.storage[x]=y 1_ */
public void test26_1() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"contract.storage[3+4]=200");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.storage_save_return retVal = parser.storage_save();
assertEquals("200 3 4 ADD SSTORE",
retVal.getTemplate().toString().trim());
}
@Test /* Test contract.storage[x]=y 2 */
public void test27() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"contract.storage[100]=200+100");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.storage_save_return retVal = parser.storage_save();
assertEquals("200 100 ADD 100 SSTORE", // todo: have to optimize it somewhere in the future
retVal.getTemplate().toString().trim());
}
@Test /* Test msg.data[x] 1 */
public void test28() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"msg.data[0]");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.msg_load_return retVal = parser.msg_load();
assertEquals("0 32 MULL CALLDATALOAD",
retVal.getTemplate().toString().trim());
}
@Test /* Test msg.data[x] 2 */
public void test29() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"msg.data[10+ 20]");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.msg_load_return retVal = parser.msg_load();
assertEquals("10 20 ADD 32 MUL CALLDATALOAD",
retVal.getTemplate().toString().trim());
}
@Test /* Test msg.data[x] 3 */
public void test30() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"msg.data[0] + msg.data[2]");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.bin_expr_return retVal = parser.bin_expr();
assertEquals("0 32 MUL CALLDATALOAD 2 32 MUL CALLDATALOAD ADD",
retVal.getTemplate().toString().trim());
}
@Test /* Test multi */
public void test31() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"contract.storage[msg.data[0]] = msg.data[1]\n" +
"");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.storage_save_return retVal = parser.storage_save();
assertEquals("1 32 MUL CALLDATALOAD 0 32 MUL CALLDATALOAD SSTORE",
retVal.getTemplate().toString().trim());
}
@Test /* Test multi */
public void test32() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"!contract.storage[msg.data[0]]");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.unr_expr_return retVal = parser.unr_expr();
assertEquals("0 32 MUL CALLDATALOAD SLOAD NOT",
retVal.getTemplate().toString().trim());
}
@Test /* Test get_var 1 */
public void test33() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"a=20\nb=20\nb==");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.test_1_return retVal = parser.test_1();
assertEquals("20 0 MSTORE 20 32 MSTORE 32 MLOAD",
retVal.getTemplate().toString().trim());
}
@Test /* Test get_var 2 */
public void test34() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"a1=20\nb4=20\n\na4=20\na4==");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.test_1_return retVal = parser.test_1();
assertEquals("20 0 MSTORE 20 32 MSTORE 20 64 MSTORE 64 MLOAD",
retVal.getTemplate().toString().trim());
}
@Test /* Test if_stmt with (!)cond */
public void test35() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
"if !contract.storage[msg.data[0]]:\n" +
" contract.storage[msg.data[0]] = msg.data[1]\n" +
" return(1)\n" +
"else:\n" +
" return(0)\n");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.if_else_stmt_return retVal = parser.if_else_stmt();
assertEquals("",
retVal.getTemplate().toString().trim());
}
@Test /* Test complex contract with if_else_stmt inside else body */
public void test36() throws FileNotFoundException, RecognitionException {
CharStream stream =
new ANTLRStringStream("" +
" contract.storage[0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826] = 1000000\n" +
" if msg.datasize == 1:\n" +
" addr = msg.data[0]\n" +
" return(contract.storage[addr])\n" +
" else:\n" +
" from = msg.sender\n" +
" fromvalue = contract.storage[from]\n" +
" to = msg.data[0]\n" +
" value = msg.data[1]\n" +
" if fromvalue >= value:\n" +
" contract.storage[from] = fromvalue - value\n" +
" contract.storage[to] = contract.storage[to] + value\n" +
" return(1)\n" +
" else:\n" +
" return(0)\n");
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.gen_body_return retVal = parser.gen_body();
assertEquals("",
retVal.getTemplate().toString().trim());
}
@Test /* Test for hex number */
public void test37() throws FileNotFoundException, RecognitionException {
String hexNum = "0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826";
CharStream stream =
new ANTLRStringStream(hexNum);
SerpentLexer lex = new SerpentLexer(stream);
CommonTokenStream tokens = new CommonTokenStream(lex);
SerpentParser parser = new SerpentParser(tokens);
String userDir = System.getProperty("user.dir");
String templateFileName = userDir + "\\src\\main\\java\\org\\ethereum\\serpent\\Serpent2Asm.stg";
StringTemplateGroup template = new StringTemplateGroup(new FileReader(templateFileName),
AngleBracketTemplateLexer.class);
parser.setTemplateLib(template);
SerpentParser.hex_num_return retVal = parser.hex_num();
assertEquals(Utils.hexStringToDecimalString(hexNum),
retVal.getTemplate().toString().trim());
}
}