From 9bb6645cac297d8478aabbf56c1d9eb766583366 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 27 Feb 2014 13:12:21 +0000 Subject: [PATCH] Binary operators. --- libethereum/Instruction.cpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/libethereum/Instruction.cpp b/libethereum/Instruction.cpp index bf8d02f4e..9d2b4170c 100644 --- a/libethereum/Instruction.cpp +++ b/libethereum/Instruction.cpp @@ -139,7 +139,8 @@ 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 = { '+', '-', '*', '/', '%' }; + std::map const c_binary = { { "<", Instruction::LT }, { "<=", Instruction::LE }, { ">", Instruction::GT }, { ">=", Instruction::GE }, { "=", Instruction::EQ }, { "!=", Instruction::NOT } }; + std::set const c_allowed = { '+', '-', '*', '/', '%', '<', '>', '=', '!' }; bool exec = false; @@ -488,8 +489,28 @@ static bool compileLispFragment(char const*& d, char const* e, bool _quiet, u256 break; } } - else if (!_quiet) - cwarn << "Unknown assembler token" << t; + else + { + auto it = c_binary.find(t); + if (it != c_binary.end()) + { + vector>> codes(1); + while (d != e && compileLispFragment(d, e, _quiet, codes.back().first, codes.back().second)) + codes.push_back(pair>()); + codes.pop_back(); + int i = codes.size(); + if (i > 2) + cwarn << "Greater than two arguments given to binary operator" << t << "; using first two"; + for (auto it = codes.rbegin(); it != codes.rend(); ++it) + if (--i < 2) + appendCode(o_code, o_locs, it->first, it->second); + if (it->second == Instruction::NOT) + o_code.push_back(Instruction::EQ); + o_code.push_back((u256)it->second); + } + else if (!_quiet) + cwarn << "Unknown assembler token" << t; + } } } }