From 16bb4f80958c85fa9ca7177986355250e66ac078 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 09:35:29 +0100 Subject: [PATCH] site: v0.14.0 Co-Authored-By: Claude Opus 5.5 --- diff --git a/site/public/_redirects b/site/public/_redirects index 4bcc9de..19bf6b2 100644 --- a/site/public/_redirects +++ b/site/public/_redirects @@ -1 +1 @@ -/download/latest /download/raz-0.13.0-linux-x86_64.tar.gz 302 +/download/latest /download/raz-0.14.0-linux-x86_64.tar.gz 302 diff --git a/site/public/advanced/listing.html b/site/public/advanced/listing.html index 6d0c785..a8c1760 100644 --- a/site/public/advanced/listing.html +++ b/site/public/advanced/listing.html @@ -258,18 +258,18 @@
$ raz -o Dot Dot.raz
 $ objdump -d -M intel --no-show-raw-insn Dot | sed -n '/<Dot.dot>:/,/ret/p' | head -12
-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
+0000000000403ae0 <Dot.dot>: + 403ae0: push rbp + 403ae1: mov rbp,rsp + 403ae4: sub rsp,0x40 + 403aeb: mov QWORD PTR [rbp-0x38],rbx + 403aef: xor ebx,ebx + 403af1: mov QWORD PTR [rbp-0x30],r12 + 403af5: xor r12d,r12d + 403af8: mov QWORD PTR [rbp-0x28],r13 + 403afc: xor r13d,r13d + 403aff: mov QWORD PTR [rbp-0x20],rdx + 403b03: 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.

← System callsContents
diff --git a/site/public/advanced/static.html b/site/public/advanced/static.html index 6379685..368bd49 100644 --- a/site/public/advanced/static.html +++ b/site/public/advanced/static.html @@ -65,7 +65,7 @@ $ ldd Bare not a dynamic executable $ ls -l Bare | awk '{print $5, "bytes"}' -26032 bytes +26224 bytes

It is statically linked: no interpreter, no shared libraries, no libc. ldd says so.

What it does when it runs

strace lists every system call a program makes. This is the whole of it:

@@ -97,8 +97,8 @@ Cprint: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, with debug_info, not stripped $ ldd Cprint | sed 's/ (0x[0-9a-f]*)//' linux-vdso.so.1 - libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 - /lib64/ld-linux-x86-64.so.2 + libc.so.6 => /usr/lib/libc.so.6 + /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2

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.

Only what is reached

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.

@@ -134,15 +134,15 @@
$ raz Lean.raz
 $ raz Heavy.raz
 $ ls -l Lean Heavy | awk '{print $9, $5}'
-Heavy 28208
-Lean 28200
+Heavy 28408 +Lean 28392

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.

Stripping

-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 28200
-Lean-stripped 6688
+Lean 28392
+Lean-stripped 6856
 $ ./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.

← Writing an allocatorContents
diff --git a/site/public/advanced/targets.html b/site/public/advanced/targets.html index 19296c3..2def322 100644 --- a/site/public/advanced/targets.html +++ b/site/public/advanced/targets.html @@ -52,7 +52,7 @@ Sys.raz Thread.linux-x86_64.raz $ cd $RAZLIB && grep -c SYSCALL Sys.linux-x86_64.raz Sys.raz -Sys.linux-x86_64.raz:89 +Sys.linux-x86_64.raz:93 Sys.raz:0

A module of your own

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.

