interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / omega-reference / main.html

2823 lines · 127980 bytes Raw Blame History
1<div class="om-page ref-page"><header class="om-hero"><div class="container"><p class="om-kicker">Omega</p><h1 class="om-title">Omega language reference</h1><p class="om-lead">Everything an agent needs to write Omega that the pinned compiler accepts and that does what it should: program shape, types, declarations, statements, words, OM-2, the host words, every compiler error and trap exit code, common mistakes, and 98 worked examples that each passed the examples gate.</p><p class="om-sub">An Omega program writes this page: <code>om/omega-reference/page.om</code> in the aienos.com repository reads the guide <code>skills/writing-omega/SKILL.md</code> and the examples of omega-harness commit <a href="https://interplane.io/drakestapleton/omega-harness/tree/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e"><code>0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e</code></a> and prints every file byte for byte. The compiler pin is omega-mv <code>e5b32567a7aff3ce594e3254a622a846a6cd190b</code>, omc sha256 <code>2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f</code>. The same text is in <a href="/llms-full.txt">llms-full.txt</a>.</p><nav class="om-toc" aria-label="On this page"><a href="#guide">Guide</a> <a href="#s11">Compiler errors</a> <a href="#s5">Traps</a> <a href="#s12">Common mistakes</a> <a href="#examples">Worked examples</a> <a href="#gate">Checking the examples</a></nav></div></header><div class="container"><section class="om-section ref-guide" id="guide"><h2>Writing Omega programs</h2><p>This guide is for any agent or person writing <code>.om</code> on any machine. It needs only a clone of
2<a href="https://interplane.io/drakestapleton/omega-mv">aien-dev/omega-mv</a> and of this repository. Every rule
3below was observed with a program at the pin in the table; the programs are in
4<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/"><code>examples/</code></a> and <a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/gate.sh"><code>examples/gate.sh</code></a> reruns all of them.
5For compiler internals, register rules, wire formats and the reasons behind each rule, read
6<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/skills/omega-encoding/SKILL.md"><code>skills/omega-encoding/SKILL.md</code></a>.</p><table><tr><th>Pin</th><th>Value</th></tr><tr><td>omega-mv commit</td><td><code>e5b32567a7aff3ce594e3254a622a846a6cd190b</code></td></tr><tr><td>compiler <code>seed/out/omc</code></td><td>sha256 <code>2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f</code></td></tr><tr><td>language reference</td><td><code>docs/one_layer/OMEGESE.md</code> at that commit</td></tr><tr><td>host</td><td>Linux AArch64 only; omc writes a static AArch64 Linux ELF</td></tr></table><p>The language reference is normative. Where this guide and OMEGESE disagree, the compiler at the pin
7decides; this guide records the known disagreements.</p><h3 id="s1">1. Compile and run</h3><pre>
8# omega-mv is moving to Interplane, my own forge; downloads are not open yet.
9git checkout e5b32567a7aff3ce594e3254a622a846a6cd190b
10sha256sum seed/out/omc # must print the pinned hash
11sh seed/omega-from-seed.sh # optional: rebuild omc from the 408-byte loader
12seed/out/omc &lt; prog.om &gt; prog; echo &quot;compile $?&quot;
13chmod +x prog &amp;&amp; ./prog; echo &quot;exit $?&quot;
14</pre><ul><li>omc reads the unit on stdin and writes the executable on stdout. Exit 0 means accepted. Any other
15exit is the error number, and stderr has one line <code>omc: line N: error K</code>.</li><li><strong>Never run the output of a refused compile</strong>: stdout may still hold bytes.</li><li>A unit with <code>fn main</code> is program mode (an executable). A unit without it is function mode (raw
16machine words for a host to call); never execute function-mode output directly.</li><li>Check behavior, not compile success: compare stdout bytes and the exit status with what you
17expected, and run one deliberately wrong variant to see the check fail.</li><li>To check a whole set the way this repository does:
18<code>examples/gate.sh path/to/seed/out/omc</code> prints <code>PASS name</code> or <code>FAIL name: reason</code> per program and
19ends <code>GATE PASS examples=N ...</code> or <code>GATE FAIL ...</code> (exit 1); a different compiler hash exits 2.</li></ul><h3 id="s2">2. Program shape</h3><pre>
20note hello: the smallest complete program
21text msg &quot;hello, world\n&quot;
22fn main to int
23k = write 1 msg 0 len msg
24if ne k long len msg
25ret 1
26end
27ret 0
28end
29</pre><p>(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/hello.om"><code>examples/hello.om</code></a>). The rules it shows:</p><ul><li><strong>One statement per line.</strong> No semicolons, no braces. Blocks (<code>fn</code>, <code>if</code>, <code>else</code>, <code>while</code>, <code>for</code>,
30<code>struct</code>) close with <code>end</code>. Indentation is optional.</li><li><strong>Prefix words only.</strong> Every operation is a word followed by its values: <code>add x 1</code>,
31<code>mul add a b c</code>, <code>lt i n</code>. Each word has a fixed number of values. Parentheses may group a value
32and add nothing: <code>(add a b)</code>.</li><li><strong>Comments are <code>note</code> lines.</strong> <code>//</code> and <code>#</code> are refused (error 16).</li><li><strong><code>fn NAME p1 T1 p2 T2 ... to R</code></strong>, result <code>R</code> a type or <code>void</code>. Parameters fill <strong>6 slots</strong>: an
33OM-2 unsized <code>view T *</code> / <code>edit T *</code> takes 2 (pointer and count), every other parameter 1 (a
34scalar, a sized array, a struct). Two unsized views plus three scalars is error 9
35(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/om2_param_slots.om"><code>examples/om2_param_slots.om</code></a>,
36<code>mistake_om2_param_slots.om</code>). <code>fn main to int</code> takes no parameters and its <code>ret</code> value is the exit
37status (0..255).</li><li><strong>Assignment is <code>name = value</code>.</strong> The first assignment creates the name with the value's type. The
38<code>=</code> is optional, which is a trap: <code>return x</code> is accepted as &quot;assign x to a new name <code>return</code>&quot;.</li><li>A function may call a function defined later in the unit, and program-mode functions may recurse.</li><li>Top-level lines are <code>note</code>, <code>fn</code>, <code>text</code>, <code>global</code>, <code>const</code>, <code>struct</code>, and in OM-2 units
39<code>layout</code>, <code>abstract</code>, <code>holds</code> and <code>export fn</code>. <code>struct</code> and <code>layout</code> lines come before the first
40<code>fn</code> (error 9 after it). A <code>const</code> is visible only after its line.</li></ul><p>Every declaration and statement form at the pin (OMEGESE sections 9 to 12 are the full rules):</p><table><tr><th>Form</th><th>Where</th><th>Notes</th></tr><tr><td><code>note ANY TEXT</code></td><td>any line</td><td>a comment line</td></tr><tr><td><code>om2</code>, <code>om2 debug</code>, <code>om2 release</code>, <code>om2 diag</code>, <code>om2 diag debug</code>, <code>om2 diag release</code></td><td>first line that is not blank or <code>note</code></td><td>opts the unit into OM-2 (section 9); <code>release</code> erases contract checks, <code>diag</code> adds a machine-readable stderr record</td></tr><tr><td><code>om2 fmt</code></td><td>the first line, at byte 0</td><td>prints the canonical text of the rest of the input instead of compiling it</td></tr><tr><td><code>text NAME &quot;bytes&quot;</code></td><td>top level</td><td>a read-only <code>uchar</code> array</td></tr><tr><td><code>global NAME T N</code></td><td>top level, program mode</td><td>zero-filled array, never an argument</td></tr><tr><td><code>const NAME T LITERAL</code></td><td>top level</td><td>an integer or float value, visible after its line</td></tr><tr><td><code>struct S f1 T1 f2 T2 ... end</code></td><td>top level, before the first <code>fn</code></td><td>one line; a field <code>f T N</code> is an array of 1..64</td></tr><tr><td><code>layout NAME SIZE</code>, <code>field F T [COUNT] at OFF little</code>/<code>big</code> lines, <code>end</code></td><td>OM-2, top level, before the first <code>fn</code></td><td>an external byte layout of SIZE 1..4096 bytes</td></tr><tr><td><code>abstract NAME T [ops W ...]</code>, <code>holds NAME dup32</code></td><td>OM-2, top level, before the first <code>fn</code></td><td>T <code>uint</code> or <code>ulong</code>; W from <code>rotr rotl and or xor</code></td></tr><tr><td><code>fn NAME p1 T1 ... to R</code> ... <code>end</code></td><td>top level</td><td><code>inline</code> after R in OM-2; <code>export fn</code> marks an OM-2 function-mode entry</td></tr><tr><td>parameter <code>p T</code></td><td><code>fn</code> line</td><td>a scalar</td></tr><tr><td>parameter <code>a view T N</code> / <code>a edit T N</code></td><td><code>fn</code> line</td><td>a sized array, read-only or writable</td></tr><tr><td>parameter <code>a view T *</code> / <code>a edit T *</code></td><td><code>fn</code> line, OM-2</td><td>an array of any length</td></tr><tr><td>parameter <code>s edit T N fresh</code></td><td><code>fn</code> line, OM-2</td><td>scratch: every element written before it is read</td></tr><tr><td>parameter <code>p view S</code> / <code>p edit S</code></td><td><code>fn</code> line</td><td>a struct, or an OM-2 layout</td></tr><tr><td>parameter <code>o out T N</code></td><td><code>fn</code> line, OM-2</td><td>written by the unit; the calling host copies it out after status 0</td></tr><tr><td><code>name = value</code></td><td>function body</td><td>the first assignment creates the name</td></tr><tr><td><code>put A i v</code>, <code>put p f v</code></td><td>function body</td><td>store into an array element or a field</td></tr><tr><td><code>f v1 v2 ...</code></td><td>function body</td><td>a call to a <code>void</code> function</td></tr><tr><td><code>if C</code> ... <code>else</code> ... <code>end</code></td><td>function body</td><td><code>else</code> is optional; no <code>else if</code></td></tr><tr><td><code>while C bound N</code> ... <code>end</code></td><td>function body</td><td>N 1..999999</td></tr><tr><td><code>for i T K</code> ... <code>end</code></td><td>function body</td><td><code>unroll</code> after K in OM-2, K 1..64</td></tr><tr><td><code>break</code>, <code>continue</code></td><td>inside a loop</td><td></td></tr><tr><td><code>ret</code>, <code>ret v</code></td><td>function body</td><td><code>ret v</code> in a function with a result</td></tr><tr><td><code>exit N</code>, <code>trap N</code></td><td>function body</td><td>exit status N; trap N 1..14 exits 100+N</td></tr><tr><td><code>requires C</code>, <code>ensures C</code></td><td>OM-2, the first lines of a function</td><td><code>result</code> names the returned value</td></tr><tr><td><code>invariant C</code></td><td>OM-2, the first line of a loop body</td><td></td></tr></table><h3 id="s3">3. Types and values</h3><table><tr><th>Type</th><th>Meaning</th></tr><tr><td><code>bool</code></td><td><code>true</code> / <code>false</code>, the result of comparisons</td></tr><tr><td><code>uchar</code>, <code>ushort</code>, <code>uint</code>, <code>ulong</code></td><td>unsigned 8, 16, 32, 64 bit (also spelled <code>u8 u16 u32 u64</code>)</td></tr><tr><td><code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code></td><td>signed 8, 16, 32, 64 bit (also spelled <code>i8 i16 i32 i64</code>); <code>byte</code> is <strong>signed</strong>, and <code>char</code> is not a type (error 3)</td></tr><tr><td><code>f32</code>, <code>f64</code></td><td>IEEE binary32, binary64</td></tr><tr><td><code>void</code></td><td>no result (function result only)</td></tr></table><ul><li><strong>No implicit conversion.</strong> Both values of an operation have one type (error 7). Convert with the
41type word: <code>long x</code>, <code>uint b</code>, <code>f64 n</code>.</li><li><strong>A bare number takes its type from context</strong>: the other value of its operation, the parameter it
42is passed to, the result type at <code>ret</code>. With no context it is error 11: write <code>x = uint 5</code>, never
43<code>x = 5</code> for a new name.</li><li>Integer literals are decimal with an optional leading <code>-</code>. A literal that does not fit its type
44is error 8. No character literals: write <code>uchar 65</code>, not <code>'A'</code> (error 3).</li><li>Float literals need digits on both sides of the dot: <code>2.0f64</code>, <code>1.0e30f64</code>, <code>0.5f32</code>. The suffix is
45optional where context gives the type.</li><li><strong>Hex only in OM-2</strong> (section 9): lowercase <code>0x</code> and lowercase digits, value must fit
46(<code>int 0xffffffff</code> is error 8; write <code>int -1</code>).</li><li><code>sizeof T</code>, <code>sizeof S</code> and <code>offsetof S f</code> are compile-time <code>ulong</code> constants.</li></ul><h3 id="s4">4. Words</h3><table><tr><th>Kind</th><th>Words</th></tr><tr><td>arithmetic</td><td><code>add sub mul div rem neg</code></td></tr><tr><td>bits</td><td><code>and or xor not shl shr</code> (<code>rotr rotl</code> in OM-2)</td></tr><tr><td>compare (give <code>bool</code>)</td><td><code>eq ne lt le gt ge</code></td></tr><tr><td>float</td><td><code>add sub mul div neg</code>, <code>fsqrt fabs fmin fmax</code></td></tr><tr><td>arrays</td><td><code>new</code>, <code>get A i</code>, <code>put A i v</code>, <code>len A</code></td></tr><tr><td>structs</td><td><code>new S v1 v2 ...</code>, <code>get p f</code>, <code>put p f v</code></td></tr><tr><td>control</td><td><code>if</code> / <code>else</code> / <code>end</code>, <code>while COND bound N</code>, <code>for i T K</code>, <code>break</code>, <code>continue</code>, <code>ret</code>, <code>exit</code>, <code>trap N</code></td></tr><tr><td>conversion</td><td>the type words, <code>bitcast T v</code></td></tr><tr><td>I/O</td><td><code>read fd A off n</code>, <code>write fd A off n</code>, <code>pread</code> / <code>pwrite fd A off n pos</code>, and the 67 host words of section 10</td></tr></table><ul><li><code>and</code>, <code>or</code>, <code>xor</code> on integers of any width are bitwise, not logical: <code>and ulong 12 ulong 10</code> is 8
47(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/int_bits_and.om"><code>examples/int_bits_and.om</code></a>). Compare first when you want a bool.</li><li><code>and</code> / <code>or</code> on bools evaluate <strong>both</strong> sides; there is no short circuit
48(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/mistake_short_circuit.om"><code>examples/mistake_short_circuit.om</code></a> traps 3 on the
49index the C reader would expect to be skipped). Test the index in an outer <code>if</code>.</li><li>Comparisons follow the type: on <code>ulong</code>, <code>lt x y</code> with x = 2^64-1 and y = 1 is false.</li><li><code>div</code> and <code>rem</code> on signed values round toward zero; <code>rem</code> takes the dividend's sign;
50<code>shr</code> on a signed value keeps the sign.</li><li>A function with a result is a value, not a statement: write <code>v = f 1</code>, never a bare <code>f 1</code>
51(error 7). A <code>void</code> function is called as a statement.</li><li>A call is the name then its values with no brackets or commas: <code>ret f 1 2</code>.</li></ul><h3 id="s5">5. Traps: what stops a running program</h3><p>A trap ends the process with exit status 100 + N. Output already written stays written.</p><table><tr><th>Cause</th><th>Trap</th><th>Exit</th><th>Example</th></tr><tr><td>signed overflow in <code>add sub mul neg</code></td><td>1</td><td>101</td><td><code>examples/trap1_overflow.om</code></td></tr><tr><td><code>div</code> or <code>rem</code> by zero, and signed minimum divided by -1</td><td>2</td><td>102</td><td><code>trap2_divzero.om</code></td></tr><tr><td>array index or I/O slice out of bounds</td><td>3</td><td>103</td><td><code>trap3_bounds.om</code></td></tr><tr><td>a loop past its bound</td><td>4</td><td>104</td><td><code>trap4_loop.om</code></td></tr><tr><td>an integer conversion whose value does not fit</td><td>5</td><td>105</td><td><code>trap5_conv.om</code></td></tr><tr><td>stack exhausted (8 MiB) by calls or local arrays</td><td>6</td><td>106</td><td><code>trap6_stack.om</code></td></tr><tr><td>shift or rotate amount outside 0..width-1</td><td>7</td><td>107</td><td><code>trap7_shift.om</code></td></tr><tr><td>a contract check that fails (OM-2)</td><td>9, 10</td><td>109, 110</td><td><code>contract_requires.om</code>, <code>contract_ensures.om</code></td></tr><tr><td><code>trap N</code>, N 1..14</td><td>N</td><td>100+N</td><td><code>trap_code.om</code> (<code>trap 14</code>, exit 114)</td></tr></table><ul><li>Unsigned arithmetic wraps modulo 2^width and never traps
52(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/unsigned_wrap.om"><code>examples/unsigned_wrap.om</code></a>).</li><li>Integer conversions are checked: <code>uchar x</code> with x = 300 traps 5. Keep the low bits on purpose with
53a mask: <code>uchar and x 255</code>.</li><li>Float to integer truncates toward zero and saturates at the type's limits; NaN gives 0; it never
54traps (<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/float_to_int.om"><code>examples/float_to_int.om</code></a>). No float operation traps:
551/0 is infinity, 0/0 is NaN, and NaN compares false except with <code>ne</code>.</li><li><code>exit N</code> ends the process with status N directly, without the trap convention.</li></ul><h3 id="s6">6. Loops and bounds</h3><ul><li><code>while COND bound N</code> runs the body at most N times, N 1..999999 (error 8 outside). Without
56<code>bound</code> the limit is 1000000. One more pass traps 4. Choose N from an argument you can write down
57in a <code>note</code>, for example &quot;every pass consumes at least one byte of a 65536-byte input&quot;.</li><li><code>for i T K</code> runs i from 0 to K-1. <code>for</code> takes no <code>bound</code> (error 5); K is the limit.</li><li><code>break</code> and <code>continue</code> work in both loops. There is no <code>else if</code>: nest an <code>if</code> inside <code>else</code>.</li><li>No loop runs forever and recursion is bounded by the stack.</li></ul><h3 id="s7">7. Arrays, texts, globals, structs</h3><ul><li><strong>Local array:</strong> <code>a = new T N v</code>, N elements each v, only in a function's outermost block (error 9
58inside a block). N is a literal or an integer <code>const</code>; a variable N is error 16. There is no heap
59and no <code>free</code> (error 9).</li><li><strong>Global array:</strong> <code>global NAME T N</code> at top level, zero-filled, program mode only. <strong>A global is
60never an argument</strong> (error 19); a function uses it by naming it.</li><li><strong>Text:</strong> <code>text NAME &quot;bytes&quot;</code> is a read-only <code>uchar</code> array. Escapes are only <code>\n \t \\ \&quot;</code>
61(error 22 for others).</li><li><strong>Array parameters</strong> state type and exact length: <code>a view uchar 64</code> (read-only) or
62<code>a edit uchar 64</code> (writable). Another length is error 7. In OM-2, <code>a view T *</code> / <code>a edit T *</code> take
63any length. Writing to a view or text is error 20.</li><li><code>get A i</code> / <code>put A i v</code>: a literal index outside the array is error 21; every other index is checked
64at run time (trap 3). There are no pointers and no address arithmetic.</li><li><strong>Structs:</strong> <code>struct Pair a uint b long end</code>, before the first <code>fn</code>. Every field is an 8-byte cell,
65so a struct is never a wire or file layout. <code>p = new Pair 5 -12</code>, pass as <code>p edit Pair</code> or
66<code>p view Pair</code> (<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/structs.om"><code>examples/structs.om</code></a>).</li><li><strong>Bytes on the wire:</strong> build them with shifts and masks into a <code>uchar</code> array
67(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/wire_le.om"><code>examples/wire_le.om</code></a>); OM-2 also has <code>layout</code>.</li></ul><h3 id="s8">8. Names and limits</h3><ul><li>Variable, parameter, global, text, const, struct and field names: at most 8 characters (error 13).
68Function names: at most 63, but see section 13 for names that start with an 8-letter keyword.</li><li>A new name may not repeat a visible name, a function or a struct (error 10). A name first assigned
69inside a block ends at that block's <code>end</code>; assign it before the block to keep it.</li><li>Reserved words cannot be names, including `len read write text new free view edit global note
70exit const bound<code> and every host word (</code>stat<code>, </code>link<code>, </code>send<code>, </code>random<code>, </code>fork<code>, ...). </code>len` as a
71name is error 16, <code>free</code> is error 9.</li><li>16 registers per function hold parameters, live names, loop counters and the temporaries of the
72current statement. The 17th is error 12 (<code>examples/err12_registers.om</code>). The fix is structural:
73move part of the work into a helper function that takes a few scalars and returns one value, end
74the life of names you no longer need by computing them inside a block, and split a long nested
75expression into several assignments of one or two words each.</li><li>Size limits: an array up to 16777216 elements, 4 MiB of local arrays per function, 1 GiB of
76globals (error 23).</li></ul><h3 id="s9">9. OM-2 units</h3><p>A unit whose first line that is not blank or <code>note</code> is exactly <code>om2</code> is an OM-2 unit. OM-1 code keeps
77its meaning inside it. OM-2 adds:</p><ul><li>lowercase hex literals; <code>rotr x n</code>, <code>rotl x n</code> on unsigned types (trap 7 for an amount outside
780..width-1);</li><li>unsized parameters <code>a view T *</code> / <code>a edit T *</code> with <code>len a</code> the run-time count;</li><li><code>part A off n</code>, only as an argument to an unsized parameter: the n elements of A from off,
79checked at run time (trap 3). A part of a global is error 19; as a value of its own it is error 9;
80to a sized parameter it is error 7;</li><li><code>for i T K unroll</code>, <code>fn ... inline</code>, <code>layout</code> (external byte layouts), <code>fresh</code> scratch arrays
81(error 27), <code>abstract</code> types with <code>seal</code> / <code>open</code> (error 28), <code>export fn</code> (at most 32, error 14);</li><li>executable contracts: <code>requires</code> and <code>ensures</code> lines at the start of a function, <code>invariant</code> as the
82first line of a loop body. A failing <code>requires</code> traps 9 (exit 109), a failing <code>ensures</code> or
83<code>invariant</code> traps 10 (exit 110). A clause may not call a function, use floats or host words
84(error 30). The first line <code>om2 release</code> erases the checks
85(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/contract_release.om"><code>examples/contract_release.om</code></a>).</li></ul><p>Examples: <a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/om2_tour.om"><code>om2_tour.om</code></a>, <a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/om2_views.om"><code>om2_views.om</code></a>,
86<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/om2_hex_rotr.om"><code>om2_hex_rotr.om</code></a>, <a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/contracts_ok.om"><code>contracts_ok.om</code></a>.</p><h3 id="s10">10. Host I/O</h3><ul><li>Every host word returns a <code>long</code>: a count, or minus the Linux errno. <code>read</code> may return fewer bytes
87than asked, 0 at end of input, or a negative error; <code>write</code> may write fewer. Loop until done and
88handle <code>k &lt; 0</code> and <code>k == 0</code> separately
89(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/stdin_count.om"><code>examples/stdin_count.om</code></a>).</li><li>Descriptors, flags and modes are <code>int</code>; offsets, counts and capacities <code>ulong</code>; buffers <code>uchar</code>
90arrays. Slices are checked before the kernel call (trap 3).</li><li><code>main</code> takes no parameters: read arguments with <code>argcnt</code> and <code>argread</code>
91(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/echo_args.om"><code>examples/echo_args.om</code></a>) and the environment with <code>envcnt</code> and
92<code>envread</code> (<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/env_read.om"><code>examples/env_read.om</code></a>). Short capacity returns -105.</li><li>Host words carry the process's own permissions; they are not a sandbox.</li></ul><p>The 67 host words at the pin, with their values (OMEGESE section 10 has each one's exact contract).
93<code>A</code> and <code>B</code> are <code>uchar</code> arrays, <code>off</code>, <code>n</code> and <code>capacity</code> are <code>ulong</code>, and descriptors, flags, modes
94and ids are <code>int</code>. A path is the slice <code>A off n</code>; a packed list is a <code>u16</code> count, then <code>u16</code>-length
95records. Each word is reserved: using it as a name is error 16 at the pin, while an unreserved name
96in the same program compiles.</p><table><tr><th>Area</th><th>Words</th></tr><tr><td>files</td><td><code>fopen A off n flags mode</code>, <code>fclose fd</code>, <code>fsync fd</code>, <code>fdsync fd</code>, <code>syncfs fd</code>, <code>memfd A off n flags</code>, <code>fseek fd offset whence</code>, <code>ftrunc fd length</code>, <code>flock fd op</code>, <code>fstat fd A off n</code>, <code>getdnts fd A off n</code></td></tr><tr><td>paths</td><td><code>chmod A off n mode</code>, <code>fchmod fd mode</code>, <code>chown A off n uid gid</code>, <code>faccess A off n mode</code>, <code>truncate A off n length</code>, <code>unlink A off n</code>, <code>makedir A off n mode</code>, <code>rmdir A off n</code>, <code>cwd A off capacity</code>, <code>chdir A off n</code>, <code>fchdir fd</code>, <code>readlnk A off n B off capacity</code>, <code>stat A off n B off capacity</code>, <code>lstat A off n B off capacity</code>, <code>renames A off n flags</code>, <code>link A off n</code>, <code>symlnk A off n</code>, <code>umask mask</code></td></tr><tr><td>processes</td><td><code>getpid</code>, <code>getppid</code>, <code>getuid</code>, <code>geteuid</code>, <code>getgid</code>, <code>getegid</code>, <code>setsid</code>, <code>getpgid pid</code>, <code>getsid pid</code>, <code>setpgid pid pgid</code>, <code>fork</code>, <code>killpid pid signal</code>, <code>waitpid pid A off n options</code>, <code>pipefds A off n flags</code>, <code>dupfd fd target flags</code>, <code>execprog A off n</code></td></tr><tr><td>arguments, environment, randomness</td><td><code>argcnt</code>, <code>argread index A off capacity</code>, <code>envcnt</code>, <code>envread index A off capacity</code>, <code>random A off n flags</code></td></tr><tr><td>time</td><td><code>monons</code>, <code>realns</code>, <code>sleepns interval</code></td></tr><tr><td>sockets</td><td><code>socknew domain kind protocol</code>, <code>bind fd A off n</code>, <code>connect fd A off n</code>, <code>listen fd backlog</code>, <code>accept fd flags</code>, <code>sockname fd A off n</code>, <code>sockopt fd level option A off n</code>, <code>recv fd A off n flags</code>, <code>send fd A off n flags</code>, <code>sendmsg fd A off n flags</code>, <code>shutdown fd how</code>, <code>waitio fd events interval</code>, <code>pollfds A off n interval</code>, <code>peercred fd A off capacity</code></td></tr></table><h3 id="s11">11. Compiler errors</h3><p>Exit status = error number. The only message is the one stderr line <code>omc: line N: error K</code>, where N
97is the line being read (0 when no line was read). One refusal per code is in <code>examples/errNN_*.om</code>
98with its exact stderr line in the matching <code>.expect</code>, except 24, which needs a generated program.</p><table><tr><th>Code</th><th>Meaning</th><th>Example</th><th>Fix</th></tr><tr><td>1</td><td>omc could not read its input</td><td><code>err01_input</code> (stdin is a directory)</td><td>give omc a readable file on stdin</td></tr><tr><td>2</td><td>omc could not write its output</td><td><code>err02_output</code> (stdout is <code>/dev/full</code>)</td><td>send stdout to a writable file with space</td></tr><tr><td>3</td><td>unknown name, field or function</td><td><code>err03_unknown_name</code></td><td>assign the name before use; check the spelling</td></tr><tr><td>4</td><td>the line ends before the statement is complete</td><td><code>err04_line_ends</code></td><td>give every word all of its values</td></tr><tr><td>5</td><td>words left after a complete statement</td><td><code>err05_extra_words</code></td><td>count each word's values; there is no infix</td></tr><tr><td>6</td><td>malformed number</td><td><code>err06_bad_number</code></td><td>decimal digits; hex only in OM-2, lowercase</td></tr><tr><td>7</td><td>type mismatch</td><td><code>err07_type_mismatch</code></td><td>convert with a type word: <code>add int 1 int u</code></td></tr><tr><td>8</td><td>number out of range</td><td><code>err08_out_of_range</code></td><td>a literal that fits its type; a bound 1..999999; a trap code 1..14</td></tr><tr><td>9</td><td>a word in a place where it is not allowed</td><td><code>err09_disallowed</code></td><td>move it: <code>new</code> only in a function's outermost block, <code>struct</code> before the first <code>fn</code></td></tr><tr><td>10</td><td>duplicate name</td><td><code>err10_duplicate</code></td><td>choose a name that is not visible, not a function, not a struct</td></tr><tr><td>11</td><td>a number with nothing to give it a type</td><td><code>err11_ambiguous</code></td><td><code>x = uint 5</code></td></tr><tr><td>12</td><td>register pool exhausted (16 per function)</td><td><code>err12_registers</code></td><td>fewer live names, a helper function, shorter expressions (section 8)</td></tr><tr><td>13</td><td>name too long</td><td><code>err13_long_name</code></td><td>at most 8 characters (function names 63)</td></tr><tr><td>14</td><td>compiler capacity: names, functions, exports, expansion</td><td><code>err14_capacity</code></td><td>split the function or the unit</td></tr><tr><td>15</td><td>the input ends inside an open block</td><td><code>err15_missing_end</code></td><td>close every block with <code>end</code></td></tr><tr><td>16</td><td>syntax: unknown word or misplaced form</td><td><code>err16_syntax</code></td><td>start each line with a statement word; comments are <code>note</code> lines</td></tr><tr><td>17</td><td>a line after <code>ret</code>, <code>break</code>, <code>continue</code>, <code>trap</code> or <code>exit</code> in the same block</td><td><code>err17_after_path</code></td><td>delete the line or move it above</td></tr><tr><td>18</td><td>a function with a result whose <code>end</code> is reachable</td><td><code>err18_missing_ret</code></td><td>end every path with <code>ret v</code></td></tr><tr><td>19</td><td>a global as an argument, or one array twice in a call</td><td><code>err19_global_arg</code></td><td>name the global inside the function; pass distinct arrays</td></tr><tr><td>20</td><td>writing to a text or a view</td><td><code>err20_readonly</code></td><td>an <code>edit</code> parameter, or a local copy</td></tr><tr><td>21</td><td>a literal index outside the array</td><td><code>err21_const_index</code></td><td>an index 0..N-1</td></tr><tr><td>22</td><td>a text escape other than <code>\n \t \\ \&quot;</code></td><td><code>err22_text_escape</code></td><td>build other bytes with <code>put</code></td></tr><tr><td>23</td><td>array or pool size limit</td><td><code>err23_size</code></td><td>smaller arrays (section 8 limits)</td></tr><tr><td>24</td><td>a conditional branch farther than 1 MiB of code</td><td>generated, below</td><td>move long bodies into functions</td></tr><tr><td>25, 26</td><td>never emitted at the pin</td><td>none</td><td>none</td></tr><tr><td>27</td><td>OM-2 <code>fresh</code> element read before it is written</td><td><code>err27_fresh</code></td><td><code>put</code> every element before the <code>get</code></td></tr><tr><td>28</td><td>OM-2 <code>seal</code> outside the constructor</td><td><code>err28_seal</code></td><td><code>seal</code> only as the whole body of <code>fn NAMEmk ... to NAME</code></td></tr><tr><td>29</td><td>OM-2 <code>layout</code> field past the size or overlapping</td><td><code>err29_layout</code></td><td>keep every field inside SIZE and apart</td></tr><tr><td>30</td><td>OM-2 contract clause not allowed</td><td><code>err30_contract</code></td><td>integer words on parameters only (and <code>result</code> in <code>ensures</code>); no calls, floats or host words</td></tr></table><p>How the emitted set was established at the pin: in <code>seed/omc/omc.om</code>, <code>fn fail</code> (line 293) writes
99the stderr line and exits with the code (<code>exit int code</code>, line 322). The only other exits are
100<code>exit int 0</code> and <code>exit 0</code>, so every refusal goes through <code>fail</code>. Its callers pass the literals 1..24
101and 27..30, or forward a literal 3, 4, 6, 8, 13 or 16 through <code>lenval</code>, <code>nfail</code> and <code>unkfail</code>. No
102caller passes 25 or 26, so those two codes are never emitted. A trap inside omc itself would exit
103100 + N, which is not an error code.</p><p>Error 24 needs more than 1 MiB of code inside one loop. This program, with 270000 copies of the
104line <code>x = add x 1</code> (4 bytes of code each), was refused with <code>omc: line 270006: error 24</code>, the line
105of the loop's <code>end</code>; with 140000 copies it compiled:</p><pre>
106{ printf 'fn main to int\nx = ulong 0\nc = true\nwhile c bound 1\n'
107 yes 'x = add x 1' | head -n 270000
108 printf 'c = false\nend\nret int and x 1\nend\n'; } &gt; e24.om
109seed/out/omc &lt; e24.om &gt; e24; echo &quot;compile $?&quot; # compile 24
110</pre><h3 id="s12">12. Common mistakes</h3><p>Each row is a program in <code>examples/</code> with its observed refusal.</p><table><tr><th>Mistake</th><th>Write instead</th><th>Refusal</th><th>Example</th></tr><tr><td><code>y = x + 1</code></td><td><code>y = add x 1</code></td><td>error 5</td><td><code>mistake_infix.om</code></td></tr><tr><td><code>f(1, 2)</code></td><td><code>f 1 2</code></td><td>error 6</td><td><code>mistake_c_call.om</code></td></tr><tr><td><code>if x &gt; 0</code></td><td><code>if gt x 0</code></td><td>error 7</td><td><code>mistake_compare_operator.om</code></td></tr><tr><td><code>'A'</code></td><td><code>uchar 65</code></td><td>error 3</td><td><code>mistake_char_literal.om</code></td></tr><tr><td><code>// comment</code></td><td><code>note comment</code></td><td>error 16</td><td><code>mistake_slash_comment.om</code></td></tr><tr><td><code>x = 1;</code></td><td><code>x = uint 1</code></td><td>error 6</td><td><code>mistake_semicolon.om</code></td></tr><tr><td><code>return x</code></td><td><code>ret x</code></td><td>error 18 (silent assignment)</td><td><code>mistake_return.om</code></td></tr><tr><td><code>else if c</code></td><td><code>else</code>, then a nested <code>if c</code></td><td>error 5</td><td><code>mistake_else_if.om</code></td></tr><tr><td><code>fn main argc int to int</code></td><td><code>argcnt</code>, <code>argread</code></td><td>error 9</td><td><code>mistake_main_args.om</code></td></tr><tr><td><code>for i ulong n bound 10</code></td><td><code>while lt i n bound 10</code></td><td>error 5</td><td><code>mistake_for_bound.om</code></td></tr><tr><td><code>while c bound 1000000</code></td><td>a bound 1..999999</td><td>error 8</td><td><code>mistake_bound_range.om</code></td></tr><tr><td><code>a = new uchar n 0</code></td><td>a literal or <code>const</code> length</td><td>error 16</td><td><code>mistake_new_var_len.om</code></td></tr><tr><td><code>free a</code></td><td>nothing: no heap</td><td>error 9</td><td><code>mistake_free.om</code></td></tr><tr><td><code>len = ...</code></td><td>another name</td><td>error 16</td><td><code>mistake_reserved_name.om</code></td></tr><tr><td><code>neg = ...</code> or <code>text neg &quot;a&quot;</code></td><td>another name: <code>neg</code> is the negation word</td><td>error 16</td><td><code>mistake_neg_name.om</code></td></tr><tr><td>a bare call <code>f 1</code> with a result</td><td><code>v = f 1</code></td><td>error 7</td><td><code>mistake_value_stmt.om</code></td></tr><tr><td>a 7th parameter</td><td>a struct or an array</td><td>error 9</td><td><code>mistake_seven_params.om</code></td></tr><tr><td>2 <code>view T *</code> plus 3 scalars (7 slots)</td><td>a sized view, a struct, or fewer scalars</td><td>error 9</td><td><code>mistake_om2_param_slots.om</code></td></tr><tr><td><code>emit buf</code> with <code>buf</code> a global</td><td>name the global inside the function</td><td>error 19</td><td><code>err19_global_arg.om</code></td></tr><tr><td><code>a view uchar 8</code> given a 16-element array</td><td>the exact length, or OM-2 <code>view uchar *</code></td><td>error 7</td><td><code>mistake_param_length.om</code></td></tr><tr><td><code>trap 15</code></td><td><code>trap 1</code> .. <code>trap 14</code></td><td>error 8</td><td><code>mistake_trap_range.om</code></td></tr><tr><td><code>struct</code> after a <code>fn</code></td><td>every <code>struct</code> before the first <code>fn</code></td><td>error 9</td><td><code>mistake_struct_after_fn.om</code></td></tr><tr><td><code>rotr</code> or hex without <code>om2</code></td><td>an <code>om2</code> first line</td><td>error 3</td><td><code>mistake_om1_rotr.om</code></td></tr><tr><td><code>0X10</code> or <code>0xFF</code></td><td><code>0x10</code>, <code>0xff</code></td><td>error 6</td><td><code>mistake_om2_upper_hex.om</code></td></tr><tr><td><code>int 0xffffffff</code></td><td><code>int -1</code></td><td>error 8</td><td><code>mistake_om2_int_hex.om</code></td></tr><tr><td><code>part</code> of a global</td><td><code>part</code> of a local</td><td>error 19</td><td><code>mistake_om2_part_global.om</code></td></tr><tr><td><code>part</code> to a sized parameter</td><td>an unsized <code>view T *</code> parameter</td><td>error 7</td><td><code>mistake_om2_part_sized.om</code></td></tr><tr><td><code>s = part a 0 2</code></td><td><code>part</code> only as an argument</td><td>error 9</td><td><code>mistake_om2_part_statement.om</code></td></tr><tr><td><code>if and lt i n eq get a i 0</code> (C short circuit)</td><td>an outer <code>if lt i n</code></td><td>trap 3, exit 103</td><td><code>mistake_short_circuit.om</code></td></tr></table><h3 id="s13">13. Known compiler defects at the pin</h3><ul><li><strong><code>bitcast</code> across signedness below 64 bits</strong> (fix proposed in draft omega-mv &#35;896, not in the
111pin): the result does not compare equal to the reinterpreted value. Observed wrong: <code>uint</code> to
112<code>int</code> (4294967295 is not -1, and adding 2 traps 1), <code>int</code> to <code>uint</code>, <code>ushort</code> to and from
113<code>short</code>, <code>uchar</code> to and from <code>byte</code>, and <code>f32</code> to <code>int</code> (-1.0 is not -1082130432 and not below 0).
114Observed correct: <code>ulong</code> to and from <code>long</code>, <code>f64</code> to <code>long</code>, <code>f32</code> to <code>uint</code>, <code>int</code> to <code>f32</code>.
115Until a fixed compiler is pinned, do not <code>bitcast</code> between narrow integer types or from <code>f32</code> to
116<code>int</code>; decode signed 8, 16 and 32-bit values arithmetically: load into <code>long</code>, then subtract 2^32
117(or 2^16, 2^8) when the value is at least 2^31 (2^15, 2^7), as
118<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/wire_le.om"><code>examples/wire_le.om</code></a> does. For float bits, <code>bitcast uint</code> of an
119<code>f32</code> is correct.</li><li><strong>Empty text through <code>view uchar *</code></strong> (omega-mv issue &#35;775): the parameter does not arrive with
120<code>len</code> 0 (observed 60 and 28, depending on the other texts in the unit), so a loop over it runs past
121the text (a crash has been reported; not reproduced here). Workaround: pass <code>part e 0 0</code>, which
122arrives with <code>len</code> 0
123(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/empty_text_part.om"><code>examples/empty_text_part.om</code></a>).</li><li><strong>Function names that begin with an 8-letter keyword:</strong> a function name longer than 8 bytes whose
124first 8 bytes are a keyword (<code>continue_x</code>, <code>truncate_number</code>, <code>sockname_x</code>, <code>execprog_x</code>,
125<code>peercred_x</code>) is error 16, although function names may be 63 characters. Shorter keyword prefixes
126are fine (<code>breakx</code>, <code>whilex</code>, <code>getpid_x</code>). Workaround: rename (<code>cont_x</code>, <code>trunc_num</code>)
127(<a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/defect_keyword_prefix.om"><code>examples/defect_keyword_prefix.om</code></a>).</li><li>A fix for both is in progress in omega-mv; until a new compiler is pinned, use the workarounds.</li><li>OMEGESE 9.9 lists <code>+ - * /</code> for floats; the compiler refuses them. Write <code>add sub mul div</code>.</li></ul><h3 id="s14">14. Example index</h3><p>All in <a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/"><code>examples/</code></a>; each <code>NAME.om</code> has <code>NAME.expect</code> (compile status, exit status
128or stderr line) and, when it prints, <code>NAME.stdout</code> with the exact bytes. Wolfram checks of the
129arithmetic expectations are in <a href="https://interplane.io/drakestapleton/omega-harness/blob/0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e/examples/WOLFRAM.md"><code>examples/WOLFRAM.md</code></a>.</p><ul><li>Start here: <code>hello</code>, <code>exit_status</code>, <code>print_decimal</code>, <code>functions</code>, <code>loops</code>, <code>arrays</code>, <code>text</code>.</li><li>Arithmetic: <code>int_arith</code>, <code>int_bits_and</code>, <code>unsigned_wrap</code>, <code>checked_conv</code>, <code>float_arith</code>,
130<code>float_to_int</code>, <code>consts</code>.</li><li>Data: <code>globals</code>, <code>structs</code>, <code>wire_le</code>.</li><li>I/O: <code>stdin_count</code>, <code>echo_args</code>, <code>env_read</code>, <code>tmp_files</code>.</li><li>Traps: <code>trap1_overflow</code> .. <code>trap7_shift</code>, <code>trap_code</code>.</li><li>OM-2 and contracts: <code>om2_tour</code>, <code>om2_views</code>, <code>om2_hex_rotr</code>, <code>om2_part_trap</code>, <code>om2_param_slots</code>,
131<code>contracts_ok</code>, <code>contract_requires</code>, <code>contract_ensures</code>, <code>contract_invariant</code>, <code>contract_release</code>.</li><li>Known defects and their workarounds: <code>empty_text_part</code>, <code>defect_keyword_prefix</code>.</li><li>Refusals: <code>err01_input</code> .. <code>err30_contract</code>, <code>mistake_*</code>.</li></ul></section><section class="om-section ref-examples" id="examples"><h2 class="om-h2">Worked examples</h2><p>98 programs from <code>examples/</code> and <code>skills/omega-encoding/examples/</code>, each listed with its files. <code>NAME.om</code> is the program. <code>NAME.expect</code> is what the gate checks: <code>compile N</code> is the compiler's exit status, <code>stderr LINE</code> its whole stderr when N is not 0, <code>exit N</code> the program's exit status, <code>input dir</code> and <code>output full</code> the two refusals that need a special compiler input or output. <code>NAME.stdout</code> is the exact standard output, and an absent one means empty. <code>NAME.stdin</code>, <code>NAME.args</code> (one argument per line) and <code>NAME.env</code> (one KEY=VALUE per line) are what the program is given.</p><p>Each file is labelled <code>File: PATH</code> and followed by its bytes in one block. To copy a file out of llms-full.txt, take the lines between its two fences and end each with a newline. A label ending in (no final newline) means the file does not end in a newline, so drop the last one. A label ending in (hex bytes) means the block lists the bytes as two hex digits each, sixteen to a line. A label ending in (empty file) means the file has no bytes and no block follows.</p><article class="ref-ex" id="ex-arrays"><h3>arrays</h3><p class="ref-file">File: <code>examples/arrays.om</code></p><pre>
132note arrays: a local array is new T N v in the outermost block; N is a literal or an integer const
133note ---- output helpers: a function works on a global by naming it (a global is never an argument)
134global ob uchar 32
135note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
136note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
137fn outb off ulong n ulong to void
138e = add off n
139p = off
140while lt p e bound 32
141k = write 1 ob p sub e p
142if le k 0
143exit 90
144end
145p = add p ulong k
146end
147end
148note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
149fn putu v ulong to void
150i = ulong 31
151put ob 31 10
152n = v
153while true bound 20
154i = sub i 1
155put ob i uchar add rem n 10 48
156n = div n 10
157if eq n 0
158break
159end
160end
161outb i sub 32 i
162end
163note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
164fn putl v long to void
165if lt v 0
166put ob 0 45
167outb 0 1
168putu add ulong neg add v 1 1
169ret
170end
171putu ulong v
172end
173fn main to int
174a = new uint 8 0
175for i ulong 8
176put a i uint mul add i 1 add i 1
177end
178s = ulong 0
179for i ulong len a
180s = add s ulong get a i
181end
182putu s
183putu len a
184putu ulong get a 7
185ret 0
186end
187</pre><p class="ref-file">File: <code>examples/arrays.expect</code></p><pre>
188compile 0
189exit 0
190</pre><p class="ref-file">File: <code>examples/arrays.stdout</code></p><pre>
191204
1928
19364
194</pre></article><article class="ref-ex" id="ex-checked_conv"><h3>checked_conv</h3><p class="ref-file">File: <code>examples/checked_conv.om</code></p><pre>
195note checked_conv: integer conversions check the value; mask first to keep low bits
196note ---- output helpers: a function works on a global by naming it (a global is never an argument)
197global ob uchar 32
198note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
199note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
200fn outb off ulong n ulong to void
201e = add off n
202p = off
203while lt p e bound 32
204k = write 1 ob p sub e p
205if le k 0
206exit 90
207end
208p = add p ulong k
209end
210end
211note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
212fn putu v ulong to void
213i = ulong 31
214put ob 31 10
215n = v
216while true bound 20
217i = sub i 1
218put ob i uchar add rem n 10 48
219n = div n 10
220if eq n 0
221break
222end
223end
224outb i sub 32 i
225end
226note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
227fn putl v long to void
228if lt v 0
229put ob 0 45
230outb 0 1
231putu add ulong neg add v 1 1
232ret
233end
234putu ulong v
235end
236fn main to int
237x = uint 300
238putu ulong uchar and x 255
239y = long 2147483647
240putl long int y
241w = int -1
242putl long w
243putu ulong ushort 65535
244ret 0
245end
246</pre><p class="ref-file">File: <code>examples/checked_conv.expect</code></p><pre>
247compile 0
248exit 0
249</pre><p class="ref-file">File: <code>examples/checked_conv.stdout</code></p><pre>
25044
2512147483647
252-1
25365535
254</pre></article><article class="ref-ex" id="ex-consts"><h3>consts</h3><p class="ref-file">File: <code>examples/consts.om</code></p><pre>
255note consts: a top-level const is visible after its line and may size an array, bound a loop or name a trap
256note ---- output helpers: a function works on a global by naming it (a global is never an argument)
257global ob uchar 32
258note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
259note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
260fn outb off ulong n ulong to void
261e = add off n
262p = off
263while lt p e bound 32
264k = write 1 ob p sub e p
265if le k 0
266exit 90
267end
268p = add p ulong k
269end
270end
271note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
272fn putu v ulong to void
273i = ulong 31
274put ob 31 10
275n = v
276while true bound 20
277i = sub i 1
278put ob i uchar add rem n 10 48
279n = div n 10
280if eq n 0
281break
282end
283end
284outb i sub 32 i
285end
286note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
287fn putl v long to void
288if lt v 0
289put ob 0 45
290outb 0 1
291putu add ulong neg add v 1 1
292ret
293end
294putu ulong v
295end
296const N ulong 16
297const PASSES ulong 16
298const BAD ulong 12
299fn main to int
300a = new uchar N 1
301s = ulong 0
302i = ulong 0
303while lt i len a bound PASSES
304s = add s ulong get a i
305i = add i 1
306end
307putu s
308if ne s N
309trap BAD
310end
311ret 0
312end
313</pre><p class="ref-file">File: <code>examples/consts.expect</code></p><pre>
314compile 0
315exit 0
316</pre><p class="ref-file">File: <code>examples/consts.stdout</code></p><pre>
31716
318</pre></article><article class="ref-ex" id="ex-contract_ensures"><h3>contract_ensures</h3><p class="ref-file">File: <code>examples/contract_ensures.om</code></p><pre>
319note contract_ensures: a false postcondition traps 10, exit 110; the bug here is the + 2
320om2
321text before &quot;before\n&quot;
322fn inc x uint to uint
323requires lt x 100
324ensures eq result add x 1
325ret add x 2
326end
327fn main to int
328k = write 1 before 0 len before
329h = inc 5
330ret 0
331end
332</pre><p class="ref-file">File: <code>examples/contract_ensures.expect</code></p><pre>
333compile 0
334exit 110
335</pre><p class="ref-file">File: <code>examples/contract_ensures.stdout</code></p><pre>
336before
337</pre></article><article class="ref-ex" id="ex-contract_invariant"><h3>contract_invariant</h3><p class="ref-file">File: <code>examples/contract_invariant.om</code></p><pre>
338note contract_invariant: a false loop invariant traps 10, exit 110, at the top of the failing pass
339om2
340text before &quot;before\n&quot;
341fn main to int
342k = write 1 before 0 len before
343i = uint 0
344while lt i 10
345invariant lt i 5
346i = add i 1
347end
348ret 0
349end
350</pre><p class="ref-file">File: <code>examples/contract_invariant.expect</code></p><pre>
351compile 0
352exit 110
353</pre><p class="ref-file">File: <code>examples/contract_invariant.stdout</code></p><pre>
354before
355</pre></article><article class="ref-ex" id="ex-contract_release"><h3>contract_release</h3><p class="ref-file">File: <code>examples/contract_release.om</code></p><pre>
356note contract_release: om2 release erases contracts; the false precondition no longer traps
357om2 release
358fn half x uint to uint
359requires eq rem x 2 0
360ret div x 2
361end
362fn main to int
363ret int half 7
364end
365</pre><p class="ref-file">File: <code>examples/contract_release.expect</code></p><pre>
366compile 0
367exit 3
368</pre><p class="ref-file">File: <code>examples/contract_release.stdout</code> (empty file)</p></article><article class="ref-ex" id="ex-contract_requires"><h3>contract_requires</h3><p class="ref-file">File: <code>examples/contract_requires.om</code></p><pre>
369note contract_requires: a false precondition traps 9, exit 109, before the body runs
370om2
371text before &quot;before\n&quot;
372fn half x uint to uint
373requires eq rem x 2 0
374ret div x 2
375end
376fn main to int
377k = write 1 before 0 len before
378h = half 7
379ret 0
380end
381</pre><p class="ref-file">File: <code>examples/contract_requires.expect</code></p><pre>
382compile 0
383exit 109
384</pre><p class="ref-file">File: <code>examples/contract_requires.stdout</code></p><pre>
385before
386</pre></article><article class="ref-ex" id="ex-contracts_ok"><h3>contracts_ok</h3><p class="ref-file">File: <code>examples/contracts_ok.om</code></p><pre>
387note contracts_ok: requires and ensures form a function's prefix; invariant opens a loop body
388om2
389fn step x uint to uint
390requires lt x 10
391ensures eq result add x 1
392ret add x 1
393end
394fn main to int
395i = uint 0
396while lt i 3
397invariant le i 3
398i = step i
399end
400ret int sub i 3
401end
402</pre><p class="ref-file">File: <code>examples/contracts_ok.expect</code></p><pre>
403compile 0
404exit 0
405</pre><p class="ref-file">File: <code>examples/contracts_ok.stdout</code> (empty file)</p></article><article class="ref-ex" id="ex-defect_keyword_prefix"><h3>defect_keyword_prefix</h3><p class="ref-file">File: <code>examples/defect_keyword_prefix.om</code></p><pre>
406note defect_keyword_prefix: known defect at the pin. A function name longer than 8 bytes whose first
407note 8 bytes are an 8-letter keyword (continue, truncate, sockname, execprog, peercred) is refused error 16.
408note Workaround: rename (cont_x, trunc_num). Expected to change when the compiler fix is pinned
409fn continue_x to int
410ret 7
411end
412fn main to int
413ret continue_x
414end
415</pre><p class="ref-file">File: <code>examples/defect_keyword_prefix.expect</code></p><pre>
416compile 16
417stderr omc: line 4: error 16
418</pre></article><article class="ref-ex" id="ex-echo_args"><h3>echo_args</h3><p class="ref-file">File: <code>examples/echo_args.om</code></p><pre>
419note echo_args: argcnt includes the program name; argread copies one argument without a terminator
420note ---- output helpers: a function works on a global by naming it (a global is never an argument)
421global ob uchar 32
422note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
423note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
424fn outb off ulong n ulong to void
425e = add off n
426p = off
427while lt p e bound 32
428k = write 1 ob p sub e p
429if le k 0
430exit 90
431end
432p = add p ulong k
433end
434end
435note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
436fn putu v ulong to void
437i = ulong 31
438put ob 31 10
439n = v
440while true bound 20
441i = sub i 1
442put ob i uchar add rem n 10 48
443n = div n 10
444if eq n 0
445break
446end
447end
448outb i sub 32 i
449end
450note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
451fn putl v long to void
452if lt v 0
453put ob 0 45
454outb 0 1
455putu add ulong neg add v 1 1
456ret
457end
458putu ulong v
459end
460global ab uchar 32
461text toolong &quot;(longer than 31 bytes)\n&quot;
462fn main to int
463n = argcnt
464putl n
465i = ulong 1
466while lt long i n bound 64
467k = argread i ab 0 31
468if eq k -105
469w = write 1 toolong 0 len toolong
470else
471m = ulong k
472put ab m 10
473outbuf m
474end
475i = add i 1
476end
477ret 0
478end
479note outbuf: write ab[0 .. m] (the argument and its newline); exit 90 on an error
480fn outbuf m ulong to void
481e = add m 1
482p = ulong 0
483while lt p e bound 32
484k = write 1 ab p sub e p
485if le k 0
486exit 90
487end
488p = add p ulong k
489end
490end
491</pre><p class="ref-file">File: <code>examples/echo_args.expect</code></p><pre>
492compile 0
493exit 0
494</pre><p class="ref-file">File: <code>examples/echo_args.args</code></p><pre>
495one
496two words
497
498xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
499</pre><p class="ref-file">File: <code>examples/echo_args.stdout</code></p><pre>
5005
501one
502two words
503
504(longer than 31 bytes)
505</pre></article><article class="ref-ex" id="ex-empty_text_part"><h3>empty_text_part</h3><p class="ref-file">File: <code>examples/empty_text_part.om</code></p><pre>
506note empty_text_part: known defect at the pin (omega-mv &#35;775): an empty text passed to an unsized view
507note does not arrive with len 0. Workaround, shown here: pass part e 0 0, which arrives with len 0
508om2
509text e &quot;&quot;
510text f &quot;0123456789abcdef0123456789abcdef&quot;
511fn n a view uchar * to ulong
512ret len a
513end
514fn main to int
515k = n part e 0 0
516if ne k 0
517ret 1
518end
519m = n f
520if ne m 32
521ret 2
522end
523ret 0
524end
525</pre><p class="ref-file">File: <code>examples/empty_text_part.expect</code></p><pre>
526compile 0
527exit 0
528</pre></article><article class="ref-ex" id="ex-env_read"><h3>env_read</h3><p class="ref-file">File: <code>examples/env_read.om</code></p><pre>
529note env_read: envcnt counts the environment entries; envread copies one KEY=VALUE without a terminator
530note ---- output helpers: a function works on a global by naming it (a global is never an argument)
531global ob uchar 32
532note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
533note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
534fn outb off ulong n ulong to void
535e = add off n
536p = off
537while lt p e bound 32
538k = write 1 ob p sub e p
539if le k 0
540exit 90
541end
542p = add p ulong k
543end
544end
545note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
546fn putu v ulong to void
547i = ulong 31
548put ob 31 10
549n = v
550while true bound 20
551i = sub i 1
552put ob i uchar add rem n 10 48
553n = div n 10
554if eq n 0
555break
556end
557end
558outb i sub 32 i
559end
560note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
561fn putl v long to void
562if lt v 0
563put ob 0 45
564outb 0 1
565putu add ulong neg add v 1 1
566ret
567end
568putu ulong v
569end
570global eb uchar 64
571fn main to int
572n = envcnt
573putl n
574i = ulong 0
575while lt long i n bound 64
576k = envread i eb 0 63
577if lt k 0
578note -105: the entry is longer than the capacity; the buffer is unchanged
579k = long 0
580end
581m = ulong k
582put eb m 10
583e = add m 1
584p = ulong 0
585while lt p e bound 64
586w = write 1 eb p sub e p
587if le w 0
588exit 90
589end
590p = add p ulong w
591end
592i = add i 1
593end
594ret 0
595end
596</pre><p class="ref-file">File: <code>examples/env_read.expect</code></p><pre>
597compile 0
598exit 0
599</pre><p class="ref-file">File: <code>examples/env_read.env</code></p><pre>
600LANG=C
601OMEGA_HOME=/opt/omega
602EMPTY=
603LONG=vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
604</pre><p class="ref-file">File: <code>examples/env_read.stdout</code></p><pre>
6054
606LANG=C
607OMEGA_HOME=/opt/omega
608EMPTY=
609
610</pre></article><article class="ref-ex" id="ex-err01_input"><h3>err01_input</h3><p class="ref-file">File: <code>examples/err01_input.om</code></p><pre>
611note err01_input: error 1, the compiler could not read its input. The gate feeds a directory as stdin; this file is a placeholder valid program
612fn main to int
613ret 0
614end
615</pre><p class="ref-file">File: <code>examples/err01_input.expect</code></p><pre>
616input dir
617compile 1
618stderr omc: line 0: error 1
619</pre></article><article class="ref-ex" id="ex-err02_output"><h3>err02_output</h3><p class="ref-file">File: <code>examples/err02_output.om</code></p><pre>
620note err02_output: error 2, the compiler could not write its output. The gate sends stdout to /dev/full; the program itself is valid
621fn main to int
622ret 0
623end
624</pre><p class="ref-file">File: <code>examples/err02_output.expect</code></p><pre>
625output full
626compile 2
627stderr omc: line 5: error 2
628</pre></article><article class="ref-ex" id="ex-err03_unknown_name"><h3>err03_unknown_name</h3><p class="ref-file">File: <code>examples/err03_unknown_name.om</code></p><pre>
629note err03_unknown_name: a name that was never assigned, or an unknown field. Fix: assign it first, check spelling
630fn main to int
631ret q
632end
633</pre><p class="ref-file">File: <code>examples/err03_unknown_name.expect</code></p><pre>
634compile 3
635stderr omc: line 3: error 3
636</pre></article><article class="ref-ex" id="ex-err04_line_ends"><h3>err04_line_ends</h3><p class="ref-file">File: <code>examples/err04_line_ends.om</code></p><pre>
637note err04_line_ends: the line ends before the statement is complete; here sub needs two values
638fn main to int
639x = int 5
640ret sub x
641end
642</pre><p class="ref-file">File: <code>examples/err04_line_ends.expect</code></p><pre>
643compile 4
644stderr omc: line 5: error 4
645</pre></article><article class="ref-ex" id="ex-err05_extra_words"><h3>err05_extra_words</h3><p class="ref-file">File: <code>examples/err05_extra_words.om</code></p><pre>
646note err05_extra_words: words left over after a complete statement; prefix words have fixed arity
647fn main to int
648x = int 5
649ret x 1
650end
651</pre><p class="ref-file">File: <code>examples/err05_extra_words.expect</code></p><pre>
652compile 5
653stderr omc: line 4: error 5
654</pre></article><article class="ref-ex" id="ex-err06_bad_number"><h3>err06_bad_number</h3><p class="ref-file">File: <code>examples/err06_bad_number.om</code></p><pre>
655note err06_bad_number: a malformed number; outside OM-2 there are no hex literals
656fn main to int
657x = uint 0x10
658ret int x
659end
660</pre><p class="ref-file">File: <code>examples/err06_bad_number.expect</code></p><pre>
661compile 6
662stderr omc: line 3: error 6
663</pre></article><article class="ref-ex" id="ex-err07_type_mismatch"><h3>err07_type_mismatch</h3><p class="ref-file">File: <code>examples/err07_type_mismatch.om</code></p><pre>
664note err07_type_mismatch: no implicit conversions; both values of an op have one type. Fix: add int 1 int u
665fn main to int
666u = uint 2
667x = add int 1 u
668ret x
669end
670</pre><p class="ref-file">File: <code>examples/err07_type_mismatch.expect</code></p><pre>
671compile 7
672stderr omc: line 4: error 7
673</pre></article><article class="ref-ex" id="ex-err08_out_of_range"><h3>err08_out_of_range</h3><p class="ref-file">File: <code>examples/err08_out_of_range.om</code></p><pre>
674note err08_out_of_range: a literal that does not fit its type; int is -2147483648 .. 2147483647
675fn main to int
676x = int 2147483648
677ret 0
678end
679</pre><p class="ref-file">File: <code>examples/err08_out_of_range.expect</code></p><pre>
680compile 8
681stderr omc: line 3: error 8
682</pre></article><article class="ref-ex" id="ex-err09_disallowed"><h3>err09_disallowed</h3><p class="ref-file">File: <code>examples/err09_disallowed.om</code></p><pre>
683note err09_disallowed: a word in a place it is not allowed; new is only in a function's outermost block
684fn main to int
685if true
686a = new uchar 4 0
687end
688ret 0
689end
690</pre><p class="ref-file">File: <code>examples/err09_disallowed.expect</code></p><pre>
691compile 9
692stderr omc: line 4: error 9
693</pre></article><article class="ref-ex" id="ex-err10_duplicate"><h3>err10_duplicate</h3><p class="ref-file">File: <code>examples/err10_duplicate.om</code></p><pre>
694note err10_duplicate: a new name may not repeat a visible name, a function or a struct
695fn f x int x int to int
696ret x
697end
698fn main to int
699ret f 1 2
700end
701</pre><p class="ref-file">File: <code>examples/err10_duplicate.expect</code></p><pre>
702compile 10
703stderr omc: line 2: error 10
704</pre></article><article class="ref-ex" id="ex-err11_ambiguous"><h3>err11_ambiguous</h3><p class="ref-file">File: <code>examples/err11_ambiguous.om</code></p><pre>
705note err11_ambiguous: a bare number with nothing to give it a type. Fix: x = uint 5
706fn main to int
707x = 5
708ret 0
709end
710</pre><p class="ref-file">File: <code>examples/err11_ambiguous.expect</code></p><pre>
711compile 11
712stderr omc: line 3: error 11
713</pre></article><article class="ref-ex" id="ex-err12_registers"><h3>err12_registers</h3><p class="ref-file">File: <code>examples/err12_registers.om</code></p><pre>
714note err12_registers: the 17th live name exhausts the 16-register pool. Fix: fewer live names, smaller functions
715fn main to int
716a1 = int 1
717a2 = int 2
718a3 = int 3
719a4 = int 4
720a5 = int 5
721a6 = int 6
722a7 = int 7
723a8 = int 8
724a9 = int 9
725a10 = int 10
726a11 = int 11
727a12 = int 12
728a13 = int 13
729a14 = int 14
730a15 = int 15
731a16 = int 16
732a17 = int 17
733ret add a1 add a2 add a3 add a4 add a5 add a6 add a7 add a8 add a9 add a10 add a11 add a12 add a13 add a14 add a15 add a16 a17
734end
735</pre><p class="ref-file">File: <code>examples/err12_registers.expect</code></p><pre>
736compile 12
737stderr omc: line 19: error 12
738</pre></article><article class="ref-ex" id="ex-err13_long_name"><h3>err13_long_name</h3><p class="ref-file">File: <code>examples/err13_long_name.om</code></p><pre>
739note err13_long_name: a variable, parameter, global, text, const, struct or field name has at most 8 characters
740fn main to int
741counter1x = int 1
742ret 0
743end
744</pre><p class="ref-file">File: <code>examples/err13_long_name.expect</code></p><pre>
745compile 13
746stderr omc: line 3: error 13
747</pre></article><article class="ref-ex" id="ex-err14_capacity"><h3>err14_capacity</h3><p class="ref-file">File: <code>examples/err14_capacity.om</code></p><pre>
748note err14_capacity: a compiler capacity; here the 33rd export in one OM-2 unit (limit 32). Exports are function mode, so no main
749om2
750export fn e0 x uint to uint
751ret x
752end
753export fn e1 x uint to uint
754ret x
755end
756export fn e2 x uint to uint
757ret x
758end
759export fn e3 x uint to uint
760ret x
761end
762export fn e4 x uint to uint
763ret x
764end
765export fn e5 x uint to uint
766ret x
767end
768export fn e6 x uint to uint
769ret x
770end
771export fn e7 x uint to uint
772ret x
773end
774export fn e8 x uint to uint
775ret x
776end
777export fn e9 x uint to uint
778ret x
779end
780export fn e10 x uint to uint
781ret x
782end
783export fn e11 x uint to uint
784ret x
785end
786export fn e12 x uint to uint
787ret x
788end
789export fn e13 x uint to uint
790ret x
791end
792export fn e14 x uint to uint
793ret x
794end
795export fn e15 x uint to uint
796ret x
797end
798export fn e16 x uint to uint
799ret x
800end
801export fn e17 x uint to uint
802ret x
803end
804export fn e18 x uint to uint
805ret x
806end
807export fn e19 x uint to uint
808ret x
809end
810export fn e20 x uint to uint
811ret x
812end
813export fn e21 x uint to uint
814ret x
815end
816export fn e22 x uint to uint
817ret x
818end
819export fn e23 x uint to uint
820ret x
821end
822export fn e24 x uint to uint
823ret x
824end
825export fn e25 x uint to uint
826ret x
827end
828export fn e26 x uint to uint
829ret x
830end
831export fn e27 x uint to uint
832ret x
833end
834export fn e28 x uint to uint
835ret x
836end
837export fn e29 x uint to uint
838ret x
839end
840export fn e30 x uint to uint
841ret x
842end
843export fn e31 x uint to uint
844ret x
845end
846export fn e32 x uint to uint
847ret x
848end
849</pre><p class="ref-file">File: <code>examples/err14_capacity.expect</code></p><pre>
850compile 14
851stderr omc: line 99: error 14
852</pre></article><article class="ref-ex" id="ex-err15_missing_end"><h3>err15_missing_end</h3><p class="ref-file">File: <code>examples/err15_missing_end.om</code></p><pre>
853note err15_missing_end: the input ends inside an open function or block
854fn main to int
855ret 0
856</pre><p class="ref-file">File: <code>examples/err15_missing_end.expect</code></p><pre>
857compile 15
858stderr omc: line 4: error 15
859</pre></article><article class="ref-ex" id="ex-err16_syntax"><h3>err16_syntax</h3><p class="ref-file">File: <code>examples/err16_syntax.om</code></p><pre>
860note err16_syntax: a line that starts with no statement word; C-style declarations are not Omega
861int main to int
862ret 0
863end
864</pre><p class="ref-file">File: <code>examples/err16_syntax.expect</code></p><pre>
865compile 16
866stderr omc: line 2: error 16
867</pre></article><article class="ref-ex" id="ex-err17_after_path"><h3>err17_after_path</h3><p class="ref-file">File: <code>examples/err17_after_path.om</code></p><pre>
868note err17_after_path: a line after ret, break, continue, trap or exit in the same block can never run
869fn main to int
870ret 0
871ret 1
872end
873</pre><p class="ref-file">File: <code>examples/err17_after_path.expect</code></p><pre>
874compile 17
875stderr omc: line 4: error 17
876</pre></article><article class="ref-ex" id="ex-err18_missing_ret"><h3>err18_missing_ret</h3><p class="ref-file">File: <code>examples/err18_missing_ret.om</code></p><pre>
877note err18_missing_ret: a function with a result type whose end is reachable
878fn f x int to int
879if gt x 0
880ret 1
881end
882end
883fn main to int
884ret f 1
885end
886</pre><p class="ref-file">File: <code>examples/err18_missing_ret.expect</code></p><pre>
887compile 18
888stderr omc: line 7: error 18
889</pre></article><article class="ref-ex" id="ex-err19_global_arg"><h3>err19_global_arg</h3><p class="ref-file">File: <code>examples/err19_global_arg.om</code></p><pre>
890note err19_global_arg: a global is never an argument. Fix: name the global inside the function
891global g uchar 4
892fn f a edit uchar 4 to void
893put a 0 1
894end
895fn main to int
896f g
897ret 0
898end
899</pre><p class="ref-file">File: <code>examples/err19_global_arg.expect</code></p><pre>
900compile 19
901stderr omc: line 7: error 19
902</pre></article><article class="ref-ex" id="ex-err20_readonly"><h3>err20_readonly</h3><p class="ref-file">File: <code>examples/err20_readonly.om</code></p><pre>
903note err20_readonly: a text or a view parameter is read-only
904text t &quot;abc&quot;
905fn main to int
906put t 0 1
907ret 0
908end
909</pre><p class="ref-file">File: <code>examples/err20_readonly.expect</code></p><pre>
910compile 20
911stderr omc: line 4: error 20
912</pre></article><article class="ref-ex" id="ex-err21_const_index"><h3>err21_const_index</h3><p class="ref-file">File: <code>examples/err21_const_index.om</code></p><pre>
913note err21_const_index: a literal index outside the array is refused at compile time (a run-time index traps 3)
914fn main to int
915a = new uint 4 0
916put a 4 1
917ret 0
918end
919</pre><p class="ref-file">File: <code>examples/err21_const_index.expect</code></p><pre>
920compile 21
921stderr omc: line 4: error 21
922</pre></article><article class="ref-ex" id="ex-err22_text_escape"><h3>err22_text_escape</h3><p class="ref-file">File: <code>examples/err22_text_escape.om</code></p><pre>
923note err22_text_escape: a text escape other than backslash n, t, backslash or quote
924text t &quot;a\q&quot;
925fn main to int
926ret 0
927end
928</pre><p class="ref-file">File: <code>examples/err22_text_escape.expect</code></p><pre>
929compile 22
930stderr omc: line 2: error 22
931</pre></article><article class="ref-ex" id="ex-err23_size"><h3>err23_size</h3><p class="ref-file">File: <code>examples/err23_size.om</code></p><pre>
932note err23_size: an array over 16777216 elements, or over 4 MiB of local arrays, or 1 GiB of globals
933fn main to int
934a = new uchar 16777217 0
935ret 0
936end
937</pre><p class="ref-file">File: <code>examples/err23_size.expect</code></p><pre>
938compile 23
939stderr omc: line 3: error 23
940</pre></article><article class="ref-ex" id="ex-err27_fresh"><h3>err27_fresh</h3><p class="ref-file">File: <code>examples/err27_fresh.om</code></p><pre>
941note err27_fresh: OM-2 fresh means every element is written before it is read
942om2
943fn f s edit uint 4 fresh to uint
944ret get s 0
945end
946fn main to int
947a = new uint 4 0
948ret int f a
949end
950</pre><p class="ref-file">File: <code>examples/err27_fresh.expect</code></p><pre>
951compile 27
952stderr omc: line 4: error 27
953</pre></article><article class="ref-ex" id="ex-err28_seal"><h3>err28_seal</h3><p class="ref-file">File: <code>examples/err28_seal.om</code></p><pre>
954note err28_seal: OM-2 seal is allowed only as the whole body of the abstract type's constructor
955om2
956abstract v32 ulong
957holds v32 dup32
958fn main to int
959v = seal v32 ulong 0
960ret 0
961end
962</pre><p class="ref-file">File: <code>examples/err28_seal.expect</code></p><pre>
963compile 28
964stderr omc: line 6: error 28
965</pre></article><article class="ref-ex" id="ex-err29_layout"><h3>err29_layout</h3><p class="ref-file">File: <code>examples/err29_layout.om</code></p><pre>
966note err29_layout: an OM-2 layout field past the layout's size, or overlapping another field
967om2
968layout blk 4
969field lo uint at 2 little
970end
971fn main to int
972ret 0
973end
974</pre><p class="ref-file">File: <code>examples/err29_layout.expect</code></p><pre>
975compile 29
976stderr omc: line 4: error 29
977</pre></article><article class="ref-ex" id="ex-err30_contract"><h3>err30_contract</h3><p class="ref-file">File: <code>examples/err30_contract.om</code></p><pre>
978note err30_contract: a contract clause may not call a function, use floats or host words; result needs a value postcondition
979om2
980fn g x uint to uint
981ret x
982end
983fn f x uint to uint
984requires lt g x 10
985ret x
986end
987fn main to int
988ret int f 1
989end
990</pre><p class="ref-file">File: <code>examples/err30_contract.expect</code></p><pre>
991compile 30
992stderr omc: line 7: error 30
993</pre></article><article class="ref-ex" id="ex-exit_status"><h3>exit_status</h3><p class="ref-file">File: <code>examples/exit_status.om</code></p><pre>
994note exit_status: the exit status is main's value mod 256; 300 mod 256 = 44
995fn main to int
996ret 300
997end
998</pre><p class="ref-file">File: <code>examples/exit_status.expect</code></p><pre>
999compile 0
1000exit 44
1001</pre><p class="ref-file">File: <code>examples/exit_status.stdout</code> (empty file)</p></article><article class="ref-ex" id="ex-float_arith"><h3>float_arith</h3><p class="ref-file">File: <code>examples/float_arith.om</code></p><pre>
1002note float_arith: IEEE binary64, round to nearest even, no float operation traps
1003note ---- output helpers: a function works on a global by naming it (a global is never an argument)
1004global ob uchar 32
1005note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
1006note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
1007fn outb off ulong n ulong to void
1008e = add off n
1009p = off
1010while lt p e bound 32
1011k = write 1 ob p sub e p
1012if le k 0
1013exit 90
1014end
1015p = add p ulong k
1016end
1017end
1018note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
1019fn putu v ulong to void
1020i = ulong 31
1021put ob 31 10
1022n = v
1023while true bound 20
1024i = sub i 1
1025put ob i uchar add rem n 10 48
1026n = div n 10
1027if eq n 0
1028break
1029end
1030end
1031outb i sub 32 i
1032end
1033note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
1034fn putl v long to void
1035if lt v 0
1036put ob 0 45
1037outb 0 1
1038putu add ulong neg add v 1 1
1039ret
1040end
1041putu ulong v
1042end
1043fn main to int
1044r = fsqrt 2.0f64
1045putl long mul r 1000000000.0
1046s = add 0.1f64 0.2
1047putu bitcast ulong s
1048putu bitcast ulong 1.0f64
1049z = f64 0.0
1050n = div z z
1051if ne n n
1052putu 1
1053end
1054if eq n n
1055putu 2
1056end
1057i = div 1.0 z
1058if gt i 1.0e308
1059putu 3
1060end
1061putl long fmin -2.5f64 3.0f64
1062putl long fabs -7.9f64
1063ret 0
1064end
1065</pre><p class="ref-file">File: <code>examples/float_arith.expect</code></p><pre>
1066compile 0
1067exit 0
1068</pre><p class="ref-file">File: <code>examples/float_arith.stdout</code></p><pre>
10691414213562
10704599075939470750516
10714607182418800017408
10721
10733
1074-2
10757
1076</pre></article><article class="ref-ex" id="ex-float_to_int"><h3>float_to_int</h3><p class="ref-file">File: <code>examples/float_to_int.om</code></p><pre>
1077note float_to_int: float to integer truncates toward zero and saturates; NaN becomes 0; no trap
1078note ---- output helpers: a function works on a global by naming it (a global is never an argument)
1079global ob uchar 32
1080note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
1081note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
1082fn outb off ulong n ulong to void
1083e = add off n
1084p = off
1085while lt p e bound 32
1086k = write 1 ob p sub e p
1087if le k 0
1088exit 90
1089end
1090p = add p ulong k
1091end
1092end
1093note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
1094fn putu v ulong to void
1095i = ulong 31
1096put ob 31 10
1097n = v
1098while true bound 20
1099i = sub i 1
1100put ob i uchar add rem n 10 48
1101n = div n 10
1102if eq n 0
1103break
1104end
1105end
1106outb i sub 32 i
1107end
1108note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
1109fn putl v long to void
1110if lt v 0
1111put ob 0 45
1112outb 0 1
1113putu add ulong neg add v 1 1
1114ret
1115end
1116putu ulong v
1117end
1118fn main to int
1119putl long 1.0e30f64
1120putl long -1.0e30f64
1121putl long -5.75f64
1122putu ulong uint -5.5f64
1123z = f64 0.0
1124putl long div z z
1125putu ulong uchar 300.0f64
1126ret 0
1127end
1128</pre><p class="ref-file">File: <code>examples/float_to_int.expect</code></p><pre>
1129compile 0
1130exit 0
1131</pre><p class="ref-file">File: <code>examples/float_to_int.stdout</code></p><pre>
11329223372036854775807
1133-9223372036854775808
1134-5
11350
11360
1137255
1138</pre></article><article class="ref-ex" id="ex-functions"><h3>functions</h3><p class="ref-file">File: <code>examples/functions.om</code></p><pre>
1139note functions: at most 6 parameters; a call may name any function in the unit; recursion in program mode
1140note ---- output helpers: a function works on a global by naming it (a global is never an argument)
1141global ob uchar 32
1142note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
1143note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
1144fn outb off ulong n ulong to void
1145e = add off n
1146p = off
1147while lt p e bound 32
1148k = write 1 ob p sub e p
1149if le k 0
1150exit 90
1151end
1152p = add p ulong k
1153end
1154end
1155note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
1156fn putu v ulong to void
1157i = ulong 31
1158put ob 31 10
1159n = v
1160while true bound 20
1161i = sub i 1
1162put ob i uchar add rem n 10 48
1163n = div n 10
1164if eq n 0
1165break
1166end
1167end
1168outb i sub 32 i
1169end
1170note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
1171fn putl v long to void
1172if lt v 0
1173put ob 0 45
1174outb 0 1
1175putu add ulong neg add v 1 1
1176ret
1177end
1178putu ulong v
1179end
1180fn main to int
1181putu fact 20
1182putu gcd 1071 462
1183putu ulong pick 1 2 3 4 5 6
1184ret 0
1185end
1186fn fact n ulong to ulong
1187if le n 1
1188ret 1
1189end
1190ret mul n fact sub n 1
1191end
1192fn gcd a ulong b ulong to ulong
1193x = a
1194y = b
1195while ne y 0 bound 200
1196t = rem x y
1197x = y
1198y = t
1199end
1200ret x
1201end
1202fn pick a int b int c int d int e int f int to int
1203ret add add a f mul b sub e d
1204end
1205</pre><p class="ref-file">File: <code>examples/functions.expect</code></p><pre>
1206compile 0
1207exit 0
1208</pre><p class="ref-file">File: <code>examples/functions.stdout</code></p><pre>
12092432902008176640000
121021
12119
1212</pre></article><article class="ref-ex" id="ex-globals"><h3>globals</h3><p class="ref-file">File: <code>examples/globals.om</code></p><pre>
1213note globals: zero-filled pools in the program image, visible in every function, named directly
1214note ---- output helpers: a function works on a global by naming it (a global is never an argument)
1215global ob uchar 32
1216note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
1217note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
1218fn outb off ulong n ulong to void
1219e = add off n
1220p = off
1221while lt p e bound 32
1222k = write 1 ob p sub e p
1223if le k 0
1224exit 90
1225end
1226p = add p ulong k
1227end
1228end
1229note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
1230fn putu v ulong to void
1231i = ulong 31
1232put ob 31 10
1233n = v
1234while true bound 20
1235i = sub i 1
1236put ob i uchar add rem n 10 48
1237n = div n 10
1238if eq n 0
1239break
1240end
1241end
1242outb i sub 32 i
1243end
1244note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
1245fn putl v long to void
1246if lt v 0
1247put ob 0 45
1248outb 0 1
1249putu add ulong neg add v 1 1
1250ret
1251end
1252putu ulong v
1253end
1254global hist ulong 10
1255note tally: count the decimal digits of v into hist
1256fn tally v ulong to void
1257n = v
1258while true bound 20
1259d = rem n 10
1260put hist d add get hist d 1
1261n = div n 10
1262if eq n 0
1263break
1264end
1265end
1266end
1267fn main to int
1268tally 1234567890
1269tally 9999
1270tally 0
1271putu get hist 9
1272putu get hist 0
1273putu get hist 5
1274ret 0
1275end
1276</pre><p class="ref-file">File: <code>examples/globals.expect</code></p><pre>
1277compile 0
1278exit 0
1279</pre><p class="ref-file">File: <code>examples/globals.stdout</code></p><pre>
12805
12812
12821
1283</pre></article><article class="ref-ex" id="ex-hello"><h3>hello</h3><p class="ref-file">File: <code>examples/hello.om</code></p><pre>
1284note hello: the smallest complete program. A text is a read-only uchar array
1285text msg &quot;hello, world\n&quot;
1286fn main to int
1287k = write 1 msg 0 len msg
1288if ne k long len msg
1289ret 1
1290end
1291ret 0
1292end
1293</pre><p class="ref-file">File: <code>examples/hello.expect</code></p><pre>
1294compile 0
1295exit 0
1296</pre><p class="ref-file">File: <code>examples/hello.stdout</code></p><pre>
1297hello, world
1298</pre></article><article class="ref-ex" id="ex-int_arith"><h3>int_arith</h3><p class="ref-file">File: <code>examples/int_arith.om</code></p><pre>
1299note int_arith: signed div rounds toward zero, rem takes the dividend's sign, shr keeps the sign
1300note ---- output helpers: a function works on a global by naming it (a global is never an argument)
1301global ob uchar 32
1302note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
1303note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
1304fn outb off ulong n ulong to void
1305e = add off n
1306p = off
1307while lt p e bound 32
1308k = write 1 ob p sub e p
1309if le k 0
1310exit 90
1311end
1312p = add p ulong k
1313end
1314end
1315note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
1316fn putu v ulong to void
1317i = ulong 31
1318put ob 31 10
1319n = v
1320while true bound 20
1321i = sub i 1
1322put ob i uchar add rem n 10 48
1323n = div n 10
1324if eq n 0
1325break
1326end
1327end
1328outb i sub 32 i
1329end
1330note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
1331fn putl v long to void
1332if lt v 0
1333put ob 0 45
1334outb 0 1
1335putu add ulong neg add v 1 1
1336ret
1337end
1338putu ulong v
1339end
1340fn main to int
1341a = long -7
1342b = long 2
1343putl div a b
1344putl rem a b
1345putl div 7 neg b
1346putl rem 7 neg b
1347putl mul 123456789 long 987654321
1348putl shr long -16 2
1349putl sub 0 a
1350ret 0
1351end
1352</pre><p class="ref-file">File: <code>examples/int_arith.expect</code></p><pre>
1353compile 0
1354exit 0
1355</pre><p class="ref-file">File: <code>examples/int_arith.stdout</code></p><pre>
1356-3
1357-1
1358-3
13591
1360121932631112635269
1361-4
13627
1363</pre></article><article class="ref-ex" id="ex-int_bits_and"><h3>int_bits_and</h3><p class="ref-file">File: <code>examples/int_bits_and.om</code></p><pre>
1364note int_bits_and: and, or, xor on integers are bitwise. 12 is 1100, 10 is 1010, so and gives 1000 = 8
1365fn main to int
1366x = ulong 12
1367y = and x ulong 10
1368z = and int 12 int 10
1369if ne int y z
1370ret 1
1371end
1372ret z
1373end
1374</pre><p class="ref-file">File: <code>examples/int_bits_and.expect</code></p><pre>
1375compile 0
1376exit 8
1377</pre></article><article class="ref-ex" id="ex-loops"><h3>loops</h3><p class="ref-file">File: <code>examples/loops.om</code></p><pre>
1378note loops: while COND bound N (1..999999), for i T K, break and continue
1379note ---- output helpers: a function works on a global by naming it (a global is never an argument)
1380global ob uchar 32
1381note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
1382note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
1383fn outb off ulong n ulong to void
1384e = add off n
1385p = off
1386while lt p e bound 32
1387k = write 1 ob p sub e p
1388if le k 0
1389exit 90
1390end
1391p = add p ulong k
1392end
1393end
1394note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
1395fn putu v ulong to void
1396i = ulong 31
1397put ob 31 10
1398n = v
1399while true bound 20
1400i = sub i 1
1401put ob i uchar add rem n 10 48
1402n = div n 10
1403if eq n 0
1404break
1405end
1406end
1407outb i sub 32 i
1408end
1409note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
1410fn putl v long to void
1411if lt v 0
1412put ob 0 45
1413outb 0 1
1414putu add ulong neg add v 1 1
1415ret
1416end
1417putu ulong v
1418end
1419note collatz: the step count to reach 1; the bound is a stated limit, not a proof, so exceeding it traps 4
1420fn collatz n ulong to ulong
1421x = n
1422c = ulong 0
1423while ne x 1 bound 1000
1424c = add c 1
1425if eq rem x 2 0
1426x = div x 2
1427continue
1428end
1429x = add mul x 3 1
1430end
1431ret c
1432end
1433fn main to int
1434putu collatz 27
1435s = ulong 0
1436for i ulong 100
1437if eq rem i 3 0
1438continue
1439end
1440if gt i 50
1441break
1442end
1443s = add s i
1444end
1445putu s
1446ret 0
1447end
1448</pre><p class="ref-file">File: <code>examples/loops.expect</code></p><pre>
1449compile 0
1450exit 0
1451</pre><p class="ref-file">File: <code>examples/loops.stdout</code></p><pre>
1452111
1453867
1454</pre></article><article class="ref-ex" id="ex-mistake_bound_range"><h3>mistake_bound_range</h3><p class="ref-file">File: <code>examples/mistake_bound_range.om</code></p><pre>
1455note mistake_bound_range: a while bound is 1..999999; without bound the limit is 1000000 passes
1456fn main to int
1457i = ulong 0
1458while lt i 3 bound 1000000
1459i = add i 1
1460end
1461ret 0
1462end
1463</pre><p class="ref-file">File: <code>examples/mistake_bound_range.expect</code></p><pre>
1464compile 8
1465stderr omc: line 4: error 8
1466</pre></article><article class="ref-ex" id="ex-mistake_c_call"><h3>mistake_c_call</h3><p class="ref-file">File: <code>examples/mistake_c_call.om</code></p><pre>
1467note mistake_c_call: a call is the function name then its values, no brackets or commas. Fix: ret f 1 2
1468fn f a int b int to int
1469ret add a b
1470end
1471fn main to int
1472ret f(1, 2)
1473end
1474</pre><p class="ref-file">File: <code>examples/mistake_c_call.expect</code></p><pre>
1475compile 6
1476stderr omc: line 6: error 6
1477</pre></article><article class="ref-ex" id="ex-mistake_char_literal"><h3>mistake_char_literal</h3><p class="ref-file">File: <code>examples/mistake_char_literal.om</code></p><pre>
1478note mistake_char_literal: there are no character literals; write the byte value. Fix: x = uchar 65
1479fn main to int
1480x = uchar 'A'
1481ret 0
1482end
1483</pre><p class="ref-file">File: <code>examples/mistake_char_literal.expect</code></p><pre>
1484compile 3
1485stderr omc: line 3: error 3
1486</pre></article><article class="ref-ex" id="ex-mistake_compare_operator"><h3>mistake_compare_operator</h3><p class="ref-file">File: <code>examples/mistake_compare_operator.om</code></p><pre>
1487note mistake_compare_operator: conditions are bool values built with eq ne lt le gt ge; &gt; is not a word. Fix: if gt x 0
1488fn main to int
1489x = int 1
1490if x &gt; 0
1491ret 1
1492end
1493ret 0
1494end
1495</pre><p class="ref-file">File: <code>examples/mistake_compare_operator.expect</code></p><pre>
1496compile 7
1497stderr omc: line 4: error 7
1498</pre></article><article class="ref-ex" id="ex-mistake_else_if"><h3>mistake_else_if</h3><p class="ref-file">File: <code>examples/mistake_else_if.om</code></p><pre>
1499note mistake_else_if: there is no else if on one line; nest an if inside the else block
1500fn main to int
1501x = int 1
1502if eq x 0
1503ret 1
1504else if eq x 1
1505ret 2
1506end
1507ret 0
1508end
1509</pre><p class="ref-file">File: <code>examples/mistake_else_if.expect</code></p><pre>
1510compile 5
1511stderr omc: line 6: error 5
1512</pre></article><article class="ref-ex" id="ex-mistake_for_bound"><h3>mistake_for_bound</h3><p class="ref-file">File: <code>examples/mistake_for_bound.om</code></p><pre>
1513note mistake_for_bound: for takes no bound; its count is the limit. Use while COND bound N for a bounded variable loop
1514fn main to int
1515n = ulong 5
1516s = ulong 0
1517for i ulong n bound 10
1518s = add s i
1519end
1520ret int s
1521end
1522</pre><p class="ref-file">File: <code>examples/mistake_for_bound.expect</code></p><pre>
1523compile 5
1524stderr omc: line 5: error 5
1525</pre></article><article class="ref-ex" id="ex-mistake_free"><h3>mistake_free</h3><p class="ref-file">File: <code>examples/mistake_free.om</code></p><pre>
1526note mistake_free: there is no heap and no free; arrays are new in the outermost block or global
1527fn main to int
1528free = int 1
1529ret 0
1530end
1531</pre><p class="ref-file">File: <code>examples/mistake_free.expect</code></p><pre>
1532compile 9
1533stderr omc: line 3: error 9
1534</pre></article><article class="ref-ex" id="ex-mistake_infix"><h3>mistake_infix</h3><p class="ref-file">File: <code>examples/mistake_infix.om</code></p><pre>
1535note mistake_infix: there are no infix operators; write the prefix word. Fix: y = add x 1
1536fn main to int
1537x = int 1
1538y = x + 1
1539ret y
1540end
1541</pre><p class="ref-file">File: <code>examples/mistake_infix.expect</code></p><pre>
1542compile 5
1543stderr omc: line 4: error 5
1544</pre></article><article class="ref-ex" id="ex-mistake_main_args"><h3>mistake_main_args</h3><p class="ref-file">File: <code>examples/mistake_main_args.om</code></p><pre>
1545note mistake_main_args: main takes no parameters; read arguments with argcnt and argread
1546fn main argc int to int
1547ret 0
1548end
1549</pre><p class="ref-file">File: <code>examples/mistake_main_args.expect</code></p><pre>
1550compile 9
1551stderr omc: line 5: error 9
1552</pre></article><article class="ref-ex" id="ex-mistake_neg_name"><h3>mistake_neg_name</h3><p class="ref-file">File: <code>examples/mistake_neg_name.om</code></p><pre>
1553note mistake_neg_name: neg is the negation word, so it cannot name a value or a text. Fix: pick another name, for example negv
1554fn main to int
1555neg = int 1
1556ret 0
1557end
1558</pre><p class="ref-file">File: <code>examples/mistake_neg_name.expect</code></p><pre>
1559compile 16
1560stderr omc: line 3: error 16
1561</pre></article><article class="ref-ex" id="ex-mistake_new_var_len"><h3>mistake_new_var_len</h3><p class="ref-file">File: <code>examples/mistake_new_var_len.om</code></p><pre>
1562note mistake_new_var_len: a new length is a literal or an integer const, never a variable; there are no run-time-sized arrays
1563fn main to int
1564n = ulong 4
1565a = new uchar n 0
1566ret 0
1567end
1568</pre><p class="ref-file">File: <code>examples/mistake_new_var_len.expect</code></p><pre>
1569compile 16
1570stderr omc: line 4: error 16
1571</pre></article><article class="ref-ex" id="ex-mistake_om1_rotr"><h3>mistake_om1_rotr</h3><p class="ref-file">File: <code>examples/mistake_om1_rotr.om</code></p><pre>
1572note mistake_om1_rotr: rotr and hex need the om2 line; without it rotr is an unknown name
1573fn main to int
1574x = uint 1
1575ret int rotr x 1
1576end
1577</pre><p class="ref-file">File: <code>examples/mistake_om1_rotr.expect</code></p><pre>
1578compile 3
1579stderr omc: line 4: error 3
1580</pre></article><article class="ref-ex" id="ex-mistake_om2_int_hex"><h3>mistake_om2_int_hex</h3><p class="ref-file">File: <code>examples/mistake_om2_int_hex.om</code></p><pre>
1581note mistake_om2_int_hex: a hex literal must fit its type as a value; 0xffffffff is not an int. Fix: int -1
1582om2
1583fn main to int
1584x = int 0xffffffff
1585ret 0
1586end
1587</pre><p class="ref-file">File: <code>examples/mistake_om2_int_hex.expect</code></p><pre>
1588compile 8
1589stderr omc: line 4: error 8
1590</pre></article><article class="ref-ex" id="ex-mistake_om2_param_slots"><h3>mistake_om2_param_slots</h3><p class="ref-file">File: <code>examples/mistake_om2_param_slots.om</code></p><pre>
1591note mistake_om2_param_slots: 2 unsized views plus 3 scalars need 7 slots; the limit is 6. Fix: a sized view, a struct, or fewer scalars
1592om2
1593fn f a view uchar * b view uchar * x int y int z int to int
1594ret add add x y z
1595end
1596fn main to int
1597a = new uchar 4 0
1598ret f a a 1 2 3
1599end
1600</pre><p class="ref-file">File: <code>examples/mistake_om2_param_slots.expect</code></p><pre>
1601compile 9
1602stderr omc: line 3: error 9
1603</pre></article><article class="ref-ex" id="ex-mistake_om2_part_global"><h3>mistake_om2_part_global</h3><p class="ref-file">File: <code>examples/mistake_om2_part_global.om</code></p><pre>
1604note mistake_om2_part_global: part of a global is refused like any global argument; slice a local array
1605om2
1606global g uchar 8
1607fn f a view uchar * to ulong
1608ret len a
1609end
1610fn main to int
1611ret int f part g 0 2
1612end
1613</pre><p class="ref-file">File: <code>examples/mistake_om2_part_global.expect</code></p><pre>
1614compile 19
1615stderr omc: line 8: error 19
1616</pre></article><article class="ref-ex" id="ex-mistake_om2_part_sized"><h3>mistake_om2_part_sized</h3><p class="ref-file">File: <code>examples/mistake_om2_part_sized.om</code></p><pre>
1617note mistake_om2_part_sized: part goes only to an unsized view T * parameter, not a sized one
1618om2
1619fn f a view uchar 2 to ulong
1620ret len a
1621end
1622fn main to int
1623b = new uchar 8 0
1624ret int f part b 0 2
1625end
1626</pre><p class="ref-file">File: <code>examples/mistake_om2_part_sized.expect</code></p><pre>
1627compile 7
1628stderr omc: line 8: error 7
1629</pre></article><article class="ref-ex" id="ex-mistake_om2_part_statement"><h3>mistake_om2_part_statement</h3><p class="ref-file">File: <code>examples/mistake_om2_part_statement.om</code></p><pre>
1630note mistake_om2_part_statement: part A off n is only an argument to an unsized view parameter, never a value of its own
1631om2
1632fn main to int
1633a = new uint 4 0
1634p = part a 0 2
1635ret 0
1636end
1637</pre><p class="ref-file">File: <code>examples/mistake_om2_part_statement.expect</code></p><pre>
1638compile 9
1639stderr omc: line 5: error 9
1640</pre></article><article class="ref-ex" id="ex-mistake_om2_upper_hex"><h3>mistake_om2_upper_hex</h3><p class="ref-file">File: <code>examples/mistake_om2_upper_hex.om</code></p><pre>
1641note mistake_om2_upper_hex: OM-2 hex digits are lowercase after a lowercase 0x
1642om2
1643fn main to int
1644x = uint 0X10
1645ret 0
1646end
1647</pre><p class="ref-file">File: <code>examples/mistake_om2_upper_hex.expect</code></p><pre>
1648compile 6
1649stderr omc: line 4: error 6
1650</pre></article><article class="ref-ex" id="ex-mistake_param_length"><h3>mistake_param_length</h3><p class="ref-file">File: <code>examples/mistake_param_length.om</code></p><pre>
1651note mistake_param_length: a sized array parameter takes exactly that length; use OM-2 view T * for any length
1652fn f a view uchar 4 to uchar
1653ret get a 0
1654end
1655fn main to int
1656b = new uchar 8 0
1657ret int f b
1658end
1659</pre><p class="ref-file">File: <code>examples/mistake_param_length.expect</code></p><pre>
1660compile 7
1661stderr omc: line 7: error 7
1662</pre></article><article class="ref-ex" id="ex-mistake_reserved_name"><h3>mistake_reserved_name</h3><p class="ref-file">File: <code>examples/mistake_reserved_name.om</code></p><pre>
1663note mistake_reserved_name: len, text, read, write, stat and every host word are reserved; pick another name
1664fn main to int
1665len = int 1
1666ret 0
1667end
1668</pre><p class="ref-file">File: <code>examples/mistake_reserved_name.expect</code></p><pre>
1669compile 16
1670stderr omc: line 3: error 16
1671</pre></article><article class="ref-ex" id="ex-mistake_return"><h3>mistake_return</h3><p class="ref-file">File: <code>examples/mistake_return.om</code></p><pre>
1672note mistake_return: the word is ret. The = of an assignment is optional, so return x silently assigns x to a new name return; here the function then has no ret, error 18
1673fn main to int
1674x = int 1
1675return x
1676end
1677</pre><p class="ref-file">File: <code>examples/mistake_return.expect</code></p><pre>
1678compile 18
1679stderr omc: line 6: error 18
1680</pre></article><article class="ref-ex" id="ex-mistake_semicolon"><h3>mistake_semicolon</h3><p class="ref-file">File: <code>examples/mistake_semicolon.om</code></p><pre>
1681note mistake_semicolon: one statement per line and no semicolons; the ; makes the number malformed
1682fn main to int
1683x = int 1;
1684ret 0
1685end
1686</pre><p class="ref-file">File: <code>examples/mistake_semicolon.expect</code></p><pre>
1687compile 6
1688stderr omc: line 3: error 6
1689</pre></article><article class="ref-ex" id="ex-mistake_seven_params"><h3>mistake_seven_params</h3><p class="ref-file">File: <code>examples/mistake_seven_params.om</code></p><pre>
1690note mistake_seven_params: at most 6 parameters; pass a struct or an array for more
1691fn f a int b int c int d int e int g int h int to int
1692ret a
1693end
1694fn main to int
1695ret 0
1696end
1697</pre><p class="ref-file">File: <code>examples/mistake_seven_params.expect</code></p><pre>
1698compile 9
1699stderr omc: line 2: error 9
1700</pre></article><article class="ref-ex" id="ex-mistake_short_circuit"><h3>mistake_short_circuit</h3><p class="ref-file">File: <code>examples/mistake_short_circuit.om</code></p><pre>
1701note mistake_short_circuit: and/or evaluate both sides, so this still loads a[4] and traps 3 (exit 103)
1702note fix: test the bound in an outer if, then load inside it
1703fn main to int
1704a = new uchar 4 0
1705i = ulong 4
1706if and lt i len a eq get a i 0
1707ret 1
1708end
1709ret 0
1710end
1711</pre><p class="ref-file">File: <code>examples/mistake_short_circuit.expect</code></p><pre>
1712compile 0
1713exit 103
1714</pre><p class="ref-file">File: <code>examples/mistake_short_circuit.stdout</code> (empty file)</p></article><article class="ref-ex" id="ex-mistake_slash_comment"><h3>mistake_slash_comment</h3><p class="ref-file">File: <code>examples/mistake_slash_comment.om</code></p><pre>
1715note mistake_slash_comment: note is the only comment form; // and # are unknown words
1716// a comment
1717fn main to int
1718ret 0
1719end
1720</pre><p class="ref-file">File: <code>examples/mistake_slash_comment.expect</code></p><pre>
1721compile 16
1722stderr omc: line 2: error 16
1723</pre></article><article class="ref-ex" id="ex-mistake_struct_after_fn"><h3>mistake_struct_after_fn</h3><p class="ref-file">File: <code>examples/mistake_struct_after_fn.om</code></p><pre>
1724note mistake_struct_after_fn: a struct line comes before the first fn
1725fn main to int
1726ret 0
1727end
1728struct P a uint end
1729</pre><p class="ref-file">File: <code>examples/mistake_struct_after_fn.expect</code></p><pre>
1730compile 9
1731stderr omc: line 5: error 9
1732</pre></article><article class="ref-ex" id="ex-mistake_trap_range"><h3>mistake_trap_range</h3><p class="ref-file">File: <code>examples/mistake_trap_range.om</code></p><pre>
1733note mistake_trap_range: trap takes 1..14; codes 9 and 10 are what contracts use, and the runtime owns 1..7
1734fn main to int
1735trap 15
1736end
1737</pre><p class="ref-file">File: <code>examples/mistake_trap_range.expect</code></p><pre>
1738compile 8
1739stderr omc: line 3: error 8
1740</pre></article><article class="ref-ex" id="ex-mistake_value_stmt"><h3>mistake_value_stmt</h3><p class="ref-file">File: <code>examples/mistake_value_stmt.om</code></p><pre>
1741note mistake_value_stmt: a function with a result cannot be a statement; bind its result. Fix: v = f
1742fn f to int
1743ret 1
1744end
1745fn main to int
1746f
1747ret 0
1748end
1749</pre><p class="ref-file">File: <code>examples/mistake_value_stmt.expect</code></p><pre>
1750compile 7
1751stderr omc: line 6: error 7
1752</pre></article><article class="ref-ex" id="ex-om2_hex_rotr"><h3>om2_hex_rotr</h3><p class="ref-file">File: <code>examples/om2_hex_rotr.om</code></p><pre>
1753note om2_hex_rotr: the first line that is not blank or a note opts in to OM-2: hex literals and rotr
1754om2
1755note ---- OM-2 output helpers: an unsized view parameter takes an array or text of any length
1756note outv: write all of a to stdout; a short write continues, 0 or an error exits 90
1757fn outv a view uchar * to void
1758e = len a
1759p = ulong 0
1760while lt p e bound 65536
1761k = write 1 a p sub e p
1762if le k 0
1763exit 90
1764end
1765p = add p ulong k
1766end
1767end
1768note putu: v in decimal, then a newline
1769note a part of a global is error 19, so the digits go in a local array
1770fn putu v ulong to void
1771b = new uchar 21 0
1772i = ulong 20
1773put b 20 10
1774n = v
1775while true bound 20
1776i = sub i 1
1777put b i uchar add rem n 10 48
1778n = div n 10
1779if eq n 0
1780break
1781end
1782end
1783outv part b i sub 21 i
1784end
1785fn main to int
1786x = uint 0xdeadbeef
1787putu ulong x
1788putu ulong rotr x 8
1789putu 0xffffffffffffffff
1790putu ulong and x 0xff
1791ret 0
1792end
1793</pre><p class="ref-file">File: <code>examples/om2_hex_rotr.expect</code></p><pre>
1794compile 0
1795exit 0
1796</pre><p class="ref-file">File: <code>examples/om2_hex_rotr.stdout</code></p><pre>
17973735928559
17984024348094
179918446744073709551615
1800239
1801</pre></article><article class="ref-ex" id="ex-om2_param_slots"><h3>om2_param_slots</h3><p class="ref-file">File: <code>examples/om2_param_slots.om</code></p><pre>
1802note om2_param_slots: six parameter slots; an unsized view T * or edit T * takes two (pointer, count), every other parameter one
1803note here 2 unsized views (4 slots) plus 2 scalars fill all six; a sized view uchar 4 would take one slot
1804om2
1805fn f a view uchar * b view uchar * x int y int to int
1806ret int add add len a len b ulong add x y
1807end
1808fn g a view uchar 4 b view uchar 4 c view uchar 4 x int y int z int to int
1809ret add add x y z
1810end
1811fn main to int
1812a = new uchar 4 0
1813b = new uchar 3 0
1814c = new uchar 4 0
1815d = new uchar 4 0
1816r = f a b 1 2
1817if ne r 10
1818ret 1
1819end
1820s = g a c d 1 2 3
1821if ne s 6
1822ret 2
1823end
1824ret 0
1825end
1826</pre><p class="ref-file">File: <code>examples/om2_param_slots.expect</code></p><pre>
1827compile 0
1828exit 0
1829</pre></article><article class="ref-ex" id="ex-om2_part_trap"><h3>om2_part_trap</h3><p class="ref-file">File: <code>examples/om2_part_trap.om</code></p><pre>
1830note om2_part_trap: part A off n checks off + n &lt;= len A when it runs; past the end traps 3, exit 103
1831om2
1832text before &quot;before\n&quot;
1833fn sum a view uint * to ulong
1834s = ulong 0
1835for i ulong len a
1836s = add s ulong get a i
1837end
1838ret s
1839end
1840fn main to int
1841k = write 1 before 0 len before
1842a = new uint 4 1
1843o = ulong 3
1844t = sum part a o 2
1845ret 0
1846end
1847</pre><p class="ref-file">File: <code>examples/om2_part_trap.expect</code></p><pre>
1848compile 0
1849exit 103
1850</pre><p class="ref-file">File: <code>examples/om2_part_trap.stdout</code></p><pre>
1851before
1852</pre></article><article class="ref-ex" id="ex-om2_tour"><h3>om2_tour</h3><p class="ref-file">File: <code>examples/om2_tour.om</code></p><pre>
1853note om2_tour: layout, abstract/holds/seal/open, fresh, unroll (after omega-mv examples/om2-tour.om)
1854om2
1855layout blk 8
1856field lo uint at 0 little
1857field hi uint at 4 big
1858end
1859abstract v32 ulong
1860holds v32 dup32
1861note the erasing constructor: the body is exactly this line
1862fn v32mk x uint to v32
1863ret seal v32 or shl ulong x 32 ulong x
1864end
1865note fresh: every element is written before it is read (error 27 otherwise)
1866fn seed s edit uint 8 fresh to uint
1867for i uint 8 unroll
1868put s i mul i 3
1869end
1870ret get s 7
1871end
1872fn stamp c edit blk to void
1873put c lo 0x01020304
1874put c hi 0x05060708
1875end
1876fn main to int
1877s = new uint 8 0
1878v = seed s
1879if ne v 21
1880ret 1
1881end
1882c = new uchar 8 0
1883stamp c
1884if ne get c 0 0x04
1885ret 2
1886end
1887if ne get c 4 0x05
1888ret 3
1889end
1890u = open v32mk 7
1891if ne u 0x0000000700000007
1892ret 4
1893end
1894ret 0
1895end
1896</pre><p class="ref-file">File: <code>examples/om2_tour.expect</code></p><pre>
1897compile 0
1898exit 0
1899</pre><p class="ref-file">File: <code>examples/om2_tour.stdout</code> (empty file)</p></article><article class="ref-ex" id="ex-om2_views"><h3>om2_views</h3><p class="ref-file">File: <code>examples/om2_views.om</code></p><pre>
1900note om2_views: view T * is an unsized read-only parameter; part A off n passes a checked slice
1901om2
1902note ---- OM-2 output helpers: an unsized view parameter takes an array or text of any length
1903note outv: write all of a to stdout; a short write continues, 0 or an error exits 90
1904fn outv a view uchar * to void
1905e = len a
1906p = ulong 0
1907while lt p e bound 65536
1908k = write 1 a p sub e p
1909if le k 0
1910exit 90
1911end
1912p = add p ulong k
1913end
1914end
1915note putu: v in decimal, then a newline
1916note a part of a global is error 19, so the digits go in a local array
1917fn putu v ulong to void
1918b = new uchar 21 0
1919i = ulong 20
1920put b 20 10
1921n = v
1922while true bound 20
1923i = sub i 1
1924put b i uchar add rem n 10 48
1925n = div n 10
1926if eq n 0
1927break
1928end
1929end
1930outv part b i sub 21 i
1931end
1932text greet &quot;views and slices\n&quot;
1933fn sum a view uint * to ulong
1934s = ulong 0
1935for i ulong len a
1936s = add s ulong get a i
1937end
1938ret s
1939end
1940fn main to int
1941outv greet
1942a = new uint 6 0
1943for i ulong 6
1944put a i uint add i 1
1945end
1946putu sum a
1947putu sum part a 1 3
1948putu sum part a 5 1
1949outv part greet 0 6
1950outv part greet 16 1
1951ret 0
1952end
1953</pre><p class="ref-file">File: <code>examples/om2_views.expect</code></p><pre>
1954compile 0
1955exit 0
1956</pre><p class="ref-file">File: <code>examples/om2_views.stdout</code></p><pre>
1957views and slices
195821
19599
19606
1961views
1962</pre></article><article class="ref-ex" id="ex-print_decimal"><h3>print_decimal</h3><p class="ref-file">File: <code>examples/print_decimal.om</code></p><pre>
1963note print_decimal: the output helpers every other example copies
1964note ---- output helpers: a function works on a global by naming it (a global is never an argument)
1965global ob uchar 32
1966note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
1967note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
1968fn outb off ulong n ulong to void
1969e = add off n
1970p = off
1971while lt p e bound 32
1972k = write 1 ob p sub e p
1973if le k 0
1974exit 90
1975end
1976p = add p ulong k
1977end
1978end
1979note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
1980fn putu v ulong to void
1981i = ulong 31
1982put ob 31 10
1983n = v
1984while true bound 20
1985i = sub i 1
1986put ob i uchar add rem n 10 48
1987n = div n 10
1988if eq n 0
1989break
1990end
1991end
1992outb i sub 32 i
1993end
1994note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
1995fn putl v long to void
1996if lt v 0
1997put ob 0 45
1998outb 0 1
1999putu add ulong neg add v 1 1
2000ret
2001end
2002putu ulong v
2003end
2004fn main to int
2005putu 0
2006putu 42
2007putu 18446744073709551615
2008putl 9223372036854775807
2009putl -9223372036854775808
2010putl -1
2011ret 0
2012end
2013</pre><p class="ref-file">File: <code>examples/print_decimal.expect</code></p><pre>
2014compile 0
2015exit 0
2016</pre><p class="ref-file">File: <code>examples/print_decimal.stdout</code></p><pre>
20170
201842
201918446744073709551615
20209223372036854775807
2021-9223372036854775808
2022-1
2023</pre></article><article class="ref-ex" id="ex-stdin_count"><h3>stdin_count</h3><p class="ref-file">File: <code>examples/stdin_count.om</code></p><pre>
2024note stdin_count: read stdin to its end in 7-byte reads, counting bytes and lines
2025note read may return fewer bytes than asked, 0 at the end, or minus the errno: handle all three
2026note ---- output helpers: a function works on a global by naming it (a global is never an argument)
2027global ob uchar 32
2028note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
2029note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
2030fn outb off ulong n ulong to void
2031e = add off n
2032p = off
2033while lt p e bound 32
2034k = write 1 ob p sub e p
2035if le k 0
2036exit 90
2037end
2038p = add p ulong k
2039end
2040end
2041note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
2042fn putu v ulong to void
2043i = ulong 31
2044put ob 31 10
2045n = v
2046while true bound 20
2047i = sub i 1
2048put ob i uchar add rem n 10 48
2049n = div n 10
2050if eq n 0
2051break
2052end
2053end
2054outb i sub 32 i
2055end
2056note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
2057fn putl v long to void
2058if lt v 0
2059put ob 0 45
2060outb 0 1
2061putu add ulong neg add v 1 1
2062ret
2063end
2064putu ulong v
2065end
2066global buf uchar 7
2067fn main to int
2068bytes = ulong 0
2069lines = ulong 0
2070sum = ulong 0
2071note bound: every pass but the last reads at least one byte; this program accepts at most 65536 bytes
2072while true bound 65537
2073k = read 0 buf 0 7
2074if lt k 0
2075exit 91
2076end
2077if eq k 0
2078break
2079end
2080n = ulong k
2081for i ulong n
2082c = get buf i
2083sum = add sum ulong c
2084if eq c 10
2085lines = add lines 1
2086end
2087end
2088bytes = add bytes n
2089end
2090putu bytes
2091putu lines
2092putu sum
2093ret 0
2094end
2095</pre><p class="ref-file">File: <code>examples/stdin_count.expect</code></p><pre>
2096compile 0
2097exit 0
2098</pre><p class="ref-file">File: <code>examples/stdin_count.stdin</code> (no final newline)</p><pre>
2099alpha
2100beta
2101gamma delta
2102
2103end</pre><p class="ref-file">File: <code>examples/stdin_count.stdout</code></p><pre>
210427
21054
21062350
2107</pre></article><article class="ref-ex" id="ex-structs"><h3>structs</h3><p class="ref-file">File: <code>examples/structs.om</code></p><pre>
2108note structs: 8-byte cells in order, no padding; a struct is never a wire layout
2109note a struct line comes before the first fn
2110struct Pair a uint b long end
2111note ---- output helpers: a function works on a global by naming it (a global is never an argument)
2112global ob uchar 32
2113note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
2114note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
2115fn outb off ulong n ulong to void
2116e = add off n
2117p = off
2118while lt p e bound 32
2119k = write 1 ob p sub e p
2120if le k 0
2121exit 90
2122end
2123p = add p ulong k
2124end
2125end
2126note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
2127fn putu v ulong to void
2128i = ulong 31
2129put ob 31 10
2130n = v
2131while true bound 20
2132i = sub i 1
2133put ob i uchar add rem n 10 48
2134n = div n 10
2135if eq n 0
2136break
2137end
2138end
2139outb i sub 32 i
2140end
2141note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
2142fn putl v long to void
2143if lt v 0
2144put ob 0 45
2145outb 0 1
2146putu add ulong neg add v 1 1
2147ret
2148end
2149putu ulong v
2150end
2151note swapsum: an edit parameter may be written; a view parameter may not (error 20)
2152fn swapsum p edit Pair to long
2153t = long get p a
2154put p a uint 7
2155ret add t get p b
2156end
2157fn main to int
2158p = new Pair 5 -12
2159v = swapsum p
2160putl v
2161putu ulong get p a
2162putu sizeof Pair
2163putu offsetof Pair b
2164putu sizeof uint
2165ret 0
2166end
2167</pre><p class="ref-file">File: <code>examples/structs.expect</code></p><pre>
2168compile 0
2169exit 0
2170</pre><p class="ref-file">File: <code>examples/structs.stdout</code></p><pre>
2171-7
21727
217316
21748
21754
2176</pre></article><article class="ref-ex" id="ex-text"><h3>text</h3><p class="ref-file">File: <code>examples/text.om</code></p><pre>
2177note text: escapes are only backslash n, t, backslash and quote; len of a text is its byte count
2178note ---- output helpers: a function works on a global by naming it (a global is never an argument)
2179global ob uchar 32
2180note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
2181note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
2182fn outb off ulong n ulong to void
2183e = add off n
2184p = off
2185while lt p e bound 32
2186k = write 1 ob p sub e p
2187if le k 0
2188exit 90
2189end
2190p = add p ulong k
2191end
2192end
2193note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
2194fn putu v ulong to void
2195i = ulong 31
2196put ob 31 10
2197n = v
2198while true bound 20
2199i = sub i 1
2200put ob i uchar add rem n 10 48
2201n = div n 10
2202if eq n 0
2203break
2204end
2205end
2206outb i sub 32 i
2207end
2208note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
2209fn putl v long to void
2210if lt v 0
2211put ob 0 45
2212outb 0 1
2213putu add ulong neg add v 1 1
2214ret
2215end
2216putu ulong v
2217end
2218text t &quot;tab\there \&quot;q\&quot; back\\slash\n&quot;
2219fn main to int
2220k = write 1 t 0 len t
2221putu len t
2222putu ulong get t 0
2223ret 0
2224end
2225</pre><p class="ref-file">File: <code>examples/text.expect</code></p><pre>
2226compile 0
2227exit 0
2228</pre><p class="ref-file">File: <code>examples/text.stdout</code></p><pre>
2229tab here &quot;q&quot; back\slash
223024
2231116
2232</pre></article><article class="ref-ex" id="ex-tmp_files"><h3>tmp_files</h3><p class="ref-file">File: <code>examples/tmp_files.om</code></p><pre>
2233note tmp_files: make a directory and a file relative to the current directory, read it back, clean up
2234note run it in an empty scratch directory. Linux AArch64 values: O_WRONLY|O_CREAT|O_EXCL = 193,
2235note mode 0600 = 384, 0700 = 448, st_size at byte 48 of the 128-byte stat structure
2236note ---- output helpers: a function works on a global by naming it (a global is never an argument)
2237global ob uchar 32
2238note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
2239note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
2240fn outb off ulong n ulong to void
2241e = add off n
2242p = off
2243while lt p e bound 32
2244k = write 1 ob p sub e p
2245if le k 0
2246exit 90
2247end
2248p = add p ulong k
2249end
2250end
2251note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
2252fn putu v ulong to void
2253i = ulong 31
2254put ob 31 10
2255n = v
2256while true bound 20
2257i = sub i 1
2258put ob i uchar add rem n 10 48
2259n = div n 10
2260if eq n 0
2261break
2262end
2263end
2264outb i sub 32 i
2265end
2266note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
2267fn putl v long to void
2268if lt v 0
2269put ob 0 45
2270outb 0 1
2271putu add ulong neg add v 1 1
2272ret
2273end
2274putu ulong v
2275end
2276text dname &quot;scratch&quot;
2277text fname &quot;scratch/f.txt&quot;
2278text body &quot;omega\n&quot;
2279global rb uchar 16
2280global sb uchar 128
2281fn main to int
2282r = makedir dname 0 len dname 448
2283putl r
2284fd = fopen fname 0 len fname 193 384
2285if lt fd 0
2286exit 93
2287end
2288w = write int fd body 0 len body
2289putl w
2290c = fclose int fd
2291putl c
2292fd2 = fopen fname 0 len fname 0 0
2293if lt fd2 0
2294exit 94
2295end
2296k = read int fd2 rb 0 16
2297putl k
2298c2 = fclose int fd2
2299s = stat fname 0 len fname sb 0 128
2300putl s
2301sz = or uint get sb 48 shl uint get sb 49 8
2302putu ulong sz
2303if gt k 0
2304x = write 1 rb 0 ulong k
2305end
2306u = unlink fname 0 len fname
2307putl u
2308d = rmdir dname 0 len dname
2309putl d
2310gone = fopen fname 0 len fname 0 0
2311putl gone
2312ret 0
2313end
2314</pre><p class="ref-file">File: <code>examples/tmp_files.expect</code></p><pre>
2315compile 0
2316exit 0
2317</pre><p class="ref-file">File: <code>examples/tmp_files.stdout</code></p><pre>
23180
23196
23200
23216
23220
23236
2324omega
23250
23260
2327-2
2328</pre></article><article class="ref-ex" id="ex-trap1_overflow"><h3>trap1_overflow</h3><p class="ref-file">File: <code>examples/trap1_overflow.om</code></p><pre>
2329note trap1_overflow: signed add past the maximum traps 1, exit 101; stdout written before stays
2330text before &quot;before\n&quot;
2331fn main to int
2332k = write 1 before 0 len before
2333x = long 9223372036854775807
2334y = add x 1
2335ret 0
2336end
2337</pre><p class="ref-file">File: <code>examples/trap1_overflow.expect</code></p><pre>
2338compile 0
2339exit 101
2340</pre><p class="ref-file">File: <code>examples/trap1_overflow.stdout</code></p><pre>
2341before
2342</pre></article><article class="ref-ex" id="ex-trap2_div0"><h3>trap2_div0</h3><p class="ref-file">File: <code>examples/trap2_div0.om</code></p><pre>
2343note trap2_div0: integer div or rem by zero traps 2, exit 102
2344text before &quot;before\n&quot;
2345fn main to int
2346k = write 1 before 0 len before
2347d = uint 0
2348q = div uint 10 d
2349ret 0
2350end
2351</pre><p class="ref-file">File: <code>examples/trap2_div0.expect</code></p><pre>
2352compile 0
2353exit 102
2354</pre><p class="ref-file">File: <code>examples/trap2_div0.stdout</code></p><pre>
2355before
2356</pre></article><article class="ref-ex" id="ex-trap3_bounds"><h3>trap3_bounds</h3><p class="ref-file">File: <code>examples/trap3_bounds.om</code></p><pre>
2357note trap3_bounds: a run-time index outside the array traps 3, exit 103
2358text before &quot;before\n&quot;
2359fn main to int
2360k = write 1 before 0 len before
2361a = new uint 4 0
2362i = ulong 4
2363v = get a i
2364ret 0
2365end
2366</pre><p class="ref-file">File: <code>examples/trap3_bounds.expect</code></p><pre>
2367compile 0
2368exit 103
2369</pre><p class="ref-file">File: <code>examples/trap3_bounds.stdout</code></p><pre>
2370before
2371</pre></article><article class="ref-ex" id="ex-trap4_loop"><h3>trap4_loop</h3><p class="ref-file">File: <code>examples/trap4_loop.om</code></p><pre>
2372note trap4_loop: one pass more than the bound traps 4, exit 104
2373text before &quot;before\n&quot;
2374fn main to int
2375k = write 1 before 0 len before
2376i = ulong 0
2377while lt i 10 bound 5
2378i = add i 1
2379end
2380ret 0
2381end
2382</pre><p class="ref-file">File: <code>examples/trap4_loop.expect</code></p><pre>
2383compile 0
2384exit 104
2385</pre><p class="ref-file">File: <code>examples/trap4_loop.stdout</code></p><pre>
2386before
2387</pre></article><article class="ref-ex" id="ex-trap5_conv"><h3>trap5_conv</h3><p class="ref-file">File: <code>examples/trap5_conv.om</code></p><pre>
2388note trap5_conv: a conversion whose value does not fit traps 5, exit 105
2389text before &quot;before\n&quot;
2390fn main to int
2391k = write 1 before 0 len before
2392x = uint 300
2393c = uchar x
2394ret 0
2395end
2396</pre><p class="ref-file">File: <code>examples/trap5_conv.expect</code></p><pre>
2397compile 0
2398exit 105
2399</pre><p class="ref-file">File: <code>examples/trap5_conv.stdout</code></p><pre>
2400before
2401</pre></article><article class="ref-ex" id="ex-trap6_stack"><h3>trap6_stack</h3><p class="ref-file">File: <code>examples/trap6_stack.om</code></p><pre>
2402note trap6_stack: recursion below the 8 MiB stack traps 6, exit 106
2403text before &quot;before\n&quot;
2404fn deep n ulong to ulong
2405if eq n 0
2406ret 0
2407end
2408ret add 1 deep sub n 1
2409end
2410fn main to int
2411k = write 1 before 0 len before
2412x = deep 100000000
2413ret 0
2414end
2415</pre><p class="ref-file">File: <code>examples/trap6_stack.expect</code></p><pre>
2416compile 0
2417exit 106
2418</pre><p class="ref-file">File: <code>examples/trap6_stack.stdout</code></p><pre>
2419before
2420</pre></article><article class="ref-ex" id="ex-trap7_shift"><h3>trap7_shift</h3><p class="ref-file">File: <code>examples/trap7_shift.om</code></p><pre>
2421note trap7_shift: a shift amount outside 0..width-1 traps 7, exit 107
2422text before &quot;before\n&quot;
2423fn main to int
2424k = write 1 before 0 len before
2425s = uint 32
2426x = shl uint 1 s
2427ret 0
2428end
2429</pre><p class="ref-file">File: <code>examples/trap7_shift.expect</code></p><pre>
2430compile 0
2431exit 107
2432</pre><p class="ref-file">File: <code>examples/trap7_shift.stdout</code></p><pre>
2433before
2434</pre></article><article class="ref-ex" id="ex-trap_code"><h3>trap_code</h3><p class="ref-file">File: <code>examples/trap_code.om</code></p><pre>
2435note trap_code: trap N for N in 1..14 exits 100+N; use it for your own fatal codes
2436text before &quot;before\n&quot;
2437fn main to int
2438k = write 1 before 0 len before
2439trap 14
2440end
2441</pre><p class="ref-file">File: <code>examples/trap_code.expect</code></p><pre>
2442compile 0
2443exit 114
2444</pre><p class="ref-file">File: <code>examples/trap_code.stdout</code></p><pre>
2445before
2446</pre></article><article class="ref-ex" id="ex-unsigned_wrap"><h3>unsigned_wrap</h3><p class="ref-file">File: <code>examples/unsigned_wrap.om</code></p><pre>
2447note unsigned_wrap: unsigned add sub mul shl neg not wrap mod 2^width; comparisons follow the type
2448note ---- output helpers: a function works on a global by naming it (a global is never an argument)
2449global ob uchar 32
2450note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
2451note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
2452fn outb off ulong n ulong to void
2453e = add off n
2454p = off
2455while lt p e bound 32
2456k = write 1 ob p sub e p
2457if le k 0
2458exit 90
2459end
2460p = add p ulong k
2461end
2462end
2463note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
2464fn putu v ulong to void
2465i = ulong 31
2466put ob 31 10
2467n = v
2468while true bound 20
2469i = sub i 1
2470put ob i uchar add rem n 10 48
2471n = div n 10
2472if eq n 0
2473break
2474end
2475end
2476outb i sub 32 i
2477end
2478note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
2479fn putl v long to void
2480if lt v 0
2481put ob 0 45
2482outb 0 1
2483putu add ulong neg add v 1 1
2484ret
2485end
2486putu ulong v
2487end
2488fn main to int
2489m = uint 4294967295
2490putu ulong add m 1
2491putu ulong sub uint 0 1
2492putu ulong mul uint 65536 65536
2493putu ulong neg uint 1
2494putu ulong not uint 0
2495putu ulong shl uint 1 31
2496big = ulong 18446744073709551615
2497if lt big 1
2498ret 1
2499end
2500putu add big 1
2501ret 0
2502end
2503</pre><p class="ref-file">File: <code>examples/unsigned_wrap.expect</code></p><pre>
2504compile 0
2505exit 0
2506</pre><p class="ref-file">File: <code>examples/unsigned_wrap.stdout</code></p><pre>
25070
25084294967295
25090
25104294967295
25114294967295
25122147483648
25130
2514</pre></article><article class="ref-ex" id="ex-wire_le"><h3>wire_le</h3><p class="ref-file">File: <code>examples/wire_le.om</code></p><pre>
2515note wire_le: there is no byte-layout type; encode and decode integers with shifts and masks
2516note ---- output helpers: a function works on a global by naming it (a global is never an argument)
2517global ob uchar 32
2518note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
2519note bound: every pass writes at least one byte and n &lt;= 32, so at most 32 passes
2520fn outb off ulong n ulong to void
2521e = add off n
2522p = off
2523while lt p e bound 32
2524k = write 1 ob p sub e p
2525if le k 0
2526exit 90
2527end
2528p = add p ulong k
2529end
2530end
2531note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
2532fn putu v ulong to void
2533i = ulong 31
2534put ob 31 10
2535n = v
2536while true bound 20
2537i = sub i 1
2538put ob i uchar add rem n 10 48
2539n = div n 10
2540if eq n 0
2541break
2542end
2543end
2544outb i sub 32 i
2545end
2546note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
2547fn putl v long to void
2548if lt v 0
2549put ob 0 45
2550outb 0 1
2551putu add ulong neg add v 1 1
2552ret
2553end
2554putu ulong v
2555end
2556fn main to int
2557b = new uchar 4 0
2558h = uint 305419896
2559put b 0 uchar and h 255
2560put b 1 uchar and shr h 8 255
2561put b 2 uchar and shr h 16 255
2562put b 3 uchar and shr h 24 255
2563for i ulong 4
2564putu ulong get b i
2565end
2566v = or uint get b 0 shl uint get b 1 8
2567v = or v shl uint get b 2 16
2568v = or v shl uint get b 3 24
2569putu ulong v
2570note a signed 32-bit decode without integer bitcast (see omega-encoding section 5)
2571s = long v
2572put b 3 255
2573u = or uint get b 0 shl uint get b 1 8
2574u = or u shl uint get b 2 16
2575u = or u shl uint get b 3 24
2576s = long u
2577if ge u 2147483648
2578s = sub s 4294967296
2579end
2580putl s
2581ret 0
2582end
2583</pre><p class="ref-file">File: <code>examples/wire_le.expect</code></p><pre>
2584compile 0
2585exit 0
2586</pre><p class="ref-file">File: <code>examples/wire_le.stdout</code></p><pre>
2587120
258886
258952
259018
2591305419896
2592-13347208
2593</pre></article><article class="ref-ex" id="ex-fnv1a32"><h3>fnv1a32</h3><p class="ref-file">File: <code>skills/omega-encoding/examples/fnv1a32.om</code></p><pre>
2594note fnv1a32: read all of stdin (at most 65535 bytes), write its FNV-1a 32-bit hash as 4 little-endian bytes
2595note exit 0 ok, 2 read error or input of 65536 bytes or more, 3 short or failed write
2596global inb uchar 65536
2597global outb uchar 4
2598fn rdall to long
2599t = ulong 0
2600while lt t len inb bound 65536
2601k = read 0 inb t sub len inb t
2602if lt k 0
2603ret k
2604end
2605if eq k 0
2606ret long t
2607end
2608t = add t ulong k
2609end
2610ret neg long 27
2611end
2612fn fnv n ulong to uint
2613h = uint 2166136261
2614for i ulong n
2615h = mul xor h uint get inb i 16777619
2616end
2617ret h
2618end
2619fn put4 h uint to void
2620put outb 0 uchar and h 255
2621put outb 1 uchar and shr h 8 255
2622put outb 2 uchar and shr h 16 255
2623put outb 3 uchar and shr h 24 255
2624end
2625fn wrall n ulong to long
2626d = ulong 0
2627while lt d n bound 4
2628w = write 1 outb d sub n d
2629if le w 0
2630ret w
2631end
2632d = add d ulong w
2633end
2634ret long d
2635end
2636fn main to int
2637r = rdall
2638if lt r 0
2639ret 2
2640end
2641h = fnv ulong r
2642put4 h
2643w = wrall 4
2644if ne w 4
2645ret 3
2646end
2647ret 0
2648end
2649</pre><p class="ref-file">File: <code>skills/omega-encoding/examples/fnv1a32.expect</code></p><pre>
2650compile 0
2651exit 0
2652</pre><p class="ref-file">File: <code>skills/omega-encoding/examples/fnv1a32.stdin</code> (no final newline)</p><pre>
2653abc</pre><p class="ref-file">File: <code>skills/omega-encoding/examples/fnv1a32.stdout</code> (hex bytes)</p><pre>
26540b e9 47 1a
2655</pre></article><article class="ref-ex" id="ex-trap_narrow"><h3>trap_narrow</h3><p class="ref-file">File: <code>skills/omega-encoding/examples/trap_narrow.om</code></p><pre>
2656note edge: narrowing a value that does not fit traps 5, so the program exits 105
2657note the masked store on line 6 is fine (300 and 255 = 44); the unmasked store on line 7 traps
2658global b uchar 2
2659fn main to int
2660x = uint 300
2661put b 0 uchar and x 255
2662put b 1 uchar x
2663ret int get b 0
2664end
2665</pre><p class="ref-file">File: <code>skills/omega-encoding/examples/trap_narrow.expect</code></p><pre>
2666compile 0
2667exit 105
2668</pre></article></section><section class="om-section ref-gate" id="gate"><h2 class="om-h2">Checking the examples</h2><p>Write every file on this page into one directory tree with the paths in the labels, then run <code>bash examples/gate.sh /path/to/omc</code>. The gate refuses any compiler but the pinned one. It compiles each program, runs it with an empty environment plus its <code>.env</code> in an empty scratch directory under a 20 second timeout, compares the exit status and the stdout bytes, requires an empty stderr, prints one PASS or FAIL line per program and ends with <code>GATE PASS examples=N</code> when all of them match. The aienos.com build checks that the files in llms-full.txt are these files, and <code>om/omega-reference/llms-check.sh</code> extracts them from llms-full.txt and runs this gate on them.</p><p class="ref-file">File: <code>examples/gate.sh</code></p><pre>
2669#!/usr/bin/env bash
2670# gate.sh OMC: compile and run every worked example against its expected result.
2671#
2672# Covers examples/*.om and skills/*/examples/*.om. Each NAME.om needs NAME.expect:
2673# compile N required; the compiler's exit status
2674# stderr LINE required when N is not 0; the compiler's whole stderr is exactly LINE
2675# exit N required when N is 0; the program's exit status
2676# input dir optional; the compiler reads a directory instead of NAME.om (error 1)
2677# output full optional; the compiler writes to /dev/full (error 2)
2678# Optional fixtures: NAME.stdout (exact stdout bytes, absent means empty), NAME.stdin (piped in,
2679# so reads can be partial), NAME.args (one argument per line), NAME.env (one KEY=VALUE per line).
2680# Every program runs with `env -i` plus NAME.env, in an empty scratch directory, under a 20 s
2681# timeout, and must write nothing to stderr. A compile that is not expected to succeed is never run.
2682#
2683# The compiler must match the pinned sha256 below; a different binary is refused, not tolerated.
2684# Exit 0 only when every example passes; 1 on any failure; 2 on a usage or pin error.
2685set -u
2686
2687PIN_REV=e5b32567a7aff3ce594e3254a622a846a6cd190b
2688PIN_SHA=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f
2689
2690if [ &quot;$#&quot; -ne 1 ]; then
2691 echo &quot;usage: $0 /path/to/omc (omega-mv $PIN_REV seed/out/omc)&quot; &gt;&amp;2
2692 exit 2
2693fi
2694omc=$(cd &quot;$(dirname &quot;$1&quot;)&quot; &amp;&amp; pwd)/$(basename &quot;$1&quot;)
2695if [ ! -x &quot;$omc&quot; ]; then
2696 echo &quot;GATE FAIL $1 is not an executable file&quot; &gt;&amp;2
2697 exit 2
2698fi
2699got=$(sha256sum &quot;$omc&quot; | cut -d' ' -f1)
2700if [ &quot;$got&quot; != &quot;$PIN_SHA&quot; ]; then
2701 echo &quot;GATE FAIL omc sha256 $got, pinned $PIN_SHA (omega-mv $PIN_REV)&quot; &gt;&amp;2
2702 exit 2
2703fi
2704
2705here=$(cd &quot;$(dirname &quot;$0&quot;)&quot; &amp;&amp; pwd)
2706root=$(dirname &quot;$here&quot;)
2707work=$(mktemp -d)
2708trap 'rm -rf &quot;$work&quot;' EXIT
2709
2710pass=0
2711fail=0
2712total=0
2713
2714bad() {
2715 echo &quot;FAIL $name: $*&quot;
2716 fail=$((fail + 1))
2717}
2718
2719# Expected files without a program are a silent loss of coverage, so they fail too.
2720for f in &quot;$root&quot;/examples/*.expect &quot;$root&quot;/skills/*/examples/*.expect; do
2721 [ -e &quot;$f&quot; ] || continue
2722 if [ ! -e &quot;${f%.expect}.om&quot; ]; then
2723 name=$(basename &quot;${f%.expect}&quot;)
2724 total=$((total + 1))
2725 bad &quot;expect file without $name.om&quot;
2726 fi
2727done
2728
2729for src in &quot;$root&quot;/examples/*.om &quot;$root&quot;/skills/*/examples/*.om; do
2730 [ -e &quot;$src&quot; ] || continue
2731 base=${src%.om}
2732 name=${base#&quot;$root&quot;/}
2733 total=$((total + 1))
2734 if [ ! -f &quot;$base.expect&quot; ]; then
2735 bad &quot;no $name.expect&quot;
2736 continue
2737 fi
2738 want_compile= want_stderr= want_exit= input=file output=file
2739 while IFS= read -r line || [ -n &quot;$line&quot; ]; do
2740 case $line in
2741 &quot;compile &quot;*) want_compile=${line#compile } ;;
2742 &quot;stderr &quot;*) want_stderr=${line#stderr } ;;
2743 &quot;exit &quot;*) want_exit=${line#exit } ;;
2744 &quot;input dir&quot;) input=dir ;;
2745 &quot;output full&quot;) output=full ;;
2746 &quot;&quot;) ;;
2747 *) want_compile=invalid ;;
2748 esac
2749 done &lt; &quot;$base.expect&quot;
2750 case $want_compile in
2751 ''|invalid|*[!0-9]*) bad &quot;malformed $name.expect&quot;; continue ;;
2752 esac
2753 if [ &quot;$want_compile&quot; != 0 ] &amp;&amp; [ -z &quot;$want_stderr&quot; ]; then
2754 bad &quot;$name.expect has no stderr line&quot;; continue
2755 fi
2756 if [ &quot;$want_compile&quot; = 0 ]; then
2757 case $want_exit in
2758 ''|*[!0-9]*) bad &quot;$name.expect has no exit line&quot;; continue ;;
2759 esac
2760 fi
2761
2762 d=$work/$(echo &quot;$name&quot; | tr / _)
2763 mkdir -p &quot;$d/run&quot;
2764 in=$src
2765 [ &quot;$input&quot; = dir ] &amp;&amp; in=$d/run
2766 out=$d/p
2767 [ &quot;$output&quot; = full ] &amp;&amp; out=/dev/full
2768 &quot;$omc&quot; &lt; &quot;$in&quot; &gt; &quot;$out&quot; 2&gt; &quot;$d/cerr&quot;
2769 rc=$?
2770 if [ &quot;$rc&quot; != &quot;$want_compile&quot; ]; then
2771 bad &quot;compile exit $rc, expected $want_compile: $(cat &quot;$d/cerr&quot;)&quot;
2772 continue
2773 fi
2774 if [ &quot;$want_compile&quot; != 0 ]; then
2775 if printf '%s\n' &quot;$want_stderr&quot; | cmp -s - &quot;$d/cerr&quot;; then
2776 echo &quot;PASS $name (compile $rc: $want_stderr)&quot;
2777 pass=$((pass + 1))
2778 else
2779 bad &quot;compiler stderr '$(cat &quot;$d/cerr&quot;)', expected '$want_stderr'&quot;
2780 fi
2781 continue
2782 fi
2783 if [ -s &quot;$d/cerr&quot; ]; then
2784 bad &quot;compiler stderr not empty: $(cat &quot;$d/cerr&quot;)&quot;
2785 continue
2786 fi
2787
2788 chmod +x &quot;$d/p&quot;
2789 args=()
2790 envs=()
2791 if [ -f &quot;$base.args&quot; ]; then mapfile -t args &lt; &quot;$base.args&quot;; fi
2792 if [ -f &quot;$base.env&quot; ]; then mapfile -t envs &lt; &quot;$base.env&quot;; fi
2793 if [ -f &quot;$base.stdin&quot; ]; then
2794 (cd &quot;$d/run&quot; &amp;&amp; cat &quot;$base.stdin&quot; | timeout 20 env -i &quot;${envs[@]}&quot; &quot;$d/p&quot; &quot;${args[@]}&quot; &gt; &quot;$d/out&quot; 2&gt; &quot;$d/rerr&quot;)
2795 else
2796 (cd &quot;$d/run&quot; &amp;&amp; timeout 20 env -i &quot;${envs[@]}&quot; &quot;$d/p&quot; &quot;${args[@]}&quot; &lt; /dev/null &gt; &quot;$d/out&quot; 2&gt; &quot;$d/rerr&quot;)
2797 fi
2798 rc=$?
2799 want_out=/dev/null
2800 [ -f &quot;$base.stdout&quot; ] &amp;&amp; want_out=$base.stdout
2801 if [ &quot;$rc&quot; != &quot;$want_exit&quot; ]; then
2802 bad &quot;exit $rc, expected $want_exit&quot;
2803 elif ! cmp -s &quot;$want_out&quot; &quot;$d/out&quot;; then
2804 bad &quot;stdout differs from $(basename &quot;$want_out&quot;): $(od -An -c &quot;$d/out&quot; | tr -s ' ' | tr -d '\n' | cut -c1-200)&quot;
2805 elif [ -s &quot;$d/rerr&quot; ]; then
2806 bad &quot;program stderr not empty: $(cat &quot;$d/rerr&quot;)&quot;
2807 else
2808 echo &quot;PASS $name (exit $rc, $(wc -c &lt; &quot;$d/out&quot; | tr -d ' ') stdout bytes)&quot;
2809 pass=$((pass + 1))
2810 fi
2811done
2812
2813if [ &quot;$total&quot; = 0 ]; then
2814 echo &quot;GATE FAIL no examples found under $root&quot;
2815 exit 1
2816fi
2817if [ &quot;$fail&quot; = 0 ] &amp;&amp; [ &quot;$pass&quot; = &quot;$total&quot; ]; then
2818 echo &quot;GATE PASS examples=$total pass=$pass fail=0 omc=$PIN_SHA&quot;
2819 exit 0
2820fi
2821echo &quot;GATE FAIL examples=$total pass=$pass fail=$fail omc=$got&quot;
2822exit 1
2823</pre></section></div></div>