File: Interpreter.cpp

package info (click to toggle)
praat 5.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 40,728 kB
  • sloc: cpp: 333,759; ansic: 237,947; makefile: 731; python: 340
file content (1594 lines) | stat: -rw-r--r-- 64,026 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
/* Interpreter.cpp
 *
 * Copyright (C) 1993-2011 Paul Boersma
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * pb 2002/03/07 GPL
 * pb 2002/03/25 option menus
 * pb 2002/06/04 include the script compiler
 * pb 2002/09/26 removed bug: crashed if a line in a form contained only the word "comment"
 * pb 2002/11/25 Melder_double
 * pb 2002/12/10 include files
 * pb 2002/12/14 more informative error messages
 * pb 2003/05/19 Melder_atof
 * pb 2003/07/15 assert
 * pb 2003/07/19 if undefined fails
 * pb 2004/10/16 C++ compatible structs
 * pb 2004/12/06 made Interpreter_getArgumentsFromDialog resistant to changes in the script while the dialog is up
 * pb 2005/01/01 there can be spaces before the "form" statement
 * pb 2005/11/26 allow mixing of "option" and "button", as in Ui.c
 * pb 2006/01/11 local variables
 * pb 2007/02/05 preferencesDirectory$, homeDirectory$, temporaryDirectory$
 * pb 2007/04/02 allow comments (with '#' or ';' or empty lines) in forms
 * pb 2007/04/19 allow comments with '!' in forms
 * pb 2007/05/24 some wchar_t
 * pb 2007/06/09 wchar_t
 * pb 2007/08/12 more wchar_t
 * pb 2007/11/30 removed bug: allowed long arguments to the "call" statement (thanks to Ingmar Steiner)
 * pb 2007/12/10 predefined numeric variables macintosh/windows/unix
 * pb 2008/04/30 new Formula API
 * pb 2008/05/01 arrays
 * pb 2008/05/15 praatVersion, praatVersion$
 * pb 2009/01/04 Interpreter_voidExpression
 * pb 2009/01/17 arguments to UiForm callbacks
 * pb 2009/01/20 pause forms
 * pb 2009/03/17 split up structPraat
 * pb 2009/12/22 invokingButtonTitle
 * pb 2010/04/30 guard against leading nonbreaking spaces
 * pb 2011/05/14 C++
 */

#include <ctype.h>
#include "Interpreter.h"
#include "praatP.h"
extern structMelderDir praatDir;
#include "praat_script.h"
#include "Formula.h"
#include "praat_version.h"
#include "UnicodeData.h"

#define Interpreter_WORD 1
#define Interpreter_REAL 2
#define Interpreter_POSITIVE 3
#define Interpreter_INTEGER 4
#define Interpreter_NATURAL 5
#define Interpreter_BOOLEAN 6
#define Interpreter_SENTENCE 7
#define Interpreter_TEXT 8
#define Interpreter_CHOICE 9
#define Interpreter_OPTIONMENU 10
#define Interpreter_BUTTON 11
#define Interpreter_OPTION 12
#define Interpreter_COMMENT 13

Thing_implement (InterpreterVariable, SimpleString, 0);

void structInterpreterVariable :: v_destroy () {
	Melder_free (string);
	Melder_free (stringValue);
	NUMmatrix_free (numericArrayValue. data, 1, 1);
	InterpreterVariable_Parent :: v_destroy ();
}

static InterpreterVariable InterpreterVariable_create (const wchar *key) {
	try {
		if (key [0] == 'e' && key [1] == '\0')
			Melder_throw ("You cannot use 'e' as the name of a variable (e is the constant 2.71...).");
		if (key [0] == 'p' && key [1] == 'i' && key [2] == '\0')
			Melder_throw ("You cannot use 'pi' as the name of a variable (pi is the constant 3.14...).");
		if (key [0] == 'u' && key [1] == 'n' && key [2] == 'd' && key [3] == 'e' && key [4] == 'f' && key [5] == 'i' &&
			key [6] == 'n' && key [7] == 'e' && key [8] == 'd' && key [9] == '\0')
			Melder_throw ("You cannot use 'undefined' as the name of a variable.");
		autoInterpreterVariable me = Thing_new (InterpreterVariable);
		my string = Melder_wcsdup (key);
		return me.transfer();
	} catch (MelderError) {
		Melder_throw ("Interpreter variable not created.");
	}
}

Thing_implement (Interpreter, Thing, 0);

void structInterpreter :: v_destroy () {
	Melder_free (environmentName);
	for (int ipar = 1; ipar <= Interpreter_MAXNUM_PARAMETERS; ipar ++)
		Melder_free (arguments [ipar]);
	forget (variables);
	Interpreter_Parent :: v_destroy ();
}

Interpreter Interpreter_create (wchar_t *environmentName, ClassInfo editorClass) {
	try {
		autoInterpreter me = Thing_new (Interpreter);
		my variables = SortedSetOfString_create ();
		my environmentName = Melder_wcsdup (environmentName);
		my editorClass = editorClass;
		return me.transfer();
	} catch (MelderError) {
		Melder_throw ("Interpreter not created.");
	}
}

Interpreter Interpreter_createFromEnvironment (Editor editor) {
	if (editor == NULL) return Interpreter_create (NULL, NULL);
	return Interpreter_create (editor -> name, editor -> classInfo);
}

void Melder_includeIncludeFiles (wchar **text) {
	for (int depth = 0; ; depth ++) {
		wchar *head = *text;
		long numberOfIncludes = 0;
		if (depth > 10)
			Melder_throw ("Include files nested too deep. Probably cyclic.");
		for (;;) {
			wchar *includeLocation, *includeFileName, *tail, *newText;
			long headLength, includeTextLength, newLength;
			/*
				Look for an include statement. If not found, we have finished.
			 */
			includeLocation = wcsnequ (head, L"include ", 8) ? head : wcsstr (head, L"\ninclude ");
			if (includeLocation == NULL) break;
			if (includeLocation != head) includeLocation += 1;
			numberOfIncludes += 1;
			/*
				Separate out the head.
			 */
			*includeLocation = '\0';
			/*
				Separate out the name of the include file.
			 */
			includeFileName = includeLocation + 8;
			while (*includeFileName == ' ' || *includeFileName == '\t') includeFileName ++;
			tail = includeFileName;
			while (*tail != '\n' && *tail != '\0') tail ++;
			if (*tail == '\n') {
				*tail = '\0';
				tail += 1;
			}
			/*
				Get the contents of the include file.
			 */
			structMelderFile includeFile = { 0 };
			Melder_relativePathToFile (includeFileName, & includeFile);
			autostring includeText;
			try {
				includeText.reset (MelderFile_readText (& includeFile));
			} catch (MelderError) {
				Melder_throw ("Include file ", & includeFile, " not read.");
			}
			/*
				Construct the new text.
			 */
			headLength = (head - *text) + wcslen (head);
			includeTextLength = wcslen (includeText.peek());
			newLength = headLength + includeTextLength + 1 + wcslen (tail);
			newText = Melder_malloc (wchar, newLength + 1);
			wcscpy (newText, *text);
			wcscpy (newText + headLength, includeText.peek());
			wcscpy (newText + headLength + includeTextLength, L"\n");
			wcscpy (newText + headLength + includeTextLength + 1, tail);
			/*
				Replace the old text with the new.
			 */
			Melder_free (*text);
			*text = newText;
			/*
				Cycle.
			 */
			head = *text + headLength + includeTextLength + 1;
		}
		if (numberOfIncludes == 0) break;
	}
}

