interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / stack / stack.om

109 lines · 1784 bytes Raw Blame History
1note Rosetta Code task "Stack": https://rosettacode.org/wiki/Stack
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tpush "push "
8text tpop "pop "
9text tpeek "peek "
10text tempty "empty: "
11text tsize "size "
12text tyes "yes"
13text tno "no"
14global stk long 16
15global top ulong 1
16note emit writes every byte of a to stdout; a short write is retried, an error exits 1
17fn emit a view uchar * to void
18o = ulong 0
19while lt o len a bound 4096
20k = write 1 a o sub len a o
21if le k 0
22exit 1
23end
24o = add o ulong k
25end
26end
27note emitu writes n in decimal; 20 digits hold every ulong
28fn emitu n ulong to void
29d = new uchar 20 0
30i = ulong 20
31v = n
32while true bound 20
33i = sub i 1
34put d i uchar add rem v 10 48
35v = div v 10
36if eq v 0
37break
38end
39end
40emit part d i sub 20 i
41end
42note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
43fn emiti n long to void
44if lt n 0
45emit minus
46emitu add bitcast ulong not n 1
47else
48emitu ulong n
49end
50end
51note a fixed-capacity stack of 16 longs: stk[0..top-1], the top element last; push on a full stack and
52note pop on an empty one stop the program (exit 2), since there is no heap to grow into
53fn push v long to void
54t = get top 0
55if eq t 16
56exit 2
57end
58put stk t v
59put top 0 add t 1
60emit tpush
61emiti v
62emit nl
63end
64fn pop to long
65t = get top 0
66if eq t 0
67exit 2
68end
69put top 0 sub t 1
70v = get stk sub t 1
71emit tpop
72emiti v
73emit nl
74ret v
75end
76fn peek to long
77v = get stk sub get top 0 1
78emit tpeek
79emiti v
80emit nl
81ret v
82end
83fn empty to bool
84e = eq get top 0 0
85emit tempty
86if e
87emit tyes
88else
89emit tno
90end
91emit nl
92ret e
93end
94fn main to int
95e = empty
96push 10
97push -20
98push 30
99v = peek
100emit tsize
101emitu get top 0
102emit nl
103v = pop
104v = pop
105e = empty
106v = pop
107e = empty
108ret 0
109end