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 / queue-usage.wiki

121 lines · 2078 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 tempty "empty: "
12text tyes "yes"
13text tno "no"
14text terr "pop from an empty queue: error"
15global ring long 8
16global head ulong 1
17global cnt 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 FIFO queue in a ring of 8 slots: cnt elements starting at ring[head]; push writes behind the
54note last one, pop takes ring[head]; a full ring stops the program (exit 2)
55fn push v long to void
56c = get cnt 0
57if eq c 8
58exit 2
59end
60put ring rem add get head 0 c 8 v
61put cnt 0 add c 1
62emit tpush
63emiti v
64emit nl
65end
66note pop returns false, and prints the error, when the queue is empty
67fn pop to bool
68c = get cnt 0
69if eq c 0
70emit terr
71emit nl
72ret false
73end
74h = get head 0
75emit tpop
76emiti get ring h
77emit nl
78put head 0 rem add h 1 8
79put cnt 0 sub c 1
80ret true
81end
82fn empty to void
83emit tempty
84if eq get cnt 0 0
85emit tyes
86else
87emit tno
88end
89emit nl
90end
91fn main to int
92empty
93push 1
94push 2
95push 3
96ok = pop
97empty
98push 4
99ok = pop
100ok = pop
101ok = pop
102empty
103ok = pop
104ret 0
105end
106</syntaxhighlight>
107{{out}}
108<pre>
109empty: yes
110push 1
111push 2
112push 3
113pop 1
114empty: no
115push 4
116pop 2
117pop 3
118pop 4
119empty: yes
120pop from an empty queue: error
121</pre>