Skip to content

Class worldlang::ProgramExecutor

ClassList > worldlang > ProgramExecutor

Class that manages program execution. More...

  • #include <ProgramExecutor.hpp>

Classes

Type Name
struct Identifier

Public Types

Type Name
typedef std::function< void(ProgramExecutor &)> Callable
typedef std::variant< size_t, double, std::string, Callable, Identifier > Value
Variant type containing all possible values types for variables.

Public Functions

Type Name
ProgramExecutor ()
Constructor.
ProgramExecutor (cse491::WorldBase & world)
Constructor with function registration.
T as (const Value & a)
Get a value of type T from provided Value.
void error (const std::string & error)
Sets the error message and end interpreter execution.
std::string getErrorMessage ()
Gets the error message stored.
bool has (const Value & a)
Check whether or not this Value contains the expected type.
std::vector< Value > popArgs ()
Returns all arguments passed to an interpreter function.
Value popStack ()
Retrieves a single value from the interpreter value stack.
void pushStack (Value value)
Pushes a single Value onto the interpreter value stack.
void registerFunction (std::string name, Callable callable)
Registers a function on this ProgramExecutor object.
bool run (const std::string & program)
Executes a program from a string.
bool run ()
bool runFile (const std::string & filename)
Executes a program from a file.
void setVariable (const std::string & name, Value value)
void skipBlock (int nest=0)
T var (const std::string & name)
Gets the value of a variable as type T.
virtual ~ProgramExecutor () = default

Detailed Description

Handles run-time state of program and contains the interpreter function.

Native C++ functions can be registered on this object to extend the functionality.

Public Types Documentation

typedef Callable

using Callable =  std::function<void(ProgramExecutor&)>;

This is the signature interpreter functions should have.

To read the arguments passed to the function, use ProgramExecutor::popArgs(). To return values from a function, use ProgramExecutor::pushStack().

typedef Value

using Value =  std::variant < size_t, double, std::string, Callable, Identifier >;

Public Functions Documentation

function ProgramExecutor [1/2]

inline worldlang::ProgramExecutor::ProgramExecutor () 

function ProgramExecutor [2/2]

inline worldlang::ProgramExecutor::ProgramExecutor (
    cse491::WorldBase & world
) 

function as

Get a value of type T from provided Value.

template<typename T typename T>
inline T worldlang::ProgramExecutor::as (
    const Value & a
) 

Gets the value of type T from given Value whether it contains a value or an identifier storing that value.

If the value cannot be accessed, sets the error message and returns a default-constructed value.

For example, if your program consists of a=5 and b=a then as<double> will handle both 5 and a correctly as arguments std::get<double> is longer and only handles 5.

Parameters:

  • a Value to retrieve value from

Returns:

Value of type T

function error

Sets the error message and end interpreter execution.

inline void worldlang::ProgramExecutor::error (
    const std::string & error
) 

Sets the stored error message for the interpreter. Only the first error set is saved.

Parameters:

  • error Message to store

function getErrorMessage

Gets the error message stored.

inline std::string worldlang::ProgramExecutor::getErrorMessage () 

Gets the error message from the interpreter. If no error was set, this will be the empty string.

Returns:

Error message

function has

Check whether or not this Value contains the expected type.

template<typename T typename T>
inline bool worldlang::ProgramExecutor::has (
    const Value & a
) 

Checks the type of the given Value. If the type does not match and cannot be obtained, sets the interpreter error message and returns false. Otherwise, returns true.

Parameters:

  • a Value to validate type of.

Returns:

true if type is usable

function popArgs

Returns all arguments passed to an interpreter function.

inline std::vector< Value > worldlang::ProgramExecutor::popArgs () 

This function retrieves the arguments of the function in the same order as in the source code. The end of the argument list is determined by an internal special Identifier

This function should be called once for any Callable to get the arguments passed.

Returns:

Vector of Values provided to an interpreter function.

function popStack

Retrieves a single value from the interpreter value stack.

inline Value worldlang::ProgramExecutor::popStack () 

Returns:

Value object from stack.

function pushStack

Pushes a single Value onto the interpreter value stack.

inline void worldlang::ProgramExecutor::pushStack (
    Value value
) 

Parameters:

  • value Value to push to interpreter stack

function registerFunction

Registers a function on this ProgramExecutor object.

inline void worldlang::ProgramExecutor::registerFunction (
    std::string name,
    Callable callable
) 

This allows the given function to be called from the interpreter via a function called name.

Note:

Names can be overridden by the user's program if they overwrite name.

Parameters:

  • name Function name
  • callable Function accepting a ProgramExecutor& with no return.

function run [1/2]

Executes a program from a string.

inline bool worldlang::ProgramExecutor::run (
    const std::string & program
) 

Executes a program from a string. This is the main interpreter function. See Language.hpp for most interesting syntax and parsing details.

Parameters:

  • program Program to run.

Returns:

true if program ran successfully, false if an error occured

function run [2/2]

inline bool worldlang::ProgramExecutor::run () 

function runFile

Executes a program from a file.

inline bool worldlang::ProgramExecutor::runFile (
    const std::string & filename
) 

Parameters:

  • filename File to load

Returns:

true if program ran successfully, false if an error occured

function setVariable

inline void worldlang::ProgramExecutor::setVariable (
    const std::string & name,
    Value value
) 

function skipBlock

inline void worldlang::ProgramExecutor::skipBlock (
    int nest=0
) 

Moves the interpreter's program counter to the end of the current block.

Parameters:

  • nest Optional starting nest value (defaults to zero) Begins as if it was nested within this many start_block operations.

function var

Gets the value of a variable as type T.

template<typename T typename T>
inline T worldlang::ProgramExecutor::var (
    const std::string & name
) 

Exception:

  • std::out_of_range if it is not defined
  • std::bad_variant_access if variable is wrong type

Parameters:

  • name Variable name to check

Returns:

Value of variable as type T

function ~ProgramExecutor

virtual worldlang::ProgramExecutor::~ProgramExecutor () = default

The documentation for this class was generated from the following file source/Worlds/ProgramExecutor.hpp