raz / doc / raz.7

747 lines · 17.9 KiB · raw · history · last change e0d008cd

1.\" raz(7) - the raz language at a glance
2.Dd October 15, 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
9raz is a small systems language for x86-64 Linux, with no garbage
10collector and no hidden run time.
11This page is what a programmer looks up; the report,
12.Pa doc/raz.md
13in raz's sources, is the definition, and the numbers in brackets,
14such as [6.8], are its sections.
15.Xr raz-tutorial 7
16is a tour,
17.Xr raz-lib 3
18the library and
19.Xr raz 1
20the compiler.
21.Sh WORDS AND SYMBOLS
22[2] A program is UTF-8; everything outside strings, characters and
23comments is ASCII.
24.Bl -tag -width "Identifiers"
25.It Identifiers
26A letter or
27.Cm _ ,
28then letters, digits and
29.Cm _ ,
30up to 63 characters; case matters.
31.Cm _
32alone is the blank.
33.It Numbers
34.Cm 123 ;
35.Cm 0FFH ,
36hexadecimal, in capitals;
37.Cm 1.5
38and
39.Cm 2.0E-3 ,
40reals, with a digit on both sides of the point.
41A number is untyped until it meets a type.
42.It Characters
43.Cm 'a' ,
44the code point, an integer.
45.It Strings
46.Cm \(dqhello\en\(dq ,
47its UTF-8 bytes, ending on its line.
48.It Raw strings
49.Cm \(gaC:\etmp\(ga ,
50the text between backquotes as it stands, no escapes, over lines; if
51the opening backquote ends its line and the closing one has only
52blanks before it, those blanks come off every line.
53.It Escapes
54.Cm \en \et \er \e0 \e\e \e' \e\(dq \eu{E9} ,
55and in strings
56.Cm \exHH ,
57any byte.
58.It Comments
59.Cm "(* ... *)" ,
60nesting.
61The comment just before a declaration, or before
62.Cm module ,
63is its documentation.
64.El
65.Pp
66The reserved words, all lowercase but
67.Cm VOLATILE :
68.Bd -literal -offset indent
69and begin break by code const continue defer div do else elsif
70end extern for forward if import in link match mod module not
71of or packed proc record repeat return set shl shr then to try
72type until var variant VOLATILE while xor
73.Ed
74.Pp
75The basic types,
76.Cm ERROR , true , false , nil
77and the built-in procedures are predeclared, in a scope around every
78module, and may be declared again.
79.Sh THE GRAMMAR IN BRIEF
80[appendix B]
81.Cm "{ x }"
82is any number of
83.Cm x ,
84.Cm "[ x ]"
85is one or none.
86.Bd -literal -offset indent
87Module = "module" Name [ "(" Params ")" ] ";" { Import }
88 { "link" ( String | "static" ) ";" }
89 [ "check" "off" ";" ]
90 { Decl } [ "begin" Stmts ] "end" Name "." .
91Import = "import" Item { "," Item } ";" .
92Item = Name [ ":=" Name [ "(" Type { "," Type } ")" ] ] .
93Decl = "const" { Name ["*"] [ ":" Type ] "=" Expr ";" }
94 | "type" { Name ["*"] "=" Type ";" }
95 | "var" { Names ":" Type ";" }
96 | "proc" Name ["*"] Signature ";" Body ";"
97 | "extern" ( "proc" Name ["*"] Signature
98 | "var" Name ["*"] ":" Type ) ";" .
99Body = "forward" | { Local } "begin" Stmts "end" [ Name ]
100 | "code" { [ Label ":" ] [ Instr ] ";" } "end" [ Name ] .
101Signature = [ "(" Params ")" ] [ ":" ( Type | "(" Types ")" ) ] .
102Type = Name | Module "." Name | "[" Expr "]" Type | "[" "]" Type
103 | "^" [ "VOLATILE" ] Type
104 | [ "packed" ] "record" Fields "end"
105 | "variant" { Case ";" } "end" | "(" Names ")"
106 | "set" "of" Type | "proc" Signature .
107Stmt = Designator [ ":=" Expr ] | "_" ":=" Expr
108 | Target "," Target { "," Target } ":=" Expr
109 | "if" Expr "then" Stmts { "elsif" Expr "then" Stmts }
110 [ "else" Stmts ] "end"
111 | "while" Expr "do" Stmts "end"
112 | "repeat" Stmts "until" Expr
113 | "for" Name ":=" Expr "to" Expr [ "by" Expr ]
114 "do" Stmts "end"
115 | "match" Expr "of" { "|" Arm } "end"
116 | "return" [ Exprs ] | "break" | "continue" | "defer" Stmt .
117Arm = ( Case [ "(" Names ")" ] | Label { "," Label } | "else" )
118 ":" Stmts .
119Stmts = { Stmt ";" } .
120.Ed
121.Pp
122Every statement ends in
123.Cm \&; ,
124even one that ends in
125.Cm end
126or
127.Cm until .
128A procedure without parameters leaves out the parentheses where it is
129declared and where it is called.
130A name is declared before it is used, but for a pointer's type, which
131may come later in the same
132.Cm type
133section, and a
134.Cm forward
135procedure.
136Procedures don't nest.
137.Sh MODULES
138[3] Module
139.Cm M
140lives in
141.Pa M.raz ,
142or
143.Pa M.linux-x86_64.raz
144for one target's own; it is found beside the main module, then in each
145.Fl I
146directory, then in the library.
147A program has one module of each name, and the library's names are
148kept for it.
149.Bl -bullet
150.It
151A name declared with
152.Cm *
153is exported, and used as
154.Cm M.name .
155An exported variable is read-only outside its module.
156.It
157.Cm "import O := Out;"
158renames;
159.Cm "import Ints := List(INT);"
160makes an instance of a generic module, one for each distinct list of
161arguments, its procedure arguments named after its types:
162.Cm "Map([]const U8, INT, Str.hash, Str.eq)" .
163.It
164A module's body runs once, after those of the modules it imports, and
165before the main module's
166.Cm main .
167.It
168.Cm "link \(dqlibX.so.1\(dq;"
169loads a C library (it needs
170.Cm "import SYSTEM" );
171.Cm "link static;"
172says the program must not use C.
173.Cm "check off;"
174leaves the module's index and slice checks out (it too needs
175.Cm "import SYSTEM" ).
176.It
177.Cm "import SYSTEM;"
178is no file but the language's module of what reaches memory by other
179roads than variables.
180A module needs it for
181.Cm SYSTEM.ADR ,
182.Cm SYSTEM.PTR ,
183.Cm SYSTEM.SYSCALL
184and
185.Cm SYSTEM.ARGV ;
186pointer arithmetic, indexing and slicing;
187conversions to and from pointers;
188.Cm extern ,
189.Cm link ,
190code procedures and
191.Cm "check off;" .
192It can't be renamed; a module that imports one that says it needn't.
193.El
194.Sh TYPES
195[5] Sizes are x86-64's.
196.Bl -column "U8 U16 U32 U64" "n * SIZE(T)" "an enumeration, 256 names at most"
197.It Sy Type Ta Sy Size Ta Sy Values
198.It "I8 I16 I32 I64" Ta "1, 2, 4, 8" Ta "signed, two's complement"
199.It "U8 U16 U32 U64" Ta "1, 2, 4, 8" Ta "unsigned"
200.It "INT, UINT" Ta "8" Ta "as big as an address, 16 bits at least"
201.It "BOOL" Ta "1" Ta "false, true"
202.It "F32, F64" Ta "4, 8" Ta "IEEE 754"
203.It "ERROR" Ta "8" Ta "nil, or a constant's error"
204.It "^T, proc ..." Ta "8" Ta "an address, or nil"
205.It "[]T" Ta "16" Ta "a pointer and a length"
206.It "[]const T" Ta "16" Ta "a slice, its elements only read"
207.It "[n]T" Ta "n * SIZE(T)" Ta "n elements, copied whole"
208.It "(a, b, c)" Ta "1" Ta "an enumeration, 256 names at most"
209.It "set of E" Ta "1, 2, 4, 8" Ta "bit i for E's name i, 64 at most"
210.El
211.Pp
212An enumeration's name may be written with its type,
213.Cm Color.red ,
214and must be where another declaration of its scope has the name too
215[5.7].
216.Bl -bullet
217.It
218A
219.Cm record
220is laid out as C's struct; a
221.Cm "packed record"
222has no padding, and its misaligned fields no address.
223.It
224A
225.Cm variant
226is an
227.Cm INT
228tag and the room of its largest case:
229.Cm "variant circle(r: F64); dot; end" .
230A case is built as
231.Cm circle(1.0)
232or
233.Cm dot ,
234and read only by
235.Cm match .
236A typed constant may hold variants:
237.Cm "const Shapes: []Shape = [circle(1.0), dot];"
238.It
239.Cm "^VOLATILE T"
240points at what something else may change: each access is one access,
241of its size, in order [7.6].
242.It
243.Cm "const NotFound*: ERROR = \(dqnot found\(dq;"
244makes an error unlike every other, compared by
245.Cm =
246and
247.Cm # ;
248.Cm MSG(e)
249is its message.
250.It
251Every variable starts at zero: 0, false, nil, the empty slice, a
252variant's first case.
253.It
254Named record, variant and enumeration types are each a type of their
255own; other types built alike are the same.
256.Cm "type A = B;"
257is another name for
258.Cm B .
259.El
260.Pp
261A value goes where another type is wanted only as an integer constant
262that fits it,
263.Cm nil
264into a pointer, procedure or
265.Cm ERROR ,
266.Cm ^T
267into
268.Cm "^VOLATILE T" ,
269.Cm [n]T
270into
271.Cm []T
272or
273.Cm []const T ,
274a
275.Cm []T
276into
277.Cm []const T ,
278a string into
279.Cm "[]const U8" , []U8 , ^U8
280(with a zero byte after it) or
281.Cm [n]U8 ,
282a procedure into its type,
283.Cm INT
284into
285.Cm I64 ,
286.Cm UINT
287into
288.Cm U64 ,
289wherever it goes, results passed on whole and a
290.Cm for Ns 's
291bounds too, and
292.Cm U8 , I8
293and
294.Cm I16
295into
296.Cm INT ,
297.Cm U8
298and
299.Cm U16
300into
301.Cm UINT ,
302in the same places but for results passed on whole, unless an
303operator made them: with
304.Cm "a, b: U8" ,
305.Cm "n := a + b"
306is an error, and
307.Cm "n := INT(a) + b"
308is the sum
309[5.10].
310Anything else is converted by name.
311.Sh OPERATORS
312[6] From the tightest:
313.Bl -column "* / div mod & shl shr" "and, or, not: BOOL; & | xor ~: bits"
314.It Sy Operators Ta Sy Notes
315.It "not ~" Ta "not of a BOOL; ~ of an integer's bits"
316.It "* / div mod & shl shr" Ta "/ for reals; shifts count modulo the width"
317.It "+ - | xor" Ta "a unary minus takes the first term"
318.It "= # < <= > >= in" Ta "# is different; in, for a set"
319.It "and" Ta "evaluates its right side only if needed"
320.It "or" Ta "the same"
321.El
322.Bl -bullet
323.It
324Both operands have one type, or one is an untyped constant that fits
325the other's, or one is an
326.Cm INT
327and the other an
328.Cm I64
329.Pq Cm UINT , U64 ,
330and the result is an
331.Cm I64 :
332with
333.Cm "i: I32" ,
334.Cm "i < LEN(s)"
335is an error.
336Integers wrap at their width.
337.Cm div
338rounds toward zero, and
339.Cm mod
340takes the dividend's sign; dividing by zero stops the program.
341.It
342Pointers:
343.Cm p^ ,
344.Cm "p + i"
345and
346.Cm "p - i"
347in elements,
348.Cm "p - q" ,
349.Cm p[i] ,
350and
351.Cm p[lo..hi] ,
352a slice; a record's fields through it are
353.Cm p^.f .
354Arithmetic, indexing and slicing need
355.Cm "import SYSTEM" .
356.It
357Arrays and slices:
358.Cm a[i] ,
359and
360.Cm a[lo..hi]
361from
362.Cm lo
363up to but not including
364.Cm hi .
365Two of integers, BOOLs, enumerations, sets or pointers compare with
366.Cm =
367and
368.Cm # .
369.It
370Sets:
371.Cm +
372union,
373.Cm *
374intersection,
375.Cm -
376difference,
377.Cm <=
378and
379.Cm >=
380subset and superset.
381.It
382Literals:
383.Cm "[1, 2, 3]" ,
384an array, and
385.Cm "{x: 1, y: 2}" ,
386a record with its other fields zero, where the type is known;
387.Cm "{red, blue}" ,
388a set.
389.Cm "const Weekend: []Day = [sat, sun];"
390takes its length from its literal [4].
391.El
392.Sh CONVERSIONS
393[6.7] A type's name converts:
394.Cm I32(x) , F64(n) , ^Cell(adr) , "^VOLATILE U32(0FEE00000H)" .
395.Bl -column "pointer, procedure" "pointer, procedure" "rounded toward zero"
396.It Sy From Ta Sy To Ta Sy Result
397.It "integer" Ta "integer" Ta "cut, or extended"
398.It "integer" Ta "F32, F64" Ta "the nearest"
399.It "F32, F64" Ta "integer" Ta "rounded toward zero"
400.It "F32 / F64" Ta "F64 / F32" Ta "the same, or the nearest"
401.It "enumeration, set" Ta "integer" Ta "its position, its bits"
402.It "integer" Ta "enumeration, set" Ta "checked; its low bits"
403.It "INT, UINT" Ta "pointer, procedure" Ta "the address"
404.It "pointer, procedure" Ta "INT, UINT, pointer" Ta "the address"
405.El
406.Pp
407The last two rows need
408.Cm "import SYSTEM" .
409There is no other: none between
410.Cm BOOL
411and integers, and none to records, arrays, slices, variants or
412.Cm ERROR .
413.Sh VIEWS
414[6.9]
415.Cm v.(T)
416is the
417.Cm T
418whose bytes start where the variable
419.Cm v
420does: a variable, not a copy, read and written.
421.Bd -literal -offset indent
422mem[16].(I64) := -1; (* mem[16] to mem[23] *)
423k := buf[0].(Header).count;
424bits := x.(U64); (* an F64's bits; I64(x) is its number *)
425.Ed
426.Pp
427.Cm T
428is an integer, a real or a pointer, or a record or array of them.
429.Cm "a[i].(T)"
430lies from
431.Cm a[i]
432to the end of
433.Cm a ,
434which is checked as an index is; any other
435.Cm v.(T)
436lies within
437.Cm v .
438A view may be misaligned, so it has no address, and a volatile
439variable can't be viewed.
440.Sh BUILT-IN PROCEDURES
441[6.8]
442.Bl -tag -width "SYSTEM.SYSCALL(n, x1, ...)"
443.It Fn SIZE T
444The size of type
445.Ar T ,
446an untyped constant.
447.It Fn SIZE T.f
448The size of field
449.Ar f
450of the record type
451.Ar T ,
452an untyped constant.
453.It Fn OFFSET T f
454The byte offset of field
455.Ar f
456in the record type
457.Ar T ,
458an untyped constant.
459.It Fn SYSTEM.ADR x
460The address of the variable
461.Ar x .
462.It Fn LEN a
463The length of an array, a string constant or a slice.
464.It Fn SYSTEM.PTR s
465The address the slice
466.Ar s
467starts at, a
468.Li ^T
469for a
470.Li []T ,
471unchecked: for an empty slice what it holds,
472.Dv nil
473for one never set.
474Of an array, the address of its first element.
475.It Fn NEW p a
476.Ar p
477set to a fresh, zeroed
478.Ar T
479from the allocator
480.Ar a ,
481a
482.Cm ^Mem.Allocator ,
483or to nil.
484.It Fn BYTES x
485The bytes of the variable
486.Ar x
487as a
488.Li []U8 :
489for a
490.Li []T
491its
492.Li LEN(s) * SIZE(T)
493bytes; of a type a view may take.
494.It Fn MSG e
495The message of an
496.Cm ERROR .
497.It Fn COPY d s
498The elements of
499.Ar s
500copied to the start of
501.Ar d ,
502as one block, even if they overlap; past
503.Fn LEN d ,
504a failed check.
505.It Fn FILL d x
506Every element of the array or slice
507.Ar d
508set to
509.Ar x ,
510a value of its type.
511.It Fn INC v k , Fn DEC v k
512.Ar v
513plus or minus
514.Ar k ,
515or 1; it wraps.
516.It Fn CAS v old new
517If the variable
518.Ar v
519is
520.Ar old ,
521set it to
522.Ar new
523and return true; atomically.
524.It Fn XCHG v x , Fn XADD v x
525Set, or add to, the variable
526.Ar v ,
527returning the value before; atomically.
528.It Fn ABS x
529.Ar x
530without its sign, of its type: an integer or a real.
531.It Fn ROL x n , Fn ROR x n
532.Ar x ,
533of an unsigned integer type, rotated left or right by
534.Ar n
535bits modulo its width.
536.It Fn CLZ x , Fn CTZ x
537The zero bits above the highest one, or below the lowest one, in
538.Ar x ,
539of an unsigned integer type; its width if
540.Ar x
541is 0.
542.It Fn POPCOUNT x
543The one bits in
544.Ar x ,
545of an unsigned integer type, or the members of a set.
546.It Fn MIN T , Fn MAX T
547The smallest, or the largest, value of the integer type
548.Ar T .
549.It Cm FENCE
550A full memory barrier.
551.It Fn SYSTEM.SYSCALL n x1 ...
552Linux system call
553.Ar n ,
554with up to six arguments; minus an errno on failure.
555.It Fn ARGC , Fn SYSTEM.ARGV
556The command line: its count, and its C strings.
557.El
558.Sh STATEMENTS
559[7]
560.Bl -tag -width "repeat ... until"
561.It Cm "x := e"
562Copies; arrays, records and slices whole.
563.Cm "a, b := f(x)"
564takes several results apart, and
565.Cm "_ := f(x)"
566drops one on purpose.
567.It Cm "f(x)"
568A call; a result it drops is warned of.
569.It Cm if , while , repeat
570On a
571.Cm BOOL .
572.Cm repeat
573runs its body once at least.
574.It Cm for
575.Cm "for i := a to b by s do ... end" :
576.Cm a
577and
578.Cm b
579evaluated once,
580.Cm s
581a constant, 1 if left out; never steps past
582.Cm b ,
583and leaves
584.Cm i
585at the last value it took.
586.It Cm match
587On a variant, an arm for each case, naming its fields; on an
588enumeration, an arm for each name; on an integer, arms of constants
589and ranges,
590.Cm "| '0' .. '9':" ,
591and an
592.Cm "| else:" ;
593on a string, arms of string constants,
594.Cm "| \(dqadd\(dq, \(dqplus\(dq:" ,
595and an
596.Cm "| else:" ,
597which any
598.Cm match
599may end with.
600An imported case may be written qualified,
601.Cm "| Xml.done:" .
602.It Cm return
603With the results, several separated by commas, or
604.Cm "return f(x)" ,
605passing on
606.Cm f Ns 's;
607off the end, a procedure returns zeros.
608.It Cm break
609Out of the innermost
610.Cm while , repeat
611or
612.Cm for .
613.It Cm continue
614To the end of the innermost loop's round: a
615.Cm while
616or a
617.Cm repeat
618tests again, a
619.Cm for
620steps; so a
621.Cm while Ns 's
622step comes before it.
623There is no
624.Cm goto .
625.It Cm defer
626Runs its statement when the statements it stands in end, by their end,
627a
628.Cm return ,
629a
630.Cm break
631or a
632.Cm continue ;
633the last first.
634.El
635.Pp
636Operands, arguments and an assignment's designators are evaluated left
637to right, the designators first [7.5].
638.Sh PROCEDURES
639[8]
640.Bl -bullet
641.It
642A parameter is a copy; a
643.Cm var
644one is the caller's variable, of the parameter's type exactly; a
645.Cm const
646one is the caller's variable too, read in place and never changed, and
647takes any expression of its type.
648A procedure takes what it reads as a value when it is a single value
649or a slice, as
650.Cm const
651when it is a record or an array; what it changes as
652.Cm var ;
653and a pointer only for what it keeps.
654.It
655.Cm "proc f(x: INT): (INT, ERROR);"
656returns several results.
657Returning a slice of a local array, of a value parameter, or of an
658array literal is an error: the frame it points into is gone [7.3].
659An array a call returns lives to the end of the procedure the call
660is in [8].
661A procedure is a value of its type,
662.Cm "proc(a, b: INT): BOOL" ,
663or nil.
664.It
665A
666.Cm code
667procedure is machine code, in Intel's syntax, called by the System V
668convention:
669.Cm "rdtsc; shl rdx, 32; or rax, rdx; ret;" .
670.It
671.Cm "extern proc printf(f: ^U8; ...): I32;"
672(in a module that imports SYSTEM) is C's [9]: its parameters integers, reals, pointers and procedures.
673.Cm "extern var environ: ^^U8;"
674is C's variable.
675A raz procedure of those types is a C callback.
676.It
677.Cm "proc main*;"
678or
679.Cm "proc main*: INT;"
680of the main module runs after every body; its result is the exit
681status.
682.Cm Rt.exit
683ends the program after the procedures given to
684.Cm Rt.atExit
685[10].
686.El
687.Sh WHAT IS CHECKED
688[6.5, 10] Each of these stops the program with status 2, or its
689signal's, saying on standard error where, why, and the calls under way
690with their variables:
691.Bl -bullet -compact
692.It
693an index past the length, and a slice out of
694.Cm "0 <= lo <= hi <= length" ,
695unless the module has checks off;
696.It
697a view that runs past the end of what it is made of, with the same
698proviso;
699.It
700a slice of a pointer that runs backwards, and a number that is no
701position of the enumeration it converts to;
702.It
703a
704.Cm nil
705pointer, a
706.Cm nil
707procedure, and a stack overflow;
708.It
709a division by zero, and the smallest value of a signed type divided by
710-1.
711.El
712.Pp
713Integers wrap and reals never stop the program.
714The compiler warns of what is declared and not used, of a variable
715only assigned, and of a dropped result;
716.Fl Werror
717makes them errors.
718What isn't checked is memory reached around the checks: a stale or
719made-up pointer, indexing through a pointer, C, and code procedures.
720.Sh LIMITS
721This compiler's, beyond the report's 63 characters to a name, 256
722names to an enumeration and 64 to a set:
723.Bl -column "deferred statements in a procedure" "integers and reals."
724.It "a string literal" Ta "65,535 bytes"
725.It "a number, as written" Ta "255 characters"
726.It "elements of a literal" Ta "1,024"
727.It "names in one declaration" Ta "64"
728.It "variables in one assignment" Ta "16"
729.It "deferred statements in a procedure" Ta "64"
730.It "labels in one match" Ta "64"
731.It "arguments of a call" Ta "63"
732.It "type arguments of a generic module" Ta "8"
733.It "scopes nested" Ta "64"
734.It "parameters of a code procedure" Ta "6 integers, 8 reals"
735.It "procedures given to Rt.atExit" Ta "32"
736.It "-I directories" Ta "16"
737.It "errors told in one run" Ta "20"
738.El
739.Sh SEE ALSO
740.Xr raz 1 ,
741.Xr raz-lib 3 ,
742.Xr raz-tutorial 7 ;
743the report,
744.Pa doc/raz.md ,
745and the guide to the compiler,
746.Pa doc/compiler.md ,
747in raz's sources.