mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
LLL: fix the set keyword (create symbol if not present)
This commit is contained in:
parent
1ffe286a81
commit
af57c083f2
@ -171,13 +171,23 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
|
|||||||
return string();
|
return string();
|
||||||
};
|
};
|
||||||
|
|
||||||
auto varAddress = [&](string const& n)
|
auto varAddress = [&](string const& n, bool createMissing = false)
|
||||||
{
|
{
|
||||||
if (n.empty())
|
if (n.empty())
|
||||||
error<InvalidName>("Empty variable name not allowed");
|
error<InvalidName>("Empty variable name not allowed");
|
||||||
auto it = _s.vars.find(n);
|
auto it = _s.vars.find(n);
|
||||||
if (it == _s.vars.end())
|
if (it == _s.vars.end())
|
||||||
|
{
|
||||||
|
if (createMissing)
|
||||||
|
{
|
||||||
|
// Create new variable
|
||||||
|
bool ok;
|
||||||
|
tie(it, ok) = _s.vars.insert(make_pair(n, make_pair(_s.stackSize, 32)));
|
||||||
|
_s.stackSize += 32;
|
||||||
|
}
|
||||||
|
else
|
||||||
error<InvalidName>(std::string("Symbol not found: ") + n);
|
error<InvalidName>(std::string("Symbol not found: ") + n);
|
||||||
|
}
|
||||||
return it->second.first;
|
return it->second.first;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -210,7 +220,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
|
|||||||
for (auto const& i: _t)
|
for (auto const& i: _t)
|
||||||
if (c++ == 2)
|
if (c++ == 2)
|
||||||
m_asm.append(CodeFragment(i, _s, false).m_asm);
|
m_asm.append(CodeFragment(i, _s, false).m_asm);
|
||||||
m_asm.append((u256)varAddress(firstAsString()));
|
m_asm.append((u256)varAddress(firstAsString(), true));
|
||||||
m_asm.append(Instruction::MSTORE);
|
m_asm.append(Instruction::MSTORE);
|
||||||
}
|
}
|
||||||
else if (us == "GET")
|
else if (us == "GET")
|
||||||
|
@ -57,6 +57,26 @@ BOOST_AUTO_TEST_CASE(panic)
|
|||||||
BOOST_REQUIRE(m_output.empty());
|
BOOST_REQUIRE(m_output.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(variables)
|
||||||
|
{
|
||||||
|
char const* sourceCode = R"(
|
||||||
|
(returnlll
|
||||||
|
(seq
|
||||||
|
(set 'x 1)
|
||||||
|
(set 'y 2)
|
||||||
|
;; this should equal to 3
|
||||||
|
(set 'z (add (get 'x) (get 'y)))
|
||||||
|
;; overwriting it here
|
||||||
|
(set 'y 4)
|
||||||
|
;; each variable has a 32 byte slot, starting from memory location 0x80
|
||||||
|
;; variable addresses can also be retrieved by x or (ref 'x)
|
||||||
|
(set 'k (add (add (ref 'x) (ref 'y)) z))
|
||||||
|
(return (add (add (get 'x) (add (get 'y) (get 'z))) (get 'k)))))
|
||||||
|
)";
|
||||||
|
compileAndRun(sourceCode);
|
||||||
|
BOOST_CHECK(callFallback() == encodeArgs(u256(488)));
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(when)
|
BOOST_AUTO_TEST_CASE(when)
|
||||||
{
|
{
|
||||||
char const* sourceCode = R"(
|
char const* sourceCode = R"(
|
||||||
|
Loading…
Reference in New Issue
Block a user