long Interpreter_readParameters (Interpreter me, wchar *text) {
	wchar *formLocation = NULL;
	long npar = 0;
	my dialogTitle [0] = '\0';
	/*
	 * Look for a "form" line.
	 */
	{// scope
		wchar *p = text;
		for (;;) {
			while (*p == ' ' || *p == '\t') p ++;
			if (wcsnequ (p, L"form ", 5)) {
				formLocation = p;
				break;
			}
			while (*p != '\0' && *p != '\n') p ++;
			if (*p == '\0') break;
			p ++;   /* Skip newline symbol. */
		}
	}
	/*
	 * If there is no "form" line, there are no parameters.
	 */
	if (formLocation) {
		wchar *dialogTitle = formLocation + 5, *newLine;
		while (*dialogTitle == ' ' || *dialogTitle == '\t') dialogTitle ++;
		newLine = wcschr (dialogTitle, '\n');
		if (newLine) *newLine = '\0';
		wcscpy (my dialogTitle, dialogTitle);
		if (newLine) *newLine = '\n';
		my numberOfParameters = 0;
		while (newLine) {
			wchar_t *line = newLine + 1, *p;
			int type = 0;
			while (*line == ' ' || *line == '\t') line ++;
			while (*line == '#' || *line == ';' || *line == '!' || *line == '\n') {
				newLine = wcschr (line, '\n');
				if (newLine == NULL)
					Melder_throw ("Unfinished form.");
				line = newLine + 1;
				while (*line == ' ' || *line == '\t') line ++;
			}
			if (wcsnequ (line, L"endform", 7)) break;
			if (wcsnequ (line, L"word ", 5)) { type = Interpreter_WORD; p = line + 5; }
			else if (wcsnequ (line, L"real ", 5)) { type = Interpreter_REAL; p = line + 5; }
			else if (wcsnequ (line, L"positive ", 9)) { type = Interpreter_POSITIVE; p = line + 9; }
			else if (wcsnequ (line, L"integer ", 8)) { type = Interpreter_INTEGER; p = line + 8; }
			else if (wcsnequ (line, L"natural ", 8)) { type = Interpreter_NATURAL; p = line + 8; }
			else if (wcsnequ (line, L"boolean ", 8)) { type = Interpreter_BOOLEAN; p = line + 8; }
			else if (wcsnequ (line, L"sentence ", 9)) { type = Interpreter_SENTENCE; p = line + 9; }
			else if (wcsnequ (line, L"text ", 5)) { type = Interpreter_TEXT; p = line + 5; }
			else if (wcsnequ (line, L"choice ", 7)) { type = Interpreter_CHOICE; p = line + 7; }
			else if (wcsnequ (line, L"optionmenu ", 11)) { type = Interpreter_OPTIONMENU; p = line + 11; }
			else if (wcsnequ (line, L"button ", 7)) { type = Interpreter_BUTTON; p = line + 7; }
			else if (wcsnequ (line, L"option ", 7)) { type = Interpreter_OPTION; p = line + 7; }
			else if (wcsnequ (line, L"comment ", 8)) { type = Interpreter_COMMENT; p = line + 8; }
			else {
				newLine = wcschr (line, '\n');
				if (newLine) *newLine = '\0';
				Melder_error_ ("Unknown parameter type:\n\"", line, "\".");
				if (newLine) *newLine = '\n';
				throw MelderError ();
				return 0;
			}
			/*
				Example:
					form Something
						real Time_(s) 3.14 (= pi)
						choice Colour 2
							button Red
							button Green
							button Blue
					endform
				my parameters [1] := "Time_(s)"
				my parameters [2] := "Colour"
				my parameters [3] := ""
				my parameters [4] := ""
				my parameters [5] := ""
				my arguments [1] := "3.14 (= pi)"
				my arguments [2] := "2"
				my arguments [3] := "Red"   (funny, but needed in Interpreter_getArgumentsFromString)
				my arguments [4] := "Green"
				my arguments [5] := "Blue"
			*/
			if (type <= Interpreter_OPTIONMENU) {
				while (*p == ' ' || *p == '\t') p ++;
				if (*p == '\n' || *p == '\0')
					Melder_throw ("Missing parameter:\n\"", line, "\".");
				wchar_t *q = my parameters [++ my numberOfParameters];
				while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') * (q ++) = * (p ++);
				*q = '\0';
				npar ++;
			} else {
				my parameters [++ my numberOfParameters] [0] = '\0';
			}
			while (*p == ' ' || *p == '\t') p ++;
			newLine = wcschr (p, '\n');
			if (newLine) *newLine = '\0';
			Melder_free (my arguments [my numberOfParameters]);
			my arguments [my numberOfParameters] = Melder_wcsdup_f (p);
			if (newLine) *newLine = '\n';
			my types [my numberOfParameters] = type;
		}
	} else {
		npar = my numberOfParameters = 0;
	}
	return npar;
}

UiForm Interpreter_createForm (Interpreter me, GuiObject parent, const wchar *path,
	void (*okCallback) (UiForm, const wchar *, Interpreter, const wchar *, bool, void *), void *okClosure)
{
	UiForm form = UiForm_create (parent, my dialogTitle [0] ? my dialogTitle : L"Script arguments", okCallback, okClosure, NULL, NULL);
	Any radio = NULL;
	if (path) UiForm_addText (form, L"$file", path);
	for (int ipar = 1; ipar <= my numberOfParameters; ipar ++) {
		/*
		 * Convert underscores to spaces.
		 */
		wchar_t parameter [100], *p = & parameter [0];
		wcscpy (parameter, my parameters [ipar]);
		while (*p) { if (*p == '_') *p = ' '; p ++; }
		switch (my types [ipar]) {
			case Interpreter_WORD:
				UiForm_addWord (form, parameter, my arguments [ipar]); break;
			case Interpreter_REAL:
				UiForm_addReal (form, parameter, my arguments [ipar]); break;
			case Interpreter_POSITIVE:
				UiForm_addPositive (form, parameter, my arguments [ipar]); break;
			case Interpreter_INTEGER:
				UiForm_addInteger (form, parameter, my arguments [ipar]); break;
			case Interpreter_NATURAL:
				UiForm_addNatural (form, parameter, my arguments [ipar]); break;
			case Interpreter_BOOLEAN:
				UiForm_addBoolean (form, parameter, my arguments [ipar] [0] == '1' ||
					my arguments [ipar] [0] == 'y' || my arguments [ipar] [0] == 'Y' ||
					(my arguments [ipar] [0] == 'o' && my arguments [ipar] [1] == 'n')); break;
			case Interpreter_SENTENCE:
				UiForm_addSentence (form, parameter, my arguments [ipar]); break;
			case Interpreter_TEXT:
				UiForm_addText (form, parameter, my arguments [ipar]); break;
			case Interpreter_CHOICE:
				radio = UiForm_addRadio (form, parameter, wcstol (my arguments [ipar], NULL, 10)); break;
			case Interpreter_OPTIONMENU:
				radio = UiForm_addOptionMenu (form, parameter, wcstol (my arguments [ipar], NULL, 10)); break;
			case Interpreter_BUTTON:
				if (radio) UiRadio_addButton (radio, my arguments [ipar]); break;
			case Interpreter_OPTION:
				if (radio) UiOptionMenu_addButton (radio, my arguments [ipar]); break;
			case Interpreter_COMMENT:
				UiForm_addLabel (form, parameter, my arguments [ipar]); break;
			default:
				UiForm_addWord (form, parameter, my arguments [ipar]); break;
		}
		/*
		 * Strip parentheses and colon off parameter name.
		 */
		if ((p = wcschr (my parameters [ipar], '(')) != NULL) {
			*p = '\0';
			if (p - my parameters [ipar] > 0 && p [-1] == '_') p [-1] = '\0';
		}
		p = my parameters [ipar];
		if (*p != '\0' && p [wcslen (p) - 1] == ':') p [wcslen (p) - 1] = '\0';
	}
	UiForm_finish (form);
	return form;
}

void Interpreter_getArgumentsFromDialog (Interpreter me, Any dialog) {
	for (int ipar = 1; ipar <= my numberOfParameters; ipar ++) {
		wchar parameter [100], *p;
		/*
		 * Strip parentheses and colon off parameter name.
		 */
		if ((p = wcschr (my parameters [ipar], '(')) != NULL) {
			*p = '\0';
			if (p - my parameters [ipar] > 0 && p [-1] == '_') p [-1] = '\0';
		}
		p = my parameters [ipar];
		if (*p != '\0' && p [wcslen (p) - 1] == ':') p [wcslen (p) - 1] = '\0';
		/*
		 * Convert underscores to spaces.
		 */
		wcscpy (parameter, my parameters [ipar]);
		p = & parameter [0]; while (*p) { if (*p == '_') *p = ' '; p ++; }
		switch (my types [ipar]) {
			case Interpreter_REAL:
			case Interpreter_POSITIVE: {
				double value = UiForm_getReal_check (dialog, parameter); therror
				Melder_free (my arguments [ipar]);
				my arguments [ipar] = Melder_calloc_f (wchar_t, 40);
				wcscpy (my arguments [ipar], Melder_double (value));
				break;
			}
			case Interpreter_INTEGER:
			case Interpreter_NATURAL:
			case Interpreter_BOOLEAN: {
				long value = UiForm_getInteger (dialog, parameter); therror
				Melder_free (my arguments [ipar]);
				my arguments [ipar] = Melder_calloc_f (wchar_t, 40);
				swprintf (my arguments [ipar], 40, L"%ld", value);
				break;
			}
			case Interpreter_CHOICE:
			case Interpreter_OPTIONMENU: {
				long integerValue = 0;
				wchar_t *stringValue = NULL;
				integerValue = UiForm_getInteger (dialog, parameter); therror
				stringValue = UiForm_getString (dialog, parameter); therror
				Melder_free (my arguments [ipar]);
				my arguments [ipar] = Melder_calloc_f (wchar, 40);
				swprintf (my arguments [ipar], 40, L"%ld", integerValue);
				wcscpy (my choiceArguments [ipar], stringValue);
				break;
			}
			case Interpreter_BUTTON:
			case Interpreter_OPTION:
			case Interpreter_COMMENT:
				break;
			default: {
				wchar *value = UiForm_getString (dialog, parameter);
				Melder_free (my arguments [ipar]);
				my arguments [ipar] = Melder_wcsdup_f (value);
				break;
			}
		}
	}
}

