11// SPDX-License-Identifier: Apache-2.0
22
33#include < scratchcpp/dev/compiler.h>
4+ #include < scratchcpp/dev/compilercontext.h>
45#include < scratchcpp/dev/compilerconstant.h>
56#include < scratchcpp/block.h>
67#include < scratchcpp/input.h>
1213
1314using namespace libscratchcpp ;
1415
15- /* ! Constructs Compiler. */
16+ /* ! Constructs Compiler using the given context. */
17+ Compiler::Compiler (CompilerContext *ctx) :
18+ impl(spimpl::make_unique_impl<CompilerPrivate>(ctx))
19+ {
20+ }
21+
22+ /* ! Constructs Compiler using a new context for the given target. */
1623Compiler::Compiler (IEngine *engine, Target *target) :
1724 impl(spimpl::make_unique_impl<CompilerPrivate>(engine, target))
1825{
@@ -21,13 +28,13 @@ Compiler::Compiler(IEngine *engine, Target *target) :
2128/* ! Returns the Engine of the project. */
2229IEngine *Compiler::engine () const
2330{
24- return impl->engine ;
31+ return impl->ctx -> engine () ;
2532}
2633
2734/* ! Returns the Target of this compiler. */
2835Target *Compiler::target () const
2936{
30- return impl->target ;
37+ return impl->ctx -> target () ;
3138}
3239
3340/* ! Returns currently compiled block. */
@@ -39,7 +46,7 @@ std::shared_ptr<libscratchcpp::Block> Compiler::block() const
3946/* ! Compiles the script starting with the given block. */
4047std::shared_ptr<ExecutableCode> Compiler::compile (std::shared_ptr<Block> startBlock)
4148{
42- impl->builder = impl->builderFactory ->create (impl->target , startBlock-> id () , false );
49+ impl->builder = impl->builderFactory ->create (impl->ctx , false );
4350 impl->substackTree .clear ();
4451 impl->substackHit = false ;
4552 impl->emptySubstack = false ;
@@ -570,6 +577,13 @@ const std::unordered_set<std::string> &Compiler::unsupportedBlocks() const
570577 return impl->unsupportedBlocks ;
571578}
572579
580+ /* ! Creates a compiler context for the given target. */
581+ std::shared_ptr<CompilerContext> Compiler::createContext (IEngine *engine, Target *target)
582+ {
583+ CompilerPrivate::initBuilderFactory ();
584+ return CompilerPrivate::builderFactory->createCtx (engine, target);
585+ }
586+
573587CompilerValue *Compiler::addInput (Input *input)
574588{
575589 if (!input)
0 commit comments