@@ -1,960 +1,2 @@
1−/+ *interlist
21|%
3−:: Name
4−::
5−+$ name @t
6−:: AST
7−::
8−+$ ast
9− $%
10− [%blok blok]
11− ==
12−++ print
13− |= [=ast]
14− ^- tape
15− ?- -.ast
16− %blok (print-blok +.ast)
17− ==
18−++ apex
19− %^ tnee %apex ast
20− %+ ifix [ws ws]
21− ;~ pose
22− (cook |=(=blok [%blok blok]) parse-blok)
23− ==
24−:: Expr
25−::
26−+$ expr
27− $%
28− [%string string]
29− [%binop expr binop expr]
30− [%prefix-expr prefix-expr]
31− [%num numeral]
32− [%table table]
33− [%true ~]
34− [%false ~]
35− [%nil ~]
36− ==
37−:: The expressions that are separated by binary operators
38−::
39−++ parse-atomic-expr
40− %^ tnee %parse-atomic-expr expr
41− ;~ pose
42− (cold [%nil ~] (jest 'nil'))
43− ::
44− (cold [%true ~] (jest 'true'))
45− ::
46− (cold [%false ~] (jest 'false'))
47− ::
48− %+ cook
49− |=(=numeral [%num numeral])
50− parse-numeral
51− ::
52− %+ cook
53− |=(=prefix-expr [%prefix-expr prefix-expr])
54− (knee *prefix-expr |.(parse-prefix-expr))
55− ::
56− %+ cook
57− |=(=table [%table table])
58− parse-table
59− %+ cook
60− |=(=string [%string string])
61− parse-string
62− ==
63−:: Parse expressions separated by binary operators
64−::
65−++ parse-expr-list
66− %^ tnee %parse-expr-list (interlist binop expr)
67− %+ parse-interlist (ifix [ws ws] parse-binop) parse-atomic-expr
68−:: TODO: This is mock and should take operator priority into account.
69−::
70−++ process-expr-list
71− |= l=(interlist binop expr)
72− ^- expr
73− ?- -.l
74− %single val.l
75− %double [%binop val.l sep.l (process-expr-list tail.l)]
76− ==
77−++ parse-expr
78− %^ tnee %parse-expr expr
79− %+ cook process-expr-list parse-expr-list
80−++ parse-parened-expr
81− %+ cook
82− |=([=expr] [%expr expr])
83− (ifix [(just '(') (just ')')] (knee *expr |.(parse-expr)))
84−++ print-expr
85− |= [=expr]
86− ^- tape
87− ?- -.expr
88− %prefix-expr (print-prefix-expr +.expr)
89− %nil "nil"
90− %true "true"
91− %false "false"
92− %binop
93− %- zing
94− :~
95− (print-expr +<.expr)
96− " "
97− (print-binop +>-.expr)
98− " "
99− (print-expr +>+.expr)
100− ==
101− %num (print-numeral +.expr)
102− %table (print-table +.expr)
103− %string (print-string +.expr)
104− ==
105−:: string
106−::
107−+$ string @t
108−++ print-string
109− |= =string
110− %- zing
111− :~
112− "\""
113− (trip string)
114− "\""
115− ==
116−++ parse-string
117− %^ tnee %parse-string string
118− =>
119− |%
120− ++ parse-string
121− |= quot=char
122− =/ quot (just quot)
123− %+ cook
124− |=([* =tape *] (crip tape))
125− ;~ plug
126− quot
127− (star ;~(less quot next))
128− quot
129− ==
130− --
131− ;~ pose
132− (parse-string '"')
133− (parse-string '\'')
134− ==
135−:: table
136−::
137−+$ table field-list
138−++ print-table
139− |= =table
140− ^- tape
141− %- zing
142− :~
143− "\{"
144− (print-field-list table)
145− "}"
146− ==
147−++ parse-table
148− %^ tnee %parse-table table
149− %+ cook
150− |=([* =table *] table)
151− ;~ (glue ws)
152− (just '{')
153− parse-field-list
154− (just '}')
155− ==
156−:: field-list
157−::
158−+$ field-list (list field)
159−++ print-field-list
160− |= =field-list
161− ^- tape
162− %- commaed
163− %+ turn field-list print-field
164−++ parse-field-list
165− %^ tnee %parse-field-list field-list
166− =>
167− |%
168− ++ field-sep
169− ;~ pose
170− com
171− mic
172− ==
173− --
174− %+ cook
175− |=([=field-list *] field-list)
176− ;~ (glue ws)
177− (more (ifix [ws ws] field-sep) parse-field)
178− (punt field-sep)
179− ==
180−:: field
181−::
182−+$ field
183− $%
184− [%keyed expr expr]
185− [%unkeyed expr]
186− ==
187−++ print-field
188− |= =field
189− ^- tape
190− ?- -.field
191− %keyed
192− %- zing
193− :~
194− "["
195− (print-expr +<.field)
196− "] = "
197− (print-expr +>.field)
198− ==
199− %unkeyed
200− (print-expr +.field)
201− ==
202−++ parse-field
203− %+ knee *field
204− |.
205− %^ tnee %parse-field field
206− ;~ pose
207− %+ cook
208− |=([* key=expr * * val=expr] [%keyed key val])
209− ;~ (glue ws)
210− (just '[')
211− parse-expr
212− (just ']')
213− tis
214− parse-expr
215− ==
216− ::
217− %+ cook
218− |=([=name * =expr] [%keyed [%string name] expr])
219− ;~ (glue ws)
220− parse-name
221− tis
222− parse-expr
223− ==
224− ::
225− %+ cook
226− |=(=expr [%unkeyed expr])
227− parse-expr
228− ==
229−:: binop
230−::
231−+$ binop
232− $?
233− %add
234− %sub
235− %mul
236− %div
237− %int-div
238− %pow
239− %mod
240− %band
241− %bor
242− %bxor
243− %rshft
244− %lshft
245− %and
246− %or
247− %lt
248− %lteq
249− %eq
250− %gt
251− %gteq
252− %neq
253− %concat
254− ==
255−++ print-binop
256− |= [=binop]
257− ?- binop
258− %add "+"
259− %sub "-"
260− %mul "*"
261− %div "/"
262− %int-div "//"
263− %pow "^"
264− %mod "%"
265− %band "&"
266− %bor "|"
267− %bxor "~"
268− %rshft ">>"
269− %lshft "<<"
270− %and "and"
271− %or "or"
272− %lt "<"
273− %lteq "<="
274− %eq "=="
275− %gt ">"
276− %gteq ">="
277− %neq "~="
278− %concat ".."
279− ==
280−++ parse-binop
281− %^ tnee %parse-binop binop
282− ;~ pose
283− (cold %add (jest '+'))
284− (cold %sub (jest '-'))
285− (cold %mul (jest '*'))
286− (cold %div (jest '/'))
287− (cold %int-div (jest '//'))
288− (cold %pow (jest '^'))
289− (cold %mod (jest '%'))
290− (cold %band (jest '&'))
291− (cold %bor (jest '|'))
292− (cold %bxor (jest '~'))
293− (cold %rshft (jest '>>'))
294− (cold %lshft (jest '<<'))
295− (cold %and (jest 'and'))
296− (cold %or (jest 'or'))
297− (cold %lt (jest '<'))
298− (cold %lteq (jest '<='))
299− (cold %eq (jest '=='))
300− (cold %gt (jest '>'))
301− (cold %gteq (jest '>='))
302− (cold %neq (jest '~='))
303− (cold %concat (jest '..'))
304− ==
305−:: numeral
306−::
307−+$ numeral
308− $%
309− [%int @u]
310− [%float @rd]
311− ==
312−++ parse-numeral
313− %^ tnee %parse-numeral numeral
314− ;~ pose
315− %+ cook
316− |= x=@rd [%float x]
317− ;~ pose
318− parse-float
319− parse-hex-float
320− ==
321− ::
322− %+ cook
323− |= x=@u [%int x]
324− ;~ pose
325− ;~ pfix
326− (just '0')
327− (mask "xX")
328− hex
329− ==
330− ::
331− (bass 10 (plus dit))
332− ==
333− ==
334−++ print-numeral
335− |= [num=numeral]
336− ^- tape
337− ?- -.num
338− %int (show-u +.num)
339− %float (show-float +.num)
340− ==
341−:: exprlist
342−::
343−+$ exprlist (lest expr)
344−++ print-exprlist
345− |= [=exprlist]
346− %- commaed
347− %+ turn exprlist print-expr
348−++ parse-exprlist
349− %^ tnee %parse-exprlist exprlist
350− %+ most (ifix [ws ws] com) parse-expr
351−:: Var
352−::
353−+$ var
354− $%
355− [%dot prefix-expr name]
356− [%indexed prefix-expr expr]
357− [%name name]
358− ==
359−++ print-var
360− |= [=var]
361− ^- tape
362− ?- -.var
363− %name (trip +.var)
364− %indexed
365− %- zing
366− :~
367− (print-prefix-expr +<.var)
368− "["
369− (print-expr +>.var)
370− "]"
371− ==
372− %dot
373− %- zing
374− :~
375− (print-prefix-expr +<.var)
376− "."
377− (trip +>.var)
378− ==
379− ==
380−++ parse-var
381− %^ tnee %parse-var var
382− ;< prefix=prefix-expr bind parse-prefix-expr
383− ?: ?=(%var -.prefix) (easy +.prefix) fail
384−:: varlist
385−::
386−+$ varlist (lest var)
387−++ print-varlist
388− |= [=varlist]
389− %- commaed
390− %+ turn varlist print-var
391−++ parse-varlist
392− %^ tnee %parse-varlist varlist
393− %+ most (ifix [ws ws] com) parse-var
394−:: Prefix Expr
395−::
396−+$ prefix-expr
397− $%
398− [%expr expr]
399− [%call functioncall]
400− [%var var]
401− ==
402−++ parse-prefix-expr
403− %^ tnee %parse-prefix-expr prefix-expr
404− =<
405− ;< prefix=prefix-expr bind
406− ;~ pose
407− %+ cook |=(=name [%var %name name])
408− parse-name
409− ::
410− parse-parened-expr
411− ==
412− (recurse-prefix-var prefix)
413− |%
414− :: Takes a prefix (initially [%var ...]) and applies variable access and function calls.
415− :: This completely sidesteps calling the var parser.
416− ::
417− ++ recurse-prefix-var
418− |= prefix=prefix-expr
419− %+ knee *prefix-expr
420− |.
421− ;~ pose
422− ;< =var bind
423− ;~ pose
424− (parse-access prefix)
425− (parse-dot prefix)
426− ==
427− (recurse-prefix-var [%var var])
428− ::
429− ;< prefix=prefix-expr bind
430− %+ cook |=(=functioncall [%call functioncall])
431− (parse-functioncall-args prefix)
432− (recurse-prefix-var prefix)
433− ::
434− (easy prefix)
435− ==
436− ++ parse-access
437− |= =prefix-expr
438− %+ cook
439− |=([* =expr *] [%indexed prefix-expr expr])
440− ;~ (glue ws)
441− (just '[')
442− parse-expr
443− (just ']')
444− ==
445− ++ parse-dot
446− |= =prefix-expr
447− %+ cook
448− |=([* =name] [%dot prefix-expr name])
449− ;~ plug
450− dot
451− parse-name
452− ==
453− --
454−++ print-prefix-expr
455− |= [=prefix-expr]
456− ?- -.prefix-expr
457− %var (print-var +.prefix-expr)
458− %expr
459− %- zing
460− :~
461− "("
462− (print-expr +.prefix-expr)
463− ")"
464− ==
465− %call (print-functioncall +.prefix-expr)
466− ==
467−:: stat
468−::
469−+$ stat
470− $%
471− [%blok blok]
472− [%asmnt varlist exprlist]
473− [%label label]
474− [%goto label]
475− [%func-call functioncall]
476− [%while cond=expr body=blok]
477− [%repeat body=blok cond=expr]
478− [%if if]
479− [%break ~]
480− [%empty ~]
481− ==
482−++ parse-stat
483− %+ knee *stat
484− |.
485− %^ tnee %parse-stat stat
486− ;~ pose
487− (cold [%empty ~] (just ';'))
488− ::
489− (cold [%break ~] (jest 'break'))
490− ::
491− %+ cook
492− |= [* cond=expr * body=blok *] [%while cond body]
493− ;~ (glue wss)
494− (jest 'while')
495− parse-expr
496− (jest 'do')
497− parse-blok
498− (jest 'end')
499− ==
500− ::
501− %+ cook
502− |= [* body=blok * cond=expr] [%repeat body cond]
503− ;~ (glue wss)
504− (jest 'repeat')
505− parse-blok
506− (jest 'until')
507− parse-expr
508− ==
509− ::
510− %+ cook
511− |= [* =label] [%goto label]
512− ;~ (glue ws)
513− (jest 'goto')
514− parse-name
515− ==
516− ::
517− %+ cook
518− |= =if [%if if]
519− parse-if
520− ::
521− %+ cook
522− |= =label [%label label]
523− parse-label
524− ::
525− %+ cook
526− |=([* =blok *] [%blok blok])
527− ;~ (glue wss)
528− (jest 'do')
529− (knee *blok |.(parse-blok))
530− (jest 'end')
531− ==
532− ::
533− %+ cook
534− |=([=varlist * =exprlist] [%asmnt varlist exprlist])
535− ;~ (glue ws)
536− parse-varlist
537− (just '=')
538− parse-exprlist
539− ==
540− ::
541− %+ cook
542− |= =functioncall [%func-call functioncall]
543− parse-functioncall
544− ==
545−++ print-stat
546− |= [=stat]
547− ^- tape
548− ?- -.stat
549− %empty ";"
550− %blok (print-blok +.stat)
551− %asmnt
552− %- zing
553− :~
554− (commaed (turn +<.stat print-var))
555− "="
556− (commaed (turn +>.stat print-expr))
557− ==
558− %label (print-label +.stat)
559− %func-call (print-functioncall +.stat)
560− %break "break"
561− %goto
562− %- zing
563− :~
564− "goto "
565− (trip +.stat)
566− ==
567− %while
568− %- zing
569− :~
570− "while "
571− (print-expr cond.stat)
572− " do\0a"
573− (print-blok body.stat)
574− ==
575− %repeat
576− %- zing
577− :~
578− "repeat\0a"
579− (print-blok body.stat)
580− "until "
581− (print-expr cond.stat)
582− ==
583− %if (print-if +.stat)
584− ==
585−:: if
586−::
587−+$ if [cond=expr body=blok elsa=(list [cond=expr body=blok]) else=(unit blok)]
588−++ print-if
589− |= =if
590− ^- tape
591− %- zing
592− :~
593− "if "
594− (print-expr cond.if)
595− " then\0a"
596− (print-blok body.if)
597− ::
598− ^- tape
599− %- zing
600− %+ turn elsa.if
601− |= [cond=expr body=blok]
602− %- zing
603− :~
604− "\0aelseif"
605− (print-expr cond)
606− " then\0a"
607− (print-blok body)
608− ==
609− ::
610− ?~ else.if ""
611− ^- tape
612− %- zing
613− :~
614− "\0aelse\0a"
615− (print-blok u.else.if)
616− ==
617− ::
618− "\0aend"
619− ==
620−++ parse-if
621− %+ knee *if
622− |.
623− %^ tnee %parse-if if
624− %+ cook
625− |= [* cond=expr * body=blok elsa=(list [cond=expr body=blok]) else=(unit blok) *]
626− [cond body elsa else]
627− ;~ (glue wss)
628− (jest 'if')
629− parse-expr
630− (jest 'then')
631− parse-blok
632− ::
633− %- star
634− %+ cook
635− |= [* cond=expr * body=blok] [cond body]
636− ;~ (glue wss)
637− (jest 'elseif')
638− parse-expr
639− (jest 'then')
640− parse-blok
641− ==
642− ::
643− %- punt
644− %+ cook
645− |= [* =blok] blok
646− ;~ (glue wss)
647− (jest 'else')
648− parse-blok
649− ==
650− (jest 'end')
651− ==
652−:: functioncall
653−::
654−+$ functioncall
655− $%
656− [%call func=prefix-expr =args]
657− [%method obj=prefix-expr method=name =args]
658− ==
659−++ print-functioncall
660− |= call=functioncall
661− ^- tape
662− ?- -.call
663− %call
664− %- zing
665− :~
666− (print-prefix-expr func.call)
667− (print-args args.call)
668− ==
669− %method
670− %- zing
671− :~
672− (print-prefix-expr obj.call)
673− ":"
674− (trip method.call)
675− (print-args args.call)
676− ==
677− ==
678−++ parse-functioncall
679− %^ tnee %parse-functioncall functioncall
680− ;< prefix=prefix-expr bind parse-prefix-expr
681− ;~ pose
682− (parse-functioncall-args prefix)
683− ::
684− ?: ?=(%call -.prefix) (easy +.prefix) fail
685− ==
686−++ parse-functioncall-args
687− |= =prefix-expr
688− %^ tnee %parse-functioncall-args functioncall
689− ;~ pfix
690− ws
691− ;~ pose
692− %+ cook
693− |= =args [%call prefix-expr args]
694− parse-args
695− ::
696− %+ cook
697− |= [* method=name =args]
698− [%method prefix-expr method args]
699− ;~ (glue ws)
700− (just ':')
701− parse-name
702− parse-args
703− ==
704− ==
705− ==
706−:: args
707−::
708−+$ args exprlist
709−++ print-args
710− |= =args
711− ^- tape
712− %- zing
713− :~
714− "("
715− (print-exprlist args)
716− ")"
717− ==
718−++ parse-args
719− %^ tnee %parse-args args
720− ;~ pose
721− %+ cook
722− |= [* =exprlist *] exprlist
723− ;~ (glue ws)
724− (just '(')
725− parse-exprlist
726− (just ')')
727− ==
728− ::
729− %+ cook
730− |= =table ~[[%table table]]
731− parse-table
732− ::
733− %+ cook
734− |= =string ~[[%string string]]
735− parse-string
736− ==
737−::
738−:: label
739−::
740−+$ label name
741−++ print-label
742− |= =label
743− ^- tape
744− %- zing
745− :~
746− "::"
747− (trip label)
748− "::"
749− ==
750−++ parse-label
751− %^ tnee %parse-label label
752− %+ cook
753− |= [* =label *] label
754− ;~ (glue ws)
755− (jest '::')
756− parse-name
757− (jest '::')
758− ==
759−:: Blok
760−::
761−+$ blok [body=(lest stat) ret=(unit ret)]
762−++ print-blok
763− |= [=blok]
764− ^- tape
765− %- zing
766− %+ join "\0a"
767− %+ weld
768− (turn body.blok print-stat)
769− ?~ ret.blok ~
770− ~[(print-ret u.ret.blok)]
771−++ parse-blok
772− %^ tnee %parse-blok blok
773− ;~ plug
774− (most ws parse-stat)
775− ::
776− %- punt
777− ;~ pfix
778− wss
779− parse-ret
780− ==
781− ==
782−:: ret
783−::
784−+$ ret exprlist
785−++ print-ret
786− |= =ret
787− ^- tape
788− %- zing
789− :~
790− "return "
791− (print-exprlist ret)
792− ==
793−++ parse-ret
794− %^ tnee %parse-ret ret
795− %+ cook
796− |= [[* =exprlist] *] exprlist
797− ;~ plug
798− ;~ (glue wss)
799− (jest 'return')
800− parse-exprlist
801− ==
802− (punt (just ';'))
803− ==
804−:: Helpers
805−::
806−++ commaed
807− |= [l=(list tape)]
808− ^- tape
809− %- zing
810− %+ join "," l
811−++ parse-name
812− =>
813− |%
814− ++ name-char ;~(pose name-fst-char dit:ab)
815− ++ name-fst-char ;~(pose alf:ab (just '_'))
816− --
817− %+ cook |= =tape (crip tape)
818− ;~(less keyword ;~(plug name-fst-char (star name-char)))
819−++ show-s
820− |=(s=@s `tape`[?:((syn:si s) %$ '-') (slag 2 (scow %ui (abs:si s)))])
821−++ show-u
822− |=(u=@u `tape`(slag 2 (scow %ui u)))
823−++ bind
824− |* =mold
825− |* [prev=rule cont=$-(mold rule)]
826− |= =nail
827− =/ prev-edge (prev nail)
828− ?~ q.prev-edge prev-edge
829− ((cont p.u.q.prev-edge) q.u.q.prev-edge)
830−++ w
831− ;~ pose
832− gah
833− (just '\09')
834− ==
835−++ ws
836− %+ cold ~
837− %- star w
838−++ wss
839− %+ cold ~
840− %- plus w
841−++ parse-float
842− %^ tnee %parse-float @rd
843− %+ cook ryld
844− %+ cook royl-cell:so
845− %+ sear
846− |= [int=@ frac=(unit [@ @]) exp=(unit [exp-sign=? exp=@])]
847− ?: ?=([~ ~] [frac exp]) ~
848− %- some
849− :*
850− %d
851− &
852− int
853− (fall frac [0 0])
854− (fall exp [& 0])
855− ==
856− =/ moo
857− |= a=tape
858− :- (lent a)
859− (scan a (bass 10 (plus sid:ab)))
860− ;~ plug
861− ;~ plug
862− dim:ag
863− ::
864− (punt ;~(pfix dot (cook moo (plus (shim '0' '9')))))
865− ::
866− %- punt
867− ;~ pfix
868− (mask "eE")
869− ;~(plug ;~(pose (cold | hep) (easy &)) dim:ag)
870− ==
871− ==
872− ==
873−++ parse-hex-float
874− %^ tnee %parse-hex-float @rd
875− =<
876− %+ sear
877− |= [* * int=tape frac=(unit tape) exp=(unit [exp-sign=? exp=@])]
878− ?: ?=([~ ~] [frac exp]) ~
879− %- some
880− =/ frac (fall frac "")
881− =/ whole=@rd (sun:rd (scan (weld int frac) hex))
882− =/ frac-exp=@rd (sun:rd (pow 16 (lent frac)))
883− =/ without-exp (div:rd whole frac-exp)
884− ?~ exp without-exp
885− =/ exp-exp (sun:rd (pow 2 exp.u.exp))
886− ?: exp-sign.u.exp
887− (mul:rd without-exp exp-exp)
888− (div:rd without-exp exp-exp)
889− ;~ plug
890− (just '0')
891− ::
892− (mask "xX")
893− ::
894− (plus hex-digit)
895− ::
896− %- punt
897− ;~ pfix
898− dot
899− (plus hex-digit)
900− ==
901− ::
902− %- punt
903− ;~ pfix
904− (mask "pP")
905− ;~(plug ;~(pose (cold | hep) (cold & lus) (easy &)) dim:ag)
906− ==
907− ==
908− |%
909− ++ hex-digit
910− %^ tnee %hex-digit char
911− ;~ pose
912− (shim '0' '9')
913− (shim 'a' 'f')
914− (shim 'A' 'F')
915− ==
916− --
917−++ show-float
918− |= x=@rd
919− ^- tape
920− %- r-co:co
921− %- rlyd
922− x
923−++ keyword
924− %+ cold ~
925− ;~ pose
926− (jest 'and')
927− (jest 'break')
928− (jest 'do')
929− (jest 'else')
930− (jest 'elseif')
931− (jest 'end')
932− (jest 'false')
933− (jest 'for')
934− (jest 'function')
935− (jest 'goto')
936− (jest 'if')
937− (jest 'in')
938− (jest 'local')
939− (jest 'nil')
940− (jest 'not')
941− (jest 'or')
942− (jest 'repeat')
943− (jest 'return')
944− (jest 'then')
945− (jest 'true')
946− (jest 'until')
947− (jest 'while')
948− ==
949−++ tnee
950− |* [=term gar=mold sef=rule]
951− |= tub=nail
952− ^- (like gar)
953− =/ trace &
954− ~? trace [term q.tub]
955− =/ res (sef tub)
956− ?. trace res
957− ?~ q.res
958− ~& [term %nope] res
959− ~& > [term %ok] res
960− --
2+--