subverge

Writing programs

A program in Subverge is an omega word. The two examples below are a guessing game that runs at the prompt and a full-screen reaction timer. After them come structuring, testing, installing and sharing programs.

Read first steps, locals and the flow words first.

Where to write

Short words can be typed at the prompt. Anything longer than a line goes in the editor:

'guess edit         a definition in this tab
"games2" unit       a new root unit, shared by every tab

Start with edit while the program is changing quickly. edit stores a parsed quote, so back undoes a bad change and comments are lost. Move the finished program into a unit, where comments are kept, and every tab can run it.

A guessing game

The program picks a number from 1 to 100 and reads guesses until one is right.

'guess edit

and type:

"guess" title
100 rand 1 + 0 1 { secret tries going }
"  I picked a number from 1 to 100." say
[ going ] [
  "  guess: " ask { line }
  tries 1 + ->tries
  [ line >num ] try [
    { n }
    n secret = [
      "  right, in " tries str cat " tries" cat say
      0 ->going
    ] [
      n secret < [ "  higher" ] [ "  lower" ] if say
    ] if
  ] when
] while
"omega" title

Save with C-x C-s, leave with C-x C-c, and run it:

guess

How it works:

if takes two quotes, and [ " higher" ] [ " lower" ] if say picks a string and then prints it.

Testing pieces

Words are easiest to test on their own. Split the parts that compute something from the parts that print or read keys:

[ { n secret } n secret = [ "right" ] [ n secret < [ "higher" ] [ "lower" ] if ] if ] :verdict
10 50 verdict           "higher"
50 50 verdict           "right"

A word that only takes values and leaves values can be tried at the prompt, in any order, without starting the program.

To watch a word run, trace steps through it one item at a time:

[ 10 50 verdict ] trace

Space steps, c runs the rest and q stops. The stack is shown before every step.

To make rand and ask predictable while testing, define your own versions in the tab:

[ drop 49 ] :rand
[ drop "50" ] :ask
guess
'rand forget 'ask forget

A tab definition hides a built-in of the same name, so guess now picks 50 and always guesses 50. forget removes the stand-ins and uncovers the built-ins again (names).

A full-screen program

The second program measures reaction time. It shows a grey bar, waits a random time, turns the bar red and counts how long it takes you to press a key.

"react" unit

and type:

# reaction timer

[ [ key? -1 = not ] [ ] while ] :drain-keys

[ { dt }
  'react-best defined? [ react-best ] [ 999999 ] if { best }
  dt best < [ dt :react-best ] when
] :react-score

[ "react" title wipe nocursor
  "Press any key when the bar turns red. q leaves." 2 1 7 text
  1 { going }
  [ going ] [
    "wait for it...        " 2 3 8 text
    32 0 5 80 7 fill
    200 rand 100 + sleep
    drain-keys
    32 0 5 80 0 4 attr fill
    ticks { t0 }
    key { k }
    ticks t0 - 10 * { ms }
    k quit-key? [ 0 ->going ] [
      ms react-score
      ms str " ms, best " cat react-best str cat " ms      " cat 2 3 14 text
      100 sleep drain-keys
    ] if
  ] while
  wipe "omega" title
] :react

Save, leave, and run react.

The screen

wipe clears the tab's screen and nocursor hides the cursor. From then on the program draws at fixed positions instead of printing:

Rows count from 0 at the top of the tab's area, under the tab bar, and there are 24 of them. A space drawn with a background colour is a solid block, so 32 0 5 80 0 4 attr fill paints row 5 red: 0 4 attr is colour 0 on colour 4. Colours are numbers whose actual colour comes from the theme; 4 is red in every theme, 7 is normal text and 14 the accent.

The last line, wipe "omega" title, leaves the tab as the program found it. Every bundled program ends this way.

Timing

ticks counts hundredths of a second since boot, so the difference between two readings times ten is milliseconds. 200 rand 100 + sleep waits between one and three seconds without using the CPU.

Keys

drain-keys throws away keys pressed too early: key? returns the next key or -1 without waiting, and the loop reads until the queue is empty. Without it, a key pressed during the wait would count as an instant reaction. key waits for the next key and returns its code, and quit-key? is true for q, Escape and Ctrl-C (keyboard).

Keeping a best score

react-score keeps the best time in a definition called react-best, made at run time with :react-best. Definitions made while a program runs go into the tab's world like any other, so:

'react-best defined? checks whether the word exists yet, so the first run starts from a large number.

Adding it to apps

apps prints a hand-written list (apps). Open it:

'apps open

and add a line inside the inner brackets:

[ "react" "Reaction timer." ]

The program now shows in apps in every tab.

Structure

Programs in Subverge stay readable when they follow the conventions the bundled ones use:

Speed

Most programs need no tuning. When one does:

Sharing a program

A unit is plain text. 'react src say prints its source, and the same text can be typed into "react" unit on another system. To include a program in Subverge itself, add it as a file in root/ in the source tree and send a pull request (development). Root words need a help entry, which is written in C in kernel/help.c (development).