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