Variadic macros work.

This commit is contained in:
Gav Wood 2014-05-30 14:55:07 +02:00
parent 78c0baa026
commit b379ce9065
3 changed files with 12 additions and 9 deletions

View File

@ -172,6 +172,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
unsigned ii = 0;
if (_t.size() != 3 && _t.size() != 4)
error<IncorrectParameterCount>();
vector<string> args;
for (auto const& i: _t)
{
if (ii == 1)
@ -198,16 +199,18 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
if (j.tag() || j.which() != sp::utree_type::symbol_type)
error<InvalidMacroArgs>();
auto sr = j.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
_s.macros[n].args.push_back(string(sr.begin(), sr.end()));
args.push_back(string(sr.begin(), sr.end()));
}
else if (ii == 3)
{
_s.macros[n].code = i;
_s.macros[n].env = _s.outers;
auto k = make_pair(n, args.size());
_s.macros[k].code = i;
_s.macros[k].env = _s.outers;
_s.macros[k].args = args;
for (auto const& i: _s.args)
_s.macros[n].env[i.first] = i.second;
_s.macros[k].env[i.first] = i.second;
for (auto const& i: _s.defs)
_s.macros[n].env[i.first] = i.second;
_s.macros[k].env[i.first] = i.second;
}
++ii;
}
@ -292,9 +295,9 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
auto requireMaxSize = [&](unsigned s) { if (code.size() > s) error<IncorrectParameterCount>(); };
auto requireDeposit = [&](unsigned i, int s) { if (code[i].m_asm.deposit() != s) error<InvalidDeposit>(); };
if (_s.macros.count(s) && _s.macros.at(s).args.size() == code.size())
if (_s.macros.count(make_pair(s, code.size())))
{
Macro const& m = _s.macros.at(s);
Macro const& m = _s.macros.at(make_pair(s, code.size()));
CompilerState cs = _s;
for (auto const& i: m.env)
cs.outers[i.first] = i.second;

View File

@ -42,8 +42,8 @@ void CompilerState::populateStandard()
static const string s = "{"
"(def 'gav 0x8a40bfaa73256b60764c1bf40675a99083efb075)"
"(def 'send (to value) (call (- (gas) 21) to value 0 0 0 0))"
#if 0
"(def 'send (gaslimit to value) (call gaslimit to value 0 0 0 0))"
#if 1
"(def 'alloc (len) (asm msize 0 1 len msize add sub mstore8))"
"(def 'msg (gaslimit to value data datasize outsize) { [32]:outsize [0]:(alloc @32) (call gaslimit to value data datasize @0 @32) @0 })"
"(def 'msg (gaslimit to value data datasize) { (call gaslimit to value data datasize 0 32) @0 })"

View File

@ -43,7 +43,7 @@ struct CompilerState
std::map<std::string, CodeFragment> defs;
std::map<std::string, CodeFragment> args;
std::map<std::string, CodeFragment> outers;
std::map<std::string, Macro> macros;
std::map<std::pair<std::string, unsigned>, Macro> macros;
std::vector<boost::spirit::utree> treesToKill;
};