omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / research / om-bench / REFERENCE-CARD.md
| 1 | # Omega (.om) reference card for om-bench v1 |
| 2 | |
| 3 | Checked against omega-mv 0b77e6ea, compiler seed/out/omc sha256 2c824805...029f. Both examples at the end |
| 4 | compile and run with that compiler. |
| 5 | |
| 6 | ## Shape |
| 7 | - One statement per line. Words are separated by spaces. No commas, semicolons, braces or indentation. |
| 8 | `note TEXT` is the only comment. |
| 9 | - `fn NAME P1 T1 P2 T2 to TYPE` ... `end`, or `to void`. At most 6 parameters. `fn main to int` takes none; |
| 10 | its return value is the exit status. `exit v` ends the program at once. |
| 11 | - A function with a return type must end every path with `ret v`. A line after `ret`, `break`, |
| 12 | `continue` or `exit` in the same block is an error. |
| 13 | - A function call that returns a value cannot be a statement: bind it, `k = write 1 buf 0 n`. |
| 14 | - Blocks: `if C` ... `else` ... `end`; `while C bound N` ... `end` (N from 1 to 999999, at most N passes); |
| 15 | `for i ulong K` ... `end` (i = 0 .. K-1); `break`; `continue`. |
| 16 | |
| 17 | ## Values are prefix words with fixed arity (no infix, no brackets needed) |
| 18 | - `add sub mul div rem and or xor shl shr` take 2 and give the operand type. |
| 19 | `eq ne lt le gt ge` take 2 and give bool. `not neg` take 1. |
| 20 | - `add mul r r 4` means r*r+4. `eq rem n 2 0` means n mod 2 == 0. |
| 21 | - `and`/`or` evaluate both sides (no short circuit). Check an index bound in an outer `if`, then load. |
| 22 | - `get A i` reads an array element, `put A i v` writes one, `len A` is the ulong length. |
| 23 | |
| 24 | ## Types |
| 25 | - `bool uchar ushort uint ulong byte short int long` (u8 u16 u32 u64 i8 i16 i32 i64). `uchar` is a number. |
| 26 | - No implicit conversion: both operands, a name and its new value, an argument and its parameter must |
| 27 | have the same type. Convert with the type word: `ulong k`, `uchar x`, `long u`. A conversion whose value |
| 28 | does not fit traps (exit 105): mask first, `uchar and x 255`. |
| 29 | - A bare number takes the type of the other operand. A new name needs a typed value: `x = ulong 0`, not |
| 30 | `x = 0`. `true`/`false` are bools. |
| 31 | - Literals are decimal only (no hex, no 'A'; write 65). Unsigned arithmetic wraps mod 2^width. Signed |
| 32 | overflow traps (exit 101). Division by zero traps (102). Out-of-range index traps (103). |
| 33 | |
| 34 | ## Names and limits |
| 35 | - Variable, parameter, global, text and const names: at most 8 characters. Function names up to 63. |
| 36 | - A name first assigned inside a block dies at its `end`; assign it before the block to keep it. |
| 37 | - About 10 live names per function (hard limit 16 registers including temporaries); split big |
| 38 | functions into helpers. |
| 39 | - Reserved, cannot be names: every type name, every operator word (`add sub mul div rem and or xor shl |
| 40 | shr eq ne lt le gt ge neg not`), `get put len read write true false view edit fn to end if else while |
| 41 | bound for ret void global text const free exit note new struct break continue trap own bitcast fsqrt |
| 42 | fabs fmin fmax sizeof offsetof` and the host words. Pick plain names like `cnt`, `acc`, `pos`. |
| 43 | - `global NAME TYPE N` declares a zero-filled array visible in every function. A global can never be |
| 44 | passed as an argument; functions use it by name. |
| 45 | - `NAME = new TYPE N v` makes a local array of N elements set to v; N must be a literal. |
| 46 | - `text NAME "bytes\n"` is a read-only uchar array (escapes `\n \t \\ \"` only). |
| 47 | - `const NAME TYPE LITERAL` names a constant. |
| 48 | |
| 49 | ## Input and output |
| 50 | - `read 0 A off n` reads at most n bytes from stdin into A starting at off. `write 1 A off n` writes n |
| 51 | bytes of A starting at off to stdout. Both return a long: a count, 0 at end of input, or a negative |
| 52 | error. Read in a loop until 0. Compare a write result with `long n`. |
| 53 | - There is no print word for numbers: convert digits into a uchar array yourself (see printu below). |
| 54 | |
| 55 | ## Compiler errors: `omc: line N: error K` |
| 56 | 3 unknown name, 4 line ends early, 5 extra words, 6 bad number, 7 type mismatch, 8 number out of range, |
| 57 | 9 word not allowed here, 10 duplicate name, 11 bare number needs a type, 12 too many live names, |
| 58 | 13 name longer than 8, 15 missing end, 16 unknown word or syntax, 17 line after ret/break/exit, |
| 59 | 18 missing ret, 19 global passed as argument, 20 write to text, 21 constant index out of range. |
| 60 | |
| 61 | ## Example 1: sum of non-negative whole numbers on stdin (verified; a '-' sign is skipped, not handled) |
| 62 | ``` |
| 63 | global inb uchar 65536 |
| 64 | global outb uchar 32 |
| 65 | fn readall to ulong |
| 66 | n = ulong 0 |
| 67 | while lt n 65536 bound 65537 |
| 68 | k = read 0 inb n sub 65536 n |
| 69 | if lt k 0 |
| 70 | exit 2 |
| 71 | end |
| 72 | if eq k 0 |
| 73 | break |
| 74 | end |
| 75 | n = add n ulong k |
| 76 | end |
| 77 | ret n |
| 78 | end |
| 79 | fn printu v ulong to void |
| 80 | i = ulong 31 |
| 81 | put outb i 10 |
| 82 | x = v |
| 83 | while true bound 21 |
| 84 | i = sub i 1 |
| 85 | put outb i uchar add rem x 10 48 |
| 86 | x = div x 10 |
| 87 | if eq x 0 |
| 88 | break |
| 89 | end |
| 90 | end |
| 91 | off = i |
| 92 | k = write 1 outb off sub 32 off |
| 93 | if ne k long sub 32 off |
| 94 | exit 3 |
| 95 | end |
| 96 | end |
| 97 | fn main to int |
| 98 | n = readall |
| 99 | sum = ulong 0 |
| 100 | cur = ulong 0 |
| 101 | have = false |
| 102 | for i ulong n |
| 103 | c = get inb i |
| 104 | if and ge c 48 le c 57 |
| 105 | cur = add mul cur 10 ulong sub c 48 |
| 106 | have = true |
| 107 | else |
| 108 | if have |
| 109 | sum = add sum cur |
| 110 | end |
| 111 | cur = 0 |
| 112 | have = false |
| 113 | end |
| 114 | end |
| 115 | if have |
| 116 | sum = add sum cur |
| 117 | end |
| 118 | printu sum |
| 119 | ret 0 |
| 120 | end |
| 121 | ``` |
| 122 | |
| 123 | ## Example 2: uppercase stdin, then print a fixed word (verified) |
| 124 | ``` |
| 125 | global inb uchar 65536 |
| 126 | text yes "changed\n" |
| 127 | text no "same\n" |
| 128 | fn readall to ulong |
| 129 | n = ulong 0 |
| 130 | while lt n 65536 bound 65537 |
| 131 | k = read 0 inb n sub 65536 n |
| 132 | if lt k 0 |
| 133 | exit 2 |
| 134 | end |
| 135 | if eq k 0 |
| 136 | break |
| 137 | end |
| 138 | n = add n ulong k |
| 139 | end |
| 140 | ret n |
| 141 | end |
| 142 | fn main to int |
| 143 | n = readall |
| 144 | hit = false |
| 145 | for i ulong n |
| 146 | c = get inb i |
| 147 | if and ge c 97 le c 122 |
| 148 | put inb i sub c 32 |
| 149 | hit = true |
| 150 | end |
| 151 | end |
| 152 | k = write 1 inb 0 n |
| 153 | if ne k long n |
| 154 | exit 3 |
| 155 | end |
| 156 | if hit |
| 157 | k = write 1 yes 0 len yes |
| 158 | else |
| 159 | k = write 1 no 0 len no |
| 160 | end |
| 161 | ret 0 |
| 162 | end |
| 163 | ``` |