raz / site / content / tour / 01-hello.txt

115 lines · 3.1 KiB · raw · history · last change a5145f64

1title: Hello, world
2part: Raz
3summary: A module, an import, a procedure; running, printing, exit status and arguments.
4---
5Every raz program is a module with a `main` procedure. This is the smallest one that does something.
6
7```raz Hello run
8```
9
10A 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. *)
18module Quiet;
19import Out;
20
21proc main*;
22begin
23 Out.str("hello, again\n");
24end;
25
26end 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
36The 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
43module Numbers;
44import Out;
45
46proc main*;
47begin
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");
53end;
54
55end 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
65module Status;
66import Out;
67
68proc main*: INT;
69begin
70 Out.str("exiting with 3\n");
71 return 3;
72end;
73
74end 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
86module Args;
87import Out;
88import SYSTEM;
89
90(* The length of a C string. *)
91proc len(s: ^U8): INT;
92var n: INT;
93begin
94 n := 0;
95 while s[n] # 0 do n := n + 1; end;
96 return n;
97end;
98
99proc main*: INT;
100var i: INT; s: ^U8;
101begin
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;
108end;
109
110end Args.
111```
112
113```sh
114$ raz run Args.raz one "two words"; echo "status $?"
115```