TYL PROGRAMMING LANGUAGE
▶ WORKING WITH EXECUTABLE PROGRAMS
Calling Executable Programs with Arguments
Let's create a demonstrative Beep executable program:
≡ if there are no arguments, just beep with default frequency  and duration
argsnum len in
argsnum 0 ? beep
≡ error codes
FREQNOTNUMERIC 1
DURNOTNUMERIC 2
≡ beep variables
freq dur 0
defaultdur 400
≡ set TRUE to enforce duration argument check, if frequency argument exists
enforcedur
≡ get incoming arguments
freqarg in 0
durarg in 1
≡ get numeric values of arguments
not null freqarg ? freqnum number.of freqarg ? freq freqnum
not null durarg ? durnum number.of durarg ? dur durnum
≡ exit if an argument is non-numeric
not freq ? exit FREQNOTNUMERIC
enforcedur ? not dur ? exit DURNOTNUMERIC
≡ if duration was not provided or is not of numeric type, resort to default duration
not dur ? dur defaultdur
≡ finally beep
beep freq dur
Create an empty file 'beep.tyl', load with Tyl Launcher, paste the above code and click on the circular arrow icon to build the application. You'll notice that the icon goes off and than on, indicating that build has completed. If an error occurs, an error message will be seen in the run box. Now turn on fast button to cause the system to create a fast background executable program, than click "CREATE EXECUTABLE", and an executable program 'beep.exe' will be created.

In a console, cd into the current working directory, and beep:
> beep
beep program makes use of in system function that is a record that holds all incoming arguments in the format number: argument.

To call 'beep.exe' with an argument:
> beep 1760
In line freqarg in 0, we assign the first argument that the program got, to freqarg variable. If there are no arguments, freqarg value will be NULL.

In line not null freqarg ? freqnum number.of freqarg ? freq freqnum, first we check if freqarg is not NULL, then we check if it is of numeric type. If all went well, freq will have the frequency that the user passed as an argument.

As for the second argument, which is the duration, we incorporated a boolean scalar enforcedur, that indicates whether to make the duration argument optional, or not. If enforcedur is FALSE, duration check statement will be skipped and the system will beep with the provided frequency and the defaultdur value, otherwise duration check statement will be executed.

To call 'beep.exe' with frequency and duration arguments:
> beep 1760 "1000"
Tyl System turns all arguments into scalar strings, and it's up to the programmer to turn them into whatever type he wishes to work with.

Now let's feed a non numeric first argument:
> beep X
Line not freq ? exit FREQNOTNUMERIC, will cause the system to exit with exit code 1, thus terminating program running, and the beep with parameters line beep freq dur, will not be reached.

Executable programs that run successfully, or that did not exit with a specified exit code, will exit with exit code 0. Though we can use the out system function to output an error message, usually the right method of giving an indication of an error is defining a set of numeric error codes that the calling process will get when a program running exited with an exit code.

An example of such calling processes is given in this runner file 'beep.runner.tyl':
prog 'beep.exe'
errors 'OK' 'Invalid frequency!' 'Invalid duration!'
p4args 1760 1000
p1 process prog
p2 process prog 1760
p3 process prog 'X'
p4 process prog p4args
p1.run
print 'no arguments: ' + errors p1.exitcode
p2.run
print 'frequency 1760 Hz.: ' + errors p2.exitcode
p3.run
print 'frequency `X`: ' + errors p3.exitcode
p4.run
print 'frequency 1760 Hz, duration 1 sec.: ' + errors p4.exitcode
no arguments: OK
frequency 1760 Hz.: OK
frequency 'X': Invalid frequency!
frequency 1760 Hz, duration 1 sec.: OK
PART 2