Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienos.com/blob/main/om/omega-reference/main.html # aienos.com Source of www.aienos.com, served from my own hardware Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienos.com / om / omega-reference / main.html` 2823 lines ยท 127980 bytes Raw Blame History 1

Omega

Omega language reference

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.

An Omega program writes this page: om/omega-reference/page.om in the aienos.com repository reads the guide skills/writing-omega/SKILL.md and the examples of omega-harness commit `0aba14876fdd8d65cc886a8b5dc0984f16bb4a0e` and prints every file byte for byte. The compiler pin is omega-mv `e5b32567a7aff3ce594e3254a622a846a6cd190b`, omc sha256 `2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f`. The same text is in llms-full.txt.

Writing Omega programs

This guide is for any agent or person writing .om on any machine. It needs only a clone of 2 aien-dev/omega-mv and of this repository. Every rule 3 below was observed with a program at the pin in the table; the programs are in 4 examples/ and examples/gate.sh reruns all of them. 5 For compiler internals, register rules, wire formats and the reasons behind each rule, read 6 skills/omega-encoding/SKILL.md.

PinValue
omega-mv commit`e5b32567a7aff3ce594e3254a622a846a6cd190b`
compiler seed/out/omcsha256 `2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f`
language referencedocs/one_layer/OMEGESE.md at that commit
hostLinux AArch64 only; omc writes a static AArch64 Linux ELF

The language reference is normative. Where this guide and OMEGESE disagree, the compiler at the pin 7 decides; this guide records the known disagreements.

1. Compile and run


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 
  • 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 omc: line N: error K.
  • Never run the output of a refused compile: stdout may still hold bytes.
  • A unit with fn main 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.
  • 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.
  • To check a whole set the way this repository does: 18 examples/gate.sh path/to/seed/out/omc prints PASS name or FAIL name: reason per program and 19 ends GATE PASS examples=N ... or GATE FAIL ... (exit 1); a different compiler hash exits 2.

2. Program shape


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 

(examples/hello.om). The rules it shows:

  • One statement per line. No semicolons, no braces. Blocks (fn, if, else, while, for, 30 struct) close with end. Indentation is optional.
  • Prefix words only. Every operation is a word followed by its values: add x 1, 31 mul add a b c, lt i n. Each word has a fixed number of values. Parentheses may group a value 32 and add nothing: (add a b).
  • Comments are note lines. // and # are refused (error 16).
  • fn NAME p1 T1 p2 T2 ... to R, result R a type or void. Parameters fill 6 slots: an 33 OM-2 unsized view T * / edit T * 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 (examples/om2_param_slots.om, 36 mistake_om2_param_slots.om). fn main to int takes no parameters and its ret value is the exit 37 status (0..255).
  • Assignment is name = value. The first assignment creates the name with the value's type. The 38 = is optional, which is a trap: return x is accepted as "assign x to a new name return".
  • A function may call a function defined later in the unit, and program-mode functions may recurse.
  • Top-level lines are note, fn, text, global, const, struct, and in OM-2 units 39 layout, abstract, holds and export fn. struct and layout lines come before the first 40 fn (error 9 after it). A const is visible only after its line.

Every declaration and statement form at the pin (OMEGESE sections 9 to 12 are the full rules):

FormWhereNotes
note ANY TEXTany linea comment line
om2, om2 debug, om2 release, om2 diag, om2 diag debug, om2 diag releasefirst line that is not blank or noteopts the unit into OM-2 (section 9); release erases contract checks, diag adds a machine-readable stderr record
om2 fmtthe first line, at byte 0prints the canonical text of the rest of the input instead of compiling it
text NAME "bytes"top levela read-only uchar array
global NAME T Ntop level, program modezero-filled array, never an argument
const NAME T LITERALtop levelan integer or float value, visible after its line
struct S f1 T1 f2 T2 ... endtop level, before the first fnone line; a field f T N is an array of 1..64
layout NAME SIZE, field F T [COUNT] at OFF little/big lines, endOM-2, top level, before the first fnan external byte layout of SIZE 1..4096 bytes
abstract NAME T [ops W ...], holds NAME dup32OM-2, top level, before the first fnT uint or ulong; W from rotr rotl and or xor
fn NAME p1 T1 ... to R ... endtop levelinline after R in OM-2; export fn marks an OM-2 function-mode entry
parameter p Tfn linea scalar
parameter a view T N / a edit T Nfn linea sized array, read-only or writable
parameter a view T * / a edit T *fn line, OM-2an array of any length
parameter s edit T N freshfn line, OM-2scratch: every element written before it is read
parameter p view S / p edit Sfn linea struct, or an OM-2 layout
parameter o out T Nfn line, OM-2written by the unit; the calling host copies it out after status 0
name = valuefunction bodythe first assignment creates the name
put A i v, put p f vfunction bodystore into an array element or a field
f v1 v2 ...function bodya call to a void function
if C ... else ... endfunction bodyelse is optional; no else if
while C bound N ... endfunction bodyN 1..999999
for i T K ... endfunction bodyunroll after K in OM-2, K 1..64
break, continueinside a loop
ret, ret vfunction bodyret v in a function with a result
exit N, trap Nfunction bodyexit status N; trap N 1..14 exits 100+N
requires C, ensures COM-2, the first lines of a functionresult names the returned value
invariant COM-2, the first line of a loop body

3. Types and values