diff --git a/site/public/download/raz-0.14.0-linux-x86_64.tar.gz b/site/public/download/raz-0.14.0-linux-x86_64.tar.gz new file mode 100644 index 0000000..96fb91e Binary files /dev/null and b/site/public/download/raz-0.14.0-linux-x86_64.tar.gz differ diff --git a/site/public/download/raz-0.14.0-linux-x86_64.tar.gz.sha256 b/site/public/download/raz-0.14.0-linux-x86_64.tar.gz.sha256 new file mode 100644 index 0000000..5d42cc3 --- /dev/null +++ b/site/public/download/raz-0.14.0-linux-x86_64.tar.gz.sha256 @@ -0,0 +1 @@ +a410faef783d33ccd84f85b0788f7f06e2e9f682bd6c8945e8729fce52d8abba raz-0.14.0-linux-x86_64.tar.gz diff --git a/site/public/index.html b/site/public/index.html index 28a6a4a..22893ca 100644 --- a/site/public/index.html +++ b/site/public/index.html @@ -42,7 +42,7 @@
~10,700 lines of compiler ~500K lines compiled a second - 1 report, 27 pages + 1 report, 28 pages static executables, no run time to install
@@ -109,28 +109,28 @@

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 (27 pages) and +a PDF: the language (28 pages) and the library.

Download

-

raz 0.13.0 is a tarball for x86-64 Linux with the compiler, raz, +

raz 0.14.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.

- Download raz 0.13.0 - SHA-256 + Download raz 0.14.0 + SHA-256
-
$ 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
+
$ tar xzf raz-0.14.0-linux-x86_64.tar.gz
+$ ln -s $PWD/raz-0.14.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.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 +and download/raz-0.14.0-linux-x86_64.tar.gz to this one, for +good. Earlier releases: 0.13.0, 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 5b7caa843dea6cc97f9164e3dacb08e513f78604abcee2652085f7202eb3616f

+

SHA-256 a410faef783d33ccd84f85b0788f7f06e2e9f682bd6c8945e8729fce52d8abba

raz is under the ISC license.
diff --git a/site/public/raz-lib.pdf b/site/public/raz-lib.pdf index a768f73..c49cdbc 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 6de64ce..5e9c7a1 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 b3f23a3..3561a27 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.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.

+

This is version 2026-10.26 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. 2026-10.26 is a fifth: an INT widens to an I64 wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, v.(T), reads and writes the bytes of a variable as a T (section 6.9); 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;
@@ -207,7 +207,7 @@
 
 
 
-

INT is the everyday integer: lengths, indexes and sizes are INTs. It is as big as an address, so it is the integer a pointer converts to (section 6.7), and a word the atomic built-ins work on (section 6.8). INT and I64 are two types, even where they have the same size: an INT goes silently into an I64 (section 5.10), but the two don’t mix in an expression (section 6.1), and an I64 becomes an INT only by INT(x), which may drop bits on a smaller target. A type bigger than the largest INT in bytes is an error.

+

INT is the everyday integer: lengths, indexes and sizes are INTs. It is as big as an address, so it is the integer a pointer converts to (section 6.7), and a word the atomic built-ins work on (section 6.8). INT and I64 are two types, even where they have the same size: an INT goes silently into an I64 wherever it meets one, in an expression too (sections 5.10 and 6.1), and an I64 becomes an INT only by INT(x), which may drop bits on a smaller target. A type bigger than the largest INT in bytes is an error.

F32 and F64 exist only on a target that has reals (appendix A). On one that hasn’t, a real literal, and a use of F32, F64 or of a name whose type is made of them, is an error; a declaration that no used code names isn’t, so a module like Fmt keeps one interface on every target.

Each is aligned as the target’s C aligns it: to its size on x86-64. So are ERROR (section 5.8), pointers and procedures, each as big as an address; a slice is aligned as an address is.

5.2 Arrays

