raz / commit fbf1697e

commit
fbf1697efc3d8aaf3e88d5c0d227d5b6e2922c1e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
f8fb7674
raz(7), the language at a glance

What a programmer looks up: words and symbols, the grammar in brief,
modules, the types and their sizes, the operators and their order,
conversions, the built-in procedures, the statements, procedures and
C, what is checked, and this compiler's limits; each with the
report's section.

patch · log

2 files changed, +560 -1

PLAN.md (history) modified +1 -1

@@ -39,7 +39,7 @@
3939 declarations with theirs, under a heading of its name. `make` builds
4040 the page from `lib/`, so it says what the library says, always.
4141 About 60 lines of `Doc`.
42-3. **`raz(7)`, the language at a glance.** What a programmer looks up:
42+3. Done: **`raz(7)`, the language at a glance.** What a programmer looks up:
4343 the grammar in brief, the types and their sizes, the operators and
4444 their order, the built-in procedures, the statements, what is
4545 checked, and the limits; each pointing to the report's section for

doc/raz.7 (history) added +559 -0

@@ -0,0 +1,559 @@
1+.\" raz(7) - the raz language at a glance
2+.Dd October 12, 2026
3+.Dt RAZ 7
4+.Os
5+.Sh NAME
6+.Nm raz
7+.Nd the raz programming language at a glance
8+.Sh DESCRIPTION
9+raz is a small systems language for x86-64 Linux, with no garbage
10+collector and no hidden run time.
11+This page is what a programmer looks up; the report,
12+.Pa doc/raz.md
13+in raz's sources, is the definition, and the numbers in brackets,
14+such as [6.8], are its sections.
15+.Xr raz-tutorial 7
16+is a tour,
17+.Xr raz-lib 3
18+the library and
19+.Xr raz 1
20+the compiler.
21+.Sh WORDS AND SYMBOLS
22+[2] A program is UTF-8; everything outside strings, characters and
23+comments is ASCII.
24+.Bl -tag -width "Identifiers"
25+.It Identifiers
26+A letter or
27+.Cm _ ,
28+then letters, digits and
29+.Cm _ ,
30+up to 63 characters; case matters.
31+.Cm _
32+alone is the blank.
33+.It Numbers
34+.Cm 123 ;
35+.Cm 0FFH ,
36+hexadecimal, in capitals;
37+.Cm 1.5
38+and
39+.Cm 2.0E-3 ,
40+reals, with a digit on both sides of the point.
41+A number is untyped until it meets a type.
42+.It Characters
43+.Cm 'a' ,
44+the code point, an integer.
45+.It Strings
46+.Cm \(dqhello\en\(dq ,
47+its UTF-8 bytes, ending on its line.
48+.It Escapes
49+.Cm \en \et \er \e0 \e\e \e' \e\(dq \eu{E9} ,
50+and in strings
51+.Cm \exHH ,
52+any byte.
53+.It Comments
54+.Cm "(* ... *)" ,
55+nesting.
56+The comment just before a declaration, or before
57+.Cm module ,
58+is its documentation.
59+.El
60+.Pp
61+The reserved words, all lowercase but
62+.Cm VOLATILE :
63+.Bd -literal -offset indent
64+and begin break by code const defer div do else elsif end
65+extern for forward if import in link match mod module not of
66+or packed proc record repeat return set shl shr then to type
67+until var variant VOLATILE while xor
68+.Ed
69+.Pp
70+The basic types,
71+.Cm ERROR , true , false , nil
72+and the built-in procedures are predeclared, in a scope around every
73+module, and may be declared again.
74+.Sh THE GRAMMAR IN BRIEF
75+[appendix B]
76+.Cm "{ x }"
77+is any number of
78+.Cm x ,
79+.Cm "[ x ]"
80+is one or none.
81+.Bd -literal -offset indent
82+Module = "module" Name [ "(" Params ")" ] ";" { Import }
83+ { "link" ( String | "static" ) ";" }
84+ [ "check" "off" ";" ]
85+ { Decl } [ "begin" Stmts ] "end" Name "." .
86+Import = "import" Item { "," Item } ";" .
87+Item = Name [ ":=" Name [ "(" Type { "," Type } ")" ] ] .
88+Decl = "const" { Name ["*"] [ ":" Type ] "=" Expr ";" }
89+ | "type" { Name ["*"] "=" Type ";" }
90+ | "var" { Names ":" Type ";" }
91+ | "proc" Name ["*"] Signature ";" Body ";"
92+ | "extern" ( "proc" Name ["*"] Signature
93+ | "var" Name ["*"] ":" Type ) ";" .
94+Body = "forward" | { Local } "begin" Stmts "end" [ Name ]
95+ | "code" { [ Label ":" ] [ Instr ] ";" } "end" [ Name ] .
96+Signature = [ "(" Params ")" ] [ ":" ( Type | "(" Types ")" ) ] .
97+Type = Name | Module "." Name | "[" Expr "]" Type | "[" "]" Type
98+ | "^" [ "VOLATILE" ] Type
99+ | [ "packed" ] "record" Fields "end"
100+ | "variant" { Case ";" } "end" | "(" Names ")"
101+ | "set" "of" Type | "proc" Signature .
102+Stmt = Designator [ ":=" Expr ] | "_" ":=" Expr
103+ | Target "," Target { "," Target } ":=" Expr
104+ | "if" Expr "then" Stmts { "elsif" Expr "then" Stmts }
105+ [ "else" Stmts ] "end"
106+ | "while" Expr "do" Stmts "end"
107+ | "repeat" Stmts "until" Expr
108+ | "for" Name ":=" Expr "to" Expr [ "by" Expr ]
109+ "do" Stmts "end"
110+ | "match" Expr "of" { "|" Arm } "end"
111+ | "return" [ Exprs ] | "break" | "defer" Stmt .
112+Arm = ( Case [ "(" Names ")" ] | Label { "," Label } | "else" )
113+ ":" Stmts .
114+Stmts = { Stmt ";" } .
115+.Ed
116+.Pp
117+Every statement ends in
118+.Cm ";" ,
119+even one that ends in
120+.Cm end
121+or
122+.Cm until .
123+A procedure without parameters leaves out the parentheses where it is
124+declared and where it is called.
125+A name is declared before it is used, but for a pointer's type, which
126+may come later in the same
127+.Cm type
128+section, and a
129+.Cm forward
130+procedure.
131+Procedures don't nest.
132+.Sh MODULES
133+[3] Module
134+.Cm M
135+lives in
136+.Pa M.raz ,
137+or
138+.Pa M.linux-x86_64.raz
139+for one target's own; it is found beside the main module, then in each
140+.Fl I
141+directory, then in the library.
142+A program has one module of each name, and the library's names are
143+kept for it.
144+.Bl -bullet
145+.It
146+A name declared with
147+.Cm *
148+is exported, and used as
149+.Cm M.name .
150+An exported variable is read-only outside its module.
151+.It
152+.Cm "import O := Out;"
153+renames;
154+.Cm "import Ints := List(INT);"
155+makes an instance of a generic module, one for each distinct list of
156+arguments, its procedure arguments named after its types:
157+.Cm "Map([]U8, INT, Str.hash, Str.eq)" .
158+.It
159+A module's body runs once, after those of the modules it imports, and
160+before the main module's
161+.Cm main .
162+.It
163+.Cm "link \(dqlibX.so.1\(dq;"
164+loads a C library;
165+.Cm "link static;"
166+says the program must not use C.
167+.Cm "check off;"
168+leaves the module's index and slice checks out.
169+.El
170+.Sh TYPES
171+[5] Sizes are x86-64's.
172+.Bl -column "U8 U16 U32 U64" "n * SIZE(T)" "an enumeration, 256 names at most"
173+.It Sy Type Ta Sy Size Ta Sy Values
174+.It "I8 I16 I32 I64" Ta "1, 2, 4, 8" Ta "signed, two's complement"
175+.It "U8 U16 U32 U64" Ta "1, 2, 4, 8" Ta "unsigned"
176+.It "INT, UINT" Ta "8" Ta "as big as an address"
177+.It "BOOL" Ta "1" Ta "false, true"
178+.It "F32, F64" Ta "4, 8" Ta "IEEE 754"
179+.It "ERROR" Ta "8" Ta "nil, or a constant's error"
180+.It "^T, proc ..." Ta "8" Ta "an address, or nil"
181+.It "[]T" Ta "16" Ta "a pointer and a length"
182+.It "[n]T" Ta "n * SIZE(T)" Ta "n elements, copied whole"
183+.It "(a, b, c)" Ta "1" Ta "an enumeration, 256 names at most"
184+.It "set of E" Ta "1, 2, 4, 8" Ta "bit i for E's name i, 64 at most"
185+.El
186+.Pp
187+.Bl -bullet
188+.It
189+A
190+.Cm record
191+is laid out as C's struct; a
192+.Cm "packed record"
193+has no padding, and its misaligned fields no address.
194+.It
195+A
196+.Cm variant
197+is an
198+.Cm INT
199+tag and the room of its largest case:
200+.Cm "variant circle(r: F64); dot; end" .
201+A case is built as
202+.Cm circle(1.0)
203+or
204+.Cm dot ,
205+and read only by
206+.Cm match .
207+.It
208+.Cm "^VOLATILE T"
209+points at what something else may change: each access is one access,
210+of its size, in order [7.6].
211+.It
212+.Cm "const NotFound*: ERROR = \(dqnot found\(dq;"
213+makes an error unlike every other, compared by
214+.Cm =
215+and
216+.Cm # ;
217+.Cm MSG(e)
218+is its message.
219+.It
220+Every variable starts at zero: 0, false, nil, the empty slice, a
221+variant's first case.
222+.It
223+Named record, variant and enumeration types are each a type of their
224+own; other types built alike are the same.
225+.Cm "type A = B;"
226+is another name for
227+.Cm B .
228+.El
229+.Pp
230+A value goes where another type is wanted only as an integer constant
231+that fits it,
232+.Cm nil
233+into a pointer, procedure or
234+.Cm ERROR ,
235+.Cm ^T
236+into
237+.Cm "^VOLATILE T" ,
238+.Cm [n]T
239+into
240+.Cm []T ,
241+a string into
242+.Cm []U8 , ^U8
243+(with a zero byte after it) or
244+.Cm [n]U8 ,
245+a procedure into its type, and
246+.Cm INT
247+into
248+.Cm I64 ,
249+.Cm UINT
250+into
251+.Cm U64
252+[5.10].
253+Anything else is converted by name.
254+.Sh OPERATORS
255+[6] From the tightest:
256+.Bl -column "* / div mod & shl shr" "and, or, not: BOOL; & | xor ~: bits"
257+.It Sy Operators Ta Sy Notes
258+.It "not ~" Ta "not of a BOOL; ~ of an integer's bits"
259+.It "* / div mod & shl shr" Ta "/ for reals; shifts count modulo the width"
260+.It "+ - | xor" Ta "a unary minus takes the first term"
261+.It "= # < <= > >= in" Ta "# is different; in, for a set"
262+.It "and" Ta "evaluates its right side only if needed"
263+.It "or" Ta "the same"
264+.El
265+.Pp
266+.Bl -bullet
267+.It
268+Both operands have one type, or one is an untyped constant that fits
269+the other's: with
270+.Cm "i: I64" ,
271+.Cm "i < LEN(s)"
272+is an error.
273+Integers wrap at their width.
274+.Cm div
275+rounds toward zero, and
276+.Cm mod
277+takes the dividend's sign; dividing by zero stops the program.
278+.It
279+Pointers:
280+.Cm p^ ,
281+.Cm "p + i"
282+and
283+.Cm "p - i"
284+in elements,
285+.Cm "p - q" ,
286+.Cm p[i] ,
287+and
288+.Cm p[lo..hi] ,
289+a slice; a record's fields through it are
290+.Cm p^.f .
291+.It
292+Arrays and slices:
293+.Cm a[i] ,
294+and
295+.Cm a[lo..hi]
296+from
297+.Cm lo
298+up to but not including
299+.Cm hi .
300+Two of integers, BOOLs, enumerations, sets or pointers compare with
301+.Cm =
302+and
303+.Cm # .
304+.It
305+Sets:
306+.Cm +
307+union,
308+.Cm *
309+intersection,
310+.Cm -
311+difference,
312+.Cm <=
313+and
314+.Cm >=
315+subset and superset.
316+.It
317+Literals:
318+.Cm "[1, 2, 3]" ,
319+an array, and
320+.Cm "{x: 1, y: 2}" ,
321+a record with its other fields zero, where the type is known;
322+.Cm "{red, blue}" ,
323+a set.
324+.El
325+.Sh CONVERSIONS
326+[6.7] A type's name converts:
327+.Cm I32(x) , F64(n) , ^Cell(adr) , "^VOLATILE U32(0FEE00000H)" .
328+.Bl -column "pointer, procedure" "pointer, procedure" "rounded toward zero"
329+.It Sy From Ta Sy To Ta Sy Result
330+.It "integer" Ta "integer" Ta "cut, or extended"
331+.It "integer" Ta "F32, F64" Ta "the nearest"
332+.It "F32, F64" Ta "integer" Ta "rounded toward zero"
333+.It "F32 / F64" Ta "F64 / F32" Ta "the same, or the nearest"
334+.It "enumeration, set" Ta "integer" Ta "its position, its bits"
335+.It "integer" Ta "enumeration, set" Ta "checked; its low bits"
336+.It "INT, UINT" Ta "pointer, procedure" Ta "the address"
337+.It "pointer, procedure" Ta "INT, UINT, pointer" Ta "the address"
338+.El
339+.Pp
340+There is no other: none between
341+.Cm BOOL
342+and integers, and none to records, arrays, slices, variants or
343+.Cm ERROR .
344+.Sh BUILT-IN PROCEDURES
345+[6.8]
346+.Bl -tag -width "SYSCALL(n, x1, ...)"
347+.It Fn SIZE T
348+The size of type
349+.Ar T ,
350+an untyped constant.
351+.It Fn ADR x
352+The address of the variable
353+.Ar x .
354+.It Fn LEN a
355+The length of an array, a string constant or a slice.
356+.It Fn NEW p a
357+.Ar p
358+set to a fresh, zeroed
359+.Ar T
360+from the allocator
361+.Ar a ,
362+a
363+.Cm ^Mem.Allocator ,
364+or to nil.
365+.It Fn MSG e
366+The message of an
367+.Cm ERROR .
368+.It Fn COPY d s
369+The elements of
370+.Ar s
371+copied to the start of
372+.Ar d ,
373+as one block, even if they overlap; past
374+.Fn LEN d ,
375+a failed check.
376+.It Fn INC v k , Fn DEC v k
377+.Ar v
378+plus or minus
379+.Ar k ,
380+or 1; it wraps.
381+.It Fn CAS p old new
382+If
383+.Ar p Ns ^
384+is
385+.Ar old ,
386+set it to
387+.Ar new
388+and return true; atomically.
389+.It Fn XCHG p x , Fn XADD p x
390+Set, or add to,
391+.Ar p Ns ^ ,
392+returning the value before; atomically.
393+.It Cm FENCE
394+A full memory barrier.
395+.It Fn SYSCALL n x1 ...
396+Linux system call
397+.Ar n ,
398+with up to six arguments; minus an errno on failure.
399+.It Fn ARGC , Fn ARGV
400+The command line: its count, and its C strings.
401+.El
402+.Sh STATEMENTS
403+[7]
404+.Bl -tag -width "repeat ... until"
405+.It Cm "x := e"
406+Copies; arrays, records and slices whole.
407+.Cm "a, b := f(x)"
408+takes several results apart, and
409+.Cm "_ := f(x)"
410+drops one on purpose.
411+.It Cm "f(x)"
412+A call; a result it drops is warned of.
413+.It Cm if , while , repeat
414+On a
415+.Cm BOOL .
416+.Cm repeat
417+runs its body once at least.
418+.It Cm for
419+.Cm "for i := a to b by s do ... end" :
420+.Cm a
421+and
422+.Cm b
423+evaluated once,
424+.Cm s
425+a constant, 1 if left out; never steps past
426+.Cm b ,
427+and leaves
428+.Cm i
429+at the last value it took.
430+.It Cm match
431+On a variant, an arm for each case, naming its fields; on an
432+enumeration, an arm for each name; on an integer, arms of constants
433+and ranges,
434+.Cm "| '0' .. '9':" ,
435+and an
436+.Cm "| else:" ,
437+which any
438+.Cm match
439+may end with.
440+.It Cm return
441+With the results, several separated by commas, or
442+.Cm "return f(x)" ,
443+passing on
444+.Cm f Ns 's;
445+off the end, a procedure returns zeros.
446+.It Cm break
447+Out of the innermost
448+.Cm while , repeat
449+or
450+.Cm for .
451+There is no
452+.Cm goto
453+and no
454+.Cm continue .
455+.It Cm defer
456+Runs its statement when the statements it stands in end, by their end,
457+a
458+.Cm return
459+or a
460+.Cm break ;
461+the last first.
462+.El
463+.Pp
464+Operands, arguments and an assignment's designators are evaluated left
465+to right, the designators first [7.5].
466+.Sh PROCEDURES
467+[8]
468+.Bl -bullet
469+.It
470+A parameter is a copy; a
471+.Cm var
472+one is the caller's variable, of the parameter's type exactly.
473+.It
474+.Cm "proc f(x: INT): (INT, ERROR);"
475+returns several results.
476+A procedure is a value of its type,
477+.Cm "proc(a, b: INT): BOOL" ,
478+or nil.
479+.It
480+A
481+.Cm code
482+procedure is machine code, in Intel's syntax, called by the System V
483+convention:
484+.Cm "rdtsc; shl rdx, 32; or rax, rdx; ret;" .
485+.It
486+.Cm "extern proc printf(f: ^U8; ...): I32;"
487+is C's [9]: its parameters integers, reals, pointers and procedures.
488+.Cm "extern var environ: ^^U8;"
489+is C's variable.
490+A raz procedure of those types is a C callback.
491+.It
492+.Cm "proc main*;"
493+or
494+.Cm "proc main*: INT;"
495+of the main module runs after every body; its result is the exit
496+status.
497+.Cm Rt.exit
498+ends the program after the procedures given to
499+.Cm Rt.atExit
500+[10].
501+.El
502+.Sh WHAT IS CHECKED
503+[6.5, 10] Each of these stops the program with status 2, or its
504+signal's, saying on standard error where, why, and the calls under way
505+with their variables:
506+.Bl -bullet -compact
507+.It
508+an index past the length, and a slice out of
509+.Cm "0 <= lo <= hi <= length" ,
510+unless the module has checks off;
511+.It
512+a slice of a pointer that runs backwards, and a number that is no
513+position of the enumeration it converts to;
514+.It
515+a
516+.Cm nil
517+pointer, a
518+.Cm nil
519+procedure, and a stack overflow;
520+.It
521+a division by zero, and the smallest value of a signed type divided by
522+-1.
523+.El
524+.Pp
525+Integers wrap and reals never stop the program.
526+The compiler warns of what is declared and not used, of a variable
527+only assigned, and of a dropped result;
528+.Fl Werror
529+makes them errors.
530+What isn't checked is memory reached around the checks: a stale or
531+made-up pointer, indexing through a pointer, C, and code procedures.
532+.Sh LIMITS
533+This compiler's, beyond the report's 63 characters to a name, 256
534+names to an enumeration and 64 to a set:
535+.Bl -column "deferred statements in a procedure" "integers and reals."
536+.It "a string literal" Ta "65,535 bytes"
537+.It "a number, as written" Ta "255 characters"
538+.It "elements of a literal" Ta "1,024"
539+.It "names in one declaration" Ta "64"
540+.It "variables in one assignment" Ta "16"
541+.It "deferred statements in a procedure" Ta "64"
542+.It "labels in one match" Ta "64"
543+.It "arguments of a call" Ta "63"
544+.It "type arguments of a generic module" Ta "8"
545+.It "scopes nested" Ta "64"
546+.It "parameters of a code procedure" Ta "6 integers, 8 reals"
547+.It "procedures given to Rt.atExit" Ta "32"
548+.It "-I directories" Ta "16"
549+.It "errors told in one run" Ta "20"
550+.El
551+.Sh SEE ALSO
552+.Xr raz 1 ,
553+.Xr raz-lib 3 ,
554+.Xr raz-tutorial 7 ;
555+the report,
556+.Pa doc/raz.md ,
557+and the guide to the compiler,
558+.Pa doc/compiler.md ,
559+in raz's sources.