Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
sf::ScriptInterpreter Class Reference

Script interpreter for running a loaded script. More...

#include <ScriptInterpreter.h>

Inheritance diagram for sf::ScriptInterpreter:
Collaboration diagram for sf::ScriptInterpreter:

Classes

struct  CodePos
 Position in the source with line and offset. More...
 
struct  Instruction
 Executable instruction as result of interpreting the script. More...
 

Public Types

enum  EState {
  esError = 0 , esEmpty , esCompiled , esRunning ,
  esReady , esCurrent
}
 State of the running script. More...
 
enum  EExecMode {
  emInit , emRun , emStart , emStep ,
  emAbort
}
 Enumerate for indicating the mode of execution. More...
 
typedef TListener< const ScriptInterpreter * > ChangeListener
 Declare type for listening and at teh sme time a type for emitting the changes of the script like State and Error and execution pointer.
 
- Public Types inherited from sf::ScriptEngine
enum  EArithError {
  aeCurrent = -1 , aeSuccess = 0 , aeStringTooLong , aeUnexpectedEnd ,
  aeUnexpectedCharacter , aeUnexpectedIdentifier , aeExpectedRightParenthesis , aeExpectedLeftParenthesis ,
  aeExpectedDelimiter , aeExpectedFunction , aeExpectedIdentifier , aeUnknownFunction ,
  aeUnknownConstant , aeUnknownVariable , aeUnknownSymbol , aeUnknownIdentifier ,
  aeUnknownObjectMember , aeAssignConstant , aeMaxIdentifierLength , aeTooManyParameters ,
  aeTooFewParameters , aeMultipleDeclaration , aeUnexpectedKeyword , aeDivisionByZero ,
  aeLabelNotFound , aeIpStack , aeExternalKeyword , aeScriptTimeout ,
  aeFunctionError , aeNotLValue , aeNotObject , aeCompilerImplementationError
}
 Errors when compiling a script. More...
 
- Public Types inherited from sf::ScriptObject
enum  EIdentifier {
  idUnknown = 0 , idConstant , idVariable , idFunction ,
  idTypedef , idKeyword
}
 Keyword identifiers. More...
 
typedef std::string::size_type pos_type
 Source position type.
 
typedef ssize_t ip_type
 Instruction pointer type.
 

Public Member Functions

 ScriptInterpreter ()
 Default constructor.
 
 ~ScriptInterpreter () override
 Virtual destructor.
 
ChangeListener::emitter_typegetChangeEmitter ()
 Gets the emitter for change events.
 
std::string getScriptName () const
 Returns the name given by the user.
 
void setScriptName (const std::string &name)
 Sets the name given by the user.
 
void setOutputStream (std::ostream *os)
 Sets and enables debugging output stream.
 
EState Execute (EExecMode exec_mode=emRun)
 Executes a macro depending on the execution mode.
 
bool compile (const char *cmd)
 Compiles the passed script text and outputs debug info to the stream.
 
const char * getStateName (EState exec_state=esCurrent) const
 Gets the execution state name from the passed state or the current one on state 'esCurrent'.
 
EState getState () const
 
std::string getDebugText () const
 
CodePos getErrorPos () const
 
ip_type getInstructionPtr () const
 
ip_type getErrorInstructionPtr () const
 Gets the error instruction pointer in case of an error.
 
std::string getInstructionText (ip_type ip) const
 Gets the instruction text at pointer IP.
 
void flush ()
 Calls the protected virtual clear function.
 
bool callFunction (const std::string &name, bool step_mode)
 
void callFunction (ip_type ip, bool step_mode)
 Makes it possible to execute function/gosub.
 
void setMaxLoopTime (unsigned long usec)
 Sets the maximum loop time for this instance.
 
strings getInfoNames () const override
 Gets names of info structures in lines.
 
const IdInfogetInfo (const std::string &name) const override
 
const TVector< Instruction > & getInstructions () const
 Gets the instruction text at pointer IP.
 
const TVector< VariableInfo * > & getVariables () const
 Gets the current variables declared in the script.
 
strings getIdentifiers (EIdentifier id) const override
 Gets a list of available identifiers.
 
bool isStepMode () const
 Get the step mode of the script.
 
- Public Member Functions inherited from sf::ScriptEngine
 ScriptEngine ()
 Default constructor.
 
bool calculate (const std::string &script, Value &result)
 Calculates the passed script.
 
EArithError getError () const
 Gets the error value of this instance.
 
std::string getErrorReason () const
 Gets the associated error reason string.
 
bool setError (EArithError error_value, const std::string &reason=std::string())
 Sets the error value for this instance.
 
const char * getErrorText (EArithError error_value=EArithError::aeCurrent) const
 
pos_type getPos () const
 Gets the current position.
 
- Public Member Functions inherited from sf::ScriptObject
 ScriptObject (const char *type_name, ScriptObject *parent=nullptr)
 
