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
omega-mv / apps / omtex / lib / omtex.om
| 1 | note omtex: an Omega -> LaTeX library. Concatenate it in front of a paper body (Omega has no imports, FR-3): |
| 2 | note cat lib/omtex.om paper.om | omc > paper.gen ; ./paper.gen > paper.tex ; ./paper.gen check OMC DIR |
| 3 | note Reserved for the library: globals and texts whose names start with z, functions whose names start with tx. |
| 4 | note Any input the library cannot render safely is refused: a message on stderr, then trap 11 (exit 111). |
| 5 | om2 |
| 6 | global zob uchar 4194304 |
| 7 | global zst ulong 64 |
| 8 | global znb uchar 32 |
| 9 | global zpb uchar 4096 |
| 10 | global zomc uchar 2048 |
| 11 | global zdir uchar 2048 |
| 12 | global zlng uchar 64 |
| 13 | global zws uchar 8 |
| 14 | note zst cells: 0 out pos, 1 element, 2 mode (1 check), 3 phase (1 preamble, 2 body, 3 ended), 4 quote, |
| 15 | note 5 emphasis depth, 6-13 emphasis stack, 14 list depth, 15-30 list stack, 31 table open, 32 cells in row, |
| 16 | note 33 pass, 34 fail, 35 fragments, 36 omc path len, 37 dir len, 38 expected rc of next Omega listing, |
| 17 | note 39 language len. |
| 18 | text zmhead "omtex: refused element " |
| 19 | text zmat " at byte " |
| 20 | text zmnl "\n" |
| 21 | text zrbs ": backslash in prose must be followed by one of \\ * ` $ [ ] \" ~" |
| 22 | text zrctl ": control byte" |
| 23 | text zrutf ": malformed or unsupported UTF-8" |
| 24 | text zrquo ": unbalanced \" quote" |
| 25 | text zremph ": unclosed or misnested * / ** emphasis" |
| 26 | text zrdeep ": emphasis nested deeper than 8" |
| 27 | text zrcode ": unclosed ` code span" |
| 28 | text zrmath ": unclosed or empty $ math span" |
| 29 | text zrbrace ": unbalanced braces in math" |
| 30 | text zrmch ": byte not allowed in math (% $ # control non-ASCII, or & outside align)" |
| 31 | text zrnl ": \\\\ outside align" |
| 32 | text zrdeny ": control sequence on the deny list" |
| 33 | text zrkey ": label or key must be non-empty [A-Za-z0-9:._+/-]" |
| 34 | text zropt ": option text must be [A-Za-z0-9,=.:+/*- ]" |
| 35 | text zrorder ": word used in the wrong document phase" |
| 36 | text zrlist ": item, cell or row outside its list or table, or list nesting over 16" |
| 37 | text zrclose ": enddoc with an open list or table" |
| 38 | text zrlst ": listing body has \\end{lstlisting}, non-ASCII or a control byte" |
| 39 | text zrfull ": output larger than 4 MiB" |
| 40 | text zrusage ": usage: GEN > paper.tex | GEN check OMC DIR" |
| 41 | text zrlang ": set codelang before listing" |
| 42 | text zrexec ": cannot write or run a listing check" |
| 43 | text zbsl "\\textbackslash{}" |
| 44 | text ztil "\\textasciitilde{}" |
| 45 | text zcir "\\textasciicircum{}" |
| 46 | text zlt "\\textless{}" |
| 47 | text zgt "\\textgreater{}" |
| 48 | text zbar "\\textbar{}" |
| 49 | text zdq "\\textquotedbl{}" |
| 50 | text zgrv "\\textasciigrave{}" |
| 51 | text zlbr "{[}" |
| 52 | text zrbr "{]}" |
| 53 | text ztt "\\texttt{" |
| 54 | text zemph "\\emph{" |
| 55 | text zbold "\\textbf{" |
| 56 | text zoq "``" |
| 57 | text zcq "''" |
| 58 | text zcite "~\\cite{" |
| 59 | text zref "~\\ref{" |
| 60 | text zdcls "\\documentclass" |
| 61 | text zusep "\\usepackage" |
| 62 | text ztitle "\\title{" |
| 63 | text zauth "\\author{" |
| 64 | text zdate "\\date{" |
| 65 | text zbegd "\\begin{document}\n" |
| 66 | text zmkt "\\maketitle\n\n" |
| 67 | text zabs1 "\\begin{abstract}\n" |
| 68 | text zabs2 "\n\\end{abstract}\n\n" |
| 69 | text zsec "\\section{" |
| 70 | text zsecs "\\section*{" |
| 71 | text zsub "\\subsection{" |
| 72 | text zsubs "\\subsection*{" |
| 73 | text zlabel "\\label{" |
| 74 | text znoind "\\noindent " |
| 75 | text zitem "\\item " |
| 76 | text zbitz "\\begin{itemize}" |
| 77 | text zeitz "\\end{itemize}\n\n" |
| 78 | text zbenu "\\begin{enumerate}" |
| 79 | text zeenu "\\end{enumerate}\n\n" |
| 80 | text zbeq "\\begin{equation}\n" |
| 81 | text zeeq "\n\\end{equation}\n" |
| 82 | text zbdm "\\[\n" |
| 83 | text zedm "\n\\]\n" |
| 84 | text zbal "\\begin{align}\n" |
| 85 | text zeal "\n\\end{align}\n" |
| 86 | text zbls "\\begin{lstlisting}[language=" |
| 87 | text zcap ",caption={" |
| 88 | text zlbl "},label=" |
| 89 | text zels "\\end{lstlisting}\n\n" |
| 90 | text zendl "\\end{lstlisting}" |
| 91 | text zomega "Omega" |
| 92 | text zendd "\\end{document}\n" |
| 93 | text zbib1 "\\begin{thebibliography}{99}\n" |
| 94 | text zbib2 "\\end{thebibliography}\n\n" |
| 95 | text zbibi "\\bibitem{" |
| 96 | text zbsty "\\bibliographystyle{" |
| 97 | text zbibf "}\n\\bibliography{" |
| 98 | text ztab1 "\\begin{table}[htbp]\n\\centering\n\\caption{" |
| 99 | text ztab2 "\\begin{tabular}{" |
| 100 | text ztab3 "\\end{tabular}\n\\end{table}\n\n" |
| 101 | text zrow " \\\\\n" |
| 102 | text zamp " & " |
| 103 | text zhline "\\hline\n" |
| 104 | text zfig1 "\\begin{figure}[htbp]\n\\centering\n\\includegraphics[width=\\linewidth]{" |
| 105 | text zfig2 "}\n\\caption{" |
| 106 | text zfig3 "\\end{figure}\n\n" |
| 107 | text zck "check" |
| 108 | text zlst1 "LISTING " |
| 109 | text zlst2 " want=" |
| 110 | text zlst3 " got=" |
| 111 | text zpass " PASS\n" |
| 112 | text zfailw " FAIL\n" |
| 113 | text zfragw " fragment NOT_CHECKED\n" |
| 114 | text zsum1 "LISTINGS pass=" |
| 115 | text zsum2 " fail=" |
| 116 | text zsum3 " fragments=" |
| 117 | text zsfx ".om" |
| 118 | text zsfxb ".bin" |
| 119 | text zsfxe ".err" |
| 120 | text zd1 "input" |
| 121 | text zd2 "include" |
| 122 | text zd3 "write" |
| 123 | text zd4 "def" |
| 124 | text zd5 "edef" |
| 125 | text zd6 "gdef" |
| 126 | text zd7 "xdef" |
| 127 | text zd8 "let" |
| 128 | text zd9 "catcode" |
| 129 | text zd10 "immediate" |
| 130 | text zd11 "openout" |
| 131 | text zd12 "openin" |
| 132 | text zd13 "csname" |
| 133 | text zd14 "special" |
| 134 | text zd15 "read" |
| 135 | text zlang1 "\\usepackage{xcolor}\n\\usepackage{listings}\n\\lstdefinelanguage{Omega}{sensitive=true,morecomment=[l]{note},morestring=[b]\",\n" |
| 136 | text zlang2 "morekeywords=[1]{fn,to,end,if,else,while,bound,for,ret,break,continue,trap,exit,void,global,text,const,struct,own,view,edit,true,false,free,om2,ir,export,layout,field,at,little,big,abstract,holds,seal,open,inline,unroll,fresh,part,out,ops,requires,ensures,invariant,result,old},\n" |
| 137 | text zlang3 "morekeywords=[2]{bool,uchar,u8,ushort,u16,uint,u32,ulong,u64,byte,i8,short,i16,int,i32,long,i64,f32,f64},\n" |
| 138 | text zlang4 "morekeywords=[3]{add,sub,mul,div,rem,and,or,xor,shl,shr,eq,ne,lt,le,gt,ge,neg,not,get,len,read,write,put,new,bitcast,fsqrt,fabs,fmin,fmax,sizeof,offsetof,rotr,rotl},\n" |
| 139 | text zlang5 "morekeywords=[4]{fclose,fsync,fchmod,fseek,socknew,listen,accept,fork,dupfd,monons,fopen,unlink,makedir,getdnts,connect,bind,waitpid,fstat,recv,send,sleepns,argcnt,argread,waitio,execprog,renames,rmdir,killpid,pipefds,getpid,realns,sockname,cwd,readlnk,sockopt,random,envcnt,envread,chdir,ftrunc,stat,link,symlnk,chmod,faccess,getuid,geteuid,getgid,getegid,lstat,truncate,chown,fchdir,getppid,umask,fdsync,memfd,setsid,getpgid,setpgid,pread,pwrite,getsid,syncfs,shutdown,devctl,mapnew,mapdrop,mapread,mapwrite,devcall,uvmctl,mapaddr,mapget,mapset,maphold,mapbind}}\n" |
| 140 | text zlang6 "\\lstset{basicstyle=\\ttfamily\\small,keywordstyle=[1]\\color{blue!65!black},keywordstyle=[2]\\color{teal!75!black},keywordstyle=[3]\\color{violet!80!black},keywordstyle=[4]\\color{orange!70!black},commentstyle=\\itshape\\color{black!55},stringstyle=\\color{red!60!black},columns=flexible,keepspaces=true,frame=single,breaklines=true,captionpos=b}\n" |
| 141 | note ---- byte output: stderr/stdout writers and the document buffer ---- |
| 142 | fn txw fd int t view uchar * to void |
| 143 | o = ulong 0 |
| 144 | while lt o len t bound 999999 |
| 145 | n = write fd t o sub len t o |
| 146 | if le n 0 |
| 147 | trap 12 |
| 148 | end |
| 149 | o = add o ulong n |
| 150 | end |
| 151 | end |
| 152 | fn txnum v ulong to ulong |
| 153 | i = ulong 31 |
| 154 | put znb i uchar add 48 rem v 10 |
| 155 | x = div v 10 |
| 156 | while gt x 0 bound 21 |
| 157 | i = sub i 1 |
| 158 | put znb i uchar add 48 rem x 10 |
| 159 | x = div x 10 |
| 160 | end |
| 161 | ret i |
| 162 | end |
| 163 | fn txwn fd int v ulong to void |
| 164 | i = txnum v |
| 165 | n = write fd znb i sub 32 i |
| 166 | if le n 0 |
| 167 | trap 12 |
| 168 | end |
| 169 | end |
| 170 | fn txfail why view uchar * pos ulong to void |
| 171 | txw 2 zmhead |
| 172 | txwn 2 get zst 1 |
| 173 | txw 2 why |
| 174 | txw 2 zmat |
| 175 | txwn 2 pos |
| 176 | txw 2 zmnl |
| 177 | trap 11 |
| 178 | end |
| 179 | fn txb b uchar to void |
| 180 | p = get zst 0 |
| 181 | if ge p 4194304 |
| 182 | txfail zrfull p |
| 183 | end |
| 184 | put zob p b |
| 185 | put zst 0 add p 1 |
| 186 | end |
| 187 | fn txs t view uchar * to void |
| 188 | for i ulong len t |
| 189 | txb get t i |
| 190 | end |
| 191 | end |
| 192 | fn txrng t view uchar * s ulong e ulong to void |
| 193 | i = s |
| 194 | while lt i e bound 999999 |
| 195 | txb get t i |
| 196 | i = add i 1 |
| 197 | end |
| 198 | end |
| 199 | fn txflush to void |
| 200 | n = get zst 0 |
| 201 | o = ulong 0 |
| 202 | while lt o n bound 999999 |
| 203 | w = write 1 zob o sub n o |
| 204 | if le w 0 |
| 205 | trap 12 |
| 206 | end |
| 207 | o = add o ulong w |
| 208 | end |
| 209 | end |
| 210 | note ---- element and phase bookkeeping ---- |
| 211 | fn txel ph ulong to void |
| 212 | put zst 1 add get zst 1 1 |
| 213 | if ne get zst 3 ph |
| 214 | txfail zrorder 0 |
| 215 | end |
| 216 | end |
| 217 | note ---- character classes ---- |
| 218 | fn txalpha c uchar to bool |
| 219 | ret or and ge c 65 le c 90 and ge c 97 le c 122 |
| 220 | end |
| 221 | fn txalnum c uchar to bool |
| 222 | ret or txalpha c and ge c 48 le c 57 |
| 223 | end |
| 224 | fn txmark c uchar to bool |
| 225 | if or or or eq c 92 eq c 42 or eq c 96 eq c 36 or or eq c 91 eq c 93 eq c 34 |
| 226 | ret true |
| 227 | end |
| 228 | ret eq c 126 |
| 229 | end |
| 230 | fn txplain c uchar to bool |
| 231 | ret or or or eq c 35 eq c 36 or eq c 37 eq c 38 or or eq c 95 eq c 123 eq c 125 |
| 232 | end |
| 233 | fn txkeyc c uchar comma bool to bool |
| 234 | if txalnum c |
| 235 | ret true |
| 236 | end |
| 237 | if or or eq c 58 eq c 46 or eq c 95 eq c 45 |
| 238 | ret true |
| 239 | end |
| 240 | if or eq c 43 eq c 47 |
| 241 | ret true |
| 242 | end |
| 243 | ret and comma eq c 44 |
| 244 | end |
| 245 | fn txoptc c uchar to bool |
| 246 | if txkeyc c true |
| 247 | ret true |
| 248 | end |
| 249 | ret or or eq c 61 eq c 42 eq c 32 |
| 250 | end |
| 251 | note ---- one ASCII byte as LaTeX text ---- |
| 252 | fn txch2 c uchar pos ulong to void |
| 253 | if eq c 126 |
| 254 | txs ztil |
| 255 | ret |
| 256 | end |
| 257 | if eq c 94 |
| 258 | txs zcir |
| 259 | ret |
| 260 | end |
| 261 | if eq c 60 |
| 262 | txs zlt |
| 263 | ret |
| 264 | end |
| 265 | if eq c 62 |
| 266 | txs zgt |
| 267 | ret |
| 268 | end |
| 269 | if eq c 124 |
| 270 | txs zbar |
| 271 | ret |
| 272 | end |
| 273 | if eq c 34 |
| 274 | txs zdq |
| 275 | ret |
| 276 | end |
| 277 | if eq c 96 |
| 278 | txs zgrv |
| 279 | ret |
| 280 | end |
| 281 | if eq c 91 |
| 282 | txs zlbr |
| 283 | ret |
| 284 | end |
| 285 | if eq c 93 |
| 286 | txs zrbr |
| 287 | ret |
| 288 | end |
| 289 | txb c |
| 290 | end |
| 291 | fn txch c uchar pos ulong to void |
| 292 | if txplain c |
| 293 | txb 92 |
| 294 | txb c |
| 295 | ret |
| 296 | end |
| 297 | if eq c 92 |
| 298 | txs zbsl |
| 299 | ret |
| 300 | end |
| 301 | if or eq c 9 eq c 10 |
| 302 | txb 32 |
| 303 | ret |
| 304 | end |
| 305 | if or lt c 32 ge c 127 |
| 306 | txfail zrctl pos |
| 307 | end |
| 308 | txch2 c pos |
| 309 | end |
| 310 | note ---- prose scanner ---- |
| 311 | fn txfind t view uchar * s ulong c uchar to ulong |
| 312 | i = s |
| 313 | while lt i len t bound 999999 |
| 314 | if eq get t i c |
| 315 | ret i |
| 316 | end |
| 317 | i = add i 1 |
| 318 | end |
| 319 | ret len t |
| 320 | end |
| 321 | fn txpesc t view uchar * i ulong to ulong |
| 322 | j = add i 1 |
| 323 | if ge j len t |
| 324 | txfail zrbs i |
| 325 | ret j |
| 326 | end |
| 327 | c = get t j |
| 328 | if not txmark c |
| 329 | txfail zrbs i |
| 330 | end |
| 331 | txch c j |
| 332 | ret add j 1 |
| 333 | end |
| 334 | fn txemph k ulong pos ulong to void |
| 335 | d = get zst 5 |
| 336 | if gt d 0 |
| 337 | if eq get zst add 5 d k |
| 338 | txb 125 |
| 339 | put zst 5 sub d 1 |
| 340 | ret |
| 341 | end |
| 342 | end |
| 343 | if ge d 8 |
| 344 | txfail zrdeep pos |
| 345 | end |
| 346 | if eq k 1 |
| 347 | txs zemph |
| 348 | else |
| 349 | txs zbold |
| 350 | end |
| 351 | put zst 5 add d 1 |
| 352 | put zst add 6 d k |
| 353 | end |
| 354 | fn txpstar t view uchar * i ulong to ulong |
| 355 | k = ulong 1 |
| 356 | j = add i 1 |
| 357 | if lt j len t |
| 358 | if eq get t j 42 |
| 359 | k = ulong 2 |
| 360 | j = add j 1 |
| 361 | end |
| 362 | end |
| 363 | txemph k i |
| 364 | ret j |
| 365 | end |
| 366 | fn txpcode t view uchar * i ulong to ulong |
| 367 | s = add i 1 |
| 368 | j = txfind t s 96 |
| 369 | if ge j len t |
| 370 | txfail zrcode i |
| 371 | end |
| 372 | txs ztt |
| 373 | k = s |
| 374 | while lt k j bound 999999 |
| 375 | txch get t k k |
| 376 | k = add k 1 |
| 377 | end |
| 378 | txb 125 |
| 379 | ret add j 1 |
| 380 | end |
| 381 | note the closing $ of inline math; a backslash pair such as \$ inside the math is skipped |
| 382 | fn txfindm t view uchar * s ulong to ulong |
| 383 | i = s |
| 384 | while lt i len t bound 999999 |
| 385 | c = get t i |
| 386 | if eq c 36 |
| 387 | ret i |
| 388 | end |
| 389 | if eq c 92 |
| 390 | i = add i 1 |
| 391 | end |
| 392 | i = add i 1 |
| 393 | end |
| 394 | ret len t |
| 395 | end |
| 396 | fn txpmath t view uchar * i ulong to ulong |
| 397 | s = add i 1 |
| 398 | j = txfindm t s |
| 399 | if or ge j len t eq j s |
| 400 | txfail zrmath i |
| 401 | end |
| 402 | txmchk t s j false |
| 403 | txb 36 |
| 404 | txrng t s j |
| 405 | txb 36 |
| 406 | ret add j 1 |
| 407 | end |
| 408 | fn txpbrk t view uchar * i ulong to ulong |
| 409 | s = add i 2 |
| 410 | if ge s len t |
| 411 | txch 91 i |
| 412 | ret add i 1 |
| 413 | end |
| 414 | c = get t add i 1 |
| 415 | if and ne c 64 ne c 35 |
| 416 | txch 91 i |
| 417 | ret add i 1 |
| 418 | end |
| 419 | j = txfind t s 93 |
| 420 | txkey t s j eq c 64 |
| 421 | if eq c 64 |
| 422 | txs zcite |
| 423 | else |
| 424 | txs zref |
| 425 | end |
| 426 | txrng t s j |
| 427 | txb 125 |
| 428 | ret add j 1 |
| 429 | end |
| 430 | fn txquote to void |
| 431 | if eq get zst 4 0 |
| 432 | txs zoq |
| 433 | put zst 4 1 |
| 434 | else |
| 435 | txs zcq |
| 436 | put zst 4 0 |
| 437 | end |
| 438 | end |
| 439 | fn txutf8 t view uchar * i ulong to ulong |
| 440 | c = get t i |
| 441 | n = ulong 0 |
| 442 | if and ge c 194 le c 223 |
| 443 | n = ulong 1 |
| 444 | end |
| 445 | if and ge c 224 le c 239 |
| 446 | n = ulong 2 |
| 447 | end |
| 448 | if and ge c 240 le c 244 |
| 449 | n = ulong 3 |
| 450 | end |
| 451 | if or eq n 0 ge add i n len t |
| 452 | txfail zrutf i |
| 453 | end |
| 454 | for k ulong n |
| 455 | d = get t add add i 1 k |
| 456 | if or lt d 128 gt d 191 |
| 457 | txfail zrutf i |
| 458 | end |
| 459 | end |
| 460 | txrng t i add add i 1 n |
| 461 | ret add add i 1 n |
| 462 | end |
| 463 | fn txpat t view uchar * i ulong to ulong |
| 464 | c = get t i |
| 465 | if eq c 92 |
| 466 | ret txpesc t i |
| 467 | end |
| 468 | if eq c 42 |
| 469 | ret txpstar t i |
| 470 | end |
| 471 | if eq c 96 |
| 472 | ret txpcode t i |
| 473 | end |
| 474 | if eq c 36 |
| 475 | ret txpmath t i |
| 476 | end |
| 477 | if eq c 91 |
| 478 | ret txpbrk t i |
| 479 | end |
| 480 | if eq c 34 |
| 481 | txquote |
| 482 | ret add i 1 |
| 483 | end |
| 484 | if eq c 126 |
| 485 | txb 126 |
| 486 | ret add i 1 |
| 487 | end |
| 488 | if ge c 128 |
| 489 | ret txutf8 t i |
| 490 | end |
| 491 | txch c i |
| 492 | ret add i 1 |
| 493 | end |
| 494 | fn txprose t view uchar * to void |
| 495 | put zst 4 0 |
| 496 | put zst 5 0 |
| 497 | i = ulong 0 |
| 498 | while lt i len t bound 999999 |
| 499 | i = txpat t i |
| 500 | end |
| 501 | if ne get zst 4 0 |
| 502 | txfail zrquo len t |
| 503 | end |
| 504 | if ne get zst 5 0 |
| 505 | txfail zremph len t |
| 506 | end |
| 507 | end |
| 508 | note ---- keys, options and math ---- |
| 509 | fn txkey t view uchar * s ulong e ulong comma bool to void |
| 510 | if ge s e |
| 511 | txfail zrkey s |
| 512 | end |
| 513 | i = s |
| 514 | while lt i e bound 999999 |
| 515 | if not txkeyc get t i comma |
| 516 | txfail zrkey i |
| 517 | end |
| 518 | i = add i 1 |
| 519 | end |
| 520 | end |
| 521 | fn txopt t view uchar * to void |
| 522 | for i ulong len t |
| 523 | if not txoptc get t i |
| 524 | txfail zropt i |
| 525 | end |
| 526 | end |
| 527 | end |
| 528 | fn txeqr t view uchar * s ulong e ulong w view uchar * to bool |
| 529 | if ne sub e s len w |
| 530 | ret false |
| 531 | end |
| 532 | for i ulong len w |
| 533 | if ne get t add s i get w i |
| 534 | ret false |
| 535 | end |
| 536 | end |
| 537 | ret true |
| 538 | end |
| 539 | fn txdeny2 t view uchar * s ulong e ulong to bool |
| 540 | if or or txeqr t s e zd9 txeqr t s e zd10 or txeqr t s e zd11 txeqr t s e zd12 |
| 541 | ret true |
| 542 | end |
| 543 | ret or or txeqr t s e zd13 txeqr t s e zd14 txeqr t s e zd15 |
| 544 | end |
| 545 | fn txdeny t view uchar * s ulong e ulong to bool |
| 546 | if or or txeqr t s e zd1 txeqr t s e zd2 or txeqr t s e zd3 txeqr t s e zd4 |
| 547 | ret true |
| 548 | end |
| 549 | if or or txeqr t s e zd5 txeqr t s e zd6 or txeqr t s e zd7 txeqr t s e zd8 |
| 550 | ret true |
| 551 | end |
| 552 | ret txdeny2 t s e |
| 553 | end |
| 554 | fn txmcmd t view uchar * i ulong e ulong amp bool to ulong |
| 555 | j = add i 1 |
| 556 | if ge j e |
| 557 | txfail zrmch i |
| 558 | end |
| 559 | c = get t j |
| 560 | if txalpha c |
| 561 | k = add j 1 |
| 562 | while lt k e bound 999999 |
| 563 | if not txalpha get t k |
| 564 | break |
| 565 | end |
| 566 | k = add k 1 |
| 567 | end |
| 568 | if txdeny t j k |
| 569 | txfail zrdeny i |
| 570 | end |
| 571 | ret k |
| 572 | end |
| 573 | if and eq c 92 not amp |
| 574 | txfail zrnl i |
| 575 | end |
| 576 | if or lt c 32 gt c 126 |
| 577 | txfail zrmch j |
| 578 | end |
| 579 | ret add j 1 |
| 580 | end |
| 581 | fn txmbad c uchar amp bool to bool |
| 582 | if or or lt c 32 gt c 126 or eq c 37 eq c 36 |
| 583 | ret true |
| 584 | end |
| 585 | ret or eq c 35 and eq c 38 not amp |
| 586 | end |
| 587 | fn txmchk t view uchar * s ulong e ulong amp bool to void |
| 588 | d = long 0 |
| 589 | i = s |
| 590 | while lt i e bound 999999 |
| 591 | c = get t i |
| 592 | if eq c 92 |
| 593 | i = txmcmd t i e amp |
| 594 | continue |
| 595 | end |
| 596 | if eq c 123 |
| 597 | d = add d 1 |
| 598 | end |
| 599 | if eq c 125 |
| 600 | d = sub d 1 |
| 601 | if lt d 0 |
| 602 | txfail zrbrace i |
| 603 | end |
| 604 | end |
| 605 | if and txmbad c amp ne c 10 |
| 606 | txfail zrmch i |
| 607 | end |
| 608 | i = add i 1 |
| 609 | end |
| 610 | if ne d 0 |
| 611 | txfail zrbrace e |
| 612 | end |
| 613 | end |
| 614 | fn txmath t view uchar * amp bool to void |
| 615 | if eq len t 0 |
| 616 | txfail zrmath 0 |
| 617 | end |
| 618 | txmchk t 0 len t amp |
| 619 | txs t |
| 620 | end |
| 621 | fn txlchk t view uchar * to void |
| 622 | for i ulong len t |
| 623 | c = get t i |
| 624 | if and or lt c 32 gt c 126 and ne c 10 ne c 9 |
| 625 | txfail zrlst i |
| 626 | end |
| 627 | if eq c 92 |
| 628 | if txeqr t i add i len zendl zendl |
| 629 | txfail zrlst i |
| 630 | end |
| 631 | end |
| 632 | end |
| 633 | end |
| 634 | fn txlabel l view uchar * to void |
| 635 | txkey l 0 len l false |
| 636 | txs zlabel |
| 637 | txs l |
| 638 | txb 125 |
| 639 | end |
| 640 | note ---- arguments and phase 0 ---- |
| 641 | fn txinit to void |
| 642 | n = argcnt |
| 643 | if eq n 1 |
| 644 | ret |
| 645 | end |
| 646 | if ne n 4 |
| 647 | txfail zrusage 0 |
| 648 | end |
| 649 | k = argread 1 zpb 0 64 |
| 650 | if ne k 5 |
| 651 | txfail zrusage 0 |
| 652 | end |
| 653 | for i ulong 5 |
| 654 | if ne get zpb i get zck i |
| 655 | txfail zrusage 0 |
| 656 | end |
| 657 | end |
| 658 | a = argread 2 zomc 0 2040 |
| 659 | b = argread 3 zdir 0 2040 |
| 660 | if or le a 0 le b 0 |
| 661 | txfail zrusage 0 |
| 662 | end |
| 663 | put zst 36 ulong a |
| 664 | put zst 37 ulong b |
| 665 | put zst 2 1 |
| 666 | end |
| 667 | note ---- public preamble words ---- |
| 668 | fn docclass opts view uchar * cls view uchar * to void |
| 669 | txel 0 |
| 670 | txinit |
| 671 | put zst 3 1 |
| 672 | txopt opts |
| 673 | txkey cls 0 len cls false |
| 674 | txs zdcls |
| 675 | if gt len opts 0 |
| 676 | txb 91 |
| 677 | txs opts |
| 678 | txb 93 |
| 679 | end |
| 680 | txb 123 |
| 681 | txs cls |
| 682 | txb 125 |
| 683 | txb 10 |
| 684 | end |
| 685 | fn usepkgo opts view uchar * name view uchar * to void |
| 686 | txel 1 |
| 687 | txopt opts |
| 688 | txkey name 0 len name true |
| 689 | txs zusep |
| 690 | txb 91 |
| 691 | txs opts |
| 692 | txb 93 |
| 693 | txb 123 |
| 694 | txs name |
| 695 | txb 125 |
| 696 | txb 10 |
| 697 | end |
| 698 | fn usepkg name view uchar * to void |
| 699 | txel 1 |
| 700 | txkey name 0 len name true |
| 701 | txs zusep |
| 702 | txb 123 |
| 703 | txs name |
| 704 | txb 125 |
| 705 | txb 10 |
| 706 | end |
| 707 | fn preamble t view uchar * to void |
| 708 | txel 1 |
| 709 | txmath t true |
| 710 | txb 10 |
| 711 | end |
| 712 | fn omlang to void |
| 713 | txel 1 |
| 714 | txs zlang1 |
| 715 | txs zlang2 |
| 716 | txs zlang3 |
| 717 | txs zlang4 |
| 718 | txs zlang5 |
| 719 | txs zlang6 |
| 720 | end |
| 721 | fn txwrap w view uchar * t view uchar * to void |
| 722 | txs w |
| 723 | txprose t |
| 724 | txb 125 |
| 725 | txb 10 |
| 726 | end |
| 727 | fn title t view uchar * to void |
| 728 | txel 1 |
| 729 | txwrap ztitle t |
| 730 | end |
| 731 | fn author t view uchar * to void |
| 732 | txel 1 |
| 733 | txwrap zauth t |
| 734 | end |
| 735 | fn date t view uchar * to void |
| 736 | txel 1 |
| 737 | txwrap zdate t |
| 738 | end |
| 739 | fn begindoc to void |
| 740 | txel 1 |
| 741 | txb 10 |
| 742 | txs zbegd |
| 743 | put zst 3 2 |
| 744 | end |
| 745 | note ---- public body words ---- |
| 746 | fn maketitle to void |
| 747 | txel 2 |
| 748 | txs zmkt |
| 749 | end |
| 750 | fn docabstract t view uchar * to void |
| 751 | txel 2 |
| 752 | txs zabs1 |
| 753 | txprose t |
| 754 | txs zabs2 |
| 755 | end |
| 756 | fn section t view uchar * to void |
| 757 | txel 2 |
| 758 | txwrap zsec t |
| 759 | txb 10 |
| 760 | end |
| 761 | fn sectionl t view uchar * l view uchar * to void |
| 762 | txel 2 |
| 763 | txs zsec |
| 764 | txprose t |
| 765 | txb 125 |
| 766 | txlabel l |
| 767 | txb 10 |
| 768 | txb 10 |
| 769 | end |
| 770 | fn sectionn t view uchar * to void |
| 771 | txel 2 |
| 772 | txwrap zsecs t |
| 773 | txb 10 |
| 774 | end |
| 775 | fn subsection t view uchar * to void |
| 776 | txel 2 |
| 777 | txwrap zsub t |
| 778 | txb 10 |
| 779 | end |
| 780 | fn subsectn t view uchar * to void |
| 781 | txel 2 |
| 782 | txwrap zsubs t |
| 783 | txb 10 |
| 784 | end |
| 785 | fn para t view uchar * to void |
| 786 | txel 2 |
| 787 | txprose t |
| 788 | txb 10 |
| 789 | txb 10 |
| 790 | end |
| 791 | note prose: text that does not end its paragraph (e.g. the lead-in to a display). |
| 792 | fn prose t view uchar * to void |
| 793 | txel 2 |
| 794 | txprose t |
| 795 | txb 10 |
| 796 | end |
| 797 | fn paran t view uchar * to void |
| 798 | txel 2 |
| 799 | txs znoind |
| 800 | txprose t |
| 801 | txb 10 |
| 802 | txb 10 |
| 803 | end |
| 804 | fn rawtex t view uchar * to void |
| 805 | txel 2 |
| 806 | txmath t true |
| 807 | txb 10 |
| 808 | txb 10 |
| 809 | end |
| 810 | note Displays (equation, displaymath, align) end without a blank line, so the next para continues the |
| 811 | note same paragraph, as in LaTeX. parbreak ends that paragraph instead. |
| 812 | fn parbreak to void |
| 813 | txel 2 |
| 814 | txb 10 |
| 815 | end |
| 816 | fn txlpush k ulong to void |
| 817 | d = get zst 14 |
| 818 | if ge d 16 |
| 819 | txfail zrlist 0 |
| 820 | end |
| 821 | put zst 14 add d 1 |
| 822 | put zst add 15 d k |
| 823 | end |
| 824 | fn itemize to void |
| 825 | txel 2 |
| 826 | txlpush 1 |
| 827 | txs zbitz |
| 828 | txb 10 |
| 829 | end |
| 830 | fn itemizeo opts view uchar * to void |
| 831 | txel 2 |
| 832 | txopt opts |
| 833 | txlpush 1 |
| 834 | txs zbitz |
| 835 | txb 91 |
| 836 | txs opts |
| 837 | txb 93 |
| 838 | txb 10 |
| 839 | end |
| 840 | fn enumerate to void |
| 841 | txel 2 |
| 842 | txlpush 2 |
| 843 | txs zbenu |
| 844 | txb 10 |
| 845 | end |
| 846 | fn item t view uchar * to void |
| 847 | txel 2 |
| 848 | d = get zst 14 |
| 849 | if eq d 0 |
| 850 | txfail zrlist 0 |
| 851 | end |
| 852 | if eq get zst add 14 d 3 |
| 853 | txfail zrlist 0 |
| 854 | end |
| 855 | txs zitem |
| 856 | txprose t |
| 857 | txb 10 |
| 858 | end |
| 859 | fn endlist to void |
| 860 | txel 2 |
| 861 | d = get zst 14 |
| 862 | if eq d 0 |
| 863 | txfail zrlist 0 |
| 864 | end |
| 865 | k = get zst add 14 d |
| 866 | put zst 14 sub d 1 |
| 867 | if eq k 1 |
| 868 | txs zeitz |
| 869 | end |
| 870 | if eq k 2 |
| 871 | txs zeenu |
| 872 | end |
| 873 | if eq k 3 |
| 874 | txs zbib2 |
| 875 | end |
| 876 | end |
| 877 | fn equation m view uchar * to void |
| 878 | txel 2 |
| 879 | txs zbeq |
| 880 | txmath m false |
| 881 | txs zeeq |
| 882 | end |
| 883 | fn equationl l view uchar * m view uchar * to void |
| 884 | txel 2 |
| 885 | txs zbeq |
| 886 | txlabel l |
| 887 | txb 10 |
| 888 | txmath m false |
| 889 | txs zeeq |
| 890 | end |
| 891 | fn displaymath m view uchar * to void |
| 892 | txel 2 |
| 893 | txs zbdm |
| 894 | txmath m false |
| 895 | txs zedm |
| 896 | end |
| 897 | fn align m view uchar * to void |
| 898 | txel 2 |
| 899 | txs zbal |
| 900 | txmath m true |
| 901 | txs zeal |
| 902 | end |
| 903 | fn figure path view uchar * cap view uchar * l view uchar * to void |
| 904 | txel 2 |
| 905 | txkey path 0 len path false |
| 906 | txs zfig1 |
| 907 | txs path |
| 908 | txs zfig2 |
| 909 | txprose cap |
| 910 | txb 125 |
| 911 | txlabel l |
| 912 | txb 10 |
| 913 | txs zfig3 |
| 914 | end |
| 915 | fn table cap view uchar * l view uchar * spec view uchar * to void |
| 916 | txel 2 |
| 917 | if ne get zst 31 0 |
| 918 | txfail zrlist 0 |
| 919 | end |
| 920 | txs ztab1 |
| 921 | txprose cap |
| 922 | txb 125 |
| 923 | txlabel l |
| 924 | txb 10 |
| 925 | txs ztab2 |
| 926 | txmath spec false |
| 927 | txb 125 |
| 928 | txb 10 |
| 929 | put zst 31 1 |
| 930 | put zst 32 0 |
| 931 | end |
| 932 | fn cell t view uchar * to void |
| 933 | txel 2 |
| 934 | if eq get zst 31 0 |
| 935 | txfail zrlist 0 |
| 936 | end |
| 937 | if gt get zst 32 0 |
| 938 | txs zamp |
| 939 | end |
| 940 | txprose t |
| 941 | put zst 32 add get zst 32 1 |
| 942 | end |
| 943 | fn row to void |
| 944 | txel 2 |
| 945 | if or eq get zst 31 0 eq get zst 32 0 |
| 946 | txfail zrlist 0 |
| 947 | end |
| 948 | txs zrow |
| 949 | put zst 32 0 |
| 950 | end |
| 951 | fn hline to void |
| 952 | txel 2 |
| 953 | if or eq get zst 31 0 ne get zst 32 0 |
| 954 | txfail zrlist 0 |
| 955 | end |
| 956 | txs zhline |
| 957 | end |
| 958 | fn endtable to void |
| 959 | txel 2 |
| 960 | if or eq get zst 31 0 ne get zst 32 0 |
| 961 | txfail zrlist 0 |
| 962 | end |
| 963 | txs ztab3 |
| 964 | put zst 31 0 |
| 965 | end |
| 966 | fn bibbegin to void |
| 967 | txel 2 |
| 968 | txlpush 3 |
| 969 | txs zbib1 |
| 970 | end |
| 971 | fn bibitem k view uchar * t view uchar * to void |
| 972 | txel 2 |
| 973 | d = get zst 14 |
| 974 | if or eq d 0 ne get zst add 14 d 3 |
| 975 | txfail zrlist 0 |
| 976 | end |
| 977 | txkey k 0 len k false |
| 978 | txs zbibi |
| 979 | txs k |
| 980 | txb 125 |
| 981 | txb 32 |
| 982 | txprose t |
| 983 | txb 10 |
| 984 | end |
| 985 | fn bibtex style view uchar * file view uchar * to void |
| 986 | txel 2 |
| 987 | txkey style 0 len style false |
| 988 | txkey file 0 len file false |
| 989 | txs zbsty |
| 990 | txs style |
| 991 | txs zbibf |
| 992 | txs file |
| 993 | txb 125 |
| 994 | txb 10 |
| 995 | end |
| 996 | note ---- listings ---- |
| 997 | fn codelang t view uchar * to void |
| 998 | txel 2 |
| 999 | txkey t 0 len t false |
| 1000 | if gt len t 60 |
| 1001 | txfail zrkey 60 |
| 1002 | end |
| 1003 | for i ulong len t |
| 1004 | put zlng i get t i |
| 1005 | end |
| 1006 | put zst 39 len t |
| 1007 | end |
| 1008 | fn omrefuse rc ulong to void |
| 1009 | txel 2 |
| 1010 | put zst 38 rc |
| 1011 | end |
| 1012 | fn txlhead cap view uchar * l view uchar * omega bool to void |
| 1013 | txkey l 0 len l false |
| 1014 | txs zbls |
| 1015 | if omega |
| 1016 | txs zomega |
| 1017 | else |
| 1018 | n = get zst 39 |
| 1019 | if eq n 0 |
| 1020 | txfail zrlang 0 |
| 1021 | end |
| 1022 | for i ulong n |
| 1023 | txb get zlng i |
| 1024 | end |
| 1025 | end |
| 1026 | txs zcap |
| 1027 | txprose cap |
| 1028 | txs zlbl |
| 1029 | txs l |
| 1030 | txb 93 |
| 1031 | txb 10 |
| 1032 | end |
| 1033 | fn txlbody code view uchar * to void |
| 1034 | txlchk code |
| 1035 | txs code |
| 1036 | n = len code |
| 1037 | if or eq n 0 ne get code sub n 1 10 |
| 1038 | txb 10 |
| 1039 | end |
| 1040 | txs zels |
| 1041 | end |
| 1042 | fn listing cap view uchar * l view uchar * code view uchar * to void |
| 1043 | txel 2 |
| 1044 | txlhead cap l false |
| 1045 | txlbody code |
| 1046 | end |
| 1047 | fn txpath l view uchar * sfx view uchar * to ulong |
| 1048 | n = get zst 37 |
| 1049 | for i ulong n |
| 1050 | put zpb i get zdir i |
| 1051 | end |
| 1052 | put zpb n 47 |
| 1053 | p = add n 1 |
| 1054 | for i ulong len l |
| 1055 | put zpb add p i get l i |
| 1056 | end |
| 1057 | p = add p len l |
| 1058 | for i ulong len sfx |
| 1059 | put zpb add p i get sfx i |
| 1060 | end |
| 1061 | p = add p len sfx |
| 1062 | put zpb p 0 |
| 1063 | ret p |
| 1064 | end |
| 1065 | fn txsave l view uchar * code view uchar * to void |
| 1066 | n = txpath l zsfx |
| 1067 | fd = fopen zpb 0 n 577 420 |
| 1068 | if lt fd 0 |
| 1069 | txfail zrexec 0 |
| 1070 | end |
| 1071 | txw int fd code |
| 1072 | c = fclose int fd |
| 1073 | end |
| 1074 | fn txchild l view uchar * to void |
| 1075 | n = txpath l zsfx |
| 1076 | fd = fopen zpb 0 n 0 0 |
| 1077 | if lt fd 0 |
| 1078 | exit 97 |
| 1079 | end |
| 1080 | r = dupfd int fd 0 0 |
| 1081 | n = txpath l zsfxb |
| 1082 | fd = fopen zpb 0 n 577 493 |
| 1083 | r = dupfd int fd 1 0 |
| 1084 | n = txpath l zsfxe |
| 1085 | fd = fopen zpb 0 n 577 420 |
| 1086 | r = dupfd int fd 2 0 |
| 1087 | m = get zst 36 |
| 1088 | put zpb 0 1 |
| 1089 | put zpb 1 0 |
| 1090 | put zpb 2 uchar and m 255 |
| 1091 | put zpb 3 uchar shr m 8 |
| 1092 | for i ulong m |
| 1093 | put zpb add 4 i get zomc i |
| 1094 | end |
| 1095 | r = execprog zpb 0 add 4 m |
| 1096 | exit 97 |
| 1097 | end |
| 1098 | fn txrun l view uchar * to ulong |
| 1099 | pid = fork |
| 1100 | if lt pid 0 |
| 1101 | txfail zrexec 0 |
| 1102 | end |
| 1103 | if eq pid 0 |
| 1104 | txchild l |
| 1105 | end |
| 1106 | r = waitpid int pid zws 0 4 0 |
| 1107 | if lt r 0 |
| 1108 | txfail zrexec 0 |
| 1109 | end |
| 1110 | s = get zws 0 |
| 1111 | if ne and s 127 0 |
| 1112 | ret add 256 ulong and s 127 |
| 1113 | end |
| 1114 | ret ulong get zws 1 |
| 1115 | end |
| 1116 | fn txcheck l view uchar * code view uchar * to void |
| 1117 | w = get zst 38 |
| 1118 | txsave l code |
| 1119 | g = txrun l |
| 1120 | txw 1 zlst1 |
| 1121 | txw 1 l |
| 1122 | txw 1 zlst2 |
| 1123 | txwn 1 w |
| 1124 | txw 1 zlst3 |
| 1125 | txwn 1 g |
| 1126 | if eq g w |
| 1127 | txw 1 zpass |
| 1128 | put zst 33 add get zst 33 1 |
| 1129 | else |
| 1130 | txw 1 zfailw |
| 1131 | put zst 34 add get zst 34 1 |
| 1132 | end |
| 1133 | end |
| 1134 | fn omlisting cap view uchar * l view uchar * code view uchar * to void |
| 1135 | txel 2 |
| 1136 | txlhead cap l true |
| 1137 | txlbody code |
| 1138 | if eq get zst 2 1 |
| 1139 | txcheck l code |
| 1140 | end |
| 1141 | put zst 38 0 |
| 1142 | end |
| 1143 | fn omfragment cap view uchar * l view uchar * code view uchar * to void |
| 1144 | txel 2 |
| 1145 | txlhead cap l true |
| 1146 | txlbody code |
| 1147 | if eq get zst 2 1 |
| 1148 | txw 1 zlst1 |
| 1149 | txw 1 l |
| 1150 | txw 1 zfragw |
| 1151 | end |
| 1152 | put zst 35 add get zst 35 1 |
| 1153 | end |
| 1154 | note ---- end of document ---- |
| 1155 | fn enddoc to void |
| 1156 | txel 2 |
| 1157 | if or ne get zst 14 0 ne get zst 31 0 |
| 1158 | txfail zrclose 0 |
| 1159 | end |
| 1160 | put zst 3 3 |
| 1161 | txs zendd |
| 1162 | if eq get zst 2 0 |
| 1163 | txflush |
| 1164 | ret |
| 1165 | end |
| 1166 | txw 1 zsum1 |
| 1167 | txwn 1 get zst 33 |
| 1168 | txw 1 zsum2 |
| 1169 | txwn 1 get zst 34 |
| 1170 | txw 1 zsum3 |
| 1171 | txwn 1 get zst 35 |
| 1172 | txw 1 zmnl |
| 1173 | if ne get zst 34 0 |
| 1174 | exit 113 |
| 1175 | end |
| 1176 | end |