From 78988285a0a61729322fbf49ceb669e76917c5e9 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:47:38 +0100 Subject: [PATCH] site: v0.15.0 The tarball and its SHA-256; the report's PDFs. Collections gains Vec, and Freq keeps its entries in one; Files gains modes, links and a lock (openMode, linkInfo, Info.ino, lock, syncAll); the slices tour says a slice of a local can't be returned. --- diff --git a/site/content/advanced/03-collections.txt b/site/content/advanced/03-collections.txt index 97102c1..61d357a 100644 --- a/site/content/advanced/03-collections.txt +++ b/site/content/advanced/03-collections.txt @@ -1,8 +1,8 @@ -title: Lists, maps and sorting +title: Lists, arrays, maps and sorting part: The library -summary: List(T), Map(K, V) and Sort(T), put together in a word-frequency program. +summary: List(T), Vec(T), Map(K, V) and Sort(T), put together in a word-frequency program. --- -The three generic modules are instantiated like any other generic module, with the types and procedures they need. All three take their memory from an allocator you give them; none of them allocates anything you didn't ask for. +The four generic modules are instantiated like any other generic module, with the types and procedures they need. All four take their memory from an allocator you give them; none of them allocates anything you didn't ask for. ## `List(T)` @@ -29,6 +29,30 @@ end Pile. ``` +## `Vec(T)` + +A growable array: one block of elements that doubles when it is full, for what a program learns the length of only as it reads. `push` adds at the end, `add` adds a zero element and gives a pointer to fill it in place, `resize` cuts it short or grows it with zeros. `all` gives the elements as a slice, indexed and checked as any slice is; the slice is good until the array next grows. Running out of memory stops the program, as with `Buf`: an element dropped silently would be wrong data. `room` asks first, for a program that can do better. + +```raz Squares run +module Squares; +import Out, Heap, Ints := Vec(INT); + +proc main*; +var v: Ints.Vec; i: INT; s: []INT; +begin + Ints.init(ADR(v), Heap.heap); + defer Ints.done(ADR(v)); + for i := 1 to 1000 do Ints.push(ADR(v), i * i); end; + s := Ints.all(ADR(v)); + Out.int(v.len); Out.str(" squares, the last "); Out.int(s[LEN(s) - 1]); Out.ln; + Ints.resize(ADR(v), 3); + for i := 0 to v.len - 1 do Out.int(Ints.all(ADR(v))[i]); Out.char(' '); end; + Out.ln; +end; + +end Squares. +``` + ## `Map(K, V; hash, eq)` A hash map. Its key's hash and equality are procedure arguments, so any type can be a key. `put` replaces what a key maps to; `get` says what it maps to and whether it maps to anything; `remove` says whether the key was there. `next` walks the map: from 0, it gives the next entry and the number to pass for the one after, and -1 when there are no more. @@ -71,12 +95,13 @@ ## Putting them together -Counting words, then listing them by count and then alphabetically: a `Map` for the counts, an array of records to sort, and `Sort` with a two-level `less`. +Counting words, then listing them by count and then alphabetically: a `Map` for the counts, a `Vec` of records to sort, however many words there are, and `Sort` with a two-level `less`. ```raz Freq run module Freq; import Fmt, Str, Heap, Count := Map([]U8, INT, Str.hash, Str.eq), + Entries := Vec(Entry), Rank := Sort(Entry, before); type Entry = record word: []U8; n: INT; end; @@ -92,8 +117,8 @@ proc main*; var - m: Count.Map; entries: [32]Entry; i, k, n, found: INT; - w, rest: []U8; key: []U8; v: INT; + m: Count.Map; entries: Entries.Vec; i, k, found: INT; + w, rest: []U8; key: []U8; v: INT; all: []Entry; begin Count.init(ADR(m), Heap.heap); w, rest := Str.field(Text); @@ -102,17 +127,16 @@ _ := Count.put(ADR(m), w, k + 1); w, rest := Str.field(rest); end; - n := 0; + Entries.init(ADR(entries), Heap.heap); found, key, v := Count.next(ADR(m), 0); while found >= 0 do - entries[n].word := key; - entries[n].n := v; - INC(n); + Entries.push(ADR(entries), {word: key, n: v}); found, key, v := Count.next(ADR(m), found); end; - Rank.sort(entries[0..n]); - for i := 0 to n - 1 do - Fmt.print("{} {}\n", [Fmt.int(entries[i].n), Fmt.str(entries[i].word)]); + all := Entries.all(ADR(entries)); + Rank.sort(all); + for i := 0 to LEN(all) - 1 do + Fmt.print("{} {}\n", [Fmt.int(all[i].n), Fmt.str(all[i].word)]); end; end; @@ -123,4 +147,4 @@ ## Choosing -Use a `List` when you push and pop at one end and walk it in order. Use a `Map` when you look things up by key. Use an array and `Sort` when you know the size and want an order. When none of them fits, they are each under a hundred lines of raz in `lib/`, and the advanced tour's allocator lesson shows what is underneath. +Use a `List` when you push and pop at one end and walk it in order. Use a `Vec` when you add at the end and index, and don't know how many will come; an array when you do. Use a `Map` when you look things up by key. Use `Sort` on either's slice when you want an order. When none of them fits, they are each under a hundred lines of raz in `lib/`, and the advanced tour's allocator lesson shows what is underneath. diff --git a/site/content/advanced/04-files.txt b/site/content/advanced/04-files.txt index 00d3dd0..6c57e1d 100644 --- a/site/content/advanced/04-files.txt +++ b/site/content/advanced/04-files.txt @@ -136,6 +136,44 @@ end Tree. ``` +## Modes, links, and a lock + +`Sys.openMode` is `open` with the permissions a file it makes should have, less those the umask takes away: 1EDH (755) for a script, 124H (444) for a file never to be written. `Sys.linkInfo` looks at a symbolic link itself, where `info` follows it, so a link whose target is gone is still seen. `Info.ino` is the file's inode: a file renamed over another has a new one, though its size and time may match, which is how a program knows a file it cached is still the same. + +`Sys.lock` takes the lock on an open file, which one open file holds at a time; another that asks for it waits, or with `wait` false gets -11 at once. The lock goes when the file is closed or the program ends, however it ends, so it can't go stale as a lock file can. `Sys.syncAll` puts everything written to a file system on the disk at once, before the one file that names it all is written: a database's or a version control system's commit. + +```raz Store run +module Store; +import Fmt, Sys; + +proc main*; +var fd, other: INT; named, target: Sys.Info; +begin + fd := Sys.openMode("run.sh", {Sys.writing, Sys.creating, Sys.truncating}, 1EDH); + _ := Sys.write(fd, "echo hi\n"); + _ := Sys.syncAll(fd); + Sys.close(fd); + target, _ := Sys.info("run.sh"); + Fmt.print("its owner may run it: {}\n", [Fmt.bool(target.mode & 40H # 0)]); + + _ := Sys.remove("current"); + _ := Sys.symlink("run.sh", "current"); + named, _ := Sys.linkInfo("current"); + Fmt.print("a link: {}, of {} bytes\n", [Fmt.bool(named.symlink), Fmt.int(named.size)]); + named, _ := Sys.info("current"); + Fmt.print("the same file: {}\n", [Fmt.bool(named.ino = target.ino)]); + + fd := Sys.openRead("run.sh"); + other := Sys.openRead("run.sh"); + Fmt.print("{} {}\n", [Fmt.int(Sys.lock(fd, false)), Fmt.int(Sys.lock(other, false))]); (* -11: held *) + Sys.close(fd); + Fmt.print("{}\n", [Fmt.int(Sys.lock(other, false))]); (* free again *) + Sys.close(other); +end; + +end Store. +``` + ## A command line, from a table A program describes its options once, in a table of `Opt.Row`s: its synopsis lines, its subcommands and its options. The help and the error messages are made from the table. The parser reads by the rules of the C library's `getopt` with GNU's long options, so `-vn3`, `-n 3`, `--count=3` and `--count 3` all work, and `--` ends the options. diff --git a/site/content/tour/06-slices.txt b/site/content/tour/06-slices.txt index 47a6103..567b38d 100644 --- a/site/content/tour/06-slices.txt +++ b/site/content/tour/06-slices.txt @@ -68,6 +68,8 @@ Every index and every slice bound is checked, so a slice can never reach past what it was made from; the lesson on failures shows what that looks like. +A slice of a local array is good only while its procedure runs: the array is gone once it returns. So returning one is an error, `return a[0..n]` or `return a` with `a` a local, a value parameter or an array literal. Take the array as a `var` parameter instead, and the slice is of the caller's array; or fill a buffer the caller gives. + ## `COPY` `COPY(d, s)` copies the elements of `s` to the start of `d`, as one block, and is right when they overlap. `LEN(s)` must be at most `LEN(d)`. diff --git a/site/public/_redirects b/site/public/_redirects index 19bf6b2..c44455a 100644 --- a/site/public/_redirects +++ b/site/public/_redirects @@ -1 +1 @@ -/download/latest /download/raz-0.14.0-linux-x86_64.tar.gz 302 +/download/latest /download/raz-0.15.0-linux-x86_64.tar.gz 302 diff --git a/site/public/advanced/allocators.html b/site/public/advanced/allocators.html index e3b46a7..af001aa 100644 --- a/site/public/advanced/allocators.html +++ b/site/public/advanced/allocators.html @@ -21,7 +21,7 @@
raz is under the ISC license.
diff --git a/site/public/advanced/files.html b/site/public/advanced/files.html index 9ef66e2..b71e550 100644 --- a/site/public/advanced/files.html +++ b/site/public/advanced/files.html @@ -21,7 +21,7 @@
+

A mistake in the table itself, such as an option given twice, is the program's, and init says so and ends with status 1 before it reads a word.

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

← Lists, arrays, maps and sortingContents
diff --git a/site/public/advanced/fmt.html b/site/public/advanced/fmt.html index d16efea..32b116e 100644 --- a/site/public/advanced/fmt.html +++ b/site/public/advanced/fmt.html @@ -21,7 +21,7 @@