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 / core / cortex / tools / pageunit / enc.om

155 lines · 3367 bytes Raw Blame History
1om2
2note enc: page text on stdin becomes an om2 unit on stdout, streamed
3note the emitted unit holds the page as text records tN (N = 0, 1, 2, ...) and a main that calls
4note emit on each record in order, writing the same bytes back. A record ends at a newline, which it
5note keeps as \n, or after RECMAX input bytes of one long line, so no text literal grows with the page.
6note exit 0 ok, 2 read error, 4 control byte refused (below 32 other than tab and newline, or 127),
7note 6 write error. Output is written while input is read, so on a nonzero exit stdout holds an
8note incomplete unit that must be discarded.
9note no size refusal: enc holds one 65536-byte input block and one output buffer, flushed at 65536
10note bytes, whatever the page size. What remains is the memory of the compiler that builds the unit:
11note tests/pageunit/run.sh compiles a page's unit in omc's usage mode and alarms when the unit puts
12note its stated threshold of 8-byte words or more in the compiler's arena (measured 2026-11-11 with a
13note fixed arena of 16777216 words: about 524000 records, or about 64 MB of long lines).
14note out is a reserved word in om2 units so the output buffer is ob.
15note Loop bounds: each pass of the read loop consumes at least one byte or ends it; the inner loops
16note run once per byte of one block, once per digit, or once per record.
17global ib uchar 65536
18global ob uchar 65536
19global st ulong 1
20const RECMAX ulong 4096
21text HEAD "om2\nnote cortex page unit generated by tools/pageunit/enc.om\nnote compile with the omc pinned in this repository and run: stdout is the page text byte for byte\n"
22text TXT "text t"
23text QOPEN " \""
24text QEND "\"\n"
25text EMITA "fn emit a view uchar * to void\nn = len a\no = ulong 0\nwhile lt o n bound 1000000\n"
26text EMITB "k = write 1 a o sub n o\nif le k 0\nexit 1\nend\no = add o ulong k\nend\nend\n"
27text MAINH "fn main to int\n"
28text CALL "emit t"
29text NL "\n"
30text TAIL "ret 0\nend\n"
31fn oflush to void
32n = get st 0
33o = ulong 0
34while lt o n bound 65536
35k = write 1 ob o sub n o
36if le k 0
37exit 6
38end
39o = add o ulong k
40end
41put st 0 0
42end
43fn putb c uchar to void
44p = get st 0
45put ob p c
46p = add p 1
47put st 0 p
48if ge p 65536
49oflush
50end
51end
52fn puts a view uchar * to void
53n = len a
54i = ulong 0
55while lt i n bound 4096
56putb get a i
57i = add i 1
58end
59end
60fn putdec v ulong to void
61d = new uchar 20 0
62k = ulong 0
63x = v
64while gt x 0 bound 20
65put d k uchar add rem x 10 48
66x = div x 10
67k = add k 1
68end
69if eq k 0
70putb 48
71end
72while gt k 0 bound 20
73k = sub k 1
74putb get d k
75end
76end
77fn main to int
78puts HEAD
79cnt = ulong 0
80opn = ulong 0
81inrec = ulong 0
82go = ulong 1
83while eq go 1 bound 9223372036854775807
84r = read 0 ib 0 65536
85if lt r 0
86ret 2
87end
88if eq r 0
89go = ulong 0
90else
91n = ulong r
92i = ulong 0
93while lt i n bound 65536
94c = get ib i
95if eq opn 0
96puts TXT
97putdec cnt
98puts QOPEN
99opn = ulong 1
100inrec = ulong 0
101end
102if eq c 10
103putb 92
104putb 110
105puts QEND
106opn = ulong 0
107cnt = add cnt 1
108else
109if eq c 9
110putb 92
111putb 116
112else
113if lt c 32
114ret 4
115end
116if eq c 127
117ret 4
118end
119if eq c 34
120putb 92
121end
122if eq c 92
123putb 92
124end
125putb c
126end
127inrec = add inrec 1
128if ge inrec RECMAX
129puts QEND
130opn = ulong 0
131cnt = add cnt 1
132end
133end
134i = add i 1
135end
136end
137end
138if eq opn 1
139puts QEND
140cnt = add cnt 1
141end
142puts EMITA
143puts EMITB
144puts MAINH
145j = ulong 0
146while lt j cnt bound 9223372036854775807
147puts CALL
148putdec j
149puts NL
150j = add j 1
151end
152puts TAIL
153oflush
154ret 0
155end