From 9b86e3764cbaaec48ec4c3ae60fe821f0d171027 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 27 Feb 2014 11:29:39 +0000 Subject: [PATCH] Make good arithmetic operations. --- json_spirit/json_spirit_writer_template.h | 5 +++- libethereum/Instruction.cpp | 33 ++++++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/json_spirit/json_spirit_writer_template.h b/json_spirit/json_spirit_writer_template.h index c99375655..dbd0f45da 100644 --- a/json_spirit/json_spirit_writer_template.h +++ b/json_spirit/json_spirit_writer_template.h @@ -25,7 +25,9 @@ namespace json_spirit return 'A' - 10 + ch; } - template< class String_type > +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-local-typedefs") + template< class String_type > String_type non_printable_to_string( unsigned int c ) { typedef typename String_type::value_type Char_type; @@ -41,6 +43,7 @@ namespace json_spirit return result; } +#pragma GCC diagnostic pop template< typename Char_type, class String_type > bool add_esc_char( Char_type c, String_type& s ) diff --git a/libethereum/Instruction.cpp b/libethereum/Instruction.cpp index 8b7eae18d..41cbf7fe2 100644 --- a/libethereum/Instruction.cpp +++ b/libethereum/Instruction.cpp @@ -138,12 +138,15 @@ static void appendCode(u256s& o_code, vector& o_locs, u256s _code, vec static bool compileLispFragment(char const*& d, char const* e, bool _quiet, u256s& o_code, vector& o_locs) { + std::map const c_arith = { { "+", Instruction::ADD }, { "-", Instruction::SUB }, { "*", Instruction::MUL }, { "/", Instruction::DIV }, { "%", Instruction::MOD } }; + std::set const c_allowed = { '+', '-', '*', '/', '%' }; + bool exec = false; while (d != e) { // skip to next token - for (; d != e && !isalnum(*d) && *d != '(' && *d != ')' && *d != '_' && *d != '"'; ++d) {} + for (; d != e && !isalnum(*d) && *d != '(' && *d != ')' && *d != '_' && *d != '"' && !c_allowed.count(*d); ++d) {} if (d == e) break; @@ -186,7 +189,7 @@ static bool compileLispFragment(char const*& d, char const* e, bool _quiet, u256 else { char const* s = d; - for (; d != e && (isalnum(*d) || *d == '_'); ++d) {} + for (; d != e && (isalnum(*d) || *d == '_' || c_allowed.count(*d)); ++d) {} t = string(s, d - s); if (isdigit(t[0])) { @@ -360,8 +363,30 @@ static bool compileLispFragment(char const*& d, char const* e, bool _quiet, u256 o_code.push_back(it->second); } } - else if (!_quiet) - cwarn << "Unknown assembler token" << t; + else + { + auto it = c_arith.find(t); + if (it != c_arith.end()) + { + int i = 0; + while (d != e) + { + u256s codes; + vector locs; + if (compileLispFragment(d, e, _quiet, codes, locs)) + { + appendCode(o_code, o_locs, codes, locs); + if (i) + o_code.push_back((u256)it->second); + ++i; + } + else + break; + } + } + else if (!_quiet) + cwarn << "Unknown assembler token" << t; + } } }