Implement for loops.

This commit is contained in:
chriseth 2019-04-25 00:01:13 +02:00
parent c1ca16cd33
commit 597d37b731
2 changed files with 40 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#include <libsolidity/codegen/ir/IRGenerationContext.h>
#include <libsolidity/codegen/YulUtilFunctions.h>
#include <libsolidity/ast/TypeProvider.h>
#include <libyul/AsmPrinter.h>
#include <libyul/AsmData.h>
@ -126,6 +127,42 @@ bool IRGeneratorForStatements::visit(Assignment const& _assignment)
return false;
}
bool IRGeneratorForStatements::visit(ForStatement const& _for)
{
m_code << "for {\n";
if (_for.initializationExpression())
_for.initializationExpression()->accept(*this);
m_code << "} return_flag {\n";
if (_for.loopExpression())
_for.loopExpression()->accept(*this);
m_code << "}\n";
if (_for.condition())
{
_for.condition()->accept(*this);
m_code <<
"if iszero(" <<
expressionAsType(*_for.condition(), *TypeProvider::boolean()) <<
") { break }\n";
}
_for.body().accept(*this);
m_code << "}\n";
// Bubble up the return condition.
m_code << "if iszero(return_flag) { break }\n";
return false;
}
bool IRGeneratorForStatements::visit(Continue const&)
{
m_code << "continue\n";
return false;
}
bool IRGeneratorForStatements::visit(Break const&)
{
m_code << "break\n";
return false;
}
bool IRGeneratorForStatements::visit(Return const& _return)
{
if (Expression const* value = _return.expression())

View File

@ -46,6 +46,9 @@ public:
bool visit(VariableDeclarationStatement const& _variableDeclaration) override;
bool visit(Assignment const& _assignment) override;
bool visit(ForStatement const& _forStatement) override;
bool visit(Continue const& _continueStatement) override;
bool visit(Break const& _breakStatement) override;
bool visit(Return const& _return) override;
void endVisit(BinaryOperation const& _binOp) override;
bool visit(FunctionCall const& _funCall) override;