6.6.2 Cheating, spying, and debugging

Prev Up Next Page 80 of 800 Search internet

The Logiweb programming language is univocal in the sense that if you invoke the same function on the same arguments two times, and if both invocations return a value in finite time, then the two return values are the same. In this connection, an exception counts as a value like any other value.

The language is pure functional in the sense of being univocal. But it does permit side effects like printing a value to the terminal or dying with a death report to the terminal.

The language has no functions for e.g. telling what time it is. A function which can tell what time it is would return different values each time it was called, and that would violate univocality. Likewise, the language has no functions for reading from the users terminal.

Do not use functions with side effects for programming; they are not portable. Use them for debugging only. If you want to write programs that do IO, learn about Logiweb machines.

Here are the functions for cheating, spying, and debugging:

print ( x )
Print the text x to the terminal and return the string 'print'.
trace ( x )
Print the value x to the terminal and return the string 'trace'.
Write a death report. Quit instead of returning a value.
timer ( x )
Stop the current timer, start timer x, and return the string 'timer'. As an example, timer ( 2 ) starts timer number 2 (there are 1000 timers numbered from 0 to 999). A later call to timer ( 3 ) starts timer 3 and stops timer 2. A later call to timer ( true ) stops timer 3. When the program terminates (naturally or by calling 'bottom' or by ctrl-C) you will get a death report which includes the values of all non-zero timers.
spy ( x )
Set the spy variable to x and return the string 'spy'. When the program terminates, the value of the spy variable is printed unless it equals true. All test cases set the spy variable before executing the test and clear it afterwards by setting the spy variable back to true. If a test case loops indefinitely (or, rather, if you run out of patience), stop lgc using ctrl-C. From the death report you can then see which test was being executed.

One way to get debug output is to invoke it from a test case. Here are some examples you can try adding to append.lgs:

   \item "[[ etst !"print" ; print ( << !"Hello" ,, !"World" >> ) end test ]]"

That prints 'HelloWorld' to the terminal. The argument of print is scanned left to right for strings and all strings are printed. The exclamation marks before strings ensure the strings are rendered as strings. That is not so important for strings that merely contain letters. The print function returns 'print' so the test succeeds.

You get no line feed after 'HelloWorld' because you did not ask for one. If you want a linefeed at the end you can use the constant LF:

   print ( << !"Hello" ,, !"World" ,, LF >> )

Or you may use println instead of print:

   println ( << !"Hello" ,, !"World" >> )

Println returns the string 'print'. Use LF even if your host operating system uses some other newline convention. It is the responsibility of lgc to adapt to the host operating system.

The trace function provides another means for getting debug output:

   \item "[[ etst !"trace" ; trace ( << !"Hello" ,, !"World" >> ) end test ]]"

That prints something like this to the terminal:


The thing above is a pair operator P applied to the string 'Hello' and a second argument. The second argument comprises a pair operator P applied to the string 'World' and the constant T which represents true.

   \item "[[ etst !"timer" ; timer ( 0 ) end test ]]"
   \item "[[ etst !"timer" ; timer ( true ) end test ]]"

When lgc(1) terminates, it says goodbye. But afterwards, since lgc runs on top of the Logiweb Abstract Machine lgwam(1), you will get a death report from lgwam. The output may end something like this:

   User rendering
   Values of timers
   Timer Calls     Seconds
       0     1    0.001331

The output above says that timer 1 was started one time and ran for 0.001331 seconds.

Prev Up Next Page 80 of 800 Search logiweb.eu

Copyright © 2010 Klaus Grue, GRD-2010-01-05