raz / commit 56567d3c

commit
56567d3cea35acca8a5ceac12509c35cba1c1df9
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
209dbf3c
site: the website, with the tour Raz, Set, Go

Plain files in site/public/, made by make site from site/content/:
a home page, fourteen lessons whose programs come from the tutorial
and are run when the pages are built, and the report as one page.

patch · log

40 files changed, +4206 -1

Makefile (history) modified +7 -1

@@ -119,4 +119,10 @@
119119 rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(MAN)/man1/raz.1 $(MAN)/man3/raz-lib.3 \
120120 $(MAN)/man7/raz.7 $(MAN)/man7/raz-tutorial.7
121121
122-.PHONY: units tests check gdb bench kernels stress fmt loc man pdf clean install uninstall
122+# The website, plain files in site/public/: its home page, the tour
123+# "Raz, Set, Go" (whose programs are run here, so the output shown is
124+# real) and the report. site/build.py says how; it needs ./raz and pandoc.
125+site: raz
126+ python3 site/build.py
127+
128+.PHONY: units tests check gdb bench kernels stress fmt loc man pdf site clean install uninstall

README.md (history) modified +2 -0

@@ -41,6 +41,7 @@
4141 | `examples/` | Small programs, each with the output it should give |
4242 | `tools/Doc.raz` | Prints what a module exports, with its comments |
4343 | `tools/Pretty.raz` | The formatter: fixes indentation and spacing |
44+| `site/` | The website: home page, the tour "Raz, Set, Go" and the report, as plain files in `site/public/`; `make site` rebuilds them |
4445 | `editor/` | Support for vim and emacs |
4546 | `conform/` | The conformance suite: what every implementation must do |
4647 | `test/` | This compiler's own tests, and the scripts that run them |
@@ -70,6 +71,7 @@
7071 make loc # the compiler's lines
7172 make tests # tests of the scanner, encoder and linker on their own
7273 make pdf # build/raz.pdf, the report, by pandoc and Chromium
74+ make site # site/public/, the website, by pandoc and python3
7375
7476 ## Using it
7577

site/build.py added +291 -0

@@ -0,0 +1,291 @@
1+#!/usr/bin/env python3
2+"""Builds the website into site/public/ from site/content/ and the repository.
3+
4+ python3 site/build.py
5+
6+The site is plain files: open public/index.html, or put the folder on
7+any web server. Nothing runs in the reader's browser; the code on the
8+tour's pages is run here, by ./raz, and the output shown is what it
9+printed, so a lesson that stops working fails the build.
10+
11+Lessons are site/content/NN-name.txt:
12+
13+ title: Hello, world (the first lines are "key: value")
14+ part: Raz (the lessons are grouped in parts)
15+ summary: a line for the contents page
16+ ---
17+ Paragraphs, separated by blank lines. `code`, **bold**,
18+ [links](url) and "- " lists are understood, and nothing else.
19+
20+ ```raz Name run
21+ ```
22+
23+A fenced block "raz Name" is a module, written as Name.raz in a folder
24+of its own for the lesson. "run" has the build run it and show what it
25+printed; "fail" runs it and shows the failure it ends in. A block with
26+nothing in it is taken from doc/raz-tutorial.7, whose programs make
27+check already runs, by the module's name.
28+"""
29+import html, os, re, shutil, subprocess, sys, tempfile
30+
31+HERE = os.path.dirname(os.path.abspath(__file__))
32+ROOT = os.path.dirname(HERE)
33+OUT = os.path.join(HERE, "public")
34+RAZ = os.path.join(ROOT, "raz")
35+REPO = "https://github.com/ldiaz94/raz"
36+
37+KEYWORDS = set("""and begin break by code const defer div do else elsif end
38+extern for forward if import in link match mod module not of or packed proc
39+record repeat return set shl shr then to type until var variant VOLATILE
40+while xor""".split())
41+PREDECLARED = set("""INT UINT I8 I16 I32 I64 U8 U16 U32 U64 F32 F64 BOOL ERROR
42+true false nil LEN ADR NEW SIZE INC DEC MSG XADD CAS XCHG""".split())
43+
44+# ---- highlighting ------------------------------------------------------
45+
46+TOKEN = re.compile(r"""
47+ (?P<comment>\(\*.*?\*\)) |
48+ (?P<string>"(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*') |
49+ (?P<number>\b\d[0-9A-Fa-f]*(?:\.\d+)?(?:E[-+]?\d+)?H?\b) |
50+ (?P<word>[A-Za-z_][A-Za-z_0-9]*)
51+""", re.X | re.S)
52+
53+def highlight(src):
54+ """src as HTML, its comments, strings, numbers and words in spans."""
55+ out, at = [], 0
56+ for m in TOKEN.finditer(src):
57+ out.append(html.escape(src[at:m.start()]))
58+ text, kind = m.group(), m.lastgroup
59+ if kind == "word":
60+ kind = ("kw" if text in KEYWORDS else
61+ "ty" if text in PREDECLARED else None)
62+ if kind:
63+ out.append('<span class="%s">%s</span>' % (kind, html.escape(text)))
64+ else:
65+ out.append(html.escape(text))
66+ at = m.end()
67+ out.append(html.escape(src[at:]))
68+ return "".join(out)
69+
70+# ---- the tutorial's programs -------------------------------------------
71+
72+def tutorial_blocks():
73+ """Every literal block of the manual page, as raz source."""
74+ blocks, cur = [], None
75+ for line in open(os.path.join(ROOT, "doc", "raz-tutorial.7"), encoding="utf-8"):
76+ line = line.rstrip("\n")
77+ if line.startswith(".Bd -literal"):
78+ cur = []
79+ elif line == ".Ed" and cur is not None:
80+ blocks.append("\n".join(cur) + "\n")
81+ cur = None
82+ elif cur is not None:
83+ # roff writes a backslash as \e and a dot at a line's start as \&.
84+ cur.append(line.replace("\\&", "").replace("\\e", "\\"))
85+ return blocks
86+
87+def tutorial_module(name):
88+ for b in tutorial_blocks():
89+ if re.search(r"^module %s\b" % re.escape(name), b, re.M):
90+ return b
91+ sys.exit("no program %s in the tutorial" % name)
92+
93+# ---- running -----------------------------------------------------------
94+
95+def run(directory, name):
96+ """(stdout, stderr, status) of the module name, compiled and run."""
97+ p = subprocess.run([RAZ, "run", name + ".raz"], cwd=directory,
98+ capture_output=True, text=True, timeout=60)
99+ return p.stdout, p.stderr, p.returncode
100+
101+def output_html(stdout, stderr):
102+ out = html.escape(stdout)
103+ if stderr:
104+ out += '<span class="err">%s</span>' % html.escape(stderr)
105+ return '<pre class="output"><code>%s</code></pre>' % out
106+
107+# ---- a little markup -----------------------------------------------------
108+
109+def inline(s):
110+ s = html.escape(s, quote=False)
111+ s = re.sub(r"`([^`]+)`", r"<code>\1</code>", s)
112+ s = re.sub(r"\*\*([^*]+)\*\*", r"<strong>\1</strong>", s)
113+ s = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", r'<a href="\2">\1</a>', s)
114+ return s
115+
116+def prose(text):
117+ out = []
118+ for para in re.split(r"\n\s*\n", text.strip()):
119+ lines = para.split("\n")
120+ if all(l.startswith("- ") for l in lines):
121+ out.append("<ul>%s</ul>" % "".join(
122+ "<li>%s</li>" % inline(l[2:]) for l in lines))
123+ else:
124+ out.append("<p>%s</p>" % inline(" ".join(lines)))
125+ return "\n".join(out)
126+
127+# ---- lessons -------------------------------------------------------------
128+
129+def read_lesson(path):
130+ head, _, body = open(path, encoding="utf-8").read().partition("\n---\n")
131+ meta = dict(l.split(": ", 1) for l in head.strip().split("\n"))
132+ meta["slug"] = re.sub(r"^\d+-|\.txt$", "", os.path.basename(path))
133+ meta["body"] = body
134+ return meta
135+
136+def lesson_body(lesson):
137+ """The lesson's page content; runs its programs."""
138+ parts, tmp = [], tempfile.mkdtemp()
139+ try:
140+ chunks = re.split(r"^```raz ([^\n]*)\n(.*?)^```$", lesson["body"],
141+ flags=re.M | re.S)
142+ # chunks: text, info, code, text, info, code, ..., text
143+ for i in range(0, len(chunks), 3):
144+ if chunks[i].strip():
145+ parts.append(prose(chunks[i]))
146+ if i + 1 >= len(chunks):
147+ break
148+ info, code = chunks[i + 1].split(), chunks[i + 2]
149+ name, flags = info[0], info[1:]
150+ if not code.strip():
151+ code = tutorial_module(name)
152+ with open(os.path.join(tmp, name + ".raz"), "w", encoding="utf-8") as f:
153+ f.write(code)
154+ parts.append('<div class="file"><span>%s.raz</span></div>'
155+ '<pre class="code"><code>%s</code></pre>'
156+ % (name, highlight(code.rstrip("\n"))))
157+ if "run" in flags or "fail" in flags:
158+ stdout, stderr, status = run(tmp, name)
159+ if ("fail" in flags) == (status == 0):
160+ sys.exit("%s: %s ended with status %d\n%s%s" % (
161+ lesson["slug"], name, status, stdout, stderr))
162+ parts.append(output_html(stdout, stderr))
163+ finally:
164+ shutil.rmtree(tmp)
165+ return "\n".join(parts)
166+
167+# ---- pages ---------------------------------------------------------------
168+
169+def page(title, body, depth=0, current=""):
170+ up = "../" * depth
171+ nav = "".join('<a href="%s%s"%s>%s</a>' % (
172+ up, href, ' aria-current="page"' if key == current else "", label)
173+ for key, href, label in [
174+ ("home", "index.html", "raz"),
175+ ("tour", "tour/index.html", "Raz, Set, Go"),
176+ ("spec", "spec.html", "Report"),
177+ ("get", "index.html#download", "Download")])
178+ return """<!doctype html>
179+<html lang="en">
180+<head>
181+<meta charset="utf-8">
182+<meta name="viewport" content="width=device-width, initial-scale=1">
183+<title>%s</title>
184+<link rel="stylesheet" href="%sstyle.css">
185+</head>
186+<body>
187+<header><nav>%s<a class="repo" href="%s">GitHub</a></nav></header>
188+<main>
189+%s
190+</main>
191+<footer>raz is under the ISC license. <a href="%s">Source</a>.</footer>
192+</body>
193+</html>
194+""" % (html.escape(title), up, nav, REPO, body, REPO)
195+
196+def write(path, text):
197+ path = os.path.join(OUT, path)
198+ os.makedirs(os.path.dirname(path), exist_ok=True)
199+ with open(path, "w", encoding="utf-8") as f:
200+ f.write(text)
201+
202+def build_tour(lessons):
203+ parts = []
204+ for l in lessons:
205+ if l["part"] not in parts:
206+ parts.append(l["part"])
207+ for n, l in enumerate(lessons):
208+ toc = []
209+ for p in parts:
210+ toc.append("<h3>%s</h3><ol start='%d'>" % (
211+ p, 1 + next(i for i, x in enumerate(lessons) if x["part"] == p)))
212+ for i, x in enumerate(lessons):
213+ if x["part"] == p:
214+ cur = ' aria-current="page"' if i == n else ""
215+ toc.append('<li><a href="%s.html"%s>%s</a></li>' % (
216+ x["slug"], cur, html.escape(x["title"])))
217+ toc.append("</ol>")
218+ pager = '<a href="index.html">Contents</a>'
219+ if n > 0:
220+ pager = '<a href="%s.html">&larr; %s</a>' % (
221+ lessons[n - 1]["slug"], html.escape(lessons[n - 1]["title"])) + pager
222+ if n + 1 < len(lessons):
223+ pager += '<a class="next" href="%s.html">%s &rarr;</a>' % (
224+ lessons[n + 1]["slug"], html.escape(lessons[n + 1]["title"]))
225+ else:
226+ pager += '<a class="next" href="../spec.html">The report &rarr;</a>'
227+ body = ('<div class="tour"><aside>%s</aside><article>'
228+ '<p class="kicker">%s &middot; lesson %d of %d</p><h1>%s</h1>\n%s'
229+ '<p class="tryit">Save a program under its module&rsquo;s name and run '
230+ 'it with <code>raz run Name.raz</code>.</p>'
231+ '<div class="pager">%s</div></article></div>') % (
232+ "\n".join(toc), l["part"], n + 1, len(lessons),
233+ html.escape(l["title"]), lesson_body(l), pager)
234+ write("tour/%s.html" % l["slug"], page(
235+ "%s - Raz, Set, Go" % l["title"], body, 1, "tour"))
236+ cards = []
237+ for p in parts:
238+ cards.append("<h2>%s</h2><ol start='%d'>" % (
239+ p, 1 + next(i for i, x in enumerate(lessons) if x["part"] == p)))
240+ for x in lessons:
241+ if x["part"] == p:
242+ cards.append('<li><a href="%s.html">%s</a> <span>%s</span></li>' % (
243+ x["slug"], html.escape(x["title"]), html.escape(x["summary"])))
244+ cards.append("</ol>")
245+ intro = open(os.path.join(HERE, "content", "tour-intro.html"),
246+ encoding="utf-8").read()
247+ write("tour/index.html", page("Raz, Set, Go: a tour of raz",
248+ '<div class="contents">%s%s</div>' % (intro, "\n".join(cards)), 1, "tour"))
249+
250+def build_spec():
251+ if not shutil.which("pandoc"):
252+ sys.exit("pandoc makes the report's page: apt-get install pandoc")
253+ p = subprocess.run(
254+ ["pandoc", os.path.join(ROOT, "doc", "raz.md"), "-s", "--template",
255+ os.path.join(HERE, "content", "spec.tmpl"), "--toc", "--toc-depth=1",
256+ "--shift-heading-level-by=-1", "--no-highlight", "-t", "html"],
257+ capture_output=True, text=True, check=True)
258+ body = '<div class="spec">%s</div>' % p.stdout
259+ write("spec.html", page("The raz report", body, 0, "spec"))
260+
261+def build_home():
262+ snippet = open(os.path.join(HERE, "content", "home.raz"), encoding="utf-8").read()
263+ tmp = tempfile.mkdtemp()
264+ try:
265+ with open(os.path.join(tmp, "Taste.raz"), "w", encoding="utf-8") as f:
266+ f.write(snippet)
267+ stdout, stderr, status = run(tmp, "Taste")
268+ if status != 0:
269+ sys.exit("Taste failed:\n" + stderr)
270+ finally:
271+ shutil.rmtree(tmp)
272+ text = open(os.path.join(HERE, "content", "home.html"), encoding="utf-8").read()
273+ text = text.replace("@SNIPPET@", highlight(snippet.rstrip("\n")))
274+ text = text.replace("@OUTPUT@", html.escape(stdout.rstrip("\n")))
275+ write("index.html", page("raz: a small systems language", text, 0, "home"))
276+
277+def main():
278+ if not os.path.exists(RAZ):
279+ sys.exit("make raz first")
280+ shutil.rmtree(OUT, ignore_errors=True)
281+ os.makedirs(OUT)
282+ shutil.copy(os.path.join(HERE, "style.css"), OUT)
283+ names = sorted(f for f in os.listdir(os.path.join(HERE, "content"))
284+ if re.match(r"\d+-.*\.txt$", f))
285+ lessons = [read_lesson(os.path.join(HERE, "content", f)) for f in names]
286+ build_tour(lessons)
287+ build_spec()
288+ build_home()
289+ print("site/public: %d lessons" % len(lessons))
290+
291+main()

site/content/01-hello.txt added +14 -0

@@ -0,0 +1,14 @@
1+title: Hello, world
2+part: Raz
3+summary: A module, an import and a procedure.
4+---
5+Every raz program is a module with a `main` procedure. This is the smallest one that does something.
6+
7+```raz Hello run
8+```
9+
10+A module lives in a file of its own name, `Hello.raz`, and its name comes again after its last `end`. `import Out` lets it use what the module `Out` exports, as `Out.str`.
11+
12+The `*` after `main` exports it; the program starts at the `main` of the module named on the command line. Every statement ends in `;`, even one that ends in `end`.
13+
14+Compile it and run it with `raz Hello.raz && ./Hello`, or with `raz run Hello.raz`, which writes no file.

site/content/02-modules.txt added +17 -0

@@ -0,0 +1,17 @@
1+title: Modules
2+part: Raz
3+summary: Exports, renaming, and a body that runs first.
4+---
5+A program is a set of modules. Here is one, saved as `Greet.raz`:
6+
7+```raz Greet
8+```
9+
10+and a program beside it that uses it:
11+
12+```raz Twice run
13+```
14+
15+A name with `*` is exported; any other is the module's own. `G := Greet` imports `Greet` under a shorter name. Another module may read `G.count`, but only `Greet` may change it.
16+
17+The statements after a module's last `begin` are its body. It runs once, before `main`, which is why "Greet is ready" comes first.

site/content/03-values.txt added +14 -0

@@ -0,0 +1,14 @@
1+title: Values and conversions
2+part: Raz
3+summary: Sized integers that wrap, and nothing converts unasked.
4+---
5+```raz Values run
6+```
7+
8+The integers are `I8`, `I16`, `I32`, `I64` and `U8`, `U16`, `U32`, `U64`, and `INT` and `UINT`, as big as an address. `INT` is the everyday one: lengths, indexes and counts are `INT`s. The reals are `F32` and `F64`.
9+
10+Arithmetic wraps at the type's width and never stops the program, except dividing by zero. That is why `250 + 10` in a `U8` is 4.
11+
12+Two types never mix: with `i: I64`, `i < LEN(s)` is an error. A type's name converts, as `F64(n)` and `I64(b)` do above. The one silent conversion between numbers is an `INT` into an `I64`, which loses nothing.
13+
14+`#`, not `<>`, is "different". `and`, `or` and `not` are for `BOOL`s; `&`, `|`, `xor` and `~` are for the bits of integers. Every variable starts at zero.

site/content/04-procedures.txt added +10 -0

@@ -0,0 +1,10 @@
1+title: Procedures and several results
2+part: Raz
3+summary: Return two values, change a variable, pass a procedure.
4+---
5+```raz Procs run
6+```
7+
8+A parameter is a copy, unless it is `var`, when it is the caller's variable itself. A procedure may return several results, which a multiple assignment takes apart; the blank, `_`, drops one.
9+
10+A procedure is a value too, of a procedure type, as `f` is in `twice`. A procedure without parameters leaves out the parentheses, as `Out.ln` does. Procedures don't nest, and a name is declared before it is used.

site/content/05-slices.txt added +12 -0

@@ -0,0 +1,12 @@
1+title: Arrays, slices and strings
2+part: Set
3+summary: Five in a row, a window onto them, and bytes in UTF-8.
4+---
5+```raz Slices run
6+```
7+
8+`[5]INT` is an array: five `INT`s in a row, copied when it is assigned. `[]INT` is a slice: a pointer to elements that live elsewhere, and their number, `LEN(s)`. `a[lo..hi]` is the slice of the elements from `lo` up to, but not including, `hi`, and an array goes silently where a slice is wanted.
9+
10+A string is a slice of bytes, `[]U8`, in UTF-8, and its bytes are constant; a `[8]U8` made from one is a copy. Arrays and slices of integers compare with `=` and `#`; `Str` has the rest: ordering, finding, hashing and reading a number.
11+
12+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.

site/content/06-records.txt added +10 -0

@@ -0,0 +1,10 @@
1+title: Records and pointers
2+part: Set
3+summary: Laid out as C lays out a struct, reached through ^.
4+---
5+```raz Boxes run
6+```
7+
8+A record is laid out as C lays out a struct. `{lo: ..., hi: ...}` builds one, naming its fields; the ones it leaves out are zero.
9+
10+`^Box` is a pointer to a `Box`, `ADR(b)` the address of `b`, and `b^` what a pointer points at. A pointer is never the record: it is `b^.lo`, never `b.lo`.

site/content/07-variants.txt added +10 -0

@@ -0,0 +1,10 @@
1+title: Enumerations, sets, variants and match
2+part: Set
3+summary: Choices the compiler can check you handled.
4+---
5+```raz Shapes run
6+```
7+
8+An enumeration is a list of names, in order; a `set of` it holds any choice of them, in the bits of a word, with `in`, `+` for union, `*` for intersection and `-` for difference.
9+
10+A variant holds one of its cases, each with fields of its own, and `match` is the only way to read them: it runs the arm of the case the variant holds, naming its fields. Every case has an arm, unless an `| else:` comes last. On an integer, `match` takes constants and ranges, and must have an `| else:`.

site/content/08-memory.txt added +12 -0

@@ -0,0 +1,12 @@
1+title: Memory and allocators
2+part: Set
3+summary: No garbage collector; you choose where memory comes from.
4+---
5+```raz Stack run
6+```
7+
8+raz has no garbage collector, and no allocator of its own: memory comes from an allocator, a `Mem.Allocator`, which the program chooses and passes.
9+
10+`NEW(n, a)` sets `n` to a fresh, zeroed `Node` from `a`, or to `nil` if `a` has no room. `Heap.heap` is the general one, which `Mem.free` gives memory back to. An `Arena` hands out memory from one block and frees it all at once, as the last lines do.
11+
12+`Buf`, `List` and `Map` take the allocator they use from `init`.

site/content/09-errors.txt added +10 -0

@@ -0,0 +1,10 @@
1+title: Errors
2+part: Go
3+summary: A second result, compared by identity. No exceptions.
4+---
5+```raz Errors run
6+```
7+
8+A procedure that can fail returns an `ERROR` beside its result, and `nil` when it didn't. Each `ERROR` constant is an error of its own, compared with `=` by which declaration made it, never by its message; `MSG(err)` is the message.
9+
10+An exported one, as `Empty` is here, lets the callers tell it from the others. raz has no exceptions.

site/content/10-defer.txt added +10 -0

@@ -0,0 +1,10 @@
1+title: Defer
2+part: Go
3+summary: Clean up beside the line that needs it.
4+---
5+```raz Defers run
6+```
7+
8+`defer s;` runs `s` when the program leaves the statements the `defer` stands in: at their end, or by a `return` or a `break`. Deferred statements run the last first, and only those whose `defer` was reached.
9+
10+So a file is closed beside the line that opens it, however the procedure ends.

site/content/11-checks.txt added +12 -0

@@ -0,0 +1,12 @@
1+title: When it goes wrong
2+part: Go
3+summary: Every index is checked, and a failure tells you everything.
4+---
5+```raz Fail fail
6+```
7+
8+Every index and slice is checked. A failed check says where it failed and with what values, then the calls under way with their variables, and ends the program with status 2. So do a `nil` pointer, a division by zero and a stack overflow.
9+
10+A module that is sure of its indexes may say `check off;` after its imports, for speed; an index out of range there reads or writes whatever it reaches.
11+
12+The program carries line numbers for gdb, and `RAZ_PROFILE=1` in its environment has it say where its time went.

site/content/12-generics.txt added +17 -0

@@ -0,0 +1,17 @@
1+title: Generic modules
2+part: Go
3+summary: A module that takes types and procedures as arguments.
4+---
5+A module may take types, and the procedures it needs on them, as parameters. `Biggest.raz`:
6+
7+```raz Biggest
8+```
9+
10+and a program that uses it:
11+
12+```raz Generics run
13+```
14+
15+Each import with its own arguments makes an instance: the module compiled again, with `T` standing for the type given, and `less` calling the procedure given. The arguments may name what the importing module declares later, as `before` is here.
16+
17+The library's `List`, `Map` and `Sort` are generic modules: `import Count := Map([]U8, INT, Str.hash, Str.eq);`

site/content/13-c.txt added +10 -0

@@ -0,0 +1,10 @@
1+title: Talking to C
2+part: Go
3+summary: Call a C library, and be called back.
4+---
5+```raz Qsort run
6+```
7+
8+An `extern` procedure is found by its name in the C library, or in a library a `link` line names. Its parameters are integers, reals, pointers and procedures, as C declares them; a raz procedure of such types can be given to C to call back, as `order` is.
9+
10+A program that uses C is linked against it dynamically and starts through it; one that doesn't is a static executable with nothing in it but its own code.

site/content/14-threads.txt added +12 -0

@@ -0,0 +1,12 @@
1+title: Threads
2+part: Go
3+summary: Native threads, atomics and a mutex.
4+---
5+```raz Hits run
6+```
7+
8+`Thread.spawn` runs a procedure in a thread of its own, with a stack of its own, and `Thread.join` waits for it to end. `XADD`, `CAS` and `XCHG` change a word atomically; a `Thread.Mutex` keeps a longer section to one thread at a time. `Out` and `Heap` lock themselves.
9+
10+`Thread` makes its own threads, without C, so a program that uses it can't use C; one that does makes its threads with C's `pthread_create` instead.
11+
12+That is the tour. The rest is in [the report](../spec.html), and the library's interfaces are printed by `tools/Doc.raz`.

site/content/home.html (history) added +72 -0

@@ -0,0 +1,72 @@
1+<section class="hero">
2+ <h1>raz</h1>
3+ <p class="tag">A small systems language in Wirth&rsquo;s tradition, for x86-64 Linux.
4+ <em>Raz</em> is Polish for <em>one</em>, as in counting.</p>
5+ <div class="cta">
6+ <a class="btn primary" href="tour/index.html">Take the tour: Raz, Set, Go</a>
7+ <a class="btn" href="#download">Download</a>
8+ <a class="btn" href="spec.html">Read the report</a>
9+ </div>
10+</section>
11+
12+<p>A raz program can lay a record over a device&rsquo;s registers, read and
13+write memory exactly as written, take part in lock-free algorithms, make
14+system calls and call C libraries. It has no garbage collector and no hidden
15+run time: what the program does is what its text says.</p>
16+
17+<p>The compiler is one Free Pascal program of about 7,800 lines. It makes
18+x86-64 machine code and the ELF file itself, with no assembler or linker, in
19+one pass, at about a million lines a second.</p>
20+
21+<div class="facts">
22+ <span><b>~7,800</b> lines of compiler</span>
23+ <span><b>~1M</b> lines compiled a second</span>
24+ <span><b>static</b> executables, no run time to install</span>
25+ <span><b>1</b> report, <b>~2,000</b> lines</span>
26+</div>
27+
28+<div class="cols">
29+ <div>
30+ <h2>A taste</h2>
31+ <div class="file"><span>Taste.raz</span></div>
32+<pre class="code"><code>@SNIPPET@</code></pre>
33+<pre class="output"><code>@OUTPUT@</code></pre>
34+ </div>
35+ <div>
36+ <h2>What it has</h2>
37+ <ul class="features">
38+ <li>Integers of 8 to 64 bits, signed and unsigned, which wrap; <code>BOOL</code>, <code>F32</code>, <code>F64</code>. Nothing converts unless it is written.</li>
39+ <li>Arrays, checked slices, pointers, records laid out as C lays them out, packed records, enumerations, sets, and variants with <code>match</code>.</li>
40+ <li>Every index and slice is checked; a failure says where it was, with the values, and the calls under way.</li>
41+ <li>Every variable starts at zero. Behaviour is defined everywhere but through pointers.</li>
42+ <li>No garbage collector: memory comes from an allocator the program chooses and passes.</li>
43+ <li>Several results, <code>ERROR</code> values and <code>defer</code>, in place of exceptions.</li>
44+ <li>Modules with bodies, and generic modules: <code>Map(K, V; hash, eq)</code>.</li>
45+ <li><code>^VOLATILE T</code> and the atomics <code>CAS</code>, <code>XCHG</code>, <code>XADD</code>, <code>FENCE</code>, for devices and lock-free code.</li>
46+ <li><code>extern</code> procedures and variables from C, code procedures in assembly, and <code>SYSCALL</code>.</li>
47+ <li>Threads without C, a library of <code>Out</code>, <code>Str</code>, <code>Fmt</code>, <code>Map</code>, <code>Sort</code> and more, and gdb that knows your lines.</li>
48+ <li>A formatter, a documentation tool, and editor support for vim and emacs.</li>
49+ </ul>
50+ </div>
51+</div>
52+
53+<h2>The language, in writing</h2>
54+<p>The language is defined by <a href="spec.html">the report</a>, which is
55+short on purpose: whatever it doesn&rsquo;t allow, raz doesn&rsquo;t have. There
56+is also a <a href="https://github.com/ldiaz94/raz/blob/main/doc/compiler.md">guide to reading the
57+compiler</a>, file by file.</p>
58+
59+<h2 id="download">Download</h2>
60+<p>Each release is a tarball with the compiler, <code>raz</code>, and its
61+library, <code>lib/</code>, which it finds beside itself.</p>
62+<div class="cta">
63+ <a class="btn primary" href="https://github.com/ldiaz94/raz/releases/latest">Latest release</a>
64+ <a class="btn" href="https://github.com/ldiaz94/raz">Source on GitHub</a>
65+</div>
66+<pre class="code"><code>$ tar xzf raz-*.tar.gz
67+$ ./raz run examples/Hello.raz
68+hello, world</code></pre>
69+<p>To build it yourself you need Free Pascal 3.2.2:</p>
70+<pre class="code"><code>$ apt-get install fp-compiler
71+$ git clone https://github.com/ldiaz94/raz
72+$ cd raz &amp;&amp; make &amp;&amp; make install # ~/.local, or PREFIX=/usr/local</code></pre>

