\d p \l k / decompose () [] {} if-then-else lr:{:[x[0]~*y;(0,(=/+\'y~\:/:x)?1)_ y;(+,x),'(y;())]} then:lr[$`if`then] else:lr[$`if`else] par:lr[+,"()"] bra:lr[+,"[]"] cur:lr[+,"{}"] / parser parse:{[t]:[#t;(,()),parse_[t]t?/:$`is`where;(();();)]} parse_:{[t;i]used . lift . semi .:[ f x is g x where g x is x lift:{[e;w](e;@[w;_n;lift_ .])} lift_:{[v;e;w]caller[shadow[w]@across[w]/@[w;_n;free .];v;e]w} shadow:{[w;f]@[f;_n;_dvl;w[;0]]} caller:{[f;v;e;w]call[f;v;e]@[@[w;_n;call[f].];_n;{called[f;y]. x};!w]} called:{[f;s;v;e;w](f[s],v;e;w)} call:{[f;v;e;w](v;call_[@[f;_n;"I",'$:];"L",'$!f]e;@[w;_n;lift_ .])} call_:{[i;l;e]:[{}~*e;@[e;1 2;_f[i;l]];e _in l;{({};x;y)}/[e;i`$1_ e];e]} across:{[w;f]across_[w]/[f;!w]} across_:{[w;f;s]@[f;s;?(,/f calls[!w]. w s),]} calls:{[s;v;e;w]s@&s _lin lits e} free:{[v;e;w]vars[e]_dvl v,!w} names:{[c;e]?,//{:[{}~*x;_f'x 1 2;~c~*x;();`$1_ x]}e} vars:names["I"] lits:names["L"] / f x where g is x -> x in g is a variable semi:{[e;w](e;@[w;_n;semi_[args w].])} semi_:{[u;v;e;w](v;var["L",'$?u,v]e;@[w;_n;_f[?v,args w].])} var:{[v;e]:[{}~*e;@[e;1 2;_f[v]];e _in v;"I",1_ e;e]} args:{[w]:[_n~w;0#`;?,/w[;0]]} / f x is F, x not in F -> f x is first F [x] used:{[e;w](e;@[w;_n;used_ .])} used_:{[v;e;w](v;:[#f:v _dvl occurs e;first[$f]e;e];@[w;_n;_f .])} occurs:{[e]:[{}~*e;?,/_f'e 1 2;"I"~*e;(),`$1_ e;()]} first:{[f;e]({};({};"Lfirst";e);form list[f;+,1!"][",1_,/";",'f])} / where f .. -> d.f:.. where:{[t]*(defs .)/(;t)} defs:{[r;t]:[#t:("where"~*t)_ t;(dict[r]c 0;(c:1_'par t)1);(r;t)]} dict:{[r;t]d:def . split[t]t?"where";@[r;*d;:;d 1]} split:{[t;i]:[~i;(1_ t;());(i#t;(i+1)_ t)]} def:{[a;b]i:a?"is";c:i#a;d:(1+i)_ a;(`$*c;(`$1_ c;form expr[1_ c]d;where b))} / expression expr:{[v;t]:[~#t;{;t};"if"~*t;cond;"("~**t;group;"["~**t;list;"{"~**t;kexp;oper][v]t} oper:{[v;t](,term[v]t 0),expr[v]1_ t} / if-then-else cond:{[v;t]b:then@*a:else t;{(({`n};,"O");(x;(y;z)))}.(expr[v]1_)'b,,a 1} / ()s nest: (..) -> ,.. group:{[v;t]a:par t;b:1_*a;:[#b;,expr[v;b];()],expr[v]1_ a 1} / {x}, x is an uncompiled k expression - {{[x] but not {{ [x] kexp:{[v;t]a:cur t;(,({`n};"N",,/1_*a)),expr[v]1_ a 1} / list is a cons-expression: [a;b;..] -> a:b:[] list:{[v;t]a:bra t;expr[v]elem[null a[0],,,"]"],1_ a 1} null:{*(null_ .)/(();x)} null_:{:[~#y;,x;rb y;,null__[x]y;sc y;,x,:[|/(sc;lb)@\:-1#x;,"null";()],,*y;,x,,*y],,1_ y} null__:{:[lb -1#x;(-1_ x),,"nil";sc -1#x;x,("null";*y);x,,*y]} elem:{:[x~,"null";x;*(elem_ .)/(();x;!0)]} elem_:{ :[~#y ;(x;y;z) lp y ;(x,,*y;1_ y;z,1) rp y ;(x,,*y;1_ y;-1_ z) lb y ;(x,+,"((";1_ y;z,2) rb y ;(x,(,")";,":";"nil";,")");1_ y;-1_ z) (~1=*-1#z)&sc y ;(x,+,"):(";1_ y;z) (x,,*y;1_ y;z)]} lp:(,"(")~*: rp:(,")")~*: lb:(,"[")~*: rb:(,"]")~*: sc:(,";")~*: / term term:{[v;t]{(. 1!"}{`",x;y,t)}.+,$type[v]t} / type L:_ci(,/97 65+\:!26;48+!10) num:{(x[0]_in"-.",L 1)&|/L[1]_lin x} lit:{x[0]_in*L} type:{[v;t] :["null"~t ;`nZ "nil"~t ;`nY "nan"~t ;`nj "Nan"~t ;`nJ "inf"~t ;`nk "Inf"~t ;`nK "eq"~t ;`vE t _in$`converge`Converge ;`aA t _in$`loop`Loop ;`bA "`"~*t ;`nS "\""~*t ;`nQ t _in$`head`tail ;`uW t _in v ;`nI lit t ;`nL num t ;`nN t[0]_in"'/\\" ;`aA ":"~*t ;`vC "."~*1_ t ;`vU `vV]} / make elided nulls explicit: x::y -> x:null:y nulls:{nulls__[({`v};"C:");({`n};"Ynil")]@*(nulls_[({`v};"C:");({`n};"Ynil")].)/(();x)} nulls_:{[c;n;x;y]:[~#y;,x;~7=4:**y;,x,,nulls@*y;&/c~/:(*-1#x;*y);,x,(n;*y);,x,,*y],,1_ y} nulls__:{[c;n;x]:[c~*-1#x;_f[c;n]x,,n;c~*x;(,n),x;x]} / introduce explicit application: xy -> {}xy apply:{:[4:x 1;system . x;({`n};,"O")~2#*x;apply_[x[1;0]]. x[1;1];{},_f'x]} apply_:{({};({};({};,"O";apply x);apply y);apply z)} system:{:[y[0]_in"ECILW";y;(x;y)]} \d ^ Parse:p.parse