Programs and hosts

A Rio program runs inside a host: a program that embeds Rio, such as the rio command line tool or a game console. The host gives the program its memory, its log, its host procs and its modules, and decides when the program's procs run. How a host does that in C is in the guide.

Running

  1. The host compiles the main file. It's rejected as a whole if anything in it, or in what it imports, doesn't compile.
  2. The top-level code runs, from the top. Imported modules run where they're first imported.
  3. The host calls the procs it wants: the CLI calls main if there is one; a game console might call update and draw each frame. Globals keep their values between calls.

A runtime error stops what's running and reports its line (and file, outside the main file). The errors are: an index or slice out of bounds, division by zero, pop from an empty builder, a value that isn't one of an enum's, calling an unset proc value, a failed assert, an error from a host proc, and being stopped by the host.

What the host can find

The host can find main, and the main file's top-level procs and globals marked with *, the same mark modules use for exports. A generic proc isn't found, even marked: the host couldn't say what its T is (other modules can still use it).

score* := 0                 # the host can read and change it
update* :: proc()           # the host can call it
  score += 1
end

Host procs

A host proc is a proc the host provides, written in the host's language. Its parameters and result can be Int, Float, String and Blob, and its parameters structs. A host proc has a plain name, global like a builtin, or a module name and a name, such as pc.cls. Host procs in a module are used through that module: with import pc, or everywhere if the host auto-imports pc. If the host has no source for the module, the module is just its host procs, all exported.

import pc                   # a module of host procs (a host that has one)
pc.cls(0)

The CLI's host procs are in its module os (the guide's page): files and folders, running programs, the environment and arguments, text in and out, and time, for a program that imports it (import os).

A host proc can't call back into the program while it runs. It can stop the program with a runtime error. Since it can't call back, a host proc can always be a proc value, as a proc that calls through no values can: tick := os.clock.

Declaring host procs

host declares what the host provides, and the compiler checks the declaration against it. Such declarations document a host's API in Rio, and since they're checked, they can't drift. For a host with a global host proc clock (the CLI's are in its module os, whose source declares them so):

clock* :: host proc() -> Float     # the host's clock: checked against it
start := clock()
clock :: host proc() -> Int  # error: doesn't match the host's clock: proc() -> Float
  • Host procs: name :: host proc(...) -> R, a proc type, as for proc values: its parameter and result types must be exactly the host's, except that an enum or a Bool may stand for an Int, which it is underneath, and a struct parameter for its fields (below); calls then take the declared types. Its last parameters may be optional (t?: Text), and the host gets zeros for those a call leaves out, just as an image made before a host proc gained parameters at its end passes zeros for them. So a host proc can gain options without breaking programs, compiled or not. In a module, name is the host proc of the module's name (cls in pc.rio is pc.cls), else a global one. When the host has a source for a module of host procs (one it gives with its procs, ideally), that source is the module (auto-imported ones too), and it must declare every one of the module's host procs. So a module can give its host procs types of its own:

    Pointer* :: enum            # in pc.rio
      game, ui, none
    end
    pointer* :: host proc(mode: Pointer)   # the host's pc.pointer takes an Int
    
  • Struct parameters: a struct goes to the host by address, and the host reads it in place, so it can hold anything a struct can, and be any size. The host's sig gives its layout: each field's name and what it is to C (an enum or a Bool is an Int), which the declaration's must be. The host only borrows it, as it does views, for the call; it reads it as it is when the call is made:

    Pos* :: struct              # in pc.rio
      x, y: Int
    end
    Text* :: struct
      at: Pos
      color: Int(8)
      big: Bool(1)
    end
    text* :: host proc(s: String, t: Text)   # the host's sig: "s{at:{x:Int,y:Int},color:Int(8),big:Int(1)}"
    pc.text("hi", {at = {x = 3, y = 4}, big = true})   # color 0
    

    An enum, in a layout or as a parameter, is named by its values (Enum{game,ui,none}, a word), so one reordered doesn't fit an image made before, while an enum parameter's values added at its end do. (A plain i for an enum parameter works too, unchecked.)

    A field left out of a literal is zero, so a host proc's options can grow without breaking calls written before: add fields at the end, and make zero mean what calls did before. Images made before then still load: the host reads zeros for the fields they lack. A host writes its side from the module's source, which makes C structs and sigs to match: see the embedding guide.

  • &T parameters: as for a proc, a host proc may take a place, &T, and change it: an Int, a Float, a struct, or a point or size ([N]Int or [N]Float, N up to 8) (&i, &f, &{...}, &(ii) in its sig). Calls pass &x. Unlike a proc's, a host proc's & parameter may be optional (size?: &[2]Int): left out, the host gets 0 for its address and size, and writes nothing. The host writes no more than the place holds, so a struct an older image made with fewer fields stays as it was past them:

    Mouse* :: struct            # in pc.rio
      x, y: Int
      down: Bool
    end
    mouse* :: host proc(m: &Mouse)   # the host fills it in
    m: pc.Mouse
    pc.mouse(&m)
    
  • Points and sizes: an [N]Int or [N]Float parameter (N up to 8) goes to the host as its words, one after another, as that many i (or f) in its sig: rect* :: host proc(pos, size: [2]Int, c: Int) is the host's "iiiii". Calls take any such array, so a literal is typed by the parameter: pc.rect({x, y}, {8, 8}, 7).

  • Lists of points: a [][N]Int (or [][N]Float, N from 2 to 8) is a slice like any other, 2 words: where its elements start and how many there are; its sig writes the element's words in the brackets: polygon* :: host proc(points: [][2]Int, c: Int) is the host's "[ii]i", and the host reads the points as int32_t pairs, x then y.

  • Results as points: pointerAt* :: host proc() -> [2]Int is the host's ">ii": it writes the words back in order.

  • Results: a host proc returns what a proc may: an Int, Float, String or Blob, or a struct, union or maybe of up to 16 words with no views in it (>{...}, >Union{...} in its sig). A struct result may gain fields at its end, as a parameter may: an older image gets the fields it holds.

    mouse* :: host proc() -> Mouse          # in pc.rio: rather than taking &Mouse
    keyDown* :: host proc(k: Int) -> ?Int   # nil, or how long it's been down
    
  • Builtins: declared the same way, and checked as calls with those types: sqrt* :: host proc(x: Float) -> Float. src/core.rio declares them all.

  • Builtin types: Int* :: host struct.

  • Methods: on a named type, Int.toFloat* :: host proc() -> Float; on any other type, in parentheses: (Blob[..]).push* :: host proc(byte: Int) -> Bool.

  • Static host procs: a host proc called on a type, as a static proc is: at* :: host proc(...) belongs to the module, Pos.at* :: static host proc(x, y: Int) -> Pos to its type, called pc.Pos.at(3, 4). The host registers it as Type_name (pc.Pos_at), and rio -H writes it so (pc_Pos_at_SIG, X(Pos_at, ...)).

