1form Highlighter test
2  # This is a regular comment
3  sentence Blank
4  sentence My_sentence This should all be a string
5  text My_text This should also all be a string
6  word My_word Only the first word is a string, the rest is discarded
7  boolean Binary 1
8  boolean Text no
9  boolean Quoted "yes"
10  comment This should be a string
11  optionmenu Drop-down: 1
12    option Foo
13    option 100
14  choice Radio: 1
15    option Foo
16    option 100
17  real left_Range -123.6
18  positive right_Range_max 3.3
19  integer Int 4
20  natural Nat 4
21endform
22
23beginPause: "Highlighter test"
24  sentence: "Blank", ""
25  sentence: "My sentence", "This should all be a string"
26  text: "My text", "This should also all be a string"
27  word: "My word", "Only the first word is a string, the rest is discarded"
28  boolean: "Binary", 1
29  comment: "This should be a string"
30  optionMenu: "Drop-down", 1
31    option: "Foo"
32    option: "100"
33  choice: "Choice", 1
34    option: "Foo"
35    option: "100"
36  real: "left Range", -123.6
37  positive: "right Range max", 3.3
38  integer: "Int", 4
39  natural: "Nat", 4
40button = endPause("Cancel", "OK", 1, 2)
41
42# Periods do not establish boundaries for keywords
43form.var = 10
44# Or operators
45not.an.operator$ = "Bad variable name"
46bad.or.not = 1
47
48# External scripts
49include /path/to/file
50runScript: "/path/to/file"
51execute /path/to/file
52
53# Predefined variables
54a  = praatVersion
55a  = e + pi * ( all+right) / left mod average + (mono - stereo)
56a$ = homeDirectory$ + tab$ + newline$
57a$ = temporaryDirectory$
58a$ = praatVersion$
59a$ = shellDirectory$
60a$ = homeDirectory$
61a$ = preferencesDirectory$
62a$ = defaultDirectory$
63nocheck selectObject: undefined
64# Not predefined variables
65a$ = e$
66a$ = pi$
67
68# Arrays are not comments
69a# = zero# (5, 6)
70a [3], 5 = 7
71printline 'a[3,5]', 'a[3]'
72a [1] = 2
73b [a [1]] = 3
74assert b [a [1]] = 3
75printline 'b[2]'
76
77# if-block with built-in variables
78if windows
79  # We are on Windows
80elsif unix = 1 or !macintosh
81  exitScript: "We are on Linux"
82else macintosh == 1
83  exit We are on Mac
84endif
85
86# Interpolation with precision digits
87echo unquoted 'a:3'
88echo unquoted 'a.a:3'
89echo unquoted 'a[1]:3'
90echo unquoted 'a1:3'
91
92appendInfoLine: "quoted 'a:3'"
93appendInfoLine: "quoted 'a.a:3'"
94appendInfoLine: "quoted 'a[1]:3'"
95appendInfoLine: "quoted 'a1:3'"
96
97# Interpolations are not recursive
98echo unquoted 'a'1':3'
99appendInfoLine: "quoted 'a'1':3'"
100
101# Interpolation without precision digits
102echo unquoted 'var' numeric
103echo unquoted 'var$' string
104echo unquoted 'var["a"]' numeric hash
105echo unquoted 'var$["a"]' string hash
106echo unquoted 'var[1]' numeric indexed variable
107echo unquoted 'var$[1]' string indexed variable
108
109appendInfoLine: "quoted 'var' numeric"
110appendInfoLine: "quoted 'var$' string"
111appendInfoLine: "quoted 'var["a"]' numeric hash"
112appendInfoLine: "quoted 'var$["a"]' string hash"
113appendInfoLine: "quoted 'var[1]' numeric indexed variable"
114appendInfoLine: "quoted 'var$[1]' string indexed variable"
115
116# Indeces in interpolations must be literal
117echo 'var[a]'
118echo 'var[a$]'
119
120string$ = "But don't interpolate everything!"
121string$ = "interpolatin' " + "across" + " strings ain't cool either"
122string$(10) ; This is a function
123
124repeat
125  string$ = string$ - right$(string$)
126until !length(string$)
127
128Text... 1 Right 0.2 Half many----hyphens
129Text... 1 Right -0.4 Bottom aحبيبa
130Text... 1 Right -0.6 Bottom 日本
131Draw circle (mm)... 0.5 0.5 i
132
133rows   = Object_'table'.nrow
134value$ = Table_'table'$[25, "f0"]
135fixed  = Sound_10.xmin
136fixed  = Object_foo.xmin
137fixed  = Procrustes_foo.nx
138var["vaa"] = 1 ; Hash
139
140# Special two-word keyword
141select all
142# Keyword with a predefined variable
143select  all
144
145# old-style procedure call
146call oldStyle "quoted" 2 unquoted string
147assert oldStyle.local = 1
148
149# New-style procedure call with parens
150@newStyle("quoted", 2, "quoted string")
151if praatVersion >= 5364
152  # New-style procedure call with colon
153  @newStyle: "quoted", 2, "quoted string"
154endif
155
156# inline if with inline comment
157var = if macintosh = 1 then 0 else 1 fi ; This is an inline comment
158
159# for-loop with explicit from using local variable
160# and paren-style function calls and variable interpolation
161n = numberOfSelected("Sound")
162for i from newStyle.local to n
163  name = selected$(extractWord$(selected$(), " "))
164  sound'i' = selected("Sound", i+(a*b))
165  sound[i] = sound'i'
166endfor
167
168i = 1
169while i < n
170  i += 1
171  # Different styles of object selection
172  select sound'i'
173  sound = selected()
174  sound$ = selected$("Sound")
175  select Sound 'sound$'
176  selectObject( sound[i])
177  selectObject: sound
178
179  # Pause commands
180  beginPause("Viewing " + sound$)
181  if i > 1
182    button = endPause("Stop", "Previous",
183      ...if i = total_sounds then "Finish" else "Next" fi,
184      ...3, 1)
185  else
186    button = endPause("Stop",
187      ...if i = total_sounds then "Finish" else "Next" fi,
188      ...2, 1)
189  endif
190  editor_name$ = if total_textgrids then "TextGrid " else "Sound " fi + name$
191  nocheck editor Sound 'editor_name$'
192    nocheck Close
193  nocheck endeditor
194  editor_id = editor: editor_name$
195    Close
196  endeditor
197
198  # New-style standalone command call
199  Rename: "SomeName"
200
201  # Command call with assignment
202  duration = Get total duration
203
204  # Multi-line command with modifier
205  pitch = noprogress To Pitch (ac): 0, 75, 15, "no",
206    ...0.03, 0.45, 0.01, 0.35, 0.14, 600
207  # Formulas are strings
208  Formula: "if col = 1 then row * Object_'pitch'.dx + 'first' else self fi"
209
210  # do-style command with assignment
211  minimum = do("Get minimum...", 0, 0, "Hertz", "Parabolic")
212
213  # New-style multi-line command call with broken strings
214  table = Create Table with column names: "table", 0,
215    ..."file subject speaker
216    ... f0 f1 f2 f" + string$(3) + " " +
217    ..."duration response"
218
219  # Function call with trailing space
220  removeObject: pitch, table
221
222  # Picture window commands
223  selectObject: sound
224  # do-style command
225  do("Select inner viewport...", 1, 6, 0.5, 1.5)
226  Black
227  Draw... 0 0 0 0 "no" Curve
228  Draw inner box
229  Text bottom: "yes", sound$
230  Erase all
231
232  # Demo window commands
233  demo Erase all
234  demo Select inner viewport... 0 100 0 100
235  demo Axes... 0 100 0 100
236  demo Paint rectangle... white 0 100 0 100
237  demo Text... 50 centre 50 half Click to finish
238  demoWaitForInput ( )
239  demo Erase all
240  demo Text: 50, "centre", 50, "half", "Finished"
241endwhile
242
243switch$ = if switch == 1 then "a" else
244  ...     if switch == 2 then "b" else
245  ...     if switch == 3 then "c" else
246  ...     if switch == 4 then "d" else
247  ...     "default" fi fi fi fi
248
249# An old-style sendpraat block
250# All these lines should be a string!
251sendpraat Praat
252  ...'newline$' Create Sound as pure tone... "tone" 1 0 0.4 44100 440 0.2 0.01 0.01
253  ...'newline$' Play
254  ...'newline$' Remove
255
256# A new-style sendpraat block
257beginSendPraat: "Praat"
258  Create Sound as pure tone: "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
259  duration = Get total duration
260  Remove
261endSendPraat: "duration"
262appendInfoLine: "The generated sound lasted for ", duration, "seconds"
263
264# Number types
265a =   10%
266a =  -10
267a =  +10
268a =   10.4
269a =  294e12
270a =    2.94e12
271
272# Operators
273a = 2 ^ -6
274a = -(1+1)^6
275a = 4^3 ^ 2
276a = 54 div 5.1
277a = 54.3 mod 5
278a = 3 ** 8 - 7
279a = 3 / (8 + 7)
280a = (7 * (3 + 5)) / ((2 + 3) - 1)
281
282# Logical operators
283assert (a =   b) and c
284assert  a == (b  or  c)
285assert  a <=  b  not c
286assert  a >=  b     !c
287assert  a !=  b  &   c
288assert  a !=  b  &&  c
289assert  a <>  b  ||  c
290assert  a <   b  |   c
291assert  a >   b
292
293assert (a)or (b)
294assert (a) or(b)
295assert (a)and(b)
296
297assert "hello" =  "he" + "llo"
298assert "hello" == "hello world" - " world"
299
300stopwatch
301time = stopwatch
302clearinfo
303echo This script took
304print 'time' seconds to
305printline execute.
306
307# Old-style procedure declaration
308procedure oldStyle .str1$ .num .str2$
309  .local = 1
310endproc
311
312# New-style procedure declaration with parentheses
313procedure newStyle (.str1$, .num, .str2$)
314  # Command with "local" variable
315  .local = Get total duration
316  .local = Get 'some' duration
317  .local = Get 'some[1]' value... hello 10 p[i]
318  .local = Get 'some[1,3]' value: "hello", 10, p[i]
319  .local = Get 'some$' duration
320  .local = Get 'some$[1]' duration
321endproc
322
323# New-style procedure declaration with colon
324procedure _new_style: .str1$, .num, .str2$
325  # Command with "local" variable
326  # Initial underscores in variables not allowed (unless interpolated)
327  _new_style.local = Get total duration
328endproc
329
330asserterror Unknown symbol:'newline$'« _
331assert '_new_style.local'
332
333@proc: a, selected("string"), b
334# Comment
335
336for i to saveSelection.n
337  selectObject: saveSelection.id[i]
338  appendInfoLine: selected$()
339endfor
340
341@ok(if selected$("Sound") = "tone" then 1 else 0 fi,
342  ... "selected sound is tone")
343
344@ok_formula("selected$(""Sound"") = ""tone""", "selected sound is tone")
345