nsbaci 1.0
Loading...
Searching...
No Matches
nsbaci::services::RuntimeService Class Reference

Service that manages program execution. More...

#include <runtimeService.h>

Public Member Functions

 RuntimeService ()=default
 Default constructor creates an uninitialized service.
 RuntimeService (std::unique_ptr< runtime::Interpreter > i, std::unique_ptr< runtime::Scheduler > s)
 Constructs a RuntimeService with interpreter and scheduler.
 ~RuntimeService ()=default
 Default destructor.
 RuntimeService (const RuntimeService &)=delete
RuntimeServiceoperator= (const RuntimeService &)=delete
 RuntimeService (RuntimeService &&)=default
RuntimeServiceoperator= (RuntimeService &&)=default
void loadProgram (runtime::Program &&p)
 Loads a compiled program for execution.
void reset ()
 Resets the runtime to initial state.
RuntimeResult step ()
 Executes a single instruction for any ready thread.
RuntimeResult stepThread (nsbaci::types::ThreadID threadId)
 Executes a single instruction for a specific thread.
RuntimeResult run (size_t maxSteps=0)
 Runs the program until halted, error, or step limit.
void pause ()
 Pauses continuous execution.
RuntimeState getState () const
 Gets the current runtime state.
bool isHalted () const
 Checks if the program has finished execution.
size_t threadCount () const
 Gets the number of active threads.
const std::vector< runtime::Thread > & getThreads () const
 Gets all threads from the scheduler.
const runtime::ProgramgetProgram () const
 Gets the loaded program.
void provideInput (const std::string &input)
 Provides input to the runtime.
bool isWaitingForInput () const
 Checks if the runtime is waiting for user input.
void setOutputCallback (runtime::OutputCallback callback)
 Sets the callback for output operations.
void setDrawingCallback (runtime::DrawingCallback callback)
 Sets the callback for drawing operations.

Detailed Description

Service that manages program execution.

The service is move-only to ensure single ownership of the interpreter and scheduler components.

Usage example:

RuntimeService rs(std::make_unique<NsbaciInterpreter>(),
std::make_unique<NsbaciScheduler>());
rs.loadProgram(std::move(compiledProgram));
rs.setOutputCallback([](const std::string& out) { std::cout << out; });
while (!rs.isHalted()) {
auto result = rs.step();
if (result.needsInput) {
rs.provideInput(getUserInput());
}
}
RuntimeService()=default
Default constructor creates an uninitialized service.

Constructor & Destructor Documentation

◆ RuntimeService() [1/2]

nsbaci::services::RuntimeService::RuntimeService ( )
default

Default constructor creates an uninitialized service.

A service created this way must have its interpreter and scheduler set before use, typically via move assignment from a factory-created instance.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RuntimeService() [2/2]

nsbaci::services::RuntimeService::RuntimeService ( std::unique_ptr< runtime::Interpreter > i,
std::unique_ptr< runtime::Scheduler > s )

Constructs a RuntimeService with interpreter and scheduler.

Parameters
iUnique pointer to the interpreter implementation.
sUnique pointer to the scheduler implementation.

Member Function Documentation

◆ getProgram()

const runtime::Program & nsbaci::services::RuntimeService::getProgram ( ) const

Gets the loaded program.

Returns
Const reference to the program for accessing instructions and memory.
Here is the caller graph for this function:

◆ getState()

RuntimeState nsbaci::services::RuntimeService::getState ( ) const

Gets the current runtime state.

Returns
The current RuntimeState value.
Here is the caller graph for this function:

◆ getThreads()

const std::vector< runtime::Thread > & nsbaci::services::RuntimeService::getThreads ( ) const

Gets all threads from the scheduler.

Returns
Const reference to the threads vector for UI display.
Here is the caller graph for this function:

◆ isHalted()

bool nsbaci::services::RuntimeService::isHalted ( ) const

Checks if the program has finished execution.

Returns
True if state is Halted.
Here is the caller graph for this function:

◆ isWaitingForInput()

bool nsbaci::services::RuntimeService::isWaitingForInput ( ) const

Checks if the runtime is waiting for user input.

Returns
True if a Read instruction is blocking execution.
Here is the caller graph for this function:

◆ loadProgram()

void nsbaci::services::RuntimeService::loadProgram ( runtime::Program && p)

Loads a compiled program for execution.

Initializes the runtime with the program's instructions, symbol table, and memory. Creates the initial main thread and sets state to Paused ready for execution.

Parameters
pThe compiled program to load (which must be moved into the service).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pause()

void nsbaci::services::RuntimeService::pause ( )

Pauses continuous execution.

Only affects state if currently Running; changes state to Paused.

Here is the caller graph for this function:

◆ provideInput()

void nsbaci::services::RuntimeService::provideInput ( const std::string & input)

Provides input to the runtime.

Called when the user provides input in response to a Read instruction. The input is stored and will be consumed on the next execution step.

Parameters
inputThe user-provided input string.
Here is the caller graph for this function:

◆ reset()

void nsbaci::services::RuntimeService::reset ( )

Resets the runtime to initial state.

Clears all threads, resets memory, and sets state to Idle. The program must be reloaded before execution can continue.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

RuntimeResult nsbaci::services::RuntimeService::run ( size_t maxSteps = 0)

Runs the program until halted, error, or step limit.

Executes instructions continuously until:

  • The program halts (reaches Halt instruction)
  • An error occurs
  • The maximum step count is reached
  • Input is required
Parameters
maxStepsMaximum instructions to execute (0 = unlimited).
Returns
RuntimeResult with final execution state.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setDrawingCallback()

void nsbaci::services::RuntimeService::setDrawingCallback ( runtime::DrawingCallback callback)

Sets the callback for drawing operations.

The callback is invoked whenever the program performs drawing operations (setColor, drawCircle, fillRect, etc.).

Parameters
callbackFunction to call with DrawCommand objects.
Here is the caller graph for this function:

◆ setOutputCallback()

void nsbaci::services::RuntimeService::setOutputCallback ( runtime::OutputCallback callback)

Sets the callback for output operations.

The callback is invoked whenever the program produces output (Write, Writeln, WriteRawString instructions).

Parameters
callbackFunction to call with output strings.
Here is the caller graph for this function:

◆ step()

RuntimeResult nsbaci::services::RuntimeService::step ( )

Executes a single instruction for any ready thread.

The scheduler picks the next thread to run and the interpreter executes one instruction from that thread's current position.

Returns
RuntimeResult with execution outcome, output, and I/O state.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ stepThread()

RuntimeResult nsbaci::services::RuntimeService::stepThread ( nsbaci::types::ThreadID threadId)

Executes a single instruction for a specific thread.

Allows targeted debugging by stepping only the specified thread.

Parameters
threadIdThe ID of the thread to step.
Returns
RuntimeResult with execution outcome.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ threadCount()

size_t nsbaci::services::RuntimeService::threadCount ( ) const

Gets the number of active threads.

Returns
Count of threads in the scheduler.
Here is the caller graph for this function:

The documentation for this class was generated from the following files: