a better k parser for joy

stevan apter — 2000-06-03 23:43:41

my original joy parser was a horror. i can't imagine what i was thinking
when i wrote that code. in any case, those who are using my interpreter
can replace that block with this one:

\d Parse
N:"_.0123456789"
A:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
O:("&|~+-*/%=<>";`and`or`not`plus`minus`mul`idiv`div`eq`lt`gt)

in:{r:(x;());while[#*r;r:i.next . r];r 1}
i.next:{:[" "=c:*x;i.blank;"["=c;i.list;"\""=c;i.string;c _in*O;i.oper;c _in N;i.num;i.name][x;y]}
i.blank:{n:(x=" ")?0;(n _ x;y)}
i.list:{n:(=/+\'x=/:"[]")?1;((n+1)_ x;y,,in 1_ n#x)}
i.string:{n:(t:1_ x)?"\"";((n+1)_ t;y,,n#t)}
i.num:{n:+/&\x _lin N;(n _ x;y,._ssr[n#x;"_";"-"])}
i.oper:{(1_ x;y,,O[1;O[0]?*x])}
i.name:{n:+/&\x _lin A,N;(n _ x;y,`$n#x)}

out:{(1_,/(" ",o.term@)'|x),"\n"}
o.term:{:[-3=t:4:x;"\"",x,"\"";1>t;o.list@_f'x;t=3;"'",x;t=5;_f(+($!x;x[]);`dict);$(O[0],,x)O[1]?x]}
o.list:{"[",(1_,/" ",'$x),"]"}

\d ^