TypeMeaning
booltrue / false, the result of comparisons
uchar, ushort, uint, ulongunsigned 8, 16, 32, 64 bit (also spelled u8 u16 u32 u64)
byte, short, int, longsigned 8, 16, 32, 64 bit (also spelled i8 i16 i32 i64); byte is signed, and char is not a type (error 3)
f32, f64IEEE binary32, binary64
voidno result (function result only)
  • No implicit conversion. Both values of an operation have one type (error 7). Convert with the 41 type word: long x, uint b, f64 n.
  • A bare number takes its type from context: the other value of its operation, the parameter it 42 is passed to, the result type at ret. With no context it is error 11: write x = uint 5, never 43 x = 5 for a new name.
  • Integer literals are decimal with an optional leading -. A literal that does not fit its type 44 is error 8. No character literals: write uchar 65, not 'A' (error 3).
  • Float literals need digits on both sides of the dot: 2.0f64, 1.`0e30f64`, 0.5f32. The suffix is 45 optional where context gives the type.
  • Hex only in OM-2 (section 9): lowercase 0x and lowercase digits, value must fit 46 (int 0xffffffff is error 8; write int -1).
  • sizeof T, sizeof S and offsetof S f are compile-time ulong constants.

4. Words

KindWords
arithmeticadd sub mul div rem neg
bitsand or xor not shl shr (rotr rotl in OM-2)
compare (give bool)eq ne lt le gt ge
floatadd sub mul div neg, fsqrt fabs fmin fmax
arraysnew, get A i, put A i v, len A
structsnew S v1 v2 ..., get p f, put p f v
controlif / else / end, while COND bound N, for i T K, break, continue, ret, exit, trap N
conversionthe type words, bitcast T v
I/Oread fd A off n, write fd A off n, pread / pwrite fd A off n pos, and the 67 host words of section 10
  • and, or, xor on integers of any width are bitwise, not logical: and ulong 12 ulong 10 is 8 47 (examples/int_bits_and.om). Compare first when you want a bool.
  • and / or on bools evaluate both sides; there is no short circuit 48 (examples/mistake_short_circuit.om traps 3 on the 49 index the C reader would expect to be skipped). Test the index in an outer if.
  • Comparisons follow the type: on ulong, lt x y with x = 2^64-1 and y = 1 is false.
  • div and rem on signed values round toward zero; rem takes the dividend's sign; 50 shr on a signed value keeps the sign.
  • A function with a result is a value, not a statement: write v = f 1, never a bare f 1 51 (error 7). A void function is called as a statement.
  • A call is the name then its values with no brackets or commas: ret f 1 2.

5. Traps: what stops a running program

A trap ends the process with exit status 100 + N. Output already written stays written.

CauseTrapExitExample
signed overflow in add sub mul neg1101examples/trap1_overflow.om
div or rem by zero, and signed minimum divided by -12102trap2_divzero.om
array index or I/O slice out of bounds3103trap3_bounds.om
a loop past its bound4104trap4_loop.om
an integer conversion whose value does not fit5105trap5_conv.om
stack exhausted (8 MiB) by calls or local arrays6106trap6_stack.om
shift or rotate amount outside 0..width-17107trap7_shift.om
a contract check that fails (OM-2)9, 10109, 110contract_requires.om, contract_ensures.om
trap N, N 1..14N100+Ntrap_code.om (trap 14, exit 114)
  • Unsigned arithmetic wraps modulo 2^width and never traps 52 (examples/unsigned_wrap.om).
  • Integer conversions are checked: uchar x with x = 300 traps 5. Keep the low bits on purpose with 53 a mask: uchar and x 255.
  • Float to integer truncates toward zero and saturates at the type's limits; NaN gives 0; it never 54 traps (examples/float_to_int.om). No float operation traps: 55 1/0 is infinity, 0/0 is NaN, and NaN compares false except with ne.
  • exit N ends the process with status N directly, without the trap convention.

6. Loops and bounds

  • while COND bound N runs the body at most N times, N 1..999999 (error 8 outside). Without 56 bound the limit is `1000000`. One more pass traps 4. Choose N from an argument you can write down 57 in a note, for example "every pass consumes at least one byte of a 65536-byte input".
  • for i T K runs i from 0 to K-1. for takes no bound (error 5); K is the limit.
  • break and continue work in both loops. There is no else if: nest an if inside else.
  • No loop runs forever and recursion is bounded by the stack.

7. Arrays, texts, globals, structs

  • Local array: a = new T N v, N elements each v, only in a function's outermost block (error 9 58 inside a block). N is a literal or an integer const; a variable N is error 16. There is no heap 59 and no free (error 9).
  • Global array: global NAME T N at top level, zero-filled, program mode only. A global is 60 never an argument (error 19); a function uses it by naming it.
  • Text: text NAME "bytes" is a read-only uchar array. Escapes are only \n \t \\ \" 61 (error 22 for others).
  • Array parameters state type and exact length: a view uchar 64 (read-only) or 62 a edit uchar 64 (writable). Another length is error 7. In OM-2, a view T * / a edit T * take 63 any length. Writing to a view or text is error 20.
  • get A i / put A i v: 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.
  • Structs: struct Pair a uint b long end, before the first fn. Every field is an 8-byte cell, 65 so a struct is never a wire or file layout. p = new Pair 5 -12, pass as p edit Pair or 66 p view Pair (examples/structs.om).
  • Bytes on the wire: build them with shifts and masks into a uchar array 67 (examples/wire_le.om); OM-2 also has layout.

