Convenience fixups.

This commit is contained in:
Gav Wood 2014-05-26 20:09:15 +02:00
parent 51e6c25164
commit 8a0dcc26c4
4 changed files with 13 additions and 5 deletions

View File

@ -142,6 +142,10 @@ AssemblyItem const& Assembly::append(AssemblyItem const& _i)
return back();
}
void Assembly::optimise()
{
}
bytes Assembly::assemble() const
{
bytes ret;

View File

@ -92,6 +92,7 @@ public:
std::string out() const { std::stringstream ret; streamOut(ret); return ret.str(); }
int deposit() const { return m_deposit; }
bytes assemble() const;
void optimise();
std::ostream& streamOut(std::ostream& _out) const;
private:

View File

@ -102,7 +102,7 @@ CodeFragment::CodeFragment(sp::utree const& _t, CompilerState& _s, bool _allowAS
void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
{
if (_t.empty())
if (_t.tag() == 0 && _t.empty())
error<EmptyList>();
else if (_t.tag() == 0 && _t.front().which() != sp::utree_type::symbol_type)
error<DataNotExecutable>();
@ -361,14 +361,17 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
{
requireSize(3);
requireDeposit(0, 1);
int minDep = min(code[1].m_asm.deposit(), code[2].m_asm.deposit());
m_asm.append(code[0].m_asm);
auto pos = m_asm.appendJumpI();
m_asm.onePath();
m_asm << code[2].m_asm;
m_asm.append(code[2].m_asm, minDep);
auto end = m_asm.appendJump();
m_asm.otherPath();
m_asm << pos.tag() << code[1].m_asm << end.tag();
m_asm << pos.tag();
m_asm.append(code[1].m_asm, minDep);
m_asm << end.tag();
m_asm.donePaths();
}
else if (us == "WHEN" || us == "UNLESS")

View File

@ -43,10 +43,10 @@ public:
static CodeFragment compile(std::string const& _src, CompilerState& _s);
/// Consolidates data and compiles code.
bytes code() const { return m_asm.assemble(); }
bytes code() { m_asm.optimise(); return m_asm.assemble(); }
/// Consolidates data and compiles code.
std::string assembly() const { return m_asm.out(); }
std::string assembly() { m_asm.optimise(); return m_asm.out(); }
private:
template <class T> void error() const { throw T(); }