void Interpreter_getArgumentsFromString (Interpreter me, const wchar *arguments) {
	int size = my numberOfParameters;
	long length = wcslen (arguments);
	while (size >= 1 && my parameters [size] [0] == '\0')
		size --;   /* Ignore fields without a variable name (button, comment). */
	for (int ipar = 1; ipar <= size; ipar ++) {
		wchar *p = my parameters [ipar];
		/*
		 * Ignore buttons and comments again.
		 */
		if (! *p) continue;
		/*
		 * Strip parentheses and colon off parameter name.
		 */
		if ((p = wcschr (p, '(')) != NULL) {
			*p = '\0';
			if (p - my parameters [ipar] > 0 && p [-1] == '_') p [-1] = '\0';
		}
		p = my parameters [ipar];
		if (*p != '\0' && p [wcslen (p) - 1] == ':') p [wcslen (p) - 1] = '\0';
	}
	for (int ipar = 1; ipar < size; ipar ++) {
		int ichar = 0;
		/*
		 * Ignore buttons and comments again. The buttons will keep their labels as "arguments".
		 */
		if (my parameters [ipar] [0] == '\0') continue;
		Melder_free (my arguments [ipar]);   // erase the current values, probably the default values
		my arguments [ipar] = Melder_calloc_f (wchar_t, length + 1);   // replace with the actual arguments
		/*
		 * Skip spaces until next argument.
		 */
		while (*arguments == ' ' || *arguments == '\t') arguments ++;
		/*
		 * The argument is everything up to the next space, or, if that starts with a double quote,
		 * everything between this quote and the matching double quote;
		 * in this case, the argument can represent a double quote by a sequence of two double quotes.
		 * Example: the string
		 *     "I said ""hello"""
		 * will be passed to the dialog as a single argument containing the text
		 *     I said "hello"
		 */
		if (*arguments == '\"') {
			arguments ++;   // do not include leading double quote
			for (;;) {
				if (*arguments == '\0')
					Melder_throw ("Missing matching quote.");
				if (*arguments == '\"' && * ++ arguments != '\"') break;   // remember second quote
				my arguments [ipar] [ichar ++] = *arguments ++;
			}
		} else {
			while (*arguments != ' ' && *arguments != '\t' && *arguments != '\0')
				my arguments [ipar] [ichar ++] = *arguments ++;
		}
		my arguments [ipar] [ichar] = '\0';   // trailing null byte
	}
	/* The last item is handled separately, because it consists of the rest of the line.
	 * Leading spaces are skipped, but trailing spaces are included.
	 */
	if (size > 0) {
		while (*arguments == ' ' || *arguments == '\t') arguments ++;
		Melder_free (my arguments [size]);
		my arguments [size] = Melder_wcsdup_f (arguments);
	}
	/*
	 * Convert booleans and choices to numbers.
	 */
	for (int ipar = 1; ipar <= size; ipar ++) {
		if (my types [ipar] == Interpreter_BOOLEAN) {
			wchar_t *arg = & my arguments [ipar] [0];
			if (wcsequ (arg, L"1") || wcsequ (arg, L"yes") || wcsequ (arg, L"on") ||
			    wcsequ (arg, L"Yes") || wcsequ (arg, L"On") || wcsequ (arg, L"YES") || wcsequ (arg, L"ON"))
			{
				wcscpy (arg, L"1");
			} else if (wcsequ (arg, L"0") || wcsequ (arg, L"no") || wcsequ (arg, L"off") ||
			    wcsequ (arg, L"No") || wcsequ (arg, L"Off") || wcsequ (arg, L"NO") || wcsequ (arg, L"OFF"))
			{
				wcscpy (arg, L"0");
			} else {
				Melder_throw ("Unknown value \"", arg, "\" for boolean \"", my parameters [ipar], "\".");
			}
		} else if (my types [ipar] == Interpreter_CHOICE) {
			int jpar;
			wchar_t *arg = & my arguments [ipar] [0];
			for (jpar = ipar + 1; jpar <= my numberOfParameters; jpar ++) {
				if (my types [jpar] != Interpreter_BUTTON && my types [jpar] != Interpreter_OPTION)
					Melder_throw ("Unknown value \"", arg, "\" for choice \"", my parameters [ipar], "\".");
				if (wcsequ (my arguments [jpar], arg)) {   // the button labels are in the arguments; see Interpreter_readParameters
					swprintf (arg, 40, L"%d", jpar - ipar);
					wcscpy (my choiceArguments [ipar], my arguments [jpar]);
					break;
				}
			}
			if (jpar > my numberOfParameters)
				Melder_throw ("Unknown value \"", arg, "\" for choice \"", my parameters [ipar], "\".");
		} else if (my types [ipar] == Interpreter_OPTIONMENU) {
			int jpar;
			wchar_t *arg = & my arguments [ipar] [0];
			for (jpar = ipar + 1; jpar <= my numberOfParameters; jpar ++) {
				if (my types [jpar] != Interpreter_OPTION && my types [jpar] != Interpreter_BUTTON)
					Melder_throw ("Unknown value \"", arg, "\" for option menu \"", my parameters [ipar], "\".");
				if (wcsequ (my arguments [jpar], arg)) {
					swprintf (arg, 40, L"%d", jpar - ipar);
					wcscpy (my choiceArguments [ipar], my arguments [jpar]);
					break;
				}
			}
			if (jpar > my numberOfParameters)
				Melder_throw ("Unknown value \"", arg, "\" for option menu \"", my parameters [ipar], "\".");
		}
	}
}

static int Interpreter_addNumericVariable (Interpreter me, const wchar *key, double value) {
	InterpreterVariable variable = InterpreterVariable_create (key);
	variable -> numericValue = value;
	Collection_addItem (my variables, variable);
	return 1;
}

static InterpreterVariable Interpreter_addStringVariable (Interpreter me, const wchar *key, const wchar *value) {
	InterpreterVariable variable = InterpreterVariable_create (key);
	variable -> stringValue = Melder_wcsdup (value);
	Collection_addItem (my variables, variable);
	return variable;
}

InterpreterVariable Interpreter_hasVariable (Interpreter me, const wchar *key) {
	long ivar = 0;
	wchar_t variableNameIncludingProcedureName [1+200];
	Melder_assert (key != NULL);
	if (key [0] == '.') {
		wcscpy (variableNameIncludingProcedureName, my procedureNames [my callDepth]);
		wcscat (variableNameIncludingProcedureName, key);
	} else {
		wcscpy (variableNameIncludingProcedureName, key);
	}
	ivar = SortedSetOfString_lookUp (my variables, variableNameIncludingProcedureName);
	return ivar ? (InterpreterVariable) my variables -> item [ivar] : NULL;
}

InterpreterVariable Interpreter_lookUpVariable (Interpreter me, const wchar *key) {
	InterpreterVariable var = NULL;
	wchar variableNameIncludingProcedureName [1+200];
	Melder_assert (key != NULL);
	if (key [0] == '.') {
		wcscpy (variableNameIncludingProcedureName, my procedureNames [my callDepth]);
		wcscat (variableNameIncludingProcedureName, key);
	} else {
		wcscpy (variableNameIncludingProcedureName, key);
	}
	var = Interpreter_hasVariable (me, variableNameIncludingProcedureName);
	if (var) return var;
	var = InterpreterVariable_create (variableNameIncludingProcedureName);
	Collection_addItem (my variables, var);
	return Interpreter_hasVariable (me, variableNameIncludingProcedureName);
}

static long lookupLabel (Interpreter me, const wchar *labelName) {
	for (long ilabel = 1; ilabel <= my numberOfLabels; ilabel ++)
		if (wcsequ (labelName, my labelNames [ilabel]))
			return ilabel;
	Melder_throw ("Unknown label \"", labelName, "\".");
}

static bool isCommand (const wchar *p) {
	/*
	 * Things that start with "nowarn", "noprogress", or "nocheck" are commands.
	 */
	if (p [0] == 'n' && p [1] == 'o' &&
		(wcsnequ (p + 2, L"warn ", 5) || wcsnequ (p + 2, L"progress ", 9) || wcsnequ (p + 2, L"check ", 6))) return true;
	if (wcsnequ (p, L"demo ", 5)) return true;
	/*
	 * Otherwise, things that start with lower case are formulas.
	 */
	if (! isupper (*p)) return false;
	/*
	 * The remaining possibility is things that start with upper case.
	 * If they contain an underscore, they are object names, hence we must have a formula.
	 * Otherwise, we have a command.
	 */
	while (isalnum (*p)) p ++;
	return *p != '_';
}

