polserver/pol-core/bscript/compiler/astbuilder/UserFunctionVisitor.cpp

65 lines
2.2 KiB
C++
Raw Permalink Normal View History

#include "bscript/compiler/astbuilder/UserFunctionVisitor.h"
#include "bscript/compiler/ast/ClassDeclaration.h"
#include "bscript/compiler/ast/UserFunction.h"
#include "bscript/compiler/astbuilder/BuilderWorkspace.h"
#include "bscript/compiler/model/CompilerWorkspace.h"
namespace Pol::Bscript::Compiler
{
UserFunctionVisitor::UserFunctionVisitor( const SourceFileIdentifier& sfi, BuilderWorkspace& ws,
const std::string& scope,
Node* top_level_statements_child_node )
: workspace( ws ),
tree_builder( sfi, ws ),
scope( scope ),
top_level_statements_child_node( top_level_statements_child_node )
{
}
antlrcpp::Any UserFunctionVisitor::visitFunctionDeclaration(
EscriptGrammar::EscriptParser::FunctionDeclarationContext* ctx )
{
if ( !scope.empty() )
{
tree_builder.current_scope_name = ScopeName( scope );
}
auto uf = tree_builder.function_declaration( ctx, scope );
workspace.function_resolver.register_user_function( scope, uf.get() );
workspace.compiler_workspace.user_functions.push_back( std::move( uf ) );
if ( !scope.empty() )
{
tree_builder.current_scope_name = ScopeName::Global;
}
return antlrcpp::Any();
}
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
antlrcpp::Any UserFunctionVisitor::visitFunctionExpression(
EscriptGrammar::EscriptParser::FunctionExpressionContext* ctx )
{
auto uf = tree_builder.function_expression( ctx );
workspace.function_resolver.register_user_function( "", uf.get() );
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
workspace.compiler_workspace.user_functions.push_back( std::move( uf ) );
return antlrcpp::Any();
}
antlrcpp::Any UserFunctionVisitor::visitClassDeclaration(
EscriptGrammar::EscriptParser::ClassDeclarationContext* ctx )
{
auto cd = tree_builder.class_declaration( ctx, top_level_statements_child_node );
if ( cd )
{
workspace.function_resolver.register_class_declaration( cd.get() );
workspace.compiler_workspace.class_declaration_indexes[cd->name] =
workspace.compiler_workspace.class_declarations.size();
workspace.compiler_workspace.class_declarations.push_back( std::move( cd ) );
}
return antlrcpp::Any();
}
} // namespace Pol::Bscript::Compiler