8. Names and limits

  • 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.
  • 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 end; assign it before the block to keep it.
  • Reserved words cannot be names, including `len read write text new free view edit global note 70 exit const bound and every host word (stat, link, send, random, fork, ...). len` as a 71 name is error 16, free is error 9.
  • 16 registers per function hold parameters, live names, loop counters and the temporaries of the 72 current statement. The 17th is error 12 (examples/err12_registers.om). 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.
  • Size limits: an array up to `16777216` elements, 4 MiB of local arrays per function, 1 GiB of 76 globals (error 23).

9. OM-2 units

A unit whose first line that is not blank or note is exactly om2 is an OM-2 unit. OM-1 code keeps 77 its meaning inside it. OM-2 adds:

  • lowercase hex literals; rotr x n, rotl x n on unsigned types (trap 7 for an amount outside 78 0..width-1);
  • unsized parameters a view T * / a edit T * with len a the run-time count;
  • part A off n, 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;
  • for i T K unroll, fn ... inline, layout (external byte layouts), fresh scratch arrays 81 (error 27), abstract types with seal / open (error 28), export fn (at most 32, error 14);
  • executable contracts: requires and ensures lines at the start of a function, invariant as the 82 first line of a loop body. A failing requires traps 9 (exit 109), a failing ensures or 83 invariant traps 10 (exit 110). A clause may not call a function, use floats or host words 84 (error 30). The first line om2 release erases the checks 85 (examples/contract_release.om).

Examples: om2_tour.om, om2_views.om, 86 om2_hex_rotr.om, contracts_ok.om.

10. Host I/O

  • Every host word returns a long: a count, or minus the Linux errno. read may return fewer bytes 87 than asked, 0 at end of input, or a negative error; write may write fewer. Loop until done and 88 handle k < 0 and k == 0 separately 89 (examples/stdin_count.om).
  • Descriptors, flags and modes are int; offsets, counts and capacities ulong; buffers uchar 90 arrays. Slices are checked before the kernel call (trap 3).
  • main takes no parameters: read arguments with argcnt and argread 91 (examples/echo_args.om) and the environment with envcnt and 92 envread (examples/env_read.om). Short capacity returns -105.
  • Host words carry the process's own permissions; they are not a sandbox.

