polserver/pol-core/bscript/compiler/optimizer/ReferencedFunctionGatherer.cpp

91 lines
2.6 KiB
C++
Raw Permalink Normal View History

#include "bscript/compiler/optimizer/ReferencedFunctionGatherer.h"
#include "bscript/compiler/ast/ClassParameterDeclaration.h"
#include "bscript/compiler/ast/FunctionCall.h"
Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
#include "bscript/compiler/ast/FunctionExpression.h"
#include "bscript/compiler/ast/FunctionReference.h"
#include "bscript/compiler/ast/UserFunction.h"
#include "bscript/compiler/model/FunctionLink.h"
namespace Pol::Bscript::Compiler
{
ReferencedFunctionGatherer::ReferencedFunctionGatherer(
std::vector<std::unique_ptr<ModuleFunctionDeclaration>>& all_module_function_declarations,
std::vector<std::unique_ptr<UserFunction>> all_user_functions )
{
for ( auto& mfd : all_module_function_declarations )
{
unreferenced_module_function_declarations.insert( mfd.get() );
}
for ( auto& uf : all_user_functions )
{
UserFunction* p = uf.get();
unreferenced_user_functions[p] = std::move( uf );
}
}
void ReferencedFunctionGatherer::visit_function_call( FunctionCall& fc )
{
visit_children( fc );
reference( *fc.function_link );
}
void ReferencedFunctionGatherer::visit_function_reference( FunctionReference& fr )
{
reference( *fr.function_link );
}
Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
void ReferencedFunctionGatherer::visit_function_expression( FunctionExpression& fr )
{
reference( *fr.function_link );
}
void ReferencedFunctionGatherer::visit_class_parameter_declaration( ClassParameterDeclaration& cpd )
{
reference( *cpd.constructor_link );
}
void ReferencedFunctionGatherer::reference( FunctionLink& link )
{
if ( auto mfd = link.module_function_declaration() )
reference( mfd );
else if ( auto uf = link.user_function() )
reference( uf );
}
void ReferencedFunctionGatherer::reference( ModuleFunctionDeclaration* mfd )
{
auto itr = unreferenced_module_function_declarations.find( mfd );
if ( itr != unreferenced_module_function_declarations.end() )
{
referenced_module_function_declarations.push_back( mfd );
unreferenced_module_function_declarations.erase( itr );
}
}
void ReferencedFunctionGatherer::reference( UserFunction* uf )
{
auto itr = unreferenced_user_functions.find( uf );
if ( itr != unreferenced_user_functions.end() )
{
referenced_user_functions.push_back( std::move( ( *itr ).second ) );
unreferenced_user_functions.erase( itr );
uf->accept( *this );
}
}
std::vector<std::unique_ptr<UserFunction>>
ReferencedFunctionGatherer::take_referenced_user_functions()
{
return std::move( referenced_user_functions );
}
std::vector<ModuleFunctionDeclaration*>
ReferencedFunctionGatherer::take_referenced_module_function_declarations()
{
return std::move( referenced_module_function_declarations );
}
} // namespace Pol::Bscript::Compiler