| 1 |
/* Interpreter.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2026 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "Interpreter.h" |
| 20 |
#include "praatP.h" |
| 21 |
#include "praat_script.h" |
| 22 |
#include "UiPause.h" |
| 23 |
#include "DemoEditor.h" |
| 24 |
#include "Formula.h" |
| 25 |
#include "Notebook.h" |
| 26 |
#include "../kar/UnicodeData.h" |
| 27 |
|
| 28 |
#include "../fon/Vector.h" |
| 29 |
#include "../dwsys/NUMsorting.h" |
| 30 |
|
| 31 |
#define Interpreter_WORD 1 |
| 32 |
#define Interpreter_SENTENCE 2 |
| 33 |
#define Interpreter_TEXT 3 |
| 34 |
#define Interpreter_INFILE 4 |
| 35 |
#define Interpreter_OUTFILE 5 |
| 36 |
#define Interpreter_FOLDER 6 |
| 37 |
|
| 38 |
#define Interpreter_REAL 7 |
| 39 |
#define Interpreter_NONNEGATIVE 8 |
| 40 |
#define Interpreter_POSITIVE 9 |
| 41 |
#define Interpreter_INTEGER 10 |
| 42 |
#define Interpreter_NATURAL0 11 |
| 43 |
#define Interpreter_NATURAL1 12 |
| 44 |
#define Interpreter_BOOLEAN 13 |
| 45 |
#define Interpreter_MINIMUM_TYPE_FOR_NUMERIC_VARIABLE Interpreter_REAL |
| 46 |
#define Interpreter_MAXIMUM_TYPE_FOR_NUMERIC_VARIABLE Interpreter_BOOLEAN |
| 47 |
|
| 48 |
#define Interpreter_REALVECTOR 14 |
| 49 |
#define Interpreter_NONNEGATIVEVECTOR 15 |
| 50 |
#define Interpreter_POSITIVEVECTOR 16 |
| 51 |
#define Interpreter_INTEGERVECTOR 17 |
| 52 |
#define Interpreter_NATURAL0VECTOR 18 |
| 53 |
#define Interpreter_NATURAL1VECTOR 19 |
| 54 |
#define Interpreter_MINIMUM_TYPE_FOR_NUMERIC_VECTOR_VARIABLE Interpreter_REALVECTOR |
| 55 |
#define Interpreter_MAXIMUM_TYPE_FOR_NUMERIC_VECTOR_VARIABLE Interpreter_NATURAL1VECTOR |
| 56 |
|
| 57 |
#define Interpreter_REALMATRIX 20 |
| 58 |
#define Interpreter_CHOICE 21 |
| 59 |
#define Interpreter_OPTIONMENU 22 |
| 60 |
#define Interpreter_MAXIMUM_TYPE_WITH_VARIABLE_NAME Interpreter_OPTIONMENU |
| 61 |
|
| 62 |
#define Interpreter_BUTTON 23 |
| 63 |
#define Interpreter_OPTION 24 |
| 64 |
#define Interpreter_HEADING 25 |
| 65 |
#define Interpreter_COMMENT 26 |
| 66 |
#define Interpreter_CAPTION 27 |
| 67 |
|
| 68 |
Thing_implement (InterpreterVariable, SimpleString, 0); |
| 69 |
|
| 70 |
static autoInterpreterVariable InterpreterVariable_create (conststring32 key) { |
| 71 |
try { |
| 72 |
if (key [0] == U'e' && key [1] == U'\0') |
| 73 |
Melder_throw (U"You cannot use “e” as the name of a variable (e is the constant 2.71...)."); |
| 74 |
if (key [0] == U'p' && key [1] == U'i' && key [2] == U'\0') |
| 75 |
Melder_throw (U"You cannot use “pi” as the name of a variable (pi is the constant 3.14...)."); |
| 76 |
if (key [0] == U'u' && key [1] == U'n' && key [2] == U'd' && key [3] == U'e' && key [4] == U'f' && key [5] == U'i' && |
| 77 |
key [6] == U'n' && key [7] == U'e' && key [8] == U'd' && key [9] == U'\0') |
| 78 |
Melder_throw (U"You cannot use “undefined” as the name of a variable."); |
| 79 |
if (key [0] == U's' && key [1] == U't' && key [2] == U'o' && key [3] == U'p' && key [4] == U'w' && key [5] == U'a' && |
| 80 |
key [6] == U't' && key [7] == U'c' && key [8] == U'h' && key [9] == U'\0') |
| 81 |
Melder_throw (U"You cannot use “stopwatch” as the name of a variable."); |
| 82 |
autoInterpreterVariable me = Thing_new (InterpreterVariable); |
| 83 |
my string = Melder_dup (key); |
| 84 |
return me; |
| 85 |
} catch (MelderError) { |
| 86 |
Melder_throw (U"Interpreter variable not created."); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
conststring32 kInterpreter_ReturnType_errorMessage (kInterpreter_ReturnType returnType, conststring32 command) { |
| 91 |
switch (returnType) { |
| 92 |
case kInterpreter_ReturnType::VOID_: |
| 93 |
return Melder_cat (U"The command \"", command, U"\" does not return anything"); |
| 94 |
case kInterpreter_ReturnType::OBJECT_: |
| 95 |
return Melder_cat (U"The command \"", command, U"\" returns an object"); |
| 96 |
case kInterpreter_ReturnType::REAL_: |
| 97 |
case kInterpreter_ReturnType::INTEGER_: |
| 98 |
return Melder_cat (U"The command \"", command, U"\" returns a number or a string"); |
| 99 |
case kInterpreter_ReturnType::STRING_: |
| 100 |
return Melder_cat (U"The command \"", command, U"\" returns a string"); |
| 101 |
case kInterpreter_ReturnType::REALVECTOR_: |
| 102 |
case kInterpreter_ReturnType::INTEGERVECTOR_: |
| 103 |
return Melder_cat (U"The command \"", command, U"\" returns a vector"); |
| 104 |
case kInterpreter_ReturnType::REALMATRIX_: |
| 105 |
return Melder_cat (U"The command \"", command, U"\" returns a matrix"); |
| 106 |
case kInterpreter_ReturnType::STRINGARRAY_: |
| 107 |
return Melder_cat (U"The command \"", command, U"\" returns a string array"); |
| 108 |
default: |
| 109 |
return Melder_cat (U"The command \"", command, U"\" has an unknown return type"); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
Thing_implement (Interpreter, Thing, 0); |
| 114 |
|
| 115 |
static CollectionOf <structInterpreter> theReferencesToAllLivingInterpreters; |
| 116 |
|
| 117 |
/* global */ structInterpreterStack theInterpreterStack; |
| 118 |
|
| 119 |
void structInterpreter :: v9_destroy () noexcept { |
| 120 |
theReferencesToAllLivingInterpreters. undangleItem (this); |
| 121 |
UiPause_interpreterGoesAway (this); |
| 122 |
Demo_interpreterGoesAway (this); |
| 123 |
our Interpreter_Parent :: v9_destroy (); |
| 124 |
} |
| 125 |
|
| 126 |
autoInterpreter Interpreter_createFromEnvironment (InterpreterStack interpreterStack, Editor optionalInterpreterOwningEditor, MelderFile optionalFile) { |
| 127 |
Melder_assert (interpreterStack); |
| 128 |
try { |
| 129 |
autoInterpreter me = Thing_new (Interpreter); |
| 130 |
my owningInterpreterStack = interpreterStack; |
| 131 |
if (optionalFile) { |
| 132 |
MelderFile_copy (optionalFile, & my file); |
| 133 |
/* |
| 134 |
As for the working directory, put it in canonical form, i.e. don't use only MelderFile_getParentFolder(). TODO: make MelderFile_getParentFolder canonical |
| 135 |
*/ |
| 136 |
{// scope |
| 137 |
autoMelderFileSetCurrentFolder folder (& my file); |
| 138 |
Melder_getCurrentFolder (& my workingDirectory); |
| 139 |
} |
| 140 |
} else { |
| 141 |
Melder_getCurrentFolder (& my workingDirectory); // last resort; TODO: figure out how useful this is |
| 142 |
} |
| 143 |
my setOwningEditorEnvironmentFromOptionalEditor (optionalInterpreterOwningEditor); |
| 144 |
my variablesMap. max_load_factor (0.65f); |
| 145 |
theReferencesToAllLivingInterpreters. addItem_ref (me.get()); |
| 146 |
return me; |
| 147 |
} catch (MelderError) { |
| 148 |
Melder_throw (U"Interpreter not created."); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void Interpreters_undangleEnvironment (Editor environment) noexcept { |
| 153 |
for (integer i = 1; i <= theReferencesToAllLivingInterpreters.size; i ++) { |
| 154 |
const Interpreter interpreter = theReferencesToAllLivingInterpreters.at [i]; |
| 155 |
if (interpreter -> optionalOwningEnvironmentEditor() == environment) |
| 156 |
interpreter -> undangleOwningEditor(); |
| 157 |
if (interpreter -> optionalDynamicEnvironmentEditor() == environment) |
| 158 |
interpreter -> undangleDynamicEditor(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
void Interpreter_rememberScript (Interpreter me, MelderFile scriptFile, const bool fullTrust) { |
| 163 |
if (MelderFile_isNull (scriptFile)) |
| 164 |
return; |
| 165 |
autoScript script = Script_createFromFile (scriptFile); |
| 166 |
Script_rememberDuringThisAppSession_move (script.move()); |
| 167 |
my scriptReference = Script_find (MelderFile_peekPath (scriptFile)); |
| 168 |
my scriptReference -> trusted |= fullTrust; |
| 169 |
} |
| 170 |
void Interpreter_rememberScript_override (Interpreter me, MelderFile scriptFile, const bool fullTrust) { |
| 171 |
if (MelderFile_isNull (scriptFile)) |
| 172 |
return; |
| 173 |
autoScript script = Script_createFromFile (scriptFile); |
| 174 |
Script_rememberDuringThisAppSession_move (script.move()); |
| 175 |
my scriptReference = Script_find (MelderFile_peekPath (scriptFile)); |
| 176 |
my scriptReference -> trusted = fullTrust; |
| 177 |
} |
| 178 |
|
| 179 |
void Interpreter_rememberNotebook (Interpreter me, MelderFile notebookFile, const bool fullTrust) { |
| 180 |
TRACE |
| 181 |
trace (U"Remembering notebook file ", notebookFile); |
| 182 |
if (MelderFile_isNull (notebookFile)) |
| 183 |
return; |
| 184 |
autoNotebook notebook = Notebook_createFromFile (notebookFile); |
| 185 |
Notebook_rememberDuringThisAppSession_move (notebook.move()); |
| 186 |
my notebookReference = Notebook_find (MelderFile_peekPath (notebookFile)); |
| 187 |
my notebookReference -> trusted |= fullTrust; |
| 188 |
} |
| 189 |
|
| 190 |
static bool Melder_scriptTextIsNotebookText (conststring32 text) { |
| 191 |
/* |
| 192 |
Notebooks start with a opening double quote. |
| 193 |
Alternatively, notebooks tend to contain opening and closing braces |
| 194 |
in the first position of some line, |
| 195 |
but that cue is not reliable, |
| 196 |
because some notebooks contain no code chunks at all. |
| 197 |
*/ |
| 198 |
return text [0] == U'"'; |
| 199 |
} |
| 200 |
|
| 201 |
void Melder_includeIncludeFiles (autostring32 *inout_text, bool onlyInCodeChunks) { |
| 202 |
for (int depth = 0; ; depth ++) { |
| 203 |
char32 *head = inout_text->get(); |
| 204 |
integer numberOfIncludes = 0; |
| 205 |
if (depth > 10) |
| 206 |
Melder_throw (U"Include files nested too deep. Probably cyclic."); |
| 207 |
for (;;) { |
| 208 |
char32 *includeLocation, *includeFileName, *tail; |
| 209 |
/* |
| 210 |
Look for an include statement. If not found, we have finished. |
| 211 |
*/ |
| 212 |
includeLocation = ( str32nequ (head, U"include ", 8) ? head : str32str (head, U"\ninclude ") ); |
| 213 |
if (! includeLocation) |
| 214 |
break; |
| 215 |
if (includeLocation != head) |
| 216 |
includeLocation += 1; |
| 217 |
if (onlyInCodeChunks) { |
| 218 |
if (includeLocation == head) |
| 219 |
continue; // if the text starts with "include", it cannot be within a code chunk |
| 220 |
integer braceDepth = 0; |
| 221 |
for (const char32 *p = head; p != includeLocation; p ++) |
| 222 |
if (Melder_isEndOfLine (*p)) { |
| 223 |
if (p [1] == U'{') { |
| 224 |
if (braceDepth > 0) |
| 225 |
Melder_throw (U"Opening brace within a code chunk. Don't know whether or not to include an include file."); |
| 226 |
braceDepth += 1; |
| 227 |
} else if (p [1] == U'}') { |
| 228 |
if (braceDepth <= 0) |
| 229 |
Melder_throw (U"Closing brace outside a code chunk. Don't know whether or not to include an include file."); |
| 230 |
braceDepth -= 1; |
| 231 |
} |
| 232 |
} |
| 233 |
if (braceDepth == 0) { |
| 234 |
head = includeLocation + 8; |
| 235 |
continue; |
| 236 |
} |
| 237 |
} |
| 238 |
numberOfIncludes += 1; |
| 239 |
/* |
| 240 |
Separate out the head. |
| 241 |
*/ |
| 242 |
*includeLocation = U'\0'; |
| 243 |
/* |
| 244 |
Separate out the name of the include file. |
| 245 |
*/ |
| 246 |
includeFileName = includeLocation + 8; |
| 247 |
Melder_skipHorizontalSpace (& includeFileName); |
| 248 |
tail = includeFileName; |
| 249 |
while (Melder_staysWithinLine (*tail)) |
| 250 |
tail ++; |
| 251 |
if (*tail != U'\0') { |
| 252 |
*tail = U'\0'; |
| 253 |
tail += 1; |
| 254 |
} |
| 255 |
/* |
| 256 |
Get the contents of the include file. |
| 257 |
*/ |
| 258 |
structMelderFile includeFile { }; |
| 259 |
Melder_relativePathToFile (includeFileName, & includeFile); |
| 260 |
autostring32 includeText; |
| 261 |
try { |
| 262 |
includeText = MelderFile_readText (& includeFile); |
| 263 |
} catch (MelderError) { |
| 264 |
Melder_throw (U"Include file ", & includeFile, U" not read."); |
| 265 |
} |
| 266 |
/* |
| 267 |
Construct the new text. |
| 268 |
*/ |
| 269 |
const integer headLength = (head - inout_text->get()) + Melder_length (head); |
| 270 |
const integer includeTextLength = Melder_length (includeText.get()); |
| 271 |
const integer newLength = headLength + includeTextLength + 1 + Melder_length (tail); |
| 272 |
autostring32 newText (newLength); |
| 273 |
str32cpy (newText.get(), inout_text->get()); |
| 274 |
str32cpy (newText.get() + headLength, includeText.get()); |
| 275 |
str32cpy (newText.get() + headLength + includeTextLength, U"\n"); |
| 276 |
str32cpy (newText.get() + headLength + includeTextLength + 1, tail); |
| 277 |
/* |
| 278 |
Replace the old text with the new. This will work even within an autostring. |
| 279 |
*/ |
| 280 |
*inout_text = newText.move(); |
| 281 |
/* |
| 282 |
Cycle. |
| 283 |
*/ |
| 284 |
head = inout_text->get() + headLength + includeTextLength + 1; |
| 285 |
} |
| 286 |
if (numberOfIncludes == 0) |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
static bool parameterMatchesLabel (conststring32 parameter, conststring32 label) { |
| 292 |
/* |
| 293 |
A parameter matches a label if they are equal or if |
| 294 |
the label has an additional opening parenthesis, optionally preceded by a space. |
| 295 |
For example, the parameter "Pitch ceiling" matches the label "Pitch ceiling (Hz)". |
| 296 |
*/ |
| 297 |
for (; *parameter != U'\0' && *label != U'\0'; parameter ++, label ++) |
| 298 |
if (*parameter != *label) |
| 299 |
return false; |
| 300 |
if (*parameter == U'\0') |
| 301 |
if (*label == U'\0' || *label == U'(' || *label == U' ' && label [1] == U'(') |
| 302 |
return true; |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
integer Interpreter_readParameters (Interpreter me, mutablestring32 text) { |
| 307 |
const bool scriptTextIsNotebookText = Melder_scriptTextIsNotebookText (text); |
| 308 |
char32 *formLocation = nullptr; |
| 309 |
integer npar = 0; |
| 310 |
my dialogTitle. reset(); |
| 311 |
autoMelderString string; |
| 312 |
/* |
| 313 |
Look for a "form" line. |
| 314 |
*/ |
| 315 |
{// scope |
| 316 |
integer braceDepth = 0; |
| 317 |
char32 *p = & text [0]; |
| 318 |
for (;;) { |
| 319 |
/* |
| 320 |
Check invariant here: we are at the beginning of a line. |
| 321 |
*/ |
| 322 |
Melder_assert (p == text || Melder_isEndOfLine (p [-1])); |
| 323 |
|
| 324 |
if (scriptTextIsNotebookText) { |
| 325 |
if (*p == U'{') { |
| 326 |
if (braceDepth > 0) |
| 327 |
Melder_throw (U"Opening brace within a code chunk. Don't know how to look for a `form`."); |
| 328 |
braceDepth += 1; |
| 329 |
} else if (*p == U'}') { |
| 330 |
if (braceDepth <= 0) |
| 331 |
Melder_throw (U"Closing brace outside a code chunk. Don't know how to look for a `form`."); |
| 332 |
braceDepth -= 1; |
| 333 |
} |
| 334 |
if (braceDepth > 0) { |
| 335 |
Melder_skipHorizontalSpace (& p); |
| 336 |
if (str32nequ (p, U"form", 4) && (p [4] == U':' || Melder_isEndOfInk (p [4]))) { |
| 337 |
formLocation = p; |
| 338 |
break; |
| 339 |
} |
| 340 |
} |
| 341 |
} else { |
| 342 |
Melder_skipHorizontalSpace (& p); |
| 343 |
if (str32nequ (p, U"form", 4) && (p [4] == U':' || Melder_isEndOfInk (p [4]))) { |
| 344 |
formLocation = p; |
| 345 |
break; |
| 346 |
} |
| 347 |
} |
| 348 |
Melder_skipToEndOfLine (& p); |
| 349 |
if (*p == U'\0') |
| 350 |
break; |
| 351 |
p ++; // skip newline symbol |
| 352 |
} |
| 353 |
} |
| 354 |
/* |
| 355 |
If there is no "form" line, there are no parameters. |
| 356 |
*/ |
| 357 |
if (formLocation) { |
| 358 |
char32 *dialogTitle = Melder_findEndOfHorizontalSpace |
| 359 |
(formLocation [4] == U':' ? formLocation + 5 : formLocation + 4); |
| 360 |
char32 *endOfLine = Melder_findEndOfLine (dialogTitle); |
| 361 |
if (*endOfLine == U'\0') |
| 362 |
Melder_throw (U"Unfinished form (only a “form” line)."); |
| 363 |
if (formLocation [4] == U':') { |
| 364 |
MelderString_empty (& string); |
| 365 |
if (dialogTitle [0] != U'"') |
| 366 |
Melder_throw (U"The title of a form should start with a double quote character (\")."); |
| 367 |
char32* p = dialogTitle + 1; // BUG: could be const (change Melder_skipHorizontalSpace()) |
| 368 |
for (; p < endOfLine; p ++) { |
| 369 |
if (*p == U'"') { |
| 370 |
if (p [1] == U'"') { |
| 371 |
p ++; |
| 372 |
continue; |
| 373 |
} else |
| 374 |
break; |
| 375 |
} |
| 376 |
MelderString_appendCharacter (& string, *p); |
| 377 |
} |
| 378 |
if (*p != U'"') |
| 379 |
Melder_throw (U"Missing closing quote character in the title of the form."); |
| 380 |
p ++; // skip closing quote |
| 381 |
Melder_skipHorizontalSpace (& p); |
| 382 |
if (! Melder_isEndOfLine (*p) && *p != U';') |
| 383 |
Melder_throw (U"Stray characters after closing quote in form title."); |
| 384 |
my dialogTitle = Melder_dup (string.string); |
| 385 |
} else { |
| 386 |
my dialogTitle = Melder_ndup (dialogTitle, endOfLine - dialogTitle); |
| 387 |
} |
| 388 |
my numberOfParameters = 0; |
| 389 |
while (true) { |
| 390 |
int type = 0; |
| 391 |
char32 *startOfLine = Melder_findEndOfHorizontalSpace (endOfLine + 1); |
| 392 |
/* |
| 393 |
Skip empty lines and lines with comments. |
| 394 |
*/ |
| 395 |
while (*startOfLine == U'#' || *startOfLine == U';' || *startOfLine == U'!' || Melder_isEndOfLine (*startOfLine)) { |
| 396 |
endOfLine = Melder_findEndOfLine (startOfLine); |
| 397 |
if (Melder_isEndOfText (*endOfLine)) |
| 398 |
Melder_throw (U"Unfinished form (missing “endform”)."); |
| 399 |
startOfLine = Melder_findEndOfHorizontalSpace (endOfLine + 1); |
| 400 |
} |
| 401 |
endOfLine = Melder_findEndOfLine (startOfLine); |
| 402 |
/* |
| 403 |
Break on endform. |
| 404 |
*/ |
| 405 |
if (str32nequ (startOfLine, U"endform", 7) && Melder_isEndOfInk (startOfLine [7])) |
| 406 |
break; |
| 407 |
/* |
| 408 |
Recognize parameter name, argument value, |
| 409 |
and perhaps format name and/or number of lines. |
| 410 |
*/ |
| 411 |
char32 *p = nullptr; |
| 412 |
bool hasColon = false; |
| 413 |
auto isEndOfInkOrColon = [] (const char32 kar) { return kar == U':' || Melder_isEndOfInk (kar); }; |
| 414 |
if (str32nequ (startOfLine, U"word", 4) && isEndOfInkOrColon (startOfLine [4])) { |
| 415 |
type = Interpreter_WORD; |
| 416 |
hasColon = ( startOfLine [4] == U':' ); |
| 417 |
p = startOfLine + 4 + hasColon; |
| 418 |
} else if (str32nequ (startOfLine, U"sentence", 8) && isEndOfInkOrColon (startOfLine [8])) { |
| 419 |
type = Interpreter_SENTENCE; |
| 420 |
hasColon = ( startOfLine [8] == U':' ); |
| 421 |
p = startOfLine + 8 + hasColon; |
| 422 |
} else if (str32nequ (startOfLine, U"infile", 6) && isEndOfInkOrColon (startOfLine [6])) { |
| 423 |
type = Interpreter_INFILE; |
| 424 |
hasColon = ( startOfLine [6] == U':' ); |
| 425 |
p = startOfLine + 6 + hasColon; |
| 426 |
} else if (str32nequ (startOfLine, U"outfile", 7) && isEndOfInkOrColon (startOfLine [7])) { |
| 427 |
type = Interpreter_OUTFILE; |
| 428 |
hasColon = ( startOfLine [7] == U':' ); |
| 429 |
p = startOfLine + 7 + hasColon; |
| 430 |
} else if (str32nequ (startOfLine, U"folder", 6) && isEndOfInkOrColon (startOfLine [6])) { |
| 431 |
type = Interpreter_FOLDER; |
| 432 |
hasColon = ( startOfLine [6] == U':' ); |
| 433 |
p = startOfLine + 6 + hasColon; |
| 434 |
} else if (str32nequ (startOfLine, U"text", 4) && isEndOfInkOrColon (startOfLine [4])) { |
| 435 |
type = Interpreter_TEXT; |
| 436 |
hasColon = ( startOfLine [4] == U':' ); |
| 437 |
p = startOfLine + 4 + hasColon; |
| 438 |
} else if (str32nequ (startOfLine, U"real", 4) && isEndOfInkOrColon (startOfLine [4])) { |
| 439 |
type = Interpreter_REAL; |
| 440 |
hasColon = ( startOfLine [4] == U':' ); |
| 441 |
p = startOfLine + 4 + hasColon; |
| 442 |
} else if (str32nequ (startOfLine, U"nonnegative", 11) && isEndOfInkOrColon (startOfLine [11])) { |
| 443 |
type = Interpreter_NONNEGATIVE; |
| 444 |
hasColon = ( startOfLine [11] == U':' ); |
| 445 |
p = startOfLine + 11 + hasColon; |
| 446 |
} else if (str32nequ (startOfLine, U"positive", 8) && isEndOfInkOrColon (startOfLine [8])) { |
| 447 |
type = Interpreter_POSITIVE; |
| 448 |
hasColon = ( startOfLine [8] == U':' ); |
| 449 |
p = startOfLine + 8 + hasColon; |
| 450 |
} else if (str32nequ (startOfLine, U"integer", 7) && isEndOfInkOrColon (startOfLine [7])) { |
| 451 |
type = Interpreter_INTEGER; |
| 452 |
hasColon = ( startOfLine [7] == U':' ); |
| 453 |
p = startOfLine + 7 + hasColon; |
| 454 |
} else if (str32nequ (startOfLine, U"natural0", 8) && isEndOfInkOrColon (startOfLine [8])) { |
| 455 |
type = Interpreter_NATURAL0; |
| 456 |
hasColon = ( startOfLine [8] == U':' ); |
| 457 |
p = startOfLine + 8 + hasColon; |
| 458 |
} else if (str32nequ (startOfLine, U"natural1", 8) && isEndOfInkOrColon (startOfLine [8])) { |
| 459 |
type = Interpreter_NATURAL1; |
| 460 |
hasColon = ( startOfLine [8] == U':' ); |
| 461 |
p = startOfLine + 8 + hasColon; |
| 462 |
} else if (str32nequ (startOfLine, U"natural", 7) && isEndOfInkOrColon (startOfLine [7])) { |
| 463 |
type = Interpreter_NATURAL1; |
| 464 |
hasColon = ( startOfLine [7] == U':' ); |
| 465 |
p = startOfLine + 7 + hasColon; |
| 466 |
} else if (str32nequ (startOfLine, U"boolean", 7) && isEndOfInkOrColon (startOfLine [7])) { |
| 467 |
type = Interpreter_BOOLEAN; |
| 468 |
hasColon = ( startOfLine [7] == U':' ); |
| 469 |
p = startOfLine + 7 + hasColon; |
| 470 |
} else if (str32nequ (startOfLine, U"realvector", 10) && isEndOfInkOrColon (startOfLine [10])) { |
| 471 |
type = Interpreter_REALVECTOR; |
| 472 |
hasColon = ( startOfLine [10] == U':' ); |
| 473 |
p = startOfLine + 10 + hasColon; |
| 474 |
} else if (str32nequ (startOfLine, U"nonnegativevector", 17) && isEndOfInkOrColon (startOfLine [17])) { |
| 475 |
type = Interpreter_NONNEGATIVEVECTOR; |
| 476 |
hasColon = ( startOfLine [17] == U':' ); |
| 477 |
p = startOfLine + 17 + hasColon; |
| 478 |
} else if (str32nequ (startOfLine, U"positivevector", 14) && isEndOfInkOrColon (startOfLine [14])) { |
| 479 |
type = Interpreter_POSITIVEVECTOR; |
| 480 |
hasColon = ( startOfLine [14] == U':' ); |
| 481 |
p = startOfLine + 14 + hasColon; |
| 482 |
} else if (str32nequ (startOfLine, U"integervector", 13) && isEndOfInkOrColon (startOfLine [13])) { |
| 483 |
type = Interpreter_INTEGERVECTOR; |
| 484 |
hasColon = ( startOfLine [13] == U':' ); |
| 485 |
p = startOfLine + 13 + hasColon; |
| 486 |
} else if (str32nequ (startOfLine, U"natural0vector", 14) && isEndOfInkOrColon (startOfLine [14])) { |
| 487 |
type = Interpreter_NATURAL1VECTOR; |
| 488 |
hasColon = ( startOfLine [14] == U':' ); |
| 489 |
p = startOfLine + 14 + hasColon; |
| 490 |
} else if (str32nequ (startOfLine, U"natural1vector", 14) && isEndOfInkOrColon (startOfLine [14])) { |
| 491 |
type = Interpreter_NATURAL1VECTOR; |
| 492 |
hasColon = ( startOfLine [14] == U':' ); |
| 493 |
p = startOfLine + 14 + hasColon; |
| 494 |
} else if (str32nequ (startOfLine, U"naturalvector", 13) && isEndOfInkOrColon (startOfLine [13])) { |
| 495 |
type = Interpreter_NATURAL1VECTOR; |
| 496 |
hasColon = ( startOfLine [13] == U':' ); |
| 497 |
p = startOfLine + 13 + hasColon; |
| 498 |
} else if (str32nequ (startOfLine, U"realmatrix", 10) && isEndOfInkOrColon (startOfLine [10])) { |
| 499 |
type = Interpreter_REALMATRIX; |
| 500 |
hasColon = ( startOfLine [10] == U':' ); |
| 501 |
p = startOfLine + 10 + hasColon; |
| 502 |
} else if (str32nequ (startOfLine, U"choice", 6) && isEndOfInkOrColon (startOfLine [6])) { |
| 503 |
type = Interpreter_CHOICE; |
| 504 |
hasColon = ( startOfLine [6] == U':' ); |
| 505 |
p = startOfLine + 6 + hasColon; |
| 506 |
} else if (str32nequ (startOfLine, U"optionmenu", 10) && isEndOfInkOrColon (startOfLine [10])) { |
| 507 |
type = Interpreter_OPTIONMENU; |
| 508 |
hasColon = ( startOfLine [10] == U':' ); |
| 509 |
p = startOfLine + 10 + hasColon; |
| 510 |
} else if (str32nequ (startOfLine, U"button", 6) && isEndOfInkOrColon (startOfLine [6])) { |
| 511 |
type = Interpreter_BUTTON; |
| 512 |
hasColon = ( startOfLine [6] == U':' ); |
| 513 |
p = startOfLine + 6 + hasColon; |
| 514 |
} else if (str32nequ (startOfLine, U"option", 6) && isEndOfInkOrColon (startOfLine [6])) { |
| 515 |
type = Interpreter_OPTION; |
| 516 |
hasColon = ( startOfLine [6] == U':' ); |
| 517 |
p = startOfLine + 6 + hasColon; |
| 518 |
} else if (str32nequ (startOfLine, U"comment", 7) && isEndOfInkOrColon (startOfLine [7])) { |
| 519 |
type = Interpreter_COMMENT; |
| 520 |
hasColon = ( startOfLine [7] == U':' ); |
| 521 |
p = startOfLine + 7 + hasColon; |
| 522 |
} else { |
| 523 |
*endOfLine = U'\0'; // destroy input in order to limit printing of parameter type |
| 524 |
Melder_throw (U"Unknown parameter type inside form:\n“", startOfLine, U"”."); |
| 525 |
} |
| 526 |
/* |
| 527 |
Example: |
| 528 |
form: "Something" |
| 529 |
real: "Time (s)", "3.14 (= pi)" |
| 530 |
choice: "Colour", 2 |
| 531 |
button: "Red" |
| 532 |
button: "Green" |
| 533 |
button: "Blue" |
| 534 |
endform |
| 535 |
my parameters [1] := "Time (s)" |
| 536 |
my parameters [2] := "Colour" |
| 537 |
my parameters [3] := "" |
| 538 |
my parameters [4] := "" |
| 539 |
my parameters [5] := "" |
| 540 |
my arguments [1] := "3.14 (= pi)" |
| 541 |
my arguments [2] := "2" |
| 542 |
my arguments [3] := "Red" (funny, but needed in Interpreter_getArgumentsFromString) |
| 543 |
my arguments [4] := "Green" |
| 544 |
my arguments [5] := "Blue" |
| 545 |
(last checked 2023-01-21) |
| 546 |
*/ |
| 547 |
Melder_assert (p); |
| 548 |
const mutablestring32 parameter = my parameters [++ my numberOfParameters]; |
| 549 |
if (type <= Interpreter_MAXIMUM_TYPE_WITH_VARIABLE_NAME) { |
| 550 |
Melder_skipHorizontalSpace (& p); |
| 551 |
if (Melder_isEndOfLine (*p)) { |
| 552 |
*p = U'\0'; // destroy input in order to limit printing of line |
| 553 |
Melder_throw (U"Missing parameter:\n“", startOfLine, U"”."); |
| 554 |
} |
| 555 |
MelderString_empty (& string); |
| 556 |
my numbersOfLines [my numberOfParameters] = 0; |
| 557 |
if (hasColon) { |
| 558 |
/* |
| 559 |
Parse optional number-of-lines (including final comma). |
| 560 |
*/ |
| 561 |
if (Melder_isAsciiDecimalNumber (*p)) { |
| 562 |
if (!( |
| 563 |
type == Interpreter_TEXT || |
| 564 |
type == Interpreter_INFILE || type == Interpreter_OUTFILE || type == Interpreter_FOLDER || |
| 565 |
type >= Interpreter_MINIMUM_TYPE_FOR_NUMERIC_VECTOR_VARIABLE && |
| 566 |
type <= Interpreter_MAXIMUM_TYPE_FOR_NUMERIC_VECTOR_VARIABLE |
| 567 |
)) { |
| 568 |
*(p + 1) = U'\0'; |
| 569 |
Melder_throw (U"Only “text”, “infile”, “outfile”, “folder” and vector and matrix types " |
| 570 |
"support setting the number of lines in a form."); |
| 571 |
} |
| 572 |
my numbersOfLines [my numberOfParameters] = Melder_atoi (p); |
| 573 |
p ++; |
| 574 |
while (Melder_isAsciiDecimalNumber (*p)) |
| 575 |
p ++; |
| 576 |
Melder_skipHorizontalSpace (& p); |
| 577 |
if (*p != U',') { |
| 578 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 579 |
Melder_throw (U"Missing comma after number of lines:\n", startOfLine); |
| 580 |
} |
| 581 |
p ++; // skip comma |
| 582 |
Melder_skipHorizontalSpace (& p); |
| 583 |
} |
| 584 |
/* |
| 585 |
Parse obligatory variable name (including final comma). |
| 586 |
*/ |
| 587 |
if (*p != U'"') { |
| 588 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 589 |
Melder_throw (U"Missing opening quote for parameter name in form field:\n", startOfLine); |
| 590 |
} |
| 591 |
p ++; // skip opening quote |
| 592 |
for (; p < endOfLine; p ++) { |
| 593 |
if (*p == U'"') { |
| 594 |
if (p [1] == U'"') { // look ahead |
| 595 |
p ++; |
| 596 |
continue; |
| 597 |
} else |
| 598 |
break; |
| 599 |
} |
| 600 |
MelderString_appendCharacter (& string, *p); |
| 601 |
} |
| 602 |
if (*p != U'"') { |
| 603 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 604 |
Melder_throw (U"Missing closing quote character in parameter name:\n", startOfLine); |
| 605 |
} |
| 606 |
p ++; // skip closing quote |
| 607 |
Melder_skipHorizontalSpace (& p); |
| 608 |
if (*p != U',') { |
| 609 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 610 |
Melder_throw (U"Missing comma after parameter name:\n", startOfLine); |
| 611 |
} |
| 612 |
p ++; // skip comma |
| 613 |
} else { |
| 614 |
while (Melder_staysWithinInk (*p)) |
| 615 |
MelderString_appendCharacter (& string, *(p ++)); |
| 616 |
} |
| 617 |
MelderString_truncate (& string, Interpreter_MAX_PARAMETER_LENGTH); |
| 618 |
str32cpy (parameter, string.string); |
| 619 |
parameter [string.length] = U'\0'; |
| 620 |
npar ++; |
| 621 |
} else { |
| 622 |
parameter [0] = U'\0'; |
| 623 |
} |
| 624 |
Melder_skipHorizontalSpace (& p); |
| 625 |
/* |
| 626 |
Parse format type. |
| 627 |
*/ |
| 628 |
if (type >= Interpreter_MINIMUM_TYPE_FOR_NUMERIC_VECTOR_VARIABLE && type <= Interpreter_MAXIMUM_TYPE_FOR_NUMERIC_VECTOR_VARIABLE) { |
| 629 |
if (! hasColon) { |
| 630 |
*p = U'\0'; |
| 631 |
Melder_throw (U"Vector fields in forms are available only if immediately followed by a colon:\n", startOfLine); |
| 632 |
} |
| 633 |
MelderString_empty (& string); |
| 634 |
if (Melder_isEndOfLine (*p)) { |
| 635 |
*p = U'\0'; // destroy input in order to limit printing of line |
| 636 |
Melder_throw (U"Missing format:\n“", startOfLine, U"”."); |
| 637 |
} |
| 638 |
if (*p != U'"') { |
| 639 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 640 |
Melder_throw (U"Missing opening quote for vector format:\n", startOfLine); |
| 641 |
} |
| 642 |
p ++; // skip opening quote |
| 643 |
for (; p < endOfLine; p ++) { |
| 644 |
if (*p == U'"') { |
| 645 |
if (p [1] == U'"') { // look ahead |
| 646 |
p ++; |
| 647 |
continue; |
| 648 |
} else |
| 649 |
break; |
| 650 |
} |
| 651 |
MelderString_appendCharacter (& string, *p); |
| 652 |
} |
| 653 |
if (*p != U'"') { |
| 654 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 655 |
Melder_throw (U"Missing closing quote for vector format:\n", startOfLine); |
| 656 |
} |
| 657 |
p ++; // skip closing quote |
| 658 |
Melder_skipHorizontalSpace (& p); |
| 659 |
if (*p != U',') { |
| 660 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 661 |
Melder_throw (U"Missing comma after vector format:\n", startOfLine); |
| 662 |
} |
| 663 |
p ++; // skip comma |
| 664 |
Melder_skipHorizontalSpace (& p); |
| 665 |
/* |
| 666 |
Install format type. |
| 667 |
*/ |
| 668 |
MelderString_truncate (& string, Interpreter_MAX_FORMAT_LENGTH); |
| 669 |
str32cpy (my formats [my numberOfParameters], string.string); |
| 670 |
my formats [my numberOfParameters] [string.length] = U'\0'; |
| 671 |
} |
| 672 |
/* |
| 673 |
Parse obligatory content of argument. |
| 674 |
*/ |
| 675 |
if (hasColon) { |
| 676 |
MelderString_empty (& string); |
| 677 |
if (Melder_isAsciiDecimalNumber (*p)) { |
| 678 |
if (type != Interpreter_CHOICE && type != Interpreter_OPTIONMENU && type != Interpreter_BOOLEAN) { |
| 679 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 680 |
Melder_throw (U"Only “choice”, “optionmenu” and “boolean” fields can take a number:\n", startOfLine); |
| 681 |
} |
| 682 |
const integer value = Melder_atoi (p); |
| 683 |
if (type == Interpreter_CHOICE || type == Interpreter_OPTIONMENU) |
| 684 |
MelderString_append (& string, value); |
| 685 |
else |
| 686 |
MelderString_appendCharacter (& string, value != 0 ? U'1' : U'0'); |
| 687 |
p ++; // skip first digit |
| 688 |
while (Melder_isAsciiDecimalNumber (*p)) |
| 689 |
p ++; // skip next digit |
| 690 |
} else { |
| 691 |
if (*p != U'"') { |
| 692 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 693 |
Melder_throw (U"Missing opening quote after comma in form field:\n", startOfLine); |
| 694 |
} |
| 695 |
if (type == Interpreter_CHOICE || type == Interpreter_OPTIONMENU) { |
| 696 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 697 |
Melder_throw (U"A “choice” field can take only a number, not a string:\n", startOfLine); |
| 698 |
} |
| 699 |
p ++; // skip opening quote |
| 700 |
for (; p < endOfLine; p ++) { |
| 701 |
if (*p == U'"') { |
| 702 |
if (p [1] == U'"') { |
| 703 |
p ++; |
| 704 |
continue; |
| 705 |
} else |
| 706 |
break; |
| 707 |
} |
| 708 |
MelderString_appendCharacter (& string, *p); |
| 709 |
} |
| 710 |
if (*p != U'"') { |
| 711 |
*(p + 1) = U'\0'; // destroy input in order to limit printing of line |
| 712 |
Melder_throw (U"Missing closing quote character in default argument:\n", startOfLine); |
| 713 |
} |
| 714 |
p ++; // skip closing quote |
| 715 |
} |
| 716 |
Melder_skipHorizontalSpace (& p); |
| 717 |
if (! Melder_isEndOfLine (*p) && *p != U';') |
| 718 |
Melder_throw (U"Stray characters after default argument in form title:\n", startOfLine); |
| 719 |
my arguments [my numberOfParameters] = Melder_dup_f (string.string); |
| 720 |
} else { |
| 721 |
my arguments [my numberOfParameters] = Melder_ndup (p, endOfLine - p); |
| 722 |
} |
| 723 |
my types [my numberOfParameters] = type; |
| 724 |
if (Melder_isEndOfText (*endOfLine)) |
| 725 |
Melder_throw (U"Unfinished form (missing “endform”)."); |
| 726 |
} |
| 727 |
} else { |
| 728 |
npar = my numberOfParameters = 0; |
| 729 |
} |
| 730 |
return npar; |
| 731 |
} |
| 732 |
|
| 733 |
static void tidyUpParameterNames (Interpreter me, integer size) { |
| 734 |
for (integer ipar = 1; ipar <= size; ipar ++) { |
| 735 |
mutablestring32 p = my parameters [ipar]; |
| 736 |
/* |
| 737 |
Ignore buttons and comments again (after `size` was |
| 738 |
made smaller than my numberOfParameters). |
| 739 |
*/ |
| 740 |
if (! *p) |
| 741 |
continue; |
| 742 |
/* |
| 743 |
Change spaces to underscores. |
| 744 |
This has to come before stripping. |
| 745 |
*/ |
| 746 |
for (p = my parameters [ipar]; *p != U'\0'; p ++) |
| 747 |
if (Melder_isHorizontalSpace (*p)) |
| 748 |
*p = U'_'; |
| 749 |
/* |
| 750 |
Strip parentheses off parameter name. |
| 751 |
*/ |
| 752 |
p = my parameters [ipar]; |
| 753 |
if ((p = str32chr (p, U'(')) != nullptr) { |
| 754 |
*p = U'\0'; |
| 755 |
if (p - my parameters [ipar] > 0 && p [-1] == U'_') |
| 756 |
p [-1] = U'\0'; |
| 757 |
} |
| 758 |
/* |
| 759 |
Strip colon off parameter name. |
| 760 |
*/ |
| 761 |
p = my parameters [ipar]; |
| 762 |
if (*p != U'\0' && p [Melder_length (p) - 1] == U':') |
| 763 |
p [Melder_length (p) - 1] = U'\0'; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
autoUiForm Interpreter_createForm (Interpreter me, GuiWindow parent, Editor optionalEditor, conststring32 path, |
| 768 |
void (*okCallback) (UiForm, integer, Stackel, conststring32, Interpreter, conststring32, bool, void *, Editor), void *okClosure, |
| 769 |
bool selectionOnly) |
| 770 |
{ |
| 771 |
autoUiForm form = UiForm_create (parent, optionalEditor, |
| 772 |
Melder_cat (selectionOnly ? U"Run script (selection only): " : U"Run script: ", my dialogTitle.get()), |
| 773 |
okCallback, okClosure, nullptr, nullptr |
| 774 |
); |
| 775 |
if (path) |
| 776 |
form -> scriptFilePath = Melder_dup (path); |
| 777 |
for (int ipar = 1; ipar <= my numberOfParameters; ipar ++) { |
| 778 |
/* |
| 779 |
Convert underscores to spaces. |
| 780 |
*/ |
| 781 |
char32 parameter [100], *p = & parameter [0]; |
| 782 |
str32cpy (parameter, my parameters [ipar]); |
| 783 |
while (*p) { |
| 784 |
if (*p == U'_') |
| 785 |
*p = U' '; |
| 786 |
p ++; |
| 787 |
} |
| 788 |
switch (my types [ipar]) { |
| 789 |
case Interpreter_WORD: { |
| 790 |
UiForm_addWord (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 791 |
} break; case Interpreter_SENTENCE: { |
| 792 |
UiForm_addSentence (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 793 |
} break; case Interpreter_TEXT: { |
| 794 |
if (my numbersOfLines [ipar] == 0) |
| 795 |
UiForm_addText (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 796 |
else |
| 797 |
UiForm_addText (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get(), |
| 798 |
my numbersOfLines [ipar]); |
| 799 |
} break; case Interpreter_INFILE: { |
| 800 |
if (my numbersOfLines [ipar] == 0) |
| 801 |
UiForm_addInfile (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 802 |
else |
| 803 |
UiForm_addInfile (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get(), |
| 804 |
my numbersOfLines [ipar]); |
| 805 |
} break; case Interpreter_OUTFILE: { |
| 806 |
if (my numbersOfLines [ipar] == 0) |
| 807 |
UiForm_addOutfile (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 808 |
else |
| 809 |
UiForm_addOutfile (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get(), |
| 810 |
my numbersOfLines [ipar]); |
| 811 |
} break; case Interpreter_FOLDER: { |
| 812 |
if (my numbersOfLines [ipar] == 0) |
| 813 |
UiForm_addFolder (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 814 |
else |
| 815 |
UiForm_addFolder (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get(), |
| 816 |
my numbersOfLines [ipar]); |
| 817 |
} break; case Interpreter_REAL: { |
| 818 |
UiForm_addReal (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); // TODO: an address of a real variable |
| 819 |
} break; case Interpreter_NONNEGATIVE: { |
| 820 |
UiForm_addNonnegative (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 821 |
} break; case Interpreter_POSITIVE: { |
| 822 |
UiForm_addPositive (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 823 |
} break; case Interpreter_INTEGER: { |
| 824 |
UiForm_addInteger (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 825 |
} break; case Interpreter_NATURAL0: { |
| 826 |
UiForm_addNatural0 (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 827 |
} break; case Interpreter_NATURAL1: { |
| 828 |
UiForm_addNatural1 (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 829 |
} break; case Interpreter_BOOLEAN: { |
| 830 |
mutablestring32 arg = & my arguments [ipar] [0]; |
| 831 |
if ( |
| 832 |
str32equ (arg, U"1") || |
| 833 |
str32equ (arg, U"yes") || str32equ (arg, U"on") || |
| 834 |
str32equ (arg, U"Yes") || str32equ (arg, U"On") || |
| 835 |
str32equ (arg, U"YES") || str32equ (arg, U"ON") |
| 836 |
) { |
| 837 |
str32cpy (arg, U"1"); |
| 838 |
} else if ( |
| 839 |
str32equ (arg, U"0") || |
| 840 |
str32equ (arg, U"no") || str32equ (arg, U"off") || |
| 841 |
str32equ (arg, U"No") || str32equ (arg, U"Off") || |
| 842 |
str32equ (arg, U"NO") || str32equ (arg, U"OFF") |
| 843 |
) { |
| 844 |
str32cpy (arg, U"0"); |
| 845 |
} else { |
| 846 |
Melder_throw (U"Unknown value “", arg, U"” for boolean “", my parameters [ipar], U"”."); |
| 847 |
} |
| 848 |
UiForm_addBoolean (form.get(), nullptr, nullptr, parameter, my arguments [ipar] [0] == U'1'); |
| 849 |
} break; case Interpreter_REALVECTOR: { |
| 850 |
kUi_realVectorFormat format = kUi_realVectorFormat_getValue (my formats [ipar]); |
| 851 |
if (format == kUi_realVectorFormat::UNDEFINED) |
| 852 |
Melder_throw (U"Undefined real vector format “", my formats [ipar], U"”."); |
| 853 |
if (my numbersOfLines [ipar] == 0) |
| 854 |
UiForm_addRealVector (form.get(), nullptr, nullptr, parameter, format, my arguments [ipar].get()); |
| 855 |
else |
| 856 |
UiForm_addRealVector (form.get(), nullptr, nullptr, parameter, format, my arguments [ipar].get(), |
| 857 |
my numbersOfLines [ipar]); |
| 858 |
} break; case Interpreter_NONNEGATIVEVECTOR: { |
| 859 |
kUi_realVectorFormat format = kUi_realVectorFormat_getValue (my formats [ipar]); |
| 860 |
if (format == kUi_realVectorFormat::UNDEFINED) |
| 861 |
Melder_throw (U"Undefined nonnegative vector format “", my formats [ipar], U"”."); |
| 862 |
if (my numbersOfLines [ipar] == 0) |
| 863 |
UiForm_addNonnegativeVector (form.get(), nullptr, nullptr, parameter, format, my arguments [ipar].get()); |
| 864 |
else |
| 865 |
UiForm_addNonnegativeVector (form.get(), nullptr, nullptr, parameter, format, my arguments [ipar].get(), |
| 866 |
my numbersOfLines [ipar]); |
| 867 |
} break; case Interpreter_POSITIVEVECTOR: { |
| 868 |
kUi_realVectorFormat format = kUi_realVectorFormat_getValue (my formats [ipar]); |
| 869 |
if (format == kUi_realVectorFormat::UNDEFINED) |
| 870 |
Melder_throw (U"Undefined positive vector format “", my formats [ipar], U"”."); |
| 871 |
if (my numbersOfLines [ipar] == 0) |
| 872 |
UiForm_addPositiveVector (form.get(), nullptr, nullptr, parameter, format, my arguments [ipar].get()); |
| 873 |
else |
| 874 |
UiForm_addPositiveVector (form.get(), nullptr, nullptr, parameter, format, my arguments [ipar].get(), |
| 875 |
my numbersOfLines [ipar]); |
| 876 |
} break; case Interpreter_INTEGERVECTOR: { |
| 877 |
kUi_integerVectorFormat format = kUi_integerVectorFormat_getValue (my formats [ipar]); |
| 878 |
if (format == kUi_integerVectorFormat::UNDEFINED) |
| 879 |
Melder_throw (U"Undefined integer vector format “", my formats [ipar], U"”."); |
| 880 |
if (my numbersOfLines [ipar] == 0) |
| 881 |
UiForm_addIntegerVector (form.get(), nullptr, nullptr, parameter, format, my arguments [ipar].get()); |
| 882 |
else |
| 883 |
UiForm_addIntegerVector (form.get(), nullptr, nullptr, parameter, format, my arguments [ipar].get(), |
| 884 |
my numbersOfLines [ipar]); |
| 885 |
} break; case Interpreter_NATURAL0VECTOR: { |
| 886 |
kUi_integerVectorFormat format = kUi_integerVectorFormat_getValue (my formats [ipar]); |
| 887 |
if (format == kUi_integerVectorFormat::UNDEFINED) |
| 888 |
Melder_throw (U"Undefined natural vector format “", my formats [ipar], U"”."); |
| 889 |
if (my numbersOfLines [ipar] == 0) |
| 890 |
UiForm_addNatural0Vector (form.get(), nullptr, nullptr, parameter, |
| 891 |
kUi_integerVectorFormat_getValue (my formats [ipar]), my arguments [ipar].get() |
| 892 |
); |
| 893 |
else |
| 894 |
UiForm_addNatural0Vector (form.get(), nullptr, nullptr, parameter, |
| 895 |
kUi_integerVectorFormat_getValue (my formats [ipar]), my arguments [ipar].get(), |
| 896 |
my numbersOfLines [ipar] |
| 897 |
); |
| 898 |
} break; case Interpreter_NATURAL1VECTOR: { |
| 899 |
kUi_integerVectorFormat format = kUi_integerVectorFormat_getValue (my formats [ipar]); |
| 900 |
if (format == kUi_integerVectorFormat::UNDEFINED) |
| 901 |
Melder_throw (U"Undefined natural vector format “", my formats [ipar], U"”."); |
| 902 |
if (my numbersOfLines [ipar] == 0) |
| 903 |
UiForm_addNatural1Vector (form.get(), nullptr, nullptr, parameter, |
| 904 |
kUi_integerVectorFormat_getValue (my formats [ipar]), my arguments [ipar].get() |
| 905 |
); |
| 906 |
else |
| 907 |
UiForm_addNatural1Vector (form.get(), nullptr, nullptr, parameter, |
| 908 |
kUi_integerVectorFormat_getValue (my formats [ipar]), my arguments [ipar].get(), |
| 909 |
my numbersOfLines [ipar] |
| 910 |
); |
| 911 |
} break; case Interpreter_REALMATRIX: { |
| 912 |
Melder_throw (U"Cannot handle matrices in forms yet."); // TODO |
| 913 |
// UiForm_addRealMatrix (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 914 |
} break; case Interpreter_CHOICE: { |
| 915 |
UiForm_addChoice (form.get(), nullptr, nullptr, nullptr, parameter, (int) Melder_atoi (my arguments [ipar].get()), 1); |
| 916 |
} break; case Interpreter_OPTIONMENU: { |
| 917 |
UiForm_addOptionMenu (form.get(), nullptr, nullptr, nullptr, parameter, (int) Melder_atoi (my arguments [ipar].get()), 1); |
| 918 |
} break; case Interpreter_BUTTON: { |
| 919 |
UiForm_addOption (form.get(), my arguments [ipar].get()); |
| 920 |
} break; case Interpreter_OPTION: { |
| 921 |
UiForm_addOption (form.get(), my arguments [ipar].get()); |
| 922 |
} break; case Interpreter_HEADING: { |
| 923 |
UiForm_addHeading (form.get(), nullptr, my arguments [ipar].get()); |
| 924 |
} break; case Interpreter_COMMENT: { |
| 925 |
UiForm_addComment (form.get(), nullptr, my arguments [ipar].get()); |
| 926 |
} break; case Interpreter_CAPTION: { |
| 927 |
UiForm_addCaption (form.get(), nullptr, my arguments [ipar].get()); |
| 928 |
} break; default: { |
| 929 |
UiForm_addWord (form.get(), nullptr, nullptr, parameter, my arguments [ipar].get()); |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
tidyUpParameterNames (me, my numberOfParameters); |
| 934 |
UiForm_finish (form.get()); |
| 935 |
return form; |
| 936 |
} |
| 937 |
|
| 938 |
void Interpreter_getArgumentsFromDialog (Interpreter me, UiForm dialog) { |
| 939 |
tidyUpParameterNames (me, my numberOfParameters); |
| 940 |
for (int ipar = 1; ipar <= my numberOfParameters; ipar ++) { |
| 941 |
char32 parameter [1+Interpreter_MAX_PARAMETER_LENGTH]; |
| 942 |
/* |
| 943 |
Convert underscores to spaces. |
| 944 |
*/ |
| 945 |
str32cpy (parameter, my parameters [ipar]); |
| 946 |
char32 *p = & parameter [0]; |
| 947 |
while (*p) { |
| 948 |
if (*p == U'_') |
| 949 |
*p = U' '; |
| 950 |
p ++; |
| 951 |
} |
| 952 |
switch (my types [ipar]) { |
| 953 |
case Interpreter_WORD: |
| 954 |
case Interpreter_SENTENCE: |
| 955 |
case Interpreter_TEXT: |
| 956 |
{ |
| 957 |
const conststring32 value = UiForm_getString (dialog, parameter); |
| 958 |
my arguments [ipar] = Melder_dup_f (value); |
| 959 |
break; |
| 960 |
} |
| 961 |
case Interpreter_INFILE: |
| 962 |
case Interpreter_OUTFILE: |
| 963 |
case Interpreter_FOLDER: |
| 964 |
{ |
| 965 |
const conststring32 value = UiForm_getString (dialog, parameter); |
| 966 |
structMelderFile file { }; |
| 967 |
Melder_relativePathToFile (value, & file); // the working directory should have been set to the script file path TODO: check |
| 968 |
my arguments [ipar] = Melder_dup_f (MelderFile_peekPath (& file)); |
| 969 |
break; |
| 970 |
} |
| 971 |
case Interpreter_REAL: |
| 972 |
case Interpreter_NONNEGATIVE: |
| 973 |
case Interpreter_POSITIVE: |
| 974 |
{ |
| 975 |
const double value = UiForm_getReal_check (dialog, parameter); |
| 976 |
my arguments [ipar] = autostring32 (40, true); |
| 977 |
Melder_sprint (my arguments [ipar].get(),40+1, value); |
| 978 |
break; |
| 979 |
} |
| 980 |
case Interpreter_INTEGER: |
| 981 |
case Interpreter_NATURAL0: |
| 982 |
case Interpreter_NATURAL1: |
| 983 |
case Interpreter_BOOLEAN: |
| 984 |
{ |
| 985 |
const integer value = UiForm_getInteger (dialog, parameter); |
| 986 |
my arguments [ipar] = autostring32 (40, true); |
| 987 |
Melder_sprint (my arguments [ipar].get(),40+1, value); |
| 988 |
break; |
| 989 |
} |
| 990 |
case Interpreter_REALVECTOR: |
| 991 |
case Interpreter_NONNEGATIVEVECTOR: |
| 992 |
case Interpreter_POSITIVEVECTOR: |
| 993 |
{ |
| 994 |
const VEC realVectorValue = UiForm_getRealVector (dialog, parameter); |
| 995 |
autoMelderString buffer; |
| 996 |
for (integer i = 1; i <= realVectorValue.size; i ++) { |
| 997 |
MelderString_append (& buffer, realVectorValue [i]); |
| 998 |
if (i < realVectorValue.size) |
| 999 |
MelderString_appendCharacter (& buffer, U' '); |
| 1000 |
} |
| 1001 |
my arguments [ipar] = Melder_dup (buffer.string); |
| 1002 |
break; |
| 1003 |
} |
| 1004 |
case Interpreter_INTEGERVECTOR: |
| 1005 |
case Interpreter_NATURAL0VECTOR: |
| 1006 |
case Interpreter_NATURAL1VECTOR: |
| 1007 |
{ |
| 1008 |
const INTVEC integerVectorValue = UiForm_getIntegerVector (dialog, parameter); |
| 1009 |
autoMelderString buffer; |
| 1010 |
for (integer i = 1; i <= integerVectorValue.size; i ++) { |
| 1011 |
MelderString_append (& buffer, integerVectorValue [i]); |
| 1012 |
if (i < integerVectorValue.size) |
| 1013 |
MelderString_appendCharacter (& buffer, U' '); |
| 1014 |
} |
| 1015 |
my arguments [ipar] = Melder_dup (buffer.string); |
| 1016 |
break; |
| 1017 |
} |
| 1018 |
case Interpreter_REALMATRIX: |
| 1019 |
{ |
| 1020 |
Melder_throw (U"Cannot handle matrices in forms yet"); |
| 1021 |
break; |
| 1022 |
} |
| 1023 |
case Interpreter_CHOICE: |
| 1024 |
case Interpreter_OPTIONMENU: { |
| 1025 |
const integer integerValue = UiForm_getInteger (dialog, parameter); |
| 1026 |
const conststring32 stringValue = UiForm_getString (dialog, parameter); |
| 1027 |
my arguments [ipar] = autostring32 (40, true); |
| 1028 |
Melder_sprint (my arguments [ipar].get(),40+1, integerValue); |
| 1029 |
Melder_sprint (my choiceArguments [ipar],100, stringValue); |
| 1030 |
break; |
| 1031 |
} |
| 1032 |
case Interpreter_BUTTON: |
| 1033 |
case Interpreter_OPTION: |
| 1034 |
case Interpreter_COMMENT: |
| 1035 |
break; |
| 1036 |
default: |
| 1037 |
Melder_crash (U"Unhandled parameter type ", my types [ipar]); |
| 1038 |
} |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
static void convertBooleansAndChoicesToNumbersAndRelativeToAbsolutePaths (Interpreter me, integer size) { |
| 1043 |
for (integer ipar = 1; ipar <= size; ipar ++) { |
| 1044 |
if ( |
| 1045 |
my types [ipar] == Interpreter_INFILE || |
| 1046 |
my types [ipar] == Interpreter_OUTFILE || |
| 1047 |
my types [ipar] == Interpreter_FOLDER |
| 1048 |
) { |
| 1049 |
structMelderFile file { }; |
| 1050 |
Melder_relativePathToFile (my arguments [ipar].get(), & file); |
| 1051 |
my arguments [ipar] = Melder_dup_f (MelderFile_peekPath (& file)); |
| 1052 |
} else if (my types [ipar] == Interpreter_BOOLEAN) { |
| 1053 |
mutablestring32 arg = & my arguments [ipar] [0]; |
| 1054 |
if ( |
| 1055 |
str32equ (arg, U"1") || |
| 1056 |
str32equ (arg, U"yes") || str32equ (arg, U"on") || |
| 1057 |
str32equ (arg, U"Yes") || str32equ (arg, U"On") || |
| 1058 |
str32equ (arg, U"YES") || str32equ (arg, U"ON") |
| 1059 |
) { |
| 1060 |
str32cpy (arg, U"1"); |
| 1061 |
} else if ( |
| 1062 |
str32equ (arg, U"0") || |
| 1063 |
str32equ (arg, U"no") || str32equ (arg, U"off") || |
| 1064 |
str32equ (arg, U"No") || str32equ (arg, U"Off") || |
| 1065 |
str32equ (arg, U"NO") || str32equ (arg, U"OFF") |
| 1066 |
) { |
| 1067 |
str32cpy (arg, U"0"); |
| 1068 |
} else { |
| 1069 |
Melder_throw (U"Unknown value \"", arg, U"\" for boolean \"", my parameters [ipar], U"\"."); |
| 1070 |
} |
| 1071 |
} else if (my types [ipar] == Interpreter_CHOICE) { |
| 1072 |
integer jpar; |
| 1073 |
mutablestring32 arg = & my arguments [ipar] [0]; |
| 1074 |
for (jpar = ipar + 1; jpar <= my numberOfParameters; jpar ++) { |
| 1075 |
if (my types [jpar] != Interpreter_BUTTON && my types [jpar] != Interpreter_OPTION) |
| 1076 |
Melder_throw (U"Unknown value \"", arg, U"\" for choice \"", my parameters [ipar], U"\"."); |
| 1077 |
if (str32equ (my arguments [jpar].get(), arg)) { // the button labels are in the arguments; see Interpreter_readParameters |
| 1078 |
str32cpy (arg, Melder_integer (jpar - ipar)); |
| 1079 |
str32cpy (my choiceArguments [ipar], my arguments [jpar].get()); |
| 1080 |
break; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
if (jpar > my numberOfParameters) |
| 1084 |
Melder_throw (U"Unknown value \"", arg, U"\" for choice \"", my parameters [ipar], U"\"."); |
| 1085 |
} else if (my types [ipar] == Interpreter_OPTIONMENU) { |
| 1086 |
integer jpar; |
| 1087 |
mutablestring32 arg = & my arguments [ipar] [0]; |
| 1088 |
for (jpar = ipar + 1; jpar <= my numberOfParameters; jpar ++) { |
| 1089 |
if (my types [jpar] != Interpreter_OPTION && my types [jpar] != Interpreter_BUTTON) |
| 1090 |
Melder_throw (U"Unknown value \"", arg, U"\" for option menu \"", my parameters [ipar], U"\"."); |
| 1091 |
if (str32equ (my arguments [jpar].get(), arg)) { |
| 1092 |
str32cpy (arg, Melder_integer (jpar - ipar)); |
| 1093 |
str32cpy (my choiceArguments [ipar], my arguments [jpar].get()); |
| 1094 |
break; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
if (jpar > my numberOfParameters) |
| 1098 |
Melder_throw (U"Unknown value \"", arg, U"\" for option menu \"", my parameters [ipar], U"\"."); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
void Interpreter_getArgumentsFromString (Interpreter me, conststring32 arguments) { |
| 1104 |
int size = my numberOfParameters; |
| 1105 |
const integer length = Melder_length (arguments); |
| 1106 |
while (size >= 1 && my parameters [size] [0] == U'\0') |
| 1107 |
size --; /* Ignore fields without a variable name (button, comment). */ |
| 1108 |
tidyUpParameterNames (me, size); |
| 1109 |
for (int ipar = 1; ipar < size; ipar ++) { |
| 1110 |
int ichar = 0; |
| 1111 |
/* |
| 1112 |
Ignore buttons and comments again. The buttons will keep their labels as "arguments". |
| 1113 |
*/ |
| 1114 |
if (my parameters [ipar] [0] == U'\0') |
| 1115 |
continue; |
| 1116 |
/* |
| 1117 |
Erase the current values, probably the default values, |
| 1118 |
and replace with the actual arguments. |
| 1119 |
*/ |
| 1120 |
my arguments [ipar] = autostring32 (length); |
| 1121 |
/* |
| 1122 |
Skip spaces until next argument. |
| 1123 |
*/ |
| 1124 |
Melder_skipHorizontalSpace (& arguments); |
| 1125 |
/* |
| 1126 |
The argument is everything up to the next space, or, if it starts with a double quote, |
| 1127 |
everything between this quote and the matching double quote; |
| 1128 |
in this case, the argument can represent a double quote by a sequence of two double quotes. |
| 1129 |
Example: the string |
| 1130 |
"I said ""hello""" |
| 1131 |
will be passed to the dialog as a single argument containing the text |
| 1132 |
I said "hello" |
| 1133 |
*/ |
| 1134 |
if (*arguments == U'\"') { |
| 1135 |
arguments ++; // do not include leading double quote |
| 1136 |
for (;;) { |
| 1137 |
if (*arguments == U'\0') |
| 1138 |
Melder_throw (U"Missing matching quote."); |
| 1139 |
if (*arguments == U'\"' && * ++ arguments != U'\"') |
| 1140 |
break; // remember second quote |
| 1141 |
my arguments [ipar] [ichar ++] = *arguments ++; |
| 1142 |
} |
| 1143 |
} else { |
| 1144 |
while (Melder_staysWithinInk (*arguments)) |
| 1145 |
my arguments [ipar] [ichar ++] = *arguments ++; |
| 1146 |
} |
| 1147 |
my arguments [ipar] [ichar] = U'\0'; // trailing null byte |
| 1148 |
} |
| 1149 |
/* |
| 1150 |
The last item is handled separately, because it consists of the rest of the line. |
| 1151 |
Leading spaces are skipped, but trailing spaces are included. |
| 1152 |
*/ |
| 1153 |
if (size > 0) { |
| 1154 |
Melder_skipHorizontalSpace (& arguments); |
| 1155 |
my arguments [size] = Melder_dup_f (arguments); |
| 1156 |
} |
| 1157 |
convertBooleansAndChoicesToNumbersAndRelativeToAbsolutePaths (me, size); |
| 1158 |
} |
| 1159 |
|
| 1160 |
void Interpreter_getArgumentsFromArgs (Interpreter me, integer narg, Stackel args) { |
| 1161 |
trace (narg, U" arguments"); |
| 1162 |
integer size = my numberOfParameters; |
| 1163 |
while (size >= 1 && my parameters [size] [0] == U'\0') |
| 1164 |
size --; // ignore trailing fields without a variable name (button, comment) |
| 1165 |
tidyUpParameterNames (me, size); |
| 1166 |
integer iarg = 0; |
| 1167 |
for (integer ipar = 1; ipar <= size; ipar ++) { |
| 1168 |
/* |
| 1169 |
Ignore buttons and comments again. The buttons will keep their labels as "arguments". |
| 1170 |
*/ |
| 1171 |
if (my parameters [ipar] [0] == U'\0') |
| 1172 |
continue; |
| 1173 |
/* |
| 1174 |
Erase the current values, probably the default values... |
| 1175 |
*/ |
| 1176 |
my arguments [ipar]. reset(); // |
| 1177 |
if (iarg == narg) |
| 1178 |
Melder_throw (U"Found ", narg, U" arguments but expected more."); |
| 1179 |
Stackel arg = & args [++ iarg]; |
| 1180 |
/* |
| 1181 |
... and replace with the actual arguments. BUG: this loses type information. |
| 1182 |
*/ |
| 1183 |
my arguments [ipar] = |
| 1184 |
arg -> which == Stackel_NUMBER ? Melder_dup (Melder_double (arg -> number)) : |
| 1185 |
arg -> which == Stackel_STRING ? Melder_dup (arg -> getString()) : |
| 1186 |
arg -> which == Stackel_NUMERIC_VECTOR ? Melder_dup (Melder_VEC (arg -> numericVector, true)) : |
| 1187 |
autostring32(); |
| 1188 |
Melder_assert (my arguments [ipar]); |
| 1189 |
} |
| 1190 |
if (iarg < narg) |
| 1191 |
Melder_throw (U"Found ", narg, U" arguments but expected only ", iarg, U"."); |
| 1192 |
convertBooleansAndChoicesToNumbersAndRelativeToAbsolutePaths (me, size); |
| 1193 |
} |
| 1194 |
|
| 1195 |
void Interpreter_getArgumentsFromCommandLine (Interpreter me, integer argc, char **argv) { |
| 1196 |
trace (argc, U" arguments"); |
| 1197 |
integer size = my numberOfParameters; |
| 1198 |
while (size >= 1 && my parameters [size] [0] == U'\0') |
| 1199 |
size --; // ignore trailing fields without a variable name (button, comment) |
| 1200 |
tidyUpParameterNames (me, size); |
| 1201 |
integer iarg = 0; |
| 1202 |
for (integer ipar = 1; ipar <= size; ipar ++) { |
| 1203 |
/* |
| 1204 |
Ignore buttons and comments again. The buttons will keep their labels as "arguments". |
| 1205 |
*/ |
| 1206 |
if (my parameters [ipar] [0] == U'\0') |
| 1207 |
continue; |
| 1208 |
/* |
| 1209 |
Erase the current values, probably the default values, |
| 1210 |
and replace with the actual arguments. |
| 1211 |
*/ |
| 1212 |
if (iarg == argc) |
| 1213 |
Melder_throw (U"Found ", argc, U" arguments but expected more."); |
| 1214 |
my arguments [ipar] = Melder_8to32_e (argv [iarg ++]); |
| 1215 |
Melder_assert (my arguments [ipar]); |
| 1216 |
} |
| 1217 |
if (iarg < argc) |
| 1218 |
Melder_throw (U"Found ", argc, U" arguments but expected only ", iarg, U"."); |
| 1219 |
convertBooleansAndChoicesToNumbersAndRelativeToAbsolutePaths (me, size); |
| 1220 |
} |
| 1221 |
|
| 1222 |
static void Interpreter_addNumericVariable (Interpreter me, conststring32 key, double value) { |
| 1223 |
autoInterpreterVariable variable = InterpreterVariable_create (key); |
| 1224 |
variable -> numericValue = value; |
| 1225 |
my variablesMap [key] = variable.move(); |
| 1226 |
variable.releaseToAmbiguousOwner(); |
| 1227 |
} |
| 1228 |
|
| 1229 |
static void Interpreter_addStringVariable (Interpreter me, conststring32 key, conststring32 value) { |
| 1230 |
autoInterpreterVariable variable = InterpreterVariable_create (key); |
| 1231 |
variable -> stringValue = Melder_dup (value); |
| 1232 |
my variablesMap [key] = variable.move(); |
| 1233 |
variable.releaseToAmbiguousOwner(); |
| 1234 |
} |
| 1235 |
|
| 1236 |
static void Interpreter_addNumericVectorVariable (Interpreter me, conststring32 key, conststring32 value) { |
| 1237 |
autoInterpreterVariable variable = InterpreterVariable_create (key); |
| 1238 |
variable -> numericVectorValue = splitByWhitespace_VEC (value); |
| 1239 |
my variablesMap [key] = variable.move(); |
| 1240 |
variable.releaseToAmbiguousOwner(); |
| 1241 |
} |
| 1242 |
|
| 1243 |
InterpreterVariable Interpreter_hasVariable (Interpreter me, conststring32 key) { |
| 1244 |
Melder_pre (key); |
| 1245 |
conststring32 variableNameIncludingProcedureName = |
| 1246 |
( key [0] == U'.' ? Melder_cat (my procedureStackNames [my callDepth], key) : key ); |
| 1247 |
auto it = my variablesMap. find (variableNameIncludingProcedureName); |
| 1248 |
if (it != my variablesMap.end()) |
| 1249 |
return it -> second.get(); |
| 1250 |
else |
| 1251 |
return nullptr; |
| 1252 |
} |
| 1253 |
|
| 1254 |
InterpreterVariable Interpreter_lookUpVariable (Interpreter me, conststring32 key) { |
| 1255 |
Melder_pre (key); |
| 1256 |
conststring32 variableNameIncludingProcedureName = |
| 1257 |
( key [0] == U'.' ? Melder_cat (my procedureStackNames [my callDepth], key) : key ); |
| 1258 |
auto it = my variablesMap. find (variableNameIncludingProcedureName); |
| 1259 |
if (it != my variablesMap.end()) |
| 1260 |
return it -> second.get(); |
| 1261 |
/* |
| 1262 |
The variable doesn't yet exist: create a new one. |
| 1263 |
*/ |
| 1264 |
autoInterpreterVariable variable = InterpreterVariable_create (variableNameIncludingProcedureName); |
| 1265 |
InterpreterVariable variable_ref = variable.get(); |
| 1266 |
my variablesMap [variableNameIncludingProcedureName] = variable.move(); |
| 1267 |
return variable_ref; |
| 1268 |
} |
| 1269 |
|
| 1270 |
static integer lookupLabel (Interpreter me, conststring32 labelName) { |
| 1271 |
const integer position = NUMfindInSorted (my labelNames.get(), labelName); |
| 1272 |
if (position == 0) |
| 1273 |
Melder_throw (U"Unknown label \"", labelName, U"\" (we know of ", my labelNames.size, U" labels)."); |
| 1274 |
return position; |
| 1275 |
} |
| 1276 |
|
| 1277 |
static bool isCommand (conststring32 string) { |
| 1278 |
const char32 *p = & string [0]; |
| 1279 |
/* |
| 1280 |
Things that start with "nowarn", "noprogress", or "nocheck" are commands. |
| 1281 |
*/ |
| 1282 |
if (p [0] == U'n' && p [1] == U'o' && |
| 1283 |
(str32nequ (p + 2, U"warn ", 5) || str32nequ (p + 2, U"progress ", 9) || str32nequ (p + 2, U"check ", 6))) |
| 1284 |
return true; |
| 1285 |
if (str32nequ (p, U"demo ", 5)) |
| 1286 |
return true; |
| 1287 |
/* |
| 1288 |
Otherwise, things that start with nonupper case are formulas. |
| 1289 |
*/ |
| 1290 |
if (! Melder_isUpperCaseLetter (*p)) |
| 1291 |
return false; |
| 1292 |
/* |
| 1293 |
The remaining possibility is things that start with upper case. |
| 1294 |
If they contain an underscore, they are object names, hence we must have a formula. |
| 1295 |
Otherwise, we have a command. |
| 1296 |
*/ |
| 1297 |
while (Melder_isAlphanumeric (*p)) |
| 1298 |
p ++; |
| 1299 |
return *p != U'_'; |
| 1300 |
} |
| 1301 |
|
| 1302 |
static void parameterToVariable (Interpreter me, int type, conststring32 in_parameter, int ipar) { |
| 1303 |
Melder_pre (type != 0); |
| 1304 |
char32 parameter [200]; |
| 1305 |
str32cpy (parameter, in_parameter); |
| 1306 |
if (type >= Interpreter_MINIMUM_TYPE_FOR_NUMERIC_VARIABLE && type <= Interpreter_MAXIMUM_TYPE_FOR_NUMERIC_VARIABLE) { |
| 1307 |
Interpreter_addNumericVariable (me, parameter, Melder_atof (my arguments [ipar].get())); |
| 1308 |
} else if (type == Interpreter_CHOICE || type == Interpreter_OPTIONMENU) { |
| 1309 |
Interpreter_addNumericVariable (me, parameter, Melder_atof (my arguments [ipar].get())); |
| 1310 |
str32cat (parameter, U"$"); |
| 1311 |
Interpreter_addStringVariable (me, parameter, my choiceArguments [ipar]); |
| 1312 |
} else if (type == Interpreter_BUTTON || type == Interpreter_OPTION || type == Interpreter_COMMENT) { |
| 1313 |
; // do not add a variable |
| 1314 |
} else if (type >= Interpreter_MINIMUM_TYPE_FOR_NUMERIC_VECTOR_VARIABLE && type <= Interpreter_MAXIMUM_TYPE_FOR_NUMERIC_VECTOR_VARIABLE) { |
| 1315 |
str32cat (parameter, U"#"); |
| 1316 |
Interpreter_addNumericVectorVariable (me, parameter, my arguments [ipar].get()); |
| 1317 |
} else { |
| 1318 |
str32cat (parameter, U"$"); |
| 1319 |
Interpreter_addStringVariable (me, parameter, my arguments [ipar].get()); |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
inline static void NumericVectorVariable_move (InterpreterVariable variable, VEC movedVector, bool rightHandSideOwned) { |
| 1324 |
if (rightHandSideOwned) { |
| 1325 |
/* |
| 1326 |
Statement like: a# = b# + c# |
| 1327 |
*/ |
| 1328 |
variable -> numericVectorValue. adoptFromAmbiguousOwner (movedVector); |
| 1329 |
} else if (variable -> numericVectorValue.size == movedVector.size) { |
| 1330 |
if (variable -> numericVectorValue.cells == movedVector.cells) { |
| 1331 |
/* |
| 1332 |
Statement like: a# = a# |
| 1333 |
*/ |
| 1334 |
(void) 0; // assigning a variable to itself: do nothing |
| 1335 |
} else { |
| 1336 |
/* |
| 1337 |
Statement like: a# = b# // with matching sizes |
| 1338 |
*/ |
| 1339 |
variable -> numericVectorValue.all() <<= movedVector; |
| 1340 |
} |
| 1341 |
} else { |
| 1342 |
/* |
| 1343 |
Statement like: a# = b# // with non-matching sizes |
| 1344 |
*/ |
| 1345 |
variable -> numericVectorValue = copy_VEC (movedVector); |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
inline static void StringArrayVariable_move (InterpreterVariable variable, STRVEC movedVector, bool rightHandSideOwned) { |
| 1350 |
if (rightHandSideOwned) { |
| 1351 |
/* |
| 1352 |
Statement like: a$# = b$# + c$# |
| 1353 |
*/ |
| 1354 |
variable -> stringArrayValue. adoptFromAmbiguousOwner (movedVector); |
| 1355 |
} else if (variable -> stringArrayValue.size == movedVector.size) { |
| 1356 |
if ((char32 **) variable -> stringArrayValue.elements == movedVector.elements) { |
| 1357 |
/* |
| 1358 |
Statement like: a$# = a$# |
| 1359 |
*/ |
| 1360 |
(void) 0; // assigning a variable to itself: do nothing |
| 1361 |
} else { |
| 1362 |
/* |
| 1363 |
Statement like: a$# = b$# // with matching sizes |
| 1364 |
*/ |
| 1365 |
variable -> stringArrayValue.all() <<= movedVector; |
| 1366 |
} |
| 1367 |
} else { |
| 1368 |
/* |
| 1369 |
Statement like: a$# = b$# // with non-matching sizes |
| 1370 |
*/ |
| 1371 |
variable -> stringArrayValue = copy_STRVEC (movedVector); |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
inline static void NumericMatrixVariable_move (InterpreterVariable variable, MAT movedMatrix, bool rightHandSideOwned) { |
| 1376 |
if (rightHandSideOwned) { |
| 1377 |
/* |
| 1378 |
Statement like: a## = b## + c## |
| 1379 |
*/ |
| 1380 |
variable -> numericMatrixValue. adoptFromAmbiguousOwner (movedMatrix); |
| 1381 |
} else if (variable -> numericMatrixValue.nrow == movedMatrix.nrow && variable -> numericMatrixValue.ncol == movedMatrix.ncol) { |
| 1382 |
if (variable -> numericMatrixValue.cells == movedMatrix.cells) { |
| 1383 |
/* |
| 1384 |
Statement like: a## = a## |
| 1385 |
*/ |
| 1386 |
(void) 0; // assigning a variable to itself: do nothing |
| 1387 |
} else { |
| 1388 |
/* |
| 1389 |
Statement like: a## = b## // with matching sizes |
| 1390 |
*/ |
| 1391 |
variable -> numericMatrixValue.all() <<= movedMatrix; |
| 1392 |
} |
| 1393 |
} else { |
| 1394 |
/* |
| 1395 |
Statement like: a## = b## // with non-matching sizes |
| 1396 |
*/ |
| 1397 |
variable -> numericMatrixValue = copy_MAT (movedMatrix); |
| 1398 |
} |
| 1399 |
} |
| 1400 |
|
| 1401 |
inline static void NumericVectorVariable_add (InterpreterVariable variable, double scalar) { |
| 1402 |
variable -> numericVectorValue.all() += scalar; |
| 1403 |
} |
| 1404 |
inline static void NumericVectorVariable_add (InterpreterVariable variable, constVEC vector) { |
| 1405 |
VEC const& variableVector = variable -> numericVectorValue.get(); |
| 1406 |
Melder_require (vector.size == variableVector.size, |
| 1407 |
U"You cannot add a vector with size ", vector.size, |
| 1408 |
U" to a vector with a different size (", variableVector.size, U")." |
| 1409 |
); |
| 1410 |
variableVector += vector; |
| 1411 |
} |
| 1412 |
inline static void NumericVectorVariable_subtract (InterpreterVariable variable, double scalar) { |
| 1413 |
variable -> numericVectorValue.all() -= scalar; |
| 1414 |
} |
| 1415 |
inline static void NumericVectorVariable_subtract (InterpreterVariable variable, constVEC vector) { |
| 1416 |
VEC const& variableVector = variable -> numericVectorValue.get(); |
| 1417 |
Melder_require (vector.size == variable -> numericVectorValue.size, |
| 1418 |
U"You cannot subtract a vector with size ", vector.size, |
| 1419 |
U" from a vector with a different size (", variableVector.size, U")." |
| 1420 |
); |
| 1421 |
variableVector -= vector; |
| 1422 |
} |
| 1423 |
inline static void NumericVectorVariable_multiply (InterpreterVariable variable, double scalar) { |
| 1424 |
variable -> numericVectorValue.all() *= scalar; |
| 1425 |
} |
| 1426 |
inline static void NumericVectorVariable_multiply (InterpreterVariable variable, constVEC vector) { |
| 1427 |
VEC const& variableVector = variable -> numericVectorValue.get(); |
| 1428 |
Melder_require (vector.size != variableVector.size, |
| 1429 |
U"You cannot multiply a vector with size ", variableVector.size, |
| 1430 |
U" with a vector with a different size (", vector.size, U")." |
| 1431 |
); |
| 1432 |
variableVector *= vector; |
| 1433 |
} |
| 1434 |
inline static void NumericVectorVariable_divide (InterpreterVariable variable, double scalar) { |
| 1435 |
variable -> numericVectorValue.all() /= scalar; |
| 1436 |
} |
| 1437 |
inline static void NumericVectorVariable_divide (InterpreterVariable variable, constVEC vector) { |
| 1438 |
VEC const& variableVector = variable -> numericVectorValue.get(); |
| 1439 |
Melder_require (vector.size != variableVector.size, |
| 1440 |
U"You cannot divide a vector with size ", variableVector.size, |
| 1441 |
U" by a vector with a different size (", vector.size, U")." |
| 1442 |
); |
| 1443 |
variableVector /= vector; |
| 1444 |
} |
| 1445 |
inline static void NumericMatrixVariable_add (InterpreterVariable variable, double scalar) { |
| 1446 |
MAT variableMatrix = variable -> numericMatrixValue.get(); |
| 1447 |
for (integer irow = 1; irow <= variableMatrix.nrow; irow ++) |
| 1448 |
for (integer icol = 1; icol <= variableMatrix.ncol; icol ++) |
| 1449 |
variableMatrix [irow] [icol] += scalar; |
| 1450 |
} |
| 1451 |
inline static void NumericMatrixVariable_add (InterpreterVariable variable, constMAT matrix) { |
| 1452 |
MAT variableMatrix = variable -> numericMatrixValue.get(); |
| 1453 |
if (matrix.nrow != variableMatrix.nrow || matrix.ncol != variableMatrix.ncol) |
| 1454 |
Melder_throw (U"You cannot add a matrix with size ", matrix.nrow, U"x", matrix.ncol, |
| 1455 |
U" to a matrix with a different size (", variableMatrix.nrow, U"x", variableMatrix.ncol, U")."); |
| 1456 |
for (integer irow = 1; irow <= variableMatrix.nrow; irow ++) |
| 1457 |
for (integer icol = 1; icol <= variableMatrix.ncol; icol ++) |
| 1458 |
variableMatrix [irow] [icol] += matrix [irow] [icol]; |
| 1459 |
} |
| 1460 |
inline static void NumericMatrixVariable_subtract (InterpreterVariable variable, double scalar) { |
| 1461 |
MAT variableMatrix = variable -> numericMatrixValue.get(); |
| 1462 |
for (integer irow = 1; irow <= variableMatrix.nrow; irow ++) |
| 1463 |
for (integer icol = 1; icol <= variableMatrix.ncol; icol ++) |
| 1464 |
variableMatrix [irow] [icol] -= scalar; |
| 1465 |
} |
| 1466 |
inline static void NumericMatrixVariable_subtract (InterpreterVariable variable, constMAT matrix) { |
| 1467 |
MAT variableMatrix = variable -> numericMatrixValue.get(); |
| 1468 |
if (matrix.nrow != variableMatrix.nrow || matrix.ncol != variableMatrix.ncol) |
| 1469 |
Melder_throw (U"You cannot subtract a matrix with size ", matrix.nrow, U"x", matrix.ncol, |
| 1470 |
U" from a matrix with a different size (", variableMatrix.nrow, U"x", variableMatrix.ncol, U")."); |
| 1471 |
for (integer irow = 1; irow <= variableMatrix.nrow; irow ++) |
| 1472 |
for (integer icol = 1; icol <= variableMatrix.ncol; icol ++) |
| 1473 |
variableMatrix [irow] [icol] -= matrix [irow] [icol]; |
| 1474 |
} |
| 1475 |
inline static void NumericMatrixVariable_multiply (InterpreterVariable variable, double scalar) { |
| 1476 |
MAT variableMatrix = variable -> numericMatrixValue.get(); |
| 1477 |
for (integer irow = 1; irow <= variableMatrix.nrow; irow ++) |
| 1478 |
for (integer icol = 1; icol <= variableMatrix.ncol; icol ++) |
| 1479 |
variableMatrix [irow] [icol] *= scalar; |
| 1480 |
} |
| 1481 |
inline static void NumericMatrixVariable_multiply (InterpreterVariable variable, constMAT matrix) { |
| 1482 |
MAT variableMatrix = variable -> numericMatrixValue.get(); |
| 1483 |
if (matrix.nrow != variableMatrix.nrow || matrix.ncol != variableMatrix.ncol) |
| 1484 |
Melder_throw (U"You cannot multiply a matrix with size ", variableMatrix.nrow, U"x", variableMatrix.ncol, |
| 1485 |
U" from a matrix with a different size (", matrix.nrow, U"x", matrix.ncol, U")."); |
| 1486 |
for (integer irow = 1; irow <= variableMatrix.nrow; irow ++) |
| 1487 |
for (integer icol = 1; icol <= variableMatrix.ncol; icol ++) |
| 1488 |
variableMatrix [irow] [icol] *= matrix [irow] [icol]; |
| 1489 |
} |
| 1490 |
inline static void NumericMatrixVariable_divide (InterpreterVariable variable, double scalar) { |
| 1491 |
MAT variableMatrix = variable -> numericMatrixValue.get(); |
| 1492 |
for (integer irow = 1; irow <= variableMatrix.nrow; irow ++) |
| 1493 |
for (integer icol = 1; icol <= variableMatrix.ncol; icol ++) |
| 1494 |
variableMatrix [irow] [icol] /= scalar; |
| 1495 |
} |
| 1496 |
inline static void NumericMatrixVariable_divide (InterpreterVariable variable, constMAT matrix) { |
| 1497 |
MAT variableMatrix = variable -> numericMatrixValue.get(); |
| 1498 |
if (matrix.nrow != variableMatrix.nrow || matrix.ncol != variableMatrix.ncol) |
| 1499 |
Melder_throw (U"You cannot divide a matrix with size ", variableMatrix.nrow, U"x", variableMatrix.ncol, |
| 1500 |
U" by a matrix with a different size (", matrix.nrow, U"x", matrix.ncol, U")."); |
| 1501 |
for (integer irow = 1; irow <= variableMatrix.nrow; irow ++) |
| 1502 |
for (integer icol = 1; icol <= variableMatrix.ncol; icol ++) |
| 1503 |
variableMatrix [irow] [icol] /= matrix [irow] [icol]; |
| 1504 |
} |
| 1505 |
|
| 1506 |
static void Interpreter_do_procedureCall (Interpreter me, char32 *command, |
| 1507 |
constvector <mutablestring32> const& lines, integer& lineNumber, integer callStack [], int& callDepth) |
| 1508 |
{ |
| 1509 |
/* |
| 1510 |
Modern type of procedure calls, with comma separation, quoted strings, and array support. |
| 1511 |
|
| 1512 |
We just passed the `@` sign, so we continue by looking for a procedure name at the call site. |
| 1513 |
*/ |
| 1514 |
char32 *p = command; |
| 1515 |
Melder_skipHorizontalSpace (& p); |
| 1516 |
char32 *callName = p; |
| 1517 |
while (Melder_staysWithinInk (*p) && *p != U'(' && *p != U':') |
| 1518 |
p ++; |
| 1519 |
if (p == callName) Melder_throw (U"Missing procedure name after \"@\"."); |
| 1520 |
bool hasArguments = ( *p != U'\0' ); |
| 1521 |
if (hasArguments) { |
| 1522 |
bool parenthesisOrColonFound = ( *p == U'(' || *p == U':' ); |
| 1523 |
*p = U'\0'; // close procedure name |
| 1524 |
if (! parenthesisOrColonFound) { |
| 1525 |
p ++; // step over first white space |
| 1526 |
Melder_skipHorizontalSpace (& p); |
| 1527 |
hasArguments = ( *p != U'\0' ); |
| 1528 |
parenthesisOrColonFound = ( *p == U'(' || *p == U':' ); |
| 1529 |
if (hasArguments && ! parenthesisOrColonFound) |
| 1530 |
Melder_throw (U"Missing parenthesis or colon after procedure name \"", callName, U"\"."); |
| 1531 |
} |
| 1532 |
p ++; // step over parenthesis or colon |
| 1533 |
} |
| 1534 |
const integer callLength = Melder_length (callName); |
| 1535 |
integer iline = 1; |
| 1536 |
for (; iline <= lines.size; iline ++) { |
| 1537 |
if (! str32nequ (lines [iline], U"procedure", 9) || ! Melder_isHorizontalSpace (lines [iline] [9])) |
| 1538 |
continue; |
| 1539 |
char32 *q = lines [iline] + 10; |
| 1540 |
Melder_skipHorizontalSpace (& q); // skip whitespace before procedure name |
| 1541 |
char32 *const procName = q; |
| 1542 |
while (Melder_staysWithinInk (*q) && *q != U'(' && *q != U':') |
| 1543 |
q ++; |
| 1544 |
if (q == procName) |
| 1545 |
Melder_throw (U"Missing procedure name after 'procedure'."); |
| 1546 |
if (q - procName == callLength && str32nequ (procName, callName, callLength)) { |
| 1547 |
/* |
| 1548 |
We found the procedure definition. |
| 1549 |
*/ |
| 1550 |
if (++ my callDepth > Interpreter_MAX_CALL_DEPTH) |
| 1551 |
Melder_throw (U"Call depth greater than ", Interpreter_MAX_CALL_DEPTH, U"."); |
| 1552 |
str32cpy (my procedureStackNames [my callDepth], callName); |
| 1553 |
const bool parenthesisOrColonFound = ( *q == U'(' || *q == U':' ); |
| 1554 |
if (*q) |
| 1555 |
q ++; // step over parenthesis or colon or first white space |
| 1556 |
if (! parenthesisOrColonFound) { |
| 1557 |
Melder_skipHorizontalSpace (& q); |
| 1558 |
if (*q == U'(' || *q == U':') |
| 1559 |
q ++; // step over parenthesis or colon |
| 1560 |
} |
| 1561 |
while (*q && *q != U')' && *q != U';') { |
| 1562 |
static MelderString argument; |
| 1563 |
MelderString_empty (& argument); |
| 1564 |
Melder_skipHorizontalSpace (& p); |
| 1565 |
Melder_skipHorizontalSpace (& q); |
| 1566 |
conststring32 parameterName = q; |
| 1567 |
while (Melder_staysWithinInk (*q) && *q != U',' && *q != U')' && *q != U';') |
| 1568 |
q ++; // collect parameter name |
| 1569 |
int expressionDepth = 0; |
| 1570 |
for (; *p; p ++) { |
| 1571 |
if (*p == U',') { |
| 1572 |
if (expressionDepth == 0) |
| 1573 |
break; // depth-0 comma ends expression |
| 1574 |
MelderString_appendCharacter (& argument, U','); |
| 1575 |
} else if (*p == U')' || *p == U']' || *p == U'}') { |
| 1576 |
if (expressionDepth == 0) |
| 1577 |
break; // depth-0 closing parenthesis ends expression |
| 1578 |
expressionDepth --; |
| 1579 |
MelderString_appendCharacter (& argument, *p); |
| 1580 |
} else if (*p == U'(' || *p == U'[' || *p == U'{') { |
| 1581 |
expressionDepth ++; |
| 1582 |
MelderString_appendCharacter (& argument, *p); |
| 1583 |
} else if (*p == U'“') { |
| 1584 |
/* |
| 1585 |
Enter a string literal. |
| 1586 |
*/ |
| 1587 |
MelderString_appendCharacter (& argument, U'\"'); |
| 1588 |
p ++; |
| 1589 |
for (;; p ++) { |
| 1590 |
if (*p == U'\0') { |
| 1591 |
Melder_throw (U"Incomplete curly string literal: the quotes don't match."); |
| 1592 |
} else if (*p == U'\"') { |
| 1593 |
MelderString_appendCharacter (& argument, U'\"'); |
| 1594 |
MelderString_appendCharacter (& argument, U'\"'); |
| 1595 |
} else if (*p == U'”') { |
| 1596 |
MelderString_appendCharacter (& argument, U'\"'); |
| 1597 |
break; |
| 1598 |
} else { |
| 1599 |
MelderString_appendCharacter (& argument, *p); |
| 1600 |
} |
| 1601 |
} |
| 1602 |
} else if (*p == U'\"') { |
| 1603 |
/* |
| 1604 |
Enter a string literal. |
| 1605 |
*/ |
| 1606 |
MelderString_appendCharacter (& argument, U'\"'); |
| 1607 |
p ++; |
| 1608 |
for (;; p ++) { |
| 1609 |
if (*p == U'\0') { |
| 1610 |
Melder_throw (U"Incomplete string literal: the quotes don't match."); |
| 1611 |
} else if (*p == U'\"') { |
| 1612 |
MelderString_appendCharacter (& argument, U'\"'); |
| 1613 |
if (p [1] == '\"') { |
| 1614 |
p ++; // stay in the string literal |
| 1615 |
MelderString_appendCharacter (& argument, U'\"'); |
| 1616 |
} else { |
| 1617 |
break; |
| 1618 |
} |
| 1619 |
} else { |
| 1620 |
MelderString_appendCharacter (& argument, *p); |
| 1621 |
} |
| 1622 |
} |
| 1623 |
} else { |
| 1624 |
MelderString_appendCharacter (& argument, *p); |
| 1625 |
} |
| 1626 |
} |
| 1627 |
if (q == parameterName) |
| 1628 |
break; |
| 1629 |
if (*p) { |
| 1630 |
*p = U'\0'; |
| 1631 |
p ++; |
| 1632 |
} |
| 1633 |
if (q [-1] == U'$') { |
| 1634 |
my callDepth --; |
| 1635 |
autostring32 value = Interpreter_stringExpression (me, argument.string); |
| 1636 |
my callDepth ++; |
| 1637 |
const char32 save = *q; |
| 1638 |
*q = U'\0'; |
| 1639 |
InterpreterVariable var = Interpreter_lookUpVariable (me, parameterName); |
| 1640 |
*q = save; |
| 1641 |
var -> stringValue = value.move(); |
| 1642 |
} else if (q [-1] == U'#') { |
| 1643 |
if (q [-2] == U'#') { |
| 1644 |
MAT value; |
| 1645 |
bool owned; |
| 1646 |
my callDepth --; |
| 1647 |
Interpreter_numericMatrixExpression (me, argument.string, & value, & owned); |
| 1648 |
my callDepth ++; |
| 1649 |
const char32 save = *q; |
| 1650 |
*q = U'\0'; |
| 1651 |
InterpreterVariable var = Interpreter_lookUpVariable (me, parameterName); |
| 1652 |
*q = save; |
| 1653 |
NumericMatrixVariable_move (var, value, owned); |
| 1654 |
} else if (q [-2] == U'$') { |
| 1655 |
STRVEC value; |
| 1656 |
bool owned; |
| 1657 |
my callDepth --; |
| 1658 |
Interpreter_stringArrayExpression (me, argument.string, & value, & owned); |
| 1659 |
my callDepth ++; |
| 1660 |
const char32 save = *q; |
| 1661 |
*q = U'\0'; |
| 1662 |
InterpreterVariable var = Interpreter_lookUpVariable (me, parameterName); |
| 1663 |
*q = save; |
| 1664 |
StringArrayVariable_move (var, value, owned); |
| 1665 |
} else { |
| 1666 |
VEC value; |
| 1667 |
bool owned; |
| 1668 |
my callDepth --; |
| 1669 |
Interpreter_numericVectorExpression (me, argument.string, & value, & owned); |
| 1670 |
my callDepth ++; |
| 1671 |
const char32 save = *q; |
| 1672 |
*q = U'\0'; |
| 1673 |
InterpreterVariable var = Interpreter_lookUpVariable (me, parameterName); |
| 1674 |
*q = save; |
| 1675 |
NumericVectorVariable_move (var, value, owned); |
| 1676 |
} |
| 1677 |
} else { |
| 1678 |
double value; |
| 1679 |
my callDepth --; |
| 1680 |
Interpreter_numericExpression (me, argument.string, & value); |
| 1681 |
my callDepth ++; |
| 1682 |
const char32 save = *q; |
| 1683 |
*q = U'\0'; |
| 1684 |
InterpreterVariable var = Interpreter_lookUpVariable (me, parameterName); |
| 1685 |
*q = save; |
| 1686 |
var -> numericValue = value; |
| 1687 |
} |
| 1688 |
if (*q) |
| 1689 |
q ++; // skip comma |
| 1690 |
} |
| 1691 |
if (callDepth == Interpreter_MAX_CALL_DEPTH) |
| 1692 |
Melder_throw (U"Call depth greater than ", Interpreter_MAX_CALL_DEPTH, U"."); |
| 1693 |
callStack [++ callDepth] = lineNumber; |
| 1694 |
lineNumber = iline; |
| 1695 |
break; |
| 1696 |
} |
| 1697 |
} |
| 1698 |
if (iline > lines.size) |
| 1699 |
Melder_throw (U"Procedure \"", callName, U"\" not found."); |
| 1700 |
} |
| 1701 |
static void Interpreter_do_oldProcedureCall (Interpreter me, char32 *command, |
| 1702 |
constvector <mutablestring32> const& lines, integer& lineNumber, integer callStack [], int& callDepth) |
| 1703 |
{ |
| 1704 |
/* |
| 1705 |
Old type of procedure calls, with space separation, unquoted strings, and no array support. |
| 1706 |
*/ |
| 1707 |
char32 *p = command; |
| 1708 |
Melder_skipHorizontalSpace (& p); |
| 1709 |
char32 *callName = p; |
| 1710 |
while (*p != U'\0' && ! Melder_isHorizontalSpace (*p) && *p != U'(' && *p != U':') |
| 1711 |
p ++; |
| 1712 |
if (p == callName) |
| 1713 |
Melder_throw (U"Missing procedure name after 'call'."); |
| 1714 |
bool hasArguments = ( *p != U'\0' ); |
| 1715 |
*p = U'\0'; // close procedure name |
| 1716 |
const integer callLength = Melder_length (callName); |
| 1717 |
integer iline = 1; |
| 1718 |
for (; iline <= lines.size; iline ++) { |
| 1719 |
if (! str32nequ (lines [iline], U"procedure ", 10)) |
| 1720 |
continue; |
| 1721 |
char32 *q = lines [iline] + 10; |
| 1722 |
Melder_skipHorizontalSpace (& q); |
| 1723 |
char32 *procName = q; |
| 1724 |
while (*q != U'\0' && ! Melder_isHorizontalSpace (*q) && *q != U'(' && *q != U':') |
| 1725 |
q ++; |
| 1726 |
if (q == procName) |
| 1727 |
Melder_throw (U"Missing procedure name after 'procedure'."); |
| 1728 |
bool hasParameters = ( *q != U'\0' ); |
| 1729 |
if (q - procName == callLength && str32nequ (procName, callName, callLength)) { |
| 1730 |
if (hasArguments && ! hasParameters) |
| 1731 |
Melder_throw (U"Call to procedure \"", callName, U"\" has too many arguments."); |
| 1732 |
if (hasParameters && ! hasArguments) |
| 1733 |
Melder_throw (U"Call to procedure \"", callName, U"\" has too few arguments."); |
| 1734 |
if (++ my callDepth > Interpreter_MAX_CALL_DEPTH) |
| 1735 |
Melder_throw (U"Call depth greater than ", Interpreter_MAX_CALL_DEPTH, U"."); |
| 1736 |
str32cpy (my procedureStackNames [my callDepth], callName); |
| 1737 |
if (hasParameters) { |
| 1738 |
bool parenthesisOrColonFound = ( *q == U'(' || *q == U':' ); |
| 1739 |
q ++; // step over parenthesis or colon or first white space |
| 1740 |
if (! parenthesisOrColonFound) { |
| 1741 |
Melder_skipHorizontalSpace (& q); |
| 1742 |
if (*q == U'(' || *q == U':') |
| 1743 |
q ++; // step over parenthesis or colon |
| 1744 |
} |
| 1745 |
++ p; // first argument |
| 1746 |
while (*q && *q != U')') { |
| 1747 |
char32 *par, save; |
| 1748 |
static MelderString arg; |
| 1749 |
MelderString_empty (& arg); |
| 1750 |
Melder_skipHorizontalSpace (& p); |
| 1751 |
while (Melder_isHorizontalSpace (*q) || *q == U',' || *q == U')') |
| 1752 |
q ++; |
| 1753 |
par = q; |
| 1754 |
while (*q != U'\0' && ! Melder_isHorizontalSpace (*q) && *q != U',' && *q != U')') |
| 1755 |
q ++; // collect parameter name |
| 1756 |
if (*q) { // does anything follow the parameter name? |
| 1757 |
if (*p == U'\"') { |
| 1758 |
p ++; // skip initial quote |
| 1759 |
while (*p != U'\0') { |
| 1760 |
if (*p == U'\"') { // quote signals end-of-string or string-internal quote |
| 1761 |
if (p [1] == U'\"') { // double quote signals string-internal quote |
| 1762 |
MelderString_appendCharacter (& arg, U'\"'); |
| 1763 |
p += 2; // skip second quote |
| 1764 |
} else { // single quote signals end-of-string |
| 1765 |
break; |
| 1766 |
} |
| 1767 |
} else { |
| 1768 |
MelderString_appendCharacter (& arg, *p ++); |
| 1769 |
} |
| 1770 |
} |
| 1771 |
} else { |
| 1772 |
while (*p != U'\0' && ! Melder_isHorizontalSpace (*p)) |
| 1773 |
MelderString_appendCharacter (& arg, *p ++); // white space separates |
| 1774 |
} |
| 1775 |
if (*p) { |
| 1776 |
*p = U'\0'; |
| 1777 |
p ++; |
| 1778 |
} |
| 1779 |
} else { // else rest of line |
| 1780 |
while (*p != '\0') |
| 1781 |
MelderString_appendCharacter (& arg, *p ++); |
| 1782 |
} |
| 1783 |
if (q [-1] == '$') { |
| 1784 |
save = *q; *q = U'\0'; |
| 1785 |
InterpreterVariable var = Interpreter_lookUpVariable (me, par); *q = save; |
| 1786 |
var -> stringValue = Melder_dup_f (arg.string); |
| 1787 |
} else { |
| 1788 |
double value; |
| 1789 |
my callDepth --; |
| 1790 |
Interpreter_numericExpression (me, arg.string, & value); |
| 1791 |
my callDepth ++; |
| 1792 |
save = *q; *q = U'\0'; |
| 1793 |
InterpreterVariable var = Interpreter_lookUpVariable (me, par); *q = save; |
| 1794 |
var -> numericValue = value; |
| 1795 |
} |
| 1796 |
} |
| 1797 |
} |
| 1798 |
if (callDepth == Interpreter_MAX_CALL_DEPTH) |
| 1799 |
Melder_throw (U"Call depth greater than ", Interpreter_MAX_CALL_DEPTH, U"."); |
| 1800 |
callStack [++ callDepth] = lineNumber; |
| 1801 |
lineNumber = iline; |
| 1802 |
break; |
| 1803 |
} |
| 1804 |
} |
| 1805 |
if (iline > lines.size) |
| 1806 |
Melder_throw (U"Procedure \"", callName, U"\" not found."); |
| 1807 |
} |
| 1808 |
|
| 1809 |
static void assignToNumericVectorElement (Interpreter me, char32 *& p, const char32* vectorName, MelderString& valueString) { |
| 1810 |
integer indexValue = 0; |
| 1811 |
static MelderString index; |
| 1812 |
MelderString_empty (& index); |
| 1813 |
int depth = 0; |
| 1814 |
bool inString = false; |
| 1815 |
while ((depth > 0 || *p != U']' || inString) && Melder_staysWithinLine (*p)) { |
| 1816 |
MelderString_appendCharacter (& index, *p); |
| 1817 |
if (*p == U'[') { |
| 1818 |
if (! inString) |
| 1819 |
depth ++; |
| 1820 |
} else if (*p == U']') { |
| 1821 |
if (! inString) |
| 1822 |
depth --; |
| 1823 |
} |
| 1824 |
if (*p == U'"') |
| 1825 |
inString = ! inString; |
| 1826 |
p ++; |
| 1827 |
} |
| 1828 |
if (! Melder_staysWithinLine (*p)) |
| 1829 |
Melder_throw (U"Missing closing bracket (]) in array element."); |
| 1830 |
Formula_Result result; |
| 1831 |
Interpreter_anyExpression (me, index.string, & result); |
| 1832 |
if (result.expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 1833 |
indexValue = Melder_iround (result. numericResult); |
| 1834 |
} else { |
| 1835 |
Melder_throw (U"Element index should be numeric."); |
| 1836 |
} |
| 1837 |
p ++; // step over closing bracket |
| 1838 |
Melder_skipHorizontalSpace (& p); |
| 1839 |
int assignmentType = 0; |
| 1840 |
if (*p == U'=') { |
| 1841 |
p ++; // step over equals sign |
| 1842 |
} else if (*p == U'+' && p [1] == U'=') { |
| 1843 |
assignmentType = 1; |
| 1844 |
p += 2; // step over plus-gets sign |
| 1845 |
} else if (*p == U'-' && p [1] == U'=') { |
| 1846 |
assignmentType = 2; |
| 1847 |
p += 2; // step over minus-gets sign |
| 1848 |
} else if (*p == U'*' && p [1] == U'=') { |
| 1849 |
assignmentType = 3; |
| 1850 |
p += 2; // step over times-gets sign |
| 1851 |
} else if (*p == U'/' && p [1] == U'=') { |
| 1852 |
assignmentType = 4; |
| 1853 |
p += 2; // step over div-gets sign |
| 1854 |
} else |
| 1855 |
Melder_throw (U"Missing '=', '+=', '-=', '*=' or '/=' after vector element ", vectorName, U" [", index.string, U"]."); |
| 1856 |
Melder_skipHorizontalSpace (& p); // go to first token after assignment |
| 1857 |
if (*p == U'\0') |
| 1858 |
Melder_throw (U"Missing expression after vector element ", vectorName, U" [", index.string, U"]."); |
| 1859 |
double value; |
| 1860 |
if (isCommand (p)) { |
| 1861 |
/* |
| 1862 |
Get the value of the query. |
| 1863 |
*/ |
| 1864 |
MelderString_empty (& valueString); |
| 1865 |
autoMelderDivertInfo divert (& valueString); |
| 1866 |
MelderString_appendCharacter (& valueString, 1); // will be overwritten by something totally different if any MelderInfo function is called... |
| 1867 |
bool status = praat_executeCommand (me, p); |
| 1868 |
if (! status) { |
| 1869 |
value = undefined; |
| 1870 |
} else if (my returnType == kInterpreter_ReturnType::OBJECT_) { |
| 1871 |
integer IOBJECT, selectedObject = 0, numberOfSelectedObjects = 0; |
| 1872 |
WHERE (SELECTED) { |
| 1873 |
selectedObject = IOBJECT; |
| 1874 |
numberOfSelectedObjects += 1; |
| 1875 |
} |
| 1876 |
if (numberOfSelectedObjects > 1) |
| 1877 |
Melder_throw (U"Multiple objects selected. Cannot assign object IDs to vector element. " |
| 1878 |
"Perhaps use a vector variable instead."); |
| 1879 |
if (numberOfSelectedObjects == 0) |
| 1880 |
Melder_throw (U"No objects selected. Cannot assign object ID to vector element."); |
| 1881 |
value = theCurrentPraatObjects -> list [selectedObject]. id; |
| 1882 |
} else { |
| 1883 |
value = Melder_atof (valueString.string); // including --undefined-- |
| 1884 |
} |
| 1885 |
} else { |
| 1886 |
/* |
| 1887 |
Get the value of the formula. |
| 1888 |
*/ |
| 1889 |
Interpreter_numericExpression (me, p, & value); |
| 1890 |
} |
| 1891 |
InterpreterVariable var = Interpreter_hasVariable (me, vectorName); |
| 1892 |
if (! var) |
| 1893 |
Melder_throw (U"Vector ", vectorName, U" does not exist."); |
| 1894 |
/*@praat |
| 1895 |
asserterror Vector interpreter_assignToVector# does not exist. |
| 1896 |
interpreter_assignToVector# [5] = 3 |
| 1897 |
@*/ |
| 1898 |
if (indexValue < 1) |
| 1899 |
Melder_throw (U"A vector index cannot be less than 1 (the index you supplied is ", indexValue, U")."); |
| 1900 |
if (indexValue > var -> numericVectorValue.size) |
| 1901 |
Melder_throw (U"A vector index cannot be greater than the number of elements (here ", |
| 1902 |
var -> numericVectorValue.size, U"). The index you supplied is ", indexValue, U"."); |
| 1903 |
if (assignmentType == 0) |
| 1904 |
var -> numericVectorValue [indexValue] = value; |
| 1905 |
else if (assignmentType == 1) |
| 1906 |
var -> numericVectorValue [indexValue] += value; |
| 1907 |
else if (assignmentType == 2) |
| 1908 |
var -> numericVectorValue [indexValue] -= value; |
| 1909 |
else if (assignmentType == 3) |
| 1910 |
var -> numericVectorValue [indexValue] *= value; |
| 1911 |
else if (assignmentType == 4) |
| 1912 |
var -> numericVectorValue [indexValue] /= value; |
| 1913 |
else |
| 1914 |
Melder_assert (false); |
| 1915 |
} |
| 1916 |
|
| 1917 |
static void assignToNumericMatrixElement (Interpreter me, char32 *& p, const char32* matrixName, MelderString& valueString) { |
| 1918 |
integer rowNumber = 0, columnNumber = 0; |
| 1919 |
/* |
| 1920 |
Get the row number. |
| 1921 |
*/ |
| 1922 |
static MelderString rowFormula; |
| 1923 |
MelderString_empty (& rowFormula); |
| 1924 |
int depth = 0; |
| 1925 |
bool inString = false; |
| 1926 |
while ((depth > 0 || *p != U',' || inString) && Melder_staysWithinLine (*p)) { |
| 1927 |
MelderString_appendCharacter (& rowFormula, *p); |
| 1928 |
if (*p == U'[' || *p == U'(') { |
| 1929 |
if (! inString) |
| 1930 |
depth ++; |
| 1931 |
} else if (*p == U']' || *p == U')') { |
| 1932 |
if (! inString) |
| 1933 |
depth --; |
| 1934 |
} |
| 1935 |
if (*p == U'"') inString = ! inString; |
| 1936 |
p ++; |
| 1937 |
} |
| 1938 |
if (! Melder_staysWithinLine (*p)) |
| 1939 |
Melder_throw (U"Missing comma in matrix indexing."); |
| 1940 |
Formula_Result result; |
| 1941 |
Interpreter_anyExpression (me, rowFormula.string, & result); |
| 1942 |
if (result.expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 1943 |
rowNumber = Melder_iround (result. numericResult); |
| 1944 |
} else { |
| 1945 |
Melder_throw (U"Row number should be numeric."); |
| 1946 |
} |
| 1947 |
p ++; // step over comma |
| 1948 |
/* |
| 1949 |
Get the column number. |
| 1950 |
*/ |
| 1951 |
static MelderString columnFormula; |
| 1952 |
MelderString_empty (& columnFormula); |
| 1953 |
depth = 0; |
| 1954 |
inString = false; |
| 1955 |
while ((depth > 0 || *p != U']' || inString) && Melder_staysWithinLine (*p)) { |
| 1956 |
MelderString_appendCharacter (& columnFormula, *p); |
| 1957 |
if (*p == U'[') { |
| 1958 |
if (! inString) |
| 1959 |
depth ++; |
| 1960 |
} else if (*p == U']') { |
| 1961 |
if (! inString) |
| 1962 |
depth --; |
| 1963 |
} |
| 1964 |
if (*p == U'"') |
| 1965 |
inString = ! inString; |
| 1966 |
p ++; |
| 1967 |
} |
| 1968 |
if (! Melder_staysWithinLine (*p)) |
| 1969 |
Melder_throw (U"Missing closing bracket (]) in matrix indexing."); |
| 1970 |
Interpreter_anyExpression (me, columnFormula.string, & result); |
| 1971 |
if (result.expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 1972 |
columnNumber = Melder_iround (result. numericResult); |
| 1973 |
} else { |
| 1974 |
Melder_throw (U"Column number should be numeric."); |
| 1975 |
} |
| 1976 |
p ++; // step over closing bracket |
| 1977 |
Melder_skipHorizontalSpace (& p); |
| 1978 |
int assignmentType = 0; |
| 1979 |
if (*p == U'=') { |
| 1980 |
p ++; // step over equals sign |
| 1981 |
} else if (*p == U'+' && p [1] == U'=') { |
| 1982 |
assignmentType = 1; |
| 1983 |
p += 2; // step over plus-gets sign |
| 1984 |
} else if (*p == U'-' && p [1] == U'=') { |
| 1985 |
assignmentType = 2; |
| 1986 |
p += 2; // step over minus-gets sign |
| 1987 |
} else if (*p == U'*' && p [1] == U'=') { |
| 1988 |
assignmentType = 3; |
| 1989 |
p += 2; // step over times-gets sign |
| 1990 |
} else if (*p == U'/' && p [1] == U'=') { |
| 1991 |
assignmentType = 4; |
| 1992 |
p += 2; // step over div-gets sign |
| 1993 |
} else |
| 1994 |
Melder_throw (U"Missing '=', '+=', '-=', '*=' or '/=' after matrix element ", matrixName, U" [", |
| 1995 |
rowFormula.string, U",", columnFormula.string, U"]."); |
| 1996 |
Melder_skipHorizontalSpace (&p); // go to first token after assignment |
| 1997 |
if (*p == U'\0') |
| 1998 |
Melder_throw (U"Missing expression after matrix element ", matrixName, U" [", |
| 1999 |
rowFormula.string, U",", columnFormula.string, U"]."); |
| 2000 |
double value; |
| 2001 |
if (isCommand (p)) { |
| 2002 |
/* |
| 2003 |
Get the value of the query. |
| 2004 |
*/ |
| 2005 |
MelderString_empty (& valueString); |
| 2006 |
autoMelderDivertInfo divert (& valueString); |
| 2007 |
MelderString_appendCharacter (& valueString, 1); // will be overwritten by something totally different if any MelderInfo function is called... |
| 2008 |
bool status = praat_executeCommand (me, p); |
| 2009 |
if (! status) { |
| 2010 |
value = undefined; |
| 2011 |
} else if (my returnType == kInterpreter_ReturnType::OBJECT_) { |
| 2012 |
integer IOBJECT, selectedObject = 0, numberOfSelectedObjects = 0; |
| 2013 |
WHERE (SELECTED) { |
| 2014 |
selectedObject = IOBJECT; |
| 2015 |
numberOfSelectedObjects += 1; |
| 2016 |
} |
| 2017 |
if (numberOfSelectedObjects > 1) |
| 2018 |
Melder_throw (U"Multiple objects selected. Cannot assign object IDs to matrix element. " |
| 2019 |
"Perhaps use a vector variable instead."); |
| 2020 |
if (numberOfSelectedObjects == 0) |
| 2021 |
Melder_throw (U"No objects selected. Cannot assign object ID to matrix element."); |
| 2022 |
value = theCurrentPraatObjects -> list [selectedObject]. id; |
| 2023 |
} else { |
| 2024 |
value = Melder_atof (valueString.string); // including --undefined-- |
| 2025 |
} |
| 2026 |
} else { |
| 2027 |
/* |
| 2028 |
Get the value of the formula. |
| 2029 |
*/ |
| 2030 |
Interpreter_numericExpression (me, p, & value); |
| 2031 |
} |
| 2032 |
InterpreterVariable var = Interpreter_hasVariable (me, matrixName); |
| 2033 |
if (! var) |
| 2034 |
Melder_throw (U"Matrix ", matrixName, U" does not exist."); |
| 2035 |
if (rowNumber < 1) |
| 2036 |
Melder_throw (U"A row number cannot be less than 1 (the row number you supplied is ", rowNumber, U")."); |
| 2037 |
if (rowNumber > var -> numericMatrixValue. nrow) |
| 2038 |
Melder_throw (U"A row number cannot be greater than the number of rows (here ", |
| 2039 |
var -> numericMatrixValue. nrow, U"). The row number you supplied is ", rowNumber, U"."); |
| 2040 |
if (columnNumber < 1) |
| 2041 |
Melder_throw (U"A column number cannot be less than 1 (the column number you supplied is ", columnNumber, U")."); |
| 2042 |
if (columnNumber > var -> numericMatrixValue. ncol) |
| 2043 |
Melder_throw (U"A column number cannot be greater than the number of columns (here ", |
| 2044 |
var -> numericMatrixValue. ncol, U"). The column number you supplied is ", columnNumber, U"."); |
| 2045 |
if (assignmentType == 0) |
| 2046 |
var -> numericMatrixValue [rowNumber] [columnNumber] = value; |
| 2047 |
else if (assignmentType == 1) |
| 2048 |
var -> numericMatrixValue [rowNumber] [columnNumber] += value; |
| 2049 |
else if (assignmentType == 2) |
| 2050 |
var -> numericMatrixValue [rowNumber] [columnNumber] -= value; |
| 2051 |
else if (assignmentType == 3) |
| 2052 |
var -> numericMatrixValue [rowNumber] [columnNumber] *= value; |
| 2053 |
else if (assignmentType == 4) |
| 2054 |
var -> numericMatrixValue [rowNumber] [columnNumber] /= value; |
| 2055 |
else |
| 2056 |
Melder_assert (false); |
| 2057 |
} |
| 2058 |
|
| 2059 |
static void assignToStringArrayElement (Interpreter me, char32 *& p, const char32* vectorName, MelderString& valueString) { |
| 2060 |
integer indexValue = 0; |
| 2061 |
static MelderString index; |
| 2062 |
MelderString_empty (& index); |
| 2063 |
int depth = 0; |
| 2064 |
bool inString = false; |
| 2065 |
while ((depth > 0 || *p != U']' || inString) && Melder_staysWithinLine (*p)) { |
| 2066 |
MelderString_appendCharacter (& index, *p); |
| 2067 |
if (*p == U'[') { |
| 2068 |
if (! inString) |
| 2069 |
depth ++; |
| 2070 |
} else if (*p == U']') { |
| 2071 |
if (! inString) |
| 2072 |
depth --; |
| 2073 |
} |
| 2074 |
if (*p == U'"') inString = ! inString; |
| 2075 |
p ++; |
| 2076 |
} |
| 2077 |
if (! Melder_staysWithinLine (*p)) |
| 2078 |
Melder_throw (U"Missing closing bracket (]) in array element."); |
| 2079 |
Formula_Result result; |
| 2080 |
Interpreter_anyExpression (me, index.string, & result); |
| 2081 |
if (result.expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 2082 |
indexValue = Melder_iround (result. numericResult); |
| 2083 |
} else { |
| 2084 |
Melder_throw (U"Element index should be numeric."); |
| 2085 |
} |
| 2086 |
p ++; // step over closing bracket |
| 2087 |
Melder_skipHorizontalSpace (& p); |
| 2088 |
if (*p != U'=') |
| 2089 |
Melder_throw (U"Missing '=' after string vector element ", vectorName, U" [", index.string, U"]."); |
| 2090 |
p ++; // step over equals sign |
| 2091 |
Melder_skipHorizontalSpace (& p); // go to first token after assignment |
| 2092 |
if (*p == U'\0') |
| 2093 |
Melder_throw (U"Missing expression after string vector element ", vectorName, U" [", index.string, U"]."); |
| 2094 |
autostring32 value; |
| 2095 |
if (isCommand (p)) { |
| 2096 |
/* |
| 2097 |
Get the value of the query. |
| 2098 |
*/ |
| 2099 |
MelderString_empty (& valueString); |
| 2100 |
autoMelderDivertInfo divert (& valueString); |
| 2101 |
bool status = praat_executeCommand (me, p); |
| 2102 |
if (! status) |
| 2103 |
value = autostring32(); |
| 2104 |
else |
| 2105 |
value = Melder_dup (valueString.string); |
| 2106 |
} else { |
| 2107 |
/* |
| 2108 |
Get the value of the formula. |
| 2109 |
*/ |
| 2110 |
value = Interpreter_stringExpression (me, p); |
| 2111 |
} |
| 2112 |
InterpreterVariable var = Interpreter_hasVariable (me, vectorName); |
| 2113 |
if (! var) |
| 2114 |
Melder_throw (U"String vector ", vectorName, U" does not exist."); |
| 2115 |
if (indexValue < 1) |
| 2116 |
Melder_throw (U"A vector index cannot be less than 1 (the index you supplied is ", indexValue, U")."); |
| 2117 |
if (indexValue > var -> stringArrayValue.size) |
| 2118 |
Melder_throw (U"A vector index cannot be greater than the number of elements (here ", |
| 2119 |
var -> stringArrayValue.size, U"). The index you supplied is ", indexValue, U"."); |
| 2120 |
var -> stringArrayValue [indexValue] = value. move(); |
| 2121 |
} |
| 2122 |
|
| 2123 |
static void private_Interpreter_initialize (Interpreter me, autostring32 text, const bool reuseVariables) { |
| 2124 |
if (text) |
| 2125 |
my text = text.move(); // otherwise, use my existing text |
| 2126 |
char32 *command = my text.get(); |
| 2127 |
autoMelderString command2; |
| 2128 |
integer numberOfLines = 0; |
| 2129 |
bool atLastLine = false; |
| 2130 |
int chopped = 0; |
| 2131 |
/* |
| 2132 |
Count lines and set the newlines to zero. |
| 2133 |
*/ |
| 2134 |
while (! atLastLine) { |
| 2135 |
char32 *endOfLine = command; |
| 2136 |
while (Melder_staysWithinLine (*endOfLine)) |
| 2137 |
endOfLine ++; |
| 2138 |
if (*endOfLine == U'\0') |
| 2139 |
atLastLine = true; |
| 2140 |
*endOfLine = U'\0'; |
| 2141 |
numberOfLines ++; |
| 2142 |
command = endOfLine + 1; |
| 2143 |
} |
| 2144 |
/* |
| 2145 |
Remember line starts, labels and procedures. |
| 2146 |
*/ |
| 2147 |
my lines. resize (numberOfLines); |
| 2148 |
command = my text.get(); // reset |
| 2149 |
my labelNames. reset(); |
| 2150 |
my labelLines. reset(); |
| 2151 |
my procedureNames. reset(); |
| 2152 |
my procedureStartLines. reset(); |
| 2153 |
for (integer lineNumber = 1; lineNumber <= numberOfLines; lineNumber ++, command += Melder_length (command) + 1 + chopped) { |
| 2154 |
Melder_skipHorizontalSpace (& command); // nbsp can occur for scripts copied from the manual |
| 2155 |
/* |
| 2156 |
Chop trailing spaces? |
| 2157 |
*/ |
| 2158 |
#if 0 |
| 2159 |
chopped = 0; |
| 2160 |
const integer length = Melder_length (command); |
| 2161 |
while (length > 0) { |
| 2162 |
char kar = command [-- length]; |
| 2163 |
if (! Melder_isHorizontalSpace (kar)) |
| 2164 |
break; |
| 2165 |
command [length] = U'\0'; |
| 2166 |
chopped ++; |
| 2167 |
} |
| 2168 |
#endif |
| 2169 |
my lines [lineNumber] = command; |
| 2170 |
if (str32nequ (command, U"label", 5) && Melder_isHorizontalSpace (command [5])) { |
| 2171 |
MelderString_empty (& command2); |
| 2172 |
for (integer ichar = 6; ; ichar ++) { |
| 2173 |
if (Melder_isHorizontalSpace (command [ichar]) || command [ichar] == U';' || command [ichar] == U'\0') |
| 2174 |
break; |
| 2175 |
MelderString_appendCharacter (& command2, command [ichar]); |
| 2176 |
} |
| 2177 |
my labelNames. insert (0, command2.string); |
| 2178 |
my labelLines. insert (0, lineNumber); |
| 2179 |
} |
| 2180 |
if (str32nequ (command, U"procedure", 9) && Melder_isHorizontalSpace (command [9])) { |
| 2181 |
MelderString_empty (& command2); |
| 2182 |
for (integer ichar = 10; ; ichar ++) { |
| 2183 |
if (Melder_isHorizontalSpace (command [ichar]) || command [ichar] == U';' || command [ichar] == U':' || command [ichar] == U'\0') |
| 2184 |
break; |
| 2185 |
MelderString_appendCharacter (& command2, command [ichar]); |
| 2186 |
} |
| 2187 |
my procedureNames. insert (0, command2.string); |
| 2188 |
my procedureStartLines. insert (0, lineNumber); |
| 2189 |
} |
| 2190 |
if (str32nequ (command, U"proc", 4) && Melder_isHorizontalSpace (command [4])) { |
| 2191 |
MelderString_empty (& command2); |
| 2192 |
for (integer ichar = 5; ; ichar ++) { |
| 2193 |
if (Melder_isHorizontalSpace (command [ichar]) || command [ichar] == U';' || command [ichar] == U':' || command [ichar] == U'\0') |
| 2194 |
break; |
| 2195 |
MelderString_appendCharacter (& command2, command [ichar]); |
| 2196 |
} |
| 2197 |
my procedureNames. insert (0, command2.string); |
| 2198 |
my procedureStartLines. insert (0, lineNumber); |
| 2199 |
} |
| 2200 |
} |
| 2201 |
NUMsortTogether (my labelNames.get(), my labelLines.get()); |
| 2202 |
for (integer ilabel = 1; ilabel < my labelNames.size; ilabel ++) |
| 2203 |
if (str32equ (my labelNames [ilabel].get(), my labelNames [ilabel + 1].get())) |
| 2204 |
Melder_throw (U"Duplicate label \"", my labelNames [ilabel].get(), |
| 2205 |
U"\" on lines ", my labelLines [ilabel], U" and ", my labelLines [ilabel + 1], U"."); |
| 2206 |
NUMsortTogether (my procedureNames.get(), my procedureStartLines.get()); |
| 2207 |
for (integer iproc = 1; iproc < my procedureNames.size; iproc ++) |
| 2208 |
if (str32equ (my procedureNames [iproc].get(), my procedureNames [iproc + 1].get())) |
| 2209 |
Melder_warning_once ( |
| 2210 |
U"Duplicate procedure \"", my procedureNames [iproc].get(), |
| 2211 |
U"\" on lines ", my procedureStartLines [iproc], U" and ", my procedureStartLines [iproc + 1], |
| 2212 |
U". This is probably a bug, for which you may want to contact the author of the script. " |
| 2213 |
U"The script will run, but it is unpredictable which of the two procedure definitions will be chosen." |
| 2214 |
); // TURNED INTO WARNING 2026, SCHEDULED FOR ERROR 2036 |
| 2215 |
/* |
| 2216 |
Connect continuation lines. |
| 2217 |
*/ |
| 2218 |
trace (U"connect continuation lines"); |
| 2219 |
for (integer lineNumber = numberOfLines; lineNumber >= 2; lineNumber --) { |
| 2220 |
char32 *line = my lines [lineNumber]; |
| 2221 |
if (line [0] == U'.' && line [1] == U'.' && line [2] == U'.') { |
| 2222 |
char32 *previous = my lines [lineNumber - 1]; |
| 2223 |
MelderString_copy (& command2, line + 3); |
| 2224 |
MelderString_get (& command2, previous + Melder_length (previous)); |
| 2225 |
static char32 emptyLine [] = { U'\0' }; |
| 2226 |
my lines [lineNumber] = emptyLine; |
| 2227 |
} |
| 2228 |
} |
| 2229 |
/* |
| 2230 |
Copy the parameter names and argument values into the array of variables. |
| 2231 |
*/ |
| 2232 |
if (! reuseVariables) { |
| 2233 |
my variablesMap. clear (); |
| 2234 |
for (int ipar = 1; ipar <= my numberOfParameters; ipar ++) { |
| 2235 |
char32 parameter [1+Interpreter_MAX_PARAMETER_LENGTH]; |
| 2236 |
/* |
| 2237 |
Create variable names as-are and variable names without capitals. |
| 2238 |
*/ |
| 2239 |
str32cpy (parameter, my parameters [ipar]); |
| 2240 |
parameterToVariable (me, my types [ipar], parameter, ipar); // deprecated 2014? probably works only with parameter substitution |
| 2241 |
if (parameter [0] >= U'A' && parameter [0] <= U'Z') { |
| 2242 |
parameter [0] = Melder_toLowerCase (parameter [0]); |
| 2243 |
parameterToVariable (me, my types [ipar], parameter, ipar); |
| 2244 |
} |
| 2245 |
} |
| 2246 |
/* |
| 2247 |
Initialize some variables. |
| 2248 |
|
| 2249 |
TODO: turn newline$ and tab$ into constants |
| 2250 |
Time path: |
| 2251 |
1. in 20xx, start asking for trust when assigning to these variables |
| 2252 |
2. ten years later, assigning to these variables becomes an error (with an appropriate warning and recipe for correction), |
| 2253 |
just as with `pi`, `undefined` and `e`. |
| 2254 |
|
| 2255 |
TODO: turn the remainder of these variables into functions |
| 2256 |
Time path: |
| 2257 |
1. in 20xx, add functions to the side |
| 2258 |
2. ten years later, start warning against undefined use of these variables |
| 2259 |
3. another ten years later, undefined us of these variables becomes an error (with an appropriate warning and recipe for correction) |
| 2260 |
*/ |
| 2261 |
Interpreter_addStringVariable (me, U"newline$", U"\n"); |
| 2262 |
Interpreter_addStringVariable (me, U"tab$", U"\t"); |
| 2263 |
Interpreter_addStringVariable (me, U"shellDirectory$", Melder_getShellDirectory ()); |
| 2264 |
{// scope |
| 2265 |
structMelderFolder folder { }; |
| 2266 |
Melder_getCurrentFolder (& folder); |
| 2267 |
Interpreter_addStringVariable (me, U"defaultDirectory$", MelderFolder_peekPath (& folder)); |
| 2268 |
Interpreter_addStringVariable (me, U"preferencesDirectory$", MelderFolder_peekPath (Melder_preferencesFolder())); // settingsFolder$() |
| 2269 |
Melder_getHomeDir (& folder); |
| 2270 |
Interpreter_addStringVariable (me, U"homeDirectory$", MelderFolder_peekPath (& folder)); |
| 2271 |
Melder_getTempDir (& folder); |
| 2272 |
Interpreter_addStringVariable (me, U"temporaryDirectory$", MelderFolder_peekPath (& folder)); |
| 2273 |
} |
| 2274 |
#if defined (macintosh) |
| 2275 |
Interpreter_addNumericVariable (me, U"macintosh", 1); |
| 2276 |
Interpreter_addNumericVariable (me, U"windows", 0); |
| 2277 |
Interpreter_addNumericVariable (me, U"unix", 0); |
| 2278 |
#elif defined (_WIN32) |
| 2279 |
Interpreter_addNumericVariable (me, U"macintosh", 0); |
| 2280 |
Interpreter_addNumericVariable (me, U"windows", 1); |
| 2281 |
Interpreter_addNumericVariable (me, U"unix", 0); |
| 2282 |
#elif defined (UNIX) |
| 2283 |
Interpreter_addNumericVariable (me, U"macintosh", 0); |
| 2284 |
Interpreter_addNumericVariable (me, U"windows", 0); |
| 2285 |
Interpreter_addNumericVariable (me, U"unix", 1); |
| 2286 |
#else |
| 2287 |
Interpreter_addNumericVariable (me, U"macintosh", 0); |
| 2288 |
Interpreter_addNumericVariable (me, U"windows", 0); |
| 2289 |
Interpreter_addNumericVariable (me, U"unix", 0); |
| 2290 |
#endif |
| 2291 |
#if defined (__aarch64__) || defined (_M_ARM64_) |
| 2292 |
Interpreter_addNumericVariable (me, U"praat_intel32", 0); |
| 2293 |
Interpreter_addNumericVariable (me, U"praat_intel64", 0); |
| 2294 |
Interpreter_addNumericVariable (me, U"praat_arm64", 1); |
| 2295 |
Interpreter_addNumericVariable (me, U"praat_s390x", 0); |
| 2296 |
Interpreter_addNumericVariable (me, U"praat_armv7", 0); |
| 2297 |
#elif defined (__s390x__) |
| 2298 |
Interpreter_addNumericVariable (me, U"praat_intel32", 0); |
| 2299 |
Interpreter_addNumericVariable (me, U"praat_intel64", 0); |
| 2300 |
Interpreter_addNumericVariable (me, U"praat_arm64", 0); |
| 2301 |
Interpreter_addNumericVariable (me, U"praat_s390x", 1); |
| 2302 |
Interpreter_addNumericVariable (me, U"praat_armv7", 0); |
| 2303 |
#elif defined (raspberrypi) |
| 2304 |
Interpreter_addNumericVariable (me, U"praat_intel32", 0); |
| 2305 |
Interpreter_addNumericVariable (me, U"praat_intel64", 0); |
| 2306 |
Interpreter_addNumericVariable (me, U"praat_arm64", 0); |
| 2307 |
Interpreter_addNumericVariable (me, U"praat_s390x", 0); |
| 2308 |
Interpreter_addNumericVariable (me, U"praat_armv7", 1); |
| 2309 |
#else |
| 2310 |
Interpreter_addNumericVariable (me, U"praat_intel32", sizeof (void *) == 4); |
| 2311 |
Interpreter_addNumericVariable (me, U"praat_intel64", sizeof (void *) == 8); |
| 2312 |
Interpreter_addNumericVariable (me, U"praat_arm64", 0); |
| 2313 |
Interpreter_addNumericVariable (me, U"praat_s390x", 0); |
| 2314 |
Interpreter_addNumericVariable (me, U"praat_armv7", 0); |
| 2315 |
#endif |
| 2316 |
Interpreter_addNumericVariable (me, U"praat_32bit", sizeof (void *) == 4); |
| 2317 |
Interpreter_addNumericVariable (me, U"praat_64bit", sizeof (void *) == 8); |
| 2318 |
Interpreter_addNumericVariable (me, U"left", 1); // deprecated 2010 (Praat 5.2.06) |
| 2319 |
Interpreter_addNumericVariable (me, U"right", 2); // deprecated 2010 (Praat 5.2.06) |
| 2320 |
Interpreter_addNumericVariable (me, U"mono", 1); // deprecated 2010 (Praat 5.2.06) |
| 2321 |
Interpreter_addNumericVariable (me, U"stereo", 2); // deprecated 2010 (Praat 5.2.06) |
| 2322 |
Interpreter_addNumericVariable (me, U"all", 0); // deprecated 2010 (Praat 5.2.06) |
| 2323 |
Interpreter_addNumericVariable (me, U"average", 0); // deprecated 2010 (Praat 5.2.06) |
| 2324 |
Interpreter_addStringVariable (me, U"praatVersion$", Melder_appVersionSTR()); |
| 2325 |
Interpreter_addNumericVariable (me, U"praatVersion", Melder_appVersion()); |
| 2326 |
} |
| 2327 |
} |
| 2328 |
|
| 2329 |
void Interpreter_run (Interpreter me, autostring32 text, const bool reuseVariables) { |
| 2330 |
try { |
| 2331 |
//TRACE |
| 2332 |
trace (U"enter"); |
| 2333 |
private_Interpreter_initialize (me, text.move(), reuseVariables); |
| 2334 |
my lineNumber = 1; |
| 2335 |
my callDepth = 0; |
| 2336 |
my fromif = false; |
| 2337 |
my fromendfor = false; |
| 2338 |
my assertErrorLineNumber = 0; |
| 2339 |
my setDynamicFromOwningEditorEnvironment (); |
| 2340 |
Interpreter_resume (me); |
| 2341 |
} catch (MelderError) { |
| 2342 |
throw; |
| 2343 |
} |
| 2344 |
} |
| 2345 |
|
| 2346 |
void Interpreter_resume (Interpreter me) { |
| 2347 |
/* |
| 2348 |
Start. |
| 2349 |
*/ |
| 2350 |
my running = true; |
| 2351 |
my stopped = false; |
| 2352 |
my isHalted = false; |
| 2353 |
bool assertionFailed = false; |
| 2354 |
//TRACE |
| 2355 |
trace (U"resuming at line ", my lineNumber, U" in file ", & my file); |
| 2356 |
try { |
| 2357 |
static MelderString valueString; // to divert the info |
| 2358 |
autoMelderString command2; |
| 2359 |
autoMelderString buffer; |
| 2360 |
int callDepth = my callDepth; |
| 2361 |
const integer numberOfLines = my lines.size; |
| 2362 |
/* |
| 2363 |
Execute commands. |
| 2364 |
*/ |
| 2365 |
trace (U"going to handle ", numberOfLines, U" lines"); |
| 2366 |
//for (integer lineNumber = 1; lineNumber <= numberOfLines; lineNumber ++) { |
| 2367 |
//trace (U"line ", lineNumber, U": ", lines [lineNumber]); |
| 2368 |
//} |
| 2369 |
for (; my lineNumber <= numberOfLines; my lineNumber ++) { |
| 2370 |
trace (U"considering to handle line ", my lineNumber, U": ", my lines [my lineNumber], U", stopped ", my stopped, U", halted ", my isHalted, U", pass ", 1 + my isInSecondPass); |
| 2371 |
if (my stopped) |
| 2372 |
break; |
| 2373 |
if (my isHalted) |
| 2374 |
break; |
| 2375 |
trace (U"going to handle line ", my lineNumber, U": ", my lines [my lineNumber]); |
| 2376 |
constvector <mutablestring32> lines = my lines.get(); |
| 2377 |
try { |
| 2378 |
char32 c0; |
| 2379 |
bool fail = false; |
| 2380 |
MelderString_copy (& command2, lines [my lineNumber]); |
| 2381 |
c0 = command2.string [0]; |
| 2382 |
if (c0 == U'\0') |
| 2383 |
continue; |
| 2384 |
/* |
| 2385 |
Substitute variables. |
| 2386 |
*/ |
| 2387 |
trace (U"preprocessing line ", my lineNumber, U": ", command2.string); |
| 2388 |
for (char32 *p = & command2.string [0]; *p != U'\0'; p ++) if (*p == U'\'') { |
| 2389 |
/* |
| 2390 |
Found a left quote. Search for a matching right quote. |
| 2391 |
*/ |
| 2392 |
char32 *q = p + 1, varName [300], *r, *s; |
| 2393 |
integer precision = -1; |
| 2394 |
bool percent = false; |
| 2395 |
while (*q != U'\0' && *q != U'\'' && q - p < 299) |
| 2396 |
q ++; |
| 2397 |
if (*q == U'\0') |
| 2398 |
break; // no matching right quote? done with this line! |
| 2399 |
if (q - p == 1 || q - p >= 299) |
| 2400 |
continue; // ignore empty and too long variable names |
| 2401 |
trace (U"found ", q - p - 1); |
| 2402 |
/* |
| 2403 |
Found a right quote. Get potential variable name. |
| 2404 |
*/ |
| 2405 |
for (r = p + 1, s = varName; q - r > 0; r ++, s ++) |
| 2406 |
*s = *r; |
| 2407 |
*s = U'\0'; // trailing null byte |
| 2408 |
char32 *colon = str32chr (varName, U':'); |
| 2409 |
if (colon) { |
| 2410 |
precision = Melder_atoi (colon + 1); |
| 2411 |
if (str32chr (colon + 1, U'%')) |
| 2412 |
percent = true; |
| 2413 |
*colon = U'\0'; |
| 2414 |
} |
| 2415 |
InterpreterVariable var = Interpreter_hasVariable (me, varName); |
| 2416 |
if (var) { |
| 2417 |
/* |
| 2418 |
Found a variable (p points to the left quote, q to the right quote). Substitute. |
| 2419 |
*/ |
| 2420 |
const integer headlen = p - command2.string; |
| 2421 |
conststring32 string = ( var -> stringValue ? var -> stringValue.get() : |
| 2422 |
percent ? Melder_percent (var -> numericValue, precision) : |
| 2423 |
precision >= 0 ? Melder_fixed (var -> numericValue, precision) : |
| 2424 |
Melder_double (var -> numericValue) ); |
| 2425 |
const integer arglen = Melder_length (string); |
| 2426 |
MelderString_ncopy (& buffer, command2.string, headlen); |
| 2427 |
MelderString_append (& buffer, string, q + 1); |
| 2428 |
MelderString_copy (& command2, buffer.string); // This invalidates p!! (really bad bug 20070203) |
| 2429 |
p = command2.string + headlen + arglen - 1; |
| 2430 |
} else { |
| 2431 |
p = q - 1; // go to before next quote |
| 2432 |
} |
| 2433 |
} |
| 2434 |
trace (U"going to parse line ", my lineNumber, U": ", command2.string); |
| 2435 |
c0 = command2.string [0]; // resume in order to allow things like 'c$' = 5 |
| 2436 |
if ((! Melder_isLetter (c0) || Melder_isUpperCaseLetter (c0)) && c0 != U'@' && |
| 2437 |
! (c0 == U'.' && Melder_isLetter (command2.string [1]) && ! Melder_isUpperCaseLetter (command2.string [1]))) |
| 2438 |
{ |
| 2439 |
(void) praat_executeCommand (me, command2.string); |
| 2440 |
/* |
| 2441 |
Interpret control flow and variables. |
| 2442 |
*/ |
| 2443 |
} else switch (c0) { |
| 2444 |
case U'.': |
| 2445 |
fail = true; |
| 2446 |
break; |
| 2447 |
case U'@': |
| 2448 |
Interpreter_do_procedureCall (me, command2.string + 1, lines, my lineNumber, my callStack, callDepth); |
| 2449 |
break; |
| 2450 |
case U'a': |
| 2451 |
if (str32nequ (command2.string, U"assert ", 7)) { |
| 2452 |
double value; |
| 2453 |
Interpreter_numericExpression (me, command2.string + 7, & value); |
| 2454 |
if (value == 0.0 || isundef (value)) { |
| 2455 |
assertionFailed = true; |
| 2456 |
Melder_throw (U"Script assertion fails in line ", my lineNumber, |
| 2457 |
U" (", value == 0.0 ? U"false" : U"undefined", U"):\n ", command2.string + 7); |
| 2458 |
} |
| 2459 |
} else if (str32nequ (command2.string, U"asserterror ", 12)) { |
| 2460 |
MelderString_copy (& my assertErrorString, command2.string + 12); |
| 2461 |
//TRACE |
| 2462 |
trace (U"assert error string: <<", my assertErrorString.string, U">>"); |
| 2463 |
my assertErrorLineNumber = my lineNumber; |
| 2464 |
} else |
| 2465 |
fail = true; |
| 2466 |
break; |
| 2467 |
case U'b': |
| 2468 |
fail = true; |
| 2469 |
break; |
| 2470 |
case U'c': |
| 2471 |
if (str32nequ (command2.string, U"call ", 5)) { |
| 2472 |
Interpreter_do_oldProcedureCall (me, command2.string + 5, lines, my lineNumber, my callStack, callDepth); |
| 2473 |
} else |
| 2474 |
fail = true; |
| 2475 |
break; |
| 2476 |
case U'd': |
| 2477 |
if (str32nequ (command2.string, U"dec ", 4)) { |
| 2478 |
InterpreterVariable var = Interpreter_lookUpVariable (me, command2.string + 4); |
| 2479 |
var -> numericValue -= 1.0; |
| 2480 |
} else |
| 2481 |
fail = true; |
| 2482 |
break; |
| 2483 |
case U'e': |
| 2484 |
if (command2.string [1] == U'n' && command2.string [2] == U'd') { |
| 2485 |
if (str32nequ (command2.string, U"endif", 5) && |
| 2486 |
(! Melder_staysWithinInk (command2.string [5]) || command2.string [5] == U';')) |
| 2487 |
{ |
| 2488 |
const char32 *startOfInk = Melder_findInk (command2.string + 5); |
| 2489 |
if (startOfInk && *startOfInk != U';') |
| 2490 |
Melder_throw (U"Stray text after 'endif'."); |
| 2491 |
/* Ignore. */ |
| 2492 |
} else if (str32nequ (command2.string, U"endfor", 6) && |
| 2493 |
(! Melder_staysWithinInk (command2.string [6]) || command2.string [6] == U';')) |
| 2494 |
{ |
| 2495 |
const char32 *startOfInk = Melder_findInk (command2.string + 6); |
| 2496 |
if (startOfInk && *startOfInk != U';') |
| 2497 |
Melder_throw (U"Stray text after 'endfor'."); |
| 2498 |
int depth = 0; |
| 2499 |
integer iline; |
| 2500 |
for (iline = my lineNumber - 1; iline > 0; iline --) { |
| 2501 |
char32 *line = lines [iline]; |
| 2502 |
if (line [0] == U'f' && line [1] == U'o' && line [2] == U'r' && line [3] == U' ') { |
| 2503 |
if (depth == 0) { my lineNumber = iline - 1; my fromendfor = true; break; } // go before 'for' |
| 2504 |
else depth --; |
| 2505 |
} else if (str32nequ (lines [iline], U"endfor", 6) && |
| 2506 |
(! Melder_staysWithinInk (lines [iline] [6]) || lines [iline] [6] == U';')) |
| 2507 |
{ |
| 2508 |
depth ++; |
| 2509 |
} |
| 2510 |
} |
| 2511 |
if (iline <= 0) Melder_throw (U"Unmatched 'endfor'."); |
| 2512 |
} else if (str32nequ (command2.string, U"endwhile", 8) && |
| 2513 |
(! Melder_staysWithinInk (command2.string [8]) || command2.string [8] == U';')) |
| 2514 |
{ |
| 2515 |
const char32 *startOfInk = Melder_findInk (command2.string + 8); |
| 2516 |
if (startOfInk && *startOfInk != U';') |
| 2517 |
Melder_throw (U"Stray text after 'endwhile'."); |
| 2518 |
int depth = 0; |
| 2519 |
integer iline; |
| 2520 |
for (iline = my lineNumber - 1; iline > 0; iline --) { |
| 2521 |
if (str32nequ (lines [iline], U"while ", 6)) { |
| 2522 |
if (depth == 0) { |
| 2523 |
my lineNumber = iline - 1; |
| 2524 |
break; // go before 'while' |
| 2525 |
} else |
| 2526 |
depth --; |
| 2527 |
} else if (str32nequ (lines [iline], U"endwhile", 8) && |
| 2528 |
(! Melder_staysWithinInk (lines [iline] [8]) || lines [iline] [8] == U';')) |
| 2529 |
{ |
| 2530 |
depth ++; |
| 2531 |
} |
| 2532 |
} |
| 2533 |
if (iline <= 0) Melder_throw (U"Unmatched 'endwhile'."); |
| 2534 |
} else if (str32nequ (command2.string, U"endproc", 7) && |
| 2535 |
(! Melder_staysWithinInk (command2.string [7]) || command2.string [7] == U';')) |
| 2536 |
{ |
| 2537 |
const char32 *startOfInk = Melder_findInk (command2.string + 7); |
| 2538 |
if (startOfInk && *startOfInk != U';') |
| 2539 |
Melder_throw (U"Stray text after 'endproc'."); |
| 2540 |
if (callDepth == 0) |
| 2541 |
Melder_throw (U"Unmatched 'endproc'."); |
| 2542 |
my lineNumber = my callStack [callDepth --]; |
| 2543 |
-- my callDepth; |
| 2544 |
} else fail = true; |
| 2545 |
} else if (str32nequ (command2.string, U"else", 4) && |
| 2546 |
(! Melder_staysWithinInk (command2.string [4]) || command2.string [4] == U';')) |
| 2547 |
{ |
| 2548 |
const char32 *startOfInk = Melder_findInk (command2.string + 4); |
| 2549 |
if (startOfInk && *startOfInk != U';') |
| 2550 |
Melder_throw (U"Stray text after 'else'."); |
| 2551 |
int depth = 0; |
| 2552 |
integer iline; |
| 2553 |
for (iline = my lineNumber + 1; iline <= numberOfLines; iline ++) { |
| 2554 |
if (str32nequ (lines [iline], U"endif", 5) && |
| 2555 |
(! Melder_staysWithinInk (lines [iline] [5]) || lines [iline] [5] == U';')) |
| 2556 |
{ |
| 2557 |
startOfInk = Melder_findInk (lines [iline] + 5); |
| 2558 |
if (startOfInk && *startOfInk != U';') { |
| 2559 |
my lineNumber = iline; // on behalf of the error message |
| 2560 |
Melder_throw (U"Stray text after 'endif'."); |
| 2561 |
} |
| 2562 |
if (depth == 0) { my lineNumber = iline; break; } // go after `endif` |
| 2563 |
else depth --; |
| 2564 |
} else if (str32nequ (lines [iline], U"if ", 3)) { |
| 2565 |
depth ++; |
| 2566 |
} |
| 2567 |
} |
| 2568 |
if (iline > numberOfLines) |
| 2569 |
Melder_throw (U"Unmatched 'else'."); |
| 2570 |
} else if (str32nequ (command2.string, U"elsif ", 6) || str32nequ (command2.string, U"elif ", 5)) { |
| 2571 |
if (my fromif) { |
| 2572 |
double value; |
| 2573 |
my fromif = false; |
| 2574 |
Interpreter_numericExpression (me, command2.string + 5, & value); |
| 2575 |
if (value == 0.0) { |
| 2576 |
int depth = 0; |
| 2577 |
integer iline; |
| 2578 |
for (iline = my lineNumber + 1; iline <= numberOfLines; iline ++) { |
| 2579 |
if (str32nequ (lines [iline], U"endif", 5) && |
| 2580 |
(! Melder_staysWithinInk (lines [iline] [5]) || lines [iline] [5] == U';')) |
| 2581 |
{ |
| 2582 |
const char32 *startOfInk = Melder_findInk (lines [iline] + 5); |
| 2583 |
if (startOfInk && *startOfInk != U';') { |
| 2584 |
my lineNumber = iline; // on behalf of error message |
| 2585 |
Melder_throw (U"Stray text after 'endif'."); |
| 2586 |
} |
| 2587 |
if (depth == 0) { |
| 2588 |
my lineNumber = iline; |
| 2589 |
break; // go after `endif` |
| 2590 |
} else |
| 2591 |
depth --; |
| 2592 |
} else if (str32nequ (lines [iline], U"else", 4) && |
| 2593 |
(! Melder_staysWithinInk (lines [iline] [4]) || lines [iline] [4] == U';')) |
| 2594 |
{ |
| 2595 |
const char32 *startOfInk = Melder_findInk (lines [iline] + 4); |
| 2596 |
if (startOfInk && *startOfInk != U';') { |
| 2597 |
my lineNumber = iline; // on behalf of error message |
| 2598 |
Melder_throw (U"Stray text after 'else'."); |
| 2599 |
} |
| 2600 |
if (depth == 0) { |
| 2601 |
my lineNumber = iline; |
| 2602 |
break; // go after `else` |
| 2603 |
} |
| 2604 |
} else if ((str32nequ (lines [iline], U"elsif", 5) && ! Melder_staysWithinInk (lines [iline] [5])) |
| 2605 |
|| (str32nequ (lines [iline], U"elif", 4) && ! Melder_staysWithinInk (lines [iline] [4]))) { |
| 2606 |
if (depth == 0) { |
| 2607 |
my lineNumber = iline - 1; |
| 2608 |
my fromif = true; |
| 2609 |
break; // go at next 'elsif' or 'elif' |
| 2610 |
} |
| 2611 |
} else if (str32nequ (lines [iline], U"if ", 3)) { |
| 2612 |
depth ++; |
| 2613 |
} |
| 2614 |
} |
| 2615 |
if (iline > numberOfLines) |
| 2616 |
Melder_throw (U"Unmatched 'elsif'."); |
| 2617 |
} |
| 2618 |
} else { |
| 2619 |
int depth = 0; |
| 2620 |
integer iline; |
| 2621 |
for (iline = my lineNumber + 1; iline <= numberOfLines; iline ++) { |
| 2622 |
if (str32nequ (lines [iline], U"endif", 5) && |
| 2623 |
(! Melder_staysWithinInk (lines [iline] [5]) || lines [iline] [5] == U';')) |
| 2624 |
{ |
| 2625 |
const char32 *startOfInk = Melder_findInk (lines [iline] + 5); |
| 2626 |
if (startOfInk && *startOfInk != U';') { |
| 2627 |
my lineNumber = iline; // on behalf of error message |
| 2628 |
Melder_throw (U"Stray text after 'endif'."); |
| 2629 |
} |
| 2630 |
if (depth == 0) { |
| 2631 |
my lineNumber = iline; |
| 2632 |
break; // go after `endif` |
| 2633 |
} else |
| 2634 |
depth --; |
| 2635 |
} else if (str32nequ (lines [iline], U"if ", 3)) { |
| 2636 |
depth ++; |
| 2637 |
} |
| 2638 |
} |
| 2639 |
if (iline > numberOfLines) |
| 2640 |
Melder_throw (U"'elsif' not matched with 'endif'."); |
| 2641 |
} |
| 2642 |
} else if (str32nequ (command2.string, U"exit", 4)) { |
| 2643 |
if (command2.string [4] == U'\0') { |
| 2644 |
my lineNumber = numberOfLines; // go after end |
| 2645 |
} else if (command2.string [4] == U' ') { |
| 2646 |
Melder_throw (command2.string + 5); |
| 2647 |
} else |
| 2648 |
fail = true; |
| 2649 |
} else if (str32nequ (command2.string, U"echo ", 5)) { |
| 2650 |
/* |
| 2651 |
Make sure that lines like "echo = 3" will not be regarded as assignments. |
| 2652 |
*/ |
| 2653 |
(void) praat_executeCommand (me, command2.string); |
| 2654 |
} else |
| 2655 |
fail = true; |
| 2656 |
break; |
| 2657 |
case U'f': |
| 2658 |
if (command2.string [1] == U'o' && command2.string [2] == U'r' && command2.string [3] == U' ') { // for_ |
| 2659 |
double toValue, loopVariable; |
| 2660 |
char32 *frompos = str32str (command2.string, U" from "), *topos = str32str (command2.string, U" to "); |
| 2661 |
char32 *varpos = command2.string + 4, *endvar = frompos; |
| 2662 |
if (! topos) |
| 2663 |
Melder_throw (U"Missing \'to\' in \'for\' loop."); |
| 2664 |
if (! endvar) |
| 2665 |
endvar = topos; |
| 2666 |
while (*endvar == U' ') { |
| 2667 |
*endvar = U'\0'; |
| 2668 |
endvar --; |
| 2669 |
} |
| 2670 |
while (*varpos == U' ') |
| 2671 |
varpos ++; |
| 2672 |
if (endvar - varpos < 0) |
| 2673 |
Melder_throw (U"Missing loop variable after \'for\'."); |
| 2674 |
InterpreterVariable var = Interpreter_lookUpVariable (me, varpos); |
| 2675 |
Interpreter_numericExpression (me, topos + 4, & toValue); |
| 2676 |
if (my fromendfor) { |
| 2677 |
my fromendfor = false; |
| 2678 |
loopVariable = var -> numericValue + 1.0; |
| 2679 |
} else if (frompos) { |
| 2680 |
*topos = U'\0'; |
| 2681 |
Interpreter_numericExpression (me, frompos + 6, & loopVariable); |
| 2682 |
} else { |
| 2683 |
loopVariable = 1.0; |
| 2684 |
} |
| 2685 |
var -> numericValue = loopVariable; |
| 2686 |
if (loopVariable > toValue) { |
| 2687 |
int depth = 0; |
| 2688 |
integer iline; |
| 2689 |
for (iline = my lineNumber + 1; iline <= numberOfLines; iline ++) { |
| 2690 |
if (str32nequ (lines [iline], U"endfor", 6) && |
| 2691 |
(! Melder_staysWithinInk (lines [iline] [6]) || lines [iline] [6] == U';')) |
| 2692 |
{ |
| 2693 |
const char32 *startOfInk = Melder_findInk (lines [iline] + 6); |
| 2694 |
if (startOfInk && *startOfInk != U';') { |
| 2695 |
my lineNumber = iline; // on behalf of error message |
| 2696 |
Melder_throw (U"Stray text after 'endfor'."); |
| 2697 |
} |
| 2698 |
if (depth == 0) { |
| 2699 |
my lineNumber = iline; |
| 2700 |
break; // go after 'endfor' |
| 2701 |
} else |
| 2702 |
depth --; |
| 2703 |
} else if (str32nequ (lines [iline], U"for ", 4)) { |
| 2704 |
depth ++; |
| 2705 |
} |
| 2706 |
} |
| 2707 |
if (iline > numberOfLines) |
| 2708 |
Melder_throw (U"Unmatched 'for' (matching 'endfor' not found)."); |
| 2709 |
} |
| 2710 |
} else if (str32nequ (command2.string, U"form", 4) && |
| 2711 |
(command2.string [4] == U':' || Melder_isEndOfInk (command2.string [4]))) |
| 2712 |
{ |
| 2713 |
/* |
| 2714 |
We encountered a `form` statement. |
| 2715 |
The contents of everything between `form` and `endform` |
| 2716 |
is irrelevant at this point (the parameters have been read earlier), |
| 2717 |
so we will skip the whole contents of the form and jump past `endform`. |
| 2718 |
*/ |
| 2719 |
integer iline; |
| 2720 |
for (iline = my lineNumber + 1; iline <= numberOfLines; iline ++) |
| 2721 |
if (str32nequ (lines [iline], U"endform", 7) && Melder_isEndOfInk (lines [iline] [7])) { |
| 2722 |
my lineNumber = iline; |
| 2723 |
break; // go after 'endform' |
| 2724 |
} |
| 2725 |
if (iline > numberOfLines) |
| 2726 |
/* |
| 2727 |
We will probably never arrive here, because a missing `endform` |
| 2728 |
will probably have been detected by Interpreter_readParameters(), |
| 2729 |
either with "Unknown parameter type inside form:\nxxx" |
| 2730 |
or with "Unfinished form (missing “endform”)." |
| 2731 |
but we like to stay on the safe side. (last checked 2023-01-20) |
| 2732 |
*/ |
| 2733 |
Melder_throw (U"Unmatched 'form' (matching 'endform' not found)."); |
| 2734 |
} else |
| 2735 |
fail = true; |
| 2736 |
break; |
| 2737 |
case U'g': |
| 2738 |
if (str32nequ (command2.string, U"goto ", 5)) { |
| 2739 |
char32 labelName [1+Interpreter_MAX_LABEL_LENGTH]; |
| 2740 |
str32ncpy (labelName, command2.string + 5, Interpreter_MAX_LABEL_LENGTH); |
| 2741 |
labelName [Interpreter_MAX_LABEL_LENGTH] = U'\0'; |
| 2742 |
char32 *space = str32chr (labelName, U' '); |
| 2743 |
if (space == labelName) |
| 2744 |
Melder_throw (U"Missing label name after 'goto'."); |
| 2745 |
bool dojump = true; |
| 2746 |
if (space) { |
| 2747 |
double value; |
| 2748 |
*space = '\0'; |
| 2749 |
Interpreter_numericExpression (me, command2.string + 6 + Melder_length (labelName), & value); |
| 2750 |
if (value == 0.0) |
| 2751 |
dojump = false; |
| 2752 |
} |
| 2753 |
if (dojump) { |
| 2754 |
integer ilabel = lookupLabel (me, labelName); |
| 2755 |
my lineNumber = my labelLines [ilabel]; // loop will add 1 |
| 2756 |
} |
| 2757 |
} else |
| 2758 |
fail = true; |
| 2759 |
break; |
| 2760 |
case U'h': |
| 2761 |
fail = true; |
| 2762 |
break; |
| 2763 |
case U'i': |
| 2764 |
if (command2.string [1] == U'f' && Melder_isHorizontalSpace (command2.string [2])) { // if_ |
| 2765 |
double value; |
| 2766 |
Interpreter_numericExpression (me, command2.string + 3, & value); |
| 2767 |
if (value == 0.0) { |
| 2768 |
int depth = 0; |
| 2769 |
integer iline; |
| 2770 |
for (iline = my lineNumber + 1; iline <= numberOfLines; iline ++) { |
| 2771 |
if (str32nequ (lines [iline], U"endif", 5) && |
| 2772 |
(! Melder_staysWithinInk (lines [iline] [5]) || lines [iline] [5] == U';')) |
| 2773 |
{ |
| 2774 |
const char32 *startOfInk = Melder_findInk (lines [iline] + 5); |
| 2775 |
if (startOfInk && *startOfInk != U';') { |
| 2776 |
my lineNumber = iline; // on behalf of error message |
| 2777 |
Melder_throw (U"Stray text after 'endif'."); |
| 2778 |
} |
| 2779 |
if (depth == 0) { |
| 2780 |
my lineNumber = iline; |
| 2781 |
break; // go after 'endif' |
| 2782 |
} else |
| 2783 |
depth --; |
| 2784 |
} else if (str32nequ (lines [iline], U"else", 4) && |
| 2785 |
(! Melder_staysWithinInk (lines [iline] [4]) || lines [iline] [4] == U';')) |
| 2786 |
{ |
| 2787 |
const char32 *startOfInk = Melder_findInk (lines [iline] + 4); |
| 2788 |
if (startOfInk && *startOfInk != U';') { |
| 2789 |
my lineNumber = iline; // on behalf of error message |
| 2790 |
Melder_throw (U"Stray text after 'else'."); |
| 2791 |
} |
| 2792 |
if (depth == 0) { |
| 2793 |
my lineNumber = iline; |
| 2794 |
break; // go after 'else' |
| 2795 |
} |
| 2796 |
} else if (str32nequ (lines [iline], U"elsif ", 6) || str32nequ (lines [iline], U"elif ", 5)) { |
| 2797 |
if (depth == 0) { |
| 2798 |
my lineNumber = iline - 1; |
| 2799 |
my fromif = true; |
| 2800 |
break; // go at 'elsif' |
| 2801 |
} |
| 2802 |
} else if (str32nequ (lines [iline], U"if ", 3)) { |
| 2803 |
depth ++; |
| 2804 |
} |
| 2805 |
} |
| 2806 |
if (iline > numberOfLines) |
| 2807 |
Melder_throw (U"Unmatched 'if'."); |
| 2808 |
} else if (isundef (value)) { |
| 2809 |
Melder_throw (U"The value of the 'if' condition is undefined."); |
| 2810 |
} |
| 2811 |
} else if (str32nequ (command2.string, U"inc ", 4)) { |
| 2812 |
InterpreterVariable var = Interpreter_lookUpVariable (me, command2.string + 4); |
| 2813 |
var -> numericValue += 1.0; |
| 2814 |
} else |
| 2815 |
fail = true; |
| 2816 |
break; |
| 2817 |
case U'j': |
| 2818 |
fail = true; |
| 2819 |
break; |
| 2820 |
case U'k': |
| 2821 |
fail = true; |
| 2822 |
break; |
| 2823 |
case U'l': |
| 2824 |
if (str32nequ (command2.string, U"label ", 6)) { |
| 2825 |
if (command2.string [6] == U'=') |
| 2826 |
Melder_throw (U"\"label\" cannot be used as a variable name."); |
| 2827 |
; // ignore labels |
| 2828 |
} else |
| 2829 |
fail = true; |
| 2830 |
break; |
| 2831 |
case U'm': |
| 2832 |
fail = true; |
| 2833 |
break; |
| 2834 |
case U'n': |
| 2835 |
fail = true; |
| 2836 |
break; |
| 2837 |
case U'o': |
| 2838 |
fail = true; |
| 2839 |
break; |
| 2840 |
case U'p': |
| 2841 |
if (str32nequ (command2.string, U"procedure", 9) && Melder_isHorizontalSpace (command2.string [9])) { |
| 2842 |
integer iline = my lineNumber + 1; |
| 2843 |
for (; iline <= numberOfLines; iline ++) { |
| 2844 |
if (str32nequ (lines [iline], U"endproc", 7) && |
| 2845 |
(! Melder_staysWithinInk (lines [iline] [7]) || lines [iline] [7] == U';')) |
| 2846 |
{ |
| 2847 |
const char32 *startOfInk = Melder_findInk (lines [iline] + 7); |
| 2848 |
if (startOfInk && *startOfInk != U';') { |
| 2849 |
my lineNumber = iline; // on behalf of error message |
| 2850 |
Melder_throw (U"Stray text after 'endproc'."); |
| 2851 |
} |
| 2852 |
my lineNumber = iline; |
| 2853 |
break; |
| 2854 |
} // go after `endproc` |
| 2855 |
} |
| 2856 |
if (iline > numberOfLines) Melder_throw (U"Unmatched 'procedure'."); |
| 2857 |
} else if (str32nequ (command2.string, U"print", 5)) { |
| 2858 |
/* |
| 2859 |
* Make sure that lines like "print = 3" will not be regarded as assignments. |
| 2860 |
*/ |
| 2861 |
if (command2.string [5] == U' ' || (str32nequ (command2.string + 5, U"line", 4) && (command2.string [9] == U' ' || command2.string [9] == U'\0'))) { |
| 2862 |
(void) praat_executeCommand (me, command2.string); |
| 2863 |
} else |
| 2864 |
fail = true; |
| 2865 |
} else |
| 2866 |
fail = true; |
| 2867 |
break; |
| 2868 |
case U'q': |
| 2869 |
fail = true; |
| 2870 |
break; |
| 2871 |
case U'r': |
| 2872 |
if (str32nequ (command2.string, U"repeat", 6) && |
| 2873 |
(! Melder_staysWithinInk (command2.string [6]) || command2.string [6] == U';')) |
| 2874 |
{ |
| 2875 |
const char32 *startOfInk = Melder_findInk (command2.string + 6); |
| 2876 |
if (startOfInk && *startOfInk != U';') |
| 2877 |
Melder_throw (U"Stray text after 'repeat'."); |
| 2878 |
/* Ignore. */ |
| 2879 |
} else |
| 2880 |
fail = true; |
| 2881 |
break; |
| 2882 |
case U's': |
| 2883 |
if (str32nequ (command2.string, U"stopwatch", 9) && ! Melder_staysWithinInk (command2.string [9])) { |
| 2884 |
(void) Melder_stopwatch (); // reset stopwatch |
| 2885 |
} else |
| 2886 |
fail = true; |
| 2887 |
break; |
| 2888 |
case U't': |
| 2889 |
fail = true; |
| 2890 |
break; |
| 2891 |
case U'u': |
| 2892 |
if (str32nequ (command2.string, U"until ", 6)) { |
| 2893 |
double value; |
| 2894 |
Interpreter_numericExpression (me, command2.string + 6, & value); |
| 2895 |
if (value == 0.0) { |
| 2896 |
int depth = 0; |
| 2897 |
integer iline = my lineNumber - 1; |
| 2898 |
for (; iline > 0; iline --) { |
| 2899 |
if (str32nequ (lines [iline], U"repeat", 6) && |
| 2900 |
(! Melder_staysWithinInk (lines [iline] [6]) || lines [iline] [6] == U';')) |
| 2901 |
{ |
| 2902 |
if (depth == 0) { |
| 2903 |
my lineNumber = iline; |
| 2904 |
break; // go after `repeat` |
| 2905 |
} else |
| 2906 |
depth --; |
| 2907 |
} else if (str32nequ (lines [iline], U"until ", 6)) { |
| 2908 |
depth ++; |
| 2909 |
} |
| 2910 |
} |
| 2911 |
if (iline <= 0) |
| 2912 |
Melder_throw (U"Unmatched 'until'."); |
| 2913 |
} |
| 2914 |
} else |
| 2915 |
fail = true; |
| 2916 |
break; |
| 2917 |
case U'v': |
| 2918 |
fail = true; |
| 2919 |
break; |
| 2920 |
case U'w': |
| 2921 |
if (str32nequ (command2.string, U"while ", 6)) { |
| 2922 |
double value; |
| 2923 |
Interpreter_numericExpression (me, command2.string + 6, & value); |
| 2924 |
if (value == 0.0) { |
| 2925 |
int depth = 0; |
| 2926 |
integer iline = my lineNumber + 1; |
| 2927 |
for (; iline <= numberOfLines; iline ++) { |
| 2928 |
if (str32nequ (lines [iline], U"endwhile", 8) && |
| 2929 |
(! Melder_staysWithinInk (lines [iline] [8]) || lines [iline] [8] == U';')) |
| 2930 |
{ |
| 2931 |
const char32 *startOfInk = Melder_findInk (lines [iline] + 8); |
| 2932 |
if (startOfInk && *startOfInk != U';') { |
| 2933 |
my lineNumber = iline; |
| 2934 |
Melder_throw (U"Stray text after 'endwhile'."); |
| 2935 |
} |
| 2936 |
if (depth == 0) { |
| 2937 |
my lineNumber = iline; |
| 2938 |
break; // go after `endwhile` |
| 2939 |
} else |
| 2940 |
depth --; |
| 2941 |
} else if (str32nequ (lines [iline], U"while ", 6)) { |
| 2942 |
depth ++; |
| 2943 |
} |
| 2944 |
} |
| 2945 |
if (iline > numberOfLines) |
| 2946 |
Melder_throw (U"Unmatched 'while'."); |
| 2947 |
} |
| 2948 |
} else |
| 2949 |
fail = true; |
| 2950 |
break; |
| 2951 |
case U'x': |
| 2952 |
fail = true; |
| 2953 |
break; |
| 2954 |
case U'y': |
| 2955 |
fail = true; |
| 2956 |
break; |
| 2957 |
case U'z': |
| 2958 |
fail = true; |
| 2959 |
break; |
| 2960 |
default: |
| 2961 |
fail = true; |
| 2962 |
break; |
| 2963 |
} |
| 2964 |
if (fail) { |
| 2965 |
/* |
| 2966 |
Found an unknown word starting with a nonupper-case letter, optionally preceded by a period. |
| 2967 |
See whether the word is a variable name. |
| 2968 |
*/ |
| 2969 |
trace (U"found an unknown word starting with a nonupper-case letter, optionally preceded by a period"); |
| 2970 |
char32 *p = & command2.string [0]; |
| 2971 |
/* |
| 2972 |
Variable names consist of a sequence of letters, digits, and underscores, |
| 2973 |
optionally preceded by a period and optionally followed by a $ and/or #. |
| 2974 |
*/ |
| 2975 |
if (*p == U'.') |
| 2976 |
p ++; |
| 2977 |
while (Melder_isWordCharacter (*p) || *p == U'.') |
| 2978 |
p ++; |
| 2979 |
if (*p == U'$') { |
| 2980 |
if (p [1] == U'#') { |
| 2981 |
/* |
| 2982 |
Assign to a string array variable or a string array element. |
| 2983 |
*/ |
| 2984 |
static MelderString arrayName; |
| 2985 |
p ++; |
| 2986 |
*p = U'\0'; // erase the number sign temporarily |
| 2987 |
MelderString_copy (& arrayName, command2.string, U"#"); |
| 2988 |
*p = U'#'; // put the number sign back |
| 2989 |
p ++; // step over number sign |
| 2990 |
Melder_skipHorizontalSpace (& p); // go to first token after array name |
| 2991 |
if (*p == U'=') { |
| 2992 |
/* |
| 2993 |
This must be an assignment to a string array variable. |
| 2994 |
*/ |
| 2995 |
p ++; // step over equals sign |
| 2996 |
Melder_skipHorizontalSpace (& p); // go to first token after assignment |
| 2997 |
if (*p == U'\0') |
| 2998 |
Melder_throw (U"Missing right-hand expression in assignment to string array ", arrayName.string, U"."); |
| 2999 |
if (isCommand (p)) { |
| 3000 |
/* |
| 3001 |
Statement like: lines$# = Get all strings |
| 3002 |
*/ |
| 3003 |
bool status = praat_executeCommand (me, p); |
| 3004 |
InterpreterVariable var = Interpreter_lookUpVariable (me, arrayName.string); |
| 3005 |
if (! status) |
| 3006 |
var -> stringArrayValue = autoSTRVEC(); // anything can have happened, including an incorrect returnType |
| 3007 |
else if (my returnType == kInterpreter_ReturnType::STRINGARRAY_) |
| 3008 |
var -> stringArrayValue = my returnedStringArray.move(); |
| 3009 |
else |
| 3010 |
Melder_throw (kInterpreter_ReturnType_errorMessage (my returnType, p), |
| 3011 |
U"; not assigned to the string array variable \"", arrayName.string, U"\"."); |
| 3012 |
} else { |
| 3013 |
/* |
| 3014 |
Statement like: files$# = fileNames$# ("*.wav") |
| 3015 |
*/ |
| 3016 |
STRVEC value; |
| 3017 |
bool owned; |
| 3018 |
Interpreter_stringArrayExpression (me, p, & value, & owned); |
| 3019 |
InterpreterVariable var = Interpreter_lookUpVariable (me, arrayName.string); |
| 3020 |
StringArrayVariable_move (var, value, owned); |
| 3021 |
} |
| 3022 |
} else if (*p == U'[') { |
| 3023 |
assignToStringArrayElement (me, ++ p, arrayName.string, valueString); |
| 3024 |
} else Melder_throw (U"Missing '=' or '[' after string array variable ", arrayName.string, U"."); |
| 3025 |
} else { |
| 3026 |
/* |
| 3027 |
Assign to a string variable. |
| 3028 |
*/ |
| 3029 |
trace (U"detected an assignment to a string variable"); |
| 3030 |
char32 *endOfVariable = ++ p; |
| 3031 |
char32 *variableName = command2.string; |
| 3032 |
Melder_skipHorizontalSpace (& p); // go to first token after variable name |
| 3033 |
if (*p == U'[') { |
| 3034 |
/* |
| 3035 |
This must be an assignment to an indexed string variable. |
| 3036 |
*/ |
| 3037 |
*endOfVariable = U'\0'; |
| 3038 |
static MelderString indexedVariableName; |
| 3039 |
MelderString_copy (& indexedVariableName, command2.string, U"["); |
| 3040 |
for (;;) { |
| 3041 |
p ++; // skip opening bracket or comma |
| 3042 |
static MelderString index; |
| 3043 |
MelderString_empty (& index); |
| 3044 |
int depth = 0; |
| 3045 |
bool inString = false; |
| 3046 |
while ((depth > 0 || (*p != U',' && *p != U']') || inString) && Melder_staysWithinLine (*p)) { |
| 3047 |
MelderString_appendCharacter (& index, *p); |
| 3048 |
if (*p == U'[') { |
| 3049 |
if (! inString) |
| 3050 |
depth ++; |
| 3051 |
} else if (*p == U']') { |
| 3052 |
if (! inString) |
| 3053 |
depth --; |
| 3054 |
} |
| 3055 |
if (*p == U'"') |
| 3056 |
inString = ! inString; |
| 3057 |
p ++; |
| 3058 |
} |
| 3059 |
if (! Melder_staysWithinLine (*p)) |
| 3060 |
Melder_throw (U"Missing closing bracket (]) in indexed variable."); |
| 3061 |
Formula_Result result; |
| 3062 |
Interpreter_anyExpression (me, index.string, & result); |
| 3063 |
if (result.expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3064 |
const double numericIndexValue = result. numericResult; |
| 3065 |
MelderString_append (& indexedVariableName, numericIndexValue); |
| 3066 |
} else if (result.expressionType == kFormula_EXPRESSION_TYPE_STRING) { |
| 3067 |
MelderString_append (& indexedVariableName, U"\"", result. stringResult.get(), U"\""); |
| 3068 |
} |
| 3069 |
MelderString_appendCharacter (& indexedVariableName, *p); |
| 3070 |
if (*p == U']') { |
| 3071 |
break; |
| 3072 |
} |
| 3073 |
} |
| 3074 |
variableName = indexedVariableName.string; |
| 3075 |
p ++; // skip closing bracket |
| 3076 |
} |
| 3077 |
Melder_skipHorizontalSpace (& p); // go to first token after (perhaps indexed) variable name |
| 3078 |
int typeOfAssignment; // 0, 1, 2, 3 or 4 |
| 3079 |
if (*p == U'=') { |
| 3080 |
typeOfAssignment = 0; // assignment |
| 3081 |
} else if (*p == U'+') { |
| 3082 |
if (p [1] == U'=') { |
| 3083 |
typeOfAssignment = 1; // adding assignment |
| 3084 |
p ++; |
| 3085 |
} else { |
| 3086 |
Melder_throw (U"Missing “=” after string variable ", variableName, U" followed by “+”."); |
| 3087 |
} |
| 3088 |
} else if (*p == U'<') { |
| 3089 |
typeOfAssignment = 2; // read from file |
| 3090 |
} else if (*p == U'>') { |
| 3091 |
if (p [1] == U'>') { |
| 3092 |
typeOfAssignment = 3; // append to file |
| 3093 |
p ++; |
| 3094 |
} else { |
| 3095 |
typeOfAssignment = 4; // save to file |
| 3096 |
} |
| 3097 |
} else Melder_throw (U"Missing “=”, “+=”, “<”, or “>” after string variable ", variableName, U"."); |
| 3098 |
*endOfVariable = U'\0'; |
| 3099 |
p ++; |
| 3100 |
Melder_skipHorizontalSpace (& p); // go to first token after assignment or I/O symbol |
| 3101 |
if (*p == U'\0') { |
| 3102 |
if (typeOfAssignment >= 2) |
| 3103 |
Melder_throw (U"Missing file name after variable ", variableName, U"."); |
| 3104 |
else |
| 3105 |
Melder_throw (U"Missing expression after variable ", variableName, U"."); |
| 3106 |
} |
| 3107 |
if (typeOfAssignment >= 2) { |
| 3108 |
structMelderFile file { }; |
| 3109 |
Melder_relativePathToFile (p, & file); |
| 3110 |
if (typeOfAssignment == 2) { |
| 3111 |
autostring32 stringValue = MelderFile_readText (& file); |
| 3112 |
InterpreterVariable var = Interpreter_lookUpVariable (me, variableName); |
| 3113 |
var -> stringValue = stringValue.move(); |
| 3114 |
} else if (typeOfAssignment == 3) { |
| 3115 |
if (theCurrentPraatObjects != & theForegroundPraatObjects) Melder_throw (U"Commands that write to a file are not available inside pictures."); |
| 3116 |
InterpreterVariable var = Interpreter_hasVariable (me, variableName); |
| 3117 |
if (! var) |
| 3118 |
Melder_throw (U"Variable ", variableName, U" undefined."); |
| 3119 |
MelderFile_appendText_e (& file, var -> stringValue.get()); |
| 3120 |
} else { |
| 3121 |
if (theCurrentPraatObjects != & theForegroundPraatObjects) Melder_throw (U"Commands that write to a file are not available inside pictures."); |
| 3122 |
InterpreterVariable var = Interpreter_hasVariable (me, variableName); |
| 3123 |
if (! var) |
| 3124 |
Melder_throw (U"Variable ", variableName, U" undefined."); |
| 3125 |
MelderFile_writeText_e (& file, var -> stringValue.get(), Melder_getOutputEncoding ()); |
| 3126 |
} |
| 3127 |
} else if (isCommand (p)) { |
| 3128 |
/* |
| 3129 |
Statement like: name$ = Get name |
| 3130 |
*/ |
| 3131 |
MelderString_empty (& valueString); // empty because command may print nothing; also makes sure that valueString.string exists |
| 3132 |
autoMelderDivertInfo divert (& valueString); |
| 3133 |
bool status = praat_executeCommand (me, p); |
| 3134 |
InterpreterVariable var = Interpreter_lookUpVariable (me, variableName); |
| 3135 |
if (! status) |
| 3136 |
var -> stringValue = Melder_dup (U""); // anything can have happened, including an incorrect returnType |
| 3137 |
else if (my returnType == kInterpreter_ReturnType::STRING_ || |
| 3138 |
my returnType == kInterpreter_ReturnType::REAL_ || |
| 3139 |
my returnType == kInterpreter_ReturnType::INTEGER_ |
| 3140 |
) |
| 3141 |
var -> stringValue = Melder_dup (valueString.string); |
| 3142 |
else |
| 3143 |
Melder_throw (kInterpreter_ReturnType_errorMessage (my returnType, p), |
| 3144 |
U"; not assigned to the string variable \"", variableName, U"\"."); |
| 3145 |
} else { |
| 3146 |
/* |
| 3147 |
Evaluate a string expression and assign the result to the variable. |
| 3148 |
Statements like: |
| 3149 |
sentence$ = subject$ + verb$ + object$ |
| 3150 |
extension$ = if index (file$, ".") <> 0 |
| 3151 |
... then right$ (file$, length (file$) - rindex (file$, ".")) |
| 3152 |
... else "" fi |
| 3153 |
*/ |
| 3154 |
trace (U"evaluating string expression"); |
| 3155 |
autostring32 stringValue = Interpreter_stringExpression (me, p); |
| 3156 |
trace (U"assigning to string variable ", variableName); |
| 3157 |
if (typeOfAssignment == 1) { |
| 3158 |
InterpreterVariable var = Interpreter_hasVariable (me, variableName); |
| 3159 |
if (! var) |
| 3160 |
Melder_throw (U"The string ", variableName, U" does not exist.\n" |
| 3161 |
U"You can increment (+=) only existing strings."); |
| 3162 |
const integer oldLength = Melder_length (var -> stringValue.get()); |
| 3163 |
const integer extraLength = Melder_length (stringValue.get()); |
| 3164 |
autostring32 newString (oldLength + extraLength, false); |
| 3165 |
str32cpy (newString.get(), var -> stringValue.get()); |
| 3166 |
str32cpy (newString.get() + oldLength, stringValue.get()); |
| 3167 |
var -> stringValue = newString.move(); |
| 3168 |
} else { |
| 3169 |
InterpreterVariable var = Interpreter_lookUpVariable (me, variableName); |
| 3170 |
var -> stringValue = stringValue.move(); |
| 3171 |
} |
| 3172 |
} |
| 3173 |
} |
| 3174 |
} else if (*p == U'#') { |
| 3175 |
if (p [1] == U'#') { |
| 3176 |
/* |
| 3177 |
Assign to a numeric matrix variable or to a matrix element. |
| 3178 |
*/ |
| 3179 |
static MelderString matrixName; |
| 3180 |
p ++; // go to second '#' |
| 3181 |
*p = U'\0'; // erase the last number sign temporarily |
| 3182 |
MelderString_copy (& matrixName, command2.string, U'#'); |
| 3183 |
*p = U'#'; // put the number sign back |
| 3184 |
p ++; // step over last number sign |
| 3185 |
Melder_skipHorizontalSpace (& p); // go to first token after matrix name |
| 3186 |
if (*p == U'=') { |
| 3187 |
/* |
| 3188 |
This must be an assignment to a matrix variable. |
| 3189 |
*/ |
| 3190 |
p ++; // step over equals sign |
| 3191 |
Melder_skipHorizontalSpace (& p); // go to first token after assignment |
| 3192 |
if (*p == U'\0') |
| 3193 |
Melder_throw (U"Missing right-hand expression in assignment to matrix ", matrixName.string, U"."); |
| 3194 |
if (isCommand (p)) { |
| 3195 |
/* |
| 3196 |
Statement like: values## = Get all values |
| 3197 |
*/ |
| 3198 |
const bool status = praat_executeCommand (me, p); |
| 3199 |
InterpreterVariable var = Interpreter_lookUpVariable (me, matrixName.string); |
| 3200 |
if (! status) |
| 3201 |
var -> numericMatrixValue = autoMAT(); // anything can have happened, including an incorrect returnType |
| 3202 |
else if (my returnType == kInterpreter_ReturnType::REALMATRIX_) |
| 3203 |
var -> numericMatrixValue = my returnedRealMatrix.move(); |
| 3204 |
else |
| 3205 |
Melder_throw (kInterpreter_ReturnType_errorMessage (my returnType, p), |
| 3206 |
U"; not assigned to the matrix variable \"", matrixName.string, U"\"."); |
| 3207 |
} else { |
| 3208 |
MAT value; |
| 3209 |
bool owned; |
| 3210 |
Interpreter_numericMatrixExpression (me, p, & value, & owned); |
| 3211 |
InterpreterVariable var = Interpreter_lookUpVariable (me, matrixName.string); |
| 3212 |
NumericMatrixVariable_move (var, value, owned); |
| 3213 |
} |
| 3214 |
} else if (*p == U'[') { |
| 3215 |
assignToNumericMatrixElement (me, ++ p, matrixName.string, valueString); |
| 3216 |
} else if (*p == U'+' && p [1] == U'=') { |
| 3217 |
InterpreterVariable var = Interpreter_hasVariable (me, matrixName.string); |
| 3218 |
if (! var) |
| 3219 |
Melder_throw (U"The matrix ", matrixName.string, U" does not exist.\n" |
| 3220 |
U"You can increment (+=) only existing matrices."); |
| 3221 |
Formula_Result result; |
| 3222 |
Interpreter_anyExpression (me, p += 2, & result); |
| 3223 |
if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC_MATRIX) { |
| 3224 |
NumericMatrixVariable_add (var, result. numericMatrixResult); |
| 3225 |
} else if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3226 |
NumericMatrixVariable_add (var, result. numericResult); |
| 3227 |
} else { |
| 3228 |
Melder_throw (U"You can increment (+=) a numeric matrix only with a number or another numeric matrix."); |
| 3229 |
} |
| 3230 |
} else if (*p == U'-' && p [1] == U'=') { |
| 3231 |
InterpreterVariable var = Interpreter_hasVariable (me, matrixName.string); |
| 3232 |
if (! var) |
| 3233 |
Melder_throw (U"The matrix ", matrixName.string, U" does not exist.\n" |
| 3234 |
U"You can decrement (-=) only existing matrices."); |
| 3235 |
Formula_Result result; |
| 3236 |
Interpreter_anyExpression (me, p += 2, & result); |
| 3237 |
if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC_MATRIX) { |
| 3238 |
NumericMatrixVariable_subtract (var, result. numericMatrixResult); |
| 3239 |
} else if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3240 |
NumericMatrixVariable_subtract (var, result. numericResult); |
| 3241 |
} else { |
| 3242 |
Melder_throw (U"You can decrement (-=) a numeric matrix only with a number or another numeric matrix."); |
| 3243 |
} |
| 3244 |
} else if (*p == U'*' && p [1] == U'=') { |
| 3245 |
InterpreterVariable var = Interpreter_hasVariable (me, matrixName.string); |
| 3246 |
if (! var) |
| 3247 |
Melder_throw (U"The matrix ", matrixName.string, U" does not exist.\n" |
| 3248 |
U"You can multiply (*=) only existing matrices."); |
| 3249 |
Formula_Result result; |
| 3250 |
Interpreter_anyExpression (me, p += 2, & result); |
| 3251 |
if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC_MATRIX) { |
| 3252 |
NumericMatrixVariable_multiply (var, result. numericMatrixResult); |
| 3253 |
} else if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3254 |
NumericMatrixVariable_multiply (var, result. numericResult); |
| 3255 |
} else { |
| 3256 |
Melder_throw (U"You can multiply (*=) a numeric matrix only with a number or another numeric matrix."); |
| 3257 |
} |
| 3258 |
} else if (*p == U'/' && p [1] == U'=') { |
| 3259 |
InterpreterVariable var = Interpreter_hasVariable (me, matrixName.string); |
| 3260 |
if (! var) |
| 3261 |
Melder_throw (U"The matrix ", matrixName.string, U" does not exist.\n" |
| 3262 |
U"You can divide (/=) only existing matrices."); |
| 3263 |
Formula_Result result; |
| 3264 |
Interpreter_anyExpression (me, p += 2, & result); |
| 3265 |
if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC_MATRIX) { |
| 3266 |
NumericMatrixVariable_divide (var, result. numericMatrixResult); |
| 3267 |
} else if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3268 |
NumericMatrixVariable_divide (var, result. numericResult); |
| 3269 |
} else { |
| 3270 |
Melder_throw (U"You can divide (/=) a numeric matrix only with a number or another numeric matrix."); |
| 3271 |
} |
| 3272 |
} else if (*p == U'~') { |
| 3273 |
/* |
| 3274 |
This must be a formula assignment to a matrix variable. |
| 3275 |
*/ |
| 3276 |
p ++; // step over tilde |
| 3277 |
Melder_skipHorizontalSpace (& p); // go to first token after assignment |
| 3278 |
if (*p == U'\0') |
| 3279 |
Melder_throw (U"Missing formula expression for matrix ", matrixName.string, U"."); |
| 3280 |
InterpreterVariable var = Interpreter_hasVariable (me, matrixName.string); |
| 3281 |
if (! var) |
| 3282 |
Melder_throw (U"The matrix ", matrixName.string, U" does not exist.\n" |
| 3283 |
"You can assign a formula only to an existing matrix."); |
| 3284 |
static Matrix matrixObject; |
| 3285 |
if (! matrixObject) |
| 3286 |
matrixObject = Matrix_createSimple (1, 1). releaseToAmbiguousOwner(); // prevent exit-time destruction |
| 3287 |
MAT mat = var -> numericMatrixValue.get(); |
| 3288 |
matrixObject -> xmax = mat.ncol + 0.5; |
| 3289 |
matrixObject -> nx = mat.ncol; |
| 3290 |
matrixObject -> ymax = mat.nrow + 0.5; |
| 3291 |
matrixObject -> ny = mat.nrow; |
| 3292 |
matrixObject -> z.cells = mat.cells; // just a reference (YUCK) |
| 3293 |
matrixObject -> z.nrow = mat.nrow; |
| 3294 |
matrixObject -> z.ncol = mat.ncol; |
| 3295 |
Matrix_formula (matrixObject, p, me, nullptr); |
| 3296 |
} else Melder_throw (U"Missing '=' after matrix variable ", matrixName.string, U"."); |
| 3297 |
} else { |
| 3298 |
/* |
| 3299 |
Assign to a numeric vector variable or to a vector element. |
| 3300 |
*/ |
| 3301 |
static MelderString vectorName; |
| 3302 |
*p = U'\0'; // erase the number sign temporarily |
| 3303 |
MelderString_copy (& vectorName, command2.string, U"#"); |
| 3304 |
*p = U'#'; // put the number sign back |
| 3305 |
p ++; // step over number sign |
| 3306 |
Melder_skipHorizontalSpace (& p); // go to first token after array name |
| 3307 |
if (*p == U'=') { |
| 3308 |
/* |
| 3309 |
This must be an assignment to a vector variable. |
| 3310 |
First compute the value of the expression, |
| 3311 |
*then* look up (perhaps create) the vector variable (the order is important) |
| 3312 |
then assign the value to that vector variable. |
| 3313 |
Reversing the order of the first and second step would fail the following test: |
| 3314 |
*/ |
| 3315 |
/*@praat |
| 3316 |
asserterror Unknown variable:'newline$'« interpreter_assignToVector# |
| 3317 |
interpreter_assignToVector# = interpreter_assignToVector# + 5 |
| 3318 |
@*/ |
| 3319 |
p ++; // step over equals sign |
| 3320 |
Melder_skipHorizontalSpace (& p); // go to first token after assignment |
| 3321 |
if (*p == U'\0') |
| 3322 |
Melder_throw (U"Missing right-hand expression in assignment to vector ", vectorName.string, U"."); |
| 3323 |
if (isCommand (p)) { |
| 3324 |
/* |
| 3325 |
Statement like: times# = Get all times |
| 3326 |
*/ |
| 3327 |
bool status = praat_executeCommand (me, p); |
| 3328 |
InterpreterVariable var = Interpreter_lookUpVariable (me, vectorName.string); |
| 3329 |
if (! status) |
| 3330 |
var -> numericVectorValue = autoVEC(); // anything can have happened, including an incorrect returnType |
| 3331 |
else if (my returnType == kInterpreter_ReturnType::OBJECT_) { |
| 3332 |
var -> numericVectorValue = autoVEC(); |
| 3333 |
integer IOBJECT; |
| 3334 |
WHERE (SELECTED) |
| 3335 |
*var -> numericVectorValue. append() = ID; |
| 3336 |
} else if (my returnType == kInterpreter_ReturnType::REALVECTOR_) |
| 3337 |
var -> numericVectorValue = my returnedRealVector.move(); |
| 3338 |
else |
| 3339 |
Melder_throw (kInterpreter_ReturnType_errorMessage (my returnType, p), U"; not assigned to the vector variable \"", vectorName.string, U"\"."); |
| 3340 |
} else { |
| 3341 |
VEC value; |
| 3342 |
bool owned; |
| 3343 |
Interpreter_numericVectorExpression (me, p, & value, & owned); |
| 3344 |
InterpreterVariable var = Interpreter_lookUpVariable (me, vectorName.string); // not earlier, because it could have appeared in the expression |
| 3345 |
NumericVectorVariable_move (var, value, owned); |
| 3346 |
} |
| 3347 |
} else if (*p == U'[') { |
| 3348 |
assignToNumericVectorElement (me, ++ p, vectorName.string, valueString); |
| 3349 |
} else if (*p == U'+' && p [1] == U'=') { |
| 3350 |
InterpreterVariable var = Interpreter_hasVariable (me, vectorName.string); |
| 3351 |
if (! var) |
| 3352 |
Melder_throw (U"The vector ", vectorName.string, U" does not exist.\n" |
| 3353 |
U"You can increment (+=) only existing vectors."); |
| 3354 |
/*@praat |
| 3355 |
asserterror The vector interpreter_assignToVector# does not exist. |
| 3356 |
interpreter_assignToVector# += 5 |
| 3357 |
@*/ |
| 3358 |
Formula_Result result; |
| 3359 |
Interpreter_anyExpression (me, p += 2, & result); |
| 3360 |
if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC_VECTOR) { |
| 3361 |
NumericVectorVariable_add (var, result. numericVectorResult); |
| 3362 |
} else if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3363 |
NumericVectorVariable_add (var, result. numericResult); |
| 3364 |
} else { |
| 3365 |
Melder_throw (U"You can increment (+=) a numeric vector only with a number or another numeric vector."); |
| 3366 |
} |
| 3367 |
} else if (*p == U'-' && p [1] == U'=') { |
| 3368 |
InterpreterVariable var = Interpreter_hasVariable (me, vectorName.string); |
| 3369 |
if (! var) |
| 3370 |
Melder_throw (U"The vector ", vectorName.string, U" does not exist.\n" |
| 3371 |
U"You can decrement (-=) only existing vectors."); |
| 3372 |
Formula_Result result; |
| 3373 |
Interpreter_anyExpression (me, p += 2, & result); |
| 3374 |
if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC_VECTOR) { |
| 3375 |
NumericVectorVariable_subtract (var, result. numericVectorResult); |
| 3376 |
} else if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3377 |
NumericVectorVariable_subtract (var, result. numericResult); |
| 3378 |
} else { |
| 3379 |
Melder_throw (U"You can decrement (-=) a numeric vector only with a number or another numeric vector."); |
| 3380 |
} |
| 3381 |
} else if (*p == U'*' && p [1] == U'=') { |
| 3382 |
InterpreterVariable var = Interpreter_hasVariable (me, vectorName.string); |
| 3383 |
if (! var) |
| 3384 |
Melder_throw (U"The vector ", vectorName.string, U" does not exist.\n" |
| 3385 |
U"You can multiply (*=) only existing vectors."); |
| 3386 |
Formula_Result result; |
| 3387 |
Interpreter_anyExpression (me, p += 2, & result); |
| 3388 |
if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC_VECTOR) { |
| 3389 |
NumericVectorVariable_multiply (var, result. numericVectorResult); |
| 3390 |
} else if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3391 |
NumericVectorVariable_multiply (var, result. numericResult); |
| 3392 |
} else { |
| 3393 |
Melder_throw (U"You can multiply (*=) a numeric vector only with a number or another numeric vector."); |
| 3394 |
} |
| 3395 |
} else if (*p == U'/' && p [1] == U'=') { |
| 3396 |
InterpreterVariable var = Interpreter_hasVariable (me, vectorName.string); |
| 3397 |
if (! var) |
| 3398 |
Melder_throw (U"The vector ", vectorName.string, U" does not exist.\n" |
| 3399 |
U"You can divide (/=) only existing vectors."); |
| 3400 |
Formula_Result result; |
| 3401 |
Interpreter_anyExpression (me, p += 2, & result); |
| 3402 |
if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC_VECTOR) { |
| 3403 |
NumericVectorVariable_divide (var, result. numericVectorResult); |
| 3404 |
} else if (result. expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3405 |
NumericVectorVariable_divide (var, result. numericResult); |
| 3406 |
} else { |
| 3407 |
Melder_throw (U"You can divide (/=) a numeric vector only with a number or another numeric vector."); |
| 3408 |
} |
| 3409 |
} else if (*p == U'~') { |
| 3410 |
/* |
| 3411 |
This must be a formula assignment to a vector variable. |
| 3412 |
*/ |
| 3413 |
p ++; // step over tilde |
| 3414 |
Melder_skipHorizontalSpace (& p); // go to first token after assignment |
| 3415 |
if (*p == U'\0') |
| 3416 |
Melder_throw (U"Missing formula expression for vector ", vectorName.string, U"."); |
| 3417 |
InterpreterVariable var = Interpreter_hasVariable (me, vectorName.string); |
| 3418 |
if (! var) |
| 3419 |
Melder_throw (U"The vector ", vectorName.string, U" does not exist.\n" |
| 3420 |
"You can assign a formula only to an existing vector."); |
| 3421 |
static Matrix vectorObject; |
| 3422 |
if (! vectorObject) |
| 3423 |
vectorObject = Matrix_createSimple (1, 1). releaseToAmbiguousOwner(); // prevent destruction when program ends |
| 3424 |
VEC vec = var -> numericVectorValue.get(); |
| 3425 |
//vectorObject -> xmin = 0.5; |
| 3426 |
vectorObject -> xmax = vec.size + 0.5; |
| 3427 |
vectorObject -> nx = vec.size; |
| 3428 |
vectorObject -> z.cells = & vec [1]; |
| 3429 |
//vectorObject -> z.nrow = 1; |
| 3430 |
vectorObject -> z.ncol = vec.size; |
| 3431 |
Matrix_formula (vectorObject, p, me, nullptr); |
| 3432 |
} else Melder_throw (U"Missing '=' or '+=' or '[' or '~' after vector variable ", vectorName.string, U"."); |
| 3433 |
} |
| 3434 |
} else { |
| 3435 |
/* |
| 3436 |
Try to assign to a numeric variable. |
| 3437 |
*/ |
| 3438 |
double value; |
| 3439 |
char32 *variableName = command2.string; |
| 3440 |
int typeOfAssignment = 0; // plain assignment |
| 3441 |
if (*p == U'\0') { |
| 3442 |
/* |
| 3443 |
Command ends here: it may be a PraatShell command. |
| 3444 |
*/ |
| 3445 |
(void) praat_executeCommand (me, command2.string); |
| 3446 |
continue; // next line |
| 3447 |
} |
| 3448 |
char32 *endOfVariable = p; |
| 3449 |
Melder_skipHorizontalSpace (& p); |
| 3450 |
if (*p == U'=' || ((*p == U'+' || *p == U'-' || *p == U'*' || *p == U'/') && p [1] == U'=')) { |
| 3451 |
/* |
| 3452 |
This must be an assignment (though: "echo = ..." ???) |
| 3453 |
*/ |
| 3454 |
typeOfAssignment = ( *p == U'+' ? 1 : *p == U'-' ? 2 : *p == U'*' ? 3 : *p == U'/' ? 4 : 0 ); |
| 3455 |
*endOfVariable = U'\0'; // close variable name; FIXME: this can be any weird character, e.g. hallo& |
| 3456 |
} else if (*p == U'[') { |
| 3457 |
/* |
| 3458 |
This must be an assignment to an indexed numeric variable. |
| 3459 |
*/ |
| 3460 |
*endOfVariable = U'\0'; |
| 3461 |
static MelderString indexedVariableName; |
| 3462 |
MelderString_copy (& indexedVariableName, command2.string, U"["); |
| 3463 |
for (;;) { |
| 3464 |
p ++; // skip opening bracket or comma |
| 3465 |
static MelderString index; |
| 3466 |
MelderString_empty (& index); |
| 3467 |
int depth = 0; |
| 3468 |
bool inString = false; |
| 3469 |
while ((depth > 0 || (*p != U',' && *p != U']') || inString) && Melder_staysWithinLine (*p)) { |
| 3470 |
MelderString_appendCharacter (& index, *p); |
| 3471 |
if (*p == U'[') { |
| 3472 |
if (! inString) |
| 3473 |
depth ++; |
| 3474 |
} else if (*p == U']') { |
| 3475 |
if (! inString) |
| 3476 |
depth --; |
| 3477 |
} |
| 3478 |
if (*p == U'"') |
| 3479 |
inString = ! inString; |
| 3480 |
p ++; |
| 3481 |
} |
| 3482 |
if (! Melder_staysWithinLine (*p)) |
| 3483 |
Melder_throw (U"Missing closing bracket (]) in indexed variable."); |
| 3484 |
Formula_Result result; |
| 3485 |
Melder_assert (! result. stringResult); |
| 3486 |
Interpreter_anyExpression (me, index.string, & result); |
| 3487 |
if (result.expressionType == kFormula_EXPRESSION_TYPE_NUMERIC) { |
| 3488 |
const double numericIndexValue = result. numericResult; |
| 3489 |
MelderString_append (& indexedVariableName, numericIndexValue); |
| 3490 |
} else if (result.expressionType == kFormula_EXPRESSION_TYPE_STRING) { |
| 3491 |
MelderString_append (& indexedVariableName, U"\"", result. stringResult.get(), U"\""); |
| 3492 |
} |
| 3493 |
MelderString_appendCharacter (& indexedVariableName, *p); |
| 3494 |
if (*p == U']') |
| 3495 |
break; |
| 3496 |
} |
| 3497 |
variableName = indexedVariableName.string; |
| 3498 |
p ++; // skip closing bracket |
| 3499 |
Melder_skipHorizontalSpace (& p); |
| 3500 |
if (*p == U'=' || ((*p == U'+' || *p == U'-' || *p == U'*' || *p == U'/') && p [1] == U'=')) { |
| 3501 |
typeOfAssignment = ( *p == U'+' ? 1 : *p == U'-' ? 2 : *p == U'*' ? 3 : *p == U'/' ? 4 : 0 ); |
| 3502 |
} |
| 3503 |
} else { |
| 3504 |
/* |
| 3505 |
Not an assignment: perhaps a PraatShell command (select, echo, execute, pause ...). |
| 3506 |
*/ |
| 3507 |
(void) praat_executeCommand (me, variableName); |
| 3508 |
continue; // next line |
| 3509 |
} |
| 3510 |
p += ( typeOfAssignment == 0 ? 1 : 2 ); |
| 3511 |
Melder_skipHorizontalSpace (& p); |
| 3512 |
if (*p == U'\0') |
| 3513 |
Melder_throw (U"Missing expression after variable ", variableName, U"."); |
| 3514 |
/* |
| 3515 |
Three classes of assignments: |
| 3516 |
var = formula |
| 3517 |
var = Query |
| 3518 |
var = Object creation |
| 3519 |
*/ |
| 3520 |
if (isCommand (p)) { |
| 3521 |
/* |
| 3522 |
Get the value of the query. |
| 3523 |
*/ |
| 3524 |
MelderString_empty (& valueString); |
| 3525 |
autoMelderDivertInfo divert (& valueString); |
| 3526 |
MelderString_appendCharacter (& valueString, 1); // will be overwritten by something totally different if any MelderInfo function is called... |
| 3527 |
bool status = praat_executeCommand (me, p); |
| 3528 |
if (! status) { |
| 3529 |
value = undefined; // anything can have happened, including an incorrect return type |
| 3530 |
} else if (my returnType == kInterpreter_ReturnType::OBJECT_) { |
| 3531 |
integer IOBJECT, selectedObject = 0, numberOfSelectedObjects = 0; |
| 3532 |
WHERE (SELECTED) { |
| 3533 |
selectedObject = IOBJECT; |
| 3534 |
numberOfSelectedObjects += 1; |
| 3535 |
} |
| 3536 |
if (numberOfSelectedObjects > 1) |
| 3537 |
Melder_throw (U"Multiple objects selected. Cannot assign object IDs to numeric variable. " |
| 3538 |
"Perhaps use a vector variable instead."); |
| 3539 |
if (numberOfSelectedObjects == 0) |
| 3540 |
Melder_throw (U"No objects selected. Cannot assign object ID to variable."); |
| 3541 |
value = theCurrentPraatObjects -> list [selectedObject]. id; |
| 3542 |
} else if (my returnType == kInterpreter_ReturnType::REAL_ || my returnType == kInterpreter_ReturnType::INTEGER_) { |
| 3543 |
value = Melder_atof (valueString.string); // including --undefined-- |
| 3544 |
} else |
| 3545 |
Melder_throw (kInterpreter_ReturnType_errorMessage (my returnType, p), |
| 3546 |
U"; not assigned to the numeric variable \"", variableName, U"\"."); |
| 3547 |
} else { |
| 3548 |
/* |
| 3549 |
Get the value of the formula. |
| 3550 |
*/ |
| 3551 |
Interpreter_numericExpression (me, p, & value); |
| 3552 |
} |
| 3553 |
/* |
| 3554 |
Assign the value to a variable. |
| 3555 |
*/ |
| 3556 |
if (typeOfAssignment == 0) { |
| 3557 |
/* |
| 3558 |
Use an existing variable, or create a new one. |
| 3559 |
*/ |
| 3560 |
//Melder_casual (U"looking up variable ", variableName); |
| 3561 |
InterpreterVariable var = Interpreter_lookUpVariable (me, variableName); |
| 3562 |
var -> numericValue = value; |
| 3563 |
} else { |
| 3564 |
/* |
| 3565 |
Modify an existing variable. |
| 3566 |
*/ |
| 3567 |
InterpreterVariable var = Interpreter_hasVariable (me, variableName); |
| 3568 |
if (! var) |
| 3569 |
Melder_throw (U"The variable ", variableName, U" does not exist. You can modify only existing variables."); |
| 3570 |
if (isundef (var -> numericValue)) { |
| 3571 |
/* Keep it that way. */ |
| 3572 |
} else { |
| 3573 |
if (typeOfAssignment == 1) { |
| 3574 |
var -> numericValue += value; |
| 3575 |
} else if (typeOfAssignment == 2) { |
| 3576 |
var -> numericValue -= value; |
| 3577 |
} else if (typeOfAssignment == 3) { |
| 3578 |
var -> numericValue *= value; |
| 3579 |
} else if (value == 0) { |
| 3580 |
var -> numericValue = undefined; |
| 3581 |
} else { |
| 3582 |
var -> numericValue /= value; |
| 3583 |
} |
| 3584 |
} |
| 3585 |
} |
| 3586 |
} |
| 3587 |
} // endif fail |
| 3588 |
if (my assertErrorLineNumber != 0 && my assertErrorLineNumber != my lineNumber) { |
| 3589 |
const integer save_assertErrorLineNumber = my assertErrorLineNumber; |
| 3590 |
my assertErrorLineNumber = 0; |
| 3591 |
Melder_throw (U"Script assertion fails in line ", save_assertErrorLineNumber, |
| 3592 |
U": error « ", my assertErrorString.string, U" » not raised. Instead: no error."); |
| 3593 |
|
| 3594 |
} |
| 3595 |
} catch (MelderError) { |
| 3596 |
// Melder_casual (U"Error: << ", Melder_getError(), |
| 3597 |
// U" >>\nassertErrorLineNumber: ", my assertErrorLineNumber, |
| 3598 |
// U"\nlineNumber: ", lineNumber, |
| 3599 |
// U"\nAssert error string: << ", assertErrorString.string, |
| 3600 |
// U" >>\n" |
| 3601 |
// ); |
| 3602 |
if (Melder_hasCrash ()) |
| 3603 |
throw; |
| 3604 |
if (my assertErrorLineNumber == 0) { |
| 3605 |
throw; |
| 3606 |
} else if (my assertErrorLineNumber != my lineNumber) { |
| 3607 |
if (str32str (Melder_getError (), my assertErrorString.string)) { |
| 3608 |
Melder_clearError (); |
| 3609 |
my assertErrorLineNumber = 0; |
| 3610 |
} else { |
| 3611 |
autostring32 errorCopy_nothrow = Melder_dup_f (Melder_getError ()); |
| 3612 |
Melder_clearError (); |
| 3613 |
Melder_throw (U"Script assertion fails in line ", my assertErrorLineNumber, |
| 3614 |
U": error « ", my assertErrorString.string, U" » not raised. Instead:\n", |
| 3615 |
errorCopy_nothrow.get() |
| 3616 |
); |
| 3617 |
} |
| 3618 |
} |
| 3619 |
} |
| 3620 |
} // endfor lineNumber |
| 3621 |
if (! my isHalted) { |
| 3622 |
//my numberOfLabels = 0; |
| 3623 |
my running = false; |
| 3624 |
my stopped = false; |
| 3625 |
} |
| 3626 |
} catch (MelderError) { |
| 3627 |
//TRACE |
| 3628 |
trace (U"catch"); |
| 3629 |
trace (U"catch ", Melder_pointer (me)); |
| 3630 |
trace (U"catch at line number ", my lineNumber); |
| 3631 |
trace (U"catch with stack ", Melder_pointer (my owningInterpreterStack)); |
| 3632 |
trace (U"catch in file ", & my file); |
| 3633 |
trace (U"catch at level ", my owningInterpreterStack -> currentLevel); |
| 3634 |
trace (U"catch at stack interpreter ", Melder_pointer (my owningInterpreterStack -> current_0 ())); |
| 3635 |
if (my lineNumber > 0) { |
| 3636 |
const bool normalExplicitExit = str32nequ (my lines [my lineNumber], U"exit ", 5) || Melder_hasError (U"Script exited."); |
| 3637 |
if (! normalExplicitExit && ! assertionFailed) { // don't show the message twice! |
| 3638 |
while (my lines [my lineNumber] [0] == U'\0') { // did this use to be a continuation line? |
| 3639 |
my lineNumber --; |
| 3640 |
trace (U"line number lowered to ", my lineNumber); |
| 3641 |
Melder_assert (my lineNumber > 0); // originally empty lines that stayed empty should not generate errors |
| 3642 |
} |
| 3643 |
Melder_appendError (U"Script line ", my lineNumber, U" not performed or completed:\n« ", my lines [my lineNumber], U" »"); |
| 3644 |
} |
| 3645 |
} |
| 3646 |
//my numberOfLabels = 0; |
| 3647 |
my running = false; |
| 3648 |
my stopped = false; |
| 3649 |
//my isHalted = false; // TODO: needed? |
| 3650 |
trace (U"catch: has finished...", Melder_pointer (my owningInterpreterStack)); |
| 3651 |
trace (U"catch: ... has finished"); |
| 3652 |
if (Melder_hasCrash ()) { |
| 3653 |
my owningInterpreterStack -> currentLevel -= 1; // TODO: encapsulate |
| 3654 |
throw; |
| 3655 |
} else if (str32equ (Melder_getError (), U"\nScript exited.\n")) { |
| 3656 |
Melder_clearError (); |
| 3657 |
} else { |
| 3658 |
my owningInterpreterStack -> currentLevel -= 1; // TODO: encapsulate |
| 3659 |
throw; |
| 3660 |
} |
| 3661 |
} |
| 3662 |
} |
| 3663 |
|
| 3664 |
void Interpreter_stop (Interpreter me) { |
| 3665 |
//Melder_casual (U"Interpreter_stop in: ", Melder_pointer (me)); |
| 3666 |
my stopped = true; |
| 3667 |
my running = false; |
| 3668 |
//my numberOfLabels = 0; |
| 3669 |
//Melder_casual (U"Interpreter_stop out: ", Melder_pointer (me)); |
| 3670 |
} |
| 3671 |
|
| 3672 |
void Interpreter_voidExpression (Interpreter me, conststring32 expression) { |
| 3673 |
Formula_compile (me, nullptr, expression, kFormula_EXPRESSION_TYPE_NUMERIC, false); |
| 3674 |
Formula_Result result; |
| 3675 |
Formula_run (0, 0, & result); |
| 3676 |
} |
| 3677 |
|
| 3678 |
void Interpreter_numericExpression (Interpreter me, conststring32 expression, double *out_value) { |
| 3679 |
Formula_compile (me, nullptr, expression, kFormula_EXPRESSION_TYPE_NUMERIC, false); |
| 3680 |
Formula_Result result; |
| 3681 |
Formula_run (0, 0, & result); |
| 3682 |
*out_value = result. numericResult; |
| 3683 |
} |
| 3684 |
|
| 3685 |
void Interpreter_numericVectorExpression (Interpreter me, conststring32 expression, VEC *out_value, bool *out_owned) { |
| 3686 |
Formula_compile (me, nullptr, expression, kFormula_EXPRESSION_TYPE_NUMERIC_VECTOR, false); |
| 3687 |
Formula_Result result; |
| 3688 |
Formula_run (0, 0, & result); |
| 3689 |
*out_value = result. numericVectorResult; |
| 3690 |
*out_owned = result. owned; |
| 3691 |
result. owned = false; |
| 3692 |
} |
| 3693 |
|
| 3694 |
void Interpreter_numericMatrixExpression (Interpreter me, conststring32 expression, MAT *out_value, bool *out_owned) { |
| 3695 |
Formula_compile (me, nullptr, expression, kFormula_EXPRESSION_TYPE_NUMERIC_MATRIX, false); |
| 3696 |
Formula_Result result; |
| 3697 |
Formula_run (0, 0, & result); |
| 3698 |
*out_value = result. numericMatrixResult; |
| 3699 |
*out_owned = result. owned; |
| 3700 |
result. owned = false; |
| 3701 |
} |
| 3702 |
|
| 3703 |
autostring32 Interpreter_stringExpression (Interpreter me, conststring32 expression) { |
| 3704 |
Formula_compile (me, nullptr, expression, kFormula_EXPRESSION_TYPE_STRING, false); |
| 3705 |
Formula_Result result; |
| 3706 |
Formula_run (0, 0, & result); |
| 3707 |
return result. stringResult.move(); |
| 3708 |
} |
| 3709 |
|
| 3710 |
void Interpreter_stringArrayExpression (Interpreter me, conststring32 expression, STRVEC *out_value, bool *out_owned) { |
| 3711 |
Formula_compile (me, nullptr, expression, kFormula_EXPRESSION_TYPE_STRING_ARRAY, false); |
| 3712 |
Formula_Result result; |
| 3713 |
Formula_run (0, 0, & result); |
| 3714 |
*out_value = result. stringArrayResult; |
| 3715 |
*out_owned = result. owned; |
| 3716 |
result. owned = false; |
| 3717 |
} |
| 3718 |
|
| 3719 |
void Interpreter_anyExpression (Interpreter me, conststring32 expression, Formula_Result *out_result) { |
| 3720 |
Formula_compile (me, nullptr, expression, kFormula_EXPRESSION_TYPE_UNKNOWN, false); |
| 3721 |
Formula_run (0, 0, out_result); |
| 3722 |
} |
| 3723 |
|
| 3724 |
Thing_implement (InterpreterStack, Thing, 0); |
| 3725 |
|
| 3726 |
/* |
| 3727 |
Schematic code: |
| 3728 |
|
| 3729 |
void Interpreter_run (Interpreter me) { |
| 3730 |
my lineNumber = 1; |
| 3731 |
Interpreter_resume (me); |
| 3732 |
} |
| 3733 |
void Interpreter_resume (Interpreter me) { |
| 3734 |
my isHalted = false; |
| 3735 |
try { |
| 3736 |
for (; lineNumber <= my lines.size; lineNumber ++) { |
| 3737 |
if (my isHalted) |
| 3738 |
break; |
| 3739 |
handleOneLine (); |
| 3740 |
} |
| 3741 |
} catch (MelderError) { |
| 3742 |
my owningInterpreterStack -> currentLevel -= 1; |
| 3743 |
throw; |
| 3744 |
} |
| 3745 |
} |
| 3746 |
void runScript (InterpreterStack interpreterStack, conststring32 fileName, integer narg, Stackel args, Editor optionalInterpreterOwningEditor) { |
| 3747 |
Melder_assert (interpreterStack); |
| 3748 |
Interpreter parentInterpreter = interpreterStack -> current_a (); |
| 3749 |
if (parentInterpreter -> isInSecondPass) { |
| 3750 |
interpreterStack -> resumeNextLevelInSecondPass (); |
| 3751 |
} else { |
| 3752 |
Melder_assert (MelderFolder_equal (Melder_peekWorkingDirectory (), & parentInterpreter -> workingDirectory)); |
| 3753 |
//autoMelderSetCurrentFolder folder (& parentInterpreter -> workingDirectory); // TODO: is this superfluous, i.e. does the previous assertion never fire? |
| 3754 |
structMelderFile file { }; |
| 3755 |
try { |
| 3756 |
Melder_relativePathToFile (fileName, & file); |
| 3757 |
autostring32 text = MelderFile_readText (& file); |
| 3758 |
{// scope |
| 3759 |
autoMelderFileSetCurrentFolder folder (& file); // so that callee-relative file names can be used for including include files |
| 3760 |
Melder_includeIncludeFiles (& text); |
| 3761 |
} // back to the default directory of the caller |
| 3762 |
autoInterpreter me = Interpreter_createFromEnvironment ( |
| 3763 |
interpreterStack, // owner always needed |
| 3764 |
optionalInterpreterOwningEditor, |
| 3765 |
& file // so that callee-relative file names can be used inside the script |
| 3766 |
); |
| 3767 |
Interpreter_readParameters (me.get(), text.get()); // TODO: should become a field of structInterpreter |
| 3768 |
my text = text.move(); |
| 3769 |
Interpreter_getArgumentsFromArgs (me.get(), narg, args); // interpret caller-relative paths for infile/outfile/folder arguments |
| 3770 |
interpreterStack -> runDown (me.move(), autostring32(), false); // back to the default directory of the caller |
| 3771 |
} catch (MelderError) { |
| 3772 |
Melder_throw (U"Script ", & file, U" not completed."); // don't refer to 'fileName', because its contents may have changed |
| 3773 |
} |
| 3774 |
} |
| 3775 |
} |
| 3776 |
int endPause (int numberOfContinueButtons, int defaultContinueButton, int cancelContinueButton, |
| 3777 |
conststring32 continueText1, conststring32 continueText2, conststring32 continueText3, |
| 3778 |
conststring32 continueText4, conststring32 continueText5, conststring32 continueText6, |
| 3779 |
conststring32 continueText7, conststring32 continueText8, conststring32 continueText9, |
| 3780 |
conststring32 continueText10, Interpreter interpreter) |
| 3781 |
{ |
| 3782 |
Melder_assert (interpreter); |
| 3783 |
if (interpreter -> isInSecondPass) { |
| 3784 |
Melder_assert (interpreter == thePauseForm_interpreterReference); |
| 3785 |
Melder_assert (! thePauseForm); |
| 3786 |
interpreter -> isInSecondPass = false; // TODO: needed? |
| 3787 |
return thePauseForm_clicked; |
| 3788 |
} else { |
| 3789 |
if (! thePauseForm) |
| 3790 |
Melder_throw (U"Found the function “endPause” without a preceding “beginPause”."); |
| 3791 |
thePauseForm_interpreterReference = interpreter; |
| 3792 |
thePauseForm_savedEditorReference = interpreter -> optionalDynamicEnvironmentEditor(); |
| 3793 |
UiForm_setPauseForm (thePauseForm.get(), numberOfContinueButtons, defaultContinueButton, cancelContinueButton, |
| 3794 |
continueText1, continueText2, continueText3, continueText4, continueText5, |
| 3795 |
continueText6, continueText7, continueText8, continueText9, continueText10, |
| 3796 |
thePauseFormCancelCallback |
| 3797 |
); |
| 3798 |
theCancelContinueButton = cancelContinueButton; |
| 3799 |
UiForm_finish (thePauseForm.get()); |
| 3800 |
thePauseForm_clicked = 0; |
| 3801 |
Melder_assert (interpreter -> owningInterpreterStack); |
| 3802 |
interpreter -> owningInterpreterStack -> haltAll (); |
| 3803 |
UiForm_destroyWhenUnmanaged (thePauseForm.get()); |
| 3804 |
UiForm_do (thePauseForm.get(), false); // put he pause form on the screen |
| 3805 |
return 0; |
| 3806 |
} |
| 3807 |
} |
| 3808 |
void Demo_waitForInput (Interpreter interpreter) { |
| 3809 |
Melder_assert (interpreter); |
| 3810 |
if (interpreter -> isInSecondPass) { |
| 3811 |
interpreter -> isInSecondPass = false; // TODO: needed? |
| 3812 |
} else { |
| 3813 |
if (! theReferenceToTheOnlyDemoEditor) |
| 3814 |
Melder_throw (U"Cannot do demoWaitForInput() if the Demo window isn’t visible."); |
| 3815 |
if (theReferenceToTheOnlyDemoEditor -> waitingForInput &&0) { |
| 3816 |
Melder_throw (U"You cannot work with the Demo window while it is waiting for input. " |
| 3817 |
U"Please click or type into the Demo window or close it."); |
| 3818 |
} |
| 3819 |
//GuiThing_show (theReferenceToTheOnlyDemoEditor -> windowForm); |
| 3820 |
theReferenceToTheOnlyDemoEditor -> clicked = false; |
| 3821 |
theReferenceToTheOnlyDemoEditor -> keyPressed = false; |
| 3822 |
theReferenceToTheOnlyDemoEditor -> waitingForInput = true; |
| 3823 |
interpreter -> owningInterpreterStack -> haltAll (); |
| 3824 |
theReferenceToTheOnlyDemoEditor -> interpreterReference = interpreter; |
| 3825 |
} |
| 3826 |
} |
| 3827 |
static void thePauseFormOkCallback (void *closure) { |
| 3828 |
if (! thePauseForm_interpreterReference) { // interpreter was destroyed? |
| 3829 |
GuiThing_hide (thePauseForm -> d_dialogForm); // BUG: memory leak |
| 3830 |
thePauseForm. releaseToUser(); |
| 3831 |
return; |
| 3832 |
} |
| 3833 |
if (! thePauseForm) // BUG: perhaps there was a mistake in the script |
| 3834 |
return; |
| 3835 |
thePauseForm_clicked = UiForm_getClickedContinueButton (thePauseForm.get()); |
| 3836 |
if (thePauseForm_clicked != theCancelContinueButton) |
| 3837 |
UiForm_Interpreter_addVariables (thePauseForm.get(), (Interpreter) closure); // 'closure', not 'interpreter' or 'theInterpreter'! |
| 3838 |
Melder_assert (thePauseForm_clicked >= 1 && thePauseForm_clicked <= 10); |
| 3839 |
autostring32 clickedText = Melder_dup (thePauseForm -> continueTexts [thePauseForm_clicked].get()); // very safe |
| 3840 |
if (thePauseForm -> d_dialogForm) { |
| 3841 |
GuiThing_hide (thePauseForm -> d_dialogForm); // BUG: memory leak |
| 3842 |
//thePauseForm_interpreterReference -> isInSecondPass = false; // TODO: corrected? and what about isHalted? |
| 3843 |
} |
| 3844 |
thePauseForm. releaseToUser(); // undangle (will be autodestroyed when unmanaged) |
| 3845 |
Melder_assert (! thePauseForm); |
| 3846 |
try { |
| 3847 |
autoPraatBackground background; |
| 3848 |
Melder_assert (thePauseForm_interpreterReference -> owningInterpreterStack); |
| 3849 |
thePauseForm_interpreterReference -> owningInterpreterStack -> resumeFromTop (); |
| 3850 |
} catch (MelderError) { |
| 3851 |
if (thePauseForm_clicked == theCancelContinueButton) |
| 3852 |
Melder_throw (U"This happened after you cancelled the pause form."); |
| 3853 |
else |
| 3854 |
Melder_throw (U"This happened after you clicked “", clickedText.get(), U"” in the pause form."); |
| 3855 |
} |
| 3856 |
} |
| 3857 |
*/ |
| 3858 |
|
| 3859 |
autoInterpreterStack InterpreterStack_create (Editor optionalInterpreterStackOwningEditor) { |
| 3860 |
try { |
| 3861 |
autoInterpreterStack me = Thing_new (InterpreterStack); |
| 3862 |
my optionalInterpreterStackOwningEditor = optionalInterpreterStackOwningEditor; |
| 3863 |
return me; |
| 3864 |
} catch (MelderError) { |
| 3865 |
Melder_throw (U"InterpreterStack not created."); |
| 3866 |
} |
| 3867 |
} |
| 3868 |
|
| 3869 |
void structInterpreterStack :: emptyAll () { |
| 3870 |
for (integer ilevel = 1; ilevel <= our currentLevel; ilevel ++) |
| 3871 |
our interpreters [ilevel]. reset(); |
| 3872 |
our currentLevel = 0; |
| 3873 |
} |
| 3874 |
|
| 3875 |
void structInterpreterStack :: runDown (autoInterpreter interpreter, autostring32 text, const bool reuseVariables) { |
| 3876 |
if (our currentLevel == InterpreterStack_MAXIMUM_NUMBER_OF_LEVELS) |
| 3877 |
Melder_throw (U"Cannot have more than ", InterpreterStack_MAXIMUM_NUMBER_OF_LEVELS, U" levels of nested scripts."); |
| 3878 |
our currentLevel += 1; |
| 3879 |
if (interpreter) |
| 3880 |
our interpreters [our currentLevel] = interpreter.move(); // otherwise, use my existing interpreter |
| 3881 |
const integer oldLevel = our currentLevel; // sets up an assertion below |
| 3882 |
Interpreter childInterpreter = our current_a(); |
| 3883 |
try { |
| 3884 |
autoMelderSetCurrentFolder folder (& childInterpreter -> workingDirectory); |
| 3885 |
Interpreter_run (childInterpreter, text.move(), reuseVariables); |
| 3886 |
} catch (MelderError) { |
| 3887 |
//if (currentLevel == 0) |
| 3888 |
// our emptyAll (); // TODO: figure out when precisely an interpreter can be deleted |
| 3889 |
//Melder_throw (U"Interpreter stack not run completely."); |
| 3890 |
throw; |
| 3891 |
} |
| 3892 |
Melder_assert (our currentLevel == oldLevel); |
| 3893 |
our currentLevel -= 1; |
| 3894 |
if (our currentLevel == 0) { |
| 3895 |
//our emptyAll(); // TODO: successful run of the whole stack; can we delete? Does it depend on halted? |
| 3896 |
return; // nothing else to do |
| 3897 |
} |
| 3898 |
Interpreter parentInterpreter = our current_a(); |
| 3899 |
if (! childInterpreter -> isHalted) { // TODO: why the child interpreter? |
| 3900 |
parentInterpreter -> isInSecondPass = false; // TODO: why the parent interpreter? |
| 3901 |
//childInterpreter. reset(); // TODO: figure out when precisely an interpreter can be deleted |
| 3902 |
} |
| 3903 |
} |
| 3904 |
|
| 3905 |
void structInterpreterStack :: haltAll () { |
| 3906 |
for (integer ilevel = 1; ilevel <= our currentLevel; ilevel ++) { |
| 3907 |
Melder_assert (our interpreters [ilevel]); |
| 3908 |
our interpreters [ilevel] -> isHalted = true; // ensures fast unwinding, without processing any more lines |
| 3909 |
our interpreters [ilevel] -> lineNumber -= 1; // ensure that all runScripts will get a second pass |
| 3910 |
} |
| 3911 |
} |
| 3912 |
|
| 3913 |
void structInterpreterStack :: resumeFromTop () { |
| 3914 |
our currentLevel = 1; |
| 3915 |
Melder_assert (our interpreters [1]); |
| 3916 |
|
| 3917 |
/* |
| 3918 |
Unhalt all. |
| 3919 |
*/ |
| 3920 |
for (integer ilevel = 1; ilevel <= InterpreterStack_MAXIMUM_NUMBER_OF_LEVELS; ilevel ++) |
| 3921 |
if (our interpreters [ilevel]) { |
| 3922 |
our interpreters [ilevel] -> isHalted = false; |
| 3923 |
our interpreters [ilevel] -> isInSecondPass = true; // ensures that all runScripts, the pause window and demoWaitForInput will short-cut |
| 3924 |
} |
| 3925 |
|
| 3926 |
try { |
| 3927 |
autoMelderSetCurrentFolder folder (& our interpreters [1] -> workingDirectory); |
| 3928 |
Interpreter_resume (our interpreters [1].get()); |
| 3929 |
} catch (MelderError) { |
| 3930 |
//if (currentLevel == 0) |
| 3931 |
// our emptyAll (); // TODO: figure out when precisely an interpreter can be deleted; perhaps always, when we're here? |
| 3932 |
//Melder_throw (U"Interpreter stack has not run to completion."); |
| 3933 |
throw; |
| 3934 |
} |
| 3935 |
our currentLevel -= 1; |
| 3936 |
Melder_assert (our currentLevel == 0); // running an interpreter to its end MUST have decremented the level |
| 3937 |
//our emptyAll(); // TODO: successful run of the whole stack; can we delete? Does it depend on halted? |
| 3938 |
} |
| 3939 |
|
| 3940 |
void structInterpreterStack :: resumeNextLevelInSecondPass () { |
| 3941 |
our currentLevel += 1; |
| 3942 |
Melder_assert (our currentLevel <= InterpreterStack_MAXIMUM_NUMBER_OF_LEVELS); |
| 3943 |
Interpreter childInterpreter = our current_a(); |
| 3944 |
{// scope |
| 3945 |
autoMelderSetCurrentFolder folder (& childInterpreter -> workingDirectory); // so that callee-relative file names can be used inside the script |
| 3946 |
Interpreter_resume (childInterpreter); |
| 3947 |
} // back to the default directory of the caller |
| 3948 |
our currentLevel -= 1; |
| 3949 |
if (our currentLevel == 0) |
| 3950 |
return; // nothing to do |
| 3951 |
Interpreter parentInterpreter = our current_a(); |
| 3952 |
//Melder_assert (! childInterpreter -> isHalted); // TODO: why not parentInterpreter? and why can this fire? |
| 3953 |
if (! childInterpreter -> isHalted) { |
| 3954 |
parentInterpreter -> isInSecondPass = false; // TODO: why not childInterpreter? |
| 3955 |
//our interpreters [our currentLevel + 1]. reset(); // TODO: figure out when precisely an interpreter can be deleted |
| 3956 |
} |
| 3957 |
} |
| 3958 |
|
| 3959 |
/* End of file Interpreter.cpp */ |