virtual ~ScriptObject ()=default
 Virtual destructor which can be overloaded to clean up objects.
 
virtual std::string getStatusText ()
 Gets the status text of this object for debugging purposes.
 
int getRefCount () const
 Gets the reference count.
 
 operator Value () const
 Cast operator to be able to return this instance as a Value.
 
std::string getTypeName () const
 Returns the type name Set at the constructor.
 
ScriptObjectgetParent ()
 Gets the script object owner.
 
const ScriptObjectgetParent () const
 Gets the script object owner.
 
ScriptObjectcastToObject (const Value &value)
 Casts a sf::Value::vitCustom typed sf::Value to a ScriptObject typed pointer.
 

Protected Types

enum  EExitCode { ecNormal = 0 , ecError , ecScript , ecApplication }
 Possible exit codes. More...
 
enum  EKeyWord {
  kwNone = 0 , kwIf , kwWhile , kwElse ,
  kwGoto , kwBreak , kwContinue , kwSubroutine ,
  kwReturn , kwExternal , kwExtern
}
 Keywords available for the script. More...
 

Protected Member Functions

bool getSetValue (const IdInfo *info, Value *value, Value::vector_type *params, bool flag_set) override
 Overridden from ScriptEngine class.
 
virtual bool compileAdditionalStatement (const IdInfo *info, const std::string &source)
 Compile external supplied keyword.
 
virtual void exitFunction (EExitCode exitcode, const Value &value)
 Function is called at the exit of the script.
 
virtual void clear ()
 Clears compiled data can be overloaded by derived classes.
 
virtual void linkInstruction ()
 Links the labels of the calls in the script.
 
void setState (EState exec_state)
 Sets the execution state.
 
ip_type getLabelIp (const std::string &name)
 Gets the ip from the label name.
 
- Protected Member Functions inherited from sf::ScriptEngine
bool isAlpha (char ch)
 Determines if the passed character is an alpha one.
 
- Protected Member Functions inherited from sf::ScriptObject
void makeParent (ScriptObject *so)
 Makes this object the owner of the other object.
 
void setParent (ScriptObject *parent)
 Sets the owner to the pass script object.
 

Protected Attributes

ElapseTimer _loopTimer
 Timer to prevent endless loops.
 
std::ostream * _outputStream {nullptr}
 Output stream to write debug information to.
 

Additional Inherited Members

- Static Public Member Functions inherited from sf::ScriptObject
static const IdInfogetInfoUnknown ()
 Returns the static Info structure for unknowns.
 

Detailed Description

Script interpreter for running a loaded script.

Member Typedef Documentation

◆ ChangeListener

Declare type for listening and at teh sme time a type for emitting the changes of the script like State and Error and execution pointer.

Member Enumeration Documentation

◆ EExecMode

Enumerate for indicating the mode of execution.

Enumerator
emInit 

Initialize script in continues mode.

emRun 

Run script in continuous mode.

emStart 

Starts a stepping macro again.

emStep 

Execute only one instruction and return.

emAbort 

To abort a running script.

◆ EExitCode

Possible exit codes.

Enumerator
ecNormal 

Default normal stop at end of script

ecError 

Exit as result of an error in the script

ecScript 

Script exited by script function Exit()

ecApplication 

Script exited by Execute() member function

◆ EKeyWord

Keywords available for the script.

Enumerator
kwNone 
kwIf 
kwWhile 
kwElse 
kwGoto 
kwBreak 
kwContinue 
kwSubroutine 
kwReturn 
kwExternal 
kwExtern 

◆ EState

State of the running script.

Enumerator
esError 

Script went into an error due to compiling or running.

esEmpty 

Script is empty

esCompiled 

Script is compiled and ready for initialization.

esRunning 

Script is running in step mode. (also internally used in run mode)

esReady 

Script is initialized and ready for running or just finished running.

esCurrent 

When passed to getStateName() the current state is used.

Constructor & Destructor Documentation

◆ ScriptInterpreter()

sf::ScriptInterpreter::ScriptInterpreter ( )

Default constructor.

◆ ~ScriptInterpreter()

sf::ScriptInterpreter::~ScriptInterpreter ( )
override

Virtual destructor.

Member Function Documentation

◆ callFunction() [1/2]

bool sf::ScriptInterpreter::callFunction ( const std::string &  name,
bool  step_mode 
)

Calls a label/function in the script.

Parameters
nameName of the label.
step_modeWhen true in step mode
Returns
True when label/function was found.

◆ callFunction() [2/2]

void sf::ScriptInterpreter::callFunction ( ip_type  ip,
bool  step_mode 
)

Makes it possible to execute function/gosub.

◆ clear()

virtual void sf::ScriptInterpreter::clear ( )
protectedvirtual

Clears compiled data can be overloaded by derived classes.

◆ compile()

bool sf::ScriptInterpreter::compile ( const char *  cmd)

Compiles the passed script text and outputs debug info to the stream.

Parameters
cmdScript to compile.
Returns
True when successful.