The 67 host words at the pin, with their values (OMEGESE section 10 has each one's exact contract). 93 A and B are uchar arrays, off, n and capacity are ulong, and descriptors, flags, modes 94 and ids are int. A path is the slice A off n; a packed list is a u16 count, then u16-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.

AreaWords
filesfopen A off n flags mode, fclose fd, fsync fd, fdsync fd, syncfs fd, memfd A off n flags, fseek fd offset whence, ftrunc fd length, flock fd op, fstat fd A off n, getdnts fd A off n
pathschmod A off n mode, fchmod fd mode, chown A off n uid gid, faccess A off n mode, truncate A off n length, unlink A off n, makedir A off n mode, rmdir A off n, cwd A off capacity, chdir A off n, fchdir fd, readlnk A off n B off capacity, stat A off n B off capacity, lstat A off n B off capacity, renames A off n flags, link A off n, symlnk A off n, umask mask
processesgetpid, getppid, getuid, geteuid, getgid, getegid, setsid, getpgid pid, getsid pid, setpgid pid pgid, fork, killpid pid signal, waitpid pid A off n options, pipefds A off n flags, dupfd fd target flags, execprog A off n
arguments, environment, randomnessargcnt, argread index A off capacity, envcnt, envread index A off capacity, random A off n flags
timemonons, realns, sleepns interval
socketssocknew domain kind protocol, bind fd A off n, connect fd A off n, listen fd backlog, accept fd flags, sockname fd A off n, sockopt fd level option A off n, recv fd A off n flags, send fd A off n flags, sendmsg fd A off n flags, shutdown fd how, waitio fd events interval, pollfds A off n interval, peercred fd A off capacity

11. Compiler errors

Exit status = error number. The only message is the one stderr line omc: line N: error K, where N 97 is the line being read (0 when no line was read). One refusal per code is in examples/errNN_*.om 98 with its exact stderr line in the matching .expect, except 24, which needs a generated program.

CodeMeaningExampleFix
1omc could not read its inputerr01_input (stdin is a directory)give omc a readable file on stdin
2omc could not write its outputerr02_output (stdout is /dev/full)send stdout to a writable file with space
3unknown name, field or functionerr03_unknown_nameassign the name before use; check the spelling
4the line ends before the statement is completeerr04_line_endsgive every word all of its values
5words left after a complete statementerr05_extra_wordscount each word's values; there is no infix
6malformed numbererr06_bad_numberdecimal digits; hex only in OM-2, lowercase
7type mismatcherr07_type_mismatchconvert with a type word: add int 1 int u
8number out of rangeerr08_out_of_rangea literal that fits its type; a bound 1..999999; a trap code 1..14
9a word in a place where it is not allowederr09_disallowedmove it: new only in a function's outermost block, struct before the first fn
10duplicate nameerr10_duplicatechoose a name that is not visible, not a function, not a struct
11a number with nothing to give it a typeerr11_ambiguousx = uint 5
12register pool exhausted (16 per function)err12_registersfewer live names, a helper function, shorter expressions (section 8)
13name too longerr13_long_nameat most 8 characters (function names 63)
14compiler capacity: names, functions, exports, expansionerr14_capacitysplit the function or the unit
15the input ends inside an open blockerr15_missing_endclose every block with end
16syntax: unknown word or misplaced formerr16_syntaxstart each line with a statement word; comments are note lines
17a line after ret, break, continue, trap or exit in the same blockerr17_after_pathdelete the line or move it above
18a function with a result whose end is reachableerr18_missing_retend every path with ret v
19a global as an argument, or one array twice in a callerr19_global_argname the global inside the function; pass distinct arrays
20writing to a text or a viewerr20_readonlyan edit parameter, or a local copy
21a literal index outside the arrayerr21_const_indexan index 0..N-1
22a text escape other than \n \t \\ \"err22_text_escapebuild other bytes with put
23array or pool size limiterr23_sizesmaller arrays (section 8 limits)
24a conditional branch farther than 1 MiB of codegenerated, belowmove long bodies into functions
25, 26never emitted at the pinnonenone
27OM-2 fresh element read before it is writtenerr27_freshput every element before the get
28OM-2 seal outside the constructorerr28_sealseal only as the whole body of fn NAMEmk ... to NAME
29OM-2 layout field past the size or overlappingerr29_layoutkeep every field inside SIZE and apart
30OM-2 contract clause not allowederr30_contractinteger words on parameters only (and result in ensures); no calls, floats or host words

How the emitted set was established at the pin: in seed/omc/omc.om, fn fail (line 293) writes 99 the stderr line and exits with the code (exit int code, line 322). The only other exits are 100 exit int 0 and exit 0, so every refusal goes through fail. Its callers pass the literals 1..24 101 and 27..30, or forward a literal 3, 4, 6, 8, 13 or 16 through lenval, nfail and unkfail. 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.

Error 24 needs more than 1 MiB of code inside one loop. This program, with 270000 copies of the 104 line x = add x 1 (4 bytes of code each), was refused with omc: line 270006: error 24, the line 105 of the loop's end; with 140000 copies it compiled:


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 

12. Common mistakes

Each row is a program in examples/ with its observed refusal.

MistakeWrite insteadRefusalExample
y = x + 1y = add x 1error 5mistake_infix.om
f(1, 2)f 1 2error 6mistake_c_call.om
if x > 0if gt x 0error 7mistake_compare_operator.om
'A'uchar 65error 3mistake_char_literal.om
// commentnote commenterror 16mistake_slash_comment.om
x = 1;x = uint 1error 6mistake_semicolon.om
return xret xerror 18 (silent assignment)mistake_return.om
else if celse, then a nested if cerror 5mistake_else_if.om
fn main argc int to intargcnt, argreaderror 9mistake_main_args.om
for i ulong n bound 10while lt i n bound 10error 5mistake_for_bound.om
while c bound `1000000`a bound 1..999999error 8mistake_bound_range.om
a = new uchar n 0a literal or const lengtherror 16mistake_new_var_len.om
free anothing: no heaperror 9mistake_free.om
len = ...another nameerror 16mistake_reserved_name.om
neg = ... or text neg "a"another name: neg is the negation worderror 16mistake_neg_name.om
a bare call f 1 with a resultv = f 1error 7mistake_value_stmt.om
a 7th parametera struct or an arrayerror 9mistake_seven_params.om
2 view T * plus 3 scalars (7 slots)a sized view, a struct, or fewer scalarserror 9mistake_om2_param_slots.om
emit buf with buf a globalname the global inside the functionerror 19err19_global_arg.om
a view uchar 8 given a 16-element arraythe exact length, or OM-2 view uchar *error 7mistake_param_length.om
trap 15trap 1 .. trap 14error 8mistake_trap_range.om
struct after a fnevery struct before the first fnerror 9mistake_struct_after_fn.om
rotr or hex without om2an om2 first lineerror 3mistake_om1_rotr.om
0X10 or 0xFF0x10, 0xfferror 6mistake_om2_upper_hex.om
int 0xffffffffint -1error 8mistake_om2_int_hex.om
part of a globalpart of a localerror 19mistake_om2_part_global.om
part to a sized parameteran unsized view T * parametererror 7mistake_om2_part_sized.om
s = part a 0 2part only as an argumenterror 9mistake_om2_part_statement.om
if and lt i n eq get a i 0 (C short circuit)an outer if lt i ntrap 3, exit 103mistake_short_circuit.om

13. Known compiler defects at the pin

  • bitcast across signedness below 64 bits (fix proposed in draft omega-mv #896, not in the 111 pin): the result does not compare equal to the reinterpreted value. Observed wrong: uint to 112 int (`4294967295` is not -1, and adding 2 traps 1), int to uint, ushort to and from 113 short, uchar to and from byte, and f32 to int (-1.0 is not -`1082130432` and not below 0). 114 Observed correct: ulong to and from long, f64 to long, f32 to uint, int to f32. 115 Until a fixed compiler is pinned, do not bitcast between narrow integer types or from f32 to 116 int; decode signed 8, 16 and 32-bit values arithmetically: load into long, then subtract 2^32 117 (or 2^16, 2^8) when the value is at least 2^31 (2^15, 2^7), as 118 examples/wire_le.om does. For float bits, bitcast uint of an 119 f32 is correct.
  • Empty text through view uchar * (omega-mv issue #775): the parameter does not arrive with 120 len 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 part e 0 0, which 122 arrives with len 0 123 (examples/empty_text_part.om).
  • Function names that begin with an 8-letter keyword: a function name longer than 8 bytes whose 124 first 8 bytes are a keyword (continue_x, truncate_number, sockname_x, execprog_x, 125 peercred_x) is error 16, although function names may be 63 characters. Shorter keyword prefixes 126 are fine (breakx, whilex, getpid_x). Workaround: rename (cont_x, trunc_num) 127 (examples/defect_keyword_prefix.om).
  • A fix for both is in progress in omega-mv; until a new compiler is pinned, use the workarounds.
  • OMEGESE 9.9 lists + - * / for floats; the compiler refuses them. Write add sub mul div.

14. Example index

All in examples/; each NAME.om has NAME.expect (compile status, exit status 128 or stderr line) and, when it prints, NAME.stdout with the exact bytes. Wolfram checks of the 129 arithmetic expectations are in examples/WOLFRAM.md.

  • Start here: hello, exit_status, print_decimal, functions, loops, arrays, text.
  • Arithmetic: int_arith, int_bits_and, unsigned_wrap, checked_conv, float_arith, 130 float_to_int, consts.
  • Data: globals, structs, wire_le.
  • I/O: stdin_count, echo_args, env_read, tmp_files.
  • Traps: trap1_overflow .. trap7_shift, trap_code.
  • OM-2 and contracts: om2_tour, om2_views, om2_hex_rotr, om2_part_trap, om2_param_slots, 131 contracts_ok, contract_requires, contract_ensures, contract_invariant, contract_release.
  • Known defects and their workarounds: empty_text_part, defect_keyword_prefix.
  • Refusals: err01_input .. err30_contract, mistake_*.

Worked examples

98 programs from examples/ and skills/omega-encoding/examples/, each listed with its files. NAME.om is the program. NAME.expect is what the gate checks: compile N is the compiler's exit status, stderr LINE its whole stderr when N is not 0, exit N the program's exit status, input dir and output full the two refusals that need a special compiler input or output. NAME.stdout is the exact standard output, and an absent one means empty. NAME.stdin, NAME.args (one argument per line) and NAME.env (one KEY=VALUE per line) are what the program is given.

Each file is labelled File: PATH 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.

arrays

File: examples/arrays.om


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 

File: examples/arrays.expect


188 compile 0

189 exit 0

190 

File: examples/arrays.stdout


191 204

192 8

193 64

194 

checked_conv

File: examples/checked_conv.om


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 

File: examples/checked_conv.expect


247 compile 0

248 exit 0

249 

File: examples/checked_conv.stdout


250 44

251 `2147483647`

252 -1

253 65535

254 

consts

File: examples/consts.om


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 

File: examples/consts.expect


314 compile 0

315 exit 0

316 

File: examples/consts.stdout


317 16

318 

contract_ensures

File: examples/contract_ensures.om


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 

File: examples/contract_ensures.expect


333 compile 0

334 exit 110

335 

File: examples/contract_ensures.stdout


336 before

337 

contract_invariant

File: examples/contract_invariant.om


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 

File: examples/contract_invariant.expect


351 compile 0

352 exit 110

353 

File: examples/contract_invariant.stdout


354 before

355 

contract_release

File: examples/contract_release.om


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 

File: examples/contract_release.expect


366 compile 0

367 exit 3

368 

File: examples/contract_release.stdout (empty file)

contract_requires

File: examples/contract_requires.om


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 

File: examples/contract_requires.expect


382 compile 0

383 exit 109

384 

File: examples/contract_requires.stdout


385 before

386 

contracts_ok

File: examples/contracts_ok.om


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 

File: examples/contracts_ok.expect


403 compile 0

404 exit 0

405 

File: examples/contracts_ok.stdout (empty file)

defect_keyword_prefix

File: examples/defect_keyword_prefix.om


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 

File: examples/defect_keyword_prefix.expect


416 compile 16

417 stderr omc: line 4: error 16

418 

echo_args

File: examples/echo_args.om


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 

File: examples/echo_args.expect


492 compile 0

493 exit 0

494 

File: examples/echo_args.args


495 one

496 two words

497

498 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

499 

File: examples/echo_args.stdout


500 5

501 one

502 two words

503

504 (longer than 31 bytes)

505 

empty_text_part

File: examples/empty_text_part.om


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 

File: examples/empty_text_part.expect


526 compile 0

527 exit 0

528 

env_read

File: examples/env_read.om


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 

File: examples/env_read.expect


597 compile 0

598 exit 0

599 

File: examples/env_read.env


600 LANG=C

601 OMEGA_HOME=/opt/omega

602 EMPTY=

603 LONG=vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

604 

File: examples/env_read.stdout


605 4

606 LANG=C

607 OMEGA_HOME=/opt/omega

608 EMPTY=

609

610 

err01_input

File: examples/err01_input.om


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 

File: examples/err01_input.expect


616 input dir

617 compile 1

618 stderr omc: line 0: error 1

619 

err02_output

File: examples/err02_output.om


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 

File: examples/err02_output.expect


625 output full

626 compile 2

627 stderr omc: line 5: error 2

628 

err03_unknown_name

File: examples/err03_unknown_name.om


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 

File: examples/err03_unknown_name.expect


634 compile 3

635 stderr omc: line 3: error 3

636 

err04_line_ends

File: examples/err04_line_ends.om


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 

File: examples/err04_line_ends.expect


643 compile 4

644 stderr omc: line 5: error 4

645 

err05_extra_words

File: examples/err05_extra_words.om


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 

File: examples/err05_extra_words.expect


652 compile 5

653 stderr omc: line 4: error 5

654 

err06_bad_number

File: examples/err06_bad_number.om


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 

File: examples/err06_bad_number.expect


661 compile 6

662 stderr omc: line 3: error 6

663 

err07_type_mismatch

File: examples/err07_type_mismatch.om


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 

File: examples/err07_type_mismatch.expect


671 compile 7

672 stderr omc: line 4: error 7

673 

err08_out_of_range

File: examples/err08_out_of_range.om


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 

File: examples/err08_out_of_range.expect


680 compile 8

681 stderr omc: line 3: error 8

682 

err09_disallowed

File: examples/err09_disallowed.om


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 

File: examples/err09_disallowed.expect


691 compile 9

692 stderr omc: line 4: error 9

693 

err10_duplicate

File: examples/err10_duplicate.om


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 

File: examples/err10_duplicate.expect


702 compile 10

703 stderr omc: line 2: error 10

704 

err11_ambiguous

File: examples/err11_ambiguous.om


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 

File: examples/err11_ambiguous.expect


711 compile 11

712 stderr omc: line 3: error 11

713 

err12_registers

File: examples/err12_registers.om


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 

File: examples/err12_registers.expect


736 compile 12

737 stderr omc: line 19: error 12

738 

err13_long_name

File: examples/err13_long_name.om


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 

File: examples/err13_long_name.expect


745 compile 13

746 stderr omc: line 3: error 13

747 

err14_capacity

File: examples/err14_capacity.om


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 

File: examples/err14_capacity.expect


850 compile 14

851 stderr omc: line 99: error 14

852 

err15_missing_end

File: examples/err15_missing_end.om


853 note err15_missing_end: the input ends inside an open function or block

854 fn main to int

855 ret 0

856 

File: examples/err15_missing_end.expect


857 compile 15

858 stderr omc: line 4: error 15

859 

err16_syntax

File: examples/err16_syntax.om


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 

File: examples/err16_syntax.expect


865 compile 16

866 stderr omc: line 2: error 16

867 

err17_after_path

File: examples/err17_after_path.om


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 

File: examples/err17_after_path.expect


874 compile 17

875 stderr omc: line 4: error 17

876 

err18_missing_ret

File: examples/err18_missing_ret.om


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 

File: examples/err18_missing_ret.expect


887 compile 18

888 stderr omc: line 7: error 18

889 

err19_global_arg

File: examples/err19_global_arg.om


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 

File: examples/err19_global_arg.expect


900 compile 19

901 stderr omc: line 7: error 19

902 

err20_readonly

File: examples/err20_readonly.om


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 

File: examples/err20_readonly.expect


910 compile 20

911 stderr omc: line 4: error 20

912 

err21_const_index

File: examples/err21_const_index.om


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 

File: examples/err21_const_index.expect


920 compile 21

921 stderr omc: line 4: error 21

922 

err22_text_escape

File: examples/err22_text_escape.om


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 

File: examples/err22_text_escape.expect


929 compile 22

930 stderr omc: line 2: error 22

931 

err23_size

File: examples/err23_size.om


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 

File: examples/err23_size.expect


938 compile 23

939 stderr omc: line 3: error 23

940 

err27_fresh

File: examples/err27_fresh.om


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 

File: examples/err27_fresh.expect


951 compile 27

952 stderr omc: line 4: error 27

953 

err28_seal

File: examples/err28_seal.om


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 

File: examples/err28_seal.expect


963 compile 28

964 stderr omc: line 6: error 28

965 

err29_layout

File: examples/err29_layout.om


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 

File: examples/err29_layout.expect


975 compile 29

976 stderr omc: line 4: error 29

977 

err30_contract

File: examples/err30_contract.om


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 

File: examples/err30_contract.expect


991 compile 30

992 stderr omc: line 7: error 30

993 

exit_status

File: examples/exit_status.om


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 

File: examples/exit_status.expect


999 compile 0

1000 exit 44

1001 

File: examples/exit_status.stdout (empty file)

float_arith

File: examples/float_arith.om


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 

File: examples/float_arith.expect


1066 compile 0

1067 exit 0

1068 

File: examples/float_arith.stdout


1069 `1414213562`

1070 `4599075939470750516`

1071 `4607182418800017408`

1072 1

1073 3

1074 -2

1075 7

1076 

float_to_int

File: examples/float_to_int.om


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 

File: examples/float_to_int.expect


1129 compile 0

1130 exit 0

1131 

File: examples/float_to_int.stdout


1132 `9223372036854775807`

1133 -`9223372036854775808`

1134 -5

1135 0

1136 0

1137 255

1138 

functions

File: examples/functions.om


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 

File: examples/functions.expect


1206 compile 0

1207 exit 0

1208 

File: examples/functions.stdout


1209 `2432902008176640000`

1210 21

1211 9

1212 

globals

File: examples/globals.om


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 

File: examples/globals.expect


1277 compile 0

1278 exit 0

1279 

File: examples/globals.stdout


1280 5

1281 2

1282 1

1283 

hello

File: examples/hello.om


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 

File: examples/hello.expect


1294 compile 0

1295 exit 0

1296 

File: examples/hello.stdout


1297 hello, world

1298 

int_arith

File: examples/int_arith.om


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 

File: examples/int_arith.expect


1353 compile 0

1354 exit 0

1355 

File: examples/int_arith.stdout


1356 -3

1357 -1

1358 -3

1359 1

1360 `121932631112635269`

1361 -4

1362 7

1363 

int_bits_and

File: examples/int_bits_and.om


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 

File: examples/int_bits_and.expect


1375 compile 0

1376 exit 8

1377 

loops

File: examples/loops.om


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 

File: examples/loops.expect


1449 compile 0

1450 exit 0

1451 

File: examples/loops.stdout


1452 111

1453 867

1454 

mistake_bound_range

File: examples/mistake_bound_range.om


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 

File: examples/mistake_bound_range.expect


1464 compile 8

1465 stderr omc: line 4: error 8

1466 

mistake_c_call

File: examples/mistake_c_call.om


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 

File: examples/mistake_c_call.expect


1475 compile 6

1476 stderr omc: line 6: error 6

1477 

mistake_char_literal

File: examples/mistake_char_literal.om


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 

File: examples/mistake_char_literal.expect


1484 compile 3

1485 stderr omc: line 3: error 3

1486 

mistake_compare_operator

File: examples/mistake_compare_operator.om


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 

File: examples/mistake_compare_operator.expect


1496 compile 7

1497 stderr omc: line 4: error 7

1498 

mistake_else_if

File: examples/mistake_else_if.om


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 

File: examples/mistake_else_if.expect


1510 compile 5

1511 stderr omc: line 6: error 5

1512 

mistake_for_bound

File: examples/mistake_for_bound.om


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 

File: examples/mistake_for_bound.expect


1523 compile 5

1524 stderr omc: line 5: error 5

1525 

mistake_free

File: examples/mistake_free.om


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 

File: examples/mistake_free.expect


1532 compile 9

1533 stderr omc: line 3: error 9

1534 

mistake_infix

File: examples/mistake_infix.om


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 

File: examples/mistake_infix.expect


1542 compile 5

1543 stderr omc: line 4: error 5

1544 

mistake_main_args

File: examples/mistake_main_args.om


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 

File: examples/mistake_main_args.expect


1550 compile 9

1551 stderr omc: line 5: error 9

1552 

mistake_neg_name

File: examples/mistake_neg_name.om


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 

File: examples/mistake_neg_name.expect


1559 compile 16

1560 stderr omc: line 3: error 16

1561 

mistake_new_var_len

File: examples/mistake_new_var_len.om


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 

File: examples/mistake_new_var_len.expect


1569 compile 16

1570 stderr omc: line 4: error 16

1571 

mistake_om1_rotr

File: examples/mistake_om1_rotr.om


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 

File: examples/mistake_om1_rotr.expect


1578 compile 3

1579 stderr omc: line 4: error 3

1580 

mistake_om2_int_hex

File: examples/mistake_om2_int_hex.om


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 

File: examples/mistake_om2_int_hex.expect


1588 compile 8

1589 stderr omc: line 4: error 8

1590 

mistake_om2_param_slots

File: examples/mistake_om2_param_slots.om


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 

File: examples/mistake_om2_param_slots.expect


1601 compile 9

1602 stderr omc: line 3: error 9

1603 

mistake_om2_part_global

File: examples/mistake_om2_part_global.om


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 

File: examples/mistake_om2_part_global.expect


1614 compile 19

1615 stderr omc: line 8: error 19

1616 

mistake_om2_part_sized

File: examples/mistake_om2_part_sized.om


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 

File: examples/mistake_om2_part_sized.expect


1627 compile 7

1628 stderr omc: line 8: error 7

1629 

mistake_om2_part_statement

File: examples/mistake_om2_part_statement.om


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 

File: examples/mistake_om2_part_statement.expect


1638 compile 9

1639 stderr omc: line 5: error 9

1640 

mistake_om2_upper_hex

File: examples/mistake_om2_upper_hex.om


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 

File: examples/mistake_om2_upper_hex.expect


1648 compile 6

1649 stderr omc: line 4: error 6

1650 

mistake_param_length

File: examples/mistake_param_length.om


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 

File: examples/mistake_param_length.expect


1660 compile 7

1661 stderr omc: line 7: error 7

1662 

mistake_reserved_name

File: examples/mistake_reserved_name.om


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 

File: examples/mistake_reserved_name.expect


1669 compile 16

1670 stderr omc: line 3: error 16

1671 

mistake_return

File: examples/mistake_return.om


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 

File: examples/mistake_return.expect


1678 compile 18

1679 stderr omc: line 6: error 18

1680 

mistake_semicolon

File: examples/mistake_semicolon.om


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 

File: examples/mistake_semicolon.expect


1687 compile 6

1688 stderr omc: line 3: error 6

1689 

mistake_seven_params

File: examples/mistake_seven_params.om


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 

File: examples/mistake_seven_params.expect


1698 compile 9

1699 stderr omc: line 2: error 9

1700 

mistake_short_circuit

File: examples/mistake_short_circuit.om


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 

File: examples/mistake_short_circuit.expect


1712 compile 0

1713 exit 103

1714 

File: examples/mistake_short_circuit.stdout (empty file)

mistake_slash_comment

File: examples/mistake_slash_comment.om


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 

File: examples/mistake_slash_comment.expect


1721 compile 16

1722 stderr omc: line 2: error 16

1723 

mistake_struct_after_fn

File: examples/mistake_struct_after_fn.om


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 

File: examples/mistake_struct_after_fn.expect


1730 compile 9

1731 stderr omc: line 5: error 9

1732 

mistake_trap_range

File: examples/mistake_trap_range.om


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 

File: examples/mistake_trap_range.expect


1738 compile 8

1739 stderr omc: line 3: error 8

1740 

mistake_value_stmt

File: examples/mistake_value_stmt.om


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 

File: examples/mistake_value_stmt.expect


1750 compile 7

1751 stderr omc: line 6: error 7

1752 

om2_hex_rotr

File: examples/om2_hex_rotr.om


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 

File: examples/om2_hex_rotr.expect


1794 compile 0

1795 exit 0

1796 

File: examples/om2_hex_rotr.stdout


1797 `3735928559`

1798 `4024348094`

1799 `18446744073709551615`

1800 239

1801 

om2_param_slots

File: examples/om2_param_slots.om


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 

File: examples/om2_param_slots.expect


1827 compile 0

1828 exit 0

1829 

om2_part_trap

File: examples/om2_part_trap.om


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 

File: examples/om2_part_trap.expect


1848 compile 0

1849 exit 103

1850 

File: examples/om2_part_trap.stdout


1851 before

1852 

om2_tour

File: examples/om2_tour.om


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 

File: examples/om2_tour.expect


1897 compile 0

1898 exit 0

1899 

File: examples/om2_tour.stdout (empty file)

om2_views

File: examples/om2_views.om


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 

File: examples/om2_views.expect


1954 compile 0

1955 exit 0

1956 

File: examples/om2_views.stdout


1957 views and slices

1958 21

1959 9

1960 6

1961 views

1962 

print_decimal

File: examples/print_decimal.om


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 

File: examples/print_decimal.expect


2014 compile 0

2015 exit 0

2016 

File: examples/print_decimal.stdout


2017 0

2018 42

2019 `18446744073709551615`

2020 `9223372036854775807`

2021 -`9223372036854775808`

2022 -1

2023 

stdin_count

File: examples/stdin_count.om


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 

File: examples/stdin_count.expect


2096 compile 0

2097 exit 0

2098 

File: examples/stdin_count.stdin (no final newline)


2099 alpha

2100 beta

2101 gamma delta

2102

2103 end

File: examples/stdin_count.stdout


2104 27

2105 4

2106 2350

2107 

structs

File: examples/structs.om


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 

File: examples/structs.expect


2168 compile 0

2169 exit 0

2170 

File: examples/structs.stdout


2171 -7

2172 7

2173 16

2174 8

2175 4

2176 

text

File: examples/text.om


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 

File: examples/text.expect


2226 compile 0

2227 exit 0

2228 

File: examples/text.stdout


2229 tab here "q" back\slash

2230 24

2231 116

2232 

tmp_files

File: examples/tmp_files.om


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 

File: examples/tmp_files.expect


2315 compile 0

2316 exit 0

2317 

File: examples/tmp_files.stdout


2318 0

2319 6

2320 0

2321 6

2322 0

2323 6

2324 omega

2325 0

2326 0

2327 -2

2328 

trap1_overflow

File: examples/trap1_overflow.om


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 

File: examples/trap1_overflow.expect


2338 compile 0

2339 exit 101

2340 

File: examples/trap1_overflow.stdout


2341 before

2342 

trap2_div0

File: examples/trap2_div0.om


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 

File: examples/trap2_div0.expect


2352 compile 0

2353 exit 102

2354 

File: examples/trap2_div0.stdout


2355 before

2356 

trap3_bounds

File: examples/trap3_bounds.om


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 

File: examples/trap3_bounds.expect


2367 compile 0

2368 exit 103

2369 

File: examples/trap3_bounds.stdout


2370 before

2371 

trap4_loop

File: examples/trap4_loop.om


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 

File: examples/trap4_loop.expect


2383 compile 0

2384 exit 104

2385 

File: examples/trap4_loop.stdout


2386 before

2387 

trap5_conv

File: examples/trap5_conv.om


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 

File: examples/trap5_conv.expect


2397 compile 0

2398 exit 105

2399 

File: examples/trap5_conv.stdout


2400 before

2401 

trap6_stack

File: examples/trap6_stack.om


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 

File: examples/trap6_stack.expect


2416 compile 0

2417 exit 106

2418 

File: examples/trap6_stack.stdout


2419 before

2420 

trap7_shift

File: examples/trap7_shift.om


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 

File: examples/trap7_shift.expect


2430 compile 0

2431 exit 107

2432 

File: examples/trap7_shift.stdout


2433 before

2434 

trap_code

File: examples/trap_code.om


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 

File: examples/trap_code.expect


2442 compile 0

2443 exit 114

2444 

File: examples/trap_code.stdout


2445 before

2446 

unsigned_wrap

File: examples/unsigned_wrap.om


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 

File: examples/unsigned_wrap.expect


2504 compile 0

2505 exit 0

2506 

File: examples/unsigned_wrap.stdout


2507 0

2508 `4294967295`

2509 0

2510 `4294967295`

2511 `4294967295`

2512 `2147483648`

2513 0

2514 

wire_le

File: examples/wire_le.om


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 

File: examples/wire_le.expect


2584 compile 0

2585 exit 0

2586 

File: examples/wire_le.stdout


2587 120

2588 86

2589 52

2590 18

2591 `305419896`

2592 -`13347208`

2593 

fnv1a32

File: skills/omega-encoding/examples/fnv1a32.om


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 

File: skills/omega-encoding/examples/fnv1a32.expect


2650 compile 0

2651 exit 0

2652 

File: skills/omega-encoding/examples/fnv1a32.stdin (no final newline)


2653 abc

File: skills/omega-encoding/examples/fnv1a32.stdout (hex bytes)


2654 0b e9 47 1a

2655 

trap_narrow

File: skills/omega-encoding/examples/trap_narrow.om


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 

File: skills/omega-encoding/examples/trap_narrow.expect


2666 compile 0

2667 exit 105

2668 

Checking the examples

Write every file on this page into one directory tree with the paths in the labels, then run bash examples/gate.sh /path/to/omc. The gate refuses any compiler but the pinned one. It compiles each program, runs it with an empty environment plus its .env 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 GATE PASS examples=N when all of them match. The aienos.com build checks that the files in llms-full.txt are these files, and om/omega-reference/llms-check.sh extracts them from llms-full.txt and runs this gate on them.

File: examples/gate.sh


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