interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / research / om-bench / REFERENCE-CARD.md

163 lines · 5470 bytes Raw Blame History Rendered
1# Omega (.om) reference card for om-bench v1
2
3Checked against omega-mv 0b77e6ea, compiler seed/out/omc sha256 2c824805...029f. Both examples at the end
4compile 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`
563 unknown name, 4 line ends early, 5 extra words, 6 bad number, 7 type mismatch, 8 number out of range,
579 word not allowed here, 10 duplicate name, 11 bare number needs a type, 12 too many live names,
5813 name longer than 8, 15 missing end, 16 unknown word or syntax, 17 line after ret/break/exit,
5918 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```
63global inb uchar 65536
64global outb uchar 32
65fn readall to ulong
66n = ulong 0
67while lt n 65536 bound 65537
68k = read 0 inb n sub 65536 n
69if lt k 0
70exit 2
71end
72if eq k 0
73break
74end
75n = add n ulong k
76end
77ret n
78end
79fn printu v ulong to void
80i = ulong 31
81put outb i 10
82x = v
83while true bound 21
84i = sub i 1
85put outb i uchar add rem x 10 48
86x = div x 10
87if eq x 0
88break
89end
90end
91off = i
92k = write 1 outb off sub 32 off
93if ne k long sub 32 off
94exit 3
95end
96end
97fn main to int
98n = readall
99sum = ulong 0
100cur = ulong 0
101have = false
102for i ulong n
103c = get inb i
104if and ge c 48 le c 57
105cur = add mul cur 10 ulong sub c 48
106have = true
107else
108if have
109sum = add sum cur
110end
111cur = 0
112have = false
113end
114end
115if have
116sum = add sum cur
117end
118printu sum
119ret 0
120end
121```
122
123## Example 2: uppercase stdin, then print a fixed word (verified)
124```
125global inb uchar 65536
126text yes "changed\n"
127text no "same\n"
128fn readall to ulong
129n = ulong 0
130while lt n 65536 bound 65537
131k = read 0 inb n sub 65536 n
132if lt k 0
133exit 2
134end
135if eq k 0
136break
137end
138n = add n ulong k
139end
140ret n
141end
142fn main to int
143n = readall
144hit = false
145for i ulong n
146c = get inb i
147if and ge c 97 le c 122
148put inb i sub c 32
149hit = true
150end
151end
152k = write 1 inb 0 n
153if ne k long n
154exit 3
155end
156if hit
157k = write 1 yes 0 len yes
158else
159k = write 1 no 0 len no
160end
161ret 0
162end
163```