From 1ea1e717cf450353595bc598205349da09cc73e5 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 10:03:34 +0100 Subject: [PATCH] site: the tours for v0.16.0 Records: a constant typed []Planet takes its literal's length. Collections: Map.done and Vec's done deferred; Str.free with Str.copy. Files: Files.load says why. Processes: signals, catch and handle. --- diff --git a/site/content/advanced/03-collections.txt b/site/content/advanced/03-collections.txt index 61d357a..1537bac 100644 --- a/site/content/advanced/03-collections.txt +++ b/site/content/advanced/03-collections.txt @@ -121,6 +121,7 @@ w, rest: []U8; key: []U8; v: INT; all: []Entry; begin Count.init(ADR(m), Heap.heap); + defer Count.done(ADR(m)); w, rest := Str.field(Text); while LEN(w) > 0 do k, _ := Count.get(ADR(m), w); @@ -128,6 +129,7 @@ w, rest := Str.field(rest); end; Entries.init(ADR(entries), Heap.heap); + defer Entries.done(ADR(entries)); found, key, v := Count.next(ADR(m), 0); while found >= 0 do Entries.push(ADR(entries), {word: key, n: v}); @@ -143,7 +145,7 @@ end Freq. ``` -The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with `Str.copy`. +The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with `Str.copy`, and gives each back with `Str.free` before `Count.done` gives back the map's own memory. ## Choosing diff --git a/site/content/advanced/04-files.txt b/site/content/advanced/04-files.txt index 6c57e1d..5d3fb1f 100644 --- a/site/content/advanced/04-files.txt +++ b/site/content/advanced/04-files.txt @@ -25,10 +25,13 @@ end; ok := Files.read("missing.txt", ADR(b)); if not ok then Out.str("missing.txt: not there\n"); end; + if Files.load("missing.txt", ADR(b)) = -2 then Out.str("missing.txt: ENOENT\n"); end; end; end Notes. ``` + +When the reason matters, `Files.load` says it: 0, or minus the errno of what failed, -2 (ENOENT) for no such file, -13 (EACCES) for one not allowed. ## Descriptors diff --git a/site/content/advanced/05-processes.txt b/site/content/advanced/05-processes.txt index c91a7b8..c53d58b 100644 --- a/site/content/advanced/05-processes.txt +++ b/site/content/advanced/05-processes.txt @@ -1,6 +1,6 @@ title: Time, randomness and other programs part: The library -summary: Clocks, sleeping, random bytes, and running a program with its output piped back. +summary: Clocks, sleeping, random bytes, running a program with its output piped back, and signals. --- Everything here is in `Sys`, and is the same procedures on every target raz supports: through system calls on Linux, through the C library elsewhere. @@ -153,4 +153,27 @@ end; end Where. +``` + +## Signals + +A signal, Ctrl-C's `sigInt` or `sigTerm` from `kill`, ends a program unless it says otherwise. `Sys.catch` takes signals away from what they would do and gives an fd that is ready to read when one comes, so a program that waits on fds anyway, with `Sys.readyOf`, waits for signals the same way; `Sys.caught` gives each in turn. `Sys.handle` is for a program that waits inside a system call, as a shell waits for its program: the signal is noted for `Sys.came`, and the call it comes during fails with -4 (EINTR) rather than going on. Nothing of the program runs when the signal comes; it looks after the call returns. + +```raz Signals run +module Signals; +import Out, Sys; + +proc main*; +var fd: INT; +begin + fd := Sys.catch([Sys.sigUsr1]); + _ := Sys.kill(Sys.pid(), Sys.sigUsr1); + if Sys.ready(fd, 1000) and (Sys.caught(fd) = Sys.sigUsr1) then Out.str("caught sigUsr1\n"); end; + _ := Sys.handle(Sys.sigUsr2); + _ := Sys.kill(Sys.pid(), Sys.sigUsr2); + if Sys.came(Sys.sigUsr2) then Out.str("sigUsr2 came\n"); end; + if not Sys.came(Sys.sigUsr2) then Out.str("and was seen once\n"); end; +end; + +end Signals. ``` diff --git a/site/content/tour/07-records.txt b/site/content/tour/07-records.txt index b13ba25..4ab492e 100644 --- a/site/content/tour/07-records.txt +++ b/site/content/tour/07-records.txt @@ -51,7 +51,7 @@ type Planet = record name: []U8; moons: INT; water: BOOL; end; -const Inner: [4]Planet = [ +const Inner: []Planet = [ {name: "Mercury"}, {name: "Venus"}, {name: "Earth", moons: 1, water: true}, @@ -70,7 +70,7 @@ end Planets. ``` -Fields left out are zero, as in any literal: Mercury's `moons` is 0 and its `water` is `false`. +Fields left out are zero, as in any literal: Mercury's `moons` is 0 and its `water` is `false`. A constant typed `[]Planet` takes its length from its literal, here 4, so a planet added to the list needs no count changed. ## Layout diff --git a/site/public/advanced/collections.html b/site/public/advanced/collections.html index 190b554..4021428 100644 --- a/site/public/advanced/collections.html +++ b/site/public/advanced/collections.html @@ -152,6 +152,7 @@ w, rest: []U8; key: []U8; v: INT; all: []Entry; begin Count.init(ADR(m), Heap.heap); + defer Count.done(ADR(m)); w, rest := Str.field(Text); while LEN(w) > 0 do k, _ := Count.get(ADR(m), w); @@ -159,6 +160,7 @@ w, rest := Str.field(rest); end; Entries.init(ADR(entries), Heap.heap); + defer Entries.done(ADR(entries)); found, key, v := Count.next(ADR(m), 0); while found >= 0 do Entries.push(ADR(entries), {word: key, n: v}); @@ -181,7 +183,7 @@ 1 mat 1 on -

