mirror of
https://github.com/polserver/polserver
synced 2026-08-13 08:23:08 -04:00
* 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
150 lines
5.3 KiB
C++
150 lines
5.3 KiB
C++
#include "CodeGenerator.h"
|
|
|
|
#include <memory>
|
|
|
|
#include "StoredToken.h"
|
|
#include "bscript/compiler/ast/ModuleFunctionDeclaration.h"
|
|
#include "bscript/compiler/ast/Program.h"
|
|
#include "bscript/compiler/ast/ProgramParameterList.h"
|
|
#include "bscript/compiler/ast/TopLevelStatements.h"
|
|
#include "bscript/compiler/ast/UserFunction.h"
|
|
#include "bscript/compiler/codegen/InstructionEmitter.h"
|
|
#include "bscript/compiler/codegen/InstructionGenerator.h"
|
|
#include "bscript/compiler/codegen/ModuleDeclarationRegistrar.h"
|
|
#include "bscript/compiler/file/SourceFileIdentifier.h"
|
|
#include "bscript/compiler/model/CompilerWorkspace.h"
|
|
#include "bscript/compiler/model/FlowControlLabel.h"
|
|
#include "bscript/compiler/representation/CompiledScript.h"
|
|
#include "bscript/compiler/representation/ExportedFunction.h"
|
|
#include "bscript/compiler/representation/ModuleDescriptor.h"
|
|
#include "bscript/compiler/representation/ModuleFunctionDescriptor.h"
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
{
|
|
std::unique_ptr<CompiledScript> CodeGenerator::generate(
|
|
std::unique_ptr<CompilerWorkspace> workspace )
|
|
{
|
|
auto program_info =
|
|
workspace->program
|
|
? std::unique_ptr<CompiledScript::ProgramInfo>( new CompiledScript::ProgramInfo{
|
|
static_cast<unsigned>( workspace->program->parameter_list().children.size() ) } )
|
|
: std::unique_ptr<CompiledScript::ProgramInfo>();
|
|
|
|
CodeSection code;
|
|
DataSection data;
|
|
|
|
std::vector<std::string> debug_filenames;
|
|
// For parity with OG compiler, debug file #0 is always empty.
|
|
debug_filenames.emplace_back( "" );
|
|
for ( auto& ident : workspace->referenced_source_file_identifiers )
|
|
{
|
|
debug_filenames.push_back( ident->pathname );
|
|
}
|
|
DebugStore debug( std::move( debug_filenames ) );
|
|
|
|
ExportedFunctions exported_functions;
|
|
|
|
ModuleDeclarationRegistrar module_declaration_registrar;
|
|
|
|
InstructionEmitter instruction_emitter( code, data, debug, exported_functions,
|
|
module_declaration_registrar );
|
|
CodeGenerator generator( instruction_emitter, module_declaration_registrar );
|
|
|
|
generator.register_module_functions_alphabetically( *workspace );
|
|
|
|
sort_user_functions_alphabetically( *workspace );
|
|
|
|
generator.generate_instructions( *workspace );
|
|
|
|
std::vector<ModuleDescriptor> module_descriptors =
|
|
module_declaration_registrar.take_module_descriptors();
|
|
|
|
return std::make_unique<CompiledScript>(
|
|
std::move( code ), std::move( data ), std::move( debug ), std::move( exported_functions ),
|
|
std::move( workspace->global_variable_names ), std::move( module_descriptors ),
|
|
std::move( program_info ), std::move( workspace->referenced_source_file_identifiers ) );
|
|
}
|
|
|
|
CodeGenerator::CodeGenerator( InstructionEmitter& emitter,
|
|
ModuleDeclarationRegistrar& module_declaration_registrar )
|
|
: module_declaration_registrar( module_declaration_registrar ),
|
|
emitter( emitter ),
|
|
emit( emitter )
|
|
{
|
|
}
|
|
|
|
void CodeGenerator::generate_instructions( CompilerWorkspace& workspace )
|
|
{
|
|
emitter.debug_statementbegin();
|
|
emitter.debug_file_line( 0, 1 );
|
|
|
|
std::map<std::string, FlowControlLabel> user_function_labels;
|
|
InstructionGenerator generator( emitter, user_function_labels );
|
|
|
|
workspace.top_level_statements->accept( generator );
|
|
|
|
if ( auto& program = workspace.program )
|
|
{
|
|
program->accept( generator );
|
|
}
|
|
|
|
emit.progend();
|
|
|
|
for ( auto& user_function : workspace.user_functions )
|
|
{
|
|
// Function expressions are handled inside InstructionGenerator visit_function_expression
|
|
if ( !user_function->expression )
|
|
{
|
|
user_function->accept( generator );
|
|
}
|
|
}
|
|
}
|
|
|
|
void CodeGenerator::register_module_functions_alphabetically( CompilerWorkspace& workspace )
|
|
{
|
|
sort_module_functions_by_module_name( workspace );
|
|
for ( auto& module_function : workspace.referenced_module_function_declarations )
|
|
{
|
|
module_declaration_registrar.register_module( *module_function );
|
|
}
|
|
|
|
sort_module_functions_alphabetically( workspace );
|
|
for ( const auto& decl : workspace.referenced_module_function_declarations )
|
|
{
|
|
module_declaration_registrar.register_modulefunc( *decl );
|
|
}
|
|
}
|
|
|
|
void CodeGenerator::sort_module_functions_by_module_name( CompilerWorkspace& workspace )
|
|
{
|
|
auto sortByModuleName = []( ModuleFunctionDeclaration* d1,
|
|
ModuleFunctionDeclaration* d2 ) -> bool {
|
|
return d1->module_name < d2->module_name;
|
|
};
|
|
|
|
std::sort( workspace.referenced_module_function_declarations.begin(),
|
|
workspace.referenced_module_function_declarations.end(), sortByModuleName );
|
|
}
|
|
|
|
void CodeGenerator::sort_module_functions_alphabetically( CompilerWorkspace& workspace )
|
|
{
|
|
auto sortByModuleFunctionName = []( ModuleFunctionDeclaration* d1,
|
|
ModuleFunctionDeclaration* d2 ) -> bool {
|
|
return d1->name < d2->name;
|
|
};
|
|
|
|
std::sort( workspace.referenced_module_function_declarations.begin(),
|
|
workspace.referenced_module_function_declarations.end(), sortByModuleFunctionName );
|
|
}
|
|
|
|
void CodeGenerator::sort_user_functions_alphabetically( CompilerWorkspace& workspace )
|
|
{
|
|
auto sortByName = []( const std::unique_ptr<UserFunction>& s1,
|
|
const std::unique_ptr<UserFunction>& s2 ) -> bool {
|
|
return s1->name < s2->name;
|
|
};
|
|
|
|
std::sort( workspace.user_functions.begin(), workspace.user_functions.end(), sortByName );
|
|
}
|
|
|
|
} // namespace Pol::Bscript::Compiler
|