LoginSignup
0

More than 1 year has passed since last update.

CRT Terminal Effect in Dogelog Runtime

Last updated at Posted at 2021-09-26

Introduction

The Dogelog runtime is a Prolog interpreter that runs on top of a higher level host programming language. Currently there are versions for the JavaScript programming language and the Python programming languages. We made a little example that shows a CRT terminal effect in the browser:
moon_base_crop.png
The Dogelog runtime user manual contains a couple of tutorials. The manual is available on GitHub. One tutorial shows how to play a tune inside the browser by using inversion of control. The CRT terminal effect example is derived from the music example.

Music Example

We use inversion of control to let the browser repeatedly call the Prolog interpreter to simulate a loop. The music example has a live page at www.xlog.ch. The music example registers a new Prolog built-in delay/2. The loop is then realized as follows:

play([]) :- !.
play([''|L]) :- !, delay(play(L), 400).
play([X|L]) :- note(X), delay(play(L), 500).

:- play(['E','D','C','D','E','E','E','','D','D','D','','E',
         'G','G','','E','D','C','D','E','E','E','','D','D',
         'E','D','C']).

Every time delay/2 is called the Prolog interpreter schedules an event on the JavaScript timer queue. A further effect of delay/2 is that the predicate succeeds, means the Prolog interpreter stops executing play/1. The predicate play/1 is resumed later when the schedule event fires.

Canceling Delays

So far we didn't bother with canceling the delays, and when we press the "Try It" button multiple times, the music tune gets more and more scrambled. To avoid this scrambling we now use setTimeout() side by side with clearTimeout(). The new implementation of the built-in delay/2 now reads as follows:

    let timer = undefined;

    function delay(term, time) {
        timer = setTimeout(function () {
            timer = undefined;
            perform(term)}, time);
    }

The buil-in delay/2 takes a Prolog term and a delay time in milliseconds. In the above a handle to the scheduled event will be remembered in the variable timer. We can then use this handle to clear the scheduled event. This is done when the "Try It" button is pressed:

    function main() {
        if (timer !== undefined) {
            clearTimeout(timer);
            timer = undefined;
        }

By this little measure we can avoid that multiple loops, programmed via inversion of control, do overrun each other. JavaScript has no notion of a single loop, it allows posting multiple timeout events. To avoid overrun we have to manage on our own, that there is at most one timeout event.

Terminal Example

In the terminal example we use the built-in note/1 to play some backround music. The built-in delay/2 and the built-in note/1 are exactly defined as in the music example. The terminal example has a live page at www.xlog.ch for exploration. Usually we emit text from within the Dogelog runtime into a paragraph range element named "demo":

<p id="demo"></p>

The inversion of control loop is used to gradually emit some text in the upper left corner in terminal green. We also show a blinking cursor block. It turns out that this can all be done by redirecting the text into a character range element instead of a paragraph range element. The corresponding element named "demo" is now defined as follows:

<p><span id="demo"></span><span
        class='typed-cursor'>&#9608;</span></p>

The rest of the CRT terminal effect code is the same as in the music example, except that we do not play the notes, but output more and more characters of some text. We use the ISO core standard built-ins current_output/1, put_code/2 and flush_output/1 to do that. A little prompt is also simulated.

Outlook

Its possibly overkill to do such an example via Prolog. On the other hand doing it was much fun. Doing it is also feasible, since the Dogelog runtime only occupies ca. 200KBytes. This size is on par with the size of the MP3 we play in the background, and even half of the size of the background image.

Doglog Runtime on GitHub
github.com/jburse/dogelog-moon

Teaching Python to the Dogelog Runtime
qiita.com/j4n_bur53/items/be29763ecadf85b61108

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0