The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with Str.copy.

+

The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with Str.copy, and gives each back with Str.free before Count.done gives back the map's own memory.

Choosing

Use a List when you push and pop at one end and walk it in order. Use a Vec when you add at the end and index, and don't know how many will come; an array when you do. Use a Map when you look things up by key. Use Sort on either's slice when you want an order. When none of them fits, they are each under a hundred lines of raz in lib/, and the advanced tour's allocator lesson shows what is underneath.

Save a program under its module’s name and run it with raz run Name.raz.

← Strings and bytesContents
diff --git a/site/public/advanced/files.html b/site/public/advanced/files.html index b71e550..4ca6478 100644 --- a/site/public/advanced/files.html +++ b/site/public/advanced/files.html @@ -63,13 +63,16 @@ end; ok := Files.read("missing.txt", ADR(b)); if not ok then Out.str("missing.txt: not there\n"); end; + if Files.load("missing.txt", ADR(b)) = -2 then Out.str("missing.txt: ENOENT\n"); end; end; end Notes.
23 bytes: first line
 second line
 missing.txt: not there
+missing.txt: ENOENT
 
+

When the reason matters, Files.load says it: 0, or minus the errno of what failed, -2 (ENOENT) for no such file, -13 (EACCES) for one not allowed.

Descriptors

Underneath, a file is a descriptor, an INT. Sys.open takes the path and the way to open it as a set; read, write, seek and close do what they say. A result that can fail is a number, and minus an errno if it failed, as on Linux itself. Reading in a loop is the pattern for anything too big to hold at once.

Count.raz
module Count;
diff --git a/site/public/advanced/index.html b/site/public/advanced/index.html
index 2e3853c..c72d83c 100644
--- a/site/public/advanced/index.html
+++ b/site/public/advanced/index.html
@@ -33,7 +33,7 @@
 
  • Strings and bytes Copying, hashing and ordering strings, searching bytes many at a time, and reading and writing binary formats.
  • Lists, arrays, maps and sorting List(T), Vec(T), Map(K, V) and Sort(T), put together in a word-frequency program.
  • Files and the command line Whole files with Files, descriptors and directories with Sys, and options with Opt.
  • -
  • Time, randomness and other programs Clocks, sleeping, random bytes, and running a program with its output piped back.
  • +
  • Time, randomness and other programs Clocks, sleeping, random bytes, running a program with its output piped back, and signals.
  • Writing an allocator Mem.Allocator is two procedures; make one over a fixed array, and one that counts.
  • Standing alone

      diff --git a/site/public/advanced/processes.html b/site/public/advanced/processes.html index d76410c..ebd3979 100644 --- a/site/public/advanced/processes.html +++ b/site/public/advanced/processes.html @@ -184,6 +184,28 @@ end Where.
    unset reads as empty
     hello from /tmp
    +
    +

    Signals

    +

    A signal, Ctrl-C's sigInt or sigTerm from kill, ends a program unless it says otherwise. Sys.catch takes signals away from what they would do and gives an fd that is ready to read when one comes, so a program that waits on fds anyway, with Sys.readyOf, waits for signals the same way; Sys.caught gives each in turn. Sys.handle is for a program that waits inside a system call, as a shell waits for its program: the signal is noted for Sys.came, and the call it comes during fails with -4 (EINTR) rather than going on. Nothing of the program runs when the signal comes; it looks after the call returns.

    +
    Signals.raz
    module Signals;
    +import Out, Sys;
    +
    +proc main*;
    +var fd: INT;
    +begin
    +    fd := Sys.catch([Sys.sigUsr1]);
    +    _ := Sys.kill(Sys.pid(), Sys.sigUsr1);
    +    if Sys.ready(fd, 1000) and (Sys.caught(fd) = Sys.sigUsr1) then Out.str("caught sigUsr1\n"); end;
    +    _ := Sys.handle(Sys.sigUsr2);
    +    _ := Sys.kill(Sys.pid(), Sys.sigUsr2);
    +    if Sys.came(Sys.sigUsr2) then Out.str("sigUsr2 came\n"); end;
    +    if not Sys.came(Sys.sigUsr2) then Out.str("and was seen once\n"); end;
    +end;
    +
    +end Signals.
    +
    caught sigUsr1
    +sigUsr2 came
    +and was seen once
     

    Save a program under its module’s name and run it with raz run Name.raz.

    ← Files and the command lineContents
    diff --git a/site/public/tour/records.html b/site/public/tour/records.html index 329f10a..d7478dc 100644 --- a/site/public/tour/records.html +++ b/site/public/tour/records.html @@ -114,7 +114,7 @@ type Planet = record name: []U8; moons: INT; water: BOOL; end; -const Inner: [4]Planet = [ +const Inner: []Planet = [ {name: "Mercury"}, {name: "Venus"}, {name: "Earth", moons: 1, water: true}, @@ -135,7 +135,7 @@ Venus has no moon 3 moons in all -

    Fields left out are zero, as in any literal: Mercury's moons is 0 and its water is false.

    +

    Fields left out are zero, as in any literal: Mercury's moons is 0 and its water is false. A constant typed []Planet takes its length from its literal, here 4, so a planet added to the list needs no count changed.

    Layout

    A record's layout is C's: each field at the next multiple of its own alignment, and the whole padded to a multiple of its largest alignment. SIZE(T) says how many bytes. A packed record has no padding, which is what a file format or a wire protocol wants.

    Layout.raz
    module Layout;