| 1 | .\" raz-tutorial(7) - a tour of raz |
| 2 | .\" Every whole module in a literal block here is a program that make |
| 3 | .\" check compiles and runs, against test/man/Name.expect; see |
| 4 | .\" tools/Doc.raz. |
| 5 | .Dd October 15, 2026 |
| 6 | .Dt RAZ-TUTORIAL 7 |
| 7 | .Os |
| 8 | .Sh NAME |
| 9 | .Nm raz-tutorial |
| 10 | .Nd a tour of the raz language |
| 11 | .Sh DESCRIPTION |
| 12 | This tour is for someone who has programmed in Pascal or C and has |
| 13 | never seen raz. |
| 14 | Each section is one idea, with a program to run. |
| 15 | The programs are whole: save one under its module's name and run it. |
| 16 | .Pp |
| 17 | The language is defined by the report, |
| 18 | .Pa doc/raz.md |
| 19 | in raz's sources; where this page is short, the report says it all. |
| 20 | .Xr raz 7 |
| 21 | is the language at a glance, |
| 22 | .Xr raz-lib 3 |
| 23 | the library, and |
| 24 | .Xr raz 1 |
| 25 | the compiler. |
| 26 | .Sh 1. HELLO, WORLD |
| 27 | .Bd -literal -offset indent |
| 28 | module Hello; |
| 29 | import Out; |
| 30 | |
| 31 | proc main*; |
| 32 | begin |
| 33 | Out.str("hello, world\en"); |
| 34 | end; |
| 35 | |
| 36 | end Hello. |
| 37 | .Ed |
| 38 | .Pp |
| 39 | Saved as |
| 40 | .Pa Hello.raz , |
| 41 | it is compiled and run with |
| 42 | .Bd -literal -offset indent |
| 43 | raz Hello.raz && ./Hello |
| 44 | raz run Hello.raz |
| 45 | .Ed |
| 46 | .Pp |
| 47 | the second without writing a file. |
| 48 | A module lives in a file of its own name, and its name comes again |
| 49 | after its last |
| 50 | .Cm end . |
| 51 | .Cm import Out |
| 52 | lets it use what the module |
| 53 | .Cm Out |
| 54 | exports, as |
| 55 | .Cm Out.str . |
| 56 | The |
| 57 | .Cm * |
| 58 | after |
| 59 | .Cm main |
| 60 | exports it; the program starts at the |
| 61 | .Cm main |
| 62 | of the module named on the command line. |
| 63 | Every statement ends in |
| 64 | .Cm \&; , |
| 65 | even one that ends in |
| 66 | .Cm end . |
| 67 | .Sh 2. MODULES |
| 68 | A program is a set of modules. |
| 69 | Here is one, saved as |
| 70 | .Pa Greet.raz : |
| 71 | .Bd -literal -offset indent |
| 72 | module Greet; |
| 73 | import Out; |
| 74 | |
| 75 | var count*: INT; (* the greetings so far *) |
| 76 | |
| 77 | proc hello*(name: []const U8); |
| 78 | begin |
| 79 | Out.str("hello, "); |
| 80 | Out.str(name); |
| 81 | Out.ln; |
| 82 | INC(count); |
| 83 | end; |
| 84 | |
| 85 | begin |
| 86 | Out.str("Greet is ready\en"); |
| 87 | end Greet. |
| 88 | .Ed |
| 89 | .Pp |
| 90 | and a program beside it that uses it: |
| 91 | .Bd -literal -offset indent |
| 92 | module Twice; |
| 93 | import Out, G := Greet; |
| 94 | |
| 95 | proc main*; |
| 96 | begin |
| 97 | G.hello("world"); |
| 98 | G.hello("raz"); |
| 99 | Out.int(G.count); |
| 100 | Out.str(" greetings\en"); |
| 101 | end; |
| 102 | |
| 103 | end Twice. |
| 104 | .Ed |
| 105 | .Bd -literal -offset indent |
| 106 | Greet is ready |
| 107 | hello, world |
| 108 | hello, raz |
| 109 | 2 greetings |
| 110 | .Ed |
| 111 | .Pp |
| 112 | A name with |
| 113 | .Cm * |
| 114 | is exported; any other is the module's own. |
| 115 | .Cm "G := Greet" |
| 116 | imports |
| 117 | .Cm Greet |
| 118 | under a shorter name. |
| 119 | Another module may read |
| 120 | .Cm G.count , |
| 121 | but only |
| 122 | .Cm Greet |
| 123 | may change it. |
| 124 | The statements after a module's last |
| 125 | .Cm begin |
| 126 | are its body, which runs once, before |
| 127 | .Cm main , |
| 128 | after the bodies of the modules it imports. |
| 129 | .Pp |
| 130 | The compiler finds |
| 131 | .Cm Greet |
| 132 | beside the main module, then in each directory given with |
| 133 | .Fl I , |
| 134 | then in the library. |
| 135 | The library's module names, such as |
| 136 | .Cm Out , Str |
| 137 | and |
| 138 | .Cm Map , |
| 139 | are kept for it: a program doesn't name a module of its own so. |
| 140 | .Sh 3. VALUES, TYPES AND CONVERSIONS |
| 141 | .Bd -literal -offset indent |
| 142 | module Values; |
| 143 | import Fmt; |
| 144 | |
| 145 | const Max = 100; (* untyped: it takes the type it meets *) |
| 146 | |
| 147 | proc main*; |
| 148 | var n: INT; b: U8; big: I64; x: F64; c: I32; ok: BOOL; |
| 149 | begin |
| 150 | n := Max * 3; |
| 151 | b := 250; |
| 152 | b := b + 10; (* wraps, in a U8: 4 *) |
| 153 | big := n; (* an INT goes into an I64 *) |
| 154 | x := F64(n) / 7.0; (* nothing else converts unasked *) |
| 155 | c := '\eu{E9}'; (* a character is its code point *) |
| 156 | ok := (n > Max) and (b # 0); |
| 157 | Fmt.print("{} {} {} {} {} {}\en", [Fmt.int(n), Fmt.int(I64(b)), |
| 158 | Fmt.int(big), Fmt.real(x, 3), Fmt.int(I64(c)), |
| 159 | Fmt.bool(ok)]); |
| 160 | end; |
| 161 | |
| 162 | end Values. |
| 163 | .Ed |
| 164 | .Bd -literal -offset indent |
| 165 | 300 4 300 42.857 233 true |
| 166 | .Ed |
| 167 | .Pp |
| 168 | The integers are |
| 169 | .Cm I8 , I16 , I32 , I64 |
| 170 | and |
| 171 | .Cm U8 , U16 , U32 , U64 , |
| 172 | and |
| 173 | .Cm INT |
| 174 | and |
| 175 | .Cm UINT , |
| 176 | as big as an address. |
| 177 | .Cm INT |
| 178 | is the everyday one: lengths, indexes and counts are |
| 179 | .Cm INT Ns s . |
| 180 | The reals are |
| 181 | .Cm F32 |
| 182 | and |
| 183 | .Cm F64 . |
| 184 | Arithmetic wraps at the type's width, and never stops the program, |
| 185 | except dividing by zero. |
| 186 | .Pp |
| 187 | Two types never mix but where nothing can be lost: |
| 188 | .Cm INT |
| 189 | goes into |
| 190 | .Cm I64 , |
| 191 | wherever the one meets the other, so with |
| 192 | .Cm "big: I64" , |
| 193 | .Cm "big + n" |
| 194 | is an |
| 195 | .Cm I64 ; |
| 196 | and |
| 197 | .Cm U8 , I8 |
| 198 | and |
| 199 | .Cm I16 , |
| 200 | which an |
| 201 | .Cm INT |
| 202 | holds on any machine, go into |
| 203 | .Cm INT , |
| 204 | so a digit's value is |
| 205 | .Cm "n := n * 10 + s[i] - '0'" . |
| 206 | What an operator made in a small type doesn't, since it may have |
| 207 | wrapped there: with |
| 208 | .Cm "a, b: U8" , |
| 209 | .Cm "n := a + b" |
| 210 | is an error, and |
| 211 | .Cm "INT(a) + b" |
| 212 | the sum. |
| 213 | With |
| 214 | .Cm "c: I32" , |
| 215 | .Cm "c < n" |
| 216 | is an error. |
| 217 | A type's name converts, as |
| 218 | .Cm F64(n) |
| 219 | and |
| 220 | .Cm I64(b) |
| 221 | do above; an |
| 222 | .Cm I64 |
| 223 | becomes an |
| 224 | .Cm INT |
| 225 | only so, as |
| 226 | .Cm INT(big) , |
| 227 | since an |
| 228 | .Cm INT |
| 229 | may be smaller on another machine. |
| 230 | A number written in the program is untyped until it meets a type; |
| 231 | .Cm 0FFH |
| 232 | is hexadecimal, and |
| 233 | .Cm 1.5 |
| 234 | a real, which an integer never becomes without a conversion. |
| 235 | .Pp |
| 236 | .Cm # , |
| 237 | not |
| 238 | .Cm <> , |
| 239 | is |
| 240 | .Dq different . |
| 241 | .Cm and , or |
| 242 | and |
| 243 | .Cm not |
| 244 | are for |
| 245 | .Cm BOOL Ns s , |
| 246 | and bind below the relations, so |
| 247 | .Cm "i < n and a[i] # 0" |
| 248 | needs no parentheses; |
| 249 | .Cm & , | , xor |
| 250 | and |
| 251 | .Cm ~ |
| 252 | are for the bits of integers. |
| 253 | Every variable starts at zero. |
| 254 | .Sh 4. PROCEDURES AND SEVERAL RESULTS |
| 255 | .Bd -literal -offset indent |
| 256 | module Procs; |
| 257 | import Out; |
| 258 | |
| 259 | proc divmod(a, b: INT): (INT, INT); |
| 260 | begin |
| 261 | return a div b, a mod b; |
| 262 | end; |
| 263 | |
| 264 | proc swap(var a, b: INT); |
| 265 | var t: INT; |
| 266 | begin |
| 267 | t := a; |
| 268 | a := b; |
| 269 | b := t; |
| 270 | end; |
| 271 | |
| 272 | proc square(n: INT): INT; |
| 273 | begin |
| 274 | return n * n; |
| 275 | end; |
| 276 | |
| 277 | proc twice(f: proc(n: INT): INT; n: INT): INT; |
| 278 | begin |
| 279 | return f(f(n)); |
| 280 | end; |
| 281 | |
| 282 | proc main*; |
| 283 | var q, r, x, y: INT; |
| 284 | begin |
| 285 | q, r := divmod(17, 5); |
| 286 | x := 1; |
| 287 | y := 2; |
| 288 | swap(x, y); |
| 289 | Out.int(q); Out.char(' '); Out.int(r); Out.ln; |
| 290 | Out.int(x); Out.char(' '); Out.int(y); Out.ln; |
| 291 | Out.int(twice(square, 3)); Out.ln; |
| 292 | _, r := divmod(-7, 2); |
| 293 | Out.int(r); Out.ln; |
| 294 | end; |
| 295 | |
| 296 | end Procs. |
| 297 | .Ed |
| 298 | .Bd -literal -offset indent |
| 299 | 3 2 |
| 300 | 2 1 |
| 301 | 81 |
| 302 | -1 |
| 303 | .Ed |
| 304 | .Pp |
| 305 | A parameter is a copy, unless it is |
| 306 | .Cm var , |
| 307 | when it is the caller's variable itself. |
| 308 | A procedure may return several results, which a multiple assignment |
| 309 | takes apart; the blank, |
| 310 | .Cm _ , |
| 311 | drops one. |
| 312 | A procedure is a value too, of a procedure type, as |
| 313 | .Cm f |
| 314 | is in |
| 315 | .Cm twice . |
| 316 | A procedure without parameters leaves out the parentheses, as |
| 317 | .Cm Out.ln |
| 318 | does. |
| 319 | Procedures don't nest, and a name is declared before it is used. |
| 320 | .Pp |
| 321 | A call that drops a result, as |
| 322 | .Cm "Sys.write(fd, s);" |
| 323 | does, is warned of; |
| 324 | .Cm "_ := Sys.write(fd, s);" |
| 325 | says it is meant. |
| 326 | .Pp |
| 327 | A record or an array that a procedure only reads goes as |
| 328 | .Cm const : |
| 329 | the caller's variable itself, read in place with nothing copied, which |
| 330 | the procedure can't change. |
| 331 | .Bd -literal -offset indent |
| 332 | module Grids; |
| 333 | import Out; |
| 334 | |
| 335 | type Grid = record cells: [64]INT; w, h: INT; end; |
| 336 | |
| 337 | proc fill(var g: Grid; v: INT); (* changes g *) |
| 338 | var i: INT; |
| 339 | begin |
| 340 | for i := 0 to g.w * g.h - 1 do g.cells[i] := v; end; |
| 341 | end; |
| 342 | |
| 343 | proc sum(const g: Grid): INT; (* reads g in place *) |
| 344 | var i, s: INT; |
| 345 | begin |
| 346 | s := 0; |
| 347 | for i := 0 to g.w * g.h - 1 do s := s + g.cells[i]; end; |
| 348 | return s; |
| 349 | end; |
| 350 | |
| 351 | proc main*; |
| 352 | var g: Grid; |
| 353 | begin |
| 354 | g.w := 4; |
| 355 | g.h := 2; |
| 356 | fill(g, 3); |
| 357 | Out.int(sum(g)); Out.ln; |
| 358 | end; |
| 359 | |
| 360 | end Grids. |
| 361 | .Ed |
| 362 | .Bd -literal -offset indent |
| 363 | 24 |
| 364 | .Ed |
| 365 | .Pp |
| 366 | So a heading says what a procedure does with what it is given: a value |
| 367 | for a single value or a slice it reads, |
| 368 | .Cm const |
| 369 | for a record or an array it reads, |
| 370 | .Cm var |
| 371 | for what it changes, and a pointer only for what it keeps past the |
| 372 | call. |
| 373 | .Sh 5. ARRAYS, SLICES AND STRINGS |
| 374 | .Bd -literal -offset indent |
| 375 | module Slices; |
| 376 | import Out, Str; |
| 377 | |
| 378 | proc sum(s: []INT): INT; |
| 379 | var i, t: INT; |
| 380 | begin |
| 381 | t := 0; |
| 382 | for i := 0 to LEN(s) - 1 do t := t + s[i]; end; |
| 383 | return t; |
| 384 | end; |
| 385 | |
| 386 | proc main*; |
| 387 | var a: [5]INT; i: INT; s, val: []const U8; name: [8]U8; ok: BOOL; |
| 388 | begin |
| 389 | for i := 0 to 4 do a[i] := i * i; end; |
| 390 | Out.int(sum(a)); Out.ln; (* the array, as a slice *) |
| 391 | Out.int(sum(a[1..3])); Out.ln; (* a[1] and a[2] *) |
| 392 | s := "hello, world"; |
| 393 | Out.str(s[7..LEN(s)]); Out.ln; |
| 394 | if s[0..5] = "hello" then Out.str("it says hello\en"); end; |
| 395 | Out.int(Str.find(s, "world")); Out.ln; |
| 396 | _, val, ok := Str.cut(" size = 42 ", "="); |
| 397 | if ok then Out.str(Str.trim(val)); Out.ln; end; |
| 398 | name := "raz"; (* a copy, the program's own *) |
| 399 | name[0] := 'R'; |
| 400 | Out.str(name[0..3]); Out.ln; |
| 401 | Out.str(\(ga |
| 402 | C:\etmp\e "as it stands" |
| 403 | over lines |
| 404 | \(ga); |
| 405 | end; |
| 406 | |
| 407 | end Slices. |
| 408 | .Ed |
| 409 | .Bd -literal -offset indent |
| 410 | 30 |
| 411 | 5 |
| 412 | world |
| 413 | it says hello |
| 414 | 7 |
| 415 | 42 |
| 416 | Raz |
| 417 | C:\etmp\e "as it stands" |
| 418 | over lines |
| 419 | .Ed |
| 420 | .Pp |
| 421 | .Cm [5]INT |
| 422 | is an array: five |
| 423 | .Cm INT Ns s |
| 424 | in a row, copied when it is assigned. |
| 425 | .Cm []INT |
| 426 | is a slice: a pointer to elements that live elsewhere, and their |
| 427 | number, |
| 428 | .Cm LEN(s) . |
| 429 | .Cm a[lo..hi] |
| 430 | is the slice of the elements from |
| 431 | .Cm lo |
| 432 | up to, but not including, |
| 433 | .Cm hi , |
| 434 | and an array goes silently where a slice is wanted. |
| 435 | A slice of a local array is good only while its procedure runs, so |
| 436 | returning one is an error: take the array as a |
| 437 | .Cm var |
| 438 | parameter instead. |
| 439 | .Pp |
| 440 | A string is a slice of bytes, |
| 441 | .Cm []U8 , |
| 442 | in UTF-8, and its bytes are constant; |
| 443 | .Cm [8]U8 |
| 444 | made from one is a copy. |
| 445 | Arrays and slices of integers compare with |
| 446 | .Cm = |
| 447 | and |
| 448 | .Cm # ; |
| 449 | .Cm Str |
| 450 | has the rest: ordering, finding, trimming, cutting a string into |
| 451 | parts, which are slices of it, UTF-8, and numbers to and from text. |
| 452 | .Pp |
| 453 | A string between backquotes is raw: no escapes, and it may run over |
| 454 | lines. |
| 455 | When the opening backquote ends its line and the closing one has |
| 456 | only blanks before it, those blanks come off every line, so the text |
| 457 | stands at the indent of the code around it. |
| 458 | .Pp |
| 459 | A |
| 460 | .Cm for |
| 461 | loop takes its bounds once, and never steps past the last: with an |
| 462 | empty slice, |
| 463 | .Cm "0 to LEN(s) - 1" |
| 464 | doesn't run at all. |
| 465 | .Cm break |
| 466 | leaves a loop, and |
| 467 | .Cm continue |
| 468 | ends its round: a |
| 469 | .Cm for |
| 470 | steps, and a |
| 471 | .Cm while |
| 472 | tests again; so a |
| 473 | .Cm while |
| 474 | moves on, as |
| 475 | .Cm "i := i + n" , |
| 476 | before it can |
| 477 | .Cm continue . |
| 478 | .Sh 6. RECORDS AND POINTERS |
| 479 | .Bd -literal -offset indent |
| 480 | module Boxes; |
| 481 | import Fmt, SYSTEM; |
| 482 | |
| 483 | type |
| 484 | Point = record x, y: INT; end; |
| 485 | Box = record lo, hi: Point; end; |
| 486 | |
| 487 | proc area(b: Box): INT; |
| 488 | begin |
| 489 | return (b.hi.x - b.lo.x) * (b.hi.y - b.lo.y); |
| 490 | end; |
| 491 | |
| 492 | proc grow(b: ^Box; n: INT); |
| 493 | begin |
| 494 | DEC(b^.lo.x, n); |
| 495 | DEC(b^.lo.y, n); |
| 496 | INC(b^.hi.x, n); |
| 497 | INC(b^.hi.y, n); |
| 498 | end; |
| 499 | |
| 500 | proc main*; |
| 501 | var b: Box; p: ^Point; |
| 502 | begin |
| 503 | b := {lo: {x: 1, y: 1}, hi: {x: 4, y: 3}}; |
| 504 | Fmt.print("{}\en", [Fmt.int(area(b))]); |
| 505 | grow(SYSTEM.ADR(b), 1); |
| 506 | p := SYSTEM.ADR(b.hi); |
| 507 | p^.x := 10; |
| 508 | Fmt.print("{} {}\en", [Fmt.int(area(b)), Fmt.int(b.hi.x)]); |
| 509 | end; |
| 510 | |
| 511 | end Boxes. |
| 512 | .Ed |
| 513 | .Bd -literal -offset indent |
| 514 | 6 |
| 515 | 40 10 |
| 516 | .Ed |
| 517 | .Pp |
| 518 | A record is laid out as C lays out a struct. |
| 519 | .Cm "{lo: ..., hi: ...}" |
| 520 | builds one, naming its fields; the ones it leaves out are zero. |
| 521 | .Cm ^Box |
| 522 | is a pointer to a |
| 523 | .Cm Box , |
| 524 | .Cm SYSTEM.ADR(b) |
| 525 | the address of |
| 526 | .Cm b , |
| 527 | and |
| 528 | .Cm b^ |
| 529 | what a pointer points at. |
| 530 | A pointer is never the record: |
| 531 | .Cm b^.lo , |
| 532 | never |
| 533 | .Cm b.lo . |
| 534 | .Pp |
| 535 | A slice is a pointer and a length, and a record can keep the two |
| 536 | apart, as an interpreter keeps a string, or as C wants them: |
| 537 | .Bd -literal -offset indent |
| 538 | module Spans; |
| 539 | import Out, SYSTEM; |
| 540 | |
| 541 | type Span = record p: ^U8; n: INT; end; |
| 542 | |
| 543 | proc spanOf(s: []const U8): Span; |
| 544 | begin |
| 545 | return {p: SYSTEM.PTR(s), n: LEN(s)}; |
| 546 | end; |
| 547 | |
| 548 | proc text(sp: Span): []U8; |
| 549 | begin |
| 550 | return sp.p[0..sp.n]; |
| 551 | end; |
| 552 | |
| 553 | proc main*; |
| 554 | var words: [3]Span; i: INT; |
| 555 | begin |
| 556 | words := [spanOf("one"), spanOf(""), spanOf("three")]; |
| 557 | for i := 0 to 2 do |
| 558 | Out.int(words[i].n); Out.char(' '); |
| 559 | Out.str(text(words[i])); Out.ln; |
| 560 | end; |
| 561 | end; |
| 562 | |
| 563 | end Spans. |
| 564 | .Ed |
| 565 | .Bd -literal -offset indent |
| 566 | 3 one |
| 567 | 0 |
| 568 | 5 three |
| 569 | .Ed |
| 570 | .Pp |
| 571 | .Cm LEN(s) |
| 572 | is the length, and |
| 573 | .Cm SYSTEM.PTR(s) |
| 574 | the pointer, a |
| 575 | .Cm ^U8 |
| 576 | for a |
| 577 | .Cm []U8 , |
| 578 | with no check. |
| 579 | .Cm SYSTEM.ADR(s[0]) |
| 580 | looks the same, and passes every test but the one with an empty |
| 581 | string: there |
| 582 | .Cm s[0] |
| 583 | is out of range, and the program stops. |
| 584 | .Cm SYSTEM.PTR |
| 585 | of an empty slice is what the slice holds, which is |
| 586 | .Cm nil |
| 587 | for one never set, so nothing needs testing first. |
| 588 | .Cm sp.p[0..sp.n] |
| 589 | is the slice again, empty when |
| 590 | .Cm sp.n |
| 591 | is 0, whatever |
| 592 | .Cm sp.p |
| 593 | is. |
| 594 | Of an array, |
| 595 | .Cm SYSTEM.PTR(a) |
| 596 | is the address of its first element. |
| 597 | .Sh 7. BYTES AND VIEWS |
| 598 | A file, a network packet or a device hands a program bytes, and the |
| 599 | program wants numbers and records out of them. |
| 600 | A |
| 601 | .Em view |
| 602 | reads and writes bytes as another type. |
| 603 | .Bd -literal -offset indent |
| 604 | module Packet; |
| 605 | import Out; |
| 606 | |
| 607 | type Header = record kind: U16; flags: U16; size: U32; end; |
| 608 | |
| 609 | var buf: [16]U8; |
| 610 | |
| 611 | proc main*; |
| 612 | var i: INT; n: I64; h: Header; x: F64; |
| 613 | begin |
| 614 | buf[0].(I64) := 258; (* buf[0] to buf[7] now hold 258 *) |
| 615 | for i := 0 to 7 do Out.int(I64(buf[i])); Out.char(' '); end; |
| 616 | Out.ln; |
| 617 | n := buf[0].(I64); (* and give it back *) |
| 618 | Out.int(n); Out.ln; |
| 619 | |
| 620 | buf[8].(Header) := {kind: 1, size: 4096}; |
| 621 | h := buf[8].(Header); |
| 622 | Out.int(I64(h.size)); Out.ln; |
| 623 | INC(buf[8].(Header).size, 4); |
| 624 | Out.int(I64(buf[8].(Header).size)); Out.ln; |
| 625 | |
| 626 | x := 1.0; |
| 627 | Out.hex(x.(U64), 16); Out.ln; (* the bits of 1.0 *) |
| 628 | Out.int(I64(x)); Out.ln; (* the number 1.0 *) |
| 629 | end; |
| 630 | |
| 631 | end Packet. |
| 632 | .Ed |
| 633 | .Bd -literal -offset indent |
| 634 | 2 1 0 0 0 0 0 0 |
| 635 | 258 |
| 636 | 4096 |
| 637 | 4100 |
| 638 | 3FF0000000000000 |
| 639 | 1 |
| 640 | .Ed |
| 641 | .Pp |
| 642 | Read |
| 643 | .Cm buf[0].(I64) |
| 644 | as |
| 645 | .Dq "the I64 whose bytes start at buf[0]" . |
| 646 | An |
| 647 | .Cm I64 |
| 648 | is 8 bytes, so the view is made of |
| 649 | .Cm buf[0] |
| 650 | to |
| 651 | .Cm buf[7] : |
| 652 | .Bd -literal -offset indent |
| 653 | buf: [0] [1] [2] [3] [4] [5] [6] [7] [8] ... |
| 654 | 2 1 0 0 0 0 0 0 |
| 655 | \e______ buf[0].(I64) = 258 ______/ |
| 656 | .Ed |
| 657 | .Pp |
| 658 | Nothing is copied to make a view: it |
| 659 | .Em is |
| 660 | those bytes, as |
| 661 | .Cm p^ |
| 662 | is the variable |
| 663 | .Cm p |
| 664 | points at. |
| 665 | Assigning it writes them, and so changes |
| 666 | .Cm buf ; |
| 667 | reading it reads them. |
| 668 | So a view goes wherever a variable goes: on the left of |
| 669 | .Cm := , |
| 670 | in |
| 671 | .Cm INC , |
| 672 | and with a field or an index after it, as |
| 673 | .Cm buf[8].(Header).size |
| 674 | is the |
| 675 | .Cm size |
| 676 | field of the |
| 677 | .Cm Header |
| 678 | whose bytes start at |
| 679 | .Cm buf[8] . |
| 680 | .Pp |
| 681 | 258 is |
| 682 | .Cm 102H , |
| 683 | and its low byte, 2, comes first: x86-64 puts a number's low byte |
| 684 | first in memory. |
| 685 | Another machine may put it last; a format with an order of its own is |
| 686 | read a byte at a time, with |
| 687 | .Cm Str.le |
| 688 | or |
| 689 | .Cm Str.be , |
| 690 | if the program is to run everywhere. |
| 691 | .Pp |
| 692 | A view isn't a conversion. |
| 693 | A conversion works on the value, a view on the bytes: |
| 694 | .Cm I64(x) |
| 695 | is 1, the number, while |
| 696 | .Cm x.(U64) |
| 697 | is |
| 698 | .Cm 3FF0000000000000H , |
| 699 | the bits that hold 1.0. |
| 700 | When the program means a number it wants a conversion; when it means |
| 701 | the bytes as they lie, a view. |
| 702 | .Pp |
| 703 | The view must lie inside the variable it is made of. |
| 704 | For an element, as |
| 705 | .Cm buf[i].(I64) , |
| 706 | that is from |
| 707 | .Cm buf[i] |
| 708 | to the end of |
| 709 | .Cm buf , |
| 710 | and the program checks it as it checks an index: |
| 711 | .Bd -literal -offset indent |
| 712 | module Overrun; |
| 713 | import Out; |
| 714 | |
| 715 | var buf: [16]U8; |
| 716 | |
| 717 | proc main*; |
| 718 | var i: INT; |
| 719 | begin |
| 720 | i := 0; |
| 721 | while i < 16 do |
| 722 | buf[i].(I32) := 1; |
| 723 | i := i + 3; |
| 724 | end; |
| 725 | Out.str("done\en"); |
| 726 | end; |
| 727 | |
| 728 | end Overrun. |
| 729 | .Ed |
| 730 | .Bd -literal -offset indent |
| 731 | Overrun.raz:11:13: a view at index 15 runs past a length of 16 |
| 732 | in Overrun.main at Overrun.raz:11:13: i = 15 |
| 733 | .Ed |
| 734 | .Pp |
| 735 | The |
| 736 | .Cm I32 |
| 737 | at |
| 738 | .Cm buf[15] |
| 739 | would be |
| 740 | .Cm buf[15] |
| 741 | to |
| 742 | .Cm buf[18] , |
| 743 | three bytes past the end. |
| 744 | An index the compiler knows, it checks itself: |
| 745 | .Cm buf[14].(I32) |
| 746 | doesn't compile. |
| 747 | A view of a whole variable must fit in it: |
| 748 | .Cm x.(U64) |
| 749 | needs |
| 750 | .Cm x |
| 751 | to have 8 bytes at least. |
| 752 | .Pp |
| 753 | A view is an integer, a real or a pointer, or a record or array made |
| 754 | of them: types that any bytes are a value of. |
| 755 | A |
| 756 | .Cm BOOL |
| 757 | or an enumeration isn't, since some bytes are none of its values: |
| 758 | view an integer, and compare or convert it. |
| 759 | A view may start at any byte, so it has no address: |
| 760 | .Cm SYSTEM.ADR |
| 761 | of it is an error. |
| 762 | Copy it to a variable first. |
| 763 | Before views, a program made a pointer for each access, as |
| 764 | .Cm "p := ^I64(SYSTEM.ADR(buf[0]))" |
| 765 | and then |
| 766 | .Cm "p^ := 258" , |
| 767 | which checks nothing. |
| 768 | .Sh 8. MEMORY AND ALLOCATORS |
| 769 | .Bd -literal -offset indent |
| 770 | module Stack; |
| 771 | import Out, Mem, Heap, Arena, SYSTEM; |
| 772 | |
| 773 | type Node = record next: ^Node; val: INT; end; |
| 774 | |
| 775 | (* a node holding v on top of top, from the allocator a; |
| 776 | nil if a has no room *) |
| 777 | proc push(top: ^Node; v: INT; a: ^Mem.Allocator): ^Node; |
| 778 | var n: ^Node; |
| 779 | begin |
| 780 | NEW(n, a); |
| 781 | if n # nil then |
| 782 | n^.next := top; |
| 783 | n^.val := v; |
| 784 | end; |
| 785 | return n; |
| 786 | end; |
| 787 | |
| 788 | proc main*; |
| 789 | var top, n: ^Node; i: INT; ar: Arena.Arena; |
| 790 | begin |
| 791 | for i := 1 to 3 do top := push(top, i, Heap.heap); end; |
| 792 | while top # nil do |
| 793 | Out.int(top^.val); Out.char(' '); |
| 794 | n := top; |
| 795 | top := top^.next; |
| 796 | Mem.free(Heap.heap, ^U8(n)); |
| 797 | end; |
| 798 | Out.ln; |
| 799 | if not Arena.init(ar, 100000) then return; end; |
| 800 | for i := 1 to 1000 do top := push(top, i, SYSTEM.ADR(ar.base)); end; |
| 801 | Out.int(top^.val); Out.ln; |
| 802 | Arena.done(ar); (* every node at once *) |
| 803 | end; |
| 804 | |
| 805 | end Stack. |
| 806 | .Ed |
| 807 | .Bd -literal -offset indent |
| 808 | 3 2 1 |
| 809 | 1000 |
| 810 | .Ed |
| 811 | .Pp |
| 812 | raz has no garbage collector, and no allocator of its own: memory |
| 813 | comes from an allocator, a |
| 814 | .Cm Mem.Allocator , |
| 815 | which the program chooses and passes. |
| 816 | .Cm "NEW(n, a)" |
| 817 | sets |
| 818 | .Cm n |
| 819 | to a fresh, zeroed |
| 820 | .Cm Node |
| 821 | from |
| 822 | .Cm a , |
| 823 | or to |
| 824 | .Cm nil |
| 825 | if |
| 826 | .Cm a |
| 827 | has no room. |
| 828 | .Cm Heap.heap |
| 829 | is the general one, which |
| 830 | .Cm Mem.free |
| 831 | gives memory back to. |
| 832 | An |
| 833 | .Cm Arena |
| 834 | hands out memory from one block, and frees it all at once. |
| 835 | .Cm Buf , List |
| 836 | and |
| 837 | .Cm Map |
| 838 | take the allocator they use from |
| 839 | .Cm init . |
| 840 | .Sh 9. ERRORS |
| 841 | .Bd -literal -offset indent |
| 842 | module Errors; |
| 843 | import Out, Str; |
| 844 | |
| 845 | const |
| 846 | Empty*: ERROR = "nothing to read"; |
| 847 | TooBig*: ERROR = "not a number up to 1000"; |
| 848 | |
| 849 | (* the number s holds, up to 1000 *) |
| 850 | proc number(s: []const U8): (INT, ERROR); |
| 851 | var n: I64; ok: BOOL; |
| 852 | begin |
| 853 | if LEN(s) = 0 then return 0, Empty; end; |
| 854 | n, ok := Str.toInt(s); |
| 855 | if not ok or (n > 1000) then return 0, TooBig; end; |
| 856 | return INT(n), nil; |
| 857 | end; |
| 858 | |
| 859 | (* the sum of the numbers in words: the first error ends it *) |
| 860 | proc total(words: [][]const U8): (INT, ERROR); |
| 861 | var i, sum: INT; |
| 862 | begin |
| 863 | for i := 0 to LEN(words) - 1 do |
| 864 | sum := sum + try number(words[i]); |
| 865 | end; |
| 866 | return sum, nil; |
| 867 | end; |
| 868 | |
| 869 | proc main*; |
| 870 | var words: [3][]const U8; i, n: INT; err: ERROR; |
| 871 | begin |
| 872 | words := ["42", "", "12345"]; |
| 873 | for i := 0 to 2 do |
| 874 | n, err := number(words[i]); |
| 875 | if err = nil then Out.int(n); |
| 876 | elsif err = Empty then Out.str("empty"); |
| 877 | else Out.str(MSG(err)); |
| 878 | end; |
| 879 | Out.ln; |
| 880 | end; |
| 881 | n, err := total(["1", "2", "3"]); |
| 882 | Out.int(n); Out.ln; |
| 883 | n, err := total(words); |
| 884 | if err # nil then Out.str(MSG(err)); Out.ln; end; |
| 885 | end; |
| 886 | |
| 887 | end Errors. |
| 888 | .Ed |
| 889 | .Bd -literal -offset indent |
| 890 | 42 |
| 891 | empty |
| 892 | not a number up to 1000 |
| 893 | 6 |
| 894 | nothing to read |
| 895 | .Ed |
| 896 | .Pp |
| 897 | A procedure that can fail returns an |
| 898 | .Cm ERROR |
| 899 | beside its result, and |
| 900 | .Cm nil |
| 901 | when it didn't. |
| 902 | Each |
| 903 | .Cm ERROR |
| 904 | constant is an error of its own, compared with |
| 905 | .Cm = |
| 906 | by which declaration made it, never by its message; |
| 907 | .Cm MSG(err) |
| 908 | is the message. |
| 909 | An exported one, as |
| 910 | .Cm Empty |
| 911 | is here, lets the callers tell it from the others. |
| 912 | .Pp |
| 913 | A caller that can't deal with an error passes it on to its own caller |
| 914 | with |
| 915 | .Cm try , |
| 916 | as |
| 917 | .Cm total |
| 918 | does: if the call returned an error that isn't |
| 919 | .Cm nil , |
| 920 | the procedure |
| 921 | .Cm try |
| 922 | stands in returns that error, with zero for its other results, and |
| 923 | otherwise |
| 924 | .Cm try |
| 925 | is the call's other results, here a number. |
| 926 | .Cm try |
| 927 | is a |
| 928 | .Cm return , |
| 929 | written where the call is: it has no handler, keeps no state and |
| 930 | unwinds nothing, so every place a procedure can leave is in its text. |
| 931 | The procedure it stands in must itself end in an |
| 932 | .Cm ERROR , |
| 933 | so |
| 934 | .Cm main , |
| 935 | which returns none, tests each error and deals with it. |
| 936 | raz has no exceptions. |
| 937 | .Sh 10. DEFER |
| 938 | .Bd -literal -offset indent |
| 939 | module Defers; |
| 940 | import Out, Sys; |
| 941 | |
| 942 | proc step(n: INT); |
| 943 | begin |
| 944 | Out.str("in "); Out.int(n); Out.ln; |
| 945 | defer Out.str("out\en"); |
| 946 | if n = 2 then return; end; |
| 947 | defer Out.str("done\en"); |
| 948 | Out.str("working\en"); |
| 949 | end; |
| 950 | |
| 951 | (* the lines of the file at path, or -1 if it can't be read *) |
| 952 | proc lines(path: []const U8): INT; |
| 953 | var fd, n, i, count: INT; buf: [4096]U8; |
| 954 | begin |
| 955 | fd := Sys.openRead(path); |
| 956 | if fd < 0 then return -1; end; |
| 957 | defer Sys.close(fd); |
| 958 | count := 0; |
| 959 | n := Sys.read(fd, buf); |
| 960 | while n > 0 do |
| 961 | for i := 0 to n - 1 do |
| 962 | if buf[i] = '\en' then INC(count); end; |
| 963 | end; |
| 964 | n := Sys.read(fd, buf); |
| 965 | end; |
| 966 | return count; |
| 967 | end; |
| 968 | |
| 969 | proc main*; |
| 970 | begin |
| 971 | step(1); |
| 972 | step(2); |
| 973 | Out.int(lines("/dev/null")); Out.ln; |
| 974 | Out.int(lines("/no/such/file")); Out.ln; |
| 975 | end; |
| 976 | |
| 977 | end Defers. |
| 978 | .Ed |
| 979 | .Bd -literal -offset indent |
| 980 | in 1 |
| 981 | working |
| 982 | done |
| 983 | out |
| 984 | in 2 |
| 985 | out |
| 986 | 0 |
| 987 | -1 |
| 988 | .Ed |
| 989 | .Pp |
| 990 | .Cm "defer s;" |
| 991 | runs |
| 992 | .Cm s |
| 993 | when the program leaves the statements the |
| 994 | .Cm defer |
| 995 | stands in: at their end, or by a |
| 996 | .Cm return , |
| 997 | a |
| 998 | .Cm break |
| 999 | or a |
| 1000 | .Cm continue . |
| 1001 | Deferred statements run the last first, and only those whose |
| 1002 | .Cm defer |
| 1003 | was reached. |
| 1004 | So a file is closed beside the line that opens it, however the |
| 1005 | procedure ends. |
| 1006 | .Sh 11. CHECKS, AND WHAT A FAILURE SAYS |
| 1007 | .Bd -literal -offset indent |
| 1008 | module Fail; |
| 1009 | import Out; |
| 1010 | |
| 1011 | proc show(a: []INT; i: INT); |
| 1012 | begin |
| 1013 | Out.int(a[i]); |
| 1014 | Out.ln; |
| 1015 | end; |
| 1016 | |
| 1017 | proc main*; |
| 1018 | var a: [3]INT; i: INT; |
| 1019 | begin |
| 1020 | for i := 0 to 3 do |
| 1021 | show(a, i); |
| 1022 | end; |
| 1023 | end; |
| 1024 | |
| 1025 | end Fail. |
| 1026 | .Ed |
| 1027 | .Bd -literal -offset indent |
| 1028 | 0 |
| 1029 | 0 |
| 1030 | 0 |
| 1031 | Fail.raz:6:15: index 3 is out of range for a length of 3 |
| 1032 | in Fail.show at Fail.raz:6:15: i = 3 |
| 1033 | in Fail.main at Fail.raz:14:9: i = 3 |
| 1034 | .Ed |
| 1035 | .Pp |
| 1036 | Every index and slice is checked. |
| 1037 | A failed check says where it failed and with what values, then the |
| 1038 | calls under way with their variables, and ends the program with |
| 1039 | status 2. |
| 1040 | So do a |
| 1041 | .Cm nil |
| 1042 | pointer, a division by zero and a stack overflow. |
| 1043 | .Pp |
| 1044 | A module that is sure of its indexes may say |
| 1045 | .Cm "check off;" |
| 1046 | after its imports (it must import SYSTEM), for speed; an index out of range there reads or |
| 1047 | writes whatever it reaches. |
| 1048 | The program carries line numbers for |
| 1049 | .Xr gdb 1 , |
| 1050 | and |
| 1051 | .Ev RAZ_PROFILE Ns =1 |
| 1052 | in its environment has it say where its time went; |
| 1053 | .Xr raz 1 |
| 1054 | says more. |
| 1055 | .Sh 12. ENUMERATIONS, SETS, VARIANTS AND MATCH |
| 1056 | .Bd -literal -offset indent |
| 1057 | module Shapes; |
| 1058 | import Out, Fmt, Math; |
| 1059 | |
| 1060 | type |
| 1061 | Colour = (red, green, blue); |
| 1062 | Colours = set of Colour; |
| 1063 | Shape = variant |
| 1064 | circle(r: F64); |
| 1065 | rect(w, h: F64); |
| 1066 | dot; |
| 1067 | end; |
| 1068 | |
| 1069 | const Some: []Shape = [circle(1.0), rect(2.0, 3.5), dot]; |
| 1070 | |
| 1071 | proc area(const s: Shape): F64; |
| 1072 | begin |
| 1073 | match s of |
| 1074 | | circle(r): return Math.pi * r * r; |
| 1075 | | rect(w, h): return w * h; |
| 1076 | | dot: return 0.0; |
| 1077 | end; |
| 1078 | end; |
| 1079 | |
| 1080 | proc kind(c: I32): []const U8; |
| 1081 | begin |
| 1082 | match c of |
| 1083 | | ' ', '\et', '\en': return "space"; |
| 1084 | | '0' .. '9': return "digit"; |
| 1085 | | 'a' .. 'z', 'A' .. 'Z': return "letter"; |
| 1086 | | else: return "other"; |
| 1087 | end; |
| 1088 | end; |
| 1089 | |
| 1090 | proc colour(name: []const U8): Colour; |
| 1091 | begin |
| 1092 | match name of |
| 1093 | | "red", "rose": return red; |
| 1094 | | "green": return green; |
| 1095 | | else: return blue; |
| 1096 | end; |
| 1097 | end; |
| 1098 | |
| 1099 | proc main*; |
| 1100 | var i: INT; warm: Colours; c: Colour; |
| 1101 | begin |
| 1102 | for i := 0 to LEN(Some) - 1 do |
| 1103 | Fmt.print("{}\en", [Fmt.real(area(Some[i]), 2)]); |
| 1104 | end; |
| 1105 | Fmt.print("{} {} {}\en", [Fmt.str(kind('7')), Fmt.str(kind('x')), |
| 1106 | Fmt.str(kind('+'))]); |
| 1107 | warm := {red, blue} - {blue} + {green}; |
| 1108 | for c := red to blue do |
| 1109 | if c in warm then Out.int(I64(c)); Out.ln; end; |
| 1110 | end; |
| 1111 | Out.int(I64(colour("rose"))); Out.int(I64(colour("green"))); Out.ln; |
| 1112 | end; |
| 1113 | |
| 1114 | end Shapes. |
| 1115 | .Ed |
| 1116 | .Bd -literal -offset indent |
| 1117 | 3.14 |
| 1118 | 7.00 |
| 1119 | 0.00 |
| 1120 | digit letter other |
| 1121 | 0 |
| 1122 | 1 |
| 1123 | 01 |
| 1124 | .Ed |
| 1125 | .Pp |
| 1126 | An enumeration is a list of names, in order, each of which may be |
| 1127 | written with its type, |
| 1128 | .Cm Color.red ; |
| 1129 | two enumerations may share a name, which is then always written so. |
| 1130 | A |
| 1131 | .Cm "set of" |
| 1132 | it holds any choice of them, in the bits of a word, with |
| 1133 | .Cm in , |
| 1134 | .Cm + |
| 1135 | for union, |
| 1136 | .Cm * |
| 1137 | for intersection and |
| 1138 | .Cm - |
| 1139 | for difference. |
| 1140 | .Pp |
| 1141 | A variant holds one of its cases, each with fields of its own, and |
| 1142 | .Cm match |
| 1143 | is the only way to read them: it runs the arm of the case the variant |
| 1144 | holds, naming its fields. |
| 1145 | A constant may hold variants, each a case built from constants, as |
| 1146 | .Cm Some |
| 1147 | does; a |
| 1148 | .Cm match |
| 1149 | on one names fields that are only read. |
| 1150 | Every case has an arm, unless an |
| 1151 | .Cm "| else:" |
| 1152 | comes last. |
| 1153 | On an integer, |
| 1154 | .Cm match |
| 1155 | takes constants and ranges, and on a string, string constants; both |
| 1156 | must have an |
| 1157 | .Cm "| else:" . |
| 1158 | .Sh 13. GENERIC MODULES |
| 1159 | A module may take types, and the procedures it needs on them, as |
| 1160 | parameters. |
| 1161 | .Pa Biggest.raz : |
| 1162 | .Bd -literal -offset indent |
| 1163 | module Biggest(T; less: proc(a, b: T): BOOL); |
| 1164 | |
| 1165 | (* the biggest of s, which mustn't be empty *) |
| 1166 | proc max*(s: []T): T; |
| 1167 | var i: INT; m: T; |
| 1168 | begin |
| 1169 | m := s[0]; |
| 1170 | for i := 1 to LEN(s) - 1 do |
| 1171 | if less(m, s[i]) then m := s[i]; end; |
| 1172 | end; |
| 1173 | return m; |
| 1174 | end; |
| 1175 | |
| 1176 | end Biggest. |
| 1177 | .Ed |
| 1178 | .Bd -literal -offset indent |
| 1179 | module Generics; |
| 1180 | import Out, Ints := Biggest(INT, before), |
| 1181 | Words := Biggest([]const U8, shorter), Order := Sort(INT, before); |
| 1182 | |
| 1183 | proc before(a, b: INT): BOOL; |
| 1184 | begin |
| 1185 | return a < b; |
| 1186 | end; |
| 1187 | |
| 1188 | proc shorter(a, b: []const U8): BOOL; |
| 1189 | begin |
| 1190 | return LEN(a) < LEN(b); |
| 1191 | end; |
| 1192 | |
| 1193 | proc main*; |
| 1194 | var a: [5]INT; i: INT; |
| 1195 | begin |
| 1196 | a := [3, 1, 4, 1, 5]; |
| 1197 | Out.int(Ints.max(a)); Out.ln; |
| 1198 | Out.str(Words.max(["a", "tree", "is"])); Out.ln; |
| 1199 | Order.sort(a); |
| 1200 | for i := 0 to 4 do Out.int(a[i]); Out.char(' '); end; |
| 1201 | Out.ln; |
| 1202 | end; |
| 1203 | |
| 1204 | end Generics. |
| 1205 | .Ed |
| 1206 | .Bd -literal -offset indent |
| 1207 | 5 |
| 1208 | tree |
| 1209 | 1 1 3 4 5 |
| 1210 | .Ed |
| 1211 | .Pp |
| 1212 | Each import with its own arguments makes an instance: the module |
| 1213 | compiled again, with |
| 1214 | .Cm T |
| 1215 | standing for the type given, and |
| 1216 | .Cm less |
| 1217 | calling the procedure given. |
| 1218 | The arguments may name what the importing module declares later, as |
| 1219 | .Cm before |
| 1220 | is here. |
| 1221 | The library's |
| 1222 | .Cm List , Map , Vec , |
| 1223 | a growable array, and |
| 1224 | .Cm Sort |
| 1225 | are generic modules: |
| 1226 | .Bd -literal -offset indent |
| 1227 | import Count := Map([]const U8, INT, Str.hash, Str.eq); |
| 1228 | .Ed |
| 1229 | .Sh 14. TALKING TO C |
| 1230 | .Bd -literal -offset indent |
| 1231 | module Qsort; |
| 1232 | import Out, Fmt, SYSTEM; |
| 1233 | |
| 1234 | link "libm.so.6"; |
| 1235 | |
| 1236 | extern proc qsort(base: ^U8; n, size: UINT; |
| 1237 | less: proc(a, b: ^U8): I32); |
| 1238 | extern proc cbrt(x: F64): F64; |
| 1239 | |
| 1240 | (* the order of two INTs, as C's qsort wants it *) |
| 1241 | proc order(a, b: ^U8): I32; |
| 1242 | var x, y: ^INT; |
| 1243 | begin |
| 1244 | x := ^INT(a); |
| 1245 | y := ^INT(b); |
| 1246 | if x^ < y^ then return -1; elsif x^ > y^ then return 1; end; |
| 1247 | return 0; |
| 1248 | end; |
| 1249 | |
| 1250 | proc main*; |
| 1251 | var a: [5]INT; i: INT; |
| 1252 | begin |
| 1253 | a := [30, 10, 50, 20, 40]; |
| 1254 | qsort(^U8(SYSTEM.ADR(a)), LEN(a), SIZE(INT), order); |
| 1255 | for i := 0 to 4 do Out.int(a[i]); Out.char(' '); end; |
| 1256 | Out.ln; |
| 1257 | Fmt.print("{}\en", [Fmt.real(cbrt(27.0), 3)]); |
| 1258 | end; |
| 1259 | |
| 1260 | end Qsort. |
| 1261 | .Ed |
| 1262 | .Bd -literal -offset indent |
| 1263 | 10 20 30 40 50 |
| 1264 | 3.000 |
| 1265 | .Ed |
| 1266 | .Pp |
| 1267 | A module that says |
| 1268 | .Cm "import SYSTEM" |
| 1269 | may declare |
| 1270 | .Cm extern |
| 1271 | procedures and name libraries with |
| 1272 | .Cm link . |
| 1273 | An |
| 1274 | .Cm extern |
| 1275 | procedure is found by its name in the C library, or in a library a |
| 1276 | .Cm link |
| 1277 | line names. |
| 1278 | Its parameters are integers, reals, pointers and procedures, as C |
| 1279 | declares them; a raz procedure of such types can be given to C to call |
| 1280 | back, as |
| 1281 | .Cm order |
| 1282 | is. |
| 1283 | .Cm ^INT(a) |
| 1284 | sees the address |
| 1285 | .Cm a |
| 1286 | as a pointer to an |
| 1287 | .Cm INT . |
| 1288 | A slice goes to C as two arguments, |
| 1289 | .Cm SYSTEM.PTR(s) |
| 1290 | and |
| 1291 | .Cm LEN(s) , |
| 1292 | which an empty slice gives too (section 6). |
| 1293 | .Pp |
| 1294 | A program that uses C is linked against it dynamically, and starts |
| 1295 | through it; one that doesn't is a static executable with nothing in it |
| 1296 | but its own code. |
| 1297 | .Cm Out |
| 1298 | and C's |
| 1299 | .Xr printf 3 |
| 1300 | keep two buffers, so a program that writes with both flushes one |
| 1301 | before it writes with the other. |
| 1302 | .Sh 15. THREADS |
| 1303 | .Bd -literal -offset indent |
| 1304 | module Hits; |
| 1305 | import Out, Thread, SYSTEM; |
| 1306 | |
| 1307 | const Workers = 4; |
| 1308 | |
| 1309 | var |
| 1310 | hits: INT; (* added to by every thread at once, by XADD *) |
| 1311 | m: Thread.Mutex; |
| 1312 | total: INT; (* added to under m *) |
| 1313 | |
| 1314 | proc work(arg: ^U8); |
| 1315 | var i, n, sum: INT; |
| 1316 | begin |
| 1317 | n := INT(arg); |
| 1318 | sum := 0; |
| 1319 | for i := 1 to n do |
| 1320 | sum := sum + i; |
| 1321 | _ := XADD(hits, 1); |
| 1322 | end; |
| 1323 | Thread.lock(m); |
| 1324 | total := total + sum; |
| 1325 | Thread.unlock(m); |
| 1326 | end; |
| 1327 | |
| 1328 | proc main*; |
| 1329 | var t: [Workers]^Thread.Thread; i: INT; err: ERROR; |
| 1330 | begin |
| 1331 | for i := 0 to Workers - 1 do |
| 1332 | t[i], err := Thread.spawn(work, ^U8(1000 * (i + 1))); |
| 1333 | if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 1334 | end; |
| 1335 | for i := 0 to Workers - 1 do Thread.join(t[i]); end; |
| 1336 | Out.int(hits); Out.char(' '); Out.int(total); Out.ln; |
| 1337 | end; |
| 1338 | |
| 1339 | end Hits. |
| 1340 | .Ed |
| 1341 | .Bd -literal -offset indent |
| 1342 | 10000 15005000 |
| 1343 | .Ed |
| 1344 | .Pp |
| 1345 | .Cm Thread.spawn |
| 1346 | runs a procedure in a thread of its own, with a stack of its own, and |
| 1347 | .Cm Thread.join |
| 1348 | waits for it to end. |
| 1349 | .Cm XADD , |
| 1350 | .Cm CAS |
| 1351 | and |
| 1352 | .Cm XCHG |
| 1353 | change a word atomically; a |
| 1354 | .Cm Thread.Mutex |
| 1355 | keeps a longer section to one thread at a time. |
| 1356 | .Cm Out |
| 1357 | and |
| 1358 | .Cm Heap |
| 1359 | lock themselves. |
| 1360 | .Pp |
| 1361 | .Cm Thread |
| 1362 | makes its own threads, without C, in a program that doesn't use C; in |
| 1363 | one that does, it makes them with C's |
| 1364 | .Xr pthread_create 3 |
| 1365 | instead, and the program is the same. |
| 1366 | .Cm Thread.signal |
| 1367 | sends a signal to one thread alone, so that, with |
| 1368 | .Cm Sys.handle , |
| 1369 | another thread can break into a read it waits in. |
| 1370 | .Sh 16. FROM PASCAL AND C TO RAZ |
| 1371 | .Bl -column "for (i=0; i<n; i++)" "for i := 0 to n-1 do" "Mem.free(Heap.heap, ^U8(p))" |
| 1372 | .It Sy C Ta Sy Pascal Ta Sy raz |
| 1373 | .It "#include" Ta "uses" Ta "import Out, Str;" |
| 1374 | .It "static" Ta "implementation" Ta "a name without *" |
| 1375 | .It "int, long" Ta "integer" Ta "I32, INT, I64" |
| 1376 | .It "char *s" Ta "string" Ta "s: []U8" |
| 1377 | .It "\(dqit's\(dq" Ta "'it''s'" Ta "\(dqit's\(dq" |
| 1378 | .It "0xFF" Ta "$FF" Ta "0FFH" |
| 1379 | .It "/* ... */" Ta "{ ... }" Ta "(* ... *), nesting" |
| 1380 | .It "x == y" Ta "x = y" Ta "x = y" |
| 1381 | .It "x != y" Ta "x <> y" Ta "x # y" |
| 1382 | .It "a && b" Ta "a and b" Ta "a and b" |
| 1383 | .It "x & m" Ta "x and m" Ta "x & m" |
| 1384 | .It "i++" Ta "inc(i)" Ta "INC(i)" |
| 1385 | .It "(long)x" Ta "longint(x)" Ta "I64(x)" |
| 1386 | .It "if (c) {...}" Ta "if c then begin" Ta "if c then ... end;" |
| 1387 | .It "for (i=0; i<n; i++)" Ta "for i := 0 to n-1 do" Ta "for i := 0 to n - 1 do" |
| 1388 | .It "switch (c)" Ta "case c of" Ta "match c of | 1: ..." |
| 1389 | .It "continue" Ta "continue" Ta "continue;" |
| 1390 | .It "goto" Ta "goto" Ta "none" |
| 1391 | .It "union" Ta "variant record" Ta "variant" |
| 1392 | .It "struct p {...}" Ta "record ... end" Ta "record ... end" |
| 1393 | .It "&x" Ta "@x" Ta "SYSTEM.ADR(x)" |
| 1394 | .It "p->f" Ta "p^.f" Ta "p^.f" |
| 1395 | .It "NULL" Ta "nil" Ta "nil" |
| 1396 | .It "malloc(sizeof *p)" Ta "new(p)" Ta "NEW(p, Heap.heap)" |
| 1397 | .It "free(p)" Ta "dispose(p)" Ta "Mem.free(Heap.heap, ^U8(p))" |
| 1398 | .It "int f(int *out)" Ta "function f(var x)" Ta "proc f: (INT, INT);" |
| 1399 | .It "void f(T *p)" Ta "procedure f(var p: T)" Ta "proc f(var p: T);" |
| 1400 | .It "f(const T *p)" Ta "procedure f(const p: T)" Ta "proc f(const p: T);" |
| 1401 | .It "return -1; errno" Ta "exceptions" Ta "return 0, NotFound;" |
| 1402 | .It "(void)f(x);" Ta "f(x);" Ta "_ := f(x);" |
| 1403 | .It "goto cleanup;" Ta "try ... finally" Ta "defer close(fd);" |
| 1404 | .It "printf(...)" Ta "writeln(...)" Ta "Out.str(s), Fmt.print(...)" |
| 1405 | .El |
| 1406 | .Pp |
| 1407 | And what neither has: every index is checked, every variable starts |
| 1408 | at zero, and no two integer types mix in an expression. |
| 1409 | .Sh SEE ALSO |
| 1410 | .Xr raz 1 , |
| 1411 | .Xr raz-lib 3 , |
| 1412 | .Xr raz 7 ; |
| 1413 | the report, |
| 1414 | .Pa doc/raz.md , |
| 1415 | in raz's sources. |