host declarations go at the top level; * exports them, as for other declarations. A name that's neither a host proc nor a builtin is an error:

nope :: host proc(x: Int)  # error: the host has no proc by this name

A host can ship such a module only as documentation, and still make its module straight from its host procs, so nothing is parsed at runtime. Its tests then compile the source against the real host procs.

Defines

The host can set top-level constants from outside, like gcc -D; the CLI takes -D NAME=value. The value is read as the constant's declared type: -D SCALE=3 gives a Float constant 3.0, -D DEBUG alone means true, and a String takes the text as it is. A define the program doesn't declare is still a constant, with its type from its text: 7 an Int, 6.28 a Float, true, false or nothing a Bool, and anything else a String. A value that doesn't fit is a compile error.

N :: 1000          # rio -D N=20 game.rio makes N 20
DEBUG :: false     # rio -D DEBUG game.rio makes it true

Memory

A program's memory is all known when it compiles: its globals, one frame for each proc (procs that never run at the same time can share one), and its strings. Nothing is allocated while it runs. Programs that don't fit are compile errors, as are programs too big for the compiler's tables.

The host sees values in words of 4 bytes, little-endian:

typewords
Int, Float, Bool, enums, proc values1
String, Blob, []T2: where the bytes or elements start, and how many
[..]T, Blob[..]3: where the length is, where the elements start, the capacity
[N]TN elements, one after another
[..N]Tthe length, then N elements
Blob[N]N bytes, padded to whole words
structsthe fields in order
unionsthe type's index (in the order listed), then room for the biggest type

Positions ("where ... start") are byte offsets in the program's memory. Host procs get their arguments as these words, one after another.

Unused procs

A proc nothing can call is left out of the compiled program: its code, and its frame. So a module's whole surface can be imported, and a program pays only for the procs it uses. What can run is the top-level code (each module's too), the procs the host can find (main and name* in the main file, see What the host can find), and the procs used as values, or declared callback; then whatever they call, and so on. A dropped proc is still compiled, so its errors are still errors, and it keeps its place in the proc table (a few bytes). rio -m says how many were dropped, and how many instructions with them. A REPL keeps them all, since what's typed next may call any of them. Not dropped (yet): globals and constants only dropped code used, and a dropped proc's big locals (over 16 words), which are set aside as it compiles.