Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
ScriptInterpreter.h
Go to the documentation of this file.
1#pragma once
5#include <misc/global.h>
6
7namespace sf
8{
9
14{
15 public:
21
26
31
36 {
37 return _emitterChange;
38 }
39
45 [[nodiscard]] std::string getScriptName() const;
46
52 void setScriptName(const std::string& name);
53
57 void setOutputStream(std::ostream* os);
58
62 enum EState
63 {
65 esError = 0,
75 esCurrent
76 };
77
94
98 struct CodePos
99 {
100 CodePos() = default;
101
103 : _line(line)
104 , _offset(offset)
105 {}
106
107 pos_type _line{0};
108 pos_type _offset{0};
109 };
110
116 EState Execute(EExecMode exec_mode = emRun);
117
124 bool compile(const char* cmd);
125
129 [[nodiscard]] const char* getStateName(EState exec_state = esCurrent) const;
130
134 [[nodiscard]] EState getState() const;
135
139 [[nodiscard]] std::string getDebugText() const;
140
144 [[nodiscard]] CodePos getErrorPos() const;
145
149 [[nodiscard]] ip_type getInstructionPtr() const;
150
154 [[nodiscard]] ip_type getErrorInstructionPtr() const;
155
159 [[nodiscard]] std::string getInstructionText(ip_type ip) const;
160
164 void flush();
165
172 bool callFunction(const std::string& name, bool step_mode);
173
177 void callFunction(ip_type ip, bool step_mode);
178
184 void setMaxLoopTime(unsigned long usec);
185
186 // Overloaded from base class.
187 [[nodiscard]] strings getInfoNames() const override;
188
195 [[nodiscard]] const IdInfo* getInfo(const std::string& name) const override;
196
201 {
206 {
207 eiGoto = -3,
208 eiContinue = -2,
209 eiBreak = -1,
210 eiNone = 0,
216 eiLastEntry
217 };
218
222 Instruction() = default;
223
227 Instruction(EInstr instr, ip_type ip, const CodePos& pos, std::string script = std::string());
228
232 EInstr _instr{eiNone};
236 CodePos _codePos{0, 0};
240 std::string _script{};
244 ip_type _absIp{-1};
245
249 [[nodiscard]] const char* getMnemonic() const;
250
256 [[nodiscard]] ip_type getJumpIp() const;
257 };
258
262 struct VariableInfo;
266 struct LabelInfo;
267
271 [[nodiscard]] const TVector<Instruction>& getInstructions() const
272 {
273 return _instructions;
274 }
275
279 [[nodiscard]] const TVector<VariableInfo*>& getVariables() const
280 {
281 return _variables;
282 }
283
289 [[nodiscard]] strings getIdentifiers(EIdentifier id) const override;
290
295 [[nodiscard]] bool isStepMode() const;
296
297 protected:
302 {
304 ecNormal = 0,
311 };
312
330
334 bool getSetValue(const IdInfo* info, Value* value, Value::vector_type* params, bool flag_set) override;
335
341 virtual bool compileAdditionalStatement(const IdInfo* info, const std::string& source);
342
349 virtual void exitFunction(EExitCode exitcode, const Value& value);
350
354 virtual void clear();
355
359 virtual void linkInstruction();
360
364 void setState(EState exec_state);
365
371 ip_type getLabelIp(const std::string& name);
372
382 std::ostream* _outputStream{nullptr};
383
384 private:
388 void getExternalSource(std::string& source);
389
393 ip_type addInstruction(Instruction::EInstr instr, ip_type ip, pos_type pos, const std::string& script = std::string());
394
398 void setJumpInstruction(ip_type ip, ip_type jmp_ip = -1);
399
403 void setBreakInstruction(ip_type start_ip, ip_type jmp_ip);
404
408 void setContinueInstruction(ip_type start_ip, ip_type jmp_ip);
409
413 void skipWhite();
414
418 void setPosition(pos_type pos);
419
423 bool getName(std::string& name);
424
430 bool doCompile();
431
435 bool compileKeyword(const IdInfo* Info, pos_type pos);
436
440 void doStep();
441
445 void doExecute();
446
453 bool isKeyWord(EKeyWord keyword);
454
461 const char* getKeyWordText(EKeyWord keyword);
462
470 bool getScript(std::string& script, char ending);
471
475 void getVarScript(std::string& script);
476
480 std::string getProfilePath();
481
487 const EState _currentState{esEmpty};
491 EState _prevState{esEmpty};
495 Value _accumulator{};
499 const char* _command;
503 pos_type _codePos{0};
507 pos_type _codeLine{0};
511 bool _flagSentry{false};
515 bool _flagStepMode{false};
519 TVector<Instruction> _instructions{};
523 ip_type _currentInstructionPtr{-1};
527 ip_type _errorInstructionPtr{-1};
531 ip_type _mainFunction{-1};
532
536 struct _MISC_CLASS StackEntry
537 {
538 explicit StackEntry(ip_type ip)
539 {
540 _ip = ip;
541 }
542
546 ip_type _ip{0};
547 };
548
552 TVector<StackEntry> _stack;
556 TVector<VariableInfo*> _variables;
560 TVector<LabelInfo*> _labels;
564 ElapseTimer _sleepTimer;
568 std::string _scriptName;
572 Value::int_type _trace{0};
576 static IdInfo _info[];
580 static const char* _instructionNames[];
584 static const char* _stateNames[];
588 static const char* _exitCodeNames[];
589
593 ChangeListener::emitter_type _emitterChange;
594};
595
596}// namespace sf
Timer for turning true when a certain amount of time has passed.
Definition ElapseTimer.h:12
Simple script engine able.
Definition ScriptEngine.h:13
Script interpreter for running a loaded script.
Definition ScriptInterpreter.h:14
void flush()
Calls the protected virtual clear function.
ip_type getLabelIp(const std::string &name)
Gets the ip from the label name.
void setState(EState exec_state)
Sets the execution state.
virtual bool compileAdditionalStatement(const IdInfo *info, const std::string &source)
Compile external supplied keyword.
strings getIdentifiers(EIdentifier id) const override
Gets a list of available identifiers.
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 setScriptName(const std::string &name)
Sets the name given by the user.
ElapseTimer _loopTimer
Timer to prevent endless loops.
Definition ScriptInterpreter.h:378
std::string getDebugText() const
const IdInfo * getInfo(const std::string &name) const override
EExecMode
Enumerate for indicating the mode of execution.
Definition ScriptInterpreter.h:82
@ emInit
Definition ScriptInterpreter.h:84
@ emRun
Definition ScriptInterpreter.h:86
@ emStep
Definition ScriptInterpreter.h:90
@ emAbort
Definition ScriptInterpreter.h:92
@ emStart
Definition ScriptInterpreter.h:88
virtual void exitFunction(EExitCode exitcode, const Value &value)
Function is called at the exit of the script.
EExitCode
Possible exit codes.
Definition ScriptInterpreter.h:302
@ ecError
Definition ScriptInterpreter.h:306
@ ecApplication
Definition ScriptInterpreter.h:310
@ ecScript
Definition ScriptInterpreter.h:308
const TVector< Instruction > & getInstructions() const
Gets the instruction text at pointer IP.
Definition ScriptInterpreter.h:271
bool callFunction(const std::string &name, bool step_mode)
EState getState() const
bool compile(const char *cmd)
Compiles the passed script text and outputs debug info to the stream.
ChangeListener::emitter_type & getChangeEmitter()
Gets the emitter for change events.
Definition ScriptInterpreter.h:35
void callFunction(ip_type ip, bool step_mode)
Makes it possible to execute function/gosub.
~ScriptInterpreter() override
Virtual destructor.
bool getSetValue(const IdInfo *info, Value *value, Value::vector_type *params, bool flag_set) override
Overridden from ScriptEngine class.
void setOutputStream(std::ostream *os)
Sets and enables debugging output stream.
EState
State of the running script.
Definition ScriptInterpreter.h:63
@ esReady
Definition ScriptInterpreter.h:73
@ esRunning
Definition ScriptInterpreter.h:71
@ esCompiled
Definition ScriptInterpreter.h:69
@ esEmpty
Definition ScriptInterpreter.h:67
EKeyWord
Keywords available for the script.
Definition ScriptInterpreter.h:317
@ kwWhile
Definition ScriptInterpreter.h:320
@ kwReturn
Definition ScriptInterpreter.h:326
@ kwBreak
Definition ScriptInterpreter.h:323
@ kwExternal
Definition ScriptInterpreter.h:327
@ kwExtern
Definition ScriptInterpreter.h:328
@ kwSubroutine
Definition ScriptInterpreter.h:325
@ kwIf
Definition ScriptInterpreter.h:319
@ kwGoto
Definition ScriptInterpreter.h:322
@ kwContinue
Definition ScriptInterpreter.h:324
@ kwElse
Definition ScriptInterpreter.h:321
TListener< const ScriptInterpreter * > ChangeListener
Declare type for listening and at teh sme time a type for emitting the changes of the script like Sta...
Definition ScriptInterpreter.h:20
virtual void clear()
Clears compiled data can be overloaded by derived classes.
std::string getScriptName() const
Returns the name given by the user.
void setMaxLoopTime(unsigned long usec)
Sets the maximum loop time for this instance.
const TVector< VariableInfo * > & getVariables() const
Gets the current variables declared in the script.
Definition ScriptInterpreter.h:279
strings getInfoNames() const override
Gets names of info structures in lines.
EState Execute(EExecMode exec_mode=emRun)
Executes a macro depending on the execution mode.
CodePos getErrorPos() const
virtual void linkInstruction()
Links the labels of the calls in the script.
const char * getStateName(EState exec_state=esCurrent) const
Gets the execution state name from the passed state or the current one on state 'esCurrent'.
bool isStepMode() const
Get the step mode of the script.
ScriptInterpreter()
Default constructor.
ip_type getInstructionPtr() const
ssize_t ip_type
Instruction pointer type.
Definition ScriptObject.h:36
EIdentifier
Keyword identifiers.
Definition ScriptObject.h:41
std::string::size_type pos_type
Source position type.
Definition ScriptObject.h:32
Emitter class type for creating an instance which is emitting events to listeners.
Definition TListener.h:180
Template class used to bind listeners to a handler_type instance.
Definition TListener.h:135
Counted vector of strings.
Definition TStrings.h:12
Counted vector having additional methods and operators for ease of usage.
Definition TVector.h:25
Value container class able to performing arithmetic functions.
Definition Value.h:19
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
Position in the source with line and offset.
Definition ScriptInterpreter.h:99
CodePos(pos_type line, pos_type offset)
Definition ScriptInterpreter.h:102
Executable instruction as result of interpreting the script.
Definition ScriptInterpreter.h:201
const char * getMnemonic() const
Gets the mnemonic of the current instruction.
EInstr
Interpreter available instructions.
Definition ScriptInterpreter.h:206
@ eiCalculate
Definition ScriptInterpreter.h:211
@ eiTestJump
Definition ScriptInterpreter.h:212
@ eiJump
Definition ScriptInterpreter.h:213
@ eiCall
Definition ScriptInterpreter.h:214
@ eiRetFunction
Definition ScriptInterpreter.h:215
Instruction()=default
Default constructor.
Instruction(EInstr instr, ip_type ip, const CodePos &pos, std::string script=std::string())
Initializing constructor.
ip_type getJumpIp() const
Gets the jump ip from this instruction when applicable.
Used to create static lookup lists.
Definition ScriptObject.h:60