nx_mathml.nx source
↩ module page · 50 lines · 1897 B
1// nx_mathml.nx -- CLI for the sovereign LaTeX-subset to MathML typesetter.
2//
3// usage: nx_mathml <latex> [inline]
4// Emits a MathML subtree on stdout, ready to paste into a page, a nx_paper_native .MATH row, or a
5// document in the unified portal. Default is DISPLAY (block); pass a second argument to get inline.
6// A formula this subset cannot read is REFUSED BY NAME with the offending command echoed, and nothing
7// is written -- a half-converted formula is wrong in a way the reader cannot see.
8//
9// license_tier: ORIGINAL
10import "syscalls.nx"
11import "nx_mathml_lib.nx"
12
13func nm_slen(s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { i = i + 1 } return i }
14func nm_puts(s: *u8) -> i64 { sys_write(1, s, nm_slen(s)); return 0 }
15
16func main(argc: i64, argv: *i64) -> i64 {
17 if argc < 2 {
18 nm_puts("usage: nx_mathml <latex> [inline]\n" as *u8)
19 nm_puts(" emits a MathML subtree; default display=block, any second argument selects inline\n" as *u8)
20 nm_puts(" supported: numbers, identifiers, operators, ^ _ groups, frac, sqrt, greek, relations, sum/int, sin/cos/ln/exp\n" as *u8)
21 sys_exit(2)
22 return 2
23 }
24 let src: *u8 = argv[1] as *u8
25 let n: i64 = nm_slen(src)
26 var display: i64 = 1
27 if argc >= 3 { display = 0 }
28
29 let cap: i64 = lm_out_cap_for(n)
30 let out: *u8 = sys_mmap(cap)
31 let bo: *i64 = (sys_mmap(8)) as *i64
32 let bl: *i64 = (sys_mmap(8)) as *i64
33
34 let r: i64 = lm_to_mathml(src, n, out, cap, display, bo, bl)
35 if r < 0 {
36 nm_puts("NX-MATHML REFUSED rule=" as *u8)
37 nm_puts(lm_err_name(r))
38 if bl[0] > 0 {
39 nm_puts(" command=" as *u8)
40 sys_write(1, ((src as i64) + bo[0]) as *u8, bl[0])
41 }
42 nm_puts("\n" as *u8)
43 sys_exit(1)
44 return 1
45 }
46 sys_write(1, out, r)
47 nm_puts("\n" as *u8)
48 sys_exit(0)
49 return 0
50}