| | @@ -0,0 +1,990 @@ |
| 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 12, 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: []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: with |
| 188 | +.Cm "i: I64" , |
| 189 | +.Cm "i < LEN(s)" |
| 190 | +is an error. |
| 191 | +A type's name converts, as |
| 192 | +.Cm F64(n) |
| 193 | +and |
| 194 | +.Cm I64(b) |
| 195 | +do above. |
| 196 | +The one silent conversion between numbers is an |
| 197 | +.Cm INT |
| 198 | +into an |
| 199 | +.Cm I64 , |
| 200 | +which loses nothing. |
| 201 | +A number written in the program is untyped until it meets a type; |
| 202 | +.Cm 0FFH |
| 203 | +is hexadecimal, and |
| 204 | +.Cm 1.5 |
| 205 | +a real, which an integer never becomes without a conversion. |
| 206 | +.Pp |
| 207 | +.Cm # , |
| 208 | +not |
| 209 | +.Cm <> , |
| 210 | +is |
| 211 | +.Dq different . |
| 212 | +.Cm and , or |
| 213 | +and |
| 214 | +.Cm not |
| 215 | +are for |
| 216 | +.Cm BOOL Ns s , |
| 217 | +and bind below the relations, so |
| 218 | +.Cm "i < n and a[i] # 0" |
| 219 | +needs no parentheses; |
| 220 | +.Cm & , | , xor |
| 221 | +and |
| 222 | +.Cm ~ |
| 223 | +are for the bits of integers. |
| 224 | +Every variable starts at zero. |
| 225 | +.Sh 4. PROCEDURES AND SEVERAL RESULTS |
| 226 | +.Bd -literal -offset indent |
| 227 | +module Procs; |
| 228 | +import Out; |
| 229 | + |
| 230 | +proc divmod(a, b: INT): (INT, INT); |
| 231 | +begin |
| 232 | + return a div b, a mod b; |
| 233 | +end; |
| 234 | + |
| 235 | +proc swap(var a, b: INT); |
| 236 | +var t: INT; |
| 237 | +begin |
| 238 | + t := a; |
| 239 | + a := b; |
| 240 | + b := t; |
| 241 | +end; |
| 242 | + |
| 243 | +proc square(n: INT): INT; |
| 244 | +begin |
| 245 | + return n * n; |
| 246 | +end; |
| 247 | + |
| 248 | +proc twice(f: proc(n: INT): INT; n: INT): INT; |
| 249 | +begin |
| 250 | + return f(f(n)); |
| 251 | +end; |
| 252 | + |
| 253 | +proc main*; |
| 254 | +var q, r, x, y: INT; |
| 255 | +begin |
| 256 | + q, r := divmod(17, 5); |
| 257 | + x := 1; |
| 258 | + y := 2; |
| 259 | + swap(x, y); |
| 260 | + Out.int(q); Out.char(' '); Out.int(r); Out.ln; |
| 261 | + Out.int(x); Out.char(' '); Out.int(y); Out.ln; |
| 262 | + Out.int(twice(square, 3)); Out.ln; |
| 263 | + _, r := divmod(-7, 2); |
| 264 | + Out.int(r); Out.ln; |
| 265 | +end; |
| 266 | + |
| 267 | +end Procs. |
| 268 | +.Ed |
| 269 | +.Bd -literal -offset indent |
| 270 | +3 2 |
| 271 | +2 1 |
| 272 | +81 |
| 273 | +-1 |
| 274 | +.Ed |
| 275 | +.Pp |
| 276 | +A parameter is a copy, unless it is |
| 277 | +.Cm var , |
| 278 | +when it is the caller's variable itself. |
| 279 | +A procedure may return several results, which a multiple assignment |
| 280 | +takes apart; the blank, |
| 281 | +.Cm _ , |
| 282 | +drops one. |
| 283 | +A procedure is a value too, of a procedure type, as |
| 284 | +.Cm f |
| 285 | +is in |
| 286 | +.Cm twice . |
| 287 | +A procedure without parameters leaves out the parentheses, as |
| 288 | +.Cm Out.ln |
| 289 | +does. |
| 290 | +Procedures don't nest, and a name is declared before it is used. |
| 291 | +.Pp |
| 292 | +A call that drops a result, as |
| 293 | +.Cm "Sys.write(fd, s);" |
| 294 | +does, is warned of; |
| 295 | +.Cm "_ := Sys.write(fd, s);" |
| 296 | +says it is meant. |
| 297 | +.Sh 5. ARRAYS, SLICES AND STRINGS |
| 298 | +.Bd -literal -offset indent |
| 299 | +module Slices; |
| 300 | +import Out, Str; |
| 301 | + |
| 302 | +proc sum(s: []INT): INT; |
| 303 | +var i, t: INT; |
| 304 | +begin |
| 305 | + t := 0; |
| 306 | + for i := 0 to LEN(s) - 1 do t := t + s[i]; end; |
| 307 | + return t; |
| 308 | +end; |
| 309 | + |
| 310 | +proc main*; |
| 311 | +var a: [5]INT; i: INT; s: []U8; name: [8]U8; |
| 312 | +begin |
| 313 | + for i := 0 to 4 do a[i] := i * i; end; |
| 314 | + Out.int(sum(a)); Out.ln; (* the array, as a slice *) |
| 315 | + Out.int(sum(a[1..3])); Out.ln; (* a[1] and a[2] *) |
| 316 | + s := "hello, world"; |
| 317 | + Out.str(s[7..LEN(s)]); Out.ln; |
| 318 | + if s[0..5] = "hello" then Out.str("it says hello\en"); end; |
| 319 | + Out.int(Str.find(s, "world")); Out.ln; |
| 320 | + name := "raz"; (* a copy, the program's own *) |
| 321 | + name[0] := 'R'; |
| 322 | + Out.str(name[0..3]); Out.ln; |
| 323 | +end; |
| 324 | + |
| 325 | +end Slices. |
| 326 | +.Ed |
| 327 | +.Bd -literal -offset indent |
| 328 | +30 |
| 329 | +5 |
| 330 | +world |
| 331 | +it says hello |
| 332 | +7 |
| 333 | +Raz |
| 334 | +.Ed |
| 335 | +.Pp |
| 336 | +.Cm [5]INT |
| 337 | +is an array: five |
| 338 | +.Cm INT Ns s |
| 339 | +in a row, copied when it is assigned. |
| 340 | +.Cm []INT |
| 341 | +is a slice: a pointer to elements that live elsewhere, and their |
| 342 | +number, |
| 343 | +.Cm LEN(s) . |
| 344 | +.Cm a[lo..hi] |
| 345 | +is the slice of the elements from |
| 346 | +.Cm lo |
| 347 | +up to, but not including, |
| 348 | +.Cm hi , |
| 349 | +and an array goes silently where a slice is wanted. |
| 350 | +.Pp |
| 351 | +A string is a slice of bytes, |
| 352 | +.Cm []U8 , |
| 353 | +in UTF-8, and its bytes are constant; |
| 354 | +.Cm [8]U8 |
| 355 | +made from one is a copy. |
| 356 | +Arrays and slices of integers compare with |
| 357 | +.Cm = |
| 358 | +and |
| 359 | +.Cm # ; |
| 360 | +.Cm Str |
| 361 | +has the rest: ordering, finding, hashing, UTF-8 and reading a number. |
| 362 | +.Pp |
| 363 | +A |
| 364 | +.Cm for |
| 365 | +loop takes its bounds once, and never steps past the last: with an |
| 366 | +empty slice, |
| 367 | +.Cm "0 to LEN(s) - 1" |
| 368 | +doesn't run at all. |
| 369 | +.Sh 6. RECORDS AND POINTERS |
| 370 | +.Bd -literal -offset indent |
| 371 | +module Boxes; |
| 372 | +import Fmt; |
| 373 | + |
| 374 | +type |
| 375 | + Point = record x, y: INT; end; |
| 376 | + Box = record lo, hi: Point; end; |
| 377 | + |
| 378 | +proc area(b: Box): INT; |
| 379 | +begin |
| 380 | + return (b.hi.x - b.lo.x) * (b.hi.y - b.lo.y); |
| 381 | +end; |
| 382 | + |
| 383 | +proc grow(b: ^Box; n: INT); |
| 384 | +begin |
| 385 | + DEC(b^.lo.x, n); |
| 386 | + DEC(b^.lo.y, n); |
| 387 | + INC(b^.hi.x, n); |
| 388 | + INC(b^.hi.y, n); |
| 389 | +end; |
| 390 | + |
| 391 | +proc main*; |
| 392 | +var b: Box; p: ^Point; |
| 393 | +begin |
| 394 | + b := {lo: {x: 1, y: 1}, hi: {x: 4, y: 3}}; |
| 395 | + Fmt.print("{}\en", [Fmt.int(area(b))]); |
| 396 | + grow(ADR(b), 1); |
| 397 | + p := ADR(b.hi); |
| 398 | + p^.x := 10; |
| 399 | + Fmt.print("{} {}\en", [Fmt.int(area(b)), Fmt.int(b.hi.x)]); |
| 400 | +end; |
| 401 | + |
| 402 | +end Boxes. |
| 403 | +.Ed |
| 404 | +.Bd -literal -offset indent |
| 405 | +6 |
| 406 | +40 10 |
| 407 | +.Ed |
| 408 | +.Pp |
| 409 | +A record is laid out as C lays out a struct. |
| 410 | +.Cm "{lo: ..., hi: ...}" |
| 411 | +builds one, naming its fields; the ones it leaves out are zero. |
| 412 | +.Cm ^Box |
| 413 | +is a pointer to a |
| 414 | +.Cm Box , |
| 415 | +.Cm ADR(b) |
| 416 | +the address of |
| 417 | +.Cm b , |
| 418 | +and |
| 419 | +.Cm b^ |
| 420 | +what a pointer points at. |
| 421 | +A pointer is never the record: |
| 422 | +.Cm b^.lo , |
| 423 | +never |
| 424 | +.Cm b.lo . |
| 425 | +.Sh 7. MEMORY AND ALLOCATORS |
| 426 | +.Bd -literal -offset indent |
| 427 | +module Stack; |
| 428 | +import Out, Mem, Heap, Arena; |
| 429 | + |
| 430 | +type Node = record next: ^Node; val: INT; end; |
| 431 | + |
| 432 | +(* a node holding v on top of top, from the allocator a; |
| 433 | + nil if a has no room *) |
| 434 | +proc push(top: ^Node; v: INT; a: ^Mem.Allocator): ^Node; |
| 435 | +var n: ^Node; |
| 436 | +begin |
| 437 | + NEW(n, a); |
| 438 | + if n # nil then |
| 439 | + n^.next := top; |
| 440 | + n^.val := v; |
| 441 | + end; |
| 442 | + return n; |
| 443 | +end; |
| 444 | + |
| 445 | +proc main*; |
| 446 | +var top, n: ^Node; i: INT; ar: Arena.Arena; |
| 447 | +begin |
| 448 | + for i := 1 to 3 do top := push(top, i, Heap.heap); end; |
| 449 | + while top # nil do |
| 450 | + Out.int(top^.val); Out.char(' '); |
| 451 | + n := top; |
| 452 | + top := top^.next; |
| 453 | + Mem.free(Heap.heap, ^U8(n)); |
| 454 | + end; |
| 455 | + Out.ln; |
| 456 | + if not Arena.init(ADR(ar), 100000) then return; end; |
| 457 | + for i := 1 to 1000 do top := push(top, i, ADR(ar.base)); end; |
| 458 | + Out.int(top^.val); Out.ln; |
| 459 | + Arena.done(ADR(ar)); (* every node at once *) |
| 460 | +end; |
| 461 | + |
| 462 | +end Stack. |
| 463 | +.Ed |
| 464 | +.Bd -literal -offset indent |
| 465 | +3 2 1 |
| 466 | +1000 |
| 467 | +.Ed |
| 468 | +.Pp |
| 469 | +raz has no garbage collector, and no allocator of its own: memory |
| 470 | +comes from an allocator, a |
| 471 | +.Cm Mem.Allocator , |
| 472 | +which the program chooses and passes. |
| 473 | +.Cm "NEW(n, a)" |
| 474 | +sets |
| 475 | +.Cm n |
| 476 | +to a fresh, zeroed |
| 477 | +.Cm Node |
| 478 | +from |
| 479 | +.Cm a , |
| 480 | +or to |
| 481 | +.Cm nil |
| 482 | +if |
| 483 | +.Cm a |
| 484 | +has no room. |
| 485 | +.Cm Heap.heap |
| 486 | +is the general one, which |
| 487 | +.Cm Mem.free |
| 488 | +gives memory back to. |
| 489 | +An |
| 490 | +.Cm Arena |
| 491 | +hands out memory from one block, and frees it all at once. |
| 492 | +.Cm Buf , List |
| 493 | +and |
| 494 | +.Cm Map |
| 495 | +take the allocator they use from |
| 496 | +.Cm init . |
| 497 | +.Sh 8. ERRORS |
| 498 | +.Bd -literal -offset indent |
| 499 | +module Errors; |
| 500 | +import Out, Str; |
| 501 | + |
| 502 | +const |
| 503 | + Empty*: ERROR = "nothing to read"; |
| 504 | + TooBig*: ERROR = "not a number up to 1000"; |
| 505 | + |
| 506 | +(* the number s holds, up to 1000 *) |
| 507 | +proc number(s: []U8): (INT, ERROR); |
| 508 | +var n: I64; ok: BOOL; |
| 509 | +begin |
| 510 | + if LEN(s) = 0 then return 0, Empty; end; |
| 511 | + n, ok := Str.toInt(s); |
| 512 | + if not ok or (n > 1000) then return 0, TooBig; end; |
| 513 | + return INT(n), nil; |
| 514 | +end; |
| 515 | + |
| 516 | +proc main*; |
| 517 | +var words: [3][]U8; i, n: INT; err: ERROR; |
| 518 | +begin |
| 519 | + words := ["42", "", "12345"]; |
| 520 | + for i := 0 to 2 do |
| 521 | + n, err := number(words[i]); |
| 522 | + if err = nil then Out.int(n); |
| 523 | + elsif err = Empty then Out.str("empty"); |
| 524 | + else Out.str(MSG(err)); |
| 525 | + end; |
| 526 | + Out.ln; |
| 527 | + end; |
| 528 | +end; |
| 529 | + |
| 530 | +end Errors. |
| 531 | +.Ed |
| 532 | +.Bd -literal -offset indent |
| 533 | +42 |
| 534 | +empty |
| 535 | +not a number up to 1000 |
| 536 | +.Ed |
| 537 | +.Pp |
| 538 | +A procedure that can fail returns an |
| 539 | +.Cm ERROR |
| 540 | +beside its result, and |
| 541 | +.Cm nil |
| 542 | +when it didn't. |
| 543 | +Each |
| 544 | +.Cm ERROR |
| 545 | +constant is an error of its own, compared with |
| 546 | +.Cm = |
| 547 | +by which declaration made it, never by its message; |
| 548 | +.Cm MSG(err) |
| 549 | +is the message. |
| 550 | +An exported one, as |
| 551 | +.Cm Empty |
| 552 | +is here, lets the callers tell it from the others. |
| 553 | +raz has no exceptions. |
| 554 | +.Sh 9. DEFER |
| 555 | +.Bd -literal -offset indent |
| 556 | +module Defers; |
| 557 | +import Out, Sys; |
| 558 | + |
| 559 | +proc step(n: INT); |
| 560 | +begin |
| 561 | + Out.str("in "); Out.int(n); Out.ln; |
| 562 | + defer Out.str("out\en"); |
| 563 | + if n = 2 then return; end; |
| 564 | + defer Out.str("done\en"); |
| 565 | + Out.str("working\en"); |
| 566 | +end; |
| 567 | + |
| 568 | +(* the lines of the file at path, or -1 if it can't be read *) |
| 569 | +proc lines(path: []U8): INT; |
| 570 | +var fd, n, i, count: INT; buf: [4096]U8; |
| 571 | +begin |
| 572 | + fd := Sys.openRead(path); |
| 573 | + if fd < 0 then return -1; end; |
| 574 | + defer Sys.close(fd); |
| 575 | + count := 0; |
| 576 | + n := Sys.read(fd, buf); |
| 577 | + while n > 0 do |
| 578 | + for i := 0 to n - 1 do |
| 579 | + if buf[i] = '\en' then INC(count); end; |
| 580 | + end; |
| 581 | + n := Sys.read(fd, buf); |
| 582 | + end; |
| 583 | + return count; |
| 584 | +end; |
| 585 | + |
| 586 | +proc main*; |
| 587 | +begin |
| 588 | + step(1); |
| 589 | + step(2); |
| 590 | + Out.int(lines("/dev/null")); Out.ln; |
| 591 | + Out.int(lines("/no/such/file")); Out.ln; |
| 592 | +end; |
| 593 | + |
| 594 | +end Defers. |
| 595 | +.Ed |
| 596 | +.Bd -literal -offset indent |
| 597 | +in 1 |
| 598 | +working |
| 599 | +done |
| 600 | +out |
| 601 | +in 2 |
| 602 | +out |
| 603 | +0 |
| 604 | +-1 |
| 605 | +.Ed |
| 606 | +.Pp |
| 607 | +.Cm "defer s;" |
| 608 | +runs |
| 609 | +.Cm s |
| 610 | +when the program leaves the statements the |
| 611 | +.Cm defer |
| 612 | +stands in: at their end, or by a |
| 613 | +.Cm return |
| 614 | +or a |
| 615 | +.Cm break . |
| 616 | +Deferred statements run the last first, and only those whose |
| 617 | +.Cm defer |
| 618 | +was reached. |
| 619 | +So a file is closed beside the line that opens it, however the |
| 620 | +procedure ends. |
| 621 | +.Sh 10. CHECKS, AND WHAT A FAILURE SAYS |
| 622 | +.Bd -literal -offset indent |
| 623 | +module Fail; |
| 624 | +import Out; |
| 625 | + |
| 626 | +proc get(a: []INT; i: INT): INT; |
| 627 | +begin |
| 628 | + return a[i]; |
| 629 | +end; |
| 630 | + |
| 631 | +proc main*; |
| 632 | +var a: [3]INT; i: INT; |
| 633 | +begin |
| 634 | + for i := 0 to 3 do |
| 635 | + Out.int(get(a, i)); |
| 636 | + Out.ln; |
| 637 | + end; |
| 638 | +end; |
| 639 | + |
| 640 | +end Fail. |
| 641 | +.Ed |
| 642 | +.Bd -literal -offset indent |
| 643 | +0 |
| 644 | +0 |
| 645 | +0 |
| 646 | +Fail.raz:6:14: index 3 is out of range for a length of 3 |
| 647 | + in Fail.get at Fail.raz:6:14: i = 3 |
| 648 | + in Fail.main at Fail.raz:13:9: i = 3 |
| 649 | +.Ed |
| 650 | +.Pp |
| 651 | +Every index and slice is checked. |
| 652 | +A failed check says where it failed and with what values, then the |
| 653 | +calls under way with their variables, and ends the program with |
| 654 | +status 2. |
| 655 | +So do a |
| 656 | +.Cm nil |
| 657 | +pointer, a division by zero and a stack overflow. |
| 658 | +.Pp |
| 659 | +A module that is sure of its indexes may say |
| 660 | +.Cm "check off;" |
| 661 | +after its imports, for speed; an index out of range there reads or |
| 662 | +writes whatever it reaches. |
| 663 | +The program carries line numbers for |
| 664 | +.Xr gdb 1 , |
| 665 | +and |
| 666 | +.Ev RAZ_PROFILE Ns =1 |
| 667 | +in its environment has it say where its time went; |
| 668 | +.Xr raz 1 |
| 669 | +says more. |
| 670 | +.Sh 11. ENUMERATIONS, SETS, VARIANTS AND MATCH |
| 671 | +.Bd -literal -offset indent |
| 672 | +module Shapes; |
| 673 | +import Out, Fmt, Math; |
| 674 | + |
| 675 | +type |
| 676 | + Colour = (red, green, blue); |
| 677 | + Colours = set of Colour; |
| 678 | + Shape = variant |
| 679 | + circle(r: F64); |
| 680 | + rect(w, h: F64); |
| 681 | + dot; |
| 682 | + end; |
| 683 | + |
| 684 | +proc area(s: Shape): F64; |
| 685 | +begin |
| 686 | + match s of |
| 687 | + | circle(r): return Math.pi * r * r; |
| 688 | + | rect(w, h): return w * h; |
| 689 | + | dot: return 0.0; |
| 690 | + end; |
| 691 | +end; |
| 692 | + |
| 693 | +proc kind(c: I32): []U8; |
| 694 | +begin |
| 695 | + match c of |
| 696 | + | ' ', '\et', '\en': return "space"; |
| 697 | + | '0' .. '9': return "digit"; |
| 698 | + | 'a' .. 'z', 'A' .. 'Z': return "letter"; |
| 699 | + | else: return "other"; |
| 700 | + end; |
| 701 | +end; |
| 702 | + |
| 703 | +proc main*; |
| 704 | +var shapes: [3]Shape; i: INT; warm: Colours; c: Colour; |
| 705 | +begin |
| 706 | + shapes := [circle(1.0), rect(2.0, 3.5), dot]; |
| 707 | + for i := 0 to 2 do |
| 708 | + Fmt.print("{}\en", [Fmt.real(area(shapes[i]), 2)]); |
| 709 | + end; |
| 710 | + Fmt.print("{} {} {}\en", [Fmt.str(kind('7')), Fmt.str(kind('x')), |
| 711 | + Fmt.str(kind('+'))]); |
| 712 | + warm := {red, blue} - {blue} + {green}; |
| 713 | + for c := red to blue do |
| 714 | + if c in warm then Out.int(I64(c)); Out.ln; end; |
| 715 | + end; |
| 716 | +end; |
| 717 | + |
| 718 | +end Shapes. |
| 719 | +.Ed |
| 720 | +.Bd -literal -offset indent |
| 721 | +3.14 |
| 722 | +7.00 |
| 723 | +0.00 |
| 724 | +digit letter other |
| 725 | +0 |
| 726 | +1 |
| 727 | +.Ed |
| 728 | +.Pp |
| 729 | +An enumeration is a list of names, in order; a |
| 730 | +.Cm "set of" |
| 731 | +it holds any choice of them, in the bits of a word, with |
| 732 | +.Cm in , |
| 733 | +.Cm + |
| 734 | +for union, |
| 735 | +.Cm * |
| 736 | +for intersection and |
| 737 | +.Cm - |
| 738 | +for difference. |
| 739 | +.Pp |
| 740 | +A variant holds one of its cases, each with fields of its own, and |
| 741 | +.Cm match |
| 742 | +is the only way to read them: it runs the arm of the case the variant |
| 743 | +holds, naming its fields. |
| 744 | +Every case has an arm, unless an |
| 745 | +.Cm "| else:" |
| 746 | +comes last. |
| 747 | +On an integer, |
| 748 | +.Cm match |
| 749 | +takes constants and ranges, and must have an |
| 750 | +.Cm "| else:" . |
| 751 | +.Sh 12. GENERIC MODULES |
| 752 | +A module may take types, and the procedures it needs on them, as |
| 753 | +parameters. |
| 754 | +.Pa Biggest.raz : |
| 755 | +.Bd -literal -offset indent |
| 756 | +module Biggest(T; less: proc(a, b: T): BOOL); |
| 757 | + |
| 758 | +(* the biggest of s, which mustn't be empty *) |
| 759 | +proc max*(s: []T): T; |
| 760 | +var i: INT; m: T; |
| 761 | +begin |
| 762 | + m := s[0]; |
| 763 | + for i := 1 to LEN(s) - 1 do |
| 764 | + if less(m, s[i]) then m := s[i]; end; |
| 765 | + end; |
| 766 | + return m; |
| 767 | +end; |
| 768 | + |
| 769 | +end Biggest. |
| 770 | +.Ed |
| 771 | +.Bd -literal -offset indent |
| 772 | +module Generics; |
| 773 | +import Out, Ints := Biggest(INT, before), |
| 774 | + Words := Biggest([]U8, shorter), Order := Sort(INT, before); |
| 775 | + |
| 776 | +proc before(a, b: INT): BOOL; |
| 777 | +begin |
| 778 | + return a < b; |
| 779 | +end; |
| 780 | + |
| 781 | +proc shorter(a, b: []U8): BOOL; |
| 782 | +begin |
| 783 | + return LEN(a) < LEN(b); |
| 784 | +end; |
| 785 | + |
| 786 | +proc main*; |
| 787 | +var a: [5]INT; i: INT; |
| 788 | +begin |
| 789 | + a := [3, 1, 4, 1, 5]; |
| 790 | + Out.int(Ints.max(a)); Out.ln; |
| 791 | + Out.str(Words.max(["a", "tree", "is"])); Out.ln; |
| 792 | + Order.sort(a); |
| 793 | + for i := 0 to 4 do Out.int(a[i]); Out.char(' '); end; |
| 794 | + Out.ln; |
| 795 | +end; |
| 796 | + |
| 797 | +end Generics. |
| 798 | +.Ed |
| 799 | +.Bd -literal -offset indent |
| 800 | +5 |
| 801 | +tree |
| 802 | +1 1 3 4 5 |
| 803 | +.Ed |
| 804 | +.Pp |
| 805 | +Each import with its own arguments makes an instance: the module |
| 806 | +compiled again, with |
| 807 | +.Cm T |
| 808 | +standing for the type given, and |
| 809 | +.Cm less |
| 810 | +calling the procedure given. |
| 811 | +The arguments may name what the importing module declares later, as |
| 812 | +.Cm before |
| 813 | +is here. |
| 814 | +The library's |
| 815 | +.Cm List , Map |
| 816 | +and |
| 817 | +.Cm Sort |
| 818 | +are generic modules: |
| 819 | +.Bd -literal -offset indent |
| 820 | +import Count := Map([]U8, INT, Str.hash, Str.eq); |
| 821 | +.Ed |
| 822 | +.Sh 13. TALKING TO C |
| 823 | +.Bd -literal -offset indent |
| 824 | +module Qsort; |
| 825 | +import Out, Fmt; |
| 826 | + |
| 827 | +link "libm.so.6"; |
| 828 | + |
| 829 | +extern proc qsort(base: ^U8; n, size: UINT; |
| 830 | + less: proc(a, b: ^U8): I32); |
| 831 | +extern proc cbrt(x: F64): F64; |
| 832 | + |
| 833 | +(* the order of two INTs, as C's qsort wants it *) |
| 834 | +proc order(a, b: ^U8): I32; |
| 835 | +var x, y: ^INT; |
| 836 | +begin |
| 837 | + x := ^INT(a); |
| 838 | + y := ^INT(b); |
| 839 | + if x^ < y^ then return -1; elsif x^ > y^ then return 1; end; |
| 840 | + return 0; |
| 841 | +end; |
| 842 | + |
| 843 | +proc main*; |
| 844 | +var a: [5]INT; i: INT; |
| 845 | +begin |
| 846 | + a := [30, 10, 50, 20, 40]; |
| 847 | + qsort(^U8(ADR(a)), LEN(a), SIZE(INT), order); |
| 848 | + for i := 0 to 4 do Out.int(a[i]); Out.char(' '); end; |
| 849 | + Out.ln; |
| 850 | + Fmt.print("{}\en", [Fmt.real(cbrt(27.0), 3)]); |
| 851 | +end; |
| 852 | + |
| 853 | +end Qsort. |
| 854 | +.Ed |
| 855 | +.Bd -literal -offset indent |
| 856 | +10 20 30 40 50 |
| 857 | +3.000 |
| 858 | +.Ed |
| 859 | +.Pp |
| 860 | +An |
| 861 | +.Cm extern |
| 862 | +procedure is found by its name in the C library, or in a library a |
| 863 | +.Cm link |
| 864 | +line names. |
| 865 | +Its parameters are integers, reals, pointers and procedures, as C |
| 866 | +declares them; a raz procedure of such types can be given to C to call |
| 867 | +back, as |
| 868 | +.Cm order |
| 869 | +is. |
| 870 | +.Cm ^INT(a) |
| 871 | +sees the address |
| 872 | +.Cm a |
| 873 | +as a pointer to an |
| 874 | +.Cm INT . |
| 875 | +.Pp |
| 876 | +A program that uses C is linked against it dynamically, and starts |
| 877 | +through it; one that doesn't is a static executable with nothing in it |
| 878 | +but its own code. |
| 879 | +.Cm Out |
| 880 | +and C's |
| 881 | +.Xr printf 3 |
| 882 | +keep two buffers, so a program that writes with both flushes one |
| 883 | +before it writes with the other. |
| 884 | +.Sh 14. THREADS |
| 885 | +.Bd -literal -offset indent |
| 886 | +module Hits; |
| 887 | +import Out, Thread; |
| 888 | + |
| 889 | +const Workers = 4; |
| 890 | + |
| 891 | +var |
| 892 | + hits: INT; (* added to by every thread at once, by XADD *) |
| 893 | + m: Thread.Mutex; |
| 894 | + total: INT; (* added to under m *) |
| 895 | + |
| 896 | +proc work(arg: ^U8); |
| 897 | +var i, n, sum: INT; |
| 898 | +begin |
| 899 | + n := INT(arg); |
| 900 | + sum := 0; |
| 901 | + for i := 1 to n do |
| 902 | + sum := sum + i; |
| 903 | + _ := XADD(ADR(hits), 1); |
| 904 | + end; |
| 905 | + Thread.lock(ADR(m)); |
| 906 | + total := total + sum; |
| 907 | + Thread.unlock(ADR(m)); |
| 908 | +end; |
| 909 | + |
| 910 | +proc main*; |
| 911 | +var t: [Workers]^Thread.Thread; i: INT; err: ERROR; |
| 912 | +begin |
| 913 | + for i := 0 to Workers - 1 do |
| 914 | + t[i], err := Thread.spawn(work, ^U8(1000 * (i + 1))); |
| 915 | + if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 916 | + end; |
| 917 | + for i := 0 to Workers - 1 do Thread.join(t[i]); end; |
| 918 | + Out.int(hits); Out.char(' '); Out.int(total); Out.ln; |
| 919 | +end; |
| 920 | + |
| 921 | +end Hits. |
| 922 | +.Ed |
| 923 | +.Bd -literal -offset indent |
| 924 | +10000 15005000 |
| 925 | +.Ed |
| 926 | +.Pp |
| 927 | +.Cm Thread.spawn |
| 928 | +runs a procedure in a thread of its own, with a stack of its own, and |
| 929 | +.Cm Thread.join |
| 930 | +waits for it to end. |
| 931 | +.Cm XADD , |
| 932 | +.Cm CAS |
| 933 | +and |
| 934 | +.Cm XCHG |
| 935 | +change a word atomically; a |
| 936 | +.Cm Thread.Mutex |
| 937 | +keeps a longer section to one thread at a time. |
| 938 | +.Cm Out |
| 939 | +and |
| 940 | +.Cm Heap |
| 941 | +lock themselves. |
| 942 | +.Pp |
| 943 | +.Cm Thread |
| 944 | +makes its own threads, without C, so a program that uses it can't use |
| 945 | +C; one that does makes its threads with C's |
| 946 | +.Xr pthread_create 3 |
| 947 | +instead. |
| 948 | +.Sh 15. FROM PASCAL AND C TO RAZ |
| 949 | +.Bl -column "for (i=0; i<n; i++)" "for i := 0 to n-1 do" "Mem.free(Heap.heap, ^U8(p))" |
| 950 | +.It Sy C Ta Sy Pascal Ta Sy raz |
| 951 | +.It "#include" Ta "uses" Ta "import Out, Str;" |
| 952 | +.It "static" Ta "implementation" Ta "a name without *" |
| 953 | +.It "int, long" Ta "integer" Ta "I32, INT, I64" |
| 954 | +.It "char *s" Ta "string" Ta "s: []U8" |
| 955 | +.It "\(dqit's\(dq" Ta "'it''s'" Ta "\(dqit's\(dq" |
| 956 | +.It "0xFF" Ta "$FF" Ta "0FFH" |
| 957 | +.It "/* ... */" Ta "{ ... }" Ta "(* ... *), nesting" |
| 958 | +.It "x == y" Ta "x = y" Ta "x = y" |
| 959 | +.It "x != y" Ta "x <> y" Ta "x # y" |
| 960 | +.It "a && b" Ta "a and b" Ta "a and b" |
| 961 | +.It "x & m" Ta "x and m" Ta "x & m" |
| 962 | +.It "i++" Ta "inc(i)" Ta "INC(i)" |
| 963 | +.It "(long)x" Ta "longint(x)" Ta "I64(x)" |
| 964 | +.It "if (c) {...}" Ta "if c then begin" Ta "if c then ... end;" |
| 965 | +.It "for (i=0; i<n; i++)" Ta "for i := 0 to n-1 do" Ta "for i := 0 to n - 1 do" |
| 966 | +.It "switch (c)" Ta "case c of" Ta "match c of | 1: ..." |
| 967 | +.It "continue, goto" Ta "goto" Ta "none: break only" |
| 968 | +.It "union" Ta "variant record" Ta "variant" |
| 969 | +.It "struct p {...}" Ta "record ... end" Ta "record ... end" |
| 970 | +.It "&x" Ta "@x" Ta "ADR(x)" |
| 971 | +.It "p->f" Ta "p^.f" Ta "p^.f" |
| 972 | +.It "NULL" Ta "nil" Ta "nil" |
| 973 | +.It "malloc(sizeof *p)" Ta "new(p)" Ta "NEW(p, Heap.heap)" |
| 974 | +.It "free(p)" Ta "dispose(p)" Ta "Mem.free(Heap.heap, ^U8(p))" |
| 975 | +.It "int f(int *out)" Ta "function f(var x)" Ta "proc f: (INT, INT);" |
| 976 | +.It "return -1; errno" Ta "exceptions" Ta "return 0, NotFound;" |
| 977 | +.It "(void)f(x);" Ta "f(x);" Ta "_ := f(x);" |
| 978 | +.It "goto cleanup;" Ta "try ... finally" Ta "defer close(fd);" |
| 979 | +.It "printf(...)" Ta "writeln(...)" Ta "Out.str(s), Fmt.print(...)" |
| 980 | +.El |
| 981 | +.Pp |
| 982 | +And what neither has: every index is checked, every variable starts |
| 983 | +at zero, and no two integer types mix in an expression. |
| 984 | +.Sh SEE ALSO |
| 985 | +.Xr raz 1 , |
| 986 | +.Xr raz 7 , |
| 987 | +.Xr raz-lib 3 ; |
| 988 | +the report, |
| 989 | +.Pa doc/raz.md , |
| 990 | +in raz's sources. |