Add block statement

This commit is contained in:
Bhargava Shastry 2021-04-30 11:53:32 +02:00
parent 46bac2377f
commit a115e61be6
3 changed files with 41 additions and 6 deletions

View File

@ -41,9 +41,11 @@
* *
*/ */
#define GENERATORLIST(MACRO, SEP, ENDSEP) \ #define GENERATORLIST(MACRO, SEP, ENDSEP) \
MACRO(BlockStmtGenerator) SEP \
MACRO(ContractGenerator) SEP \ MACRO(ContractGenerator) SEP \
MACRO(FunctionGenerator) SEP \ MACRO(FunctionGenerator) SEP \
MACRO(ImportGenerator) SEP \ MACRO(ImportGenerator) SEP \
MACRO(PragmaGenerator) SEP \ MACRO(PragmaGenerator) SEP \
MACRO(SourceUnitGenerator) SEP \ MACRO(SourceUnitGenerator) SEP \
MACRO(StatementGenerator) SEP \
MACRO(TestCaseGenerator) ENDSEP MACRO(TestCaseGenerator) ENDSEP

View File

@ -263,6 +263,16 @@ string FunctionState::params(Params _p)
return "(" + boost::algorithm::join(params, ",") + ")"; return "(" + boost::algorithm::join(params, ",") + ")";
} }
string BlockStmtGenerator::visit()
{
return "\n" + indentation() + "{}\n";
}
void FunctionGenerator::setup()
{
addGenerators({{mutator->generator<BlockStmtGenerator>(), 1}});
}
string FunctionGenerator::visit() string FunctionGenerator::visit()
{ {
string visibility; string visibility;
@ -281,7 +291,7 @@ string FunctionGenerator::visit()
state->currentFunctionState()->addOutput(TypeProvider{state}.type()); state->currentFunctionState()->addOutput(TypeProvider{state}.type());
ostringstream function; ostringstream function;
function << indentation(state->indentationLevel) function << indentation()
<< "function " << "function "
<< name << name
<< state->currentFunctionState()->params(FunctionState::Params::INPUT) << state->currentFunctionState()->params(FunctionState::Params::INPUT)
@ -291,7 +301,7 @@ string FunctionGenerator::visit()
if (!state->currentFunctionState()->outputs.empty()) if (!state->currentFunctionState()->outputs.empty())
function << " returns" function << " returns"
<< state->currentFunctionState()->params(FunctionState::Params::OUTPUT); << state->currentFunctionState()->params(FunctionState::Params::OUTPUT);
function << " {}\n"; function << generator<BlockStmtGenerator>()->visit();
return function.str(); return function.str();
} }

View File

@ -648,11 +648,11 @@ struct GeneratorBase
endVisit(); endVisit();
return generatedCode; return generatedCode;
} }
/// @returns indentation as string. Each indentation level comprises two /// @returns current indentation as string. Each indentation level comprises
/// whitespace characters. /// two whitespace characters.
std::string indentation(unsigned _indentationLevel) std::string indentation()
{ {
return std::string(_indentationLevel * 2, ' '); return std::string(state->indentationLevel * 2, ' ');
} }
/// @returns a string representing the generation of /// @returns a string representing the generation of
/// the Solidity grammar element. /// the Solidity grammar element.
@ -791,6 +791,7 @@ public:
{} {}
std::string visit() override; std::string visit() override;
std::string name() override { return "Function generator"; } std::string name() override { return "Function generator"; }
void setup() override;
/// Sets @name m_freeFunction to @param _freeFunction. /// Sets @name m_freeFunction to @param _freeFunction.
void scope(bool _freeFunction) void scope(bool _freeFunction)
{ {
@ -800,6 +801,28 @@ private:
bool m_freeFunction; bool m_freeFunction;
static constexpr unsigned s_maxInputs = 4; static constexpr unsigned s_maxInputs = 4;
static constexpr unsigned s_maxOutputs = 4; static constexpr unsigned s_maxOutputs = 4;
static constexpr unsigned s_maxStatements = 5;
};
class StatementGenerator: public GeneratorBase
{
public:
explicit StatementGenerator(std::shared_ptr<SolidityGenerator> _mutator):
GeneratorBase(std::move(_mutator))
{}
void setup() override {}
std::string visit() override { return {}; }
std::string name() override { return "Statement generator"; }
};
class BlockStmtGenerator: public GeneratorBase
{
public:
explicit BlockStmtGenerator(std::shared_ptr<SolidityGenerator> _mutator):
GeneratorBase(std::move(_mutator))
{}
std::string visit() override;
std::string name() override { return "Block statement generator"; }
}; };
class SolidityGenerator: public std::enable_shared_from_this<SolidityGenerator> class SolidityGenerator: public std::enable_shared_from_this<SolidityGenerator>