mirror of
https://github.com/polserver/polserver
synced 2026-08-13 08:23:08 -04:00
Adds enough to build the AST nodes for the module function declarations in a .em file. Adds: - Function: base class for module function declarations and user functions - FunctionParameterDeclaration: declaration for a function parameter, as well as its default value if any - FunctionParameterList: just a holder for function parameter declarations - ModuleFunctionDeclaration: for function declarations in .em files - FunctionResolver: hooks up function calls to module functions or user functions - ModuleProcessor: a visitor for processing const declarations and module function declarations (const declarations are yet to come from the main branch)
56 lines
2 KiB
C++
56 lines
2 KiB
C++
#include "ModuleDeclarationBuilder.h"
|
|
|
|
#include "compiler/ast/Expression.h"
|
|
#include "compiler/ast/FunctionParameterDeclaration.h"
|
|
#include "compiler/ast/FunctionParameterList.h"
|
|
#include "compiler/ast/ModuleFunctionDeclaration.h"
|
|
|
|
using EscriptGrammar::EscriptParser;
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
{
|
|
ModuleDeclarationBuilder::ModuleDeclarationBuilder(
|
|
const SourceFileIdentifier& source_file_identifier, BuilderWorkspace& workspace )
|
|
: SimpleStatementBuilder( source_file_identifier, workspace )
|
|
{
|
|
}
|
|
|
|
std::unique_ptr<ModuleFunctionDeclaration> ModuleDeclarationBuilder::module_function_declaration(
|
|
EscriptParser::ModuleFunctionDeclarationContext* ctx, std::string module_name )
|
|
{
|
|
std::string name = text( ctx->IDENTIFIER() );
|
|
std::vector<std::unique_ptr<FunctionParameterDeclaration>> parameters;
|
|
|
|
if ( auto param_list = ctx->moduleFunctionParameterList() )
|
|
{
|
|
for ( auto param : param_list->moduleFunctionParameter() )
|
|
{
|
|
std::string parameter_name = text( param->IDENTIFIER() );
|
|
std::unique_ptr<FunctionParameterDeclaration> parameter_declaration;
|
|
bool byref = false;
|
|
bool unused = false;
|
|
|
|
if ( auto expr_ctx = param->expression() )
|
|
{
|
|
auto default_value = expression( expr_ctx );
|
|
parameter_declaration = std::make_unique<FunctionParameterDeclaration>(
|
|
location_for( *param ), std::move( parameter_name ), byref, unused,
|
|
std::move( default_value ) );
|
|
}
|
|
else
|
|
{
|
|
parameter_declaration = std::make_unique<FunctionParameterDeclaration>(
|
|
location_for( *param ), std::move( parameter_name ), byref, unused );
|
|
}
|
|
parameters.push_back( std::move( parameter_declaration ) );
|
|
}
|
|
}
|
|
|
|
auto source_location = location_for( *ctx );
|
|
auto parameter_list =
|
|
std::make_unique<FunctionParameterList>( source_location, std::move( parameters ) );
|
|
return std::make_unique<ModuleFunctionDeclaration>(
|
|
source_location, std::move( module_name ), std::move( name ), std::move( parameter_list ) );
|
|
}
|
|
|
|
} // namespace Pol::Bscript::Compiler
|