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