| 1 |
#ifndef _Interpreter_h_ |
| 2 |
#define _Interpreter_h_ |
| 3 |
/* Interpreter.h |
| 4 |
* |
| 5 |
* Copyright (C) 1993-2018,2020-2026 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "Collection.h" |
| 22 |
#include "Gui.h" |
| 23 |
#include "Formula.h" |
| 24 |
|
| 25 |
#include <string> |
| 26 |
#include <unordered_map> |
| 27 |
|
| 28 |
Thing_define (InterpreterVariable, SimpleString) { |
| 29 |
double numericValue; // a variable whose name has no suffix: a real, an integer, or a boolean |
| 30 |
autostring32 stringValue; // a variable whose name has the suffix "$" |
| 31 |
autoVEC numericVectorValue; // a variable whose name has the suffix "#" |
| 32 |
autoMAT numericMatrixValue; // a variable whose name has the suffix "##" |
| 33 |
autoSTRVEC stringArrayValue; // a variable whose name has the suffix "$#" |
| 34 |
}; |
| 35 |
|
| 36 |
#define Interpreter_MAXNUM_PARAMETERS 400 |
| 37 |
#define Interpreter_MAX_CALL_DEPTH 50 |
| 38 |
|
| 39 |
#define Interpreter_MAX_LABEL_LENGTH 99 |
| 40 |
#define Interpreter_MAX_PARAMETER_LENGTH 99 |
| 41 |
#define Interpreter_MAX_FORMAT_LENGTH 39 |
| 42 |
|
| 43 |
Thing_declare (UiForm); |
| 44 |
Thing_declare (UiField); |
| 45 |
Thing_declare (Editor); |
| 46 |
Thing_declare (Script); |
| 47 |
Thing_declare (Notebook); |
| 48 |
Thing_declare (InterpreterStack); |
| 49 |
|
| 50 |
enum class kInterpreter_ReturnType { |
| 51 |
VOID_ = 0, // don't change; this is how it is automatically zero-initialized in structInterpreter |
| 52 |
OBJECT_, |
| 53 |
|
| 54 |
/* |
| 55 |
The following three are not distinguished in PraatScript, |
| 56 |
but they may need to be distinguished in PraatLib (for C, Python, R). |
| 57 |
*/ |
| 58 |
REAL_, |
| 59 |
INTEGER_, |
| 60 |
BOOLEAN_, |
| 61 |
|
| 62 |
STRING_, |
| 63 |
|
| 64 |
/* |
| 65 |
The following two are not distinguished in PraatScript, |
| 66 |
but they may need to be distinguished in PraatLib (for C, Python, R). |
| 67 |
*/ |
| 68 |
REALVECTOR_, |
| 69 |
INTEGERVECTOR_, |
| 70 |
|
| 71 |
REALMATRIX_, |
| 72 |
STRINGARRAY_ |
| 73 |
}; |
| 74 |
|
| 75 |
conststring32 kInterpreter_ReturnType_errorMessage (kInterpreter_ReturnType returnType, conststring32 command); |
| 76 |
|
| 77 |
Thing_define (Interpreter, Thing) { |
| 78 |
Script scriptReference; |
| 79 |
Notebook notebookReference; |
| 80 |
InterpreterStack owningInterpreterStack; |
| 81 |
bool isHalted, isInSecondPass; |
| 82 |
|
| 83 |
struct EditorEnvironment { |
| 84 |
ClassInfo _optionalClass; |
| 85 |
Editor _optionalInstance; |
| 86 |
void _setFromOptionalEditor (Editor optionalEditor) noexcept { |
| 87 |
our _optionalClass = ( optionalEditor ? ((Thing) optionalEditor) -> classInfo : nullptr ); |
| 88 |
// class Editor hasn't been defined yet, but assuming it's a Thing seems to be safe... |
| 89 |
our _optionalInstance = optionalEditor; |
| 90 |
} |
| 91 |
conststring32 _optionalClassName () noexcept { |
| 92 |
return _optionalClass ? our _optionalClass -> className : nullptr; |
| 93 |
} |
| 94 |
} _owningEditorEnvironment, _dynamicEditorEnvironment; |
| 95 |
/* |
| 96 |
Each entire editor environment (but not its parts separately) can be set from an Editor; |
| 97 |
in case of an owning editor, the editor is optional; in case of a dynamic editor, it's obligatory. |
| 98 |
The parts of an editor environment (class name and instance) can be separately gotten. |
| 99 |
*/ |
| 100 |
void setOwningEditorEnvironmentFromOptionalEditor (Editor optionalEditor) noexcept { |
| 101 |
our _owningEditorEnvironment. _setFromOptionalEditor (optionalEditor); |
| 102 |
} |
| 103 |
conststring32 optionalOwningEditorEnvironmentClassName () noexcept { |
| 104 |
return our _owningEditorEnvironment. _optionalClassName(); |
| 105 |
} |
| 106 |
Editor optionalOwningEnvironmentEditor () noexcept { |
| 107 |
return our _owningEditorEnvironment. _optionalInstance; |
| 108 |
} |
| 109 |
void setDynamicEditorEnvironmentFromEditor (Editor editor) noexcept { |
| 110 |
Melder_assert (editor); |
| 111 |
our _dynamicEditorEnvironment. _setFromOptionalEditor (editor); |
| 112 |
} |
| 113 |
conststring32 optionalDynamicEditorEnvironmentClassName () noexcept { |
| 114 |
return our _dynamicEditorEnvironment. _optionalClassName(); |
| 115 |
} |
| 116 |
Editor optionalDynamicEnvironmentEditor () noexcept { |
| 117 |
return our _dynamicEditorEnvironment. _optionalInstance; |
| 118 |
} |
| 119 |
/* |
| 120 |
An owning editor environment can be copied to a dynamic one, but not the other way round. |
| 121 |
*/ |
| 122 |
void setDynamicFromOwningEditorEnvironment () noexcept { |
| 123 |
our _dynamicEditorEnvironment = our _owningEditorEnvironment; // TODO: what if the owner has been orphaned? |
| 124 |
} |
| 125 |
/* |
| 126 |
Memory of the past. |
| 127 |
*/ |
| 128 |
bool wasStartedFromEditorEnvironment () noexcept { |
| 129 |
return !! our _owningEditorEnvironment. _optionalClass; |
| 130 |
} |
| 131 |
/* |
| 132 |
Awareness of the present. |
| 133 |
*/ |
| 134 |
bool hasDynamicEnvironmentEditor () noexcept { |
| 135 |
return !! our _dynamicEditorEnvironment. _optionalInstance; |
| 136 |
} |
| 137 |
/* |
| 138 |
Forgetting. The owning editor's class will always be remembered, though. That's our identity, so to say. |
| 139 |
|
| 140 |
The dynamic editor's class will also be remembered, |
| 141 |
on behalf of an error message by a continuing pause script. |
| 142 |
(last checked 2023-03-05) |
| 143 |
*/ |
| 144 |
void undangleOwningEditor () noexcept { |
| 145 |
our _owningEditorEnvironment. _optionalInstance = nullptr; |
| 146 |
} |
| 147 |
void undangleDynamicEditor () noexcept { |
| 148 |
our _dynamicEditorEnvironment. _optionalInstance = nullptr; |
| 149 |
} |
| 150 |
void undangleEditorEnvironments () noexcept { |
| 151 |
our _owningEditorEnvironment. _optionalInstance = nullptr; |
| 152 |
our _dynamicEditorEnvironment. _optionalInstance = nullptr; |
| 153 |
} |
| 154 |
void nullifyDynamicEditorEnvironment () noexcept { |
| 155 |
our _dynamicEditorEnvironment. _optionalClass = nullptr; |
| 156 |
our _dynamicEditorEnvironment. _optionalInstance = nullptr; |
| 157 |
} |
| 158 |
|
| 159 |
autostring32 text; |
| 160 |
structMelderFile file; |
| 161 |
structMelderFolder workingDirectory; |
| 162 |
|
| 163 |
int numberOfParameters, callDepth; |
| 164 |
int types [1+Interpreter_MAXNUM_PARAMETERS], numbersOfLines [1+Interpreter_MAXNUM_PARAMETERS]; |
| 165 |
char32 parameters [1+Interpreter_MAXNUM_PARAMETERS] [1+Interpreter_MAX_PARAMETER_LENGTH]; |
| 166 |
char32 formats [1+Interpreter_MAXNUM_PARAMETERS] [1+Interpreter_MAX_FORMAT_LENGTH]; |
| 167 |
autostring32 arguments [1+Interpreter_MAXNUM_PARAMETERS]; |
| 168 |
char32 choiceArguments [1+Interpreter_MAXNUM_PARAMETERS] [100]; |
| 169 |
autoSTRVEC labelNames; |
| 170 |
autoINTVEC labelLines; |
| 171 |
autoSTRVEC procedureNames; |
| 172 |
autoINTVEC procedureStartLines; |
| 173 |
autostring32 dialogTitle; |
| 174 |
char32 procedureStackNames [1+Interpreter_MAX_CALL_DEPTH] [100]; |
| 175 |
std::unordered_map <std::u32string, autoInterpreterVariable> variablesMap; |
| 176 |
bool running, stopped; |
| 177 |
integer callStack [1 + Interpreter_MAX_CALL_DEPTH]; |
| 178 |
bool fromif, fromendfor; |
| 179 |
|
| 180 |
autovector <mutablestring32> lines; // not autostringvector, because the elements are reference copies |
| 181 |
integer lineNumber = 0; |
| 182 |
integer assertErrorLineNumber = 0; |
| 183 |
autoMelderString assertErrorString; |
| 184 |
|
| 185 |
kInterpreter_ReturnType returnType; // automatically initialized as kInterpreter_ReturnType::VOID_ |
| 186 |
bool returnedBoolean; |
| 187 |
autostring32 returnedString; |
| 188 |
autoVEC returnedRealVector; |
| 189 |
autoINTVEC returnedIntegerVector; |
| 190 |
autoMAT returnedRealMatrix; |
| 191 |
autoSTRVEC returnedStringArray; |
| 192 |
|
| 193 |
void v9_destroy () noexcept |
| 194 |
override; |
| 195 |
}; |
| 196 |
|
| 197 |
autoInterpreter Interpreter_createFromEnvironment ( |
| 198 |
InterpreterStack owner, |
| 199 |
Editor optionalEditor, |
| 200 |
MelderFile optionalFile |
| 201 |
); |
| 202 |
|
| 203 |
void Interpreter_rememberScript (Interpreter me, MelderFile scriptFile, bool fullTrust); |
| 204 |
void Interpreter_rememberScript_override (Interpreter me, MelderFile scriptFile, const bool fullTrust); |
| 205 |
void Interpreter_rememberNotebook (Interpreter me, MelderFile notebookFile, const bool fullTrust); |
| 206 |
|
| 207 |
void Interpreters_undangleEnvironment (Editor environment) noexcept; |
| 208 |
|
| 209 |
void Melder_includeIncludeFiles (autostring32 *text, bool onlyInCodeChunks = false); |
| 210 |
integer Interpreter_readParameters (Interpreter me, mutablestring32 text); |
| 211 |
Thing_declare (UiForm); |
| 212 |
autoUiForm Interpreter_createForm (Interpreter me, GuiWindow parent, Editor optionalEditor, conststring32 fileName, |
| 213 |
void (*okCallback) (UiForm sendingForm, integer narg, Stackel args, conststring32 sendingString, |
| 214 |
Interpreter interpreter, conststring32 invokingButtonTitle, bool modified, void *closure, Editor optionalEditor), |
| 215 |
void *okClosure, bool selectionOnly |
| 216 |
); |
| 217 |
void Interpreter_getArgumentsFromDialog (Interpreter me, UiForm dialog); |
| 218 |
void Interpreter_getArgumentsFromString (Interpreter me, conststring32 arguments); |
| 219 |
void Interpreter_getArgumentsFromArgs (Interpreter me, integer nargs, Stackel args); |
| 220 |
void Interpreter_getArgumentsFromCommandLine (Interpreter me, integer argc, char **argv); |
| 221 |
void Interpreter_run (Interpreter me, autostring32 text, const bool reuseVariables); // messes up 'text' |
| 222 |
void Interpreter_stop (Interpreter me); // can be called from any procedure called deep-down by the interpreter; will stop before next line |
| 223 |
void Interpreter_resume (Interpreter me); |
| 224 |
|
| 225 |
void Interpreter_voidExpression (Interpreter me, conststring32 expression); |
| 226 |
void Interpreter_numericExpression (Interpreter me, conststring32 expression, double *p_value); |
| 227 |
void Interpreter_numericVectorExpression (Interpreter me, conststring32 expression, VEC *p_value, bool *p_owned); |
| 228 |
void Interpreter_numericMatrixExpression (Interpreter me, conststring32 expression, MAT *p_value, bool *p_owned); |
| 229 |
autostring32 Interpreter_stringExpression (Interpreter me, conststring32 expression); |
| 230 |
void Interpreter_stringArrayExpression (Interpreter me, conststring32 expression, STRVEC *out_value, bool *out_owned); |
| 231 |
void Interpreter_anyExpression (Interpreter me, conststring32 expression, Formula_Result *p_result); |
| 232 |
|
| 233 |
InterpreterVariable Interpreter_hasVariable (Interpreter me, conststring32 key); |
| 234 |
InterpreterVariable Interpreter_lookUpVariable (Interpreter me, conststring32 key); |
| 235 |
|
| 236 |
constexpr integer InterpreterStack_MAXIMUM_NUMBER_OF_LEVELS = 20; |
| 237 |
|
| 238 |
Thing_define (InterpreterStack, Thing) { |
| 239 |
Editor optionalInterpreterStackOwningEditor; // TODO: remove |
| 240 |
autoInterpreter interpreters [1 + InterpreterStack_MAXIMUM_NUMBER_OF_LEVELS] { }; |
| 241 |
integer currentLevel = 0; |
| 242 |
|
| 243 |
Interpreter current_0 () { |
| 244 |
return our interpreters [our currentLevel].get(); |
| 245 |
} |
| 246 |
Interpreter current_e () { |
| 247 |
Interpreter interpreterReference = our interpreters [our currentLevel].get(); |
| 248 |
Melder_require (interpreterReference, |
| 249 |
U"Interpreter at level ", our currentLevel, U" does not exist."); |
| 250 |
return interpreterReference; |
| 251 |
} |
| 252 |
Interpreter current_a () { |
| 253 |
Interpreter interpreterReference = our interpreters [our currentLevel].get(); |
| 254 |
Melder_assert (interpreterReference); |
| 255 |
return interpreterReference; |
| 256 |
} |
| 257 |
void emptyAll (); |
| 258 |
void runDown (autoInterpreter interpreter, autostring32 text, const bool reuseVariables); |
| 259 |
void haltAll (); |
| 260 |
void resumeFromTop (); |
| 261 |
void resumeNextLevelInSecondPass (); |
| 262 |
}; |
| 263 |
|
| 264 |
autoInterpreterStack InterpreterStack_create (Editor optionalInterpreterStackOwningEditor); |
| 265 |
|
| 266 |
extern structInterpreterStack theInterpreterStack; // TODO: remove |
| 267 |
|
| 268 |
/* End of file Interpreter.h */ |
| 269 |
#endif |