static void parameterToVariable (Interpreter me, int type, const wchar_t *in_parameter, int ipar) {
	wchar_t parameter [200];
	Melder_assert (type != 0);
	wcscpy (parameter, in_parameter);
	if (type >= Interpreter_REAL && type <= Interpreter_BOOLEAN) {
		Interpreter_addNumericVariable (me, parameter, Melder_atof (my arguments [ipar]));
	} else if (type == Interpreter_CHOICE || type == Interpreter_OPTIONMENU) {
		Interpreter_addNumericVariable (me, parameter, Melder_atof (my arguments [ipar]));
		wcscat (parameter, L"$");
		Interpreter_addStringVariable (me, parameter, my choiceArguments [ipar]);
	} else if (type == Interpreter_BUTTON || type == Interpreter_OPTION || type == Interpreter_COMMENT) {
		/* Do not add a variable. */
	} else {
		wcscat (parameter, L"$");
		Interpreter_addStringVariable (me, parameter, my arguments [ipar]);
	}
}

void Interpreter_run (Interpreter me, wchar *text) {
	autoNUMvector <wchar *> lines;   // not autostringvector, because the elements are reference copies
	long lineNumber = 0;
	bool assertionFailed = false;
	try {
		static MelderString valueString = { 0 };   // to divert the info
		static MelderString assertErrorString = { 0 };
		wchar_t *command = text;
		autoMelderString command2;
		autoMelderString buffer;
		long numberOfLines = 0, assertErrorLineNumber = 0, callStack [1 + Interpreter_MAX_CALL_DEPTH];
		int atLastLine = FALSE, fromif = FALSE, fromendfor = FALSE, callDepth = 0, chopped = 0, ipar;
		my callDepth = 0;
		/*
		 * The "environment" is NULL if we are in the Praat shell, or an editor otherwise.
		 */
		if (my editorClass) {
			praatP. editor = praat_findEditorFromString (my environmentName);
		} else {
			praatP. editor = NULL;
		}
		/*
		 * Start.
		 */
		my running = true;
		/*
		 * Count lines and set the newlines to zero.
		 */
		while (! atLastLine) {
			wchar_t *endOfLine = command;
			while (*endOfLine != '\n' && *endOfLine != '\0') endOfLine ++;
			if (*endOfLine == '\0') atLastLine = TRUE;
			*endOfLine = '\0';
			numberOfLines ++;
			command = endOfLine + 1;
		}
		/*
		 * Remember line starts and labels.
		 */
		lines.reset (1, numberOfLines);
		for (lineNumber = 1, command = text; lineNumber <= numberOfLines; lineNumber ++, command += wcslen (command) + 1 + chopped) {
			int length;
			while (*command == ' ' || *command == '\t' || *command == UNICODE_NO_BREAK_SPACE) command ++;   // nbsp can occur for scripts copied from the manual
			length = wcslen (command);
			/*
			 * Chop trailing spaces?
			 */
			/*chopped = 0;
			while (length > 0) { char kar = command [-- length]; if (kar != ' ' && kar != '\t') break; command [length] = '\0'; chopped ++; }*/
			lines [lineNumber] = command;
			if (wcsnequ (command, L"label ", 6)) {
				int ilabel;
				for (ilabel = 1; ilabel <= my numberOfLabels; ilabel ++)
					if (wcsequ (command + 6, my labelNames [ilabel]))
						Melder_throw ("Duplicate label \"", command + 6, "\".");
				if (my numberOfLabels >= Interpreter_MAXNUM_LABELS)
					Melder_throw ("Too many labels.");
				swprintf (my labelNames [++ my numberOfLabels], 50, L"%.47ls", command + 6);
				my labelLines [my numberOfLabels] = lineNumber;
			}
		}
		/*
		 * Connect continuation lines.
		 */
		for (lineNumber = numberOfLines; lineNumber >= 2; lineNumber --) {
			wchar_t *line = lines [lineNumber];
			if (line [0] == '.' && line [1] == '.' && line [2] == '.') {
				wchar_t *previous = lines [lineNumber - 1];
				MelderString_copy (& command2, line + 3);
				MelderString_get (& command2, previous + wcslen (previous));
				static wchar emptyLine [] = { '\0' };
				lines [lineNumber] = emptyLine;
			}
		}
		/*
		 * Copy the parameter names and argument values into the array of variables.
		 */
		forget (my variables);
		my variables = SortedSetOfString_create ();
		for (ipar = 1; ipar <= my numberOfParameters; ipar ++) {
			wchar_t parameter [200];
			/*
			 * Create variable names as-are and variable names without capitals.
			 */
			wcscpy (parameter, my parameters [ipar]);
			parameterToVariable (me, my types [ipar], parameter, ipar); therror
			if (parameter [0] >= 'A' && parameter [0] <= 'Z') {
				parameter [0] = tolower (parameter [0]);
				parameterToVariable (me, my types [ipar], parameter, ipar); therror
			}
		}
		/*
		 * Initialize some variables.
		 */
		Interpreter_addStringVariable (me, L"newline$", L"\n");
		Interpreter_addStringVariable (me, L"tab$", L"\t");
		Interpreter_addStringVariable (me, L"shellDirectory$", Melder_getShellDirectory ());
		structMelderDir dir = { { 0 } }; Melder_getDefaultDir (& dir);
		Interpreter_addStringVariable (me, L"defaultDirectory$", Melder_dirToPath (& dir));
		Interpreter_addStringVariable (me, L"preferencesDirectory$", Melder_dirToPath (& praatDir));
		Melder_getHomeDir (& dir);
		Interpreter_addStringVariable (me, L"homeDirectory$", Melder_dirToPath (& dir));
		Melder_getTempDir (& dir);
		Interpreter_addStringVariable (me, L"temporaryDirectory$", Melder_dirToPath (& dir));
		#if defined (macintosh)
			Interpreter_addNumericVariable (me, L"macintosh", 1);
			Interpreter_addNumericVariable (me, L"windows", 0);
			Interpreter_addNumericVariable (me, L"unix", 0);
		#elif defined (_WIN32)
			Interpreter_addNumericVariable (me, L"macintosh", 0);
			Interpreter_addNumericVariable (me, L"windows", 1);
			Interpreter_addNumericVariable (me, L"unix", 0);
		#elif defined (UNIX)
			Interpreter_addNumericVariable (me, L"macintosh", 0);
			Interpreter_addNumericVariable (me, L"windows", 0);
			Interpreter_addNumericVariable (me, L"unix", 1);
		#else
			Interpreter_addNumericVariable (me, L"macintosh", 0);
			Interpreter_addNumericVariable (me, L"windows", 0);
			Interpreter_addNumericVariable (me, L"unix", 0);
		#endif
		Interpreter_addNumericVariable (me, L"left", 1);   // to accommodate scripts from before Praat 5.2.06
		Interpreter_addNumericVariable (me, L"right", 2);   // to accommodate scripts from before Praat 5.2.06
		Interpreter_addNumericVariable (me, L"mono", 1);   // to accommodate scripts from before Praat 5.2.06
		Interpreter_addNumericVariable (me, L"stereo", 2);   // to accommodate scripts from before Praat 5.2.06
		Interpreter_addNumericVariable (me, L"all", 0);   // to accommodate scripts from before Praat 5.2.06
		Interpreter_addNumericVariable (me, L"average", 0);   // to accommodate scripts from before Praat 5.2.06
		#define xstr(s) str(s)
		#define str(s) #s
		Interpreter_addStringVariable (me, L"praatVersion$", L"" xstr(PRAAT_VERSION_STR));
		Interpreter_addNumericVariable (me, L"praatVersion", PRAAT_VERSION_NUM);
		/*
		 * Execute commands.
		 */
		#define wordEnd(c)  (c == '\0' || c == ' ' || c == '\t')
		for (lineNumber = 1; lineNumber <= numberOfLines; lineNumber ++) {
			if (my stopped) break;
			try {
				int c0, fail = FALSE;
				wchar_t *p;
				MelderString_copy (& command2, lines [lineNumber]);
				c0 = command2. string [0];
				if (c0 == '\0') continue;
				/*
				 * Substitute variables.
				 */
				for (p = & command2. string [0]; *p !='\0'; p ++) if (*p == '\'') {
					/*
					 * Found a left quote. Search for a matching right quote.
					 */
					wchar_t *q = p + 1, varName [300], *r, *s, *colon;
					int precision = -1, percent = FALSE;
					while (*q != '\0' && *q != '\'' && q - p < 299) q ++;
					if (*q == '\0') break;   /* No matching right quote: done with this line. */
					if (q - p == 1 || q - p >= 299) continue;   /* Ignore empty variable names. */
					/*
					 * Found a right quote. Get potential variable name.
					 */
					for (r = p + 1, s = varName; q - r > 0; r ++, s ++) *s = *r;
					*s = '\0';   /* Trailing null byte. */
					colon = wcschr (varName, ':');
					if (colon) {
						precision = wcstol (colon + 1, NULL, 10);
						if (wcschr (colon + 1, '%')) percent = TRUE;
						*colon = '\0';
					}
					InterpreterVariable var = Interpreter_hasVariable (me, varName);
					if (var) {
						/*
						 * Found a variable (p points to the left quote, q to the right quote). Substitute.
						 */
						int headlen = p - command2.string;
						const wchar_t *string = var -> stringValue ? var -> stringValue :
							percent ? Melder_percent (var -> numericValue, precision) :
							precision >= 0 ?  Melder_fixed (var -> numericValue, precision) :
							Melder_double (var -> numericValue);
						int arglen = wcslen (string);
						MelderString_ncopy (& buffer, command2.string, headlen);
						MelderString_append (& buffer, string, q + 1);
						MelderString_copy (& command2, buffer.string);   // This invalidates p!! (really bad bug 20070203)
						p = command2.string + headlen + arglen - 1;
					} else {
						p = q - 1;   /* Go to before next quote. */
					}
				}
				c0 = command2.string [0];   /* Resume in order to allow things like 'c$' = 5 */
				if ((c0 < 'a' || c0 > 'z') && ! (c0 == '.' && command2.string [1] >= 'a' && command2.string [1] <= 'z')) {
					praat_executeCommand (me, command2.string); therror
				/*
				 * Interpret control flow and variables.
				 */
				} else switch (c0) {
					case '.':
						fail = TRUE;
						break;
					case 'a':
						if (wcsnequ (command2.string, L"assert ", 7)) {
							double value;
							Interpreter_numericExpression (me, command2.string + 7, & value); therror
							if (value == 0.0 || value == NUMundefined) {
								assertionFailed = TRUE;
								Melder_throw ("Script assertion fails in line ", lineNumber,
									" (", value ? "undefined" : "false", "):\n   ", command2.string + 7);
							}
						} else if (wcsnequ (command2.string, L"asserterror ", 12)) {
							MelderString_copy (& assertErrorString, command2.string + 12);
							assertErrorLineNumber = lineNumber;
						} else fail = TRUE;
						break;
					case 'b':
						fail = TRUE;
						break;
					case 'c':
						if (wcsnequ (command2.string, L"call ", 5)) {
							wchar_t *p = command2.string + 5, *callName, *procName;
							long iline;
							int hasArguments, callLength;
							while (*p == ' ' || *p == '\t') p ++;
							callName = p;
							while (*p != '\0' && *p != ' ' && *p != '\t') p ++;
							if (p == callName) Melder_throw ("Missing procedure name after 'call'.");
							hasArguments = *p != '\0';
							*p = '\0';   /* Close procedure name. */
							callLength = wcslen (callName);
							for (iline = 1; iline <= numberOfLines; iline ++) {
								wchar_t *linei = lines [iline], *q;
								int hasParameters;
								if (linei [0] != 'p' || linei [1] != 'r' || linei [2] != 'o' || linei [3] != 'c' ||
									linei [4] != 'e' || linei [5] != 'd' || linei [6] != 'u' || linei [7] != 'r' ||
									linei [8] != 'e' || linei [9] != ' ') continue;
								q = lines [iline] + 10;
								while (*q == ' ' || *q == '\t') q ++;
								procName = q;
								while (*q != '\0' && *q != ' ' && *q != '\t') q ++;
								if (q == procName) Melder_throw ("Missing procedure name after 'procedure'.");
								hasParameters = *q != '\0';
								if (q - procName == callLength && wcsnequ (procName, callName, callLength)) {
									if (hasArguments && ! hasParameters)
										Melder_throw ("Call to procedure \"", callName, "\" has too many arguments.");
									if (hasParameters && ! hasArguments)
										Melder_throw ("Call to procedure \"", callName, "\" has too few arguments.");
									if (++ my callDepth > Interpreter_MAX_CALL_DEPTH)
										Melder_throw ("Call depth greater than ", Interpreter_MAX_CALL_DEPTH, ".");
									wcscpy (my procedureNames [my callDepth], callName);
									if (hasParameters) {
										++ p;   /* First argument. */
										++ q;   /* First parameter. */
										while (*q) {
											wchar_t *par, save;
											static MelderString arg = { 0 };
											MelderString_empty (& arg);
											while (*p == ' ' || *p == '\t') p ++;
											while (*q == ' ' || *q == '\t') q ++;
											par = q;
											while (*q != '\0' && *q != ' ' && *q != '\t') q ++;   /* Collect parameter name. */
											if (*q) {   /* Does anything follow the parameter name? */
												if (*p == '\"') {
													p ++;   /* Skip initial quote. */
													while (*p != '\0') {
														if (*p == '\"') {   /* Quote signals end-of-string or string-internal quote. */
															if (p [1] == '\"') {   /* Double quote signals string-internal quote. */
																MelderString_appendCharacter (& arg, '\"');
																p += 2;   /* Skip second quote. */
															} else {   /* Single quote signals end-of-string. */
																break;
															}
														} else {
															MelderString_appendCharacter (& arg, *p ++);
														}
													}
												} else {
													while (*p != '\0' && *p != ' ' && *p != '\t')
														MelderString_appendCharacter (& arg, *p ++);   /* White space separates. */
												}
												if (*p) { *p = '\0'; p ++; }
											} else {   /* Else rest of line. */
												while (*p != '\0')
													MelderString_appendCharacter (& arg, *p ++);
											}
											if (q [-1] == '$') {
												save = *q; *q = '\0';
												InterpreterVariable var = Interpreter_lookUpVariable (me, par); *q = save; therror
												Melder_free (var -> stringValue);
												var -> stringValue = Melder_wcsdup_f (arg.string);
											} else {
												double value;
												my callDepth --;
												Interpreter_numericExpression (me, arg.string, & value);
												my callDepth ++;
												save = *q; *q = '\0'; 
												InterpreterVariable var = Interpreter_lookUpVariable (me, par); *q = save; therror
												var -> numericValue = value;
											}
										}
									}
									if (callDepth == Interpreter_MAX_CALL_DEPTH)
										Melder_throw ("Call depth greater than ", Interpreter_MAX_CALL_DEPTH, ".");
									callStack [++ callDepth] = lineNumber;
									lineNumber = iline;
									break;
								}
							}
							if (iline > numberOfLines) Melder_throw ("Procedure \"", callName, "\" not found.");
						} else fail = TRUE;
						break;
					case 'd':
						if (wcsnequ (command2.string, L"dec ", 4)) {
							InterpreterVariable var = Interpreter_lookUpVariable (me, command2.string + 4); therror
							var -> numericValue -= 1.0;
						} else fail = TRUE;
						break;
					case 'e':
						if (command2.string [1] == 'n' && command2.string [2] == 'd') {
							if (wcsnequ (command2.string, L"endif", 5) && wordEnd (command2.string [5])) {
								/* Ignore. */
							} else if (wcsnequ (command2.string, L"endfor", 6) && wordEnd (command2.string [6])) {
								int depth = 0;
								long iline;
								for (iline = lineNumber - 1; iline > 0; iline --) {
									wchar_t *line = lines [iline];
									if (line [0] == 'f' && line [1] == 'o' && line [2] == 'r' && line [3] == ' ') {
										if (depth == 0) { lineNumber = iline - 1; fromendfor = TRUE; break; }   /* Go before 'for'. */
										else depth --;
									} else if (wcsnequ (lines [iline], L"endfor", 6) && wordEnd (lines [iline] [6])) {
										depth ++;
									}
								}
								if (iline <= 0) Melder_throw ("Unmatched 'endfor'.");
							} else if (wcsnequ (command2.string, L"endwhile", 8) && wordEnd (command2.string [8])) {
								int depth = 0;
								long iline;
								for (iline = lineNumber - 1; iline > 0; iline --) {
									if (wcsnequ (lines [iline], L"while ", 6)) {
										if (depth == 0) { lineNumber = iline - 1; break; }   /* Go before 'while'. */
										else depth --;
									} else if (wcsnequ (lines [iline], L"endwhile", 8) && wordEnd (lines [iline] [8])) {
										depth ++;
									}
								}
								if (iline <= 0) Melder_throw ("Unmatched 'endwhile'.");
							} else if (wcsnequ (command2.string, L"endproc", 7) && wordEnd (command2.string [7])) {
								if (callDepth == 0) Melder_throw ("Unmatched 'endproc'.");
								lineNumber = callStack [callDepth --];
								-- my callDepth;
							} else fail = TRUE;
						} else if (wcsnequ (command2.string, L"else", 4) && wordEnd (command2.string [4])) {
							int depth = 0;
							long iline;
							for (iline = lineNumber + 1; iline <= numberOfLines; iline ++) {
								if (wcsnequ (lines [iline], L"endif", 5) && wordEnd (lines [iline] [5])) {
									if (depth == 0) { lineNumber = iline; break; }   /* Go after 'endif'. */
									else depth --;
								} else if (wcsnequ (lines [iline], L"if ", 3)) {
									depth ++;
								}
							}
							if (iline > numberOfLines) Melder_throw ("Unmatched 'else'.");
						} else if (wcsnequ (command2.string, L"elsif ", 6) || wcsnequ (command2.string, L"elif ", 5)) {
							if (fromif) {
								double value;
								fromif = FALSE;
								Interpreter_numericExpression (me, command2.string + 5, & value); therror
								if (value == 0.0) {
									int depth = 0;
									long iline;
									for (iline = lineNumber + 1; iline <= numberOfLines; iline ++) {
										if (wcsnequ (lines [iline], L"endif", 5) && wordEnd (lines [iline] [5])) {
											if (depth == 0) { lineNumber = iline; break; }   /* Go after 'endif'. */
											else depth --;
										} else if (wcsnequ (lines [iline], L"else", 4) && wordEnd (lines [iline] [4])) {
											if (depth == 0) { lineNumber = iline; break; }   /* Go after 'else'. */
										} else if ((wcsnequ (lines [iline], L"elsif", 5) && wordEnd (lines [iline] [5]))
											|| (wcsnequ (lines [iline], L"elif", 4) && wordEnd (lines [iline] [4]))) {
											if (depth == 0) { lineNumber = iline - 1; fromif = TRUE; break; }   /* Go at next 'elsif' or 'elif'. */
										} else if (wcsnequ (lines [iline], L"if ", 3)) {
											depth ++;
										}
									}
									if (iline > numberOfLines) Melder_throw ("Unmatched 'elsif'.");
								}
							} else {
								int depth = 0;
								long iline;
								for (iline = lineNumber + 1; iline <= numberOfLines; iline ++) {
									if (wcsnequ (lines [iline], L"endif", 5) && wordEnd (lines [iline] [5])) {
										if (depth == 0) { lineNumber = iline; break; }   /* Go after 'endif'. */
										else depth --;
									} else if (wcsnequ (lines [iline], L"if ", 3)) {
										depth ++;
									}
								}
								if (iline > numberOfLines) Melder_throw ("'elsif' not matched with 'endif'.");
							}
						} else if (wcsnequ (command2.string, L"exit", 4)) {
							if (command2.string [4] == '\0') {
								lineNumber = numberOfLines;   /* Go after end. */
							} else {
								Melder_throw (command2.string + 5);
							}
						} else if (wcsnequ (command2.string, L"echo ", 5)) {
							/*
							 * Make sure that lines like "echo = 3" will not be regarded as assignments.
							 */
							praat_executeCommand (me, command2.string); therror
						} else fail = TRUE;
						break;
					case 'f':
						if (command2.string [1] == 'o' && command2.string [2] == 'r' && command2.string [3] == ' ') {   /* for_ */
							double toValue, loopVariable;
							wchar_t *frompos = wcsstr (command2.string, L" from "), *topos = wcsstr (command2.string, L" to ");
							wchar_t *varpos = command2.string + 4, *endvar = frompos;
							if (! topos) Melder_throw ("Missing \'to\' in \'for\' loop.");
							if (! endvar) endvar = topos;
							while (*endvar == ' ') { *endvar = '\0'; endvar --; }
							while (*varpos == ' ') varpos ++;
							if (endvar - varpos < 0) Melder_throw ("Missing loop variable after \'for\'.");
							InterpreterVariable var = Interpreter_lookUpVariable (me, varpos);
							Interpreter_numericExpression (me, topos + 4, & toValue); therror
							if (fromendfor) {
								fromendfor = FALSE;
								loopVariable = var -> numericValue + 1.0;
							} else if (frompos) {
								*topos = '\0';
								Interpreter_numericExpression (me, frompos + 6, & loopVariable); therror
							} else {
								loopVariable = 1.0;
							}
							var -> numericValue = loopVariable;
							if (loopVariable > toValue) {
								int depth = 0;
								long iline;
								for (iline = lineNumber + 1; iline <= numberOfLines; iline ++) {
									if (wcsnequ (lines [iline], L"endfor", 6)) {
										if (depth == 0) { lineNumber = iline; break; }   /* Go after 'endfor'. */
										else depth --;
									} else if (wcsnequ (lines [iline], L"for ", 4)) {
										depth ++;
									}
								}
								if (iline > numberOfLines) Melder_throw ("Unmatched 'for'.");
							}
						} else if (wcsnequ (command2.string, L"form ", 5)) {
							long iline;
							for (iline = lineNumber + 1; iline <= numberOfLines; iline ++)
								if (wcsnequ (lines [iline], L"endform", 7))
									{ lineNumber = iline; break; }   /* Go after 'endform'. */
							if (iline > numberOfLines) Melder_throw ("Unmatched 'form'.");
						} else fail = TRUE;
						break;
					case 'g':
						if (wcsnequ (command2.string, L"goto ", 5)) {
							wchar_t labelName [50], *space;
							int dojump = TRUE, ilabel;
							swprintf (labelName, 50, L"%.47ls", command2.string + 5);
							space = wcschr (labelName, ' ');
							if (space == labelName) Melder_throw ("Missing label name after 'goto'.");
							if (space) {
								double value;
								*space = '\0';
								Interpreter_numericExpression (me, command2.string + 6 + wcslen (labelName), & value); therror
								if (value == 0.0) dojump = FALSE;
							}
							if (dojump) {
								ilabel = lookupLabel (me, labelName);
								lineNumber = my labelLines [ilabel];   // loop will add 1
							}
						} else fail = TRUE;
						break;
					case 'h':
						fail = TRUE;
						break;
					case 'i':
						if (command2.string [1] == 'f' && command2.string [2] == ' ') {   /* if_ */
							double value;
							Interpreter_numericExpression (me, command2.string + 3, & value); therror
							if (value == 0.0) {
								int depth = 0;
								long iline;
								for (iline = lineNumber + 1; iline <= numberOfLines; iline ++) {
									if (wcsnequ (lines [iline], L"endif", 5)) {
										if (depth == 0) { lineNumber = iline; break; }   /* Go after 'endif'. */
										else depth --;
									} else if (wcsnequ (lines [iline], L"else", 4)) {
										if (depth == 0) { lineNumber = iline; break; }   /* Go after 'else'. */
									} else if (wcsnequ (lines [iline], L"elsif ", 6) || wcsnequ (lines [iline], L"elif ", 5)) {
										if (depth == 0) { lineNumber = iline - 1; fromif = TRUE; break; }   /* Go at 'elsif'. */
									} else if (wcsnequ (lines [iline], L"if ", 3)) {
										depth ++;
									}
								}
								if (iline > numberOfLines) Melder_throw ("Unmatched 'if'.");
							} else if (value == NUMundefined) {
								Melder_throw ("The value of the 'if' condition is undefined.");
							}
						} else if (wcsnequ (command2.string, L"inc ", 4)) {
							InterpreterVariable var = Interpreter_lookUpVariable (me, command2.string + 4); therror
							var -> numericValue += 1.0;
						} else fail = TRUE;
						break;
					case 'j':
						fail = TRUE;
						break;
					case 'k':
						fail = TRUE;
						break;
					case 'l':
						if (wcsnequ (command2.string, L"label ", 6)) {
							;   /* Ignore labels. */
						} else fail = TRUE;
						break;
					case 'm':
						fail = TRUE;
						break;
					case 'n':
						fail = TRUE;
						break;
					case 'o':
						fail = TRUE;
						break;
					case 'p':
						if (wcsnequ (command2.string, L"procedure ", 10)) {
							long iline = lineNumber + 1;
							for (; iline <= numberOfLines; iline ++) {
								if (wcsnequ (lines [iline], L"endproc", 7) && wordEnd (lines [iline] [7])) {
									lineNumber = iline;
									break;
								}   /* Go after 'endproc'. */
							}
							if (iline > numberOfLines) Melder_throw ("Unmatched 'proc'.");
						} else if (wcsnequ (command2.string, L"print", 5)) {
							/*
							 * Make sure that lines like "print = 3" will not be regarded as assingments.
							 */
							if (command2.string [5] == ' ' || (wcsnequ (command2.string + 5, L"line", 4) && (command2.string [9] == ' ' || command2.string [9] == '\0'))) {
								praat_executeCommand (me, command2.string); therror
							} else fail = TRUE;
						} else fail = TRUE;
						break;
					case 'q':
						fail = TRUE;
						break;
					case 'r':
						if (wcsnequ (command2.string, L"repeat", 6) && wordEnd (command2.string [6])) {
							/* Ignore. */
						} else fail = TRUE;
						break;
					case 's':
						if (wcsnequ (command2.string, L"stopwatch", 9) && wordEnd (command2.string [9])) {
							(void) Melder_stopwatch ();   /* Reset stopwatch. */
						} else fail = TRUE;
						break;
					case 't':
						fail = TRUE;
						break;
					case 'u':
						if (wcsnequ (command2.string, L"until ", 6)) {
							double value;
							Interpreter_numericExpression (me, command2.string + 6, & value); therror
							if (value == 0.0) {
								int depth = 0;
								long iline;
								for (iline = lineNumber - 1; iline > 0; iline --) {
									if (wcsnequ (lines [iline], L"repeat", 6) && wordEnd (lines [iline] [6])) {
										if (depth == 0) { lineNumber = iline; break; }   /* Go after 'repeat'. */
										else depth --;
									} else if (wcsnequ (lines [iline], L"until ", 6)) {
										depth ++;
									}
								}
								if (iline <= 0) Melder_throw ("Unmatched 'until'.");
							}
						} else fail = TRUE;
						break;
					case 'v':
						fail = TRUE;
						break;
					case 'w':
						if (wcsnequ (command2.string, L"while ", 6)) {
							double value;
							Interpreter_numericExpression (me, command2.string + 6, & value); therror
							if (value == 0.0) {
								int depth = 0;
								long iline;
								for (iline = lineNumber + 1; iline <= numberOfLines; iline ++) {
									if (wcsnequ (lines [iline], L"endwhile", 8) && wordEnd (lines [iline] [8])) {
										if (depth == 0) { lineNumber = iline; break; }   /* Go after 'endwhile'. */
										else depth --;
									} else if (wcsnequ (lines [iline], L"while ", 6)) {
										depth ++;
									}
								}
								if (iline > numberOfLines) Melder_throw ("Unmatched 'while'.");
							}
						} else fail = TRUE;
						break;
					case 'x':
						fail = TRUE;
						break;
					case 'y':
						fail = TRUE;
						break;
					case 'z':
						fail = TRUE;
						break;
					default: break;
				}
				if (fail) {
					/*
					 * Found an unknown word starting with a lower-case letter, optionally preceded by a period.
					 * See whether the word is a variable name.
					 */
					wchar_t *p = & command2.string [0];
					/*
					 * Variable names consist of a sequence of letters, digits, and underscores,
					 * optionally preceded by a period and optionally followed by a $ and/or #.
					 */
					if (*p == '.') p ++;
					while (isalnum (*p) || *p == '_' || *p == '.')  p ++;
					if (*p == '$') {
						/*
						 * Assign to a string variable.
						 */
						wchar_t *endOfVariable = ++ p;
						wchar_t *variableName = command2.string;
						int withFile;
						while (*p == ' ' || *p == '\t') p ++;   /* Go to first token after variable name. */
						if (*p == '[') {
							/*
							 * This must be an assignment to an indexed string variable.
							 */
							*endOfVariable = '\0';
							static MelderString indexedVariableName = { 0 };
							MelderString_copy (& indexedVariableName, command2.string);
							MelderString_appendCharacter (& indexedVariableName, '[');
							for (;;) {
								p ++;   // skip opening bracket or comma
								static MelderString index = { 0 };
								MelderString_empty (& index);
								int depth = 0;
								while ((depth > 0 || (*p != ',' && *p != ']')) && *p != '\n' && *p != '\0') {
									MelderString_appendCharacter (& index, *p);
									if (*p == '[') depth ++;
									else if (*p == ']') depth --;
									p ++;
								}
								if (*p == '\n' || *p == '\0')
									Melder_throw ("Missing closing bracket (]) in indexed variable.");
								double numericIndexValue;
								Interpreter_numericExpression (me, index.string, & numericIndexValue); therror
								MelderString_append (& indexedVariableName, Melder_double (numericIndexValue));
								MelderString_appendCharacter (& indexedVariableName, *p);
								if (*p == ']') {
									break;
								}
							}
							variableName = indexedVariableName.string;
							p ++;   // skip closing bracket
						}
						while (*p == ' ' || *p == '\t') p ++;   /* Go to first token after (perhaps indexed) variable name. */
						if (*p == '=') {
							withFile = 0;   /* Assignment. */
						} else if (*p == '<') {
							withFile = 1;   /* Read from file. */
						} else if (*p == '>') {
							if (p [1] == '>')
								withFile = 2, p ++;   /* Append to file. */
							else
								withFile = 3;   /* Save to file. */
						} else Melder_throw ("Missing '=', '<', or '>' after variable ", variableName, ".");
						*endOfVariable = '\0';
						p ++;
						while (*p == ' ' || *p == '\t') p ++;   /* Go to first token after assignment or I/O symbol. */
						if (*p == '\0') {
							if (withFile != 0)
								Melder_throw ("Missing file name after variable ", variableName, ".");
							else
								Melder_throw ("Missing expression after variable ", variableName, ".");
						}
						if (withFile) {
							structMelderFile file = { 0 };
							Melder_relativePathToFile (p, & file); therror
							if (withFile == 1) {
								wchar_t *stringValue = MelderFile_readText (& file); therror
								InterpreterVariable var = Interpreter_lookUpVariable (me, variableName); therror
								Melder_free (var -> stringValue);
								var -> stringValue = stringValue;   /* var becomes owner */
							} else if (withFile == 2) {
								if (theCurrentPraatObjects != & theForegroundPraatObjects) Melder_throw ("Commands that write to a file are not available inside pictures.");
								InterpreterVariable var = Interpreter_hasVariable (me, variableName); therror
								if (! var) Melder_throw ("Variable ", variableName, " undefined.");
								MelderFile_appendText (& file, var -> stringValue); therror
							} else {
								if (theCurrentPraatObjects != & theForegroundPraatObjects) Melder_throw ("Commands that write to a file are not available inside pictures.");
								InterpreterVariable var = Interpreter_hasVariable (me, variableName); therror
								if (! var) Melder_throw ("Variable ", variableName, " undefined.");
								MelderFile_writeText (& file, var -> stringValue); therror
							}
						} else if (isCommand (p)) {
							/*
							 * Example: name$ = Get name
							 */
							MelderString_empty (& valueString);   // empty because command may print nothing; also makes sure that valueString.string exists
							autoMelderDivertInfo divert (& valueString);
							praat_executeCommand (me, p);
							InterpreterVariable var = Interpreter_lookUpVariable (me, variableName); therror
							Melder_free (var -> stringValue);
							var -> stringValue = Melder_wcsdup (valueString.string);
						} else {
							/*
							 * Evaluate a string expression and assign the result to the variable.
							 * Examples:
							 *    sentence$ = subject$ + verb$ + object$
							 *    extension$ = if index (file$, ".") <> 0
							 *       ... then right$ (file$, length (file$) - rindex (file$, "."))
							 *       ... else "" fi
							 */
							wchar_t *stringValue;
							Interpreter_stringExpression (me, p, & stringValue); therror
							InterpreterVariable var = Interpreter_lookUpVariable (me, variableName); therror
							Melder_free (var -> stringValue);
							var -> stringValue = stringValue;   /* var becomes owner */
						}
					} else if (*p == '#') {
						/*
						 * Assign to a numeric array variable.
						 */
						wchar_t *endOfVariable = ++ p;
						while (*p == ' ' || *p == '\t') p ++;   // Go to first token after variable name.
						if (*p == '=') {
							;
						} else Melder_throw ("Missing '=' after variable ", command2.string, ".");
						*endOfVariable = '\0';
						p ++;
						while (*p == ' ' || *p == '\t') p ++;   // Go to first token after assignment or I/O symbol.
						if (*p == '\0') {
							Melder_throw ("Missing expression after variable ", command2.string, ".");
						}
						struct Formula_NumericArray value;
						Interpreter_numericArrayExpression (me, p, & value); therror
						InterpreterVariable var = Interpreter_lookUpVariable (me, command2.string); therror
						NUMmatrix_free (var -> numericArrayValue. data, 1, 1);
						var -> numericArrayValue = value;
					} else {
						/*
						 * Try to assign to a numeric variable.
						 */
						double value;
						wchar_t *variableName = command2.string;
						int typeOfAssignment = 0;   /* Plain assignment. */
						if (*p == '\0') {
							/*
							 * Command ends here: it may be a PraatShell command.
							 */
							praat_executeCommand (me, command2.string); therror
							continue;   // next line
						}
						wchar_t *endOfVariable = p;
						while (*p == ' ' || *p == '\t') p ++;
						if (*p == '=' || ((*p == '+' || *p == '-' || *p == '*' || *p == '/') && p [1] == '=')) {
							/*
							 * This must be an assignment (though: "echo = ..." ???)
							 */
							typeOfAssignment = *p == '+' ? 1 : *p == '-' ? 2 : *p == '*' ? 3 : *p == '/' ? 4 : 0;
							*endOfVariable = '\0';   // Close variable name. FIXME: this can be any weird character, e.g. hallo&
						} else if (*p == '[') {
							/*
							 * This must be an assignment to an indexed numeric variable.
							 */
							*endOfVariable = '\0';
							static MelderString indexedVariableName = { 0 };
							MelderString_copy (& indexedVariableName, command2.string);
							MelderString_appendCharacter (& indexedVariableName, '[');
							for (;;) {
								p ++;   // skip opening bracket or comma
								static MelderString index = { 0 };
								MelderString_empty (& index);
								int depth = 0;
								while ((depth > 0 || (*p != ',' && *p != ']')) && *p != '\n' && *p != '\0') {
									MelderString_appendCharacter (& index, *p);
									if (*p == '[') depth ++;
									else if (*p == ']') depth --;
									p ++;
								}
								if (*p == '\n' || *p == '\0')
									Melder_throw ("Missing closing bracket (]) in indexed variable.");
								Interpreter_numericExpression (me, index.string, & value); therror
								MelderString_append (& indexedVariableName, Melder_double (value));
								MelderString_appendCharacter (& indexedVariableName, *p);
								if (*p == ']') {
									break;
								}
							}
							variableName = indexedVariableName.string;
							p ++;   // skip closing bracket
							while (*p == ' ' || *p == '\t') p ++;
							if (*p == '=' || ((*p == '+' || *p == '-' || *p == '*' || *p == '/') && p [1] == '=')) {
								typeOfAssignment = *p == '+' ? 1 : *p == '-' ? 2 : *p == '*' ? 3 : *p == '/' ? 4 : 0;
							}
						} else {
							/*
							 * Not an assignment: perhaps a PraatShell command (select, echo, execute, pause ...).
							 */
							praat_executeCommand (me, variableName); therror
							continue;   // next line
						}
						p += typeOfAssignment == 0 ? 1 : 2;
						while (*p == ' ' || *p == '\t') p ++;			
						if (*p == '\0') Melder_throw ("Missing expression after variable ", variableName, ".");
						/*
						 * Three classes of assignments:
						 *    var = formula
						 *    var = Query
						 *    var = Object creation
						 */
						if (isCommand (p)) {
							/*
							 * Get the value of the query.
							 */
							MelderString_empty (& valueString);
							autoMelderDivertInfo divert (& valueString);
							MelderString_appendCharacter (& valueString, 1);
							praat_executeCommand (me, p);
							if (valueString.string [0] == 1) {
								int IOBJECT, result = 0, found = 0;
								WHERE (SELECTED) { result = IOBJECT; found += 1; }
								if (found > 1) {
									Melder_throw ("Multiple objects selected. Cannot assign ID to variable.");
								} else if (found == 0) {
									Melder_throw ("No objects selected. Cannot assign ID to variable.");
								} else {
									value = theCurrentPraatObjects -> list [result]. id;
								}
							} else {
								value = Melder_atof (valueString.string);   // including --undefined--
							}
						} else {
							/*
							 * Get the value of the formula.
							 */
							Interpreter_numericExpression (me, p, & value); therror
						}
						/*
						 * Assign the value to a variable.
						 */
						if (typeOfAssignment == 0) {
							/*
							 * Use an existing variable, or create a new one.
							 */
							//Melder_casual ("looking up variable %ls", variableName);
							InterpreterVariable var = Interpreter_lookUpVariable (me, variableName); therror
							var -> numericValue = value;
						} else {
							/*
							 * Modify an existing variable.
							 */
							InterpreterVariable var = Interpreter_hasVariable (me, variableName); therror
							if (var == NULL) Melder_throw ("Unknown variable ", variableName, ".");
							if (var -> numericValue == NUMundefined) {
								/* Keep it that way. */
							} else {
								if (typeOfAssignment == 1) {
									var -> numericValue += value;
								} else if (typeOfAssignment == 2) {
									var -> numericValue -= value;
								} else if (typeOfAssignment == 3) {
									var -> numericValue *= value;
								} else if (value == 0) {
									var -> numericValue = NUMundefined;
								} else {
									var -> numericValue /= value;
								}
							}
						}
					}
				} // endif fail
				if (assertErrorLineNumber != 0 && assertErrorLineNumber != lineNumber) {
					long save_assertErrorLineNumber = assertErrorLineNumber;
					assertErrorLineNumber = 0;
					Melder_throw ("Script assertion fails in line ", save_assertErrorLineNumber,
							": error " L_LEFT_GUILLEMET " ", assertErrorString.string, " " L_RIGHT_GUILLEMET " not raised. Instead: no error.");
					
				}
			} catch (MelderError) {
				//Melder_casual ("Error: << %ls >>\nassertErrorLineNumber: %ld\nlineNumber: %ld\nAssert error string: << %ls >>\n",
				//	Melder_getError(), assertErrorLineNumber, lineNumber, assertErrorString.string);
				if (assertErrorLineNumber == 0) {
					throw;
				} else if (assertErrorLineNumber != lineNumber) {
					if (wcsstr (Melder_getError (), assertErrorString.string)) {
						Melder_clearError ();
						assertErrorLineNumber = 0;
					} else {
						wchar *errorCopy_nothrow = Melder_wcsdup_f (Melder_getError ());   // UGLY but necessary (1)
						Melder_clearError ();
						autostring errorCopy = errorCopy_nothrow;   // UGLY but necessary (2)
						Melder_throw ("Script assertion fails in line ", assertErrorLineNumber,
							": error " L_LEFT_GUILLEMET " ", assertErrorString.string, " " L_RIGHT_GUILLEMET " not raised. Instead:\n",
							errorCopy.peek());
					}
				}
			}
		} // endfor lineNumber
		my numberOfLabels = 0;
		my running = false;
		my stopped = false;
	} catch (MelderError) {
		if (! wcsnequ (lines [lineNumber], L"exit ", 5) && ! assertionFailed) {   // don't show the message twice!
			while (lines [lineNumber] [0] == '\0') {   // did this use to be a continuation line?
				lineNumber --;
				Melder_assert (lineNumber > 0);   // originally empty lines that stayed empty should not generate errors
			}
			Melder_error_ ("Script line ", lineNumber, " not performed or completed:\n" L_LEFT_GUILLEMET " ", lines [lineNumber], " " L_RIGHT_GUILLEMET);
		}
		my numberOfLabels = 0;
		my running = false;
		my stopped = false;
		throw;
	}
}

