polserver/pol-core/bscript/compiler/ast/UserFunction.cpp

50 lines
1.5 KiB
C++
Raw Permalink Normal View History

#include "UserFunction.h"
#include <utility>
#include "bscript/compiler/ast/FunctionBody.h"
#include "bscript/compiler/ast/FunctionParameterList.h"
#include "bscript/compiler/ast/NodeVisitor.h"
#include "bscript/compiler/model/ClassLink.h"
namespace Pol::Bscript::Compiler
{
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
UserFunction::UserFunction( const SourceLocation& source_location, bool exported, bool expression,
UserFunctionType type, std::string scope, std::string name,
std::unique_ptr<FunctionParameterList> parameter_list,
std::unique_ptr<FunctionBody> body,
const SourceLocation& endfunction_location,
std::shared_ptr<ClassLink> class_link )
: Function( source_location, std::move( scope ), std::move( name ), std::move( parameter_list ),
std::move( body ) ),
exported( exported ),
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
expression( expression ),
type( type ),
endfunction_location( endfunction_location ),
class_link( std::move( class_link ) )
{
}
void UserFunction::accept( NodeVisitor& visitor )
{
visitor.visit_user_function( *this );
}
void UserFunction::describe_to( std::string& w ) const
{
fmt::format_to( std::back_inserter( w ), "user-function({})", name );
}
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
unsigned UserFunction::capture_count() const
{
return static_cast<unsigned>( capture_variable_scope_info.variables.size() );
}
FunctionBody& UserFunction::body()
{
return child<FunctionBody>( 1 );
}
} // namespace Pol::Bscript::Compiler