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-definition.wiki

137 lines · 2408 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
91note the ring wraps: 6 pushes, 4 pops and 5 more pushes cross slot 7 back to slot 0
92fn main to int
93empty
94for i ulong 6
95push mul long add i 1 11
96end
97for i ulong 4
98ok = pop
99end
100for i ulong 5
101push mul long add i 7 -11
102end
103empty
104note 7 elements remain, so the 8th pop finds the queue empty and ends the loop
105while pop bound 9
106end
107empty
108ret 0
109end
110</syntaxhighlight>
111{{out}}
112<pre>
113empty: yes
114push 11
115push 22
116push 33
117push 44
118push 55
119push 66
120pop 11
121pop 22
122pop 33
123pop 44
124push -77
125push -88
126push -99
127push -110
128...
129pop 66
130pop -77
131pop -88
132pop -99
133pop -110
134pop -121
135pop from an empty queue: error
136empty: yes
137</pre>