site/content/home.raz (history) added +26 -0

@@ -0,0 +1,26 @@
1+(* Several results, slices, and checks that say what failed. *)
2+module Taste;
3+import Out;
4+
5+proc divmod(a, b: INT): (INT, INT);
6+begin
7+ return a div b, a mod b;
8+end;
9+
10+proc sum(s: []INT): INT;
11+var i, t: INT;
12+begin
13+ for i := 0 to LEN(s) - 1 do t := t + s[i]; end;
14+ return t;
15+end;
16+
17+proc main*;
18+var q, r: INT; a: [5]INT; i: INT;
19+begin
20+ for i := 0 to 4 do a[i] := i * i; end;
21+ q, r := divmod(sum(a), 7);
22+ Out.int(q); Out.str(" remainder "); Out.int(r); Out.ln;
23+ Out.int(sum(a[1..3])); Out.ln;
24+end;
25+
26+end Taste.

site/content/spec.tmpl (history) added +2 -0

@@ -0,0 +1,2 @@
1+$if(toc)$$toc$$endif$
2+$body$

site/content/tour-intro.html (history) added +9 -0

@@ -0,0 +1,9 @@
1+<h1>Raz, Set, Go</h1>
2+<p>A tour of raz, in the spirit of <em>A Tour of Go</em>. Each lesson is one
3+idea and a whole program, and the output shown is what the program printed
4+when this page was made.</p>
5+<p>It is in three parts, as one counts off: <strong>Raz</strong> (one), the
6+first steps; <strong>Set</strong>, how data is laid out; and
7+<strong>Go</strong>, making programs that run, fail and finish well. To follow
8+along, <a href="../index.html#download">get raz</a> and run any program with
9+<code>raz run Name.raz</code>.</p>

site/public/index.html (history) added +114 -0

@@ -0,0 +1,114 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>raz: a small systems language</title>
7+<link rel="stylesheet" href="style.css">
8+</head>
9+<body>
10+<header><nav><a href="index.html" aria-current="page">raz</a><a href="tour/index.html">Raz, Set, Go</a><a href="spec.html">Report</a><a href="index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<section class="hero">
13+ <h1>raz</h1>
14+ <p class="tag">A small systems language in Wirth&rsquo;s tradition, for x86-64 Linux.
15+ <em>Raz</em> is Polish for <em>one</em>, as in counting.</p>
16+ <div class="cta">
17+ <a class="btn primary" href="tour/index.html">Take the tour: Raz, Set, Go</a>
18+ <a class="btn" href="#download">Download</a>
19+ <a class="btn" href="spec.html">Read the report</a>
20+ </div>
21+</section>
22+
23+<p>A raz program can lay a record over a device&rsquo;s registers, read and
24+write memory exactly as written, take part in lock-free algorithms, make
25+system calls and call C libraries. It has no garbage collector and no hidden
26+run time: what the program does is what its text says.</p>
27+
28+<p>The compiler is one Free Pascal program of about 7,800 lines. It makes
29+x86-64 machine code and the ELF file itself, with no assembler or linker, in
30+one pass, at about a million lines a second.</p>
31+
32+<div class="facts">
33+ <span><b>~7,800</b> lines of compiler</span>
34+ <span><b>~1M</b> lines compiled a second</span>
35+ <span><b>static</b> executables, no run time to install</span>
36+ <span><b>1</b> report, <b>~2,000</b> lines</span>
37+</div>
38+
39+<div class="cols">
40+ <div>
41+ <h2>A taste</h2>
42+ <div class="file"><span>Taste.raz</span></div>
43+<pre class="code"><code><span class="comment">(* Several results, slices, and checks that say what failed. *)</span>
44+<span class="kw">module</span> Taste;
45+<span class="kw">import</span> Out;
46+
47+<span class="kw">proc</span> divmod(a, b: <span class="ty">INT</span>): (<span class="ty">INT</span>, <span class="ty">INT</span>);
48+<span class="kw">begin</span>
49+ <span class="kw">return</span> a <span class="kw">div</span> b, a <span class="kw">mod</span> b;
50+<span class="kw">end</span>;
51+
52+<span class="kw">proc</span> sum(s: []<span class="ty">INT</span>): <span class="ty">INT</span>;
53+<span class="kw">var</span> i, t: <span class="ty">INT</span>;
54+<span class="kw">begin</span>
55+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span> t := t + s[i]; <span class="kw">end</span>;
56+ <span class="kw">return</span> t;
57+<span class="kw">end</span>;
58+
59+<span class="kw">proc</span> main*;
60+<span class="kw">var</span> q, r: <span class="ty">INT</span>; a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
61+<span class="kw">begin</span>
62+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> a[i] := i * i; <span class="kw">end</span>;
63+ q, r := divmod(sum(a), <span class="number">7</span>);
64+ Out.int(q); Out.str(<span class="string">&quot; remainder &quot;</span>); Out.int(r); Out.ln;
65+ Out.int(sum(a[<span class="number">1</span>..<span class="number">3</span>])); Out.ln;
66+<span class="kw">end</span>;
67+
68+<span class="kw">end</span> Taste.</code></pre>
69+<pre class="output"><code>4 remainder 2
70+5</code></pre>
71+ </div>
72+ <div>
73+ <h2>What it has</h2>
74+ <ul class="features">
75+ <li>Integers of 8 to 64 bits, signed and unsigned, which wrap; <code>BOOL</code>, <code>F32</code>, <code>F64</code>. Nothing converts unless it is written.</li>
76+ <li>Arrays, checked slices, pointers, records laid out as C lays them out, packed records, enumerations, sets, and variants with <code>match</code>.</li>
77+ <li>Every index and slice is checked; a failure says where it was, with the values, and the calls under way.</li>
78+ <li>Every variable starts at zero. Behaviour is defined everywhere but through pointers.</li>
79+ <li>No garbage collector: memory comes from an allocator the program chooses and passes.</li>
80+ <li>Several results, <code>ERROR</code> values and <code>defer</code>, in place of exceptions.</li>
81+ <li>Modules with bodies, and generic modules: <code>Map(K, V; hash, eq)</code>.</li>
82+ <li><code>^VOLATILE T</code> and the atomics <code>CAS</code>, <code>XCHG</code>, <code>XADD</code>, <code>FENCE</code>, for devices and lock-free code.</li>
83+ <li><code>extern</code> procedures and variables from C, code procedures in assembly, and <code>SYSCALL</code>.</li>
84+ <li>Threads without C, a library of <code>Out</code>, <code>Str</code>, <code>Fmt</code>, <code>Map</code>, <code>Sort</code> and more, and gdb that knows your lines.</li>
85+ <li>A formatter, a documentation tool, and editor support for vim and emacs.</li>
86+ </ul>
87+ </div>
88+</div>
89+
90+<h2>The language, in writing</h2>
91+<p>The language is defined by <a href="spec.html">the report</a>, which is
92+short on purpose: whatever it doesn&rsquo;t allow, raz doesn&rsquo;t have. There
93+is also a <a href="https://github.com/ldiaz94/raz/blob/main/doc/compiler.md">guide to reading the
94+compiler</a>, file by file.</p>
95+
96+<h2 id="download">Download</h2>
97+<p>Each release is a tarball with the compiler, <code>raz</code>, and its
98+library, <code>lib/</code>, which it finds beside itself.</p>
99+<div class="cta">
100+ <a class="btn primary" href="https://github.com/ldiaz94/raz/releases/latest">Latest release</a>
101+ <a class="btn" href="https://github.com/ldiaz94/raz">Source on GitHub</a>
102+</div>
103+<pre class="code"><code>$ tar xzf raz-*.tar.gz
104+$ ./raz run examples/Hello.raz
105+hello, world</code></pre>
106+<p>To build it yourself you need Free Pascal 3.2.2:</p>
107+<pre class="code"><code>$ apt-get install fp-compiler
108+$ git clone https://github.com/ldiaz94/raz
109+$ cd raz &amp;&amp; make &amp;&amp; make install # ~/.local, or PREFIX=/usr/local</code></pre>
110+
111+</main>
112+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
113+</body>
114+</html>

site/public/spec.html (history) added +2264 -0

