// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract AssemblyScopes {
// Demonstrates variable scopes within inline assembly
function getValues() public pure returns (uint256 retval) {
assembly {
// Declare a variable `outerValue` in the outermost scope
let outerValue := 10
{
// Declare a new scope
// Declare `innerValue` within this inner scope
let innerValue := 20
{
// Further inner scope
// Modify `innerValue` within this nested scope
innerValue := 30
}
// Exiting the scope, `innerValue` is still accessible here
// but will go out of scope after this block
}
{
// Attempting to declare `outerValue` here will cause a compilation error
// because `outerValue` is already defined in an outer scope
// Attempting to access `innerValue` here will fail because
// `innerValue` was declared in an inner scope and is not accessible here
}
// `innerValue` cannot be assigned to `retval` because it's out of scope here
// Assign `outerValue` to `retval` to return its value
retval := outerValue
}
}
}