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

35 lines
885 B
C++
Raw Permalink Normal View History

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 "FunctionExpression.h"
#include <fmt/format.h>
#include "bscript/compiler/ast/NodeVisitor.h"
#include "bscript/compiler/ast/Function.h"
#include "bscript/compiler/model/FunctionLink.h"
namespace Pol::Bscript::Compiler
{
FunctionExpression::FunctionExpression( const SourceLocation& source_location,
std::shared_ptr<FunctionLink> function_link )
: Value( source_location ), function_link( std::move( function_link ) )
{
}
void FunctionExpression::accept( NodeVisitor& visitor )
{
visitor.visit_function_expression( *this );
}
void FunctionExpression::describe_to( std::string& w ) const
{
std::string name;
if ( auto fn = function_link->function() )
name = fn->name;
else
name = "unknown";
fmt::format_to( std::back_inserter( w ), "function-expression({})", name );
}
} // namespace Pol::Bscript::Compiler