From 597d37b7315f5ffcb7d99b90710b0e583b3ebf34 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 25 Apr 2019 00:01:13 +0200 Subject: [PATCH] Implement for loops. --- .../codegen/ir/IRGeneratorForStatements.cpp | 37 +++++++++++++++++++ .../codegen/ir/IRGeneratorForStatements.h | 3 ++ 2 files changed, 40 insertions(+) diff --git a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp index 39fd826ab..1fad14494 100644 --- a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp +++ b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -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()) diff --git a/libsolidity/codegen/ir/IRGeneratorForStatements.h b/libsolidity/codegen/ir/IRGeneratorForStatements.h index b40d50ff4..331962f84 100644 --- a/libsolidity/codegen/ir/IRGeneratorForStatements.h +++ b/libsolidity/codegen/ir/IRGeneratorForStatements.h @@ -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;