raz / site / public / tour / hello.html

146 lines · 9.0 KiB · raw · history · last change de634aad

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>Hello, world - Raz, Set, Go</title>
7<meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
8<meta name="theme-color" content="#E07A5F">
9<meta property="og:title" content="Hello, world - Raz, Set, Go">
10<meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
11<meta property="og:image" content="https://raz.lucela.org/social.png">
12<meta name="twitter:card" content="summary_large_image">
13<link rel="icon" href="../favicon.svg" type="image/svg+xml">
14<link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32">
15<link rel="apple-touch-icon" href="../apple-touch-icon.png">
16<link rel="stylesheet" href="../style.css">
17</head>
18<body>
19<header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header>
20<main>
21<div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
22<li><a href="hello.html" aria-current="page">Hello, world</a></li>
23<li><a href="modules.html">Modules</a></li>
24<li><a href="integers.html">Integers</a></li>
25<li><a href="values.html">Values and conversions</a></li>
26<li><a href="procedures.html">Procedures and several results</a></li>
27</ol>
28<h3>Set</h3><ol start='6'>
29<li><a href="slices.html">Arrays, slices and strings</a></li>
30<li><a href="records.html">Records and pointers</a></li>
31<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
32<li><a href="memory.html">Memory and allocators</a></li>
33</ol>
34<h3>Go</h3><ol start='10'>
35<li><a href="errors.html">Errors</a></li>
36<li><a href="defer.html">Defer</a></li>
37<li><a href="checks.html">When it goes wrong</a></li>
38<li><a href="generics.html">Generic modules</a></li>
39<li><a href="c.html">Talking to C</a></li>
40<li><a href="threads.html">Threads</a></li>
41</ol></aside><article><p class="kicker">Raz &middot; lesson 1 of 15</p><h1>Hello, world</h1>
42<p>Every raz program is a module with a <code>main</code> procedure. This is the smallest one that does something.</p>
43<div class="file"><span>Hello.raz</span></div><pre class="code"><code><span class="kw">module</span> Hello;
44<span class="kw">import</span> Out;
45
46<span class="kw">proc</span> main*;
47<span class="kw">begin</span>
48 Out.str(<span class="string">&quot;hello, world\n&quot;</span>);
49<span class="kw">end</span>;
50
51<span class="kw">end</span> Hello.</code></pre>
52<pre class="output"><code>hello, world
53</code></pre>
54<p>A module lives in a file of its own name, <code>Hello.raz</code>, and its name comes again after its last <code>end</code>, so a stray <code>end</code> is caught early. <code>import Out</code> lets it use what the module <code>Out</code> exports, as <code>Out.str</code>. The <code>*</code> after <code>main</code> exports it; the program starts at the <code>main</code> of the module named on the command line. Every statement ends in <code>;</code>, even one that ends in <code>end</code>.</p>
55<h2>Compiling and running</h2>
56<p><code>raz Hello.raz</code> compiles the module and everything it imports, and writes the executable <code>Hello</code> beside it. <code>raz run</code> does the same without writing a file, and runs it at once.</p>
57<div class="file"><span>Quiet.raz</span></div><pre class="code"><code><span class="comment">(* The same program, with a comment: (* ... *)</span> nests, <span class="kw">and</span> may stand anywhere. *)
58<span class="kw">module</span> Quiet;
59<span class="kw">import</span> Out;
60
61<span class="kw">proc</span> main*;
62<span class="kw">begin</span>
63 Out.str(<span class="string">&quot;hello, again\n&quot;</span>);
64<span class="kw">end</span>;
65
66<span class="kw">end</span> Quiet.</code></pre>
67<pre class="output term"><code><span class="cmd">$ raz Quiet.raz</span>
68<span class="cmd">$ ./Quiet</span>
69hello, again
70<span class="cmd">$ raz run Quiet.raz</span>
71hello, again
72<span class="cmd">$ ls -l Quiet | awk &#x27;{print $5, &quot;bytes&quot;}&#x27;</span>
7328608 bytes</code></pre>
74<p>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.</p>
75<h2>What <code>Out</code> can write</h2>
76<p><code>Out</code> 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 <code>\n</code> for a line feed, <code>\t</code> for a tab, <code>\"</code> and <code>\\</code>; or between backquotes, as a raw string with no escapes at all, which may run over lines (lesson 6).</p>
77<div class="file"><span>Numbers.raz</span></div><pre class="code"><code><span class="kw">module</span> Numbers;
78<span class="kw">import</span> Out;
79
80<span class="kw">proc</span> main*;
81<span class="kw">begin</span>
82 Out.str(<span class="string">&quot;seven is &quot;</span>); Out.int(<span class="number">7</span>); Out.ln;
83 Out.str(<span class="string">&quot;minus one is &quot;</span>); Out.int(-<span class="number">1</span>); Out.ln;
84 Out.str(<span class="string">&quot;255 is 0x&quot;</span>); Out.hex(<span class="number">255</span>, <span class="number">4</span>); Out.ln;
85 Out.char(<span class="string">&#x27;R&#x27;</span>); Out.char(<span class="string">&#x27;a&#x27;</span>); Out.char(<span class="string">&#x27;z&#x27;</span>); Out.ln;
86 Out.str(<span class="string">&quot;a tab:\t|\n&quot;</span>);
87<span class="kw">end</span>;
88
89<span class="kw">end</span> Numbers.</code></pre>
90<pre class="output"><code>seven is 7
91minus one is -1
92255 is 0x00FF
93Raz
94a tab: |
95</code></pre>
96<p><code>Out.hex(n, 4)</code> writes at least four digits. <code>Out.int</code> takes an <code>I64</code> and <code>Out.hex</code> a <code>U64</code>. An <code>INT</code> goes into an <code>I64</code> as it is; any other integer is converted where it is written, as <code>Out.int(I64(b))</code>, because raz never converts a number unasked.</p>
97<h2>Ending, and the status</h2>
98<p><code>main</code> may return an <code>INT</code>, which is the program's exit status; a plain <code>proc main*;</code> ends with 0. A shell sees the status as <code>$?</code>.</p>
99<div class="file"><span>Status.raz</span></div><pre class="code"><code><span class="kw">module</span> Status;
100<span class="kw">import</span> Out;
101
102<span class="kw">proc</span> main*: <span class="ty">INT</span>;
103<span class="kw">begin</span>
104 Out.str(<span class="string">&quot;exiting with 3\n&quot;</span>);
105 <span class="kw">return</span> <span class="number">3</span>;
106<span class="kw">end</span>;
107
108<span class="kw">end</span> Status.</code></pre>
109<pre class="output term"><code><span class="cmd">$ raz run Status.raz; echo &quot;status $?&quot;</span>
110exiting with 3
111status 3</code></pre>
112<h2>Arguments</h2>
113<p><code>ARGC()</code> is the number of command line arguments, with the program's own name first, and <code>SYSTEM.ARGV()</code> the arguments, as C strings. Most programs read them through <code>Files.arg</code> and <code>Opt</code>, which the advanced tour shows; here is the raw form.</p>
114<div class="file"><span>Args.raz</span></div><pre class="code"><code><span class="kw">module</span> Args;
115<span class="kw">import</span> Out;
116<span class="kw">import</span> SYSTEM;
117
118<span class="comment">(* The length of a C string. *)</span>
119<span class="kw">proc</span> len(s: ^<span class="ty">U8</span>): <span class="ty">INT</span>;
120<span class="kw">var</span> n: <span class="ty">INT</span>;
121<span class="kw">begin</span>
122 n := <span class="number">0</span>;
123 <span class="kw">while</span> s[n] # <span class="number">0</span> <span class="kw">do</span> n := n + <span class="number">1</span>; <span class="kw">end</span>;
124 <span class="kw">return</span> n;
125<span class="kw">end</span>;
126
127<span class="kw">proc</span> main*: <span class="ty">INT</span>;
128<span class="kw">var</span> i: <span class="ty">INT</span>; s: ^<span class="ty">U8</span>;
129<span class="kw">begin</span>
130 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> ARGC() - <span class="number">1</span> <span class="kw">do</span>
131 s := SYSTEM.ARGV()[i];
132 Out.str(s[<span class="number">0</span>..len(s)]);
133 Out.ln;
134 <span class="kw">end</span>;
135 <span class="kw">return</span> ARGC() - <span class="number">1</span>;
136<span class="kw">end</span>;
137
138<span class="kw">end</span> Args.</code></pre>
139<pre class="output term"><code><span class="cmd">$ raz run Args.raz one &quot;two words&quot;; echo &quot;status $?&quot;</span>
140one
141two words
142status 2</code></pre><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="index.html">Contents</a><a class="next" href="modules.html">Modules &rarr;</a></div></article></div>
143</main>
144<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
145</body>
146</html>