From 668e440b017ffc66ca9a10c3b67d557c16e6450d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 06:54:22 +0000 Subject: [PATCH] site: v0.13.0 --- diff --git a/site/public/_redirects b/site/public/_redirects index 6a45f1d..4bcc9de 100644 --- a/site/public/_redirects +++ b/site/public/_redirects @@ -1 +1 @@ -/download/latest /download/raz-0.11.1-linux-x86_64.tar.gz 302 +/download/latest /download/raz-0.13.0-linux-x86_64.tar.gz 302 diff --git a/site/public/advanced/bytes.html b/site/public/advanced/bytes.html index 75963ee..f48557a 100644 --- a/site/public/advanced/bytes.html +++ b/site/public/advanced/bytes.html @@ -201,37 +201,45 @@
The output is the same on a little-endian and a big-endian machine, because the bytes are written one at a time in the order the format says.
Str.toInt reads a decimal number with an optional sign and nothing else, and says whether it was one. Fmt goes the other way. Str.encode and Str.decode move between characters and UTF-8, as the first tour showed.
Str.toInt reads a decimal number with an optional sign and nothing else, and says whether it was one; Str.toHex reads hexadecimal digits, and Math.toReal a real, as a constant is written but with e for E too, to the nearest F64 exactly. Str.fromInt goes the other way into an array of 20 bytes, with no allocator, and Fmt does the rest. Str.encode and Str.decode move between characters and UTF-8, as the first tour showed.
module Numbers;
-import Out, Str;
+import Out, Str, Math, Fmt;
(* the sum of the numbers in a comma separated list, or -1 if any isn't *)
proc total(s: []U8): I64;
-var start, i: INT; sum, n: I64; ok: BOOL;
+var part: []U8; sum, n: I64; ok, more: BOOL;
begin
- start := 0;
- for i := 0 to LEN(s) do
- if (i = LEN(s)) or (s[i] = ',') then
- n, ok := Str.toInt(s[start..i]);
- if not ok then return -1; end;
- sum := sum + n;
- start := i + 1;
- end;
+ more := true;
+ while more do
+ part, s, more := Str.cut(s, ",");
+ n, ok := Str.toInt(part);
+ if not ok then return -1; end;
+ sum := sum + n;
end;
return sum;
end;
proc main*;
+var u: U64; x: F64; ok: BOOL; digits: [20]U8; k: INT;
begin
Out.int(total("10,20,-5,+7")); Out.ln;
Out.int(total("10,x,20")); Out.ln;
Out.int(total("10,,20")); Out.ln;
+ u, ok := Str.toHex("ff00");
+ if ok then Out.hex(u, 1); Out.ln; end;
+ x, ok := Math.toReal("-1.5e3");
+ if ok then Fmt.print("{}\n", [Fmt.real(x, 1)]); end;
+ digits, k := Str.fromInt(-9223372036854775807 - 1);
+ Out.str(digits[0..k]); Out.ln;
end;
end Numbers.
32
-1
-1
+FF00
+-1500.0
+-9223372036854775808
Save a program under its module’s name and run it with raz run Name.raz.
Thread doesThread is a module of raz, in the library, that says link static; and makes the clone, mmap and futex system calls itself. Here is its beginning, printed from the library:
$ sed -n '1,22p' $RAZLIB/Thread.linux-x86_64.raz
-(* Threads for programs without C, made with the clone system call.
+(* Threads, made with the clone system call in a program without C.
Each runs on a stack of its own, mmaped, with a page below it that
can't be touched, so that running off the stack stops the program
rather than spoiling what lies below; above it, a little stack for
Rt's signal handlers, so that they can say so. join waits on the
thread's id, which the kernel clears, and wakes it for, when the
- thread ends; a Mutex sleeps on a futex. A program with C threads
- uses C's, through pthread_create, and so can't use these. *)
+ thread ends; a Mutex sleeps on a futex.
+
+ A program that uses C makes them with C's pthread_create instead,
+ since C keeps things of each thread's own, errno and its buffers,
+ that a thread clone makes wouldn't have. The compiler's entry point
+ finds cCreate and cJoin by name and stores C's calls in them, as it
+ stores C's exit in Rt.cExit; so this module uses no C itself, and
+ serves both kinds of program. C's thread has a stack and a guard
+ page of C's; the mapping here holds only the handlers' stack and
+ the Thread. *)
module Thread;
import Sys, Rt;
-link static;
const
Failed*: ERROR = "can't start a thread";
- Stack = 1 shl 20;
- Alt = 16384; (* the signal handlers' stack *)
- Page = 4096;
- (* clone: share memory, files, signal handlers, the thread group;
- write the id to tid, and clear it at the end *)
- Flags = 350F00H;
- FutexWait = 0;
- FutexWake = 1;
+ Stack = 1 shl 20;
A program that imports it is therefore a static executable, and every thread it spawns is a Linux thread with a stack of its own, mapped with a page below it that can't be touched, so that running off the stack ends the program with a message and not with corruption.
module Hello;
import Out, Thread;
@@ -112,8 +112,7 @@
end;
end Mixed.
-$ raz Mixed.raz
-raz: Thread says link static, but C uses C
+$ raz Mixed.raz
Which one you want depends on what the program is for. A program that calls a C library (a window system, a database client) uses C's threads, C.pthread_create, because the library may assume them. A program that talks only to the kernel uses Thread, and is a static executable.
The rule isn't special to Thread. A module of yours that makes SYSCALLs directly, or relies on being the only owner of signal handlers, or sets up its own stacks, says link static; too, and then the compiler keeps C away from it for you. The module is documenting a fact about itself, and the compiler checks it where the program is assembled.
Save a program under its module’s name and run it with raz run Name.raz.
Read it from the top. a arrives in rdi and rsi, pointer and length; b in rdx and rcx, which the procedure saves in its frame. i and sum live in rbx and r12, which the procedure saves and restores because the convention says they belong to the caller. The loop's test is at the bottom, and its limit, LEN(a) - 1, is taken once.
In the body, each index has a check: cmp rbx, rsi; jae compares i with a's length in a single instruction and jumps to a stub at the end of the procedure, which calls Rt.check0 with the values it needs to say what failed. When the index is in range, which is nearly always, the cost is one compare and a branch that isn't taken.
The compares, the branches and the stubs are gone. It is faster, and it is no longer safe: an index out of range reads or writes whatever memory it reaches, and what follows isn't defined. A program turns checks off where it has measured and is sure, and says so at the top of the module, where a reader can see it.
make kernels in the compiler's sources measures test/bench's kernels against the same kernels written in Pascal and compiled by Free Pascal at -O2. The compiler's own guide, doc/compiler.md, gives the numbers and says what the compiler does and doesn't do. It is not the fastest code: it is code whose every instruction you can account for.
gdb finds procedures by name and lines by number, since the executable carries them: break Dot.dot and list work, and so does stepping a line at a time.RAZ_PROFILE=1 samples the program every millisecond and says which procedures and lines took the time.objdump -d disassembles what is really in the file, if you want to see the bytes behind a listing.$ raz -o Dot Dot.raz
$ objdump -d -M intel --no-show-raw-insn Dot | sed -n '/<Dot.dot>:/,/ret/p' | head -12
-0000000000403aa0 <Dot.dot>:
- 403aa0: push rbp
- 403aa1: mov rbp,rsp
- 403aa4: sub rsp,0x40
- 403aab: mov QWORD PTR [rbp-0x30],rbx
- 403aaf: xor ebx,ebx
- 403ab1: mov QWORD PTR [rbp-0x28],r12
- 403ab5: xor r12d,r12d
- 403ab8: mov QWORD PTR [rbp-0x20],rdx
- 403abc: mov QWORD PTR [rbp-0x18],rcx
- 403ac0: xor ebx,ebx
- 403ac2: mov r8,rsi
+0000000000403a60 <Dot.dot>:
+ 403a60: push rbp
+ 403a61: mov rbp,rsp
+ 403a64: sub rsp,0x40
+ 403a6b: mov QWORD PTR [rbp-0x38],rbx
+ 403a6f: xor ebx,ebx
+ 403a71: mov QWORD PTR [rbp-0x30],r12
+ 403a75: xor r12d,r12d
+ 403a78: mov QWORD PTR [rbp-0x28],r13
+ 403a7c: xor r13d,r13d
+ 403a7f: mov QWORD PTR [rbp-0x20],rdx
+ 403a83: mov QWORD PTR [rbp-0x18],rcx
The listing is what the encoder meant; objdump is what the processor will read. They tell the same story.
Save a program under its module’s name and run it with raz run Name.raz.
It is statically linked: no interpreter, no shared libraries, no libc. ldd says so.
strace lists every system call a program makes. This is the whole of it:
Both are fine. The point is that you choose, and the language says which you chose: a program uses C exactly when a module of it has an extern or a link of a library.
The compiler keeps only what can be reached from main and the module bodies. Unused procedures, variables and strings are left out. Here are two programs that differ by one large procedure that nothing calls.
$ raz Lean.raz
$ raz Heavy.raz
$ ls -l Lean Heavy | awk '{print $9, $5}'
-Heavy 28304
-Lean 28296
+Heavy 28208
+Lean 28200
The sizes differ by a few bytes, not by the hundreds of kilobytes that the table and the procedure would take: they are not in the executable at all. You can link in a library module without worrying what else it offers.
-s leaves out what is there for finding faults: the symbols and line numbers, the places a failure is told by, and the profiler. It is for the final build of a size-conscious program; the default keeps them so that gdb and failure messages work.
$ raz -s -o Lean-stripped Lean.raz
$ ls -l Lean Lean-stripped | awk '{print $9, $5}'
-Lean 28296
-Lean-stripped 6720
+Lean 28200
+Lean-stripped 6688
$ ./Lean-stripped
same program
raz -S Lean.raz is the other direction: it prints the machine code the compiler made, one source line at a time. The next two parts of the tour use it.
Save a program under its module’s name and run it with raz run Name.raz.
Here is a module with two files. Clock.linux-x86_64.raz is for this target; Clock.raz is what any other target would use. Both say what they are in Name, and both make ticks.
BOOL, F32, F64. Nothing converts unless it is written.match.match on a string.ERROR values and defer, in place of exceptions.The language is defined by the report, which is short on purpose: whatever it doesn’t allow, raz doesn’t have. Its second volume describes the library. Both are on one page, and each is -a PDF: the language (26 pages) and +a PDF: the language (27 pages) and the library.
raz 0.11.1 is a tarball for x86-64 Linux with the compiler, raz,
+
raz 0.13.0 is a tarball for x86-64 Linux with the compiler, raz,
its library, lib/, which it finds beside itself, the formatter
and the documentation tool as sources in tools/, and the manual
pages. It needs nothing else installed.
$ tar xzf raz-0.11.1-linux-x86_64.tar.gz
-$ ln -s $PWD/raz-0.11.1-linux-x86_64/raz ~/.local/bin/raz
+$ tar xzf raz-0.13.0-linux-x86_64.tar.gz
+$ ln -s $PWD/raz-0.13.0-linux-x86_64/raz ~/.local/bin/raz
$ raz run Hello.raz
The link works since raz finds lib/ beside its real path. The
manual pages are in man/: man ./man/raz-tutorial.7.
The address download/latest always leads to the newest release,
-and download/raz-0.11.1-linux-x86_64.tar.gz to this one, for
-good. Earlier releases: 0.11.0, 0.10.1, 0.10.0, 0.9.1, 0.9.0, 0.8.0, 0.7.1; each has its SHA-256 beside it, with
+and download/raz-0.13.0-linux-x86_64.tar.gz to this one, for
+good. Earlier releases: 0.12.0, 0.11.1, 0.11.0, 0.10.1, 0.10.0, 0.9.1, 0.9.0, 0.8.0, 0.7.1; each has its SHA-256 beside it, with
.sha256 added to its name.
-SHA-256 65e8d5ffd5b1b22b917e260516bbf708bb6af9e25264bec5881aee02c821b22b
+SHA-256 5b7caa843dea6cc97f9164e3dacb08e513f78604abcee2652085f7202eb3616f
diff --git a/site/public/raz-lib.pdf b/site/public/raz-lib.pdf
index d55ba7a..a768f73 100644
Binary files a/site/public/raz-lib.pdf and b/site/public/raz-lib.pdf differ
diff --git a/site/public/raz.pdf b/site/public/raz.pdf
index 9eac1dd..6de64ce 100644
Binary files a/site/public/raz.pdf and b/site/public/raz.pdf differ
diff --git a/site/public/spec.html b/site/public/spec.html
index 8299cb3..b3f23a3 100644
--- a/site/public/spec.html
+++ b/site/public/spec.html
@@ -38,7 +38,7 @@
raz is a small systems language, first made for x86-64 Linux. A raz program can lay a record over a device’s registers, read and write a device’s memory exactly as written, take part in lock-free algorithms, make system calls and call C libraries. It has no garbage collector and no hidden run time: what the program does is what its text says.
This is the first volume of the report: the language, what an implementation chooses, and the grammar. The second, doc/raz-lib.md, is the library, which sections 3 and 10 and appendix A name.
This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.
-This is version 2026-10.23 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for try (section 7.8); it breaks a program that has a name try.
+This is version 2026-10.25 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for try (section 7.8); it breaks a program that has a name try. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and match on a string (section 7.4); it breaks no program.
The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program written for it. What anyone wishes it had waits on a list, for a later thaw.
1. A first look
module Hello;
@@ -65,7 +65,18 @@
An integer is an untyped constant, which takes the type it is used as, up to the largest I64. A hexadecimal number above that, such as 0FFFFFFFFFFFFFFFFH, has type U64: so -1 never fits a U64, and the big number goes nowhere else without a conversion. Arithmetic on untyped constants doesn’t wrap: a result beyond the I64s, such as 9223372036854775807 + 1, is an error.
A real is an untyped constant too. It is read as the nearest F64 (a tie goes to the even one), and takes F32 or F64, whichever it is used as: an F32 is the nearest to that F64. Where nothing says which, it is an F64. A real never becomes an integer, nor an integer a real, without a conversion: x := 1; is an error for x: F64, and x := 1.0; is right.
Characters. 'a' is the code point of one character, an integer: 'a' is 97 and 'é' is 233.
-Strings. "héllo\n" is a sequence of bytes, the UTF-8 of its text: "é" is the two bytes 0C3H, 0A9H. Both characters and strings take the escapes \n (line feed), \t (tab), \r (carriage return), \0 (zero), \\, \', \", \u{H...} (the code point with hexadecimal value H…, up to 10FFFFH, in a string its UTF-8) and, in strings only, \xHH (the byte with hexadecimal value HH, which needn’t make valid UTF-8). In an escape, the hexadecimal digits may be capitals or not. A character or a string ends on the line it starts on.
+Strings. "héllo\n" is a sequence of bytes, the UTF-8 of its text: "é" is the two bytes 0C3H, 0A9H. Both characters and strings take the escapes \n (line feed), \t (tab), \r (carriage return), \0 (zero), \\, \', \", \u{H...} (the code point with hexadecimal value H…, up to 10FFFFH, in a string its UTF-8) and, in strings only, \xHH (the byte with hexadecimal value HH, which needn’t make valid UTF-8). In an escape, the hexadecimal digits may be capitals or not. A character, or a string in double quotes, ends on the line it starts on.
+Raw strings. `C:\tmp\"a b"` is a string written between backquotes: its bytes are the text between them as it stands, with no escapes, so \ and " are themselves. A raw string may run over lines, and each line break in it is a line feed, part of the string; a carriage return in it is left out.
+A raw string whose opening backquote ends its line, and whose closing backquote has only blanks (spaces and tabs) before it on its line, is indented: the text is its lines between those two, each without the closing line’s blanks at its start, and each ending in a line feed. So a long text may stand at the indentation of the code around it:
+proc usage;
+begin
+ Out.str(`
+ usage: tool [-v] file ...
+ -v say what is done
+ `);
+end;
+writes "usage: tool [-v] file ...\n -v say what is done\n". Each line of an indented raw string must start with the closing line’s blanks, the same spaces and tabs in the same order, or it is an error; a line holding only blanks is an empty line of the text. A raw string that isn’t indented is the text between its backquotes as it stands.
+A raw string can’t hold a backquote; a text that needs one joins it with +, as "`". Everywhere else a raw string is a string, as one in double quotes is.
Messages give the line and the column, counted in characters (section 10).
Comments are (* … *) and nest. By convention, the comment just before an exported declaration is its documentation, and the one before module is the module’s.
Operators and delimiters:
@@ -495,7 +506,7 @@
| rect(w, h): area := w * h;
| dot: area := 0.0;
end;
-match takes a variant, an enumeration or an integer type (a U8 included); other types are an error. It runs the arm of the case s holds. An arm may name the case’s fields, in order, all of them; the names stand for those fields of s itself, so assigning to one changes s; if s isn’t a variable, they stand for the fields of a copy. An arm may also leave the fields unnamed. Every case must have an arm, and only one, unless there is an | else: arm, which must come last.
+match takes a variant, an enumeration, an integer type (a U8 included) or a string, a []U8; other types are an error. It runs the arm of the case s holds. An arm may name the case’s fields, in order, all of them; the names stand for those fields of s itself, so assigning to one changes s; if s isn’t a variable, they stand for the fields of a copy. An arm may also leave the fields unnamed. Every case must have an arm, and only one, unless there is an | else: arm, which must come last.
match on an enumeration works the same way: each arm names one of the enumeration’s names, or several, separated by commas, for the arm to run when the value is any of them:
match op of
| sPlus, sMinus: kind := additive;
@@ -509,7 +520,14 @@
| '0' .. '9': digit := true;
| else:
end;
-A label is a constant, or lo .. hi, the constants from lo to hi, both included (unlike a slice, which leaves hi out; lo may not be above hi). Each must fit the integer’s type, and no value may be in two labels. An integer match must have an | else: arm, which may be empty. Whether an arm names a case or gives labels is set by the type matched: | Max: is a case of a variant or an enumeration, and a constant for an integer.
+A label is a constant, or lo .. hi, the constants from lo to hi, both included (unlike a slice, which leaves hi out; lo may not be above hi). Each must fit the integer’s type, and no value may be in two labels. An integer match must have an | else: arm, which may be empty.
+match on a string, a []U8 or what converts to one (section 5.10), runs the arm one of whose labels it equals, as = compares strings (section 6.5):
+match word of
+| "add", "plus": op := add;
+| "sub": op := sub;
+| else: op := none;
+end;
+A label is a string constant, and no two labels may be equal; there are no ranges. A match on a string must have an | else: arm, which may be empty. Whether an arm names a case or gives labels is set by the type matched: | Max: is a case of a variant or an enumeration, and a constant for an integer or a string.
7.5 Order of evaluation
Operands, arguments and the designators of an assignment are evaluated from left to right, and the designators of an assignment before its right side: a[i()] := g() calls i and then g.
7.6 Volatile
@@ -680,7 +698,8 @@
| digit { hexDigit } "H"
| digit { digit } "." digit { digit } [ "E" [ "+" | "-" ] digit { digit } ] .
char = "'" ( character | escape ) "'" .
-string = '"' { character | escape | "\x" anyHex anyHex } '"' .
+string = '"' { character | escape | "\x" anyHex anyHex } '"'
+ | "`" { rawChar } "`" .
escape = "\n" | "\t" | "\r" | "\0" | "\\" | "\'" | '\"'
| "\u{" anyHex { anyHex } "}" .
anyHex = hexDigit | "a" .. "f" .
@@ -765,7 +784,7 @@
Results = Type | "(" Type { "," Type } ")" .
ExternDeclaration = "extern" ( "proc" IdentDef Signature
| "var" IdentDef ":" Type ) .
-character is any character but a line break, and in a string any but " and \, in a character any but ' and \. A procedure argument of a generic module (section 3) is written as a QualIdent, which is a Type here. ... comes last in a Signature, and only in an extern. An Instruction is written in the target’s assembly language (appendix A).
character is any character but a line break, and in a string any but " and \, in a character any but ' and \. rawChar is any character but `, line breaks included. A procedure argument of a generic module (section 3) is written as a QualIdent, which is a Type here. ... comes last in a Signature, and only in an extern. An Instruction is written in the target’s assembly language (appendix A).
The second volume of the report on raz. The first, doc/raz.md, defines the language; this one defines the modules that come with it, and refers to the first’s sections by number, as “section 6.8”. Only the core binds every implementation (appendix A of the first volume); the rest is the standard library.
Modules written in raz, in the compiler’s lib directory:
Sysopen, openRead, create, read, write, seek, close, mmap, munmap, isTerminal, yield, exit; files and directories, rename, remove, removeDir, makeDir, chmod, symlink, readLink, info, openDir, nextEntry, closeDir; the terminal, rawMode, restoreMode, termSize, ready; the environment, env, workDir, setWorkDir; programs, pipes and signals; time, clock, now, sleep; random; and sockets. One file per target, and Sys.raz over the C library for the othersopen, openRead, create, read, write, seek, close, mmap, munmap, isTerminal, yield, exit; files and directories, rename, remove, removeDir, makeDir, chmod, symlink, readLink, info, openDir, nextEntry, closeDir; the terminal, rawMode, restoreMode, termSize, ready; the environment, env, workDir, setWorkDir; programs, pipes and signals; time, clock, now, sleep; random; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and Sys.raz over the C library for the othersOutThreadspawn, join, and Mutex; it says link staticspawn and join, and Mutex, in any program; in one that uses C, made by C’s pthread_createMemStr[]U8 strings: comparing, hashing, finding, UTF-8, reading a number[]U8 strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from textBytesA Lock is for short sections: a thread that finds it taken tries again, giving up the processor now and then. A Mutex sleeps. Each of Thread’s threads runs on a stack of its own, with a page below it that can’t be touched, so that running off the stack stops the program. A program that uses C makes its threads with C’s pthread_create, and so can’t use Thread.
A Lock is for short sections: a thread that finds it taken tries again, giving up the processor now and then. A Mutex sleeps. Each of Thread’s threads runs on a stack of its own, with a page below it that can’t be touched, so that running off the stack stops the program with a message, as in the main thread. In a program that uses C, Thread makes its threads with C’s pthread_create, so that C’s library knows of them; in one that doesn’t, with the system’s own calls. The program is the same either way.
module Out;
proc str*(s: []U8);
proc char*(c: U8);
@@ -958,6 +977,7 @@
tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *)
end;
Status* = (running, exited, killed, stopped);
+ Peer* = record ... end; (* whom a datagram came from *)
proc write*(fd: INT; s: []U8): INT; (* the bytes written *)
proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *)
@@ -1024,7 +1044,17 @@
proc listen*(path: []U8): INT; (* a Unix socket made at path, taking connections *)
proc listenTcp*(addr: []U8; port: INT): INT;
proc accept*(fd: INT): INT; (* the next connection, waiting for one *)
-proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *)
+proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *)
+proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *)
+proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *)
+proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *)
+proc reply*(fd: INT; peer: Peer; s: []U8): INT; (* s, as one datagram, to peer *)
+proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *)
+proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *)
+proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *)
+
+proc memFile*(size: INT): INT; (* a file of size zero bytes, in memory, with no name: its fd *)
+proc mapFile*(fd, size: INT): ^U8; (* the first size bytes of fd, shared; munmap gives them back; or nil *)
info follows a symbolic link: size, dir and file are those of what it names, and symlink says the path was a link, so that a program walking a tree can choose not to follow one back up it. A link to nothing is -ENOENT, as a missing file is. file lets a walk open only what holds data: opening a pipe would wait for a program to write to it.
nextEntry gives every name in the directory except . and .., in no particular order. rawMode turns off what the terminal does with keys before the program reads them (echo, line editing, signals, flow control), and NL to CR NL on output. A program that turns it on restores it before it ends: Rt.atExit is the place.
Files. open gives a file for what how asks: reading, writing or both; creating it if it is missing, readable by all and writable by its owner; with exclusive, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; truncating it to nothing; appending, each write going to its end, however other programs write it too. seek moves to pos from the start of the file, or from its end if fromEnd, where a negative pos reads back from the end. Info.mtime is what a program checks to see whether a file changed under it; Info.mode holds the nine bits of rwxrwxrwx, and chmod sets them, 1EDH (755) for a program, 1A4H (644) for data.
wait waits for the program pid to change, or for any of this program’s if pid is -1, and says which one, and how: exited, with its exit status; killed, with the number of the signal that ended it; or stopped, by Ctrl-Z or a signal, with the signal’s number. Without block it doesn’t wait, and is 0 and running if no program changed. A program that has nothing to wait for is -10 (ECHILD). kill sends a program a signal, sigTerm to end it; to a group, if pid is minus its number.
Signals. catch takes the signals sigs away from what they would do, ending the program most of them, and gives an fd that is ready to read when one comes, so that a program waits for signals with ready and readyOf as it waits for keys or a server. caught gives each signal that came, in turn. A later catch adds to the signals caught and gives the same fd. Catching sigKill or sigStop, or the signals Rt reports, SIGSEGV and SIGFPE, is -22 (EINVAL). ignore makes a signal do nothing, as a shell ignores sigTtou so that it can take its terminal back, and ignore(sig, false) gives it back what it does at first. A program that spawn starts begins with neither.
Time. clock is for measuring how long something takes: it never goes back, and says nothing of the date. now is the date and time, which a person or the network may set back. sleep waits ms milliseconds.
Sockets. connect is for talking to a server on the same machine, an X server at /tmp/.X11-unix/X0 say; connectTcp for one across the network, given its address as numbers, "127.0.0.1" or "::1": turning a name into an address is a library’s work over these. connectUdp gives a socket of datagrams, each send one message to that address and each read one message from it, as a program asks a name server. What a socket receives comes by read, what it sends goes by send, and close ends it. send is write for a socket, except when the other end has closed it: write would stop the program with the signal SIGPIPE, where send returns -32 (EPIPE), so that a program learns its server has gone and can end as it wants. listen and listenTcp are the other side: a server’s socket, on path, which must not be there yet, or on port at addr, "0.0.0.0" or "::" for every address the machine has; accept gives each connection that comes, as a socket of its own.
Sockets. connect is for talking to a server on the same machine, an X server at /tmp/.X11-unix/X0 say; connectTcp for one across the network, given its address as numbers, "127.0.0.1" or "::1": turning a name into an address is a library’s work over these. connectUdp gives a socket of datagrams, each send one message to that address and each read one message from it, as a program asks a name server. What a socket receives comes by read, what it sends goes by send, and close ends it. send is write for a socket, except when the other end has closed it: write would stop the program with the signal SIGPIPE, where send returns -32 (EPIPE), so that a program learns its server has gone and can end as it wants. listen and listenTcp are the other side: a server’s socket, on path, which must not be there yet, or on port at addr, "0.0.0.0" or "::" for every address the machine has; accept gives each connection that comes, as a socket of its own. A server started on port 0 gets a port the system chooses, which localPort says, as a test does that must not take a port in use. connectTcp waits as long as the system does, minutes for a host that never answers; connectTcpWithin gives up after ms milliseconds, with -110 (ETIMEDOUT).
listenUdp is the server’s side of connectUdp: a socket of datagrams from anyone, as a name server has. receive gives the next datagram, at most LEN(buf) bytes of it, the rest lost, and a Peer that reply sends an answer to.
Passing files and sharing memory. A program can give another an open file over a Unix socket: sendFds sends s, at least one byte, and with it fds, at most 16, each a file the other program then has open too; receiveFds reads into buf as read does, puts the fds that came with it in fds, as many as there is room for, closing the rest, and says how many bytes and how many fds. Each is closed in a program exec starts. memFile makes a file that lives in memory, of size zeroed bytes, and mapFile maps it, or any file, so that what one program writes there another sees at once: so a program hands an X server or a Wayland compositor its pixels, by sendFds of a memFile both map, rather than by copying them down a socket.
readyOf waits on several file descriptors at once, as a program does that listens to a server and a terminal both; a descriptor at its end, or failed, counts as ready, since a read of it won’t wait. It looks at the first 64 of fds, and connect and listen take a path of at most 107 bytes, as struct sockaddr_un holds; a longer one is -36 (ENAMETOOLONG).
A program that both reads from a descriptor and writes much to it, as a terminal writes what is pasted to its program while it reads what the program writes back, must not wait on either: the program may be waiting for its own writes to be read. setBlocking(fd, false) has a read or a write on fd that would wait return -11 (EAGAIN) at once, a write having written what there was room for; readyFor waits, as readyOf does, for something to read on any of reads or for room to write on any of writes. A descriptor in both is given once, when it is ready for either; the 64 it looks at are reads, then writes.
Strings, buffers and collections.
module Str;
proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *)
proc eq*(a, b: []U8): BOOL;
+proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter's capital and small the same *)
proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *)
proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *)
proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *)
+proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *)
proc hasPrefix*(s, p: []U8): BOOL;
+proc hasSuffix*(s, x: []U8): BOOL;
+proc trim*(s: []U8): []U8; (* s without the spaces at either end *)
+proc trimLeft*(s: []U8): []U8; (* s without the spaces at its start *)
+proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *)
+proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *)
+proc field*(s: []U8): ([]U8, []U8); (* the first run of bytes in s that aren't spaces, and what follows it *)
+proc lower*(s: []U8); (* s's ASCII capitals made small, in place *)
+proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *)
+proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *)
+proc replace*(b: ^Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *)
proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *)
proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *)
+proc decodeLast*(s: []U8): (I32, INT); (* the character s ends with, and its length in bytes *)
+proc next*(s: []U8; i: INT): (I32, INT); (* the character at i, and where the next starts; -1 at the end *)
+proc valid*(s: []U8): BOOL; (* s is UTF-8, every character rightly spelled *)
+proc chars*(s: []U8): INT; (* the characters in s, as decode reads them *)
proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *)
+proc toHex*(s: []U8): (U64, BOOL); (* s, in hexadecimal; false if it isn't a number a U64 holds *)
+proc fromInt*(n: I64): ([20]U8, INT); (* n in decimal: its bytes, and how many *)
proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *)
proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *)
proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *)
@@ -1117,12 +1167,30 @@
proc atan2*(y, x: F64): F64; (* the angle of (x, y) from the x axis, from -pi to pi *)
proc bits*(x: F64): U64; (* the bits of x, as IEEE 754 lays them out *)
proc fromBits*(u: U64): F64; (* the F64 whose bits are u *)
-proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *)
+proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *)
+proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *)
A Buf, a List and a Map take their memory from the allocator init gives them. Fmt’s format takes the next argument at each {}; {{ and }} stand for { and }. A Map hashes and compares its keys with the procedures it is given: import Count := Map([]U8, INT, Str.hash, Str.eq);.
Str reads and writes UTF-8. encode writes a character, of at most 10FFFFH, in one to four bytes; a number that isn’t one becomes FFFDH, the character that stands for a wrong one. decode reads the first character of s, and gives FFFDH and 1 for a byte that doesn’t start one rightly, and 0 and 0 for an empty s. toInt reads a decimal number, with a - or + before it if any, and nothing else.
Str reads and writes UTF-8. encode writes a character, of at most 10FFFFH, in one to four bytes; a number that isn’t one becomes FFFDH, the character that stands for a wrong one. decode reads the first character of s, and gives FFFDH and 1 for a byte that doesn’t start one rightly, and 0 and 0 for an empty s: a continuation, a byte followed by too few continuations, a surrogate, a character beyond 10FFFFH or one spelled in more bytes than it needs. decodeLast reads the last character in the same way, to walk a string backwards. valid says that decode finds no such byte in s, and chars counts the characters, each such byte one. next walks a string a character at a time, as decode reads them: from 0, it gives the character at i and where the one after it starts, and 0 and -1 when i is at the end, or past it:
c, i := Str.next(s, 0);
+while i >= 0 do
+ ... (* c is a character of s *)
+ c, i := Str.next(s, i);
+end;
+A string is bytes, and Str takes no more of Unicode than UTF-8: lower, upper and eqFold change and match ASCII letters alone, and nothing says how wide a character is on a terminal, nor which spellings of a text are the same.
A space, to trim, trimLeft, trimRight and field, is a blank, a tab, a line feed, a carriage return, a vertical tab or a form feed. cut gives what comes before the first sep in s, what comes after it, and true; or s, an empty string and false if s has no sep. field passes the spaces at the start of s and gives the bytes up to the next space or the end, and the rest of s from there; two empty strings if s holds only spaces. They take a string apart without memory of their own, each part a slice of s:
line, rest, more := Str.cut(text, "\n"); (* the first line, and the others *)
+key, val, ok := Str.cut(line, "=");
+w, rest := Str.field(line); (* the words of a line *)
+while LEN(w) > 0 do
+ Out.str(w); Out.ln;
+ w, rest := Str.field(rest);
+end;
+lower and upper write s’s bytes, so they take a variable’s, not a string literal’s (section 5.10). join and replace add to a Buf as Buf.str does; an empty old adds s as it is.
toInt reads a decimal number, with a - or + before it if any, and nothing else. toHex reads hexadecimal digits, capitals or not, and nothing else: no sign, 0x or H. fromInt writes a number as Out.int does, with a - if it is negative, its bytes the first of the 20 it gives.
le and be read an unsigned number of up to 8 bytes in a fixed order, little-endian or big-endian, whatever the machine’s own, and putLE and putBE write one; more than 8 bytes stops the program. They are how a file format or a network protocol is read and written:
len := U32(Str.le(header[4..8]));
Str.putBE(packet[0..2], U64(port));
+Math.toReal reads a real as a constant is written (section 2), with a - or + before it if any, its point and the digits after it, or its exponent, left out if it has none, and e taken as E: 2, -0.5 and 1.5e-3 are reals to it. It gives the F64 nearest the number, and false for anything else, or a number too big for an F64.
Map.next walks a map: from 0, it gives the next entry and the number to pass for the one after it, and -1 when there are no more. A walk sees each entry once if the map doesn’t change during it:
i, k, v := Names.next(ADR(names), 0);
while i >= 0 do
diff --git a/site/public/tour/hello.html b/site/public/tour/hello.html
index 992f33a..b8a6815 100644
--- a/site/public/tour/hello.html
+++ b/site/public/tour/hello.html
@@ -70,10 +70,10 @@
$ raz run Quiet.raz
hello, again
$ ls -l Quiet | awk '{print $5, "bytes"}'
-28304 bytes
+28208 bytes
The program is a static executable with no run time besides the code it uses, so it is a few kilobytes, and runs without any library installed.
Out can writeOut is the library's buffered output. It writes strings, single characters, numbers in decimal or hexadecimal, and a line feed. A string is written as it is, with \n for a line feed, \t for a tab, \" and \\.
Out is the library's buffered output. It writes strings, single characters, numbers in decimal or hexadecimal, and a line feed. A string is written as it is, with \n for a line feed, \t for a tab, \" and \\; or between backquotes, as a raw string with no escapes at all, which may run over lines (lesson 6).
module Numbers;
import Out;
diff --git a/site/public/tour/index.html b/site/public/tour/index.html
index 1b2dc0b..cc2e3ec 100644
--- a/site/public/tour/index.html
+++ b/site/public/tour/index.html
@@ -35,7 +35,7 @@
Procedures and several results Value and var parameters, several results, forward, and procedures as values.
Set
-- Arrays, slices and strings Five in a row, a window onto them, bytes in UTF-8, and checks on every index.
+- Arrays, slices and strings Five in a row, a window onto them, bytes in UTF-8, raw strings over lines, and checks on every index.
- Records and pointers Laid out as C lays out a struct, packed records, pointers and nil.
- Enumerations, sets, variants and match Names in order, sets as bits, and cases the compiler makes you handle.
- Memory and allocators No garbage collector; you choose where memory comes from, and what happens when it runs out.
diff --git a/site/public/tour/memory.html b/site/public/tour/memory.html
index 8954289..1725662 100644
--- a/site/public/tour/memory.html
+++ b/site/public/tour/memory.html
@@ -159,7 +159,7 @@
Buffers that grow
Buf is a growable byte buffer; like everything else it takes the allocator it grows from. It is what the program uses to build a string of unknown length.
Build.razmodule Build;
-import Out, Buf, Heap;
+import Out, Buf, Heap, Str;
proc main*;
var b: Buf.Buf; i: INT;
@@ -173,12 +173,21 @@
end;
Out.str(Buf.bytes(ADR(b))); Out.ln;
Out.int(b.len); Out.str(" bytes\n");
+ Buf.reset(ADR(b));
+ Str.join(ADR(b), ["red", "green", "blue"], ", ");
+ Out.str(Buf.bytes(ADR(b))); Out.ln;
+ Buf.reset(ADR(b));
+ Str.replace(ADR(b), "a-b-c", "-", " and ");
+ Out.str(Buf.bytes(ADR(b))); Out.ln;
end;
end Build.
item 1,item 2,item 3,item 4,item 5,
35 bytes
+red, green, blue
+a and b and c
+Str.join and Str.replace build into a Buf too: the parts with a separator between them, or a string with one text replaced by another wherever it is.
Pages
Under the allocators are pages from the system: Mem.pages(size) gives zeroed memory in whole pages, or nil, and Mem.unmap gives it back. Most programs never need to call it directly.
Save a program under its module’s name and run it with raz run Name.raz.
diff --git a/site/public/tour/slices.html b/site/public/tour/slices.html
index 60ebe3b..8bd65f9 100644
--- a/site/public/tour/slices.html
+++ b/site/public/tour/slices.html
@@ -51,7 +51,7 @@
end;
proc main*;
-var a: [5]INT; i: INT; s: []U8; name: [8]U8;
+var a: [5]INT; i: INT; s, val: []U8; name: [8]U8; ok: BOOL;
begin
for i := 0 to 4 do a[i] := i * i; end;
Out.int(sum(a)); Out.ln; (* the array, as a slice *)
@@ -60,9 +60,15 @@
Out.str(s[7..LEN(s)]); Out.ln;
if s[0..5] = "hello" then Out.str("it says hello\n"); end;
Out.int(Str.find(s, "world")); Out.ln;
+ _, val, ok := Str.cut(" size = 42 ", "=");
+ if ok then Out.str(Str.trim(val)); Out.ln; end;
name := "raz"; (* a copy, the program's own *)
name[0] := 'R';
Out.str(name[0..3]); Out.ln;
+ Out.str(`
+ C:\tmp\ "as it stands"
+ over lines
+ `);
end;
end Slices.
@@ -71,7 +77,10 @@
world
it says hello
7
+42
Raz
+C:\tmp\ "as it stands"
+ over lines
[5]INT is an array: five INTs in a row, copied when it is assigned or passed. Its length is part of its type, so [5]INT and [6]INT are different types, and LEN(a) of an array is a constant. Array literals are written in brackets, and need a type from the place they are used.
1 1 2 3 4 5
A string is a slice of bytes, []U8, in UTF-8, and a string constant's bytes are constant. A [8]U8 made from one is a copy you can change. Arrays and slices of integers compare with = and #, so strings do too. Str has the rest: ordering, finding, hashing, prefixes, UTF-8 and reading a number.
A string is a slice of bytes, []U8, in UTF-8, and a string constant's bytes are constant. A [8]U8 made from one is a copy you can change. Arrays and slices of integers compare with = and #, so strings do too. Str has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case, UTF-8, and numbers to and from text.
module Words;
import Out, Str;
@@ -164,8 +173,12 @@
if s[0..5] = "hello" then Out.str("it says hello\n"); end;
Out.int(Str.find(s, "world")); Out.ln; (* 7 *)
Out.int(Str.find(s, "xyz")); Out.ln; (* -1 *)
+ Out.int(Str.findLast("abcabc", "bc")); Out.ln; (* 4 *)
if Str.hasPrefix(s, "hell") then Out.str("prefix\n"); end;
+ if Str.hasSuffix(s, "world") then Out.str("suffix\n"); end;
Out.int(Str.cmp("apple", "banana")); Out.ln; (* -1 *)
+ if Str.eqFold("RAZ", "raz") then Out.str("the same, but for case\n"); end;
+ Out.str("["); Out.str(Str.trim(" padded \t\n")); Out.str("]\n");
n, ok := Str.toInt("-1234");
if ok then Out.int(n + 1); Out.ln; end;
@@ -175,6 +188,8 @@
name := "raz"; (* a copy, the program's own *)
name[0] := 'R';
Out.str(name[0..3]); Out.ln;
+ Str.upper(name[0..3]); (* in place: name is a variable *)
+ Out.str(name[0..3]); Out.ln;
end;
end Words.
@@ -182,48 +197,119 @@
it says hello
7
-1
+4
prefix
+suffix
-1
+the same, but for case
+[padded]
-1233
not a number
Raz
+RAZ
-A character is an I32, its code point. Str.decode reads the first character of a string and says how many bytes it took; Str.encode goes the other way. Byte length and character count are different things, and the program is the one that says which it wants.
module Runes;
+Taking a string apart
+Str.cut splits a string at the first place a separator is: what comes before it, what comes after, and whether it was there at all. Str.field gives the first word, past any spaces, and the rest. Neither copies anything: every part is a slice of the string it came from, so taking a line or a file apart costs no memory.
+Parts.razmodule Parts;
import Out, Str;
+
+const Text = "name = raz\nkind = language\nyear = 2026\n";
-(* the characters of s, not its bytes *)
-proc chars(s: []U8): INT;
-var i, n, size: INT;
+proc main*;
+var line, rest, key, val, w: []U8; more, ok: BOOL;
begin
- i := 0;
- n := 0;
- while i < LEN(s) do
- _, size := Str.decode(s[i..LEN(s)]);
- INC(i, size);
- INC(n);
+ (* line by line, then each line at its = *)
+ line, rest, more := Str.cut(Text, "\n");
+ while more do
+ key, val, ok := Str.cut(line, "=");
+ if ok then
+ Out.str(Str.trim(key)); Out.str(" is "); Out.str(Str.trim(val)); Out.ln;
+ end;
+ line, rest, more := Str.cut(rest, "\n");
+ end;
+
+ (* word by word *)
+ w, rest := Str.field(" the quick brown fox ");
+ while LEN(w) > 0 do
+ Out.str("<"); Out.str(w); Out.str(">");
+ w, rest := Str.field(rest);
end;
- return n;
+ Out.ln;
end;
+end Parts.
+name is raz
+kind is language
+year is 2026
+<the><quick><brown><fox>
+
+When Str.cut finds no separator it gives the whole string, an empty one and false, so the loop above ends after the last line that has a line feed. Building a string goes the other way, in a Buf: Str.join adds parts with a separator between them, and Str.replace adds a string with every match of one text replaced by another (lesson 9 shows Buf).
+Raw strings
+A string between backquotes is raw: its bytes are the text as it stands, with no escapes, so a backslash or a double quote is itself, and it may run over lines. When the opening backquote ends its line and the closing one has only spaces and tabs before it, those blanks come off the start of every line between, and the text keeps the indent of the code it sits in. Each line ends in a line feed, the last one too.
+Raw.razmodule Raw;
+import Out;
+
+const Path = `C:\Users\raz\"notes".txt`;
+
+proc usage;
+begin
+ Out.str(`
+ usage: tool [-v] file ...
+ -v say what is done
+ file read this, not standard input
+ `);
+end;
+
proc main*;
-var s: []U8; c: I32; size: INT; bytes: [4]U8; k: INT;
+begin
+ Out.str(Path); Out.ln;
+ usage;
+ Out.str(`a raw string on one line, \n and all`); Out.ln;
+end;
+
+end Raw.
+C:\Users\raz\"notes".txt
+usage: tool [-v] file ...
+ -v say what is done
+ file read this, not standard input
+a raw string on one line, \n and all
+
+A line of an indented raw string that doesn't start with the closing line's blanks is an error, so a slip in the indent is caught where it is made, not printed. A raw string can't hold a backquote; join one in with + and an ordinary string that holds it.
+UTF-8
+A character is an I32, its code point. A string stays bytes; UTF-8 is how Str reads them. Str.next walks a string a character at a time, giving each one and where the next starts, -1 at the end. Str.decode reads the first character of a string and says how many bytes it took, Str.decodeLast the last, and Str.encode goes the other way. Str.chars counts characters and Str.valid says whether a string is UTF-8 at all; a byte that doesn't start a character rightly reads as FFFDH, the character that stands for a wrong one. Byte length and character count are different things, and the program is the one that says which it wants.
+Runes.razmodule Runes;
+import Out, Str;
+
+proc main*;
+var s: []U8; c: I32; i, size: INT; bytes: [4]U8; k: INT;
begin
s := "caf\u{E9} \u{3BB}";
Out.int(LEN(s)); Out.str(" bytes, ");
- Out.int(chars(s)); Out.str(" characters\n");
- c, size := Str.decode(s[3..LEN(s)]);
- Out.int(I64(c)); Out.str(" took "); Out.int(size); Out.str(" bytes\n");
+ Out.int(Str.chars(s)); Out.str(" characters\n");
+
+ (* each character, as its code point *)
+ c, i := Str.next(s, 0);
+ while i >= 0 do
+ Out.int(I64(c)); Out.str(" ");
+ c, i := Str.next(s, i);
+ end;
+ Out.ln;
+
+ c, size := Str.decodeLast(s);
+ Out.int(I64(c)); Out.str(" is last, in "); Out.int(size); Out.str(" bytes\n");
bytes, k := Str.encode(955);
Out.str(bytes[0..k]); Out.ln;
+ if not Str.valid(s[0..4]) then Out.str("cut in the middle of a character\n"); end;
end;
end Runes.
8 bytes, 6 characters
-233 took 2 bytes
+99 97 102 233 32 955
+955 is last, in 2 bytes
λ
+cut in the middle of a character
+Str knows UTF-8 and no more of Unicode: its case is ASCII's, and it doesn't say how wide a character is on a terminal or which spellings of a text are the same. Those need tables bigger than the rest of the library.
Loops over lengths
A for loop takes its bounds once, and never steps past the last: with an empty slice, 0 to LEN(s) - 1 doesn't run at all, and the variable can't wrap. for takes a constant step with by, and while and repeat ... until do the rest.
Loops.razmodule Loops;
diff --git a/site/public/tour/variants.html b/site/public/tour/variants.html
index f6bd0ce..5bfaaee 100644
--- a/site/public/tour/variants.html
+++ b/site/public/tour/variants.html
@@ -70,6 +70,15 @@
end;
end;
+proc colour(name: []U8): Colour;
+begin
+ match name of
+ | "red", "rose": return red;
+ | "green": return green;
+ | else: return blue;
+ end;
+end;
+
proc main*;
var shapes: [3]Shape; i: INT; warm: Colours; c: Colour;
begin
@@ -83,6 +92,7 @@
for c := red to blue do
if c in warm then Out.int(I64(c)); Out.ln; end;
end;
+ Out.int(I64(colour("rose"))); Out.int(I64(colour("green"))); Out.ln;
end;
end Shapes.
@@ -92,6 +102,7 @@
digit letter other
0
1
+01
An enumeration is a list of names, in order. They convert to their position and back, and a for loop can run over them. An integer that has no name at its position is a failed check, never a silent wrong value.
Save a program under its module’s name and run it with raz run Name.raz.
On a string, match takes string constants, several to an arm if need be, and compares them as = does. It too must have an | else:. It is how a program reads a command, an option or a keyword without a chain of elsifs.
module Commands;
+import Out;
+
+type Op = (none, add, sub, show);
+
+proc op(word: []U8): Op;
+begin
+ match word of
+ | "add", "plus", "+": return add;
+ | "sub", "minus", "-": return sub;
+ | "show": return show;
+ | else: return none;
+ end;
+end;
+
+proc main*;
+var total: INT; words: [6][]U8; i: INT;
+begin
+ words := ["add", "plus", "sub", "show", "times", "-"];
+ for i := 0 to 5 do
+ match op(words[i]) of
+ | add: INC(total);
+ | sub: DEC(total);
+ | show: Out.int(total); Out.ln;
+ | none: Out.str(words[i]); Out.str("?\n");
+ end;
+ end;
+ Out.int(total); Out.ln;
+end;
+
+end Commands.
+1
+times?
+0
+
+Two labels that are the same string are an error, as two arms for one case are.
Save a program under its module’s name and run it with raz run Name.raz.