interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

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