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/omega-rosetta/blob/main/tasks/stack/stack.om # omega-rosetta aien-dev/omega-rosetta 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 `omega-rosetta / tasks / stack / stack.om` 109 lines ยท 1784 bytes Raw Blame History 1 note Rosetta Code task "Stack": https://rosettacode.org/wiki/Stack 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 text tpush "push " 8 text tpop "pop " 9 text tpeek "peek " 10 text tempty "empty: " 11 text tsize "size " 12 text tyes "yes" 13 text tno "no" 14 global stk long 16 15 global top ulong 1 16 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 17 fn emit a view uchar * to void 18 o = ulong 0 19 while lt o len a bound 4096 20 k = write 1 a o sub len a o 21 if le k 0 22 exit 1 23 end 24 o = add o ulong k 25 end 26 end 27 note emitu writes n in decimal; 20 digits hold every ulong 28 fn emitu n ulong to void 29 d = new uchar 20 0 30 i = ulong 20 31 v = n 32 while true bound 20 33 i = sub i 1 34 put d i uchar add rem v 10 48 35 v = div v 10 36 if eq v 0 37 break 38 end 39 end 40 emit part d i sub 20 i 41 end 42 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 43 fn emiti n long to void 44 if lt n 0 45 emit minus 46 emitu add bitcast ulong not n 1 47 else 48 emitu ulong n 49 end 50 end 51 note a fixed-capacity stack of 16 longs: stk[0..top-1], the top element last; push on a full stack and 52 note pop on an empty one stop the program (exit 2), since there is no heap to grow into 53 fn push v long to void 54 t = get top 0 55 if eq t 16 56 exit 2 57 end 58 put stk t v 59 put top 0 add t 1 60 emit tpush 61 emiti v 62 emit nl 63 end 64 fn pop to long 65 t = get top 0 66 if eq t 0 67 exit 2 68 end 69 put top 0 sub t 1 70 v = get stk sub t 1 71 emit tpop 72 emiti v 73 emit nl 74 ret v 75 end 76 fn peek to long 77 v = get stk sub get top 0 1 78 emit tpeek 79 emiti v 80 emit nl 81 ret v 82 end 83 fn empty to bool 84 e = eq get top 0 0 85 emit tempty 86 if e 87 emit tyes 88 else 89 emit tno 90 end 91 emit nl 92 ret e 93 end 94 fn main to int 95 e = empty 96 push 10 97 push -20 98 push 30 99 v = peek 100 emit tsize 101 emitu get top 0 102 emit nl 103 v = pop 104 v = pop 105 e = empty 106 v = pop 107 e = empty 108 ret 0 109 end