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