| 1 | title: Hello, world |
| 2 | part: Raz |
| 3 | summary: A module, an import, a procedure; running, printing, exit status and arguments. |
| 4 | --- |
| 5 | Every raz program is a module with a `main` procedure. This is the smallest one that does something. |
| 6 | |
| 7 | ```raz Hello run |
| 8 | ``` |
| 9 | |
| 10 | A module lives in a file of its own name, `Hello.raz`, and its name comes again after its last `end`, so a stray `end` is caught early. `import Out` lets it use what the module `Out` exports, as `Out.str`. The `*` after `main` exports it; the program starts at the `main` of the module named on the command line. Every statement ends in `;`, even one that ends in `end`. |
| 11 | |
| 12 | ## Compiling and running |
| 13 | |
| 14 | `raz Hello.raz` compiles the module and everything it imports, and writes the executable `Hello` beside it. `raz run` does the same without writing a file, and runs it at once. |
| 15 | |
| 16 | ```raz Quiet |
| 17 | (* The same program, with a comment: (* ... *) nests, and may stand anywhere. *) |
| 18 | module Quiet; |
| 19 | import Out; |
| 20 | |
| 21 | proc main*; |
| 22 | begin |
| 23 | Out.str("hello, again\n"); |
| 24 | end; |
| 25 | |
| 26 | end Quiet. |
| 27 | ``` |
| 28 | |
| 29 | ```sh |
| 30 | $ raz Quiet.raz |
| 31 | $ ./Quiet |
| 32 | $ raz run Quiet.raz |
| 33 | $ ls -l Quiet | awk '{print $5, "bytes"}' |
| 34 | ``` |
| 35 | |
| 36 | The program is a static executable with no run time besides the code it uses, so it is a few kilobytes, and runs without any library installed. |
| 37 | |
| 38 | ## What `Out` can write |
| 39 | |
| 40 | `Out` is the library's buffered output. It writes strings, single characters, numbers in decimal or hexadecimal, and a line feed. A string is written as it is, with `\n` for a line feed, `\t` for a tab, `\"` and `\\`; or between backquotes, as a raw string with no escapes at all, which may run over lines (lesson 6). |
| 41 | |
| 42 | ```raz Numbers run |
| 43 | module Numbers; |
| 44 | import Out; |
| 45 | |
| 46 | proc main*; |
| 47 | begin |
| 48 | Out.str("seven is "); Out.int(7); Out.ln; |
| 49 | Out.str("minus one is "); Out.int(-1); Out.ln; |
| 50 | Out.str("255 is 0x"); Out.hex(255, 4); Out.ln; |
| 51 | Out.char('R'); Out.char('a'); Out.char('z'); Out.ln; |
| 52 | Out.str("a tab:\t|\n"); |
| 53 | end; |
| 54 | |
| 55 | end Numbers. |
| 56 | ``` |
| 57 | |
| 58 | `Out.hex(n, 4)` writes at least four digits. `Out.int` takes an `I64` and `Out.hex` a `U64`. An `INT` goes into an `I64` as it is; any other integer is converted where it is written, as `Out.int(I64(b))`, because raz never converts a number unasked. |
| 59 | |
| 60 | ## Ending, and the status |
| 61 | |
| 62 | `main` may return an `INT`, which is the program's exit status; a plain `proc main*;` ends with 0. A shell sees the status as `$?`. |
| 63 | |
| 64 | ```raz Status |
| 65 | module Status; |
| 66 | import Out; |
| 67 | |
| 68 | proc main*: INT; |
| 69 | begin |
| 70 | Out.str("exiting with 3\n"); |
| 71 | return 3; |
| 72 | end; |
| 73 | |
| 74 | end Status. |
| 75 | ``` |
| 76 | |
| 77 | ```sh |
| 78 | $ raz run Status.raz; echo "status $?" |
| 79 | ``` |
| 80 | |
| 81 | ## Arguments |
| 82 | |
| 83 | `ARGC()` is the number of command line arguments, with the program's own name first, and `SYSTEM.ARGV()` the arguments, as C strings. Most programs read them through `Files.arg` and `Opt`, which the advanced tour shows; here is the raw form. |
| 84 | |
| 85 | ```raz Args |
| 86 | module Args; |
| 87 | import Out; |
| 88 | import SYSTEM; |
| 89 | |
| 90 | (* The length of a C string. *) |
| 91 | proc len(s: ^U8): INT; |
| 92 | var n: INT; |
| 93 | begin |
| 94 | n := 0; |
| 95 | while s[n] # 0 do n := n + 1; end; |
| 96 | return n; |
| 97 | end; |
| 98 | |
| 99 | proc main*: INT; |
| 100 | var i: INT; s: ^U8; |
| 101 | begin |
| 102 | for i := 1 to ARGC() - 1 do |
| 103 | s := SYSTEM.ARGV()[i]; |
| 104 | Out.str(s[0..len(s)]); |
| 105 | Out.ln; |
| 106 | end; |
| 107 | return ARGC() - 1; |
| 108 | end; |
| 109 | |
| 110 | end Args. |
| 111 | ``` |
| 112 | |
| 113 | ```sh |
| 114 | $ raz run Args.raz one "two words"; echo "status $?" |
| 115 | ``` |