void Interpreter_stop (Interpreter me) {
//Melder_casual ("Interpreter_stop in: %ld", me);
	my stopped = true;
//Melder_casual ("Interpreter_stop out: %ld", me);
}

void Interpreter_voidExpression (Interpreter me, const wchar *expression) {
	Formula_compile (me, NULL, expression, kFormula_EXPRESSION_TYPE_NUMERIC, FALSE); therror
	struct Formula_Result result;
	Formula_run (0, 0, & result); therror
}

void Interpreter_numericExpression (Interpreter me, const wchar *expression, double *value) {
	Melder_assert (value != NULL);
	if (wcsstr (expression, L"(=")) {
		*value = Melder_atof (expression);
	} else {
		Formula_compile (me, NULL, expression, kFormula_EXPRESSION_TYPE_NUMERIC, FALSE); therror
		struct Formula_Result result;
		Formula_run (0, 0, & result); therror
		*value = result. result.numericResult;
	}
}

void Interpreter_stringExpression (Interpreter me, const wchar *expression, wchar **value) {
	Formula_compile (me, NULL, expression, kFormula_EXPRESSION_TYPE_STRING, FALSE); therror
	struct Formula_Result result;
	Formula_run (0, 0, & result); therror
	*value = result. result.stringResult;
}

void Interpreter_numericArrayExpression (Interpreter me, const wchar *expression, struct Formula_NumericArray *value) {
	Formula_compile (me, NULL, expression, kFormula_EXPRESSION_TYPE_NUMERIC_ARRAY, FALSE); therror
	struct Formula_Result result;
	Formula_run (0, 0, & result); therror
	*value = result. result.numericArrayResult;
}

void Interpreter_anyExpression (Interpreter me, const wchar_t *expression, struct Formula_Result *result) {
	Formula_compile (me, NULL, expression, kFormula_EXPRESSION_TYPE_UNKNOWN, FALSE); therror
	Formula_run (0, 0, result); therror
}

/* End of file Interpreter.cpp */