polserver/pol-core/bscript/compiler/astbuilder/CompilerWorkspaceBuilder.cpp
Eric Swanson a77ddd028e
New compiler: Add optimized unary expressions (#244)
Adds:
- UnaryOperator: AST node for unary operators -, ++, --, and so forth
- UnaryOperatorOptimizer: optimizes a unary operator with its operand
  - now integer and float negation, and integer inversion
  - later x[y]++ to a single instruction

Also:
- automatically include basic.em, which includes some parameter defaults like -1.

This allows the compiler to generate output for a hello, world script with the exact same .ecl output as the legacy compiler.
2020-08-26 19:24:47 -07:00

63 lines
2.2 KiB
C++

#include "CompilerWorkspaceBuilder.h"
#include "compiler/LegacyFunctionOrder.h"
#include "compiler/Profile.h"
#include "compiler/Report.h"
#include "compiler/ast/Statement.h"
#include "compiler/ast/TopLevelStatements.h"
#include "compiler/astbuilder/BuilderWorkspace.h"
#include "compiler/astbuilder/SourceFileProcessor.h"
#include "compiler/file/SourceFile.h"
#include "compiler/file/SourceFileIdentifier.h"
#include "compiler/file/SourceLocation.h"
#include "compiler/model/CompilerWorkspace.h"
namespace Pol::Bscript::Compiler
{
CompilerWorkspaceBuilder::CompilerWorkspaceBuilder( SourceFileCache& em_cache,
SourceFileCache& inc_cache,
Profile& profile, Report& report )
: em_cache( em_cache ), inc_cache( inc_cache ), profile( profile ), report( report )
{
}
std::unique_ptr<CompilerWorkspace> CompilerWorkspaceBuilder::build(
const std::string& pathname, const LegacyFunctionOrder* /*legacy_function_order*/ )
{
auto compiler_workspace = std::make_unique<CompilerWorkspace>();
BuilderWorkspace workspace( *compiler_workspace, em_cache, inc_cache, profile, report );
auto ident = std::make_unique<SourceFileIdentifier>( 0, pathname );
SourceLocation source_location( ident.get(), 0, 0 );
if ( SourceFile::enforced_case_sensitivity_mismatch( source_location, pathname, report ) )
{
report.error( *ident, "Refusing to load '", pathname, "'.\n" );
return {};
}
auto sf = SourceFile::load( *ident, profile, report );
if ( !sf || report.error_count() )
{
report.error( *ident, "Unable to load '", pathname, "'.\n" );
return {};
}
SourceFileProcessor src_processor( *ident, workspace, true );
workspace.compiler_workspace.referenced_source_file_identifiers.push_back( std::move( ident ) );
workspace.source_files[sf->pathname] = sf;
compiler_workspace->top_level_statements =
std::make_unique<TopLevelStatements>( source_location );
src_processor.use_module( "basic", source_location );
src_processor.use_module( "basicio", source_location );
src_processor.process_source( *sf );
return compiler_workspace;
}
} // namespace Pol::Bscript::Compiler