@@ -0,0 +1,2264 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>The raz report</title>
7+<link rel="stylesheet" href="style.css">
8+</head>
9+<body>
10+<header><nav><a href="index.html">raz</a><a href="tour/index.html">Raz, Set, Go</a><a href="spec.html" aria-current="page">Report</a><a href="index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="spec"><ul>
13+<li><a href="#a-first-look" id="toc-a-first-look">1. A first
14+look</a></li>
15+<li><a href="#words-and-symbols" id="toc-words-and-symbols">2. Words and
16+symbols</a></li>
17+<li><a href="#modules" id="toc-modules">3. Modules</a></li>
18+<li><a href="#declarations" id="toc-declarations">4.
19+Declarations</a></li>
20+<li><a href="#types" id="toc-types">5. Types</a></li>
21+<li><a href="#expressions" id="toc-expressions">6. Expressions</a></li>
22+<li><a href="#statements" id="toc-statements">7. Statements</a></li>
23+<li><a href="#procedures" id="toc-procedures">8. Procedures</a></li>
24+<li><a href="#calling-c" id="toc-calling-c">9. Calling C</a></li>
25+<li><a href="#programs" id="toc-programs">10. Programs</a></li>
26+<li><a href="#the-library" id="toc-the-library">11. The library</a></li>
27+<li><a href="#versions" id="toc-versions">12. Versions</a></li>
28+<li><a href="#appendix-a.-what-an-implementation-chooses"
29+id="toc-appendix-a.-what-an-implementation-chooses">Appendix A. What an
30+implementation chooses</a></li>
31+<li><a href="#appendix-b.-the-grammar"
32+id="toc-appendix-b.-the-grammar">Appendix B. The grammar</a></li>
33+</ul>
34+<p>raz is a small systems language, first made for x86-64 Linux. A raz
35+program can lay a record over a device’s registers, read and write a
36+device’s memory exactly as written, take part in lock-free algorithms,
37+make system calls and call C libraries. It has no garbage collector and
38+no hidden run time: what the program does is what its text says.</p>
39+<p>This report defines the language. It is short on purpose. Whatever it
40+doesn’t allow, raz doesn’t have.</p>
41+<p>This is version 2026-10.17 of the report; section 12 says what each
42+version changed. 2026-10.11 and 2026-10.12 are a thaw the owner chose,
43+for a language anyone can build again. They break programs written for
44+2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0,
45+the last release before them; and 2026-10.12 breaks code procedures
46+written for 2026-10.11.</p>
47+<p>The language is frozen, softly, from this version on: it changes only
48+to mend a defect, and its library grows only by what breaks no program
49+written for it. What anyone wishes it had waits on a list, for a later
50+thaw.</p>
51+<h1 id="a-first-look">1. A first look</h1>
52+<pre><code>module Hello;
53+import Out;
54+
55+proc main*;
56+begin
57+ Out.str(&quot;hello, world\n&quot;);
58+end;
59+
60+end Hello.</code></pre>
61+<p>A program is a set of modules. The one named on the command line is
62+the main module, and it exports a procedure <code>main</code>; the
63+program starts there, once every module’s body has run (section 3).
64+Saved as <code>Hello.raz</code>, it is compiled with
65+<code>raz Hello.raz</code>, which writes the executable
66+<code>Hello</code>.</p>
67+<h1 id="words-and-symbols">2. Words and symbols</h1>
68+<p>A program is written in UTF-8. The compiler rejects a file that isn’t
69+valid UTF-8: a malformed or overlong sequence, a surrogate, or a code
70+point beyond 10FFFFH. Everything outside strings, characters and
71+comments is ASCII. Spaces, tabs and line breaks separate symbols and are
72+otherwise ignored.</p>
73+<p><strong>Identifiers</strong> are an ASCII letter or <code>_</code>
74+followed by letters, digits and <code>_</code>, up to 63 characters.
75+Case matters: <code>count</code> and <code>Count</code> are different
76+names. <code>_</code> alone isn’t a name: it is the blank, which stands
77+only to the left of <code>:=</code> (section 7.1).</p>
78+<p><strong>Reserved words</strong> are lowercase, except
79+<code>VOLATILE</code>:</p>
80+<pre><code>and begin break by code const defer div do else elsif end extern for
81+forward if import in link match mod module not of or packed proc record
82+repeat return set shl shr then to type until var variant VOLATILE while
83+xor</code></pre>
84+<p><code>static</code>, in <code>link static;</code> (section 3), isn’t
85+one.</p>
86+<p><strong>Predeclared names</strong> are the basic types (section 5.1),
87+<code>ERROR</code>, <code>true</code>, <code>false</code>,
88+<code>nil</code> and the built-in procedures (section 6.8). They belong
89+to a scope around every module, so a module or a procedure may declare
90+the same name for something else, which hides them there.</p>
91+<p><strong>Numbers.</strong> <code>123</code> is decimal.
92+<code>0FFH</code> is hexadecimal: it starts with a digit, its digits are
93+<code>0</code> to <code>9</code> and <code>A</code> to <code>F</code>,
94+and it ends in <code>H</code>, all in capitals. <code>1.5</code> and
95+<code>2.0E-3</code> are reals; a real always has a digit on both sides
96+of its point, and its exponent, if it has one, is <code>E</code>, a sign
97+if wanted, and digits. A decimal number above the largest
98+<code>I64</code>, or a hexadecimal one above the largest
99+<code>U64</code>, is an error.</p>
100+<p>An integer is an untyped constant, which takes the type it is used
101+as, up to the largest <code>I64</code>. A hexadecimal number above that,
102+such as <code>0FFFFFFFFFFFFFFFFH</code>, has type <code>U64</code>: so
103+<code>-1</code> never fits a <code>U64</code>, and the big number goes
104+nowhere else without a conversion. Arithmetic on untyped constants
105+doesn’t wrap: a result beyond the <code>I64</code>s, such as
106+<code>9223372036854775807 + 1</code>, is an error.</p>
107+<p>A real is an untyped constant too, which takes <code>F32</code> or
108+<code>F64</code>, whichever it is used as, rounded to the nearest value
109+of that type; where nothing says which, it is an <code>F64</code>. A
110+real never becomes an integer, nor an integer a real, without a
111+conversion: <code>x := 1;</code> is an error for <code>x: F64</code>,
112+and <code>x := 1.0;</code> is right.</p>
113+<p><strong>Characters.</strong> <code>'a'</code> is the code point of
114+one character, an integer: <code>'a'</code> is 97 and <code>'é'</code>
115+is 233.</p>
116+<p><strong>Strings.</strong> <code>"héllo\n"</code> is a sequence of
117+bytes, the UTF-8 of its text: <code>"é"</code> is the two bytes 0C3H,
118+0A9H. Both characters and strings take the escapes <code>\n</code> (line
119+feed), <code>\t</code> (tab), <code>\r</code> (carriage return),
120+<code>\0</code> (zero), <code>\\</code>, <code>\'</code>,
121+<code>\"</code>, <code>\u{H...}</code> (the code point with hexadecimal
122+value H…, in a string its UTF-8) and, in strings only, <code>\xHH</code>
123+(the byte with hexadecimal value HH, which needn’t make valid UTF-8). In
124+an escape, the hexadecimal digits may be capitals or not. A character or
125+a string ends on the line it starts on.</p>
126+<p>Messages give the line and the column, counted in characters (section
127+10).</p>
128+<p><strong>Comments</strong> are <code>(*</code> … <code>*)</code> and
129+nest. By convention, the comment just before an exported declaration is
130+its documentation, and the one before <code>module</code> is the
131+module’s.</p>
132+<p><strong>Operators and delimiters:</strong></p>
133+<pre><code>+ - * / &amp; | ~ = # &lt; &lt;= &gt; &gt;= := : ; , . .. ... ( ) [ ] { } ^</code></pre>
134+<p><code>|</code> is also what starts an arm of <code>match</code>
135+(section 7.4); an arm comes where a statement could, never after an
136+operand, so the two don’t mix.</p>
137+<h1 id="modules">3. Modules</h1>
138+<pre><code>Module = &quot;module&quot; ident [ &quot;(&quot; ModParams &quot;)&quot; ] &quot;;&quot;
139+ { Import } { Link } [ Check ] { Declaration }
140+ [ &quot;begin&quot; Statements ] &quot;end&quot; ident &quot;.&quot; .
141+ModParams = ident { &quot;,&quot; ident } { &quot;;&quot; ident &quot;:&quot; &quot;proc&quot; Signature } .
142+Import = &quot;import&quot; ImportItem { &quot;,&quot; ImportItem } &quot;;&quot; .
143+ImportItem = ident [ &quot;:=&quot; ident [ &quot;(&quot; Type { &quot;,&quot; Type } &quot;)&quot; ] ] .
144+Link = &quot;link&quot; ( string | &quot;static&quot; ) &quot;;&quot; .
145+Check = &quot;check&quot; &quot;off&quot; &quot;;&quot; .</code></pre>
146+<p>A module lives in a file of its own name: module <code>Out</code> is
147+<code>Out.raz</code>. The name after <code>end</code> repeats the
148+module’s name.</p>
149+<p><strong>Exports.</strong> A name declared at the top of a module with
150+<code>*</code> after it is exported: other modules can use it as
151+<code>Module.name</code>. Record fields are exported the same way.
152+Everything else is private to the module. An exported variable can be
153+read by the modules that import it, but only its own module changes it.
154+Outside it, the variable is a value, not a variable: nothing that could
155+write it, or let something else write it, may name it or a field or
156+element of it. So it can’t be assigned, passed as a <code>var</code>
157+argument, changed by <code>INC</code>, <code>DEC</code> or
158+<code>NEW</code>, or given to <code>ADR</code>; an array in it can’t be
159+sliced, or used where a slice is expected (the silent conversion of an
160+array, section 5.10), though a slice in it may be sliced, since that
161+makes a new slice and can’t change the variable; in <code>match</code>,
162+its arms’ names stand for the fields of a copy. It can be read, copied
163+and compared, and a copy is the importer’s own. What a pointer or a
164+slice in it points at is another variable, and isn’t held to this: with
165+<code>s: []U8</code> exported, <code>M.s[0] := 0</code> writes what
166+<code>M.s</code> points at, not <code>M</code>. A module that means to
167+share an array exports a pointer to it, or a procedure that returns a
168+slice of it.</p>
169+<p><strong>Imports.</strong> <code>import Out;</code> makes
170+<code>Out</code>’s exports available as <code>Out.x</code>.
171+<code>import O := Out;</code> uses the name <code>O</code> instead. A
172+module can’t import itself, directly or through others.</p>
173+<p><strong>Generic modules</strong> take types as parameters:</p>
174+<pre><code>module List(T);
175+type
176+ Node* = record next*: ^Node; val*: T; end;
177+...
178+end List.</code></pre>
179+<p>An import gives the arguments:
180+<code>import Ints := List(I64);</code>. Each distinct list of arguments
181+makes one <em>instance</em>: the module compiled with <code>T</code>
182+standing for <code>I64</code>. Two imports of <code>List(I64)</code>, in
183+any modules, share one instance, its variables and its
184+<code>ERROR</code>s with it: two lists of arguments are the same when
185+their types are the same (section 5.10), an alias as what it stands for,
186+and their procedures are the same procedures. Since every instance is
187+checked on its own, an operation the argument type doesn’t support
188+(adding two records, say) is reported as an error in that instance.</p>
189+<p>An instance is made where its module is first used, not at the
190+import: so the arguments may name types the importing module declares
191+after its imports, as long as they are declared before that first use.
192+The instance is made before the code of the procedure, or the module
193+body, that first uses it, and its arguments are read in the scope
194+there.</p>
195+<pre><code>import Table := Map([]U8, Colour, Str.hash, Str.eq);
196+
197+type Colour = (red, green, blue);
198+
199+var names: Table.Map; (* the instance is made here *)</code></pre>
200+<p>What a generic module needs to do with its types, beyond moving them
201+about, it is given as procedures, after the types:</p>
202+<pre><code>module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);</code></pre>
203+<pre><code>import Names := Map([]U8, I64, Str.hash, Str.eq);</code></pre>
204+<p>A procedure argument names a procedure, not a variable or an
205+expression, and its signature, with the type arguments put in, must be
206+the parameter’s. Inside the module, <code>hash(k)</code> calls it
207+directly. Two imports make the same instance only if their types and
208+their procedures are the same.</p>
209+<p><strong>Finding modules.</strong> The compiler looks for module
210+<code>Name</code> in the main module’s directory, then in each directory
211+given with <code>-I</code>, then in the <code>lib</code> directory
212+beside the compiler. In each directory it takes the first of
213+<code>Name.OS-ARCH.raz</code>, <code>Name.OS.raz</code>,
214+<code>Name.ARCH.raz</code> and <code>Name.raz</code> that exists, where
215+<code>OS-ARCH</code> is the target, for now only
216+<code>linux-x86_64</code>. So a module whose code depends on the machine
217+or the system comes in one file per target, and the modules that import
218+it don’t change. Machine code (section 8) and system calls belong in
219+such files.</p>
220+<p><strong>One module to a name.</strong> A program has one module of
221+each name, so the compiler reports an error, naming the files, if a
222+module it looks for has files in more than one of these directories,
223+rather than take the first: a <code>Buf.raz</code> beside a program
224+would otherwise stand in for the library’s <code>Buf</code> in every
225+module that imports it, the library’s own among them. A link is the
226+exception: a file that is a symbolic link to one of the module’s files
227+in a later directory chooses that file, so that a directory holding a
228+link to <code>lib/Sys.raz</code> builds a program over the C library’s
229+<code>Sys</code> on Linux too.</p>
230+<p><strong>Names are flat.</strong> The names of a program’s modules are
231+one space, with no packages or paths. So the names of the library’s
232+modules (section 11) are kept for it: a program or another library
233+doesn’t name a module of its own <code>Arena</code>, <code>Buf</code>,
234+<code>C</code>, <code>Files</code>, <code>Fmt</code>, <code>Heap</code>,
235+<code>List</code>, <code>Lock</code>, <code>Map</code>,
236+<code>Math</code>, <code>Mem</code>, <code>Opt</code>, <code>Out</code>,
237+<code>Rt</code>, <code>Sort</code>, <code>Str</code>, <code>Sys</code>
238+or <code>Thread</code>, nor any name the library takes later. The
239+modules of any other library share a prefix of their own, as an X11
240+library’s <code>Xauth</code> and <code>Xkeys</code> do, and a program
241+that wants a shorter name imports one:
242+<code>import Auth := Xauth;</code>.</p>
243+<p>The compiler holds every file of such a module to the same interface.
244+When it takes one of <code>Name.*.raz</code>, it also reads the
245+declarations of the others beside it, and of <code>Name.raz</code> if
246+there is one, and reports an error unless they all export the same
247+names, each the same kind of thing:</p>
248+<ul>
249+<li>a constant of the same type (its value may differ: a system call
250+number, say);</li>
251+<li>a type of the same kind, and for a record or variant the same
252+exported fields and cases, with the same types (unexported fields, and
253+so the size and layout, may differ);</li>
254+<li>a variable of the same type;</li>
255+<li>a procedure of the same signature.</li>
256+</ul>
257+<p>Two types here are the same if they are built alike, a name in one
258+file standing for the same name in another.</p>
259+<p><strong>Module bodies.</strong> A module may end with
260+<code>begin</code> and statements, its body. Every module’s body runs
261+once, before the main module’s <code>main</code>: a module’s body runs
262+after the bodies of the modules it imports, in the order of its
263+<code>import</code>s, and once however many modules import it. Each
264+instance of a generic module has its body run once. Module variables are
265+zero before any body runs. <code>return;</code> ends a body early.</p>
266+<p><strong><code>link "libX11.so.6";</code></strong> names a shared
267+library to load at run time, for the <code>extern</code> declarations of
268+section 9. <strong><code>link static;</code></strong> says the module
269+needs a program without C (section 10): it makes its own system calls,
270+or its own threads, which the C library wouldn’t know about.</p>
271+<p><strong><code>check off;</code></strong>, after the links, leaves out
272+of the module’s own code the checks of indexes and slice bounds of
273+section 6.5, and that a slice of a pointer doesn’t run backwards
274+(section 6.4), for speed where the program is sure of them. An index out
275+of range there reads or writes whatever memory it reaches, and what
276+follows isn’t defined. Every other check stays, and the module’s callers
277+keep theirs. An instance of a generic module follows the generic
278+module’s own <code>check</code> line, not that of the module that made
279+it. <code>check</code> and <code>off</code> aren’t reserved words, as
280+<code>static</code> isn’t.</p>
281+<h1 id="declarations">4. Declarations</h1>
282+<pre><code>Declaration = &quot;const&quot; { IdentDef [ &quot;:&quot; Type ] &quot;=&quot; Expr &quot;;&quot; }
283+ | &quot;type&quot; { IdentDef &quot;=&quot; Type &quot;;&quot; }
284+ | &quot;var&quot; { IdentList &quot;:&quot; Type &quot;;&quot; }
285+ | ProcDeclaration &quot;;&quot;
286+ | ExternDeclaration &quot;;&quot; .
287+IdentDef = ident [ &quot;*&quot; ] .
288+IdentList = IdentDef { &quot;,&quot; IdentDef } .</code></pre>
289+<p>A name must be declared before it is used, with two exceptions: the
290+base type of a pointer may name a type declared later in the same
291+<code>type</code> section (so that two records can point at each other),
292+and a procedure may be declared <code>forward</code> and defined later
293+(section 8).</p>
294+<p><strong>Constants</strong> are evaluated by the compiler. Their
295+expressions use only literals, other constants, operators, conversions,
296+<code>SIZE</code> and the <code>LEN</code> of arrays and strings. A
297+constant is an integer, a real, a <code>BOOL</code>, a name of an
298+enumeration, a set, a string, an array, a record or an
299+<code>ERROR</code>. A constant of an integer type wraps as the program
300+would: <code>U8(250) + 10</code> is 4. Dividing by zero in a constant is
301+an error, and so is dividing the smallest value of a signed type by -1,
302+with <code>div</code> or <code>mod</code>, which stops a program
303+(section 6.1). A string constant can be indexed and sliced as an array
304+of <code>U8</code> is, and its <code>LEN</code> is its length in bytes.
305+<code>+</code> joins two string constants into one:</p>
306+<pre><code>const Text = &quot;the cat&quot;;
307+ Line = Text + &quot;\n&quot;;
308+...
309+w := Text[4..7]; (* &quot;cat&quot; *)</code></pre>
310+<p>A constant with a type is an <code>ERROR</code> (section 5.8), an
311+array or a record, and the value of an array or record constant is a
312+literal (section 6.5) of constants. Its elements and fields are
313+integers, reals, <code>BOOL</code>s, enumerations, sets,
314+<code>ERROR</code>s, procedures, <code>nil</code>, a string for a
315+<code>[]U8</code>, an array constant for a slice of its element type,
316+and arrays and records of these; no variant and no other pointer:</p>
317+<pre><code>const Primes: [5]I64 = [2, 3, 5, 7, 11];
318+type Rule = record class: []U8; tag: INT; float: BOOL; end;
319+const Rules: [2]Rule = [{class: &quot;firefox&quot;, tag: 2}, {class: &quot;mpv&quot;, float: true}];
320+...
321+p := Primes[i];</code></pre>
322+<p>It is used as a variable of that type is, but can’t be changed: an
323+array constant can be indexed and sliced and its <code>LEN</code> is
324+<code>n</code>, a record constant’s fields can be read, and either can
325+be assigned, compared as its type allows, and passed. Its parts are
326+constant as a string’s bytes are (section 5.10). A part of it isn’t a
327+constant, even at a constant index.</p>
328+<p><strong>Variables</strong> of a module live as long as the program;
329+variables of a procedure live as long as its call. Both start at zero:
330+every byte of them is zero. So a number starts as <code>0</code>
331+(<code>0.0</code> for <code>F32</code> and <code>F64</code>), a
332+<code>BOOL</code> as <code>false</code>, a pointer, procedure or
333+<code>ERROR</code> as <code>nil</code>, a slice as the empty slice
334+(<code>ptr</code> is <code>nil</code>, <code>len</code> is 0), a variant
335+as its first case with its fields zero, a set as the empty set, and an
336+enumeration as its first name. No variable is ever read before it has a
337+value.</p>
338+<h1 id="types">5. Types</h1>
339+<pre><code>Type = QualIdent
340+ | &quot;[&quot; Expr &quot;]&quot; Type (* array *)
341+ | &quot;[&quot; &quot;]&quot; Type (* slice *)
342+ | &quot;^&quot; [ &quot;VOLATILE&quot; ] Type (* pointer *)
343+ | [ &quot;packed&quot; ] &quot;record&quot; { IdentList &quot;:&quot; Type &quot;;&quot; } &quot;end&quot;
344+ | &quot;variant&quot; { Case &quot;;&quot; } &quot;end&quot;
345+ | &quot;(&quot; ident { &quot;,&quot; ident } &quot;)&quot; (* enumeration *)
346+ | &quot;set&quot; &quot;of&quot; QualIdent
347+ | &quot;proc&quot; Signature .
348+Case = ident [ &quot;(&quot; FieldList { &quot;;&quot; FieldList } &quot;)&quot; ] .
349+FieldList = ident { &quot;,&quot; ident } &quot;:&quot; Type .
350+QualIdent = [ ident &quot;.&quot; ] ident .</code></pre>
351+<h2 id="basic-types">5.1 Basic types</h2>
352+<table>
353+<colgroup>
354+<col style="width: 33%" />
355+<col style="width: 33%" />
356+<col style="width: 33%" />
357+</colgroup>
358+<thead>
359+<tr class="header">
360+<th>Type</th>
361+<th>Size</th>
362+<th>Values</th>
363+</tr>
364+</thead>
365+<tbody>
366+<tr class="odd">
367+<td><code>I8</code> <code>I16</code> <code>I32</code>
368+<code>I64</code></td>
369+<td>1, 2, 4, 8</td>
370+<td>signed integers, two’s complement</td>
371+</tr>
372+<tr class="even">
373+<td><code>INT</code></td>
374+<td>an address’s: 8 on x86-64</td>
375+<td>signed, as big as an address</td>
376+</tr>
377+<tr class="odd">
378+<td><code>UINT</code></td>
379+<td>an address’s</td>
380+<td>unsigned, as big as an address</td>
381+</tr>
382+<tr class="even">
383+<td><code>U8</code> <code>U16</code> <code>U32</code>
384+<code>U64</code></td>
385+<td>1, 2, 4, 8</td>
386+<td>unsigned integers</td>
387+</tr>
388+<tr class="odd">
389+<td><code>BOOL</code></td>
390+<td>1</td>
391+<td><code>false</code> (0), <code>true</code> (1)</td>
392+</tr>
393+<tr class="even">
394+<td><code>F32</code></td>
395+<td>4</td>
396+<td>IEEE 754 single precision</td>
397+</tr>
398+<tr class="odd">
399+<td><code>F64</code></td>
400+<td>8</td>
401+<td>IEEE 754 double precision</td>
402+</tr>
403+</tbody>
404+</table>
405+<p><code>INT</code> is the everyday integer: lengths, indexes and sizes
406+are <code>INT</code>s. It is as big as an address, so it is the integer
407+a pointer converts to (section 6.7), and a word the atomic built-ins
408+work on (section 6.8). <code>INT</code> and <code>I64</code> are two
409+types, even where they have the same size: an <code>INT</code> goes
410+silently into an <code>I64</code> (section 5.10), but the two don’t mix
411+in an expression (section 6.1), and an <code>I64</code> becomes an
412+<code>INT</code> only by <code>INT(x)</code>, which may drop bits on a
413+smaller target. A type bigger than the largest <code>INT</code> in bytes
414+is an error.</p>
415+<p><code>F32</code> and <code>F64</code> exist only on a target that has
416+reals (appendix A). On one that hasn’t, a real literal, and a use of
417+<code>F32</code>, <code>F64</code> or of a name whose type is made of
418+them, is an error; a declaration that no used code names isn’t, so a
419+module like <code>Fmt</code> keeps one interface on every target.</p>
420+<p>Each is aligned as the target’s C aligns it: to its size on x86-64.
421+So are <code>ERROR</code> (section 5.8), pointers and procedures, each
422+as big as an address; a slice is aligned as an address is.</p>
423+<h2 id="arrays">5.2 Arrays</h2>
424+<p><code>[n]T</code> is <code>n</code> elements of type <code>T</code>,
425+one after another. <code>n</code> is a constant greater than zero.
426+Arrays are values: assigning one copies it.</p>
427+<h2 id="slices">5.3 Slices</h2>
428+<p><code>[]T</code> is a view of elements that live elsewhere: a pointer
429+to the first and a count, two addresses’ worth, 16 bytes on x86-64.</p>
430+<pre><code>Slice = record ptr: ^T; len: INT; end; (* its layout *)</code></pre>
431+<p>A slice is made from an array (<code>a[2..5]</code>, or an array used
432+where a slice is wanted), from another slice, or from a pointer
433+(<code>p[0..n]</code>). It doesn’t own what it points at.</p>
434+<h2 id="pointers">5.4 Pointers</h2>
435+<p><code>^T</code> is the address of a <code>T</code>, as big as an
436+<code>INT</code>. <code>nil</code> is the pointer to nothing.</p>
437+<p><code>^VOLATILE T</code> is the address of a <code>T</code> that
438+something besides the program may read or change: a device register,
439+memory shared with another process. Section 7.6 says what that
440+promises.</p>
441+<h2 id="records">5.5 Records</h2>
442+<pre><code>type Cell = record ch: U8; attr: U8; end;</code></pre>
443+<p>Fields are laid out in order as C lays them out: each at the next
444+multiple of its own alignment, and the whole record padded to a multiple
445+of its largest alignment. A record declared like a C struct has the same
446+layout as that struct. A record may have no fields; its size is then
447+0.</p>
448+<p>A <code>packed record</code> has no padding: each field starts at the
449+byte after the field before it, and the record’s alignment is 1. So a
450+field of a packed record may sit at any address. It is read and written
451+like any other, on every target, even one whose machine can’t reach a
452+misaligned word in one access: the compiled code reaches it in parts
453+there. But a field of a packed record whose type is aligned to more than
454+1 byte, and anything inside it, has no address: <code>ADR</code> of it,
455+a <code>var</code> parameter of it and a slice of it are errors. Nor can
456+it be reached through a <code>^VOLATILE</code> pointer, since a
457+misaligned access can’t be promised as one access (section 7.6). Its
458+value can be copied out and back in.</p>
459+<p>The bytes of a value of more than one byte are in the target’s order
460+(appendix A). A file or a message in an order of its own is read and
461+written a byte at a time, as <code>Str.le</code>, <code>Str.be</code>,
462+<code>Str.putLE</code> and <code>Str.putBE</code> do (section 11).</p>
463+<h2 id="variants">5.6 Variants</h2>
464+<p>A variant holds one of several cases, each with its own fields:</p>
465+<pre><code>type
466+ Shape* = variant
467+ circle(r: F64);
468+ rect(w, h: F64);
469+ dot;
470+ end;</code></pre>
471+<p>The case names are declared in the scope around the type, so
472+<code>circle</code> can be used directly, and as
473+<code>Shapes.circle</code> from another module if <code>Shape</code> is
474+exported. A case with fields is built by calling it like a procedure,
475+<code>circle(2.0)</code>; a case without fields is just
476+<code>dot</code>. The only way to read the fields is <code>match</code>
477+(section 7.4).</p>
478+<p>Layout: an <code>INT</code> tag (0 for the first case, 1 for the
479+second, and so on), then the space for the fields. Each case’s fields
480+are laid out in that space as a record’s would be, after the tag, and
481+the cases share it, so the variant is as big as its largest case needs,
482+padded to its alignment, the largest of an <code>INT</code>‘s and its
483+fields’. <code>variant a(x: U8); b(y: I64; z: U8); end</code> is 24
484+bytes on x86-64. A variant that starts at zero therefore holds its first
485+case.</p>
486+<h2 id="enumerations-and-sets">5.7 Enumerations and sets</h2>
487+<pre><code>type
488+ Color* = (red, green, blue);
489+ Colors* = set of Color;</code></pre>
490+<p>An enumeration is a list of names, in order. Like the cases of a
491+variant, the names are declared in the scope around the type. An
492+enumeration has at most 256 names and takes 1 byte; the first name is 0,
493+the next 1, and so on.</p>
494+<p><code>set of E</code>, for an enumeration <code>E</code> of at most
495+64 names, holds any choice of <code>E</code>’s names: bit <code>i</code>
496+stands for the name at position <code>i</code>. It takes the smallest of
497+1, 2, 4 and 8 bytes that has a bit for each name, so a set can stand for
498+a C flags word.</p>
499+<h2 id="errors">5.8 Errors</h2>
500+<pre><code>const NotFound*: ERROR = &quot;not found&quot;;</code></pre>
501+<p><code>ERROR</code> is a basic type as big as an address. A value of
502+it is made only by a constant declared as above, and each such
503+declaration makes an error different from every other, whatever its
504+message: two modules that both declare <code>"not found"</code> have two
505+different errors. A generic module makes its errors once per instance.
506+<code>nil</code> is no error.</p>
507+<p>Errors compare with <code>=</code> and <code>#</code> only, by which
508+declaration made them, never by their message. <code>MSG(e)</code> is
509+the message, a <code>[]U8</code>; <code>MSG(nil)</code> is
510+<code>""</code>.</p>
511+<pre><code>proc find*(key: I64): (^Node, ERROR);
512+...
513+n, err := Table.find(7);
514+if err = Table.NotFound then ... end;</code></pre>
515+<h2 id="procedure-types">5.9 Procedure types</h2>
516+<pre><code>type Compare = proc(a, b: I64): BOOL;</code></pre>
517+<p>A value of a procedure type is the address of a procedure with that
518+signature, as big as an address, or <code>nil</code>. Two signatures are
519+alike when their parameters have the same types, each <code>var</code>
520+or not alike, in order, and their results are the same types; the
521+parameters’ names don’t matter.</p>
522+<h2 id="same-and-assignable">5.10 Same and assignable</h2>
523+<p>Two named types are the same only if they are the same declaration.
524+Two anonymous pointer, array, slice, set or procedure types are the same
525+if they are built alike: <code>^I64</code> is <code>^I64</code> wherever
526+it is written. A record, variant or enumeration, though, is a type of
527+its own wherever it is written, named or not: two
528+<code>record x: I64; end</code> are two types. A named and an anonymous
529+type are the same if the named one is built like the anonymous one, so
530+that <code>p := ADR(n)</code> works for <code>p: NodePtr</code> with
531+<code>type NodePtr = ^Node</code>. Sameness doesn’t pass through the
532+anonymous type: with <code>type P1 = ^Node; P2 = ^Node;</code>, each of
533+<code>P1</code> and <code>P2</code> is the same as <code>^Node</code>,
534+but they are two declarations, so not the same as each other.</p>
535+<p>A type declared with only a name on the right,
536+<code>type Count = I64;</code>, is another name for that type, not a new
537+one.</p>
538+<p>A value of type <code>A</code> can be assigned to a variable of type
539+<code>B</code> (and passed to a parameter of type <code>B</code>, and
540+returned as a <code>B</code>) if they are the same type, or if:</p>
541+<ul>
542+<li><code>A</code> is an integer constant whose value fits in the
543+integer type <code>B</code>;</li>
544+<li><code>A</code> is <code>nil</code> and <code>B</code> is a pointer,
545+procedure or <code>ERROR</code> type;</li>
546+<li><code>A</code> is <code>^T</code> and <code>B</code> is
547+<code>^VOLATILE T</code>;</li>
548+<li><code>A</code> is <code>[n]T</code> and <code>B</code> is
549+<code>[]T</code>;</li>
550+<li><code>A</code> is a string literal and <code>B</code> is
551+<code>[]U8</code>, <code>^U8</code> (a pointer to the bytes followed by
552+a zero byte, as C wants), or <code>[n]U8</code> with <code>n</code> at
553+least the string’s length (the rest is zero);</li>
554+<li><code>A</code> is a procedure and <code>B</code> a procedure type
555+with the same signature;</li>
556+<li><code>A</code> is <code>INT</code> and <code>B</code> is
557+<code>I64</code>, or <code>A</code> is <code>UINT</code> and
558+<code>B</code> is <code>U64</code>, which loses nothing on any
559+target.</li>
560+</ul>
561+<p>A string’s bytes are constant. A <code>[]U8</code> or
562+<code>^U8</code> made from a string literal, or a slice of a string
563+constant, points at them, and writing through it isn’t defined: on a
564+target with read-only memory, it may stop the program as a bad address
565+(section 10). A <code>[n]U8</code> made from a string is a copy, the
566+program’s to change.</p>
567+<p>Nothing else converts silently. In particular, there is no other
568+conversion between integer types, between integers and reals, or between
569+<code>F32</code> and <code>F64</code>, without a written conversion
570+(section 6.7). Results passed on whole need the same types exactly: in
571+<code>a, b := f(x)</code> and in <code>return f(x)</code>, an
572+<code>INT</code> result doesn’t pass up as an <code>I64</code>.
573+Elsewhere, <code>return n, n</code> included, an <code>INT</code> widens
574+as above.</p>
575+<h1 id="expressions">6. Expressions</h1>
576+<pre><code>Expr = Conjunct { &quot;or&quot; Conjunct } .
577+Conjunct = Relation { &quot;and&quot; Relation } .
578+Relation = SimpleExpr [ RelOp SimpleExpr ] .
579+RelOp = &quot;=&quot; | &quot;#&quot; | &quot;&lt;&quot; | &quot;&lt;=&quot; | &quot;&gt;&quot; | &quot;&gt;=&quot; | &quot;in&quot; .
580+SimpleExpr = [ &quot;-&quot; ] Term { AddOp Term } .
581+AddOp = &quot;+&quot; | &quot;-&quot; | &quot;|&quot; | &quot;xor&quot; .
582+Term = Factor { MulOp Factor } .
583+MulOp = &quot;*&quot; | &quot;/&quot; | &quot;div&quot; | &quot;mod&quot; | &quot;&amp;&quot; | &quot;shl&quot; | &quot;shr&quot; .
584+Factor = number | char | string | Designator | &quot;(&quot; Expr &quot;)&quot;
585+ | &quot;not&quot; Factor | &quot;~&quot; Factor
586+ | &quot;[&quot; [ Expr { &quot;,&quot; Expr } ] &quot;]&quot;
587+ | &quot;{&quot; [ Element { &quot;,&quot; Element } ] &quot;}&quot;
588+ | &quot;^&quot; [ &quot;VOLATILE&quot; ] Type &quot;(&quot; Expr &quot;)&quot; .
589+Element = Expr | ident &quot;:&quot; Expr .
590+Designator = QualIdent { Selector } .
591+Selector = &quot;.&quot; ident | &quot;^&quot; | &quot;[&quot; Expr [ &quot;..&quot; Expr ] &quot;]&quot;
592+ | &quot;(&quot; [ Expr { &quot;,&quot; Expr } ] &quot;)&quot; .</code></pre>
593+<p>The operators bind in the order of the grammar: <code>not</code> and
594+<code>~</code> most tightly, then the multiplying operators, then the
595+adding ones, then the relations, then <code>and</code>, then
596+<code>or</code>. So <code>a + b * c = d</code> is
597+<code>(a + (b * c)) = d</code>, <code>x &amp; 0FH = 0</code> is
598+<code>(x &amp; 0FH) = 0</code>, and
599+<code>i &lt; n and a[i] # 0 or done</code> is
600+<code>((i &lt; n) and (a[i] # 0)) or done</code>. A unary minus applies
601+to the first term: <code>-a * b</code> is <code>-(a * b)</code>.</p>
602+<p><code>and</code>, <code>or</code> and <code>not</code> are for
603+<code>BOOL</code>s only; <code>&amp;</code>, <code>|</code>,
604+<code>xor</code> and <code>~</code> are for integers only.</p>
605+<p>A pointer to a record isn’t the record: its fields are
606+<code>p^.f</code>, never <code>p.f</code>, and the elements of a pointer
607+to an array are <code>p^[i]</code>. A statement starts with a name, so
608+<code>^T(x)^ := v</code> isn’t one.</p>
609+<h2 id="integers">6.1 Integers</h2>
610+<p>Both operands of <code>+ - * div mod &amp; | xor</code>, and of a
611+relation, have the same integer type, or one is an untyped constant that
612+takes the other’s type, which it must fit. So do <code>v</code> and
613+<code>k</code> in <code>INC(v, k)</code> and <code>DEC(v, k)</code>. The
614+result has that type and wraps around at its width: in <code>U8</code>,
615+<code>200 + 100</code> is <code>44</code>. A unary minus wraps too: for
616+<code>x</code> of an unsigned type, <code>-x</code> is
617+<code>0 - x</code>. Two integer types are never mixed, whatever their
618+sizes: with <code>i: I64</code>, <code>i &lt; LEN(s)</code> is an error,
619+and <code>i</code> should be an <code>INT</code>.</p>
620+<p>Where the report asks for an integer and names no type, as an index,
621+a slice’s bound, a shift’s count, a pointer’s offset <code>i</code> in
622+<code>p + i</code>, or a conversion’s operand, any integer type will do,
623+and its value is taken. On a target whose <code>INT</code> is narrower,
624+a pointer’s offset is cut to an <code>INT</code>.</p>
625+<p><code>SIZE(T)</code>, and <code>LEN</code> of an array or a string
626+constant, are untyped constants, like a literal.</p>
627+<p><code>div</code> rounds toward zero and <code>mod</code> takes the
628+sign of the dividend, as in C: <code>-7 div 2</code> is <code>-3</code>,
629+<code>-7 mod 2</code> is <code>-1</code>. Dividing by zero, and dividing
630+the smallest value of a signed type by <code>-1</code>, stop the program
631+(<code>SIGFPE</code>), whatever the type and the target.</p>
632+<p><code>&amp;</code> (and), <code>|</code> (or), <code>xor</code> and
633+<code>~</code> (not) work on every bit. <code>x shl n</code> and
634+<code>x shr n</code> shift by <code>n</code> modulo the width of
635+<code>x</code>’s type, in bits: for <code>x: U8</code>,
636+<code>x shl 9</code> is <code>x shl 1</code>. <code>shr</code> brings in
637+copies of the sign bit for signed types and zeros for unsigned ones.
638+<code>n</code> may be of any integer type. The result has
639+<code>x</code>’s type, cut to its width: in <code>U8</code>,
640+<code>0C0H shl 1</code> is <code>80H</code>.</p>
641+<p>Relations compare as signed or unsigned according to the type.</p>
642+<h2 id="booleans">6.2 Booleans</h2>
643+<p><code>and</code> and <code>or</code> evaluate their right operand
644+only when it decides the result. <code>not</code> negates, and
645+<code>=</code> and <code>#</code> compare; <code>#</code> is
646+“different”.</p>
647+<h2 id="reals">6.3 Reals</h2>
648+<p><code>+ - * /</code> and the relations work on <code>F32</code> and
649+on <code>F64</code>. Both operands have the same type, or one is a real
650+constant, which takes the other’s type (section 2). The result has that
651+type: <code>F32</code> arithmetic is done in single precision, rounded
652+at each step as C rounds <code>float</code>. A constant expression of
653+reals is evaluated in double precision, then rounded once to the type it
654+takes.</p>
655+<p>Relations are IEEE 754’s: a NaN is equal to nothing, itself included,
656+so for a NaN <code>x</code>, <code>x # x</code> is true and every other
657+relation with it is false. Dividing by zero gives an infinity or a NaN;
658+arithmetic on reals never stops the program.</p>
659+<h2 id="pointers-1">6.4 Pointers</h2>
660+<ul>
661+<li><code>p^</code> is the variable <code>p</code> points at.</li>
662+<li><code>p + i</code> and <code>p - i</code>, with <code>i</code> an
663+integer, move <code>p</code> by <code>i</code> elements: by
664+<code>i * SIZE(T)</code> bytes for <code>p: ^T</code>.</li>
665+<li><code>p - q</code>, with both of the same pointer type, is the
666+number of elements between them, an <code>INT</code>.</li>
667+<li><code>p[i]</code> is <code>(p + i)^</code>. <code>p[lo..hi]</code>
668+is the slice of the <code>hi - lo</code> elements from
669+<code>p + lo</code>. Neither is checked against a length, which a
670+pointer doesn’t have, but a slice of a pointer that runs backwards,
671+<code>hi &lt; lo</code>, is a failed check (section 6.5). A pointer to
672+an array isn’t indexed this way, since a step would be a whole array:
673+its elements are <code>p^[i]</code>.</li>
674+<li>Pointers compare with <code>= # &lt; &lt;= &gt; &gt;=</code> as
675+unsigned addresses. Procedure values compare with <code>=</code> and
676+<code>#</code> only.</li>
677+</ul>
678+<h2 id="arrays-and-slices">6.5 Arrays and slices</h2>
679+<p><code>a[i]</code> is an element of an array or slice, and
680+<code>a[lo..hi]</code> is the slice of the elements from <code>lo</code>
681+up to but not including <code>hi</code>. Both are checked:
682+<code>i</code> must be below the length, and
683+<code>0 &lt;= lo &lt;= hi &lt;= length</code>. A check that fails prints
684+to standard error where it is, as file, line and column, and what
685+failed, with the values involved
686+(<code>index 7 is out of range for a length of 5</code>), and stops the
687+program with status 2. An index or a bound may be of any integer type,
688+and is checked by its value, whatever its type: a negative
689+<code>I8</code>, or a <code>U64</code> past the length, fails like any
690+other. A module that says <code>check off;</code> (section 3) leaves
691+these checks out of its code.</p>
692+<p><strong>Literals.</strong> <code>[e1, e2, ...]</code> builds an
693+array, and <code>{f: e, g: e, ...}</code> a record, where the type it
694+must have is known: assigned to a variable, passed to a parameter,
695+returned, compared with a value of that type, or an element of a bigger
696+literal. The elements must be assignable to the element type, and an
697+array literal for <code>[n]T</code> has exactly <code>n</code> of them.
698+A record literal names its fields, in any order, each at most once; the
699+fields it leaves out are zero, so <code>{}</code> is the record with
700+every field zero. An array literal used as a slice is a hidden variable
701+of the procedure, and lives as long as its call.</p>
702+<pre><code>c := {ch: &#39;A&#39;, attr: 7};
703+Fmt.print(&quot;{} of {}\n&quot;, [Fmt.int(n), Fmt.int(m)]);</code></pre>
704+<p><strong>Arrays and strings compare.</strong> Two operands that are
705+each a <code>[]T</code> or a <code>[n]T</code>, of the same element type
706+<code>T</code>, are <code>=</code> when they have the same length and
707+the same elements, and <code>#</code> otherwise, where <code>T</code> is
708+an integer type, <code>BOOL</code>, an enumeration, a set or a pointer
709+type. A string literal compares as a <code>[]U8</code>. The comparison
710+is code of its own where it stands, a check of the lengths and then of
711+the bytes, not a call.</p>
712+<p>Arrays of reals don’t compare this way, since <code>-0.0 = 0.0</code>
713+and a NaN is equal to nothing, though their bytes say otherwise; nor do
714+arrays of records, variants or procedures, nor records and variants
715+themselves. Write a loop. Ordering strings is the library’s
716+<code>Str.cmp</code>.</p>
717+<h2 id="enumerations-and-sets-1">6.6 Enumerations and sets</h2>
718+<p>Enumerations compare with all six relations, by position, and an
719+enumeration can be the variable of a <code>for</code> loop and the
720+expression of a <code>match</code> (section 7.4).</p>
721+<p><code>{red, blue}</code> is a set, of the set type of its names’
722+enumeration; <code>{}</code> is the empty set, where the type it must
723+have is known. On two sets of the same type, <code>+</code> is union,
724+<code>*</code> intersection and <code>-</code> difference;
725+<code>=</code> and <code>#</code> compare, and <code>&lt;=</code> and
726+<code>&gt;=</code> are “is a subset of” and “is a superset of”.
727+<code>x in s</code> is <code>true</code> if <code>x</code>, a value of
728+the enumeration, is in the set <code>s</code>.</p>
729+<h2 id="conversions">6.7 Conversions</h2>
730+<p>A type name applied like a procedure converts: <code>I32(x)</code>,
731+<code>F64(n)</code>, <code>^Cell(addr)</code>. A pointer type written
732+out uses the <code>^</code> form of the grammar:
733+<code>^VOLATILE U32(0FEE00000H)</code>.</p>
734+<table>
735+<colgroup>
736+<col style="width: 33%" />
737+<col style="width: 33%" />
738+<col style="width: 33%" />
739+</colgroup>
740+<thead>
741+<tr class="header">
742+<th>From</th>
743+<th>To</th>
744+<th>Result</th>
745+</tr>
746+</thead>
747+<tbody>
748+<tr class="odd">
749+<td>integer</td>
750+<td>integer</td>
751+<td>the value cut to the new width, or extended by its sign (signed) or
752+zeros (unsigned)</td>
753+</tr>
754+<tr class="even">
755+<td>integer</td>
756+<td><code>F32</code>, <code>F64</code></td>
757+<td>the nearest real</td>
758+</tr>
759+<tr class="odd">
760+<td><code>F32</code>, <code>F64</code></td>
761+<td>integer</td>
762+<td>rounded toward zero; a NaN, or a real out of the range of
763+<code>I64</code>, gives the smallest <code>I64</code>, then cut</td>
764+</tr>
765+<tr class="even">
766+<td><code>F32</code></td>
767+<td><code>F64</code></td>
768+<td>the same value</td>
769+</tr>
770+<tr class="odd">
771+<td><code>F64</code></td>
772+<td><code>F32</code></td>
773+<td>the nearest <code>F32</code>; too big a value gives an infinity</td>
774+</tr>
775+<tr class="even">
776+<td>enumeration</td>
777+<td>integer</td>
778+<td>its position</td>
779+</tr>
780+<tr class="odd">
781+<td>integer</td>
782+<td>enumeration</td>
783+<td>the name at that position; a position out of range is a failed check
784+(6.5)</td>
785+</tr>
786+<tr class="even">
787+<td>set</td>
788+<td>integer</td>
789+<td>its bits</td>
790+</tr>
791+<tr class="odd">
792+<td>integer</td>
793+<td>set</td>
794+<td>the low bits, as many as the set has</td>
795+</tr>
796+<tr class="even">
797+<td><code>INT</code>, <code>UINT</code></td>
798+<td>pointer, procedure</td>
799+<td>the address</td>
800+</tr>
801+<tr class="odd">
802+<td>pointer, procedure</td>
803+<td><code>INT</code>, <code>UINT</code></td>
804+<td>the address</td>
805+</tr>
806+<tr class="even">
807+<td>pointer, procedure</td>
808+<td>pointer, procedure</td>
809+<td>the same address, seen as the new type</td>
810+</tr>
811+</tbody>
812+</table>
813+<p>There are no others: no conversion between <code>BOOL</code> and
814+integers, none between pointers and integers other than <code>INT</code>
815+and <code>UINT</code>, and none to or from records, arrays, slices,
816+variants or <code>ERROR</code>. A conversion of a constant is made by
817+the compiler, by the same rules: <code>U8(300)</code> is 44.</p>
818+<h2 id="built-in-procedures">6.8 Built-in procedures</h2>
819+<table>
820+<colgroup>
821+<col style="width: 50%" />
822+<col style="width: 50%" />
823+</colgroup>
824+<thead>
825+<tr class="header">
826+<th>Call</th>
827+<th>Result</th>
828+</tr>
829+</thead>
830+<tbody>
831+<tr class="odd">
832+<td><code>SIZE(T)</code></td>
833+<td>the size of type <code>T</code> in bytes, an untyped constant;
834+<code>T</code> is a type, not a variable</td>
835+</tr>
836+<tr class="even">
837+<td><code>ADR(x)</code></td>
838+<td>the address of variable <code>x</code>, a <code>^T</code></td>
839+</tr>
840+<tr class="odd">
841+<td><code>LEN(a)</code></td>
842+<td>the number of elements of an array or a string constant (an untyped
843+constant) or of a slice (an <code>INT</code>)</td>
844+</tr>
845+<tr class="even">
846+<td><code>NEW(p, a)</code></td>
847+<td>sets <code>p: ^T</code> to a fresh, zeroed <code>T</code> from the
848+allocator <code>a: ^Mem.Allocator</code>, or to <code>nil</code> if
849+<code>a</code> has no room</td>
850+</tr>
851+<tr class="odd">
852+<td><code>MSG(e)</code></td>
853+<td>the message of the error <code>e</code>, a <code>[]U8</code></td>
854+</tr>
855+<tr class="even">
856+<td><code>COPY(d, s)</code></td>
857+<td>copies the elements of <code>s</code> to the start of
858+<code>d</code>, both arrays or slices of one type <code>T</code>;
859+<code>LEN(s)</code> must be at most <code>LEN(d)</code></td>
860+</tr>
861+<tr class="odd">
862+<td><code>SYSCALL(n, x1, ..., x6)</code></td>
863+<td>Linux system call <code>n</code> with up to six arguments, each a
864+pointer or an integer no wider than <code>INT</code>; the raw result, an
865+<code>INT</code> (a negative number is <code>-errno</code>)</td>
866+</tr>
867+<tr class="even">
868+<td><code>INC(v)</code>, <code>INC(v, k)</code></td>
869+<td>adds 1, or <code>k</code>, to the integer variable <code>v</code>;
870+it wraps, like <code>+</code></td>
871+</tr>
872+<tr class="odd">
873+<td><code>DEC(v)</code>, <code>DEC(v, k)</code></td>
874+<td>subtracts 1, or <code>k</code>, from <code>v</code></td>
875+</tr>
876+<tr class="even">
877+<td><code>FENCE</code></td>
878+<td>a full memory barrier</td>
879+</tr>
880+<tr class="odd">
881+<td><code>CAS(p, old, new)</code></td>
882+<td>if <code>p^ = old</code>, sets <code>p^ := new</code> and returns
883+<code>true</code>; atomically</td>
884+</tr>
885+<tr class="even">
886+<td><code>XCHG(p, x)</code></td>
887+<td>sets <code>p^ := x</code> and returns the value before,
888+atomically</td>
889+</tr>
890+<tr class="odd">
891+<td><code>XADD(p, x)</code></td>
892+<td>adds <code>x</code> to <code>p^</code> and returns the value before,
893+atomically</td>
894+</tr>
895+<tr class="even">
896+<td><code>ARGC()</code></td>
897+<td>the number of command line arguments, an <code>INT</code></td>
898+</tr>
899+<tr class="odd">
900+<td><code>ARGV()</code></td>
901+<td>the arguments, a <code>^^U8</code> (C strings)</td>
902+</tr>
903+</tbody>
904+</table>
905+<p><code>SYSCALL</code> exists only on Linux targets. Other systems have
906+no stable system call interface, and their modules call the C library
907+instead (section 9).</p>
908+<p><code>NEW(p, a)</code> calls <code>a^.alloc(a, SIZE(T), A)</code>,
909+where <code>A</code> is the alignment of <code>T</code>, and zeroes the
910+bytes it gets if they aren’t <code>nil</code>.
911+<code>Mem.Allocator</code> (section 11) is the one type of the library
912+the language names.</p>
913+<p><code>COPY</code> is a statement, as <code>INC</code> is. It copies
914+the bytes of <code>LEN(s)</code> elements as one block, as an assignment
915+of an array does, and is right when <code>d</code> and <code>s</code>
916+overlap, as in <code>COPY(a[1..n], a[0..n - 1])</code>. It is checked as
917+a slice is: past <code>LEN(d)</code>, it stops the program, unless the
918+module says <code>check off;</code>.</p>
919+<p><code>INC</code> and <code>DEC</code> evaluate <code>v</code> once,
920+so <code>INC(a[f(i)])</code> calls <code>f</code> once. They read
921+<code>v</code> and then write it: two accesses, even through a
922+<code>^VOLATILE</code> pointer, and not atomic; <code>XADD</code> is the
923+atomic one.</p>
924+<p>For <code>CAS</code>, <code>XCHG</code> and <code>XADD</code>,
925+<code>p</code> is a <code>^T</code> or <code>^VOLATILE T</code> where
926+<code>T</code> is <code>INT</code>, <code>UINT</code> or a pointer type
927+(<code>INT</code> or <code>UINT</code> for <code>XADD</code>), on every
928+target. They are sequentially consistent, on every target; how a machine
929+without atomic instructions makes them so is the implementation’s choice
930+(appendix A).</p>
931+<h1 id="statements">7. Statements</h1>
932+<pre><code>Statements = { Statement &quot;;&quot; } .
933+Statement = Designator [ &quot;:=&quot; Expr ]
934+ | &quot;_&quot; &quot;:=&quot; Expr
935+ | Target &quot;,&quot; Target { &quot;,&quot; Target } &quot;:=&quot; Expr
936+ | &quot;if&quot; Expr &quot;then&quot; Statements
937+ { &quot;elsif&quot; Expr &quot;then&quot; Statements } [ &quot;else&quot; Statements ] &quot;end&quot;
938+ | &quot;while&quot; Expr &quot;do&quot; Statements &quot;end&quot;
939+ | &quot;repeat&quot; Statements &quot;until&quot; Expr
940+ | &quot;for&quot; ident &quot;:=&quot; Expr &quot;to&quot; Expr [ &quot;by&quot; Expr ] &quot;do&quot; Statements &quot;end&quot;
941+ | &quot;match&quot; Expr &quot;of&quot; { &quot;|&quot; Arm } &quot;end&quot;
942+ | &quot;return&quot; [ Expr { &quot;,&quot; Expr } ]
943+ | &quot;break&quot;
944+ | &quot;defer&quot; Statement .
945+Arm = ( ident [ &quot;(&quot; ident { &quot;,&quot; ident } &quot;)&quot; ]
946+ | Label { &quot;,&quot; Label } | &quot;else&quot; ) &quot;:&quot; Statements .
947+Label = Expr [ &quot;..&quot; Expr ] .
948+Target = Designator | &quot;_&quot; .</code></pre>
949+<p>Every statement ends in <code>;</code>, including the ones that end
950+in <code>end</code> or <code>until</code>.</p>
951+<h2 id="assignment-and-calls">7.1 Assignment and calls</h2>
952+<p><code>x := e</code> evaluates <code>e</code> and stores it in
953+<code>x</code>; arrays, records, slices and variants are copied. A
954+designator on its own is a call, and any result it returns is dropped:
955+<code>Out.ln;</code>, <code>write(fd, buf);</code>.</p>
956+<p><code>a, b := f(x);</code> assigns the results of a procedure that
957+returns several (section 8), in order.</p>
958+<p>The blank, <code>_</code>, drops what would be assigned to it:
959+<code>_ := e;</code> evaluates <code>e</code> and drops it, and
960+<code>n, _ := f(x);</code> keeps the first result and drops the second.
961+What is dropped is still evaluated as any expression is: its calls are
962+made, and a volatile variable in it is read (section 7.6). A result
963+dropped by a call standing on its own is warned of (section 10); one
964+dropped through <code>_</code> isn’t.</p>
965+<h2 id="conditions-and-loops">7.2 Conditions and loops</h2>
966+<p><code>if</code>, <code>while</code> and <code>repeat</code> take
967+<code>BOOL</code> expressions. <code>repeat</code> runs its body at
968+least once and stops when the condition is true.</p>
969+<p><code>for i := a to b by s do ... end;</code> sets <code>i</code> to
970+<code>a</code>, <code>a + s</code>, … while <code>i &lt;= b</code> (or
971+<code>i &gt;= b</code> if <code>s</code> is negative). <code>i</code> is
972+a variable of an integer or enumeration type, named by its name alone;
973+<code>a</code> and then <code>b</code> are evaluated once, before the
974+first round; <code>s</code> is a constant other than zero, and 1 if left
975+out. <code>a</code> and <code>b</code> have <code>i</code>’s type, or
976+are untyped constants that take it, as with <code>+</code> (section
977+6.1): so in <code>for i := 0 to LEN(s) - 1</code>, <code>i</code> must
978+be an <code>INT</code>.</p>
979+<p>The loop never steps past <code>b</code>, so <code>i</code> can’t
980+wrap: afterwards <code>i</code> holds the last value it took, or
981+<code>a</code> if the body never ran. The body may assign
982+<code>i</code>. Precisely, for a positive <code>s</code>: at the end of
983+a round, if <code>i &lt;= b</code> and <code>b - i &lt; s</code>, the
984+loop ends with <code>i</code> as it is; otherwise <code>i</code> becomes
985+<code>i + s</code>, wrapping, and the loop goes on while
986+<code>i &lt;= b</code>. A negative <code>s</code> is the same, the other
987+way round.</p>
988+<p><code>break</code> ends the innermost <code>while</code>,
989+<code>repeat</code> or <code>for</code> it stands in, and the program
990+goes on after it. There is no <code>goto</code>.</p>
991+<h2 id="return">7.3 Return</h2>
992+<p><code>return e;</code> ends the procedure with the result
993+<code>e</code>; <code>return a, b;</code> with several results;
994+<code>return f(x);</code> with the several results of <code>f</code>, if
995+they are the procedure’s own (section 8); <code>return;</code> from a
996+procedure without results. A procedure with results that runs off its
997+end returns zeros, each result as a variable of its type starts (section
998+4).</p>
999+<h2 id="match">7.4 Match</h2>
1000+<pre><code>match s of
1001+| circle(r): area := 3.14159 * r * r;
1002+| rect(w, h): area := w * h;
1003+| dot: area := 0.0;
1004+end;</code></pre>
1005+<p><code>match</code> runs the arm of the case <code>s</code> holds. An
1006+arm may name the case’s fields, in order, all of them; the names stand
1007+for those fields of <code>s</code> itself, so assigning to one changes
1008+<code>s</code>; if <code>s</code> isn’t a variable, they stand for the
1009+fields of a copy. An arm may also leave the fields unnamed. Every case
1010+must have an arm, and only one, unless there is an <code>| else:</code>
1011+arm, which must come last.</p>
1012+<p><code>match</code> on an enumeration works the same way: each arm
1013+names one of the enumeration’s names.</p>
1014+<p><code>match</code> on an integer runs the arm whose labels hold its
1015+value:</p>
1016+<pre><code>match ch of
1017+| &#39; &#39;, &#39;\t&#39;: skip := true;
1018+| &#39;0&#39; .. &#39;9&#39;: digit := true;
1019+| else:
1020+end;</code></pre>
1021+<p>A label is a constant, or <code>lo .. hi</code>, the constants from
1022+<code>lo</code> to <code>hi</code>, both included (unlike a slice, which
1023+leaves <code>hi</code> out). Each must fit the integer’s type, and no
1024+value may be in two labels. An integer <code>match</code> must have an
1025+<code>| else:</code> arm, which may be empty. Whether an arm names a
1026+case or gives labels is set by the type matched: <code>| Max:</code> is
1027+a case of a variant or an enumeration, and a constant for an
1028+integer.</p>
1029+<h2 id="order-of-evaluation">7.5 Order of evaluation</h2>
1030+<p>Operands, arguments and the designators of an assignment are
1031+evaluated from left to right, and the designators of an assignment
1032+before its right side: <code>a[i()] := g()</code> calls <code>i</code>
1033+and then <code>g</code>.</p>
1034+<h2 id="volatile">7.6 Volatile</h2>
1035+<p>A variable reached through a <code>^VOLATILE T</code> is volatile,
1036+and so is any field or element of it. Each read or write of a volatile
1037+variable of a basic, enumeration, set or pointer type is exactly one
1038+memory access, of exactly its size, and volatile accesses happen in the
1039+order the program writes them. Copying a whole record or array through a
1040+volatile pointer is not allowed, since it can’t be one access.</p>
1041+<p>A <code>^VOLATILE T</code> doesn’t convert to <code>^T</code> without
1042+a written conversion.</p>
1043+<p><code>FENCE</code> also orders the program’s ordinary accesses
1044+against those of other processors and devices.</p>
1045+<p><strong>Ordinary variables.</strong> Only volatile accesses,
1046+<code>CAS</code>, <code>XCHG</code>, <code>XADD</code> and
1047+<code>FENCE</code> are exact. Any other access is ordinary: an
1048+implementation may keep a variable in a register, leave out a write that
1049+nothing reads, and change the order of ordinary accesses, as long as
1050+each thread, on its own, does what this report says. A variable that one
1051+thread writes while another reads or writes it, unless both use volatile
1052+accesses or the atomic built-ins, or a <code>FENCE</code> orders them,
1053+holds no particular value then: a read of it gives a value it had or was
1054+given, or a mixture of their bytes. Nothing else of the program becomes
1055+undefined by it.</p>
1056+<h2 id="defer">7.7 Defer</h2>
1057+<p><code>defer s;</code> runs the statement <code>s</code> when the
1058+program leaves the statement list the <code>defer</code> stands in: at
1059+its end, or by a <code>return</code>, or by a <code>break</code> out of
1060+it. Statements deferred in the same list run in the opposite order to
1061+their <code>defer</code>s, and only those whose <code>defer</code> has
1062+been reached run.</p>
1063+<pre><code>module Head;
1064+import Sys, Out;
1065+
1066+(* Prints the first line of the file at path. *)
1067+proc head(path: []U8): BOOL;
1068+var fd, n, i: INT; buf: [256]U8;
1069+begin
1070+ fd := Sys.openRead(path);
1071+ if fd &lt; 0 then return false; end;
1072+ defer Sys.close(fd);
1073+ n := Sys.read(fd, buf);
1074+ if n &lt; 0 then return false; end;
1075+ for i := 0 to n - 1 do
1076+ if buf[i] = 10 then n := i; break; end;
1077+ end;
1078+ Out.str(buf[0..n]);
1079+ Out.ln;
1080+ return true;
1081+end;
1082+
1083+proc main*;
1084+begin
1085+ if not head(&quot;/etc/hostname&quot;) then Out.str(&quot;no hostname\n&quot;); end;
1086+end;
1087+
1088+end Head.</code></pre>
1089+<p><code>return e;</code> evaluates <code>e</code> before the deferred
1090+statements run. A deferred statement can’t contain <code>return</code>,
1091+<code>break</code> or <code>defer</code> that would leave it. A failed
1092+check, <code>Rt.exit</code> and <code>Sys.exit</code> end the program
1093+without running the deferred statements still pending.</p>
1094+<h1 id="procedures">8. Procedures</h1>
1095+<pre><code>ProcDeclaration = &quot;proc&quot; IdentDef Signature &quot;;&quot;
1096+ ( &quot;forward&quot; | { LocalDeclaration } &quot;begin&quot; Statements &quot;end&quot; [ ident ]
1097+ | &quot;code&quot; { [ ident &quot;:&quot; ] [ Instruction ] &quot;;&quot; } &quot;end&quot; [ ident ] ) .
1098+LocalDeclaration = &quot;const&quot; { ident [ &quot;:&quot; Type ] &quot;=&quot; Expr &quot;;&quot; }
1099+ | &quot;type&quot; { ident &quot;=&quot; Type &quot;;&quot; }
1100+ | &quot;var&quot; { ident { &quot;,&quot; ident } &quot;:&quot; Type &quot;;&quot; } .
1101+Signature = &quot;(&quot; [ Params { &quot;;&quot; Params } [ &quot;;&quot; &quot;...&quot; ] ] &quot;)&quot; [ &quot;:&quot; Results ]
1102+ | [ &quot;:&quot; Results ] .
1103+Params = [ &quot;var&quot; ] ident { &quot;,&quot; ident } &quot;:&quot; Type .
1104+Results = Type | &quot;(&quot; Type { &quot;,&quot; Type } &quot;)&quot; .</code></pre>
1105+<p>A procedure without parameters may leave out the parentheses, both
1106+where it is declared (<code>proc main*;</code>) and where it is called
1107+(<code>Out.ln</code>). Used without parentheses in an expression, a
1108+procedure is a procedure value, not a call.</p>
1109+<p><strong>Parameters.</strong> A value parameter is a copy: the
1110+procedure may change it, and the caller’s variable stays as it was. A
1111+<code>var</code> parameter is the caller’s variable itself; its argument
1112+must be a variable of exactly the parameter’s type.</p>
1113+<p><strong>Results.</strong> A procedure returns nothing, one value, or
1114+several. A call that returns several values can only stand on the right
1115+of a multiple assignment, alone as a statement, or alone after
1116+<code>return</code> in a procedure whose results are of the same types,
1117+in the same order, which then returns them as they are:
1118+<code>return divmod(n, 2);</code>.</p>
1119+<pre><code>module Divide;
1120+import Out;
1121+
1122+proc divmod(a, b: I64): (I64, I64);
1123+begin
1124+ return a div b, a mod b;
1125+end;
1126+
1127+proc main*;
1128+var q, r: I64;
1129+begin
1130+ q, r := divmod(17, 5);
1131+ Out.int(q); Out.str(&quot; rest &quot;); Out.int(r); Out.ln;
1132+end;
1133+
1134+end Divide.</code></pre>
1135+<p><strong>forward.</strong> <code>proc f(x: I64): I64; forward;</code>
1136+declares <code>f</code> so that it can be called before its body
1137+appears; the body comes later with the same signature, though its
1138+parameters may have other names.</p>
1139+<p>A name after a procedure’s <code>end</code>, if written, is the
1140+procedure’s own.</p>
1141+<p>Procedures don’t nest.</p>
1142+<p><strong>Code procedures.</strong> A procedure whose body is
1143+<code>code</code> is machine code, written by hand in the target’s
1144+assembly language (appendix A): one instruction to each <code>;</code>,
1145+and labels for jumps, up to <code>end</code>:</p>
1146+<pre><code>proc rdtsc*: U64;
1147+code
1148+ rdtsc;
1149+ shl rdx, 32;
1150+ or rax, rdx;
1151+ ret;
1152+end rdtsc;</code></pre>
1153+<p>A label, <code>name:</code>, names the place of the instruction after
1154+it, within the procedure. A constant of the module may stand wherever a
1155+number does. Any bytes may be written with <code>db</code>, as in
1156+<code>db 0FH, 31H;</code>, for an instruction the assembler doesn’t
1157+know.</p>
1158+<p>It is called like any procedure, with the System V convention:
1159+integer and pointer arguments arrive in <code>rdi</code>,
1160+<code>rsi</code>, <code>rdx</code>, <code>rcx</code>, <code>r8</code>
1161+and <code>r9</code>, <code>F32</code> and <code>F64</code> ones in
1162+<code>xmm0</code> to <code>xmm7</code>, and the result goes in
1163+<code>rax</code> (and <code>rdx</code> for a second) or
1164+<code>xmm0</code>. Its parameters and results are therefore of the types
1165+C can take (section 9), at most six integer and eight real parameters,
1166+and at most two results. The instructions are the whole procedure,
1167+<code>ret</code> included; they must keep <code>rbx</code>,
1168+<code>rbp</code>, <code>rsp</code> and <code>r12</code> to
1169+<code>r15</code> as they found them. The compiler doesn’t look past what
1170+they say.</p>
1171+<h1 id="calling-c">9. Calling C</h1>
1172+<pre><code>ExternDeclaration = &quot;extern&quot; ( &quot;proc&quot; IdentDef Signature
1173+ | &quot;var&quot; IdentDef &quot;:&quot; Type ) .</code></pre>
1174+<pre><code>link &quot;libX11.so.6&quot;;
1175+extern proc XOpenDisplay(name: ^U8): ^U8;
1176+extern proc printf(format: ^U8; ...): I32;
1177+extern var environ: ^^U8;</code></pre>
1178+<p>An <code>extern</code> procedure or variable is found by its name in
1179+the C libraries the program links: the C library always, and each
1180+library named by <code>link</code>.</p>
1181+<p>An <code>extern</code> procedure’s parameters and result are
1182+integers, <code>BOOL</code>, <code>F32</code>, <code>F64</code>,
1183+pointers or procedures; a record goes to C by pointer, never by value.
1184+<code>...</code> accepts any number of further arguments of those types,
1185+and is allowed only in an <code>extern</code>. C takes no
1186+<code>float</code> there, so an <code>F32</code> given to
1187+<code>...</code> is an error: the program writes <code>F64(x)</code>.
1188+Past the parameters, a real constant goes as an <code>F64</code>, an
1189+integer constant as an <code>INT</code>, and a string literal as a
1190+pointer to its bytes followed by a zero byte.</p>
1191+<p>An <code>extern</code> variable is C’s variable itself: reading it
1192+reads C’s, assigning it writes C’s. It can be of any type, declared as C
1193+declares it, a record laid out as C lays it out (section 5.5); the
1194+compiler can’t check that it is. A C variable that is local to each
1195+thread, such as <code>errno</code>, can’t be reached this way; the C
1196+library has a procedure for it (<code>__errno_location</code>).</p>
1197+<p>A C union has no raz type. A program reads one as an array of words,
1198+and the member it wants through a pointer conversion:</p>
1199+<pre><code>var ev: [24]I64; (* XEvent: 192 bytes *)
1200+...
1201+XNextEvent(dpy, ADR(ev));
1202+if I32(ev[0]) = KeyPress then
1203+ key := ^XKeyEvent(ADR(ev));
1204+ ...
1205+end;</code></pre>
1206+<p>Every raz procedure uses the System V calling convention, so a raz
1207+procedure whose parameters and result are of those types can be given to
1208+C as a callback.</p>
1209+<h1 id="programs">10. Programs</h1>
1210+<p>Any module may export a procedure <code>main</code>, declared
1211+<code>proc main*;</code> or <code>proc main*: INT;</code>. The program
1212+runs the bodies of all its modules (section 3), then the main module’s
1213+<code>main</code>, if it has one, and ends with status 0, or with the
1214+status <code>main</code> returns, by calling <code>Rt.exit</code>. The
1215+<code>main</code> of an imported module is an ordinary procedure, which
1216+runs only if something calls it; so a library module can carry a
1217+<code>main</code> that tests it. A program uses C if any of its modules
1218+has an <code>extern</code> declaration or a <code>link</code> of a
1219+library; a module’s other files, read only to check its interface
1220+(section 3), don’t count. A program that doesn’t is a static executable
1221+with no run time besides the code it uses. One that does is linked
1222+dynamically and starts through the C library, so that C’s own set-up and
1223+clean-up run. A program with a module that says
1224+<code>link static;</code> must not use C, and the compiler reports an
1225+error if it does.</p>
1226+<p><strong>Ending.</strong> <code>Rt.exit(status)</code> ends the
1227+program. First it runs the procedures registered with
1228+<code>Rt.atExit</code>, the last registered first, each at most once;
1229+then it ends the program with <code>status</code>, through C’s
1230+<code>exit</code> in a program that uses C, so that C flushes its own
1231+buffers. A failed check (section 6.5) runs them too, after its message,
1232+so that <code>Out</code>’s buffer is written out. <code>Sys.exit</code>
1233+ends the program at once, and a program stopped by a signal runs none of
1234+them.</p>
1235+<pre><code>proc atExit*(p: proc): ERROR; (* Rt.Full if 32 are registered *)
1236+proc exit*(status: INT);
1237+proc trace*; (* the calls under way, on standard error *)</code></pre>
1238+<p><code>Rt.trace</code> says on standard error the calls under way,
1239+from the one that calls it out, each with its variables, as a program
1240+that stops does.</p>
1241+<p>Only what can be reached from <code>main</code> and the module bodies
1242+goes into the executable: unused procedures, variables and strings are
1243+left out. The executable names its procedures and carries line numbers,
1244+so that gdb can stop at a line of the source and show where it is.</p>
1245+<p><strong>Defined behaviour.</strong> Every variable starts at zero,
1246+integers wrap, and indexing arrays and slices is checked, so a program’s
1247+behaviour is defined by this report, with one exception: memory reached
1248+other than through a checked variable. Dereferencing a stale or made-up
1249+pointer, indexing through a pointer, an index out of range in a module
1250+that says <code>check off;</code>, writing a string’s bytes, a code
1251+procedure that breaks its rules, and C code can read or write any
1252+memory, and what follows isn’t defined. Dereferencing <code>nil</code>,
1253+dividing by zero, calling a <code>nil</code> procedure and running out
1254+of stack stop the program, on every target. Before it stops, the program
1255+prints to standard error where it was, as file, line and column, and
1256+why, as far as it can tell: a <code>nil</code> pointer, a
1257+<code>nil</code> procedure, a stack overflow, a bad address or a
1258+division fault; then the calls under way, each with its variables of a
1259+basic, enumeration, set or pointer type. Whether the machine catches
1260+these or the compiled code checks for them is the implementation’s
1261+choice (appendix A); that they stop the program isn’t.</p>
1262+<p><strong>Separate compilation.</strong> An importer depends only on a
1263+module’s interface: what it exports, as an importer sees it, never its
1264+source or its code. So a module can be compiled on its own, and a
1265+program made from modules compiled at different times. Whether an
1266+implementation does so, and into what, is its own choice (appendix A);
1267+one that does makes the same program as if every module were compiled
1268+together, and keeps one copy of each instance.</p>
1269+<p><strong>Messages.</strong> The compiler reports an error with its
1270+file, line and column, the line of source with the offending part
1271+underlined, and, where they help, notes pointing at other places, such
1272+as where a name is declared, and a suggestion, such as a name in reach
1273+spelled almost alike. It goes on after an error to find the others, up
1274+to 20, and writes no executable. After an error it skips the statement
1275+or declaration the error is in and carries on with the next; a name that
1276+isn’t declared is reported once, and nothing that follows from it is
1277+reported. An error in the text itself, such as a string left open, stops
1278+the compile, and so does a module with errors, after it.</p>
1279+<p>The compiler also warns of what is declared and never used: an
1280+import; a constant, type, variable or procedure that isn’t exported; a
1281+parameter of a procedure written in raz. It warns of a variable that is
1282+assigned but never read, too, and of a call standing on its own that
1283+drops a result, as <code>Sys.write(fd, s);</code> does:
1284+<code>_ := Sys.write(fd, s);</code> says it is meant. A name that starts
1285+with <code>_</code> is never warned about. Warnings don’t stop the
1286+compile; <code>raz -Werror</code> makes them errors.</p>
1287+<h1 id="the-library">11. The library</h1>
1288+<p>Modules written in raz, in the compiler’s <code>lib</code>
1289+directory:</p>
1290+<table>
1291+<colgroup>
1292+<col style="width: 50%" />
1293+<col style="width: 50%" />
1294+</colgroup>
1295+<thead>
1296+<tr class="header">
1297+<th>Module</th>
1298+<th>What</th>
1299+</tr>
1300+</thead>
1301+<tbody>
1302+<tr class="odd">
1303+<td><code>Rt</code></td>
1304+<td>ending the program, <code>exit</code> and <code>atExit</code>;
1305+<code>stop</code>, which ends it with a message; <code>trace</code>, the
1306+calls under way; what the compiler’s checks call when they fail</td>
1307+</tr>
1308+<tr class="even">
1309+<td><code>Sys</code></td>
1310+<td>system calls: <code>open</code>, <code>openRead</code>,
1311+<code>create</code>, <code>read</code>, <code>write</code>,
1312+<code>seek</code>, <code>close</code>, <code>mmap</code>,
1313+<code>munmap</code>, <code>isTerminal</code>, <code>yield</code>,
1314+<code>exit</code>; files and directories, <code>rename</code>,
1315+<code>remove</code>, <code>removeDir</code>, <code>makeDir</code>,
1316+<code>chmod</code>, <code>symlink</code>, <code>readLink</code>,
1317+<code>info</code>, <code>openDir</code>, <code>nextEntry</code>,
1318+<code>closeDir</code>; the terminal, <code>rawMode</code>,
1319+<code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the
1320+environment, <code>env</code>, <code>workDir</code>,
1321+<code>setWorkDir</code>; programs, pipes and signals; time,
1322+<code>clock</code>, <code>now</code>, <code>sleep</code>;
1323+<code>random</code>; and sockets. One file per target, and
1324+<code>Sys.raz</code> over the C library for the others</td>
1325+</tr>
1326+<tr class="odd">
1327+<td><code>Out</code></td>
1328+<td>writing text and numbers to standard output, buffered; safe to call
1329+from several threads</td>
1330+</tr>
1331+<tr class="even">
1332+<td><code>C</code></td>
1333+<td>the parts of the C library the others use, and <code>printf</code>,
1334+<code>fflush</code>, <code>stdout</code>, <code>pthread_create</code>; a
1335+program that imports it uses C</td>
1336+</tr>
1337+<tr class="odd">
1338+<td><code>Lock</code></td>
1339+<td>a lock for short sections, for C’s threads and <code>Thread</code>’s
1340+alike</td>
1341+</tr>
1342+<tr class="even">
1343+<td><code>Thread</code></td>
1344+<td>threads for a program without C: <code>spawn</code>,
1345+<code>join</code>, and <code>Mutex</code>; it says
1346+<code>link static</code></td>
1347+</tr>
1348+<tr class="odd">
1349+<td><code>Mem</code></td>
1350+<td><code>Allocator</code>, the interface of every allocator, and pages
1351+from the system</td>
1352+</tr>
1353+<tr class="even">
1354+<td><code>Heap</code></td>
1355+<td>a general allocator, <code>Heap.heap</code>, safe to call from
1356+several threads; freeing a block twice, or one it never gave out, stops
1357+the program</td>
1358+</tr>
1359+<tr class="odd">
1360+<td><code>Arena</code></td>
1361+<td>an allocator that frees everything at once;
1362+<code>ADR(arena.base)</code> is its allocator</td>
1363+</tr>
1364+<tr class="even">
1365+<td><code>Str</code></td>
1366+<td>operations on <code>[]U8</code> strings: comparing, hashing,
1367+finding, UTF-8, reading a number</td>
1368+</tr>
1369+<tr class="odd">
1370+<td><code>Buf</code></td>
1371+<td>a growable byte buffer</td>
1372+</tr>
1373+<tr class="even">
1374+<td><code>Fmt</code></td>
1375+<td>formatted output:
1376+<code>Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])</code>, or
1377+into a <code>Buf</code> with <code>Fmt.write</code></td>
1378+</tr>
1379+<tr class="odd">
1380+<td><code>List</code></td>
1381+<td>a generic singly linked list, <code>List(T)</code></td>
1382+</tr>
1383+<tr class="even">
1384+<td><code>Map</code></td>
1385+<td>a generic hash map, <code>Map(K, V; hash, eq)</code></td>
1386+</tr>
1387+<tr class="odd">
1388+<td><code>Sort</code></td>
1389+<td>sorting a slice in place, <code>Sort(T; less)</code></td>
1390+</tr>
1391+<tr class="even">
1392+<td><code>Math</code></td>
1393+<td>roots, powers, logarithms, rounding and trigonometry, and
1394+<code>pi</code>, for <code>F64</code></td>
1395+</tr>
1396+<tr class="odd">
1397+<td><code>Files</code></td>
1398+<td>an argument as a string, and a whole file read or written</td>
1399+</tr>
1400+<tr class="even">
1401+<td><code>Opt</code></td>
1402+<td>a command line read through one table, by <code>getopt</code>’s
1403+rules with long options; its help and its mistakes</td>
1404+</tr>
1405+</tbody>
1406+</table>
1407+<p>Lengths, counts, file descriptors, process ids and statuses are
1408+<code>INT</code>s, and a character is an <code>I32</code>. What may pass
1409+an <code>INT</code> on a small target is an <code>I64</code>: a file’s
1410+size and position, a time in nanoseconds, and a number read from
1411+text.</p>
1412+<p>What follows is each module’s interface, as
1413+<code>tools/Doc.raz</code> prints it from the module’s file;
1414+<code>...</code> stands for fields that are the module’s own. A module’s
1415+<code>main</code>, where it has one, is its test, and is left out
1416+here.</p>
1417+<pre><code>module Rt;
1418+const Full*: ERROR = &quot;too many procedures to run at exit&quot;;
1419+proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *)
1420+proc exit*(status: INT); (* the atExit procedures, the last first, then the end *)
1421+proc stop*(msg: []U8); (* msg on standard error, then exit with status 2 *)
1422+proc trace*; (* the calls under way, and their variables, on standard error *)</code></pre>
1423+<p><code>Rt</code> also holds what the compiler’s checks call when they
1424+fail, and the handlers of the signals that stop a program (section 10).
1425+<code>exit</code> takes each procedure off the list before it runs it,
1426+so it runs once even if it fails or calls <code>exit</code> itself; in a
1427+program that uses C, it ends through C’s <code>exit</code>, so that C
1428+flushes its buffers too.</p>
1429+<pre><code>module Mem;
1430+type
1431+ Allocator* = record
1432+ alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* nil if out of room *)
1433+ free*: proc(a: ^Allocator; p: ^U8);
1434+ end;
1435+const Page* = 4096;
1436+proc pages*(size: INT): ^U8; (* size bytes, in whole pages, zeroed; or nil *)
1437+proc unmap*(p: ^U8; size: INT); (* pages given back *)
1438+proc alloc*(a: ^Allocator; size, align: INT): ^U8; (* a^.alloc(a, size, align) *)
1439+proc free*(a: ^Allocator; p: ^U8); (* a^.free(a, p) *)
1440+
1441+module Heap;
1442+var heap*: ^Mem.Allocator;
1443+
1444+module Arena;
1445+type Arena* = record
1446+ base*: Mem.Allocator; (* first, so that ^Arena(a) finds the rest *)
1447+ ...
1448+end;
1449+proc init*(ar: ^Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *)
1450+proc reset*(ar: ^Arena); (* every allocation forgotten; the memory used again *)
1451+proc done*(ar: ^Arena); (* its memory given back *)</code></pre>
1452+<p><code>NEW(p, a)</code> calls <code>a^.alloc</code>. <code>Heap</code>
1453+keeps blocks of nine sizes, from 16 to 4096 bytes with their header,
1454+each with a list of the free ones; a bigger block gets pages of its own.
1455+An arena hands out memory from one block: its <code>free</code> does
1456+nothing, and <code>reset</code> forgets everything.</p>
1457+<pre><code>module Lock;
1458+type Lock* = record ... end;
1459+proc take*(l: ^Lock);
1460+proc give*(l: ^Lock);
1461+
1462+module Thread;
1463+const Failed*: ERROR = &quot;can&#39;t start a thread&quot;;
1464+type
1465+ Thread* = record ... end;
1466+ Mutex* = record ... end; (* zero is free *)
1467+proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); (* p(arg) in a new thread *)
1468+proc join*(t: ^Thread); (* waits for t to end, then frees its stack *)
1469+proc lock*(m: ^Mutex);
1470+proc unlock*(m: ^Mutex);</code></pre>
1471+<p>A <code>Lock</code> is for short sections: a thread that finds it
1472+taken tries again, giving up the processor now and then. A
1473+<code>Mutex</code> sleeps. Each of <code>Thread</code>’s threads runs on
1474+a stack of its own, with a page below it that can’t be touched, so that
1475+running off the stack stops the program. A program that uses C makes its
1476+threads with C’s <code>pthread_create</code>, and so can’t use
1477+<code>Thread</code>.</p>
1478+<pre><code>module Out;
1479+proc str*(s: []U8);
1480+proc char*(c: U8);
1481+proc ln*; (* a line feed *)
1482+proc int*(n: I64); (* n in decimal *)
1483+proc hex*(n: U64; digits: INT); (* n in hexadecimal, digits digits at least *)
1484+proc flush*;</code></pre>
1485+<p><strong>Out and C’s stdio.</strong> <code>Out</code> keeps a buffer
1486+of <code>Sys.outSize</code> bytes, 4 KB on Linux. It writes it out when
1487+it is full, at each line feed when standard output is a terminal, at
1488+<code>Out.flush</code>, and when the program ends by
1489+<code>Rt.exit</code>. A lock keeps what each call writes together when
1490+threads write at once. C’s <code>stdio</code> keeps a buffer of its own
1491+for the same standard output. A program that writes with both flushes
1492+one before writing with the other, or the text comes out of order:</p>
1493+<pre><code>module Both;
1494+import Out, C; (* C: printf, fflush and C&#39;s FILE *stdout *)
1495+
1496+proc main*;
1497+begin
1498+ Out.str(&quot;from raz\n&quot;);
1499+ Out.flush;
1500+ C.printf(&quot;from C\n&quot;);
1501+ C.fflush(C.stdout);
1502+ Out.str(&quot;from raz again\n&quot;);
1503+end;
1504+
1505+end Both.</code></pre>
1506+<p><strong>Sys</strong> gives every target the same procedures, so that
1507+a program that needs only these runs wherever raz does: through system
1508+calls on Linux, through the C library elsewhere. A result that can fail
1509+is a number, minus an errno, as on Linux; 0 is success.</p>
1510+<pre><code>module Sys;
1511+const
1512+ stdin* = 0; stdout* = 1; stderr* = 2;
1513+ outSize* = 4096; (* the bytes Out keeps; each target has its own *)
1514+ (* signals, by their numbers on Linux; each target has its own *)
1515+ sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13;
1516+ sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19;
1517+ sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28;
1518+
1519+type
1520+ Access* = (reading, writing, creating, exclusive, truncating, appending);
1521+ Accesses* = set of Access;
1522+ Info* = record
1523+ size*: I64; (* in bytes *)
1524+ mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *)
1525+ mode*: INT; (* its permission bits, as chmod takes them: 1EDH is rwxr-xr-x *)
1526+ dir*: BOOL; (* a directory *)
1527+ file*: BOOL; (* a regular file: not a directory, device, pipe or socket *)
1528+ symlink*: BOOL; (* the path itself is a symbolic link *)
1529+ end;
1530+ Dir* = record ... end; (* an open directory; its fields are the target&#39;s *)
1531+ Program* = record
1532+ path*: []U8;
1533+ args*: [][]U8; (* the first is its name *)
1534+ env*: [][]U8; (* NAME=value, each added to this program&#39;s environment *)
1535+ onlyEnv*: BOOL; (* env is its whole environment *)
1536+ dir*: []U8; (* its working directory; &quot;&quot; for this program&#39;s *)
1537+ fds*: [3]INT; (* its standard input, output and error; 0 for this program&#39;s own *)
1538+ group*: INT; (* the process group it joins: 0 for this program&#39;s, -1 a new one it leads *)
1539+ foreground*: BOOL; (* its group takes the terminal on standard input *)
1540+ tty*: INT; (* a pseudo-terminal&#39;s fd, to start a session on; 0 for none *)
1541+ end;
1542+ Status* = (running, exited, killed, stopped);
1543+
1544+proc write*(fd: INT; s: []U8): INT; (* the bytes written *)
1545+proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *)
1546+proc open*(path: []U8; how: Accesses): INT; (* its fd *)
1547+proc openRead*(path: []U8): INT; (* open(path, {reading}) *)
1548+proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *)
1549+proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *)
1550+proc close*(fd: INT);
1551+proc isTerminal*(fd: INT): BOOL;
1552+proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *)
1553+proc munmap*(p: ^U8; size: INT);
1554+proc yield*; (* the processor given to another thread *)
1555+proc exit*(status: INT); (* the program ended at once, without Rt&#39;s atExit *)
1556+
1557+proc rename*(old, new: []U8): INT; (* replaces what new names *)
1558+proc remove*(path: []U8): INT; (* a file, not a directory *)
1559+proc removeDir*(path: []U8): INT; (* an empty directory *)
1560+proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *)
1561+proc chmod*(path: []U8; mode: INT): INT;
1562+proc symlink*(target, path: []U8): INT; (* path made a link to target *)
1563+proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *)
1564+proc info*(path: []U8): (Info, INT); (* following a symbolic link *)
1565+proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path&#39;s file system *)
1566+
1567+proc openDir*(d: ^Dir; path: []U8): INT;
1568+proc nextEntry*(d: ^Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *)
1569+proc closeDir*(d: ^Dir);
1570+
1571+proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *)
1572+proc restoreMode*(fd: INT); (* as rawMode found it *)
1573+proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *)
1574+proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms &lt; 0 *)
1575+proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *)
1576+proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *)
1577+proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *)
1578+
1579+proc env*(name: []U8): []U8; (* the environment variable&#39;s value; &quot;&quot; if unset *)
1580+proc workDir*: ([]U8, INT); (* the working directory, good until the next call *)
1581+proc setWorkDir*(path: []U8): INT;
1582+
1583+proc pipe*: (INT, INT); (* the fd to read and the fd to write; or minus an errno and -1 *)
1584+proc openPty*: INT; (* a pseudo-terminal: the fd of its controlling side *)
1585+proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *)
1586+proc spawn*(p: Program): INT; (* a program started: its process id *)
1587+proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *)
1588+proc kill*(pid, signal: INT): INT;
1589+proc pid*: INT; (* this program&#39;s process id *)
1590+proc setGroup*(pid, group: INT): INT; (* pid 0 is this program; group 0 a new one pid leads *)
1591+proc foreground*(fd, group: INT): INT; (* the terminal on fd gives its keys to group *)
1592+proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *)
1593+
1594+proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *)
1595+proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *)
1596+proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *)
1597+
1598+proc clock*: I64; (* nanoseconds, on a clock that only goes forward *)
1599+proc now*: I64; (* nanoseconds since 1970-01-01, UTC *)
1600+proc sleep*(ms: INT);
1601+proc random*(b: []U8); (* b filled with random bytes *)
1602+
1603+proc connect*(path: []U8): INT; (* a stream socket, connected to the Unix socket at path: its fd *)
1604+proc connectTcp*(addr: []U8; port: INT): INT; (* addr an IPv4 or IPv6 address, as numbers *)
1605+proc connectUdp*(addr: []U8; port: INT): INT;
1606+proc listen*(path: []U8): INT; (* a Unix socket made at path, taking connections *)
1607+proc listenTcp*(addr: []U8; port: INT): INT;
1608+proc accept*(fd: INT): INT; (* the next connection, waiting for one *)
1609+proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *)</code></pre>
1610+<p><code>info</code> follows a symbolic link: <code>size</code>,
1611+<code>dir</code> and <code>file</code> are those of what it names, and
1612+<code>symlink</code> says the path was a link, so that a program walking
1613+a tree can choose not to follow one back up it. A link to nothing is
1614+<code>-ENOENT</code>, as a missing file is. <code>file</code> lets a
1615+walk open only what holds data: opening a pipe would wait for a program
1616+to write to it.</p>
1617+<p><code>nextEntry</code> gives every name in the directory except
1618+<code>.</code> and <code>..</code>, in no particular order.
1619+<code>rawMode</code> turns off what the terminal does with keys before
1620+the program reads them: echo, waiting for a whole line, Ctrl-C and
1621+Ctrl-Z as signals, Ctrl-S and Ctrl-Q as flow control, and CR to NL; and
1622+with output, NL to CR NL. A program that turns it on restores it before
1623+it ends: <code>Rt.atExit</code> is the place.</p>
1624+<p><strong>Files.</strong> <code>open</code> gives a file for what
1625+<code>how</code> asks: <code>reading</code>, <code>writing</code> or
1626+both; <code>creating</code> it if it is missing, readable by all and
1627+writable by its owner; with <code>exclusive</code>, failing with -17
1628+(EEXIST) if it is there, as a program does that makes a lock or a file
1629+of its own; <code>truncating</code> it to nothing;
1630+<code>appending</code>, each write going to its end, however other
1631+programs write it too. <code>seek</code> moves to <code>pos</code> from
1632+the start of the file, or from its end if <code>fromEnd</code>, where a
1633+negative <code>pos</code> reads back from the end.
1634+<code>Info.mtime</code> is what a program checks to see whether a file
1635+changed under it; <code>Info.mode</code> holds the nine bits of
1636+<code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for
1637+a program, 1A4H (644) for data.</p>
1638+<p><strong>Programs.</strong> <code>openPty</code> makes a
1639+pseudo-terminal, a terminal that a program, a terminal emulator, plays
1640+the screen and the keyboard of: what it writes to the fd is typed, and
1641+what the programs on the terminal write it reads.</p>
1642+<p><code>spawn</code> starts the program at <code>p.path</code>, with
1643+<code>p.args</code> as its arguments, the first being its name. Its
1644+environment is this program’s with each <code>NAME=value</code> of
1645+<code>p.env</code> added or put in place of the old value, or, if
1646+<code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in
1647+<code>p.dir</code>. Its standard input, output and error are
1648+<code>p.fds</code>; a 0 gives it this program’s own, so
1649+<code>{path: "/bin/ls", args: ["ls"]}</code> runs <code>ls</code> as a
1650+shell runs it, with every other field zero. It gets no other open file,
1651+every signal does what it does at first, and none is blocked. If
1652+<code>p.tty</code> is a pseudo-terminal’s fd, the program starts a
1653+session on that terminal: it is its controlling terminal, and its
1654+standard input, output and error, and <code>p.fds</code> and
1655+<code>p.group</code> are not used.</p>
1656+<p><code>p.group</code> puts the program in a process group, the
1657+programs that a shell’s job is made of: -1 makes it the first of a new
1658+group, whose number is its process id, and the other programs of the job
1659+join that number. The terminal gives its keys, and the signals of Ctrl-C
1660+and Ctrl-Z, to one group, the one in its foreground:
1661+<code>p.foreground</code> puts the program’s group there before it
1662+starts, and <code>foreground</code> puts any group there, as a shell
1663+does to take the terminal back. <code>setGroup</code> moves a program to
1664+a group, as the shell does for each program it starts, for whichever of
1665+the two runs first; a shell runs in a group of its own,
1666+<code>setGroup(0, 0)</code>.</p>
1667+<p><code>wait</code> waits for the program <code>pid</code> to change,
1668+or for any of this program’s if <code>pid</code> is -1, and says which
1669+one, and how: <code>exited</code>, with its exit status;
1670+<code>killed</code>, with the number of the signal that ended it; or
1671+<code>stopped</code>, by Ctrl-Z or a signal, with the signal’s number.
1672+Without <code>block</code> it doesn’t wait, and is 0 and
1673+<code>running</code> if no program changed. A program that has nothing
1674+to wait for is -10 (ECHILD). <code>kill</code> sends a program a signal,
1675+<code>sigTerm</code> to end it; to a group, if <code>pid</code> is minus
1676+its number.</p>
1677+<p><strong>Signals.</strong> <code>catch</code> takes the signals
1678+<code>sigs</code> away from what they would do, ending the program most
1679+of them, and gives an fd that is ready to read when one comes, so that a
1680+program waits for signals with <code>ready</code> and
1681+<code>readyOf</code> as it waits for keys or a server.
1682+<code>caught</code> gives each signal that came, in turn. A later
1683+<code>catch</code> adds to the signals caught and gives the same fd.
1684+Catching <code>sigKill</code> or <code>sigStop</code>, or the signals
1685+<code>Rt</code> reports, SIGSEGV and SIGFPE, is -22 (EINVAL).
1686+<code>ignore</code> makes a signal do nothing, as a shell ignores
1687+<code>sigTtou</code> so that it can take its terminal back, and
1688+<code>ignore(sig, false)</code> gives it back what it does at first. A
1689+program that <code>spawn</code> starts begins with neither.</p>
1690+<p><strong>Time.</strong> <code>clock</code> is for measuring how long
1691+something takes: it never goes back, and says nothing of the date.
1692+<code>now</code> is the date and time, which a person or the network may
1693+set back. <code>sleep</code> waits <code>ms</code> milliseconds.</p>
1694+<p><strong>Sockets.</strong> <code>connect</code> is for talking to a
1695+server on the same machine, an X server at
1696+<code>/tmp/.X11-unix/X0</code> say; <code>connectTcp</code> for one
1697+across the network, given its address as numbers,
1698+<code>"127.0.0.1"</code> or <code>"::1"</code>: turning a name into an
1699+address is a library’s work over these. <code>connectUdp</code> gives a
1700+socket of datagrams, each <code>send</code> one message to that address
1701+and each <code>read</code> one message from it, as a program asks a name
1702+server. What a socket receives comes by <code>read</code>, what it sends
1703+goes by <code>send</code>, and <code>close</code> ends it.
1704+<code>send</code> is <code>write</code> for a socket, except when the
1705+other end has closed it: <code>write</code> would stop the program with
1706+the signal SIGPIPE, where <code>send</code> returns -32 (EPIPE), so that
1707+a program learns its server has gone and can end as it wants.
1708+<code>listen</code> and <code>listenTcp</code> are the other side: a
1709+server’s socket, on <code>path</code>, which must not be there yet, or
1710+on <code>port</code> at <code>addr</code>, <code>"0.0.0.0"</code> or
1711+<code>"::"</code> for every address the machine has; <code>accept</code>
1712+gives each connection that comes, as a socket of its own.</p>
1713+<p><code>readyOf</code> waits on several file descriptors at once, as a
1714+program does that listens to a server and a terminal both; a descriptor
1715+at its end, or failed, counts as ready, since a read of it won’t wait.
1716+It looks at the first 64 of <code>fds</code>, and <code>connect</code>
1717+and <code>listen</code> take a path of at most 107 bytes, as
1718+<code>struct sockaddr_un</code> holds; a longer one is -36
1719+(ENAMETOOLONG).</p>
1720+<p>A program that both reads from a descriptor and writes much to it, as
1721+a terminal writes what is pasted to its program while it reads what the
1722+program writes back, must not wait on either: the program may be waiting
1723+for its own writes to be read. <code>setBlocking(fd, false)</code> has a
1724+read or a write on <code>fd</code> that would wait return -11 (EAGAIN)
1725+at once, a write having written what there was room for;
1726+<code>readyFor</code> waits, as <code>readyOf</code> does, for something
1727+to read on any of <code>reads</code> or for room to write on any of
1728+<code>writes</code>. A descriptor in both is given once, when it is
1729+ready for either; the 64 it looks at are <code>reads</code>, then
1730+<code>writes</code>.</p>
1731+<p><strong>Strings, buffers and collections.</strong></p>
1732+<pre><code>module Str;
1733+proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *)
1734+proc eq*(a, b: []U8): BOOL;
1735+proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s&#39;s bytes in memory from a; false if a has none *)
1736+proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *)
1737+proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *)
1738+proc hasPrefix*(s, p: []U8): BOOL;
1739+proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *)
1740+proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *)
1741+proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn&#39;t a number an I64 holds *)
1742+proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *)
1743+proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *)
1744+proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *)
1745+proc putBE*(s: []U8; v: U64); (* v into s, the highest byte first *)
1746+
1747+module Buf;
1748+type Buf* = record len*: INT; ... end;
1749+proc init*(b: ^Buf; a: ^Mem.Allocator);
1750+proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *)
1751+proc add*(b: ^Buf; c: U8);
1752+proc str*(b: ^Buf; s: []U8);
1753+proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *)
1754+proc reset*(b: ^Buf); (* emptied, its memory kept *)
1755+proc done*(b: ^Buf); (* its memory given back *)
1756+
1757+module Fmt;
1758+type Arg* = variant
1759+ int(i: I64);
1760+ uint(u: U64);
1761+ hex(h: U64);
1762+ str(s: []U8);
1763+ char(c: U8);
1764+ bool(b: BOOL);
1765+ real(r: F64; places: INT);
1766+end;
1767+proc print*(f: []U8; args: []Arg); (* to standard output *)
1768+proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *)
1769+
1770+module List(T);
1771+type
1772+ Node* = record next*: ^Node; val*: T; end;
1773+ List* = record head*: ^Node; len*: INT; ... end;
1774+proc init*(l: ^List; a: ^Mem.Allocator);
1775+proc push*(l: ^List; v: T): BOOL; (* v at the front; false if the allocator has no room *)
1776+proc pop*(l: ^List): (T, BOOL); (* the front value, taken off; false if the list is empty *)
1777+proc reverse*(l: ^List);
1778+proc clear*(l: ^List);
1779+
1780+module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);
1781+type Map* = record len*: INT; ... end;
1782+proc init*(m: ^Map; a: ^Mem.Allocator);
1783+proc put*(m: ^Map; k: K; v: V): BOOL; (* k now maps to v; false if there was no room *)
1784+proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn&#39;t there *)
1785+proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *)
1786+proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *)
1787+
1788+module Sort(T; less: proc(a, b: T): BOOL);
1789+proc sort*(s: []T);
1790+
1791+module Math;
1792+const pi* = 3.141592653589793;
1793+proc sqrt*(x: F64): F64;
1794+proc pow*(x, y: F64): F64; (* x to the power y *)
1795+proc floor*(x: F64): F64;
1796+proc ceil*(x: F64): F64;
1797+proc round*(x: F64): F64; (* to the nearest whole number; a half away from 0 *)
1798+proc exp*(x: F64): F64;
1799+proc ln*(x: F64): F64;
1800+proc log2*(x: F64): F64;
1801+proc log10*(x: F64): F64;
1802+proc sin*(x: F64): F64;
1803+proc cos*(x: F64): F64;
1804+proc tan*(x: F64): F64;
1805+proc asin*(x: F64): F64; (* from -pi/2 to pi/2 *)
1806+proc acos*(x: F64): F64; (* from 0 to pi *)
1807+proc atan*(x: F64): F64; (* from -pi/2 to pi/2 *)
1808+proc atan2*(y, x: F64): F64; (* the angle of (x, y) from the x axis, from -pi to pi *)</code></pre>
1809+<p>A <code>Buf</code>, a <code>List</code> and a <code>Map</code> take
1810+their memory from the allocator <code>init</code> gives them.
1811+<code>Fmt</code>’s format takes the next argument at each
1812+<code>{}</code>; <code>{{</code> and <code>}}</code> stand for
1813+<code>{</code> and <code>}</code>. A <code>Map</code> hashes and
1814+compares its keys with the procedures it is given:
1815+<code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>.</p>
1816+<p><code>Str</code> reads and writes UTF-8. <code>encode</code> writes a
1817+character, of at most 10FFFFH, in one to four bytes; a number that isn’t
1818+one becomes FFFDH, the character that stands for a wrong one.
1819+<code>decode</code> reads the first character of <code>s</code>, and
1820+gives FFFDH and 1 for a byte that doesn’t start one rightly, and 0 and 0
1821+for an empty <code>s</code>. <code>toInt</code> reads a decimal number,
1822+with a <code>-</code> or <code>+</code> before it if any, and nothing
1823+else.</p>
1824+<p><code>le</code> and <code>be</code> read an unsigned number of up to
1825+8 bytes in a fixed order, little-endian or big-endian, whatever the
1826+machine’s own, and <code>putLE</code> and <code>putBE</code> write one;
1827+more than 8 bytes stops the program. They are how a file format or a
1828+network protocol is read and written:</p>
1829+<pre><code>len := U32(Str.le(header[4..8]));
1830+Str.putBE(packet[0..2], U64(port));</code></pre>
1831+<p><code>Map.next</code> walks a map: from 0, it gives the next entry
1832+and the number to pass for the one after it, and -1 when there are no
1833+more. A walk sees each entry once if the map doesn’t change during
1834+it:</p>
1835+<pre><code>i, k, v := Names.next(ADR(names), 0);
1836+while i &gt;= 0 do
1837+ Fmt.print(&quot;{} is {}\n&quot;, [Fmt.str(k), Fmt.int(v)]);
1838+ i, k, v := Names.next(ADR(names), i);
1839+end;</code></pre>
1840+<p><code>Sort.sort</code> puts <code>s</code> in the order
1841+<code>less</code> gives, in place, without memory of its own, in time
1842+<code>n log n</code> at most; two elements equal by <code>less</code>
1843+may change places. <code>Math</code>’s procedures are as C’s
1844+<code>math.h</code> has them, to within the last bit or so.
1845+<code>Fmt.real(x, places)</code> writes <code>x</code> with
1846+<code>places</code> digits after the point, at most 9, rounded to the
1847+nearest; from 1e15 on, with an exponent, as <code>1.50e17</code>; and
1848+<code>nan</code>, <code>inf</code> and <code>-inf</code> as
1849+themselves.</p>
1850+<p><strong>Files and Opt</strong> are what most programs need of their
1851+command line.</p>
1852+<pre><code>module Files;
1853+proc arg*(i: INT): []U8; (* argument i; 0 is the program&#39;s name *)
1854+proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *)
1855+proc write*(path, data: []U8): BOOL; (* the file made to hold data *)
1856+
1857+module Opt;
1858+type
1859+ Row* = variant
1860+ usage(scope, line: []U8); (* a synopsis line, without &quot;usage: &quot; *)
1861+ sub(scope, name, help: []U8); (* a subcommand *)
1862+ option(scope: []U8; letter: U8; long, value, help: []U8);
1863+ end;
1864+ Parser* = record
1865+ letter*: U8; (* the option just read *)
1866+ value*: []U8; (* its value; &quot;&quot; for a flag *)
1867+ long*: BOOL; (* it was written by its long name *)
1868+ ...
1869+ end;
1870+proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *)
1871+proc next*(p: ^Parser): BOOL; (* the next option; false when they end *)
1872+proc operand*(p: ^Parser): ([]U8, BOOL); (* the next operand; false when there are no more *)
1873+proc scope*(p: ^Parser; name: []U8); (* subcommand name&#39;s options, from the word after it *)
1874+proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *)
1875+proc fail*(p: ^Parser; text: []U8); (* the program&#39;s own mistake *)</code></pre>
1876+<p><code>Opt</code> reads a command line by the rules of the C library’s
1877+<code>getopt</code>, with GNU’s long options, so that a program’s
1878+options behave as every other Unix program’s. The program describes
1879+itself once, in a table of <code>Row</code>s: its synopsis lines, its
1880+subcommands and its options. The help and the messages for mistakes are
1881+made from the table, so they can’t drift from it; the program’s test of
1882+<code>p.letter</code> is the only other place an option appears. The
1883+parser is a record the program owns, and the table a slice it keeps as
1884+long as the parser: a literal written in the call to <code>init</code>
1885+lives as long as the procedure that makes it.</p>
1886+<p>A row’s <code>scope</code> is <code>""</code> for the program itself,
1887+or a subcommand’s name: <code>"start"</code>, or
1888+<code>"remote.add"</code> for a subcommand of <code>remote</code>. An
1889+option has a <code>letter</code>, a <code>long</code> name, or both. One
1890+with a long name only has a key of its own below <code>'!'</code> for a
1891+letter, so that the program can tell it from the others. With a
1892+<code>value</code>, the name the help and the messages give it, the
1893+option takes a value; with <code>""</code> it is a flag.</p>
1894+<pre><code>const wait = 1; (* the key of --wait, which has no letter *)
1895+...
1896+Opt.init(ADR(p), &quot;keep&quot;, [
1897+ Opt.usage(&quot;&quot;, &quot;keep [-v] [--wait secs] start ...&quot;),
1898+ Opt.sub(&quot;&quot;, &quot;start&quot;, &quot;start a job&quot;),
1899+ Opt.option(&quot;&quot;, &#39;v&#39;, &quot;verbose&quot;, &quot;&quot;, &quot;say more&quot;),
1900+ Opt.option(&quot;&quot;, wait, &quot;wait&quot;, &quot;secs&quot;, &quot;give the job secs to start&quot;),
1901+ Opt.usage(&quot;start&quot;, &quot;keep start [-b label] name command [argument ...]&quot;),
1902+ Opt.option(&quot;start&quot;, &#39;b&#39;, &quot;bar&quot;, &quot;label&quot;, &quot;show the job in the bar as label&quot;)]);
1903+while Opt.next(ADR(p)) do
1904+ if p.letter = &#39;v&#39; then verbose := true;
1905+ elsif p.letter = wait then secs := Opt.number(ADR(p), 1, 600);
1906+ end;
1907+end;
1908+w, ok := Opt.operand(ADR(p));
1909+if not ok or (w # &quot;start&quot;) then Opt.fail(ADR(p), &quot;no command; see keep -h&quot;); end;
1910+Opt.scope(ADR(p), &quot;start&quot;);
1911+while Opt.next(ADR(p)) do
1912+ if p.letter = &#39;b&#39; then bar := p.value; end;
1913+end;
1914+job, ok := Opt.operand(ADR(p));</code></pre>
1915+<p>The rules are <code>getopt</code>’s:</p>
1916+<ul>
1917+<li>Flags may share a dash: <code>-lw</code> is <code>-l -w</code>. A
1918+letter’s value is the rest of its word, <code>-bdev</code>, or the whole
1919+next word, <code>-b dev</code>, even one that starts with a dash or is
1920+empty; in a group, the first letter that takes a value takes the rest,
1921+<code>-qn12</code>.</li>
1922+<li>A long option is written in full, with its value after a
1923+<code>=</code> or in the next word: <code>--bar=dev</code>,
1924+<code>--bar dev</code>. An abbreviation is unknown, not guessed, so that
1925+adding an option never breaks a script. A value given to a flag,
1926+<code>--follow=x</code>, is a mistake.</li>
1927+<li><code>--</code> ends the options, and isn’t an operand;
1928+<code>-</code> alone is one. The options end at the first operand, so in
1929+<code>keep start site python -m http.server</code> the <code>-m</code>
1930+is python’s.</li>
1931+<li>Each scope has its own options. An option given twice is read
1932+twice.</li>
1933+</ul>
1934+<p><code>number</code> reads the value as <code>Str.toInt</code> does.
1935+<code>operand</code> reads what options are left first, and the program
1936+doesn’t see them.</p>
1937+<p><code>-h</code> or <code>--help</code> among a scope’s options prints
1938+its help on standard output and ends the program with status 0.
1939+<code>init</code> and <code>scope</code> answer it, looking through the
1940+scope’s options before <code>next</code> gives one, so that the program
1941+has done nothing yet: <code>keep -v -h -x</code> prints the help, and
1942+the program never sees <code>-v</code>, nor the mistake. The help is the
1943+scope’s synopsis lines, a blank line, and its subcommands, its options
1944+and the help itself, each with its line of help:</p>
1945+<pre><code>usage: keep start [-b label] name command [argument ...]
1946+
1947+ -b, --bar label show the job in the bar as label
1948+ -h, --help print this and exit</code></pre>
1949+<p>A scope with several synopsis lines has the others indented under the
1950+first; one with none, <code>usage:</code> and its words, as
1951+<code>usage: keep start</code>.</p>
1952+<p>A mistake on the command line is a sentence on standard error, the
1953+program’s name first, then the scope’s synopsis lines, as the help
1954+begins, and status 2:</p>
1955+<pre><code>keep: unknown option -x
1956+usage: keep start [-b label] name command [argument ...]</code></pre>
1957+<p>The sentences are <code>unknown option -x</code> (or
1958+<code>--name</code>, up to its <code>=</code>),
1959+<code>-b needs a label</code>, <code>--follow takes no value</code> and
1960+<code>--wait wants a number from 1 to 600, not '0'</code>.
1961+<code>fail</code> writes the program’s own mistakes the same way, but
1962+alone, so that the sentence can say where the help is:
1963+<code>keep: no command; see keep -h</code>.</p>
1964+<p>A mistake in the table is the program’s, not its user’s.
1965+<code>init</code> checks the table before it reads a word, and ends the
1966+program with status 1:
1967+<code>keep: option table, row 3: the option is in the scope already</code>.
1968+The others are an option with neither a letter nor a long name, a long
1969+name with <code>=</code> in it, a letter <code>-</code>, and
1970+<code>-h</code> or <code>--help</code> in the table, since they are
1971+<code>Opt</code>’s.</p>
1972+<h1 id="versions">12. Versions</h1>
1973+<p>A version of this report is named by the month it was made in and a
1974+count: <code>2026-10.0</code>, then <code>2026-10.1</code> for the next
1975+change that month. <code>raz --version</code> says which version the
1976+compiler follows.</p>
1977+<ul>
1978+<li><strong>2026-10.0.</strong> The first numbered version: the language
1979+of compiler 0.3.0.</li>
1980+<li><strong>2026-10.1.</strong> Messages: every error in a run, warnings
1981+of what is never used and <code>-Werror</code> (section 10); failed
1982+checks say what failed, and signals say where (sections 6.5 and
1983+10).</li>
1984+<li><strong>2026-10.2.</strong> <code>Sys</code> gains files and
1985+directories, the terminal, and the environment (section 11).</li>
1986+<li><strong>2026-10.3.</strong> <code>return f(x);</code> passes on the
1987+several results of <code>f</code> (sections 7.3 and 8).
1988+<code>and</code>, <code>or</code> and <code>not</code> are for
1989+<code>BOOL</code>s, and bind below the relations; <code>&amp;</code>,
1990+<code>|</code> and <code>~</code> are the operators on bits, and
1991+<code>xor</code> is for integers only (sections 2 and 6). Arrays and
1992+slices of integers, <code>BOOL</code>s, enumerations, sets and pointers
1993+compare with <code>=</code>, by code where the comparison stands, not by
1994+a call into <code>Rt</code> (section 6.5). A generic module’s instance
1995+is made where it is first used, so its arguments may name types declared
1996+after the imports (section 3).</li>
1997+<li><strong>2026-10.4.</strong> <code>Sys</code> gains a connection to a
1998+Unix socket, waiting on several files at once, pseudo-terminals, and
1999+starting, waiting for and stopping programs (section 11).</li>
2000+<li><strong>2026-10.5.</strong> A module with files in more than one of
2001+the directories searched is an error, unless the first is a link to one
2002+of the others (section 3).</li>
2003+<li><strong>2026-10.6.</strong> <code>Sys.Info</code> says whether the
2004+path is a regular file, and whether it is a symbolic link (section
2005+11).</li>
2006+<li><strong>2026-10.7.</strong> <code>Sys.send</code> writes to a socket
2007+without SIGPIPE (section 11).</li>
2008+<li><strong>2026-10.8.</strong> <code>Sys.workDir</code> returns the
2009+directory, with no <code>var</code> parameter (section 11).</li>
2010+<li><strong>2026-10.9.</strong> The language is frozen (the start of the
2011+report). <code>Sys</code> gains opening a file for any use, seeking,
2012+links, permissions and times; pipes, process groups and the terminal’s
2013+foreground; signals caught on a file descriptor; clocks, sleeping and
2014+random bytes; and network sockets. <code>Sys.spawn</code> takes a
2015+<code>Program</code>, and <code>Sys.wait</code> says which program
2016+changed and how. <code>Str</code> gains UTF-8 and reading a number,
2017+<code>Map</code> removing and walking, <code>Fmt</code> reals;
2018+<code>Sort</code>, <code>Math</code>, <code>Files</code> and
2019+<code>Opt</code> are new (section 11).</li>
2020+<li><strong>2026-10.10.</strong> Said at last: what the compiler did and
2021+the report didn’t say, all through; what an implementation chooses, and
2022+the core every target has (appendix A); the whole grammar (appendix B);
2023+the library’s module names are kept for it (section 3); a record,
2024+variant or enumeration is a type of its own wherever it is written
2025+(section 5.10); a <code>nil</code> pointer, a division by zero and a
2026+stack overflow stop the program on every target (section 10); byte order
2027+is the target’s, and <code>Str</code> reads and writes a fixed one
2028+(sections 5.5 and 11); <code>Out</code>’s buffer is
2029+<code>Sys.outSize</code> bytes (section 11). Changed: a field of a
2030+packed record that may be misaligned has no address and no volatile
2031+access (section 5.5). Mended: arithmetic on untyped constants that
2032+overflows is an error (section 2); a slice of a pointer can’t run
2033+backwards (section 6.4). Ordinary accesses are no longer promised to be
2034+made exactly as written, only volatile and atomic ones (section
2035+7.6).</li>
2036+<li><strong>2026-10.11.</strong> <code>INT</code> and <code>UINT</code>,
2037+as big as an address, are the integers of lengths, slices, pointer
2038+differences, pointer conversions, <code>SYSCALL</code> and
2039+<code>ARGC</code>; <code>SIZE</code> and a constant <code>LEN</code> are
2040+untyped; <code>INT</code> widens silently to <code>I64</code> and
2041+<code>UINT</code> to <code>U64</code>, but neither mixes with them in an
2042+expression or a <code>for</code> (sections 5, 6.1 and 7.2);
2043+<code>Mem.Allocator</code> takes <code>INT</code> sizes. Pointers,
2044+procedures, <code>ERROR</code>s, slices and variants’ tags are sized by
2045+the address, and basic types aligned as the target’s C aligns them
2046+(section 5). Reals are a target’s to have or not (section 5.1). An
2047+exported variable can’t be changed, passed by <code>var</code> or
2048+addressed outside its module, nor an array in it sliced (section 3).
2049+<code>check off;</code> leaves a module’s index and slice checks out
2050+(sections 3, 6.4 and 6.5). An index of any integer type is checked by
2051+its value (6.5). A shift counts modulo its operand’s width, and the
2052+smallest value of every signed type divided by -1 stops the program
2053+(6.1). Writing a string’s bytes isn’t defined (5.10). The atomic
2054+built-ins work on <code>INT</code>, <code>UINT</code> and pointers
2055+(6.8). An integer constant past an extern’s parameters goes as an
2056+<code>INT</code> (9). An importer depends only on a module’s interface,
2057+so a module can be compiled on its own; whether it is, and into what, is
2058+the implementation’s (sections 3 and 10; appendix A). The library takes
2059+and gives <code>INT</code> for lengths, counts, descriptors, process ids
2060+and statuses, <code>main</code>’s and <code>Rt.exit</code>’s among them,
2061+and <code>I32</code> for a character; <code>I64</code> stays for what
2062+may pass an <code>INT</code> on a small target: a file’s size and
2063+position, times, and numbers read from text (section 11). An array
2064+constant has a type and is used as a variable that can’t be changed;
2065+<code>+</code> joins string constants (section 4). <code>match</code>
2066+takes an integer, with constants and ranges as labels (7.4).</li>
2067+<li><strong>2026-10.12.</strong> A code procedure is written in the
2068+target’s assembly language, not as bytes (section 8; appendix A). A
2069+program that stops says the calls under way and their variables, and
2070+<code>Rt.trace</code> says them on demand (sections 10 and 11).</li>
2071+<li><strong>2026-10.13.</strong> <code>Sys</code> gains writing without
2072+waiting, and waiting for room to write (section 11).</li>
2073+<li><strong>2026-10.14.</strong> The blank, <code>_</code>, drops a
2074+value or one of several results (section 7.1); a call that drops a
2075+result is warned of (section 10).</li>
2076+<li><strong>2026-10.15.</strong> <code>Math</code> gains
2077+<code>pow</code>, <code>ceil</code>, <code>round</code>,
2078+<code>log2</code>, <code>log10</code>, <code>tan</code>,
2079+<code>asin</code>, <code>acos</code> and <code>atan</code>;
2080+<code>Sys</code> gains <code>space</code> (section 11).</li>
2081+<li><strong>2026-10.16.</strong> Record constants, and array and record
2082+constants of records, strings, procedures and more (section 4);
2083+<code>Str</code> gains <code>copy</code> (section 11).</li>
2084+<li><strong>2026-10.17.</strong> <code>COPY</code> copies the elements
2085+of one array or slice to another as a block (section 6.8).</li>
2086+</ul>
2087+<h1 id="appendix-a.-what-an-implementation-chooses">Appendix A. What an
2088+implementation chooses</h1>
2089+<p>Everything in this report binds every implementation of raz, except
2090+what follows, which each chooses for itself and documents. This
2091+compiler’s choices are in <code>doc/compiler.md</code>, under “This
2092+compiler’s choices”. The programs in <code>conform/</code> are how an
2093+implementation shows that it follows this report: it passes them all,
2094+its reals aside where its target has none; <code>conform/check.sh</code>
2095+runs them.</p>
2096+<ul>
2097+<li><strong>The target.</strong> Which machines and systems it compiles
2098+for, and the names of their files (<code>linux-x86_64</code>, section
2099+3); for each, the size of an address, and so of <code>INT</code>, and
2100+whether it has reals.</li>
2101+<li><strong>Separate compilation.</strong> Whether modules are compiled
2102+on their own, and if so into what: the object file, its format, the form
2103+of the code in it, when one is out of date, and how an instance is named
2104+so that the program keeps one (section 10). This compiler compiles every
2105+module of a program together, and writes no object files.</li>
2106+<li><strong>Atomics.</strong> How <code>CAS</code>, <code>XCHG</code>
2107+and <code>XADD</code> are made sequentially consistent on a machine
2108+without atomic instructions (section 6.8): on one with a single
2109+processor, by holding off interrupts while they run, for one.</li>
2110+<li><strong>The calling convention.</strong> On x86-64 Linux it is
2111+System V (sections 8 and 9); elsewhere it is the C library’s own, so
2112+that <code>extern</code> procedures and callbacks work. How a procedure
2113+returns several results, and how its frame is laid out, are the
2114+implementation’s.</li>
2115+<li><strong>What is the target’s.</strong> Code procedures (section 8)
2116+hold the target’s machine code, written in its assembly language, and
2117+exist only where the implementation says so; <code>SYSCALL</code> exists
2118+only on Linux targets (section 6.8). A record’s layout is the target’s C
2119+layout (section 5.5), and the order of a value’s bytes is the target’s
2120+(little-endian on x86-64).</li>
2121+<li><strong>The assembly language.</strong> How a code procedure’s
2122+instructions are written (section 8). On x86-64 it is Intel’s syntax, as
2123+NASM writes it: the destination first; registers by name
2124+(<code>rax</code>, <code>eax</code>, <code>al</code>,
2125+<code>xmm0</code>); memory as
2126+<code>[base + index * scale + disp]</code>, with <code>byte</code>,
2127+<code>word</code>, <code>dword</code> or <code>qword</code> before it
2128+where no register says the size; <code>lock</code> before an
2129+instruction. Which instructions it knows is the implementation’s;
2130+<code>db</code> gives any others.</li>
2131+<li><strong>Sizes in the library.</strong> How many bytes
2132+<code>Out</code> keeps before it writes them, <code>Sys.outSize</code>
2133+(section 11): 4 KB on Linux, fewer on a small machine.</li>
2134+<li><strong>The executable.</strong> Its format, and the names and line
2135+numbers it carries for a debugger (section 10).</li>
2136+<li><strong>How a failure is caught.</strong> A <code>nil</code>
2137+pointer, a division by zero or a stack overflow may be caught by the
2138+machine or by checks in the compiled code; either way the program stops
2139+with its message (section 10).</li>
2140+<li><strong>Messages and warnings.</strong> The text of the compiler’s
2141+messages, how many errors it reports before it stops, and which warnings
2142+it gives (section 10). A program that is an error in this report is one
2143+in every implementation; the words that say so are free.</li>
2144+<li><strong>The command line.</strong> How the compiler is run, its
2145+options, and where its library lives (sections 1 and 3).</li>
2146+</ul>
2147+<p><strong>The core.</strong> A target may leave out what the system
2148+under it can’t give: <code>Sys</code> past writing, ending the program
2149+and getting memory, <code>Thread</code>, <code>link</code> and
2150+<code>extern</code>. What it must have, besides the whole language,
2151+reals aside, is the core of the library: <code>Rt</code>,
2152+<code>Out</code>, <code>Mem</code>, <code>Arena</code>,
2153+<code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of
2154+<code>Sys</code> what they need. A program that keeps to the core runs
2155+on every target.</p>
2156+<h1 id="appendix-b.-the-grammar">Appendix B. The grammar</h1>
2157+<p>The whole syntax of raz, gathered from the sections above, with the
2158+symbols of section 2. <code>{ x }</code> is any number of
2159+<code>x</code>, <code>[ x ]</code> is <code>x</code> or nothing, and
2160+<code>( x | y )</code> is one of them.</p>
2161+<pre><code>ident = letter { letter | digit } .
2162+letter = &quot;A&quot; .. &quot;Z&quot; | &quot;a&quot; .. &quot;z&quot; | &quot;_&quot; .
2163+digit = &quot;0&quot; .. &quot;9&quot; .
2164+hexDigit = digit | &quot;A&quot; .. &quot;F&quot; .
2165+number = digit { digit }
2166+ | digit { hexDigit } &quot;H&quot;
2167+ | digit { digit } &quot;.&quot; digit { digit } [ &quot;E&quot; [ &quot;+&quot; | &quot;-&quot; ] digit { digit } ] .
2168+char = &quot;&#39;&quot; ( character | escape ) &quot;&#39;&quot; .
2169+string = &#39;&quot;&#39; { character | escape | &quot;\x&quot; anyHex anyHex } &#39;&quot;&#39; .
2170+escape = &quot;\n&quot; | &quot;\t&quot; | &quot;\r&quot; | &quot;\0&quot; | &quot;\\&quot; | &quot;\&#39;&quot; | &#39;\&quot;&#39;
2171+ | &quot;\u{&quot; anyHex { anyHex } &quot;}&quot; .
2172+anyHex = hexDigit | &quot;a&quot; .. &quot;f&quot; .
2173+
2174+Module = &quot;module&quot; ident [ &quot;(&quot; ModParams &quot;)&quot; ] &quot;;&quot;
2175+ { Import } { Link } [ Check ] { Declaration }
2176+ [ &quot;begin&quot; Statements ] &quot;end&quot; ident &quot;.&quot; .
2177+ModParams = ident { &quot;,&quot; ident } { &quot;;&quot; ident &quot;:&quot; &quot;proc&quot; Signature } .
2178+Import = &quot;import&quot; ImportItem { &quot;,&quot; ImportItem } &quot;;&quot; .
2179+ImportItem = ident [ &quot;:=&quot; ident [ &quot;(&quot; Type { &quot;,&quot; Type } &quot;)&quot; ] ] .
2180+Link = &quot;link&quot; ( string | &quot;static&quot; ) &quot;;&quot; .
2181+Check = &quot;check&quot; &quot;off&quot; &quot;;&quot; .
2182+
2183+Declaration = &quot;const&quot; { IdentDef [ &quot;:&quot; Type ] &quot;=&quot; Expr &quot;;&quot; }
2184+ | &quot;type&quot; { IdentDef &quot;=&quot; Type &quot;;&quot; }
2185+ | &quot;var&quot; { IdentList &quot;:&quot; Type &quot;;&quot; }
2186+ | ProcDeclaration &quot;;&quot;
2187+ | ExternDeclaration &quot;;&quot; .
2188+IdentDef = ident [ &quot;*&quot; ] .
2189+IdentList = IdentDef { &quot;,&quot; IdentDef } .
2190+
2191+Type = QualIdent
2192+ | &quot;[&quot; Expr &quot;]&quot; Type
2193+ | &quot;[&quot; &quot;]&quot; Type
2194+ | &quot;^&quot; [ &quot;VOLATILE&quot; ] Type
2195+ | [ &quot;packed&quot; ] &quot;record&quot; { IdentList &quot;:&quot; Type &quot;;&quot; } &quot;end&quot;
2196+ | &quot;variant&quot; { Case &quot;;&quot; } &quot;end&quot;
2197+ | &quot;(&quot; ident { &quot;,&quot; ident } &quot;)&quot;
2198+ | &quot;set&quot; &quot;of&quot; QualIdent
2199+ | &quot;proc&quot; Signature .
2200+Case = ident [ &quot;(&quot; FieldList { &quot;;&quot; FieldList } &quot;)&quot; ] .
2201+FieldList = ident { &quot;,&quot; ident } &quot;:&quot; Type .
2202+QualIdent = [ ident &quot;.&quot; ] ident .
2203+
2204+Expr = Conjunct { &quot;or&quot; Conjunct } .
2205+Conjunct = Relation { &quot;and&quot; Relation } .
2206+Relation = SimpleExpr [ RelOp SimpleExpr ] .
2207+RelOp = &quot;=&quot; | &quot;#&quot; | &quot;&lt;&quot; | &quot;&lt;=&quot; | &quot;&gt;&quot; | &quot;&gt;=&quot; | &quot;in&quot; .
2208+SimpleExpr = [ &quot;-&quot; ] Term { AddOp Term } .
2209+AddOp = &quot;+&quot; | &quot;-&quot; | &quot;|&quot; | &quot;xor&quot; .
2210+Term = Factor { MulOp Factor } .
2211+MulOp = &quot;*&quot; | &quot;/&quot; | &quot;div&quot; | &quot;mod&quot; | &quot;&amp;&quot; | &quot;shl&quot; | &quot;shr&quot; .
2212+Factor = number | char | string | Designator | &quot;(&quot; Expr &quot;)&quot;
2213+ | &quot;not&quot; Factor | &quot;~&quot; Factor
2214+ | &quot;[&quot; [ Expr { &quot;,&quot; Expr } ] &quot;]&quot;
2215+ | &quot;{&quot; [ Element { &quot;,&quot; Element } ] &quot;}&quot;
2216+ | &quot;^&quot; [ &quot;VOLATILE&quot; ] Type &quot;(&quot; Expr &quot;)&quot; .
2217+Element = Expr | ident &quot;:&quot; Expr .
2218+Designator = QualIdent { Selector } .
2219+Selector = &quot;.&quot; ident | &quot;^&quot; | &quot;[&quot; Expr [ &quot;..&quot; Expr ] &quot;]&quot;
2220+ | &quot;(&quot; [ Expr { &quot;,&quot; Expr } ] &quot;)&quot; .
2221+
2222+Statements = { Statement &quot;;&quot; } .
2223+Statement = Designator [ &quot;:=&quot; Expr ]
2224+ | &quot;_&quot; &quot;:=&quot; Expr
2225+ | Target &quot;,&quot; Target { &quot;,&quot; Target } &quot;:=&quot; Expr
2226+ | &quot;if&quot; Expr &quot;then&quot; Statements
2227+ { &quot;elsif&quot; Expr &quot;then&quot; Statements } [ &quot;else&quot; Statements ] &quot;end&quot;
2228+ | &quot;while&quot; Expr &quot;do&quot; Statements &quot;end&quot;
2229+ | &quot;repeat&quot; Statements &quot;until&quot; Expr
2230+ | &quot;for&quot; ident &quot;:=&quot; Expr &quot;to&quot; Expr [ &quot;by&quot; Expr ] &quot;do&quot; Statements &quot;end&quot;
2231+ | &quot;match&quot; Expr &quot;of&quot; { &quot;|&quot; Arm } &quot;end&quot;
2232+ | &quot;return&quot; [ Expr { &quot;,&quot; Expr } ]
2233+ | &quot;break&quot;
2234+ | &quot;defer&quot; Statement .
2235+Arm = ( ident [ &quot;(&quot; ident { &quot;,&quot; ident } &quot;)&quot; ]
2236+ | Label { &quot;,&quot; Label } | &quot;else&quot; ) &quot;:&quot; Statements .
2237+Label = Expr [ &quot;..&quot; Expr ] .
2238+Target = Designator | &quot;_&quot; .
2239+
2240+ProcDeclaration = &quot;proc&quot; IdentDef Signature &quot;;&quot;
2241+ ( &quot;forward&quot; | { LocalDeclaration } &quot;begin&quot; Statements &quot;end&quot; [ ident ]
2242+ | &quot;code&quot; { [ ident &quot;:&quot; ] [ Instruction ] &quot;;&quot; } &quot;end&quot; [ ident ] ) .
2243+LocalDeclaration = &quot;const&quot; { ident [ &quot;:&quot; Type ] &quot;=&quot; Expr &quot;;&quot; }
2244+ | &quot;type&quot; { ident &quot;=&quot; Type &quot;;&quot; }
2245+ | &quot;var&quot; { ident { &quot;,&quot; ident } &quot;:&quot; Type &quot;;&quot; } .
2246+Signature = &quot;(&quot; [ Params { &quot;;&quot; Params } [ &quot;;&quot; &quot;...&quot; ] ] &quot;)&quot; [ &quot;:&quot; Results ]
2247+ | [ &quot;:&quot; Results ] .
2248+Params = [ &quot;var&quot; ] ident { &quot;,&quot; ident } &quot;:&quot; Type .
2249+Results = Type | &quot;(&quot; Type { &quot;,&quot; Type } &quot;)&quot; .
2250+ExternDeclaration = &quot;extern&quot; ( &quot;proc&quot; IdentDef Signature
2251+ | &quot;var&quot; IdentDef &quot;:&quot; Type ) .</code></pre>
2252+<p><code>character</code> is any character but a line break, and in a
2253+string any but <code>"</code> and <code>\</code>, in a character any but
2254+<code>'</code> and <code>\</code>. A procedure argument of a generic
2255+module (section 3) is written as a <code>QualIdent</code>, which is a
2256+<code>Type</code> here. <code>...</code> comes last in a
2257+<code>Signature</code>, and only in an <code>extern</code>. An
2258+<code>Instruction</code> is written in the target’s assembly language
2259+(appendix A).</p>
2260+</div>
2261+</main>
2262+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
2263+</body>
2264+</html>

site/public/style.css (history) added +88 -0

@@ -0,0 +1,88 @@
1+:root {
2+ --bg: #fbfaf7; --fg: #1b1b1a; --muted: #6b6a66; --line: #e2e0d9;
3+ --accent: #b4401f; --code-bg: #f2f0ea; --out-bg: #1f2124; --out-fg: #d8dadc;
4+ --kw: #8a2d86; --ty: #1c6c8c; --str: #3b7a2a; --com: #8b8a84; --num: #b4401f;
5+}
6+@media (prefers-color-scheme: dark) {
7+ :root {
8+ --bg: #17181a; --fg: #e6e4de; --muted: #9a988f; --line: #2d2f33;
9+ --accent: #ee8a63; --code-bg: #202226; --out-bg: #0f1012; --out-fg: #cfd1d4;
10+ --kw: #d291cf; --ty: #6cc1e2; --str: #9bd47f; --com: #7c7b75; --num: #ee8a63;
11+ }
12+}
13+* { box-sizing: border-box; }
14+html { -webkit-text-size-adjust: 100%; }
15+body {
16+ margin: 0; background: var(--bg); color: var(--fg);
17+ font: 17px/1.6 Charter, "Bitstream Charter", Georgia, serif;
18+}
19+a { color: var(--accent); }
20+header { border-bottom: 1px solid var(--line); }
21+header nav, main, footer { max-width: 62rem; margin: 0 auto; padding: 0 16px; }
22+header nav { display: flex; flex-wrap: wrap; gap: 4px 20px; padding-top: 14px; padding-bottom: 14px;
23+ font-family: system-ui, sans-serif; font-size: 15px; }
24+header nav a { color: var(--fg); text-decoration: none; }
25+header nav a:first-child { font: 700 20px ui-monospace, monospace; color: var(--accent); }
26+header nav a[aria-current] { border-bottom: 2px solid var(--accent); }
27+header nav .repo { margin-left: auto; color: var(--muted); }
28+main { padding-top: 28px; padding-bottom: 48px; }
29+footer { color: var(--muted); font-size: 14px; padding-bottom: 32px; font-family: system-ui, sans-serif; }
30+h1 { overflow-wrap: anywhere; font-size: 2.1rem; line-height: 1.2; margin: .2em 0 .5em; }
31+h2 { font-size: 1.4rem; margin: 1.8em 0 .4em; }
32+code, pre { font: 14.5px/1.5 ui-monospace, "DejaVu Sans Mono", Menlo, monospace; }
33+code { background: var(--code-bg); padding: 1px 4px; border-radius: 3px; }
34+pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
35+pre code { background: none; padding: 0; }
36+pre.code { background: var(--code-bg); border: 1px solid var(--line); border-radius: 0 0 6px 6px; }
37+.file { font: 12px ui-monospace, monospace; color: var(--muted); background: var(--code-bg);
38+ border: 1px solid var(--line); border-bottom: 0; border-radius: 6px 6px 0 0;
39+ padding: 4px 14px; margin-top: 1.2em; }
40+pre.output { background: var(--out-bg); color: var(--out-fg); border-radius: 6px; margin: 8px 0 1.2em; }
41+pre.output::before { content: "output"; display: block; font: 11px system-ui, sans-serif;
42+ text-transform: uppercase; letter-spacing: .08em; color: #8b8e93; margin-bottom: 6px; }
43+pre .err { color: #ff9a85; }
44+.kw { color: var(--kw); font-weight: 600; } .ty { color: var(--ty); }
45+.string { color: var(--str); } .comment { color: var(--com); font-style: italic; }
46+.number { color: var(--num); }
47+
48+/* the home page */
49+.hero { padding: 24px 0 8px; }
50+.hero h1 { font-size: 3.4rem; font-family: ui-monospace, monospace; color: var(--accent); margin: 0; }
51+.hero .tag { font-size: 1.35rem; margin: .2em 0 1em; }
52+.hero .cta { display: flex; flex-wrap: wrap; gap: 10px; margin: 1em 0; font-family: system-ui, sans-serif; }
53+.btn { display: inline-block; padding: 9px 18px; border-radius: 6px; text-decoration: none;
54+ border: 1px solid var(--accent); color: var(--accent); font-weight: 600; font-size: 15px; }
55+.btn.primary { background: var(--accent); color: var(--bg); }
56+.cols { display: grid; grid-template-columns: minmax(0, 1fr); gap: 24px; }
57+@media (min-width: 52rem) { .cols { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
58+.features { padding-left: 1.2em; } .features li { margin: .35em 0; }
59+.facts { display: flex; flex-wrap: wrap; gap: 8px 28px; margin: 1.4em 0; font-family: system-ui, sans-serif;
60+ font-size: 15px; color: var(--muted); }
61+.facts b { color: var(--fg); }
62+
63+/* the tour */
64+.contents { max-width: 44rem; }
65+.contents ol { padding-left: 1.6em; } .contents li { margin: .5em 0; }
66+.contents li span { display: block; color: var(--muted); font-size: 15px; }
67+.tour { display: grid; grid-template-columns: minmax(0, 1fr); gap: 24px; }
68+@media (min-width: 56rem) { .tour { grid-template-columns: 14rem minmax(0, 1fr); gap: 40px; } }
69+.tour aside { font-family: system-ui, sans-serif; font-size: 14px; }
70+.tour aside h3 { margin: 1em 0 .2em; font-size: 12px; text-transform: uppercase;
71+ letter-spacing: .08em; color: var(--muted); }
72+.tour aside ol { margin: 0; padding-left: 1.5em; } .tour aside a { color: var(--fg); text-decoration: none; }
73+.tour aside a[aria-current] { color: var(--accent); font-weight: 700; }
74+@media (max-width: 55.99rem) { .tour aside { order: 2; border-top: 1px solid var(--line); } }
75+.tour article { min-width: 0; }
76+.kicker { font: 13px system-ui, sans-serif; color: var(--muted); text-transform: uppercase;
77+ letter-spacing: .08em; margin: 0; }
78+.tryit { color: var(--muted); font-size: 15px; }
79+.pager { display: flex; flex-wrap: wrap; gap: 8px 24px; margin-top: 2em; padding-top: 14px;
80+ border-top: 1px solid var(--line); font-family: system-ui, sans-serif; font-size: 15px; }
81+.pager .next { margin-left: auto; font-weight: 700; }
82+
83+/* the report */
84+.spec { max-width: 46rem; }
85+.spec pre { background: var(--code-bg); border-left: 2px solid var(--line); white-space: pre-wrap; }
86+.spec table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 15px; }
87+.spec td, .spec th { border-bottom: 1px solid var(--line); padding: 3px 10px; text-align: left; }
88+.spec nav#TOC ul { list-style: none; padding-left: 1em; }

site/public/tour/c.html (history) added +70 -0

@@ -0,0 +1,70 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Talking to C - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html" aria-current="page">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Go &middot; lesson 13 of 14</p><h1>Talking to C</h1>
32+<div class="file"><span>Qsort.raz</span></div><pre class="code"><code><span class="kw">module</span> Qsort;
33+<span class="kw">import</span> Out, Fmt;
34+
35+<span class="kw">link</span> <span class="string">&quot;libm.so.6&quot;</span>;
36+
37+<span class="kw">extern</span> <span class="kw">proc</span> qsort(base: ^<span class="ty">U8</span>; n, size: <span class="ty">UINT</span>;
38+ less: <span class="kw">proc</span>(a, b: ^<span class="ty">U8</span>): <span class="ty">I32</span>);
39+<span class="kw">extern</span> <span class="kw">proc</span> cbrt(x: <span class="ty">F64</span>): <span class="ty">F64</span>;
40+
41+<span class="comment">(* the order of two INTs, as C&#x27;s qsort wants it *)</span>
42+<span class="kw">proc</span> order(a, b: ^<span class="ty">U8</span>): <span class="ty">I32</span>;
43+<span class="kw">var</span> x, y: ^<span class="ty">INT</span>;
44+<span class="kw">begin</span>
45+ x := ^<span class="ty">INT</span>(a);
46+ y := ^<span class="ty">INT</span>(b);
47+ <span class="kw">if</span> x^ &lt; y^ <span class="kw">then</span> <span class="kw">return</span> -<span class="number">1</span>; <span class="kw">elsif</span> x^ &gt; y^ <span class="kw">then</span> <span class="kw">return</span> <span class="number">1</span>; <span class="kw">end</span>;
48+ <span class="kw">return</span> <span class="number">0</span>;
49+<span class="kw">end</span>;
50+
51+<span class="kw">proc</span> main*;
52+<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
53+<span class="kw">begin</span>
54+ a := [<span class="number">30</span>, <span class="number">10</span>, <span class="number">50</span>, <span class="number">20</span>, <span class="number">40</span>];
55+ qsort(^<span class="ty">U8</span>(<span class="ty">ADR</span>(a)), <span class="ty">LEN</span>(a), <span class="ty">SIZE</span>(<span class="ty">INT</span>), order);
56+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> Out.int(a[i]); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
57+ Out.ln;
58+ Fmt.print(<span class="string">&quot;{}\n&quot;</span>, [Fmt.real(cbrt(<span class="number">27.0</span>), <span class="number">3</span>)]);
59+<span class="kw">end</span>;
60+
61+<span class="kw">end</span> Qsort.</code></pre>
62+<pre class="output"><code>10 20 30 40 50
63+3.000
64+</code></pre>
65+<p>An <code>extern</code> procedure is found by its name in the C library, or in a library a <code>link</code> line names. Its parameters are integers, reals, pointers and procedures, as C declares them; a raz procedure of such types can be given to C to call back, as <code>order</code> is.</p>
66+<p>A program that uses C is linked against it dynamically and starts through it; one that doesn't is a static executable with nothing in it but its own code.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="generics.html">&larr; Generic modules</a><a href="index.html">Contents</a><a class="next" href="threads.html">Threads &rarr;</a></div></article></div>
67+</main>
68+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
69+</body>
70+</html>

site/public/tour/checks.html (history) added +63 -0

@@ -0,0 +1,63 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>When it goes wrong - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html" aria-current="page">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Go &middot; lesson 11 of 14</p><h1>When it goes wrong</h1>
32+<div class="file"><span>Fail.raz</span></div><pre class="code"><code><span class="kw">module</span> Fail;
33+<span class="kw">import</span> Out;
34+
35+<span class="kw">proc</span> get(a: []<span class="ty">INT</span>; i: <span class="ty">INT</span>): <span class="ty">INT</span>;
36+<span class="kw">begin</span>
37+ <span class="kw">return</span> a[i];
38+<span class="kw">end</span>;
39+
40+<span class="kw">proc</span> main*;
41+<span class="kw">var</span> a: [<span class="number">3</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
42+<span class="kw">begin</span>
43+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span>
44+ Out.int(get(a, i));
45+ Out.ln;
46+ <span class="kw">end</span>;
47+<span class="kw">end</span>;
48+
49+<span class="kw">end</span> Fail.</code></pre>
50+<pre class="output"><code>0
51+0
52+0
53+<span class="err">Fail.raz:6:14: index 3 is out of range for a length of 3
54+ in Fail.get at Fail.raz:6:14: i = 3
55+ in Fail.main at Fail.raz:13:9: i = 3
56+</span></code></pre>
57+<p>Every index and slice is checked. A failed check says where it failed and with what values, then the calls under way with their variables, and ends the program with status 2. So do a <code>nil</code> pointer, a division by zero and a stack overflow.</p>
58+<p>A module that is sure of its indexes may say <code>check off;</code> after its imports, for speed; an index out of range there reads or writes whatever it reaches.</p>
59+<p>The program carries line numbers for gdb, and <code>RAZ_PROFILE=1</code> in its environment has it say where its time went.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="defer.html">&larr; Defer</a><a href="index.html">Contents</a><a class="next" href="generics.html">Generic modules &rarr;</a></div></article></div>
60+</main>
61+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
62+</body>
63+</html>

site/public/tour/defer.html (history) added +85 -0

@@ -0,0 +1,85 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Defer - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html" aria-current="page">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Go &middot; lesson 10 of 14</p><h1>Defer</h1>
32+<div class="file"><span>Defers.raz</span></div><pre class="code"><code><span class="kw">module</span> Defers;
33+<span class="kw">import</span> Out, Sys;
34+
35+<span class="kw">proc</span> step(n: <span class="ty">INT</span>);
36+<span class="kw">begin</span>
37+ Out.str(<span class="string">&quot;in &quot;</span>); Out.int(n); Out.ln;
38+ <span class="kw">defer</span> Out.str(<span class="string">&quot;out\n&quot;</span>);
39+ <span class="kw">if</span> n = <span class="number">2</span> <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>;
40+ <span class="kw">defer</span> Out.str(<span class="string">&quot;done\n&quot;</span>);
41+ Out.str(<span class="string">&quot;working\n&quot;</span>);
42+<span class="kw">end</span>;
43+
44+<span class="comment">(* the lines of the file at path, or -1 if it can&#x27;t be read *)</span>
45+<span class="kw">proc</span> lines(path: []<span class="ty">U8</span>): <span class="ty">INT</span>;
46+<span class="kw">var</span> fd, n, i, count: <span class="ty">INT</span>; buf: [<span class="number">4096</span>]<span class="ty">U8</span>;
47+<span class="kw">begin</span>
48+ fd := Sys.openRead(path);
49+ <span class="kw">if</span> fd &lt; <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> -<span class="number">1</span>; <span class="kw">end</span>;
50+ <span class="kw">defer</span> Sys.close(fd);
51+ count := <span class="number">0</span>;
52+ n := Sys.read(fd, buf);
53+ <span class="kw">while</span> n &gt; <span class="number">0</span> <span class="kw">do</span>
54+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> n - <span class="number">1</span> <span class="kw">do</span>
55+ <span class="kw">if</span> buf[i] = <span class="string">&#x27;\n&#x27;</span> <span class="kw">then</span> <span class="ty">INC</span>(count); <span class="kw">end</span>;
56+ <span class="kw">end</span>;
57+ n := Sys.read(fd, buf);
58+ <span class="kw">end</span>;
59+ <span class="kw">return</span> count;
60+<span class="kw">end</span>;
61+
62+<span class="kw">proc</span> main*;
63+<span class="kw">begin</span>
64+ step(<span class="number">1</span>);
65+ step(<span class="number">2</span>);
66+ Out.int(lines(<span class="string">&quot;/dev/null&quot;</span>)); Out.ln;
67+ Out.int(lines(<span class="string">&quot;/no/such/file&quot;</span>)); Out.ln;
68+<span class="kw">end</span>;
69+
70+<span class="kw">end</span> Defers.</code></pre>
71+<pre class="output"><code>in 1
72+working
73+done
74+out
75+in 2
76+out
77+0
78+-1
79+</code></pre>
80+<p><code>defer s;</code> runs <code>s</code> when the program leaves the statements the <code>defer</code> stands in: at their end, or by a <code>return</code> or a <code>break</code>. Deferred statements run the last first, and only those whose <code>defer</code> was reached.</p>
81+<p>So a file is closed beside the line that opens it, however the procedure ends.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="errors.html">&larr; Errors</a><a href="index.html">Contents</a><a class="next" href="checks.html">When it goes wrong &rarr;</a></div></article></div>
82+</main>
83+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
84+</body>
85+</html>

site/public/tour/errors.html (history) added +73 -0

@@ -0,0 +1,73 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Errors - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html" aria-current="page">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Go &middot; lesson 9 of 14</p><h1>Errors</h1>
32+<div class="file"><span>Errors.raz</span></div><pre class="code"><code><span class="kw">module</span> Errors;
33+<span class="kw">import</span> Out, Str;
34+
35+<span class="kw">const</span>
36+ Empty*: <span class="ty">ERROR</span> = <span class="string">&quot;nothing to read&quot;</span>;
37+ TooBig*: <span class="ty">ERROR</span> = <span class="string">&quot;not a number up to 1000&quot;</span>;
38+
39+<span class="comment">(* the number s holds, up to 1000 *)</span>
40+<span class="kw">proc</span> number(s: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
41+<span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>;
42+<span class="kw">begin</span>
43+ <span class="kw">if</span> <span class="ty">LEN</span>(s) = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Empty; <span class="kw">end</span>;
44+ n, ok := Str.toInt(s);
45+ <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">or</span> (n &gt; <span class="number">1000</span>) <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, TooBig; <span class="kw">end</span>;
46+ <span class="kw">return</span> <span class="ty">INT</span>(n), <span class="ty">nil</span>;
47+<span class="kw">end</span>;
48+
49+<span class="kw">proc</span> main*;
50+<span class="kw">var</span> words: [<span class="number">3</span>][]<span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>;
51+<span class="kw">begin</span>
52+ words := [<span class="string">&quot;42&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;12345&quot;</span>];
53+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span>
54+ n, err := number(words[i]);
55+ <span class="kw">if</span> err = <span class="ty">nil</span> <span class="kw">then</span> Out.int(n);
56+ <span class="kw">elsif</span> err = Empty <span class="kw">then</span> Out.str(<span class="string">&quot;empty&quot;</span>);
57+ <span class="kw">else</span> Out.str(<span class="ty">MSG</span>(err));
58+ <span class="kw">end</span>;
59+ Out.ln;
60+ <span class="kw">end</span>;
61+<span class="kw">end</span>;
62+
63+<span class="kw">end</span> Errors.</code></pre>
64+<pre class="output"><code>42
65+empty
66+not a number up to 1000
67+</code></pre>
68+<p>A procedure that can fail returns an <code>ERROR</code> beside its result, and <code>nil</code> when it didn't. Each <code>ERROR</code> constant is an error of its own, compared with <code>=</code> by which declaration made it, never by its message; <code>MSG(err)</code> is the message.</p>
69+<p>An exported one, as <code>Empty</code> is here, lets the callers tell it from the others. raz has no exceptions.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="memory.html">&larr; Memory and allocators</a><a href="index.html">Contents</a><a class="next" href="defer.html">Defer &rarr;</a></div></article></div>
70+</main>
71+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
72+</body>
73+</html>

site/public/tour/generics.html (history) added +83 -0

@@ -0,0 +1,83 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Generic modules - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html" aria-current="page">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Go &middot; lesson 12 of 14</p><h1>Generic modules</h1>
32+<p>A module may take types, and the procedures it needs on them, as parameters. <code>Biggest.raz</code>:</p>
33+<div class="file"><span>Biggest.raz</span></div><pre class="code"><code><span class="kw">module</span> Biggest(T; less: <span class="kw">proc</span>(a, b: T): <span class="ty">BOOL</span>);
34+
35+<span class="comment">(* the biggest of s, which mustn&#x27;t be empty *)</span>
36+<span class="kw">proc</span> max*(s: []T): T;
37+<span class="kw">var</span> i: <span class="ty">INT</span>; m: T;
38+<span class="kw">begin</span>
39+ m := s[<span class="number">0</span>];
40+ <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span>
41+ <span class="kw">if</span> less(m, s[i]) <span class="kw">then</span> m := s[i]; <span class="kw">end</span>;
42+ <span class="kw">end</span>;
43+ <span class="kw">return</span> m;
44+<span class="kw">end</span>;
45+
46+<span class="kw">end</span> Biggest.</code></pre>
47+<p>and a program that uses it:</p>
48+<div class="file"><span>Generics.raz</span></div><pre class="code"><code><span class="kw">module</span> Generics;
49+<span class="kw">import</span> Out, Ints := Biggest(<span class="ty">INT</span>, before),
50+ Words := Biggest([]<span class="ty">U8</span>, shorter), Order := Sort(<span class="ty">INT</span>, before);
51+
52+<span class="kw">proc</span> before(a, b: <span class="ty">INT</span>): <span class="ty">BOOL</span>;
53+<span class="kw">begin</span>
54+ <span class="kw">return</span> a &lt; b;
55+<span class="kw">end</span>;
56+
57+<span class="kw">proc</span> shorter(a, b: []<span class="ty">U8</span>): <span class="ty">BOOL</span>;
58+<span class="kw">begin</span>
59+ <span class="kw">return</span> <span class="ty">LEN</span>(a) &lt; <span class="ty">LEN</span>(b);
60+<span class="kw">end</span>;
61+
62+<span class="kw">proc</span> main*;
63+<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
64+<span class="kw">begin</span>
65+ a := [<span class="number">3</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="number">5</span>];
66+ Out.int(Ints.max(a)); Out.ln;
67+ Out.str(Words.max([<span class="string">&quot;a&quot;</span>, <span class="string">&quot;tree&quot;</span>, <span class="string">&quot;is&quot;</span>])); Out.ln;
68+ Order.sort(a);
69+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> Out.int(a[i]); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
70+ Out.ln;
71+<span class="kw">end</span>;
72+
73+<span class="kw">end</span> Generics.</code></pre>
74+<pre class="output"><code>5
75+tree
76+1 1 3 4 5
77+</code></pre>
78+<p>Each import with its own arguments makes an instance: the module compiled again, with <code>T</code> standing for the type given, and <code>less</code> calling the procedure given. The arguments may name what the importing module declares later, as <code>before</code> is here.</p>
79+<p>The library's <code>List</code>, <code>Map</code> and <code>Sort</code> are generic modules: <code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code></p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="checks.html">&larr; When it goes wrong</a><a href="index.html">Contents</a><a class="next" href="c.html">Talking to C &rarr;</a></div></article></div>
80+</main>
81+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
82+</body>
83+</html>

site/public/tour/hello.html (history) added +50 -0

@@ -0,0 +1,50 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Hello, world - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html" aria-current="page">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Raz &middot; lesson 1 of 14</p><h1>Hello, world</h1>
32+<p>Every raz program is a module with a <code>main</code> procedure. This is the smallest one that does something.</p>
33+<div class="file"><span>Hello.raz</span></div><pre class="code"><code><span class="kw">module</span> Hello;
34+<span class="kw">import</span> Out;
35+
36+<span class="kw">proc</span> main*;
37+<span class="kw">begin</span>
38+ Out.str(<span class="string">&quot;hello, world\n&quot;</span>);
39+<span class="kw">end</span>;
40+
41+<span class="kw">end</span> Hello.</code></pre>
42+<pre class="output"><code>hello, world
43+</code></pre>
44+<p>A module lives in a file of its own name, <code>Hello.raz</code>, and its name comes again after its last <code>end</code>. <code>import Out</code> lets it use what the module <code>Out</code> exports, as <code>Out.str</code>.</p>
45+<p>The <code>*</code> after <code>main</code> exports it; the program starts at the <code>main</code> of the module named on the command line. Every statement ends in <code>;</code>, even one that ends in <code>end</code>.</p>
46+<p>Compile it and run it with <code>raz Hello.raz &amp;&amp; ./Hello</code>, or with <code>raz run Hello.raz</code>, which writes no file.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="index.html">Contents</a><a class="next" href="modules.html">Modules &rarr;</a></div></article></div>
47+</main>
48+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
49+</body>
50+</html>

site/public/tour/index.html (history) added +44 -0

@@ -0,0 +1,44 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Raz, Set, Go: a tour of raz</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="contents"><h1>Raz, Set, Go</h1>
13+<p>A tour of raz, in the spirit of <em>A Tour of Go</em>. Each lesson is one
14+idea and a whole program, and the output shown is what the program printed
15+when this page was made.</p>
16+<p>It is in three parts, as one counts off: <strong>Raz</strong> (one), the
17+first steps; <strong>Set</strong>, how data is laid out; and
18+<strong>Go</strong>, making programs that run, fail and finish well. To follow
19+along, <a href="../index.html#download">get raz</a> and run any program with
20+<code>raz run Name.raz</code>.</p>
21+<h2>Raz</h2><ol start='1'>
22+<li><a href="hello.html">Hello, world</a> <span>A module, an import and a procedure.</span></li>
23+<li><a href="modules.html">Modules</a> <span>Exports, renaming, and a body that runs first.</span></li>
24+<li><a href="values.html">Values and conversions</a> <span>Sized integers that wrap, and nothing converts unasked.</span></li>
25+<li><a href="procedures.html">Procedures and several results</a> <span>Return two values, change a variable, pass a procedure.</span></li>
26+</ol>
27+<h2>Set</h2><ol start='5'>
28+<li><a href="slices.html">Arrays, slices and strings</a> <span>Five in a row, a window onto them, and bytes in UTF-8.</span></li>
29+<li><a href="records.html">Records and pointers</a> <span>Laid out as C lays out a struct, reached through ^.</span></li>
30+<li><a href="variants.html">Enumerations, sets, variants and match</a> <span>Choices the compiler can check you handled.</span></li>
31+<li><a href="memory.html">Memory and allocators</a> <span>No garbage collector; you choose where memory comes from.</span></li>
32+</ol>
33+<h2>Go</h2><ol start='9'>
34+<li><a href="errors.html">Errors</a> <span>A second result, compared by identity. No exceptions.</span></li>
35+<li><a href="defer.html">Defer</a> <span>Clean up beside the line that needs it.</span></li>
36+<li><a href="checks.html">When it goes wrong</a> <span>Every index is checked, and a failure tells you everything.</span></li>
37+<li><a href="generics.html">Generic modules</a> <span>A module that takes types and procedures as arguments.</span></li>
38+<li><a href="c.html">Talking to C</a> <span>Call a C library, and be called back.</span></li>
39+<li><a href="threads.html">Threads</a> <span>Native threads, atomics and a mutex.</span></li>
40+</ol></div>
41+</main>
42+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
43+</body>
44+</html>

site/public/tour/memory.html (history) added +77 -0

@@ -0,0 +1,77 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Memory and allocators - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html" aria-current="page">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Set &middot; lesson 8 of 14</p><h1>Memory and allocators</h1>
32+<div class="file"><span>Stack.raz</span></div><pre class="code"><code><span class="kw">module</span> Stack;
33+<span class="kw">import</span> Out, Mem, Heap, Arena;
34+
35+<span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>;
36+
37+<span class="comment">(* a node holding v on top of top, from the allocator a;
38+ nil if a has no room *)</span>
39+<span class="kw">proc</span> push(top: ^Node; v: <span class="ty">INT</span>; a: ^Mem.Allocator): ^Node;
40+<span class="kw">var</span> n: ^Node;
41+<span class="kw">begin</span>
42+ <span class="ty">NEW</span>(n, a);
43+ <span class="kw">if</span> n # <span class="ty">nil</span> <span class="kw">then</span>
44+ n^.next := top;
45+ n^.val := v;
46+ <span class="kw">end</span>;
47+ <span class="kw">return</span> n;
48+<span class="kw">end</span>;
49+
50+<span class="kw">proc</span> main*;
51+<span class="kw">var</span> top, n: ^Node; i: <span class="ty">INT</span>; ar: Arena.Arena;
52+<span class="kw">begin</span>
53+ <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span> top := push(top, i, Heap.heap); <span class="kw">end</span>;
54+ <span class="kw">while</span> top # <span class="ty">nil</span> <span class="kw">do</span>
55+ Out.int(top^.val); Out.char(<span class="string">&#x27; &#x27;</span>);
56+ n := top;
57+ top := top^.next;
58+ Mem.free(Heap.heap, ^<span class="ty">U8</span>(n));
59+ <span class="kw">end</span>;
60+ Out.ln;
61+ <span class="kw">if</span> <span class="kw">not</span> Arena.init(<span class="ty">ADR</span>(ar), <span class="number">100000</span>) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>;
62+ <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">1000</span> <span class="kw">do</span> top := push(top, i, <span class="ty">ADR</span>(ar.base)); <span class="kw">end</span>;
63+ Out.int(top^.val); Out.ln;
64+ Arena.done(<span class="ty">ADR</span>(ar)); <span class="comment">(* every node at once *)</span>
65+<span class="kw">end</span>;
66+
67+<span class="kw">end</span> Stack.</code></pre>
68+<pre class="output"><code>3 2 1
69+1000
70+</code></pre>
71+<p>raz has no garbage collector, and no allocator of its own: memory comes from an allocator, a <code>Mem.Allocator</code>, which the program chooses and passes.</p>
72+<p><code>NEW(n, a)</code> sets <code>n</code> to a fresh, zeroed <code>Node</code> from <code>a</code>, or to <code>nil</code> if <code>a</code> has no room. <code>Heap.heap</code> is the general one, which <code>Mem.free</code> gives memory back to. An <code>Arena</code> hands out memory from one block and frees it all at once, as the last lines do.</p>
73+<p><code>Buf</code>, <code>List</code> and <code>Map</code> take the allocator they use from <code>init</code>.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="variants.html">&larr; Enumerations, sets, variants and match</a><a href="index.html">Contents</a><a class="next" href="errors.html">Errors &rarr;</a></div></article></div>
74+</main>
75+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
76+</body>
77+</html>

site/public/tour/modules.html (history) added +72 -0

@@ -0,0 +1,72 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Modules - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html" aria-current="page">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Raz &middot; lesson 2 of 14</p><h1>Modules</h1>
32+<p>A program is a set of modules. Here is one, saved as <code>Greet.raz</code>:</p>
33+<div class="file"><span>Greet.raz</span></div><pre class="code"><code><span class="kw">module</span> Greet;
34+<span class="kw">import</span> Out;
35+
36+<span class="kw">var</span> count*: <span class="ty">INT</span>; <span class="comment">(* the greetings so far *)</span>
37+
38+<span class="kw">proc</span> hello*(name: []<span class="ty">U8</span>);
39+<span class="kw">begin</span>
40+ Out.str(<span class="string">&quot;hello, &quot;</span>);
41+ Out.str(name);
42+ Out.ln;
43+ <span class="ty">INC</span>(count);
44+<span class="kw">end</span>;
45+
46+<span class="kw">begin</span>
47+ Out.str(<span class="string">&quot;Greet is ready\n&quot;</span>);
48+<span class="kw">end</span> Greet.</code></pre>
49+<p>and a program beside it that uses it:</p>
50+<div class="file"><span>Twice.raz</span></div><pre class="code"><code><span class="kw">module</span> Twice;
51+<span class="kw">import</span> Out, G := Greet;
52+
53+<span class="kw">proc</span> main*;
54+<span class="kw">begin</span>
55+ G.hello(<span class="string">&quot;world&quot;</span>);
56+ G.hello(<span class="string">&quot;raz&quot;</span>);
57+ Out.int(G.count);
58+ Out.str(<span class="string">&quot; greetings\n&quot;</span>);
59+<span class="kw">end</span>;
60+
61+<span class="kw">end</span> Twice.</code></pre>
62+<pre class="output"><code>Greet is ready
63+hello, world
64+hello, raz
65+2 greetings
66+</code></pre>
67+<p>A name with <code>*</code> is exported; any other is the module's own. <code>G := Greet</code> imports <code>Greet</code> under a shorter name. Another module may read <code>G.count</code>, but only <code>Greet</code> may change it.</p>
68+<p>The statements after a module's last <code>begin</code> are its body. It runs once, before <code>main</code>, which is why "Greet is ready" comes first.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="hello.html">&larr; Hello, world</a><a href="index.html">Contents</a><a class="next" href="values.html">Values and conversions &rarr;</a></div></article></div>
69+</main>
70+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
71+</body>
72+</html>

site/public/tour/procedures.html (history) added +83 -0

@@ -0,0 +1,83 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Procedures and several results - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html" aria-current="page">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Raz &middot; lesson 4 of 14</p><h1>Procedures and several results</h1>
32+<div class="file"><span>Procs.raz</span></div><pre class="code"><code><span class="kw">module</span> Procs;
33+<span class="kw">import</span> Out;
34+
35+<span class="kw">proc</span> divmod(a, b: <span class="ty">INT</span>): (<span class="ty">INT</span>, <span class="ty">INT</span>);
36+<span class="kw">begin</span>
37+ <span class="kw">return</span> a <span class="kw">div</span> b, a <span class="kw">mod</span> b;
38+<span class="kw">end</span>;
39+
40+<span class="kw">proc</span> swap(<span class="kw">var</span> a, b: <span class="ty">INT</span>);
41+<span class="kw">var</span> t: <span class="ty">INT</span>;
42+<span class="kw">begin</span>
43+ t := a;
44+ a := b;
45+ b := t;
46+<span class="kw">end</span>;
47+
48+<span class="kw">proc</span> square(n: <span class="ty">INT</span>): <span class="ty">INT</span>;
49+<span class="kw">begin</span>
50+ <span class="kw">return</span> n * n;
51+<span class="kw">end</span>;
52+
53+<span class="kw">proc</span> twice(f: <span class="kw">proc</span>(n: <span class="ty">INT</span>): <span class="ty">INT</span>; n: <span class="ty">INT</span>): <span class="ty">INT</span>;
54+<span class="kw">begin</span>
55+ <span class="kw">return</span> f(f(n));
56+<span class="kw">end</span>;
57+
58+<span class="kw">proc</span> main*;
59+<span class="kw">var</span> q, r, x, y: <span class="ty">INT</span>;
60+<span class="kw">begin</span>
61+ q, r := divmod(<span class="number">17</span>, <span class="number">5</span>);
62+ x := <span class="number">1</span>;
63+ y := <span class="number">2</span>;
64+ swap(x, y);
65+ Out.int(q); Out.char(<span class="string">&#x27; &#x27;</span>); Out.int(r); Out.ln;
66+ Out.int(x); Out.char(<span class="string">&#x27; &#x27;</span>); Out.int(y); Out.ln;
67+ Out.int(twice(square, <span class="number">3</span>)); Out.ln;
68+ _, r := divmod(-<span class="number">7</span>, <span class="number">2</span>);
69+ Out.int(r); Out.ln;
70+<span class="kw">end</span>;
71+
72+<span class="kw">end</span> Procs.</code></pre>
73+<pre class="output"><code>3 2
74+2 1
75+81
76+-1
77+</code></pre>
78+<p>A parameter is a copy, unless it is <code>var</code>, when it is the caller's variable itself. A procedure may return several results, which a multiple assignment takes apart; the blank, <code>_</code>, drops one.</p>
79+<p>A procedure is a value too, of a procedure type, as <code>f</code> is in <code>twice</code>. A procedure without parameters leaves out the parentheses, as <code>Out.ln</code> does. Procedures don't nest, and a name is declared before it is used.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="values.html">&larr; Values and conversions</a><a href="index.html">Contents</a><a class="next" href="slices.html">Arrays, slices and strings &rarr;</a></div></article></div>
80+</main>
81+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
82+</body>
83+</html>

site/public/tour/records.html (history) added +72 -0

@@ -0,0 +1,72 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Records and pointers - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html" aria-current="page">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Set &middot; lesson 6 of 14</p><h1>Records and pointers</h1>
32+<div class="file"><span>Boxes.raz</span></div><pre class="code"><code><span class="kw">module</span> Boxes;
33+<span class="kw">import</span> Fmt;
34+
35+<span class="kw">type</span>
36+ Point = <span class="kw">record</span> x, y: <span class="ty">INT</span>; <span class="kw">end</span>;
37+ Box = <span class="kw">record</span> lo, hi: Point; <span class="kw">end</span>;
38+
39+<span class="kw">proc</span> area(b: Box): <span class="ty">INT</span>;
40+<span class="kw">begin</span>
41+ <span class="kw">return</span> (b.hi.x - b.lo.x) * (b.hi.y - b.lo.y);
42+<span class="kw">end</span>;
43+
44+<span class="kw">proc</span> grow(b: ^Box; n: <span class="ty">INT</span>);
45+<span class="kw">begin</span>
46+ <span class="ty">DEC</span>(b^.lo.x, n);
47+ <span class="ty">DEC</span>(b^.lo.y, n);
48+ <span class="ty">INC</span>(b^.hi.x, n);
49+ <span class="ty">INC</span>(b^.hi.y, n);
50+<span class="kw">end</span>;
51+
52+<span class="kw">proc</span> main*;
53+<span class="kw">var</span> b: Box; p: ^Point;
54+<span class="kw">begin</span>
55+ b := {lo: {x: <span class="number">1</span>, y: <span class="number">1</span>}, hi: {x: <span class="number">4</span>, y: <span class="number">3</span>}};
56+ Fmt.print(<span class="string">&quot;{}\n&quot;</span>, [Fmt.int(area(b))]);
57+ grow(<span class="ty">ADR</span>(b), <span class="number">1</span>);
58+ p := <span class="ty">ADR</span>(b.hi);
59+ p^.x := <span class="number">10</span>;
60+ Fmt.print(<span class="string">&quot;{} {}\n&quot;</span>, [Fmt.int(area(b)), Fmt.int(b.hi.x)]);
61+<span class="kw">end</span>;
62+
63+<span class="kw">end</span> Boxes.</code></pre>
64+<pre class="output"><code>6
65+40 10
66+</code></pre>
67+<p>A record is laid out as C lays out a struct. <code>{lo: ..., hi: ...}</code> builds one, naming its fields; the ones it leaves out are zero.</p>
68+<p><code>^Box</code> is a pointer to a <code>Box</code>, <code>ADR(b)</code> the address of <code>b</code>, and <code>b^</code> what a pointer points at. A pointer is never the record: it is <code>b^.lo</code>, never <code>b.lo</code>.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="slices.html">&larr; Arrays, slices and strings</a><a href="index.html">Contents</a><a class="next" href="variants.html">Enumerations, sets, variants and match &rarr;</a></div></article></div>
69+</main>
70+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
71+</body>
72+</html>

site/public/tour/slices.html (history) added +72 -0

@@ -0,0 +1,72 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Arrays, slices and strings - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html" aria-current="page">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Set &middot; lesson 5 of 14</p><h1>Arrays, slices and strings</h1>
32+<div class="file"><span>Slices.raz</span></div><pre class="code"><code><span class="kw">module</span> Slices;
33+<span class="kw">import</span> Out, Str;
34+
35+<span class="kw">proc</span> sum(s: []<span class="ty">INT</span>): <span class="ty">INT</span>;
36+<span class="kw">var</span> i, t: <span class="ty">INT</span>;
37+<span class="kw">begin</span>
38+ t := <span class="number">0</span>;
39+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span> t := t + s[i]; <span class="kw">end</span>;
40+ <span class="kw">return</span> t;
41+<span class="kw">end</span>;
42+
43+<span class="kw">proc</span> main*;
44+<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; s: []<span class="ty">U8</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>;
45+<span class="kw">begin</span>
46+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> a[i] := i * i; <span class="kw">end</span>;
47+ Out.int(sum(a)); Out.ln; <span class="comment">(* the array, as a slice *)</span>
48+ Out.int(sum(a[<span class="number">1</span>..<span class="number">3</span>])); Out.ln; <span class="comment">(* a[1] and a[2] *)</span>
49+ s := <span class="string">&quot;hello, world&quot;</span>;
50+ Out.str(s[<span class="number">7</span>..<span class="ty">LEN</span>(s)]); Out.ln;
51+ <span class="kw">if</span> s[<span class="number">0</span>..<span class="number">5</span>] = <span class="string">&quot;hello&quot;</span> <span class="kw">then</span> Out.str(<span class="string">&quot;it says hello\n&quot;</span>); <span class="kw">end</span>;
52+ Out.int(Str.find(s, <span class="string">&quot;world&quot;</span>)); Out.ln;
53+ name := <span class="string">&quot;raz&quot;</span>; <span class="comment">(* a copy, the program&#x27;s own *)</span>
54+ name[<span class="number">0</span>] := <span class="string">&#x27;R&#x27;</span>;
55+ Out.str(name[<span class="number">0</span>..<span class="number">3</span>]); Out.ln;
56+<span class="kw">end</span>;
57+
58+<span class="kw">end</span> Slices.</code></pre>
59+<pre class="output"><code>30
60+5
61+world
62+it says hello
63+7
64+Raz
65+</code></pre>
66+<p><code>[5]INT</code> is an array: five <code>INT</code>s in a row, copied when it is assigned. <code>[]INT</code> is a slice: a pointer to elements that live elsewhere, and their number, <code>LEN(s)</code>. <code>a[lo..hi]</code> is the slice of the elements from <code>lo</code> up to, but not including, <code>hi</code>, and an array goes silently where a slice is wanted.</p>
67+<p>A string is a slice of bytes, <code>[]U8</code>, in UTF-8, and its bytes are constant; a <code>[8]U8</code> made from one is a copy. Arrays and slices of integers compare with <code>=</code> and <code>#</code>; <code>Str</code> has the rest: ordering, finding, hashing and reading a number.</p>
68+<p>A <code>for</code> loop takes its bounds once, and never steps past the last: with an empty slice, <code>0 to LEN(s) - 1</code> doesn't run at all.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="procedures.html">&larr; Procedures and several results</a><a href="index.html">Contents</a><a class="next" href="records.html">Records and pointers &rarr;</a></div></article></div>
69+</main>
70+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
71+</body>
72+</html>

site/public/tour/threads.html (history) added +76 -0

@@ -0,0 +1,76 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Threads - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html" aria-current="page">Threads</a></li>
31+</ol></aside><article><p class="kicker">Go &middot; lesson 14 of 14</p><h1>Threads</h1>
32+<div class="file"><span>Hits.raz</span></div><pre class="code"><code><span class="kw">module</span> Hits;
33+<span class="kw">import</span> Out, Thread;
34+
35+<span class="kw">const</span> Workers = <span class="number">4</span>;
36+
37+<span class="kw">var</span>
38+ hits: <span class="ty">INT</span>; <span class="comment">(* added to by every thread at once, by XADD *)</span>
39+ m: Thread.Mutex;
40+ total: <span class="ty">INT</span>; <span class="comment">(* added to under m *)</span>
41+
42+<span class="kw">proc</span> work(arg: ^<span class="ty">U8</span>);
43+<span class="kw">var</span> i, n, sum: <span class="ty">INT</span>;
44+<span class="kw">begin</span>
45+ n := <span class="ty">INT</span>(arg);
46+ sum := <span class="number">0</span>;
47+ <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> n <span class="kw">do</span>
48+ sum := sum + i;
49+ _ := <span class="ty">XADD</span>(<span class="ty">ADR</span>(hits), <span class="number">1</span>);
50+ <span class="kw">end</span>;
51+ Thread.lock(<span class="ty">ADR</span>(m));
52+ total := total + sum;
53+ Thread.unlock(<span class="ty">ADR</span>(m));
54+<span class="kw">end</span>;
55+
56+<span class="kw">proc</span> main*;
57+<span class="kw">var</span> t: [Workers]^Thread.Thread; i: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>;
58+<span class="kw">begin</span>
59+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> Workers - <span class="number">1</span> <span class="kw">do</span>
60+ t[i], err := Thread.spawn(work, ^<span class="ty">U8</span>(<span class="number">1000</span> * (i + <span class="number">1</span>)));
61+ <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>;
62+ <span class="kw">end</span>;
63+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> Workers - <span class="number">1</span> <span class="kw">do</span> Thread.join(t[i]); <span class="kw">end</span>;
64+ Out.int(hits); Out.char(<span class="string">&#x27; &#x27;</span>); Out.int(total); Out.ln;
65+<span class="kw">end</span>;
66+
67+<span class="kw">end</span> Hits.</code></pre>
68+<pre class="output"><code>10000 15005000
69+</code></pre>
70+<p><code>Thread.spawn</code> runs a procedure in a thread of its own, with a stack of its own, and <code>Thread.join</code> waits for it to end. <code>XADD</code>, <code>CAS</code> and <code>XCHG</code> change a word atomically; a <code>Thread.Mutex</code> keeps a longer section to one thread at a time. <code>Out</code> and <code>Heap</code> lock themselves.</p>
71+<p><code>Thread</code> makes its own threads, without C, so a program that uses it can't use C; one that does makes its threads with C's <code>pthread_create</code> instead.</p>
72+<p>That is the tour. The rest is in <a href="../spec.html">the report</a>, and the library's interfaces are printed by <code>tools/Doc.raz</code>.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="c.html">&larr; Talking to C</a><a href="index.html">Contents</a><a class="next" href="../spec.html">The report &rarr;</a></div></article></div>
73+</main>
74+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
75+</body>
76+</html>

site/public/tour/values.html (history) added +62 -0

@@ -0,0 +1,62 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Values and conversions - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html" aria-current="page">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Raz &middot; lesson 3 of 14</p><h1>Values and conversions</h1>
32+<div class="file"><span>Values.raz</span></div><pre class="code"><code><span class="kw">module</span> Values;
33+<span class="kw">import</span> Fmt;
34+
35+<span class="kw">const</span> Max = <span class="number">100</span>; <span class="comment">(* untyped: it takes the type it meets *)</span>
36+
37+<span class="kw">proc</span> main*;
38+<span class="kw">var</span> n: <span class="ty">INT</span>; b: <span class="ty">U8</span>; big: <span class="ty">I64</span>; x: <span class="ty">F64</span>; c: <span class="ty">I32</span>; ok: <span class="ty">BOOL</span>;
39+<span class="kw">begin</span>
40+ n := Max * <span class="number">3</span>;
41+ b := <span class="number">250</span>;
42+ b := b + <span class="number">10</span>; <span class="comment">(* wraps, in a U8: 4 *)</span>
43+ big := n; <span class="comment">(* an INT goes into an I64 *)</span>
44+ x := <span class="ty">F64</span>(n) / <span class="number">7.0</span>; <span class="comment">(* nothing else converts unasked *)</span>
45+ c := <span class="string">&#x27;\u{E9}&#x27;</span>; <span class="comment">(* a character is its code point *)</span>
46+ ok := (n &gt; Max) <span class="kw">and</span> (b # <span class="number">0</span>);
47+ Fmt.print(<span class="string">&quot;{} {} {} {} {} {}\n&quot;</span>, [Fmt.int(n), Fmt.int(<span class="ty">I64</span>(b)),
48+ Fmt.int(big), Fmt.real(x, <span class="number">3</span>), Fmt.int(<span class="ty">I64</span>(c)),
49+ Fmt.bool(ok)]);
50+<span class="kw">end</span>;
51+
52+<span class="kw">end</span> Values.</code></pre>
53+<pre class="output"><code>300 4 300 42.857 233 true
54+</code></pre>
55+<p>The integers are <code>I8</code>, <code>I16</code>, <code>I32</code>, <code>I64</code> and <code>U8</code>, <code>U16</code>, <code>U32</code>, <code>U64</code>, and <code>INT</code> and <code>UINT</code>, as big as an address. <code>INT</code> is the everyday one: lengths, indexes and counts are <code>INT</code>s. The reals are <code>F32</code> and <code>F64</code>.</p>
56+<p>Arithmetic wraps at the type's width and never stops the program, except dividing by zero. That is why <code>250 + 10</code> in a <code>U8</code> is 4.</p>
57+<p>Two types never mix: with <code>i: I64</code>, <code>i &lt; LEN(s)</code> is an error. A type's name converts, as <code>F64(n)</code> and <code>I64(b)</code> do above. The one silent conversion between numbers is an <code>INT</code> into an <code>I64</code>, which loses nothing.</p>
58+<p><code>#</code>, not <code>&lt;&gt;</code>, is "different". <code>and</code>, <code>or</code> and <code>not</code> are for <code>BOOL</code>s; <code>&amp;</code>, <code>|</code>, <code>xor</code> and <code>~</code> are for the bits of integers. Every variable starts at zero.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="modules.html">&larr; Modules</a><a href="index.html">Contents</a><a class="next" href="procedures.html">Procedures and several results &rarr;</a></div></article></div>
59+</main>
60+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
61+</body>
62+</html>

site/public/tour/variants.html (history) added +91 -0

@@ -0,0 +1,91 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Enumerations, sets, variants and match - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="values.html">Values and conversions</a></li>
16+<li><a href="procedures.html">Procedures and several results</a></li>
17+</ol>
18+<h3>Set</h3><ol start='5'>
19+<li><a href="slices.html">Arrays, slices and strings</a></li>
20+<li><a href="records.html">Records and pointers</a></li>
21+<li><a href="variants.html" aria-current="page">Enumerations, sets, variants and match</a></li>
22+<li><a href="memory.html">Memory and allocators</a></li>
23+</ol>
24+<h3>Go</h3><ol start='9'>
25+<li><a href="errors.html">Errors</a></li>
26+<li><a href="defer.html">Defer</a></li>
27+<li><a href="checks.html">When it goes wrong</a></li>
28+<li><a href="generics.html">Generic modules</a></li>
29+<li><a href="c.html">Talking to C</a></li>
30+<li><a href="threads.html">Threads</a></li>
31+</ol></aside><article><p class="kicker">Set &middot; lesson 7 of 14</p><h1>Enumerations, sets, variants and match</h1>
32+<div class="file"><span>Shapes.raz</span></div><pre class="code"><code><span class="kw">module</span> Shapes;
33+<span class="kw">import</span> Out, Fmt, Math;
34+
35+<span class="kw">type</span>
36+ Colour = (red, green, blue);
37+ Colours = <span class="kw">set</span> <span class="kw">of</span> Colour;
38+ Shape = <span class="kw">variant</span>
39+ circle(r: <span class="ty">F64</span>);
40+ rect(w, h: <span class="ty">F64</span>);
41+ dot;
42+ <span class="kw">end</span>;
43+
44+<span class="kw">proc</span> area(s: Shape): <span class="ty">F64</span>;
45+<span class="kw">begin</span>
46+ <span class="kw">match</span> s <span class="kw">of</span>
47+ | circle(r): <span class="kw">return</span> Math.pi * r * r;
48+ | rect(w, h): <span class="kw">return</span> w * h;
49+ | dot: <span class="kw">return</span> <span class="number">0.0</span>;
50+ <span class="kw">end</span>;
51+<span class="kw">end</span>;
52+
53+<span class="kw">proc</span> kind(c: <span class="ty">I32</span>): []<span class="ty">U8</span>;
54+<span class="kw">begin</span>
55+ <span class="kw">match</span> c <span class="kw">of</span>
56+ | <span class="string">&#x27; &#x27;</span>, <span class="string">&#x27;\t&#x27;</span>, <span class="string">&#x27;\n&#x27;</span>: <span class="kw">return</span> <span class="string">&quot;space&quot;</span>;
57+ | <span class="string">&#x27;0&#x27;</span> .. <span class="string">&#x27;9&#x27;</span>: <span class="kw">return</span> <span class="string">&quot;digit&quot;</span>;
58+ | <span class="string">&#x27;a&#x27;</span> .. <span class="string">&#x27;z&#x27;</span>, <span class="string">&#x27;A&#x27;</span> .. <span class="string">&#x27;Z&#x27;</span>: <span class="kw">return</span> <span class="string">&quot;letter&quot;</span>;
59+ | <span class="kw">else</span>: <span class="kw">return</span> <span class="string">&quot;other&quot;</span>;
60+ <span class="kw">end</span>;
61+<span class="kw">end</span>;
62+
63+<span class="kw">proc</span> main*;
64+<span class="kw">var</span> shapes: [<span class="number">3</span>]Shape; i: <span class="ty">INT</span>; warm: Colours; c: Colour;
65+<span class="kw">begin</span>
66+ shapes := [circle(<span class="number">1.0</span>), rect(<span class="number">2.0</span>, <span class="number">3.5</span>), dot];
67+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span>
68+ Fmt.print(<span class="string">&quot;{}\n&quot;</span>, [Fmt.real(area(shapes[i]), <span class="number">2</span>)]);
69+ <span class="kw">end</span>;
70+ Fmt.print(<span class="string">&quot;{} {} {}\n&quot;</span>, [Fmt.str(kind(<span class="string">&#x27;7&#x27;</span>)), Fmt.str(kind(<span class="string">&#x27;x&#x27;</span>)),
71+ Fmt.str(kind(<span class="string">&#x27;+&#x27;</span>))]);
72+ warm := {red, blue} - {blue} + {green};
73+ <span class="kw">for</span> c := red <span class="kw">to</span> blue <span class="kw">do</span>
74+ <span class="kw">if</span> c <span class="kw">in</span> warm <span class="kw">then</span> Out.int(<span class="ty">I64</span>(c)); Out.ln; <span class="kw">end</span>;
75+ <span class="kw">end</span>;
76+<span class="kw">end</span>;
77+
78+<span class="kw">end</span> Shapes.</code></pre>
79+<pre class="output"><code>3.14
80+7.00
81+0.00
82+digit letter other
83+0
84+1
85+</code></pre>
86+<p>An enumeration is a list of names, in order; a <code>set of</code> it holds any choice of them, in the bits of a word, with <code>in</code>, <code>+</code> for union, <code>*</code> for intersection and <code>-</code> for difference.</p>
87+<p>A variant holds one of its cases, each with fields of its own, and <code>match</code> is the only way to read them: it runs the arm of the case the variant holds, naming its fields. Every case has an arm, unless an <code>| else:</code> comes last. On an integer, <code>match</code> takes constants and ranges, and must have an <code>| else:</code>.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="records.html">&larr; Records and pointers</a><a href="index.html">Contents</a><a class="next" href="memory.html">Memory and allocators &rarr;</a></div></article></div>
88+</main>
89+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
90+</body>
91+</html>

site/style.css (history) added +88 -0

@@ -0,0 +1,88 @@
1+:root {
2+ --bg: #fbfaf7; --fg: #1b1b1a; --muted: #6b6a66; --line: #e2e0d9;
3+ --accent: #b4401f; --code-bg: #f2f0ea; --out-bg: #1f2124; --out-fg: #d8dadc;
4+ --kw: #8a2d86; --ty: #1c6c8c; --str: #3b7a2a; --com: #8b8a84; --num: #b4401f;
5+}
6+@media (prefers-color-scheme: dark) {
7+ :root {
8+ --bg: #17181a; --fg: #e6e4de; --muted: #9a988f; --line: #2d2f33;
9+ --accent: #ee8a63; --code-bg: #202226; --out-bg: #0f1012; --out-fg: #cfd1d4;
10+ --kw: #d291cf; --ty: #6cc1e2; --str: #9bd47f; --com: #7c7b75; --num: #ee8a63;
11+ }
12+}
13+* { box-sizing: border-box; }
14+html { -webkit-text-size-adjust: 100%; }
15+body {
16+ margin: 0; background: var(--bg); color: var(--fg);
17+ font: 17px/1.6 Charter, "Bitstream Charter", Georgia, serif;
18+}
19+a { color: var(--accent); }
20+header { border-bottom: 1px solid var(--line); }
21+header nav, main, footer { max-width: 62rem; margin: 0 auto; padding: 0 16px; }
22+header nav { display: flex; flex-wrap: wrap; gap: 4px 20px; padding-top: 14px; padding-bottom: 14px;
23+ font-family: system-ui, sans-serif; font-size: 15px; }
24+header nav a { color: var(--fg); text-decoration: none; }
25+header nav a:first-child { font: 700 20px ui-monospace, monospace; color: var(--accent); }
26+header nav a[aria-current] { border-bottom: 2px solid var(--accent); }
27+header nav .repo { margin-left: auto; color: var(--muted); }
28+main { padding-top: 28px; padding-bottom: 48px; }
29+footer { color: var(--muted); font-size: 14px; padding-bottom: 32px; font-family: system-ui, sans-serif; }
30+h1 { overflow-wrap: anywhere; font-size: 2.1rem; line-height: 1.2; margin: .2em 0 .5em; }
31+h2 { font-size: 1.4rem; margin: 1.8em 0 .4em; }
32+code, pre { font: 14.5px/1.5 ui-monospace, "DejaVu Sans Mono", Menlo, monospace; }
33+code { background: var(--code-bg); padding: 1px 4px; border-radius: 3px; }
34+pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
35+pre code { background: none; padding: 0; }
36+pre.code { background: var(--code-bg); border: 1px solid var(--line); border-radius: 0 0 6px 6px; }
37+.file { font: 12px ui-monospace, monospace; color: var(--muted); background: var(--code-bg);
38+ border: 1px solid var(--line); border-bottom: 0; border-radius: 6px 6px 0 0;
39+ padding: 4px 14px; margin-top: 1.2em; }
40+pre.output { background: var(--out-bg); color: var(--out-fg); border-radius: 6px; margin: 8px 0 1.2em; }
41+pre.output::before { content: "output"; display: block; font: 11px system-ui, sans-serif;
42+ text-transform: uppercase; letter-spacing: .08em; color: #8b8e93; margin-bottom: 6px; }
43+pre .err { color: #ff9a85; }
44+.kw { color: var(--kw); font-weight: 600; } .ty { color: var(--ty); }
45+.string { color: var(--str); } .comment { color: var(--com); font-style: italic; }
46+.number { color: var(--num); }
47+
48+/* the home page */
49+.hero { padding: 24px 0 8px; }
50+.hero h1 { font-size: 3.4rem; font-family: ui-monospace, monospace; color: var(--accent); margin: 0; }
51+.hero .tag { font-size: 1.35rem; margin: .2em 0 1em; }
52+.hero .cta { display: flex; flex-wrap: wrap; gap: 10px; margin: 1em 0; font-family: system-ui, sans-serif; }
53+.btn { display: inline-block; padding: 9px 18px; border-radius: 6px; text-decoration: none;
54+ border: 1px solid var(--accent); color: var(--accent); font-weight: 600; font-size: 15px; }
55+.btn.primary { background: var(--accent); color: var(--bg); }
56+.cols { display: grid; grid-template-columns: minmax(0, 1fr); gap: 24px; }
57+@media (min-width: 52rem) { .cols { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
58+.features { padding-left: 1.2em; } .features li { margin: .35em 0; }
59+.facts { display: flex; flex-wrap: wrap; gap: 8px 28px; margin: 1.4em 0; font-family: system-ui, sans-serif;
60+ font-size: 15px; color: var(--muted); }
61+.facts b { color: var(--fg); }
62+
63+/* the tour */
64+.contents { max-width: 44rem; }
65+.contents ol { padding-left: 1.6em; } .contents li { margin: .5em 0; }
66+.contents li span { display: block; color: var(--muted); font-size: 15px; }
67+.tour { display: grid; grid-template-columns: minmax(0, 1fr); gap: 24px; }
68+@media (min-width: 56rem) { .tour { grid-template-columns: 14rem minmax(0, 1fr); gap: 40px; } }
69+.tour aside { font-family: system-ui, sans-serif; font-size: 14px; }
70+.tour aside h3 { margin: 1em 0 .2em; font-size: 12px; text-transform: uppercase;
71+ letter-spacing: .08em; color: var(--muted); }
72+.tour aside ol { margin: 0; padding-left: 1.5em; } .tour aside a { color: var(--fg); text-decoration: none; }
73+.tour aside a[aria-current] { color: var(--accent); font-weight: 700; }
74+@media (max-width: 55.99rem) { .tour aside { order: 2; border-top: 1px solid var(--line); } }
75+.tour article { min-width: 0; }
76+.kicker { font: 13px system-ui, sans-serif; color: var(--muted); text-transform: uppercase;
77+ letter-spacing: .08em; margin: 0; }
78+.tryit { color: var(--muted); font-size: 15px; }
79+.pager { display: flex; flex-wrap: wrap; gap: 8px 24px; margin-top: 2em; padding-top: 14px;
80+ border-top: 1px solid var(--line); font-family: system-ui, sans-serif; font-size: 15px; }
81+.pager .next { margin-left: auto; font-weight: 700; }
82+
83+/* the report */
84+.spec { max-width: 46rem; }
85+.spec pre { background: var(--code-bg); border-left: 2px solid var(--line); white-space: pre-wrap; }
86+.spec table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 15px; }
87+.spec td, .spec th { border-bottom: 1px solid var(--line); padding: 3px 10px; text-align: left; }
88+.spec nav#TOC ul { list-style: none; padding-left: 1em; }