Browse Source

Merging recent serpent commits

cl-refactor
Vitalik Buterin 11 years ago
parent
commit
c3490bed36
  1. 14
      libserpent/compiler.cpp
  2. 7
      libserpent/parser.cpp
  3. 25
      libserpent/rewriter.cpp

14
libserpent/compiler.cpp

@ -99,6 +99,14 @@ programData opcodeify(Node node, programAux aux=Aux()) {
aux = sub.aux;
subs.push_back(sub.code);
}
// Debug
if (node.val == "debug") {
Node nodelist[] = {
subs[0],
token("DUP", m), token("POP", m), token("POP", m)
};
return pd(aux, multiToken(nodelist, 4, m));
}
// Seq of multiple statements
if (node.val == "seq") {
return pd(aux, astnode("_", subs, m));
@ -142,10 +150,10 @@ programData opcodeify(Node node, programAux aux=Aux()) {
Node nodelist[] = {
subs[0],
token("MSIZE", m), token("SWAP", m), token("MSIZE", m),
token("ADD", m), token("1", m), token("SWAP", m), token("SUB", m),
token("0", m), token("SWAP", m), token("MSTORE8", m)
token("ADD", m),
token("0", m), token("SWAP", m), token("MSTORE", m)
};
return pd(aux, multiToken(nodelist, 11, m));
return pd(aux, multiToken(nodelist, 8, m));
}
// Array literals
else if (node.val == "array_lit") {

7
libserpent/parser.cpp

@ -15,7 +15,7 @@ int precedence(Node tok) {
else if (v=="+" || v=="-") return 3;
else if (v=="<" || v==">" || v=="<=" || v==">=") return 4;
else if (v=="@<" || v=="@>" || v=="@<=" || v=="@>=") return 4;
else if (v=="&" || v=="|" || v=="xor" || v=="==") return 5;
else if (v=="&" || v=="|" || v=="xor" || v=="==" || v == "!=") return 5;
else if (v=="&&" || v=="and") return 6;
else if (v=="||" || v=="or") return 7;
else if (v=="=") return 10;
@ -93,7 +93,8 @@ std::vector<Node> shuntingYard(std::vector<Node> tokens) {
}
int prec = precedence(tok);
while (stack.size()
&& toktype(stack.back()) == BINARY_OP
&& (toktype(stack.back()) == BINARY_OP
|| toktype(stack.back()) == UNARY_OP)
&& precedence(stack.back()) <= prec) {
oq.push_back(stack.back());
stack.pop_back();
@ -242,7 +243,7 @@ bool bodied(std::string tok) {
bool childBlocked(std::string tok) {
return tok == "if" || tok == "elif" || tok == "else"
|| tok == "code" || tok == "shared" || tok == "init"
|| tok == "while";
|| tok == "while" || tok == "repeat" || tok == "for";
}
// Are the two commands meant to continue each other?

25
libserpent/rewriter.cpp

@ -24,7 +24,6 @@ std::string valid[][3] = {
{ "sha3", "1", "2" },
{ "return", "1", "2" },
{ "inset", "1", "1" },
{ "import", "1", "1" },
{ "array_lit", "0", tt256 },
{ "seq", "0", tt256 },
{ "---END---", "", "" } //Keep this line at the end of the list
@ -63,6 +62,10 @@ std::string macros[][2] = {
"(@%= $a $b)",
"(set $a (@% $a $b))"
},
{
"(!= $a $b)",
"(not (eq $a $b))"
},
{
"(if $cond $do (else $else))",
"(if $cond $do $else)"
@ -131,6 +134,10 @@ std::string macros[][2] = {
"(sha3 $x)",
"(seq (set $1 $x) (sha3 (ref $1) 32))"
},
{
"(sha3 $mstart $msize)",
"(~sha3 $mstart (mul 32 $msize))"
},
{
"(id $0)",
"$0"
@ -185,7 +192,7 @@ std::string macros[][2] = {
},
{
"(call $f $inp $inpsz $outsz)",
"(seq (set $1 $outsz) (set $2 (alloc (mul 32 (get $1)))) (pop (call (sub (gas) (add 25 (get $1))) $f 0 $inp (mul 32 $inpsz) (ref $2) (mul 32 (get $1)))) (get $2))"
"(seq (set $1 $outsz) (set $2 (alloc (mul 32 (get $1)))) (pop (call (sub (gas) (add 25 (get $1))) $f 0 $inp (mul 32 $inpsz) (get $2) (mul 32 (get $1)))) (get $2))"
},
{
"(msg $gas $to $val $inp $inpsz)",
@ -197,7 +204,7 @@ std::string macros[][2] = {
},
{
"(msg $gas $to $val $inp $inpsz $outsz)",
"(seq (set $1 (mul 32 $outsz)) (set $2 (alloc (get $1))) (pop (call $gas $to $val $inp (mul 32 $inpsz) (ref $2) (get $1))) (get $2))"
"(seq (set $1 (mul 32 $outsz)) (set $2 (alloc (get $1))) (pop (call $gas $to $val $inp (mul 32 $inpsz) (get $2) (get $1))) (get $2))"
},
{
"(outer (init $init $code))",
@ -219,14 +226,6 @@ std::string macros[][2] = {
"(inset $x)",
"$x"
},
{
"(create $val (import $code))",
"(seq (set $1 (msize)) (create $val (get $1) (lll $code (get $1))))"
},
{
"(create (import $x))",
"(seq (set $1 (msize)) (create $val (get $1) (lll $code (get $1))))"
},
{
"(create $x)",
"(seq (set $1 (msize)) (create $val (get $1) (lll $code (get $1))))"
@ -252,9 +251,7 @@ std::string macros[][2] = {
std::vector<std::vector<Node> > nodeMacros;
std::string synonyms[][2] = {
{ "|", "or" },
{ "or", "||" },
{ "&", "and" },
{ "and", "&&" },
{ "elif", "if" },
{ "!", "not" },
@ -289,7 +286,7 @@ matchResult match(Node p, Node n) {
matchResult o;
o.success = false;
if (p.type == TOKEN) {
if (p.val == n.val) o.success = true;
if (p.val == n.val && n.type == TOKEN) o.success = true;
else if (p.val[0] == '$') {
o.success = true;
o.map[p.val.substr(1)] = n;

Loading…
Cancel
Save