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
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_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