From c3490bed36fdc793c740da430feac03d23aef777 Mon Sep 17 00:00:00 2001 From: Vitalik Buterin Date: Sun, 29 Jun 2014 15:36:35 -0400 Subject: [PATCH] Merging recent serpent commits --- libserpent/compiler.cpp | 14 +++++++++++--- libserpent/parser.cpp | 7 ++++--- libserpent/rewriter.cpp | 25 +++++++++++-------------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/libserpent/compiler.cpp b/libserpent/compiler.cpp index 731e018f0..f83fa5481 100644 --- a/libserpent/compiler.cpp +++ b/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") { diff --git a/libserpent/parser.cpp b/libserpent/parser.cpp index 2a3efcc55..f8189b4a2 100644 --- a/libserpent/parser.cpp +++ b/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 shuntingYard(std::vector 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? diff --git a/libserpent/rewriter.cpp b/libserpent/rewriter.cpp index eef9e9211..cf40c6604 100644 --- a/libserpent/rewriter.cpp +++ b/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 > 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;