TYL PROGRAMMING LANGUAGE
▶ DEBUGGING IN DEPTH
Debugging in Depth
It can be useful to set logname based on date & time data:
time { fulldate 0 totalsecond 0 }
time now
timestamp string.section time.fulldate 0 10
timestamp string.replace timestamp '-' ''
timestamp ++ '.' + math.ceil time.totalsecond
logname appname + '.' + timestamp
@logname logname
@ 'debug file `' + logname + '.tyldebug` was created by Tyl system, upon running program `' + appname + '.tyl`'
We make use of appname system variable, and manipulation of now system function result, to construct debug file name, according to date & time data.

Debug data is saved upon each debug call:
names 'Lili' 'Mili' 'Nili'
index 10 ~ @ names index
While looping in a range that exceeds names list length, eventually the call to names index, will result in an index out of range error, but the debug data is saved upon each @ call:
Lili
Mili
Nili

The @k function is also useful for other key and value variables:
names 'Lili' 'Mili' 'Nili'
@k 'the program file name is' appname + '.tyl'
@k 'the list is' names
@k 'the second item is' names 1
All '@k key value' calls will be logged in the format 'key: value':
the program file name is: 'app.tyl'
the list is: [ 'Lili', 'Mili', 'Nili' ]
the second item is: Mili

So to log list indexes and values:
names 'Lili' 'Mili' 'Nili'
max len names
index max ~ @k index names index
In line @k index names index, the key will be an index, and the value will be the item of names list in index position:
0: Lili
1: Mili
2: Nili

During program coding and debugging, sections of code raise errors, and it is needed to stop the program immediately. We can use the @exit debug function.

Consider this Log Calculator program:
go:
print log 8
log x:
p 0.5
delta 0.01
eqdelta 0.01
stopctr 1000
~
@k p
  result math.power 10 p
  diff x - result
  absdiff math.abs diff
@k diff
  absdiff > eqdelta ?
   diff > 0 ?
    p ++ delta
    \
    p -- delta
   ^
   \
   %
  ^
stopctr --
not stopctr ? @exit
^
p
We added log data of p and diff variables, and a stop counting mechanism, with @exit debug function.

Running the program, and checking the log:
p: 0.5
diff: 4.83772233983162
p: 0.51
diff: 4.76406343070372
...
...

p: 0.9
diff: 0.0567176527571798
p: 0.91
diff: 0.12830516164099
p: 0.9
diff: 0.0567176527571798

As it becomes apparent, p toggles between 0.9 and 0.91 endlessly. To fix that, change delta 0.01, to delta 0.001, thus causing p to reach the right result. Now you can safely remove all debug code, as well as the stop counting mechanism.

There is also a fast debug exit function in order to exit immediately:
process 'prog.exe'
result process.run
@e result
not result ?
@ process.exitcode
\
 ! continue with program...
^
Assuming we have an executable progeam 'prog.exe', and running it with process system module. In order to verify the result, we put the debug exit line @e result, that will tell the system to log result variable and exit immediately.

If you don't want to terminate the program but to pause execution, you can put breakpoints in the code using @b debug function:
@b
---   BREAKPOINT   ---
>
@b causes the system to print '---   BREAKPOINT   ---', along with a prompt message '> ', as in Input Data page, and wait for input.

If you want to terminate the program and close launcher, print 'e', otherwise just hit the ENTER, and the program will resume execution.

As with @ function, you can prepend a statement with @b function, then it will be executed and the result will be printed within the breakpoint message:
@b counter 120
---   BREAKPOINT (120)   ---
>
PART 1