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
35 ChangeListener::emitter_type& getChangeEmitter()
36 {
37 return _emitterChange;
38 }
39
44 [[nodiscard]] std::string getScriptName() const;
45
50 void setScriptName(const std::string& name);
51
55 void setOutputStream(std::ostream* os);
56
60 enum EState
61 {
63 esError = 0,
73 esCurrent
74 };
75
80 {
90 emAbort
91 };
92
96 struct CodePos
97 {
98 CodePos() = default;
99
101 : _line(line)
102 , _offset(offset)
103 {}
104
105 pos_type _line{0};
106 pos_type _offset{0};
107 };
108
113 EState execute(EExecMode exec_mode = emRun);
114
121 bool compile(const char* cmd);
122
126 [[nodiscard]] const char* getStateName(EState exec_state = esCurrent) const;
127
131 [[nodiscard]] EState getState() const;
132
136 [[nodiscard]] std::string getDebugText() const;
137
141 [[nodiscard]] CodePos getErrorPos() const;
142
146 [[nodiscard]] ip_type getInstructionPtr() const;
147
151 [[nodiscard]] ip_type getErrorInstructionPtr() const;
152
156 [[nodiscard]] std::string getInstructionText(ip_type ip) const;
157
161 void flush();
162
169 bool callFunction(const std::string& name, bool step_mode);
170
174 void callFunction(ip_type ip, bool step_mode);
175
180 void setMaxLoopTime(unsigned long usec);
181
182 // Overridden from base class.
183 [[nodiscard]] string_list getInfoNames() const override;
184
190 [[nodiscard]] const IdInfo* getInfo(const std::string& name) const override;
191
196 {
201 {
202 eiGoto = -3,
203 eiContinue = -2,
204 eiBreak = -1,
205 eiNone = 0,
211 eiLastEntry
212 };
213
217 Instruction() = default;
218
222 Instruction(EInstr instr, ip_type ip, const CodePos& pos, std::string script = std::string());
223
227 EInstr _instr{eiNone};
231 CodePos _codePos{0, 0};
235 std::string _script{};
239 ip_type _absIp{-1};
240
244 [[nodiscard]] const char* getMnemonic() const;
245
250 [[nodiscard]] ip_type getJumpIp() const;
251 };
252
256 struct VariableInfo;
260 struct LabelInfo;
261
265 [[nodiscard]] const TVector<Instruction>& getInstructions() const
266 {
267 return _instructions;
268 }
269
273 [[nodiscard]] const TVector<VariableInfo*>& getVariables() const
274 {
275 return _variables;
276 }
277
282 [[nodiscard]] string_list getIdentifiers(EIdentifier id) const override;
283
288 [[nodiscard]] bool isStepMode() const;
289
290 protected:
295 {
297 ecNormal = 0,
304 };
305
323
327 bool getSetValue(const IdInfo* info, Value* value, Value::vector_type* params, bool flag_set) override;
328
333 virtual bool compileAdditionalStatement(const IdInfo* info, const std::string& source);
334
340 virtual void exitFunction(EExitCode exitcode, const Value& value);
341
345 virtual void clear();
346
350 virtual void linkInstruction();
351
355 void setState(EState exec_state);
356
362 ip_type getLabelIp(const std::string& name);
363
373 std::ostream* _outputStream{nullptr};
374
375 private:
379 void getExternalSource(std::string& source);
380
384 ip_type addInstruction(Instruction::EInstr instr, ip_type ip, pos_type pos, const std::string& script = std::string());
385
389 void setJumpInstruction(ip_type ip, ip_type jmp_ip = -1);
390
394 void setBreakInstruction(ip_type start_ip, ip_type jmp_ip);
395
399 void setContinueInstruction(ip_type start_ip, ip_type jmp_ip);
400
404 void skipWhite();
405
409 void setPosition(pos_type pos);
410
414 bool getIdName(std::string& name);
415
421 bool doCompile();
422
426 bool compileKeyword(const IdInfo* Info, pos_type pos);
427
431 void doStep();
432
436 void doExecute();
437
443 bool isKeyWord(EKeyWord keyword);
444
450 const char* getKeyWordText(EKeyWord keyword);
451
458 bool getScript(std::string& script, char ending);
459
463 void getVarScript(std::string& script);
464
468 std::string getProfilePath();
469
474 const EState _currentState{esEmpty};
478 EState _prevState{esEmpty};
482 Value _accumulator{};
486 const char* _command;
490 pos_type _codePos{0};
494 pos_type _codeLine{0};
498 bool _flagSentry{false};
502 bool _flagStepMode{false};
506 TVector<Instruction> _instructions{};
510 ip_type _currentInstructionPtr{-1};
514 ip_type _errorInstructionPtr{-1};
518 ip_type _mainFunction{-1};
519
523 struct _MISC_CLASS StackEntry
524 {
525 explicit StackEntry(ip_type ip)
526 {
527 _ip = ip;
528 }
529
533 ip_type _ip{0};
534 };
535
539 TVector<StackEntry> _stack;
543 TVector<VariableInfo*> _variables;
547 TVector<LabelInfo*> _labels;
551 ElapseTimer _sleepTimer;
555 std::string _scriptName;
559 Value::int_type _trace{0};
563 static IdInfo _info[];
567 static const char* _instructionNames[];
571 static const char* _stateNames[];
575 static const char* _exitCodeNames[];
576
580 ChangeListener::emitter_type _emitterChange;
581};
582
583}// 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.
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. Is used for referencing when debugging.
ElapseTimer _loopTimer
Timer to prevent endless loops.
Definition ScriptInterpreter.h:369
std::string getDebugText() const
const IdInfo * getInfo(const std::string &name) const override
EExecMode
Enumerate for indicating the mode of execution.
Definition ScriptInterpreter.h:80
@ emInit
Definition ScriptInterpreter.h:82
@ emRun
Definition ScriptInterpreter.h:84
@ emStep
Definition ScriptInterpreter.h:88
@ emStart
Definition ScriptInterpreter.h:86
virtual void exitFunction(EExitCode exitcode, const Value &value)
Function is called at the exit of the script.
string_list getInfoNames() const override
Gets names of info structures in lines.
EExitCode
Possible exit codes.
Definition ScriptInterpreter.h:295
@ ecError
Definition ScriptInterpreter.h:299
@ ecApplication
Definition ScriptInterpreter.h:303
@ ecScript
Definition ScriptInterpreter.h:301
const TVector< Instruction > & getInstructions() const
Gets the instruction text at pointer IP.
Definition ScriptInterpreter.h:265
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:61
@ esReady
Definition ScriptInterpreter.h:71
@ esRunning
Definition ScriptInterpreter.h:69
@ esCompiled
Definition ScriptInterpreter.h:67
@ esEmpty
Definition ScriptInterpreter.h:65
EKeyWord
Keywords available for the script.
Definition ScriptInterpreter.h:310
@ kwWhile
Definition ScriptInterpreter.h:313
@ kwReturn
Definition ScriptInterpreter.h:319
@ kwBreak
Definition ScriptInterpreter.h:316
@ kwExternal
Definition ScriptInterpreter.h:320
@ kwExtern
Definition ScriptInterpreter.h:321
@ kwSubroutine
Definition ScriptInterpreter.h:318
@ kwIf
Definition ScriptInterpreter.h:312
@ kwGoto
Definition ScriptInterpreter.h:315
@ kwContinue
Definition ScriptInterpreter.h:317
@ kwElse
Definition ScriptInterpreter.h:314
TListener< const ScriptInterpreter * > ChangeListener
Declare type for listening and at the same time a type for emitting the changes of the script like St...
Definition ScriptInterpreter.h:20
virtual void clear()
Clears compiled data can be overridden by derived classes.
std::string getScriptName() const
Gets the name given by the user. Is used for referencing when debugging.
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:273
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'.
string_list getIdentifiers(EIdentifier id) const override
Gets a list of available identifiers. Used for syntax high lighting for example or code completion.
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:37
EIdentifier
Keyword identifiers.
Definition ScriptObject.h:43
std::string::size_type pos_type
Source position type.
Definition ScriptObject.h:33
Template class used to bind listeners to a handler_type instance.
Definition TListener.h:79
Counted vector having additional methods and operators for ease of usage. This template class extends...
Definition TVector.h:19
Value container class able to performing arithmetic functions.
Definition Value.h:19
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
TStrings< std::string > string_list
Vector of std::strings with additional functionality.
Definition TStrings.h:97
Position in the source with line and offset.
Definition ScriptInterpreter.h:97
CodePos(pos_type line, pos_type offset)
Definition ScriptInterpreter.h:100
Executable instruction as result of interpreting the script.
Definition ScriptInterpreter.h:196
const char * getMnemonic() const
Gets the mnemonic of the current instruction.
EInstr
Interpreter available instructions.
Definition ScriptInterpreter.h:201
@ eiCalculate
Definition ScriptInterpreter.h:206
@ eiTestJump
Definition ScriptInterpreter.h:207
@ eiJump
Definition ScriptInterpreter.h:208
@ eiCall
Definition ScriptInterpreter.h:209
@ eiRetFunction
Definition ScriptInterpreter.h:210
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:62