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