◆ compileAdditionalStatement()

virtual bool sf::ScriptInterpreter::compileAdditionalStatement ( const IdInfo info,
const std::string &  source 
)
protectedvirtual

Compile external supplied keyword.

Returns
On error it returns 'false'.

◆ Execute()

EState sf::ScriptInterpreter::Execute ( EExecMode  exec_mode = emRun)

Executes a macro depending on the execution mode.

Returns
State execution.

◆ exitFunction()

virtual void sf::ScriptInterpreter::exitFunction ( EExitCode  exitcode,
const Value value 
)
protectedvirtual

Function is called at the exit of the script.

Parameters
exitcodeExit code provided.
value

◆ flush()

void sf::ScriptInterpreter::flush ( )

Calls the protected virtual clear function.

◆ getChangeEmitter()

ChangeListener::emitter_type & sf::ScriptInterpreter::getChangeEmitter ( )
inline

Gets the emitter for change events.

◆ getDebugText()

std::string sf::ScriptInterpreter::getDebugText ( ) const

Gets the debug text.

◆ getErrorInstructionPtr()

ip_type sf::ScriptInterpreter::getErrorInstructionPtr ( ) const

Gets the error instruction pointer in case of an error.

◆ getErrorPos()

CodePos sf::ScriptInterpreter::getErrorPos ( ) const

Returns the current error position where the compiler stopped.

◆ getIdentifiers()

strings sf::ScriptInterpreter::getIdentifiers ( EIdentifier  id) const
overridevirtual

Gets a list of available identifiers.

Used for syntax high lighting for example or code completion.

Reimplemented from sf::ScriptEngine.

◆ getInfo()

const IdInfo * sf::ScriptInterpreter::getInfo ( const std::string &  name) const
overridevirtual

Overloaded from base class to append more information on identifier/keywords.

Parameters
nameName of the identifier or keyword.
Returns
Information when found else nullptr.

Reimplemented from sf::ScriptEngine.

◆ getInfoNames()

strings sf::ScriptInterpreter::getInfoNames ( ) const
overridevirtual

Gets names of info structures in lines.

Reimplemented from sf::ScriptEngine.

◆ getInstructionPtr()

ip_type sf::ScriptInterpreter::getInstructionPtr ( ) const

Gets the current instruction pointer.

◆ getInstructions()

const TVector< Instruction > & sf::ScriptInterpreter::getInstructions ( ) const
inline

Gets the instruction text at pointer IP.

◆ getInstructionText()

std::string sf::ScriptInterpreter::getInstructionText ( ip_type  ip) const

Gets the instruction text at pointer IP.

◆ getLabelIp()

ip_type sf::ScriptInterpreter::getLabelIp ( const std::string &  name)
protected

Gets the ip from the label name.

Returns
On failure -1.

◆ getScriptName()

std::string sf::ScriptInterpreter::getScriptName ( ) const

Returns the name given by the user.

Is used for referencing when debugging.

◆ getSetValue()

bool sf::ScriptInterpreter::getSetValue ( const IdInfo info,
Value value,
Value::vector_type params,
bool  flag_set 
)
overrideprotectedvirtual

Overridden from ScriptEngine class.

Reimplemented from sf::ScriptEngine.

◆ getState()

EState sf::ScriptInterpreter::getState ( ) const

Gets the current execution state.

◆ getStateName()

const char * sf::ScriptInterpreter::getStateName ( EState  exec_state = esCurrent) const

Gets the execution state name from the passed state or the current one on state 'esCurrent'.

◆ getVariables()

const TVector< VariableInfo * > & sf::ScriptInterpreter::getVariables ( ) const
inline

Gets the current variables declared in the script.

◆ isStepMode()

bool sf::ScriptInterpreter::isStepMode ( ) const

Get the step mode of the script.

Returns
True when the script is in step mode.

◆ linkInstruction()

virtual void sf::ScriptInterpreter::linkInstruction ( )
protectedvirtual

Links the labels of the calls in the script.

◆ setMaxLoopTime()

void sf::ScriptInterpreter::setMaxLoopTime ( unsigned long  usec)

Sets the maximum loop time for this instance.

Parameters
usecMicroseconds.

◆ setOutputStream()

void sf::ScriptInterpreter::setOutputStream ( std::ostream *  os)

Sets and enables debugging output stream.

◆ setScriptName()

void sf::ScriptInterpreter::setScriptName ( const std::string &  name)

Sets the name given by the user.

Is used for referencing when debugging.

◆ setState()

void sf::ScriptInterpreter::setState ( EState  exec_state)
protected

Sets the execution state.

Member Data Documentation

◆ _loopTimer

ElapseTimer sf::ScriptInterpreter::_loopTimer
protected

Timer to prevent endless loops.

Must be reset by functions calling external functions.

◆ _outputStream

std::ostream* sf::ScriptInterpreter::_outputStream {nullptr}
protected

Output stream to write debug information to.


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