@@ -266,7 +266,7 @@
  • A is INT and B is I64, or A is UINT and B is U64, which loses nothing on any target.
  • A string’s bytes are constant. A []U8 or ^U8 made from a string literal, or a slice of a string constant, points at them, and writing through it isn’t defined: on a target with read-only memory, it may stop the program as a bad address (section 10). A [n]U8 made from a string is a copy, the program’s to change.

    -

    Nothing else converts silently. In particular, there is no other conversion between integer types, between integers and reals, or between F32 and F64, without a written conversion (section 6.7). Results passed on whole need the same types exactly: in a, b := f(x) and in return f(x), an INT result doesn’t pass up as an I64. Elsewhere, return n, n included, an INT widens as above; the bounds of a for and the step of INC and DEC take the type of the variable they work on, and an INT is not widened to it.

    +

    Nothing else converts silently. In particular, there is no other conversion between integer types, between integers and reals, or between F32 and F64, without a written conversion (section 6.7). An INT widens to I64, and a UINT to U64, wherever its value goes where an I64 is wanted: in a, b := f(x) and return f(x) as in any other assignment, to the bounds of a for and the step of INC and DEC whose variable is an I64, and in an expression, where it meets an I64 (section 6.1). The types themselves stay two: a ^INT isn’t a ^I64, an INT variable isn’t passed to a var parameter of type I64, and a procedure returning an INT isn’t of a procedure type returning an I64.

    6. Expressions

    Expr       = Conjunct { "or" Conjunct } .
     Conjunct   = Relation { "and" Relation } .
    @@ -285,12 +285,12 @@
     Element    = Expr | ident ":" Expr .
     Designator = QualIdent { Selector } .
     Selector   = "." ident | "^" | "[" Expr [ ".." Expr ] "]"
    -           | "(" [ Expr { "," Expr } ] ")" .
    + | "(" [ Expr { "," Expr } ] ")" | "." "(" Type ")" .

    The operators bind in the order of the grammar: not, ~ and try (section 7.8) most tightly, then the multiplying operators, then the adding ones, then the relations, then and, then or. So a + b * c = d is (a + (b * c)) = d, x & 0FH = 0 is (x & 0FH) = 0, and i < n and a[i] # 0 or done is ((i < n) and (a[i] # 0)) or done. A unary minus applies to the first term: -a * b is -(a * b).

    and, or and not are for BOOLs only; &, |, xor and ~ are for integers only.

    -

    A pointer to a record isn’t the record: its fields are p^.f, never p.f, and the elements of a pointer to an array are p^[i]. A statement starts with a name or try, so ^T(x)^ := v isn’t one.

    +

    A pointer to a record isn’t the record: its fields are p^.f, never p.f, and the elements of a pointer to an array are p^[i]. A statement starts with a name or try, so ^T(x)^ := v isn’t one; a view, mem[a].(I64) := v (section 6.9), says it without a pointer.

    6.1 Integers

    -

    Both operands of + - * div mod & | xor, and of a relation, have the same integer type, or one is an untyped constant that takes the other’s type, which it must fit. So do v and k in INC(v, k) and DEC(v, k). The result has that type and wraps around at its width: in U8, 200 + 100 is 44. A unary minus wraps too: for x of an unsigned type, -x is 0 - x. Two integer types are never mixed, whatever their sizes: with i: I64, i < LEN(s) is an error, and i should be an INT.

    +

    Both operands of + - * div mod & | xor, and of a relation, have the same integer type, or one is an untyped constant that takes the other’s type, which it must fit, or one is an INT and the other an I64 (a UINT and a U64), and the INT widens to I64 (section 5.10). So do v and k in INC(v, k) and DEC(v, k), where only k widens: an I64 k isn’t added to an INT v. The result has that type and wraps around at its width: in U8, 200 + 100 is 44. A unary minus wraps too: for x of an unsigned type, -x is 0 - x. Other integer types are never mixed, whatever their sizes: with i: I32, i < LEN(s) is an error, and i should be an INT. With pos: I64, pos + LEN(s) is an I64.

    Where the report asks for an integer and names no type, as an index, a slice’s bound, a shift’s count, a pointer’s offset i in p + i, or a conversion’s operand, any integer type will do, and its value is taken. On a target whose INT is narrower, a pointer’s offset is cut to an INT.

    SIZE(T), and LEN of an array or a string constant, are untyped constants, like a literal.

    div rounds toward zero and mod takes the sign of the dividend, as in C: -7 div 2 is -3, -7 mod 2 is -1. Dividing by zero, and dividing the smallest value of a signed type by -1, stop the program (section 10), whatever the type and the target.

    @@ -468,7 +468,35 @@

    NEW(p, a) calls a^.alloc(a, SIZE(T), A), where A is the alignment of T, and zeroes the bytes it gets if they aren’t nil. Mem.Allocator (the library, doc/raz-lib.md) is the one type of the library the language names.

    COPY is a statement, as INC is. It copies the bytes of LEN(s) elements as one block, as an assignment of an array does, and is right when d and s overlap, as in COPY(a[1..n], a[0..n - 1]). It is checked as a slice is: past LEN(d), it stops the program, unless the module says check off;.

    INC and DEC evaluate v once, so INC(a[f(i)]) calls f once. They read v and then write it: two accesses, even through a ^VOLATILE pointer, and not atomic; XADD is the atomic one.

    -

    For CAS, XCHG and XADD, p is a ^T or ^VOLATILE T where T is INT, UINT or a pointer type (INT or UINT for XADD), on every target. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation’s choice (appendix A).

    +

    For CAS, XCHG and XADD, p is a ^T or ^VOLATILE T where T is INT, UINT or a pointer type (INT or UINT for XADD), on every target. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation’s choice (appendix A). ### 6.9 Views

    +

    A view reads and writes the bytes of a variable as a value of another type. v.(T), with v a variable, is the T whose bytes start where v starts:

    +
    var mem: [4096]U8; n: I64; x: F64; bits: U64;
    +
    +mem[16].(I64) := -1;     (* the 8 bytes mem[16] to mem[23] now hold -1 *)
    +n := mem[16].(I64);      (* and are read back as an I64: n is -1 *)
    +bits := x.(U64);         (* the 8 bytes of x, as a U64 *)
    +

    A view is a variable, not a value: mem[16].(I64) is made of the 8 bytes mem[16] to mem[23], as p^ is the variable p points at. Reading it reads those bytes; assigning it writes them, and so changes mem. Nothing is copied to make it. It stands wherever a variable may: on the left of :=, as the variable of INC, and with selectors after it:

    +
    type Header = record magic: U32; count: U32; end;
    +var buf: [512]U8; k: U32;
    +
    +k := buf[0].(Header).count;    (* bytes 4 to 7 of buf, as a U32 *)
    +buf[0].(Header).count := 0;
    +INC(mem[a].(I64), 8);
    +

    A view isn’t a conversion. A conversion works on a value, a view on bytes. With x: F64 holding 1.5, I64(x) is 1, the number rounded toward zero, while x.(I64) is 3FF8000000000000H, the bits that hold 1.5. With n: I64 holding 258, U8(n) is 2, the number cut to its low byte, while n.(U8) is the first byte of n in memory, which is the low one only on a target that puts its low byte first (appendix A). When the program means a number, it wants a conversion; when it means the bytes as they lie, a view.

    +

    The types. T is an integer type, a real type, a pointer type, or a record or array made of these alone, at any depth: the types for which any bytes at all are a value. BOOL, an enumeration, a set, a slice, a variant, a procedure type and ERROR are left out, since bytes that are none of their values would make the program mean nothing. To read one of them from bytes, view the integer and convert it: Colour(mem[a]). v may be of any type, and is a variable: a name of one, a field or element of one, or what a pointer points at; not a constant, nor the result of a call or of an operator.

    +

    Where its bytes lie. The T lies within the variable v is part of:

    +
      +
    • for an element of an array or a slice, a[i].(T), from that element to the end of a: mem[4088].(I64) is the last 8 bytes of mem, and mem[4090].(I64) would run 2 bytes past its end;
    • +
    • for anything else, within v itself: x.(U64) is allowed only if x has at least 8 bytes.
    • +
    +

    This is checked, as an index is: by the compiler when it can tell, otherwise while the program runs, where a view that runs past the end is a failed check (section 6.5), unless the module says check off;. An element reached through a pointer, p[i].(T), is checked no more than p[i] is, since a pointer has no length.

    +

    Any address. The T may start at any byte: mem[3].(I64) is an I64 whose first byte is mem[3]. As a field of a packed record is (section 5.5), it is read and written on every target, in parts where the machine needs that; and, as such a field, a view has no address: ADR of it, a var argument of it and a slice of it are errors. Copy it out to a variable, and back in. A view of a volatile variable is an error too, since its access can’t be promised as one (section 7.6): a device’s registers are reached through a ^VOLATILE pointer to a record of them.

    +

    Byte order. The bytes are in the target’s order (appendix A). A file or a message whose format fixes an order of its own is read with Str.le, Str.be and the like (section 5.5), if the program is to read it right on every target.

    +

    Before views. A program used to make a pointer for each access, and may still: mem[a].(I64) := n does what

    +
    p := ^I64(ADR(mem[a]));
    +p^ := n;
    +

    does, but with a checked against the length of mem, no pointer variable, and no address of a word that may be misaligned.

    +

    v is evaluated once, and its check made, before the bytes are read or written.

    7. Statements

    Statements = { Statement ";" } .
     Statement  = Designator [ ":=" Expr ]
    @@ -495,7 +523,7 @@
     

    The blank, _, drops what would be assigned to it: _ := e; evaluates e and drops it, and n, _ := f(x); keeps the first result and drops the second. What is dropped is still evaluated as any expression is: its calls are made, and a volatile variable in it is read (section 7.6). A result dropped by a call standing on its own is a lint an implementation may warn of (section 10); one dropped through _ says it is meant.

    7.2 Conditions and loops

    if, while and repeat take BOOL expressions. repeat runs its body at least once and stops when the condition is true.

    -

    for i := a to b by s do ... end; sets i to a, a + s, … while i <= b (or i >= b if s is negative). i is a variable of an integer or enumeration type, named by its name alone; a and then b are evaluated once, before the first round; s is a constant other than zero, and 1 if left out. a and b have i’s type, or are untyped constants that take it, as with + (section 6.1): so in for i := 0 to LEN(s) - 1, i must be an INT.

    +

    for i := a to b by s do ... end; sets i to a, a + s, … while i <= b (or i >= b if s is negative). i is a variable of an integer or enumeration type, named by its name alone; a and then b are evaluated once, before the first round; s is a constant other than zero, and 1 if left out. a and b have i’s type, or are untyped constants that take it, or are INTs that widen to an I64 i, as with + (section 6.1): so in for i := 0 to LEN(s) - 1, i is an INT or an I64.

    The loop never steps past b, so i can’t wrap: afterwards i holds the last value it took, or a if the body never ran. The body may assign i. Precisely, for a positive s: at the end of a round, if i <= b and b - i < s, the loop ends with i as it is; otherwise i becomes i + s, wrapping, and the loop goes on while i <= b. A negative s is the same, the other way round.

    break ends the innermost while, repeat or for it stands in, and the program goes on after it. There is no goto.

    7.3 Return

    @@ -751,7 +779,7 @@ Element = Expr | ident ":" Expr . Designator = QualIdent { Selector } . Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" - | "(" [ Expr { "," Expr } ] ")" . + | "(" [ Expr { "," Expr } ] ")" | "." "(" Type ")" . Statements = { Statement ";" } . Statement = Designator [ ":=" Expr ] @@ -802,7 +830,7 @@ Sys -system calls: open, 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 others +system calls: open, openRead, create, read, write, seek, position, truncate, sync, close, mmap, munmap, isTerminal, yield, exit; files and directories, rename, remove, removeDir, makeDir, chmod, symlink, readLink, info, infoOf, 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 others Out @@ -985,6 +1013,9 @@ proc openRead*(path: []U8): INT; (* open(path, {reading}) *) proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *) +proc position*(fd: INT): I64; (* where the next read or write is *) +proc truncate*(fd: INT; size: I64): INT; (* the file made size bytes long *) +proc sync*(fd: INT): INT; (* what was written to it, on the disk *) proc close*(fd: INT); proc isTerminal*(fd: INT): BOOL; proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) @@ -1000,6 +1031,7 @@ proc symlink*(target, path: []U8): INT; (* path made a link to target *) proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) proc info*(path: []U8): (Info, INT); (* following a symbolic link *) +proc infoOf*(fd: INT): (Info, INT); (* the open file's *) proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) proc openDir*(d: ^Dir; path: []U8): INT; @@ -1057,7 +1089,7 @@ 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.

    +

    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; position says where the file is, without moving. truncate makes the file size bytes long, cutting it or adding zeros, and leaves the position where it was. sync returns once what was written to the file is on the disk, as a program needs before it says a file is saved. infoOf is info of an open file: its size is what it holds now, however it was written. 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.

    Programs. openPty makes a pseudo-terminal, a terminal that a program, a terminal emulator, plays the screen and the keyboard of: what it writes to the fd is typed, and what the programs on the terminal write it reads.

    spawn starts the program at p.path, with p.args as its arguments, the first being its name. Its environment is this program’s with each NAME=value of p.env added or put in place of the old value, or, if p.onlyEnv, p.env alone. It starts in p.dir. Its standard input, output and error are p.fds; a 0 gives it this program’s own, so {path: "/bin/ls", args: ["ls"]} runs ls as a shell runs it, with every other field zero. It gets no other open file, every signal does what it does at first, and none is blocked. If p.tty is a pseudo-terminal’s fd, the program starts a session on that terminal: it is its controlling terminal, and its standard input, output and error, and p.fds and p.group are not used.

    p.group puts the program in a process group, the programs that a shell’s job is made of: -1 makes it the first of a new group, whose number is its process id, and the other programs of the job join that number. The terminal gives its keys, and the signals of Ctrl-C and Ctrl-Z, to one group, the one in its foreground: p.foreground puts the program’s group there before it starts, and foreground puts any group there, as a shell does to take the terminal back. setGroup moves a program to a group, as the shell does for each program it starts, for whichever of the two runs first; a shell runs in a group of its own, setGroup(0, 0).

    @@ -1220,6 +1252,7 @@ proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *) proc next*(p: ^Parser): BOOL; (* the next option; false when they end *) proc operand*(p: ^Parser): ([]U8, BOOL); (* the next operand; false when there are no more *) +proc at*(p: ^Parser): INT; (* the argument operand would give next *) proc scope*(p: ^Parser; name: []U8); (* subcommand name's options, from the word after it *) proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *) proc fail*(p: ^Parser; text: []U8); (* the program's own mistake *)
    @@ -1253,7 +1286,9 @@
  • -- ends the options, and isn’t an operand; - alone is one. The options end at the first operand, so in keep start site python -m http.server the -m is python’s.
  • Each scope has its own options. An option given twice is read twice.
  • -

    number reads the value as Str.toInt does. operand reads what options are left first, and the program doesn’t see them.

    +

    number reads the value as Str.toInt does. operand reads what options are left first, and the program doesn’t see them. So does at, which says where the operands are: the arguments from operand’s next on are Files.arg(i) for i from at(p) to ARGC() - 1, so that an interpreter hands a script its arguments as they are:

    +
    script, ok := Opt.operand(ADR(p));       (* tery file arg ... *)
    +first := Opt.at(ADR(p));                 (* file's own arguments, from here on *)

    -h or --help among a scope’s options prints its help on standard output and ends the program with status 0. init and scope answer it, looking through the scope’s options before next gives one, so that the program has done nothing yet: keep -v -h -x prints the help, and the program never sees -v, nor the mistake. The help is the scope’s synopsis lines, a blank line, and its subcommands, its options and the help itself, each with its line of help:

    usage: keep start [-b label] name command [argument ...]
     
    diff --git a/site/public/tour/hello.html b/site/public/tour/hello.html
    index b8a6815..0a1dc8d 100644
    --- a/site/public/tour/hello.html
    +++ b/site/public/tour/hello.html
    @@ -70,7 +70,7 @@
     $ raz run Quiet.raz
     hello, again
     $ ls -l Quiet | awk '{print $5, "bytes"}'
    -28208 bytes
    +28400 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.

    What Out can write

    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).

    diff --git a/site/public/tour/integers.html b/site/public/tour/integers.html index 4da0fc7..d7024eb 100644 --- a/site/public/tour/integers.html +++ b/site/public/tour/integers.html @@ -70,7 +70,7 @@

    INT is the everyday one

    Lengths, indexes, counts and sizes are INTs. LEN(s) of a slice is an INT, and so is the loop variable that walks it. It is as big as an address, so it is the integer a pointer converts to, and the word the atomics work on. Reach for INT unless you have a reason: a byte is a U8, a character an I32, a file's size an I64, a hash a U64.

    -

    INT and I64 are two types even where they have the same size. An INT goes silently into an I64 (and a UINT into a U64), which is why Out.int(n) and Fmt.int(n) take an I64 and accept any INT. Nothing else converts unasked: any other integer is converted where it is written, as Out.int(I64(b)).

    +

    INT and I64 are two types even where they have the same size. An INT goes silently into an I64 (and a UINT into a U64) wherever it meets one, in pos + LEN(s) as in a call, which is why Out.int(n) and Fmt.int(n) take an I64 and accept any INT. The other way is written, INT(pos), since an INT may be smaller on another machine. Nothing else converts unasked: any other integer is converted where it is written, as Out.int(I64(b)).

    A number written in the program

    A number in the program text is untyped until it meets a type, and then it must fit. 0FFH is hexadecimal (it starts with a digit and ends in H), a character in quotes is its code point, and '\u{E9}' is a code point written by number. A hexadecimal literal too big for an I64 is a U64.

    Literals.raz
    module Literals;
    diff --git a/site/public/tour/records.html b/site/public/tour/records.html
    index b770b35..329f10a 100644
    --- a/site/public/tour/records.html
    +++ b/site/public/tour/records.html
    @@ -160,6 +160,64 @@
     [5]I16: 10
     

    A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with ADR.

    +

    Bytes as numbers and records: views

    +

    A file, a socket or a device hands a program bytes. A view reads and writes them as another type: msg[3].(U32) is the U32 whose four bytes start at msg[3], and msg[0].(Head) the Head whose bytes start at msg[0].

    +
    Wire.raz
    module Wire;
    +import Out;
    +
    +type Head = packed record magic: U16; kind: U8; len: U32; end;   (* 7 bytes, as sent *)
    +
    +var msg: [32]U8;
    +
    +proc main*;
    +var h: Head; i: INT; sum: I64;
    +begin
    +    (* what a socket might have handed us *)
    +    msg[0] := 52H; msg[1] := 5AH;
    +    msg[2] := 1;
    +    msg[3].(U32) := 8;
    +    msg[7].(I32) := 100;
    +    msg[11].(I32) := 200;
    +
    +    h := msg[0].(Head);
    +    Out.hex(U64(h.magic), 4); Out.char(' ');
    +    Out.int(I64(h.kind)); Out.char(' ');
    +    Out.int(I64(h.len)); Out.ln;
    +    sum := 0;
    +    i := 7;
    +    while i < 7 + INT(h.len) do
    +        sum := sum + I64(msg[i].(I32));
    +        i := i + 4;
    +    end;
    +    Out.int(sum); Out.ln;
    +end;
    +
    +end Wire.
    +
    5A52 1 8
    +300
    +
    +

    A view isn't a copy: it *is* those bytes, so msg[3].(U32) := 8 writes msg[3] to msg[6], and a view goes wherever a variable does, with a field after it, in INC, on the left of :=. Nor is it a conversion: I64(x) is a real's number, x.(U64) its bits. The magic number reads 5A52, its bytes backwards, because x86-64 keeps a number's low byte first; a format with an order of its own is read with Str.le or Str.be.

    +

    A view of an element must end inside the array, and the program checks it as it checks an index:

    +
    Short.raz
    module Short;
    +import Out;
    +
    +var msg: [10]U8;
    +
    +proc main*;
    +var i: INT;
    +begin
    +    i := 0;
    +    while i < LEN(msg) do
    +        Out.int(I64(msg[i].(U32))); Out.char(' ');
    +        i := i + 4;
    +    end;
    +end;
    +
    +end Short.
    +
    0 0 Short.raz:11:25: a view at index 8 runs past a length of 10
    +  in Short.main at Short.raz:11:25: i = 8
    +
    +

    The type of a view is an integer, a real or a pointer, or a record or array made of them: types any bytes are a value of, which a BOOL or an enumeration isn't. A view may start at any byte, so, as a packed record's field, it has no address of its own.

    Pointers

    ADR(x) takes the address of a variable, ^T is a pointer to a T, and p^ is what it points at. A pointer that points nowhere is nil, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.

    Pointers.raz
    module Pointers;
    diff --git a/site/public/tour/values.html b/site/public/tour/values.html
    index 9e8705f..508c2ee 100644
    --- a/site/public/tour/values.html
    +++ b/site/public/tour/values.html
    @@ -64,13 +64,13 @@
     
    300 4 300 42.857 233 true
     

    Conversions are written

    -

    Two types never mix: with i: I64 and a slice s, i < LEN(s) is an error. A type's name converts: F64(n), I64(b), U8(x). The one silent conversion between numbers is an INT into an I64, which loses nothing. The lesson on integers says what each width conversion does.

    +

    Two types never mix but for one: an INT goes into an I64 wherever it meets one, which loses nothing, so with pos: I64, pos + LEN(s) is an I64. With i: I32 and a slice s, i < LEN(s) is an error. A type's name converts: F64(n), I64(b), U8(x); an I64 becomes an INT only so, INT(pos), since an INT may be smaller on another machine. The lesson on integers says what each width conversion does.

    A number written in the program is untyped until it meets a type, and 1.5 is a real, which an integer never becomes without a conversion.

    Mix.raz
    module Mix;
     import Out;
     
     proc main*;
    -var i: I64; s: []U8;
    +var i: I32; s: []U8;
     begin
         s := "four";
         i := 3;
    @@ -79,12 +79,12 @@
     
     end Mix.
    $ raz Mix.raz
    -Mix.raz:9:8: error: I64 and INT are different types
    +Mix.raz:9:8: error: I32 and INT are different types
         9 |     if i < LEN(s) then Out.str("smaller\n"); end;
           |        ^^^^^^^^^^
    -help: convert one to the other's type, as in I64(LEN(s))
    +help: convert one to the other's type, as in INT(i)
     raz: 1 error
    -

    The fix is the conversion, written where it happens: if i < I64(LEN(s)). It costs nothing at run time and says what is meant.

    +

    The fix is the conversion, written where it happens: if INT(i) < LEN(s). It costs nothing at run time and says what is meant.

    What converts to what

    Everything else that converts does so with the type's name, and the rules are short:

    FromToResult
    integerintegercut to the new width, or extended by the sign (signed) or zeros (unsigned)
    integerF32, F64the nearest real
    F32, F64integerrounded toward zero
    F32F64the same value; the other way, the nearest F32
    enumerationintegerits position; and back, if there is a name at that position
    setintegerits bits; and back
    INT, UINTpointerthe address; and back