mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[Sol -> Yul] Implement if statements
This commit is contained in:
parent
0852ccc318
commit
b83f6d8d46
@ -152,6 +152,25 @@ bool IRGeneratorForStatements::visit(TupleExpression const& _tuple)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IRGeneratorForStatements::visit(IfStatement const& _ifStatement)
|
||||||
|
{
|
||||||
|
_ifStatement.condition().accept(*this);
|
||||||
|
string condition = expressionAsType(_ifStatement.condition(), *TypeProvider::boolean());
|
||||||
|
|
||||||
|
if (_ifStatement.falseStatement())
|
||||||
|
{
|
||||||
|
m_code << "switch " << condition << "\n" "case 0 {\n";
|
||||||
|
_ifStatement.falseStatement()->accept(*this);
|
||||||
|
m_code << "}\n" "default {\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_code << "if " << condition << " {\n";
|
||||||
|
_ifStatement.trueStatement().accept(*this);
|
||||||
|
m_code << "}\n";
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool IRGeneratorForStatements::visit(ForStatement const& _forStatement)
|
bool IRGeneratorForStatements::visit(ForStatement const& _forStatement)
|
||||||
{
|
{
|
||||||
generateLoop(
|
generateLoop(
|
||||||
|
@ -48,6 +48,7 @@ public:
|
|||||||
void endVisit(VariableDeclarationStatement const& _variableDeclaration) override;
|
void endVisit(VariableDeclarationStatement const& _variableDeclaration) override;
|
||||||
bool visit(Assignment const& _assignment) override;
|
bool visit(Assignment const& _assignment) override;
|
||||||
bool visit(TupleExpression const& _tuple) override;
|
bool visit(TupleExpression const& _tuple) override;
|
||||||
|
bool visit(IfStatement const& _ifStatement) override;
|
||||||
bool visit(ForStatement const& _forStatement) override;
|
bool visit(ForStatement const& _forStatement) override;
|
||||||
bool visit(WhileStatement const& _whileStatement) override;
|
bool visit(WhileStatement const& _whileStatement) override;
|
||||||
bool visit(Continue const& _continueStatement) override;
|
bool visit(Continue const& _continueStatement) override;
|
||||||
|
82
test/libsolidity/semanticTests/viaYul/if.sol
Normal file
82
test/libsolidity/semanticTests/viaYul/if.sol
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
contract C {
|
||||||
|
function f(bool condition) public returns (uint x) {
|
||||||
|
x = 23;
|
||||||
|
if (condition)
|
||||||
|
x = 42;
|
||||||
|
}
|
||||||
|
function g(bool condition) public returns (uint x) {
|
||||||
|
x = 0;
|
||||||
|
if (condition)
|
||||||
|
x = 42;
|
||||||
|
else
|
||||||
|
x = 23;
|
||||||
|
}
|
||||||
|
function h(bool condition) public returns (uint x) {
|
||||||
|
if (condition)
|
||||||
|
return 42;
|
||||||
|
x = 23;
|
||||||
|
}
|
||||||
|
function i(bool condition) public returns (uint x) {
|
||||||
|
if (condition)
|
||||||
|
x = 10;
|
||||||
|
else
|
||||||
|
return 23;
|
||||||
|
x = 42;
|
||||||
|
}
|
||||||
|
function j(uint a, uint b) public returns (uint x, uint y) {
|
||||||
|
x = 42;
|
||||||
|
if (a + b < 10)
|
||||||
|
x = a;
|
||||||
|
else
|
||||||
|
x = b;
|
||||||
|
y = 100;
|
||||||
|
}
|
||||||
|
function k(uint a, uint b) public returns (uint x, uint y) {
|
||||||
|
x = 42;
|
||||||
|
do {
|
||||||
|
if (a + b < 10)
|
||||||
|
{
|
||||||
|
if (a == b)
|
||||||
|
{
|
||||||
|
x = 99; y = 99;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
x = a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
x = b;
|
||||||
|
if (a != b)
|
||||||
|
y = 17;
|
||||||
|
else
|
||||||
|
y = 13;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
y = 100;
|
||||||
|
} while(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: true
|
||||||
|
// ----
|
||||||
|
// f(bool): 0 -> 23
|
||||||
|
// f(bool): 1 -> 42
|
||||||
|
// g(bool): 0 -> 23
|
||||||
|
// g(bool): 1 -> 42
|
||||||
|
// h(bool): 0 -> 23
|
||||||
|
// h(bool): 1 -> 42
|
||||||
|
// i(bool): 0 -> 23
|
||||||
|
// i(bool): 1 -> 42
|
||||||
|
// j(uint256,uint256): 1, 3 -> 1, 100
|
||||||
|
// j(uint256,uint256): 3, 1 -> 3, 100
|
||||||
|
// j(uint256,uint256): 10, 23 -> 23, 100
|
||||||
|
// j(uint256,uint256): 23, 10 -> 10, 100
|
||||||
|
// k(uint256,uint256): 1, 3 -> 1, 100
|
||||||
|
// k(uint256,uint256): 3, 1 -> 3, 100
|
||||||
|
// k(uint256,uint256): 3, 3 -> 99, 99
|
||||||
|
// k(uint256,uint256): 10, 23 -> 23, 17
|
||||||
|
// k(uint256,uint256): 23, 10 -> 10, 17
|
||||||
|
// k(uint256,uint256): 23, 23 -> 23, 13
|
Loading…
Reference in New Issue
Block a user