2020-08-10 00:42:37 -07:00
|
|
|
#include "CompilerWorkspaceBuilder.h"
|
|
|
|
|
|
2021-02-25 16:53:22 +01:00
|
|
|
#include "bscript/compiler/Profile.h"
|
|
|
|
|
#include "bscript/compiler/Report.h"
|
|
|
|
|
#include "bscript/compiler/ast/ModuleFunctionDeclaration.h"
|
|
|
|
|
#include "bscript/compiler/ast/Program.h"
|
|
|
|
|
#include "bscript/compiler/ast/Statement.h"
|
|
|
|
|
#include "bscript/compiler/ast/TopLevelStatements.h"
|
|
|
|
|
#include "bscript/compiler/ast/UserFunction.h"
|
|
|
|
|
#include "bscript/compiler/astbuilder/AvailableUserFunction.h"
|
|
|
|
|
#include "bscript/compiler/astbuilder/BuilderWorkspace.h"
|
|
|
|
|
#include "bscript/compiler/astbuilder/SourceFileProcessor.h"
|
|
|
|
|
#include "bscript/compiler/astbuilder/UserFunctionVisitor.h"
|
|
|
|
|
#include "bscript/compiler/file/SourceFile.h"
|
|
|
|
|
#include "bscript/compiler/file/SourceFileIdentifier.h"
|
|
|
|
|
#include "bscript/compiler/file/SourceLocation.h"
|
|
|
|
|
#include "bscript/compiler/model/CompilerWorkspace.h"
|
2024-01-12 06:51:44 +01:00
|
|
|
#include "clib/timer.h"
|
2020-08-10 00:42:37 -07:00
|
|
|
|
2020-08-10 03:20:29 -07:00
|
|
|
namespace Pol::Bscript::Compiler
|
2020-08-10 00:42:37 -07:00
|
|
|
{
|
2020-08-16 17:22:16 -07:00
|
|
|
CompilerWorkspaceBuilder::CompilerWorkspaceBuilder( SourceFileCache& em_cache,
|
2021-03-06 01:39:26 +01:00
|
|
|
SourceFileCache& inc_cache, Profile& profile,
|
|
|
|
|
Report& report )
|
2020-08-16 17:22:16 -07:00
|
|
|
: em_cache( em_cache ), inc_cache( inc_cache ), profile( profile ), report( report )
|
2020-08-10 00:42:37 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CompilerWorkspace> CompilerWorkspaceBuilder::build(
|
2021-03-06 01:39:26 +01:00
|
|
|
const std::string& pathname, UserFunctionInclusion user_function_inclusion )
|
2020-08-10 00:42:37 -07:00
|
|
|
{
|
2020-09-01 00:54:24 -07:00
|
|
|
auto compiler_workspace = std::make_unique<CompilerWorkspace>( report );
|
2020-08-16 17:22:16 -07:00
|
|
|
BuilderWorkspace workspace( *compiler_workspace, em_cache, inc_cache, profile, report );
|
2020-08-10 00:42:37 -07:00
|
|
|
|
|
|
|
|
auto ident = std::make_unique<SourceFileIdentifier>( 0, pathname );
|
|
|
|
|
|
2020-08-14 13:54:20 -07:00
|
|
|
SourceLocation source_location( ident.get(), 0, 0 );
|
|
|
|
|
|
|
|
|
|
if ( SourceFile::enforced_case_sensitivity_mismatch( source_location, pathname, report ) )
|
|
|
|
|
{
|
2024-01-12 06:51:44 +01:00
|
|
|
report.error( *ident, "Refusing to load '{}'.", pathname );
|
2020-08-14 13:54:20 -07:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto sf = SourceFile::load( *ident, profile, report );
|
|
|
|
|
|
|
|
|
|
if ( !sf || report.error_count() )
|
|
|
|
|
{
|
2024-01-12 06:51:44 +01:00
|
|
|
report.error( *ident, "Unable to load '{}'.", pathname );
|
2020-08-14 13:54:20 -07:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 00:41:45 -07:00
|
|
|
SourceFileProcessor src_processor( *ident, workspace, true, user_function_inclusion );
|
2020-08-18 23:32:57 -07:00
|
|
|
|
2020-08-10 00:42:37 -07:00
|
|
|
workspace.compiler_workspace.referenced_source_file_identifiers.push_back( std::move( ident ) );
|
2020-08-14 13:54:20 -07:00
|
|
|
workspace.source_files[sf->pathname] = sf;
|
2020-08-10 00:42:37 -07:00
|
|
|
|
2020-08-17 22:53:03 -07:00
|
|
|
compiler_workspace->top_level_statements =
|
|
|
|
|
std::make_unique<TopLevelStatements>( source_location );
|
|
|
|
|
|
2020-08-26 19:24:47 -07:00
|
|
|
src_processor.use_module( "basic", source_location );
|
2020-08-23 15:01:39 -07:00
|
|
|
src_processor.use_module( "basicio", source_location );
|
2020-08-18 23:32:57 -07:00
|
|
|
src_processor.process_source( *sf );
|
|
|
|
|
|
2020-08-31 18:46:27 -07:00
|
|
|
if ( report.error_count() == 0 )
|
|
|
|
|
build_referenced_user_functions( workspace );
|
|
|
|
|
|
2020-08-10 00:42:37 -07:00
|
|
|
return compiler_workspace;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 23:29:22 -07:00
|
|
|
|
2020-08-31 18:46:27 -07:00
|
|
|
void CompilerWorkspaceBuilder::build_referenced_user_functions( BuilderWorkspace& workspace )
|
|
|
|
|
{
|
|
|
|
|
Pol::Tools::HighPerfTimer timer;
|
|
|
|
|
|
|
|
|
|
std::vector<AvailableUserFunction> to_build;
|
|
|
|
|
while ( workspace.function_resolver.resolve( to_build ) )
|
|
|
|
|
{
|
|
|
|
|
for ( auto& auf : to_build )
|
|
|
|
|
{
|
|
|
|
|
UserFunctionVisitor user_function_visitor( *auf.source_location.source_file_identifier,
|
|
|
|
|
workspace );
|
|
|
|
|
|
|
|
|
|
auf.parse_rule_context->accept( &user_function_visitor );
|
|
|
|
|
}
|
|
|
|
|
to_build.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workspace.profile.ast_resolve_functions_micros += timer.ellapsed().count();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 03:20:29 -07:00
|
|
|
} // namespace Pol::Bscript::Compiler
|