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 / seed / ng / front / prune.om

1248 lines · 21426 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/front/prune.om: structural line pruning of an Omega unit (ng-front: the machine-code paths of
3note omc.om are removed from the front end with it, DECISIONS D1b/D8 and the queen's front.om scope ruling).
4note Handwritten seeded Omega, program mode, Linux AArch64, built by the pinned seed/out/omc.
5note prune SCRIPT < IN.om > OUT.om
6note An Omega unit is line structured: a line whose first word is fn, if, while or for opens a block, `else`
7note splits an if, `end` closes. SCRIPT lines (# lines and blank lines skipped), applied in order:
8note false COND every `if COND` line (COND the exact rest of the line): the if, its then-part and its end
9note go; an else-part stays in place (its else line goes). Use for a condition that is false
10note in every unit the output accepts.
11note true COND every `if COND` line: the if line goes, and the else-part with its else line; then stays.
12note call NAME every line whose first word is NAME (a call statement) goes.
13note dead ROOT from function ROOT, every fn and global no kept line of a reachable function names goes,
14note with the note lines right above it.
15note Every false, true and call line must match at least once (a stale script is refused). stderr gets one
16note line per script line: the command and how many lines it removed. Exit 0, or 1 on any refusal.
17
18text msg "prune: usage: prune SCRIPT < IN > OUT\nprune: script unreadable, malformed or too large\nprune: input too large or too many lines\nprune: unbalanced block at line \nprune: script line matched nothing: \nprune: dead root is not a function\nprune: output write failed\nprune: too many names\n removed \nprune: sink is not a kept name: \nprune: sink read at line \n: \nprune: sink function \n"
19text kw "if\nwhile\nfor\nfn\nend\nelse\nnote\nglobal\nfalse\ntrue\ncall\ndead\nput\nexit\nwrite\nread\nsink\npure\nsinkcheck\ntidy\nret\n"
20
21global inp uchar 8388608
22global inn ulong 1
23global lo ulong 65536
24global lend ulong 65536
25global kp uchar 65536
26global lk uchar 65536
27global td uchar 65536
28global nl ulong 1
29global cm uchar 1048576
30global cmn ulong 1
31global out uchar 8388608
32global otn ulong 1
33global pb uchar 4096
34global wpos ulong 2
35global me ulong 2
36global no ulong 8192
37global nln ulong 8192
38global nk uchar 8192
39global nd ulong 8192
40global nend ulong 8192
41global nlv uchar 8192
42global nn ulong 1
43global ht ulong 16384
44global wk ulong 8192
45global nrm ulong 1
46global nsk uchar 8192
47global npu uchar 8192
48global sko ulong 512
49global skl ulong 512
50global nsl ulong 1
51global tno ulong 256
52global tnl ulong 256
53global ntn ulong 1
54global lh ulong 2
55global tnot ulong 1
56
57note ---- messages --------------------------------------------------------------------------------------
58fn seg id ulong to void
59k = ulong 1
60s = ulong 0
61for i ulong len msg
62if eq get msg i 10
63if eq k id
64put wpos 0 s
65put wpos 1 sub i s
66ret
67end
68k = add k 1
69s = add i 1
70end
71end
72exit 99
73end
74fn say id ulong to void
75seg id
76r = write 2 msg get wpos 0 get wpos 1
77end
78fn nl2 to void
79put pb 0 10
80r = write 2 pb 0 1
81end
82fn num v ulong to void
83x = v
84n = ulong 0
85while ge x 10 bound 32
86put pb n uchar add 48 rem x 10
87x = div x 10
88n = add n 1
89end
90put pb n uchar add 48 x
91n = add n 1
92for i ulong n
93r = write 2 pb sub sub n i 1 1
94end
95end
96
97note ---- words ---------------------------------------------------------------------------------------------
98fn blank c uchar to bool
99ret or eq c 32 eq c 9
100end
101note the first word of inp[o .. e) into wpos (length 0 for none)
102fn firstw o ulong e ulong to void
103i = o
104while and lt i e blank get inp i bound 8388608
105i = add i 1
106end
107j = i
108while and lt j e not blank get inp j bound 8388608
109j = add j 1
110end
111put wpos 0 i
112put wpos 1 sub j i
113end
114note inp[o .. o+n) is word id of kw
115fn iskw o ulong n ulong id ulong to bool
116k = ulong 1
117s = ulong 0
118for i ulong len kw
119if eq get kw i 10
120if eq k id
121if ne n sub i s
122ret false
123end
124for j ulong n
125if ne get inp add o j get kw add s j
126ret false
127end
128end
129ret true
130end
131k = add k 1
132s = add i 1
133end
134end
135ret false
136end
137note cm[o .. o+n) is word id of kw
138fn cmkw o ulong n ulong id ulong to bool
139k = ulong 1
140s = ulong 0
141for i ulong len kw
142if eq get kw i 10
143if eq k id
144if ne n sub i s
145ret false
146end
147for j ulong n
148if ne get cm add o j get kw add s j
149ret false
150end
151end
152ret true
153end
154k = add k 1
155s = add i 1
156end
157end
158ret false
159end
160note the kind of line i: 1 opens a block, 2 else, 3 end, 0 other
161fn lkind i ulong to ulong
162firstw get lo i get lend i
163o = get wpos 0
164n = get wpos 1
165if or iskw o n 1 or iskw o n 2 or iskw o n 3 iskw o n 4
166ret 1
167end
168if iskw o n 6
169ret 2
170end
171if iskw o n 5
172ret 3
173end
174ret 0
175end
176
177note ---- input and lines ----------------------------------------------------------------------------------
178fn loadinp to bool
179n = ulong 0
180go = true
181while go bound 100000
182q = read 0 inp n sub len inp n
183if le q 0
184go = false
185else
186n = add n ulong q
187if ge n len inp
188go = false
189end
190end
191end
192put inn 0 n
193if ge n len inp
194ret false
195end
196k = ulong 0
197s = ulong 0
198for i ulong n bound 8388608
199if eq get inp i 10
200if ge k len lo
201ret false
202end
203put lo k s
204put lend k i
205k = add k 1
206s = add i 1
207end
208end
209if lt s n
210if ge k len lo
211ret false
212end
213put lo k s
214put lend k n
215k = add k 1
216end
217put nl 0 k
218for i ulong k bound 65536
219put kp i 1
220put lk i uchar lkind i
221end
222ret true
223end
224note the else line (or nl) and the end line of the block opened at line i, into me
225fn matchend i ulong to bool
226d = ulong 0
227put me 0 get nl 0
228for j ulong sub get nl 0 add i 1 bound 65536
229x = add add i 1 j
230k = get lk x
231if eq k 1
232d = add d 1
233end
234if eq k 2
235if eq d 0
236put me 0 x
237end
238end
239if eq k 3
240if eq d 0
241put me 1 x
242ret true
243end
244d = sub d 1
245end
246end
247ret false
248end
249
250note ---- the script ----------------------------------------------------------------------------------------
251fn loadcm to bool
252r = argread 1 pb 0 4095
253if le r 0
254ret false
255end
256fd = fopen pb 0 ulong r 524288 0
257if lt fd 0
258ret false
259end
260n = ulong 0
261go = true
262while go bound 100000
263q = read int fd cm n sub len cm n
264if le q 0
265go = false
266else
267n = add n ulong q
268if ge n len cm
269go = false
270end
271end
272end
273q = fclose int fd
274put cmn 0 n
275ret lt n len cm
276end
277note line i is `while ` followed by exactly cm[o .. o+n) (false removes such a loop whole)
278fn whis i ulong o ulong n ulong to bool
279s = get lo i
280if ne sub get lend i s add n 6
281ret false
282end
283firstw s get lend i
284if not iskw get wpos 0 get wpos 1 2
285ret false
286end
287for j ulong n
288if ne get inp add add s 6 j get cm add o j
289ret false
290end
291end
292ret true
293end
294note line i is `if ` followed by exactly cm[o .. o+n)
295fn ifis i ulong o ulong n ulong to bool
296s = get lo i
297if ne sub get lend i s add n 3
298ret false
299end
300if ne get inp s 105
301ret false
302end
303if or ne get inp add s 1 102 ne get inp add s 2 32
304ret false
305end
306for j ulong n
307if ne get inp add add s 3 j get cm add o j
308ret false
309end
310end
311ret true
312end
313fn drop i ulong to void
314if ne get kp i 0
315put kp i 0
316put nrm 0 add get nrm 0 1
317end
318end
319fn dropr a ulong b ulong to void
320for j ulong add sub b a 1 bound 65536
321drop add a j
322end
323end
324note false (t = false) or true (t = true) for condition cm[o .. o+n); lines matched
325fn branch o ulong n ulong t bool to ulong
326c = ulong 0
327for i ulong get nl 0 bound 65536
328if eq get lk i 1
329if or ifis i o n whis i o n
330if not matchend i
331say 4
332num add i 1
333nl2
334exit 1
335end
336c = add c 1
337branch1 i t
338end
339end
340end
341ret c
342end
343fn branch1 i ulong t bool to void
344el = get me 0
345en = get me 1
346if t
347drop i
348if lt el get nl 0
349dropr el en
350else
351drop en
352end
353else
354if lt el get nl 0
355dropr i el
356drop en
357else
358dropr i en
359end
360end
361end
362fn calls o ulong n ulong to ulong
363c = ulong 0
364for i ulong get nl 0 bound 65536
365if eq get kp i 1
366firstw get lo i get lend i
367if eq get wpos 1 n
368if cmsame get wpos 0 o n
369drop i
370c = add c 1
371end
372end
373end
374end
375ret c
376end
377fn cmsame a ulong o ulong n ulong to bool
378for j ulong n
379if ne get inp add a j get cm add o j
380ret false
381end
382end
383ret true
384end
385
386note ---- names and reachability ------------------------------------------------------------------------------
387fn hb o ulong n ulong to ulong
388h = ulong 5381
389for i ulong n
390h = xor mul h 33 ulong get inp add o i
391end
392ret and h 16383
393end
394fn nsame k ulong o ulong n ulong to bool
395if ne get nln k n
396ret false
397end
398a = get no k
399for j ulong n
400if ne get inp add a j get inp add o j
401ret false
402end
403end
404ret true
405end
406note 1 + the name entry spelled inp[o .. o+n), or 0
407fn nfind o ulong n ulong to ulong
408h = hb o n
409for t ulong 16384
410s = and add h t 16383
411v = get ht s
412if eq v 0
413ret 0
414end
415if nsame sub v 1 o n
416ret v
417end
418end
419ret 0
420end
421fn nadd o ulong n ulong kind ulong d ulong to bool
422k = get nn 0
423if ge k len no
424ret false
425end
426put no k o
427put nln k n
428put nk k uchar kind
429put nd k d
430put nend k d
431put nlv k 0
432put nsk k 0
433put npu k 0
434h = hb o n
435for t ulong 16384
436s = and add h t 16383
437if eq get ht s 0
438put ht s add k 1
439put nn 0 add k 1
440ret true
441end
442end
443ret false
444end
445note the name declared by kept line i (fn or global, at top level), as a table entry
446fn declname i ulong to bool
447firstw get lo i get lend i
448o = get wpos 0
449n = get wpos 1
450kind = ulong 0
451if iskw o n 4
452kind = ulong 1
453end
454if iskw o n 8
455kind = ulong 2
456end
457if eq kind 0
458ret true
459end
460firstw add o n get lend i
461if not nadd get wpos 0 get wpos 1 kind i
462ret false
463end
464if eq kind 1
465if not matchend i
466say 4
467num add i 1
468nl2
469exit 1
470end
471put nend sub get nn 0 1 get me 1
472end
473ret true
474end
475note mark name k live and queue it
476fn live k ulong qn ulong to ulong
477if ne get nlv k 0
478ret qn
479end
480put nlv k 1
481put wk qn k
482ret add qn 1
483end
484note every word of kept line i that names an entry is marked live
485fn scanline i ulong qn ulong to ulong
486q = qn
487e = get lend i
488p = get lo i
489firstw p e
490if iskw get wpos 0 get wpos 1 7
491ret q
492end
493while lt p e bound 65536
494firstw p e
495if eq get wpos 1 0
496ret q
497end
498v = nfind get wpos 0 get wpos 1
499if ne v 0
500q = live sub v 1 q
501end
502p = add get wpos 0 get wpos 1
503end
504ret q
505end
506note 1 + the fn entry named cm[o .. o+n), or 0
507fn findroot o ulong n ulong to ulong
508for k ulong get nn 0
509if and eq get nk k 1 eq get nln k n
510if rootis k o n
511ret add k 1
512end
513end
514end
515ret 0
516end
517note the name table of the kept lines; sink globals are marked from the sink list
518fn buildnames to void
519put nn 0 0
520for i ulong len ht
521put ht i 0
522end
523for i ulong get nl 0 bound 65536
524if eq get kp i 1
525if not declname i
526say 8
527nl2
528exit 1
529end
530end
531end
532for s ulong get nsl 0
533v = nfindcm get sko s get skl s
534if ne v 0
535put nsk sub v 1 1
536end
537end
538end
539note 1 + the name entry spelled cm[o .. o+n), or 0
540fn nfindcm o ulong n ulong to ulong
541for k ulong get nn 0
542if eq get nln k n
543if rootis k o n
544ret add k 1
545end
546end
547end
548ret 0
549end
550note remember sink global cm[o .. o+n)
551fn sinkadd o ulong n ulong to ulong
552k = get nsl 0
553if ge k len sko
554say 8
555nl2
556exit 1
557end
558put sko k o
559put skl k n
560put nsl 0 add k 1
561ret 1
562end
563note the word after inp[o .. o+n) on line i, as 1 + its name entry (0 when it names nothing)
564fn nextname o ulong n ulong i ulong to ulong
565firstw add o n get lend i
566if eq get wpos 1 0
567ret 0
568end
569ret nfind get wpos 0 get wpos 1
570end
571note line i stores to a sink global: `put G ...` with G a sink
572fn sinkput i ulong to bool
573firstw get lo i get lend i
574o = get wpos 0
575n = get wpos 1
576if not iskw o n 13
577ret false
578end
579v = nextname o n i
580if eq v 0
581ret false
582end
583ret ne get nsk sub v 1 0
584end
585note line i may have an effect beyond the dropped sinks: a store to a global that is not a sink, a host word,
586note exit, or a call of a function not (yet) known pure
587fn lineeff i ulong to bool
588e = get lend i
589firstw get lo i e
590o = get wpos 0
591n = get wpos 1
592if iskw o n 7
593ret false
594end
595if iskw o n 13
596v = nextname o n i
597if ne v 0
598if and eq get nk sub v 1 2 eq get nsk sub v 1 0
599ret true
600end
601end
602end
603ret toksff get lo i e
604end
605note name entry v (1-based, 0 none) is a function not known pure
606fn impure v ulong to bool
607if eq v 0
608ret false
609end
610k = sub v 1
611ret and eq get nk k 1 eq get npu k 0
612end
613note some word of inp[p .. e) is exit, write or read, or names a function that is not pure
614fn toksff p ulong e ulong to bool
615q = p
616while lt q e bound 65536
617firstw q e
618o = get wpos 0
619n = get wpos 1
620if eq n 0
621ret false
622end
623if or iskw o n 14 or iskw o n 15 iskw o n 16
624ret true
625end
626if impure nfind o n
627ret true
628end
629q = add o n
630end
631ret false
632end
633fn fneff k ulong to bool
634a = add get nd k 1
635b = get nend k
636while lt a b bound 65536
637if eq get kp a 1
638if lineeff a
639ret true
640end
641end
642a = add a 1
643end
644ret false
645end
646note drop the stores to sinks, find the pure functions, drop every call statement of one; lines removed
647fn pure to ulong
648buildnames
649before = get nrm 0
650for i ulong get nl 0 bound 65536
651if eq get kp i 1
652if sinkput i
653drop i
654end
655end
656end
657purity
658for i ulong get nl 0 bound 65536
659if eq get kp i 1
660purecall i
661end
662end
663sinkfns
664for k ulong get nn 0
665if eq get nk k 1
666taintdrop k
667end
668end
669ret add sub get nrm 0 before 1
670end
671note npu: 1 for every function with no effect (to a fixpoint over the calls)
672fn purity to void
673for k ulong get nn 0
674put npu k 0
675if eq get nk k 1
676put npu k 1
677end
678end
679go = true
680while go bound 100000
681go = false
682for k ulong get nn 0
683if ne get npu k 0
684if fneff k
685put npu k 0
686go = true
687end
688end
689end
690end
691end
692note tidy: drop if/while/for blocks left with nothing kept inside, and assignments to locals that no kept
693note line reads, when they call nothing impure; to a fixpoint; lines removed
694fn tidy to ulong
695buildnames
696purity
697before = get nrm 0
698go = true
699while go bound 1000
700go = false
701for k ulong get nn 0
702if eq get nk k 1
703if tidyfn k
704go = true
705end
706end
707end
708end
709ret add sub get nrm 0 before 1
710end
711fn tidyfn k ulong to bool
712ch = false
713a = add get nd k 1
714b = get nend k
715while lt a b bound 65536
716if eq get kp a 1
717if tidy1 a k
718ch = true
719end
720end
721a = add a 1
722end
723ret ch
724end
725fn tidy1 a ulong k ulong to bool
726if eq get lk a 1
727ret emptyblk a
728end
729r = asgrhs a
730if eq r 0
731ret false
732end
733if toksff r get lend a
734ret false
735end
736if readin get lh 0 get lh 1 k
737ret false
738end
739drop a
740ret true
741end
742note the if/while/for block opened at kept line a has no kept line inside but else and notes, and its
743note condition calls nothing impure: drop it whole
744fn emptyblk a ulong to bool
745firstw get lo a get lend a
746o = get wpos 0
747n = get wpos 1
748if not or iskw o n 1 or iskw o n 2 iskw o n 3
749ret false
750end
751if toksff add o n get lend a
752ret false
753end
754if not matchend a
755ret false
756end
757en = get me 1
758x = add a 1
759while lt x en bound 65536
760if eq get kp x 1
761if ne get lk x 2
762if not notel x
763ret false
764end
765end
766end
767x = add x 1
768end
769dropr a en
770ret true
771end
772fn notel x ulong to bool
773firstw get lo x get lend x
774ret iskw get wpos 0 get wpos 1 7
775end
776note some kept line of function k reads the local inp[o .. o+n): a word of it past the target of an
777note assignment
778fn readin o ulong n ulong k ulong to bool
779x = get nd k
780e = get nend k
781while le x e bound 65536
782if eq get kp x 1
783if rdname x o n
784ret true
785end
786end
787x = add x 1
788end
789ret false
790end
791fn rdname x ulong o ulong n ulong to bool
792q = asgrhs x
793if eq q 0
794q = get lo x
795end
796e = get lend x
797while lt q e bound 65536
798firstw q e
799p = get wpos 0
800m = get wpos 1
801if eq m 0
802ret false
803end
804if eq m n
805if inpsame p o n
806ret true
807end
808end
809q = add p m
810end
811ret false
812end
813note mark every function that returns a value read from a sink as a sink itself, to a fixpoint
814fn sinkfns to void
815go = true
816while go bound 64
817go = false
818for k ulong get nn 0
819if sinknew k
820put nsk k 1
821go = true
822end
823end
824end
825end
826fn sinknew k ulong to bool
827if or ne get nk k 1 ne get nsk k 0
828ret false
829end
830taintfn k
831a = add get nd k 1
832b = get nend k
833while lt a b bound 65536
834if retsink a
835ret true
836end
837a = add a 1
838end
839ret false
840end
841note kept line a is `ret EXPR` with EXPR reading a sink
842fn retsink a ulong to bool
843if eq get kp a 0
844ret false
845end
846firstw get lo a get lend a
847if not iskw get wpos 0 get wpos 1 21
848ret false
849end
850ret rdsink add get wpos 0 3 get lend a
851end
852fn purecall i ulong to void
853firstw get lo i get lend i
854v = nfind get wpos 0 get wpos 1
855if ne v 0
856if and eq get nk sub v 1 1 ne get npu sub v 1 0
857drop i
858end
859end
860end
861note every kept line of a function that reads a sink global or a local assigned from one goes to stderr;
862note their count (the script refuses when not 0)
863fn sinkchk to ulong
864buildnames
865sinkfns
866c = ulong 0
867for k ulong get nn 0
868if eq get nk k 1
869if ne get nsk k 0
870say 13
871r = write 2 inp get no k get nln k
872nl2
873end
874c = add c sinkfn k
875end
876end
877ret c
878end
879fn sinkfn k ulong to ulong
880taintfn k
881c = ulong 0
882a = add get nd k 1
883b = get nend k
884while lt a b bound 65536
885if eq get kp a 1
886if sinkread a
887c = add c 1
888say 11
889num add a 1
890say 12
891showline a
892nl2
893end
894end
895a = add a 1
896end
897ret c
898end
899fn showline i ulong to void
900s = get lo i
901r = write 2 inp s sub get lend i s
902end
903fn sinkread i ulong to bool
904e = get lend i
905firstw get lo i e
906if iskw get wpos 0 get wpos 1 7
907ret false
908end
909ret rdsink get lo i e
910end
911fn inpsame a ulong o ulong n ulong to bool
912for j ulong n
913if ne get inp add a j get inp add o j
914ret false
915end
916end
917ret true
918end
919note inp[o .. o+n) is a tainted local of the current function
920fn tainted o ulong n ulong to bool
921for t ulong get ntn 0
922if eq get tnl t n
923if inpsame get tno t o n
924ret true
925end
926end
927end
928ret false
929end
930fn issink v ulong to bool
931if eq v 0
932ret false
933end
934ret ne get nsk sub v 1 0
935end
936note inp[p .. e) has a word that is a sink global or function, or (unless tnot) a tainted local
937fn rdsink p ulong e ulong to bool
938q = p
939while lt q e bound 65536
940firstw q e
941o = get wpos 0
942n = get wpos 1
943if eq n 0
944ret false
945end
946if issink nfind o n
947ret true
948end
949if eq get tnot 0 0
950if tainted o n
951ret true
952end
953end
954q = add o n
955end
956ret false
957end
958note line i is `NAME = ...` or `put NAME ...` with NAME not a fn or global: NAME into lh and the offset of
959note the rest of the line, or 0
960fn asgrhs i ulong to ulong
961e = get lend i
962firstw get lo i e
963o = get wpos 0
964n = get wpos 1
965if eq n 0
966ret 0
967end
968isput = iskw o n 13
969firstw add o n e
970p = get wpos 0
971m = get wpos 1
972if isput
973if or eq m 0 ne nfind p m 0
974ret 0
975end
976put lh 0 p
977put lh 1 m
978ret add p m
979end
980if or ne m 1 ne get inp p 61
981ret 0
982end
983put lh 0 o
984put lh 1 n
985ret add p 1
986end
987note taint the locals of function k assigned from a sink or a tainted local (to a fixpoint)
988fn taintfn k ulong to void
989put ntn 0 0
990go = true
991while go bound 64
992go = false
993a = add get nd k 1
994b = get nend k
995while lt a b bound 65536
996if taint1 a
997go = true
998end
999a = add a 1
1000end
1001end
1002end
1003fn taint1 a ulong to bool
1004if and eq get kp a 0 eq get td a 0
1005ret false
1006end
1007r = asgrhs a
1008if eq r 0
1009ret false
1010end
1011o = get lh 0
1012n = get lh 1
1013if tainted o n
1014ret false
1015end
1016if not rdsink r get lend a
1017ret false
1018end
1019t = get ntn 0
1020if ge t len tno
1021ret false
1022end
1023put tno t o
1024put tnl t n
1025put ntn 0 add t 1
1026ret true
1027end
1028note drop the assignments to tainted locals of function k whose right-hand side reads a sink global or
1029note function directly and calls nothing impure (a use of the dropped local then fails to compile: by hand)
1030fn taintdrop k ulong to void
1031taintfn k
1032a = add get nd k 1
1033b = get nend k
1034while lt a b bound 65536
1035if eq get kp a 1
1036tdrop1 a
1037end
1038a = add a 1
1039end
1040end
1041fn tdrop1 a ulong to void
1042r = asgrhs a
1043if eq r 0
1044ret
1045end
1046put tnot 0 1
1047s = rdsink r get lend a
1048put tnot 0 0
1049if not s
1050ret
1051end
1052if not toksff r get lend a
1053drop a
1054put td a 1
1055end
1056end
1057fn dead o ulong n ulong to ulong
1058buildnames
1059root = findroot o n
1060if eq root 0
1061say 6
1062nl2
1063exit 1
1064end
1065q = live sub root 1 0
1066h = ulong 0
1067while lt h q bound 8192
1068k = get wk h
1069if eq get nk k 1
1070q = scanfn k q
1071end
1072h = add h 1
1073end
1074before = get nrm 0
1075for k ulong get nn 0
1076if eq get nlv k 0
1077dropname k
1078end
1079end
1080ret sub get nrm 0 before
1081end
1082fn rootis k ulong o ulong n ulong to bool
1083a = get no k
1084for j ulong n
1085if ne get inp add a j get cm add o j
1086ret false
1087end
1088end
1089ret true
1090end
1091fn scanfn k ulong qn ulong to ulong
1092q = qn
1093a = add get nd k 1
1094b = get nend k
1095while lt a b bound 65536
1096if eq get kp a 1
1097q = scanline a q
1098end
1099a = add a 1
1100end
1101ret q
1102end
1103note drop entry k's declaration (a fn through its end) and the note lines right above it
1104fn dropname k ulong to void
1105a = get nd k
1106dropr a get nend k
1107go = true
1108while and go gt a 0 bound 65536
1109a = sub a 1
1110firstw get lo a get lend a
1111if and eq get kp a 1 iskw get wpos 0 get wpos 1 7
1112drop a
1113else
1114go = false
1115end
1116end
1117end
1118
1119note ---- running the script and the output -------------------------------------------------------------------
1120fn cmblank i ulong to bool
1121c = get cm i
1122ret or eq c 32 eq c 9
1123end
1124fn runcm to bool
1125n = get cmn 0
1126p = ulong 0
1127while lt p n bound 1048576
1128e = p
1129while and lt e n ne get cm e 10 bound 1048576
1130e = add e 1
1131end
1132if and lt p e ne get cm p 35
1133if not cmline p e
1134ret false
1135end
1136end
1137p = add e 1
1138end
1139ret true
1140end
1141fn cmline p ulong e ulong to bool
1142j = p
1143while and lt j e not cmblank j bound 4096
1144j = add j 1
1145end
1146a = e
1147if lt j e
1148a = add j 1
1149end
1150n = sub e a
1151put nrm 0 0
1152c = ulong 0
1153if cmkw p sub j p 9
1154c = branch a n false
1155end
1156if cmkw p sub j p 10
1157c = branch a n true
1158end
1159if cmkw p sub j p 11
1160c = calls a n
1161end
1162if cmkw p sub j p 12
1163c = add dead a n 1
1164end
1165if cmkw p sub j p 17
1166c = sinkadd a n
1167end
1168if cmkw p sub j p 18
1169c = pure
1170end
1171if cmkw p sub j p 20
1172c = tidy
1173end
1174if cmkw p sub j p 19
1175if ne sinkchk 0
1176ret false
1177end
1178c = ulong 1
1179end
1180r = write 2 cm p sub e p
1181say 9
1182num get nrm 0
1183nl2
1184if eq c 0
1185say 5
1186r = write 2 cm p sub e p
1187nl2
1188ret false
1189end
1190ret true
1191end
1192fn obyte b uchar to void
1193n = get otn 0
1194if lt n len out
1195put out n b
1196end
1197put otn 0 add n 1
1198end
1199fn emitout to bool
1200for i ulong get nl 0 bound 65536
1201if eq get kp i 1
1202s = get lo i
1203for j ulong sub get lend i s bound 65536
1204obyte get inp add s j
1205end
1206obyte 10
1207end
1208end
1209n = get otn 0
1210if gt n len out
1211ret false
1212end
1213off = ulong 0
1214while lt off n bound 100000
1215r = write 1 out off sub n off
1216if le r 0
1217ret false
1218end
1219off = add off ulong r
1220end
1221ret true
1222end
1223fn main to int
1224if ne argcnt 2
1225say 1
1226nl2
1227ret 1
1228end
1229if not loadcm
1230say 2
1231nl2
1232ret 1
1233end
1234if not loadinp
1235say 3
1236nl2
1237ret 1
1238end
1239if not runcm
1240ret 1
1241end
1242if not emitout
1243say 7
1244nl2
1245ret 1
1246end
1247ret 0
1248end