├── .github └── workflows │ └── k.yml ├── _ ├── 1 │ ├── 1k.go │ ├── go.mod │ ├── k_test.go │ ├── mk │ └── readme ├── .k │ ├── six.go │ └── six.k ├── a │ ├── # │ ├── 9b │ ├── ` │ ├── qr.k │ └── readme ├── avx │ ├── main.c │ └── mk ├── g │ ├── b.go │ ├── csv.go │ ├── gen.go │ ├── go.go │ ├── go.mod │ ├── go_test.go │ ├── main.go │ ├── mat.go │ ├── mk │ ├── plot.go │ ├── rand.go │ ├── t │ ├── t_test.go │ └── unmain.go ├── g2 │ ├── k.go │ ├── parse │ │ ├── go.mod │ │ ├── main.go │ │ ├── token.go │ │ ├── token_test.go │ │ └── type.go │ └── src │ │ ├── a │ │ ├── a.go2 │ │ ├── a_test.go2 │ │ └── mk │ │ └── k │ │ ├── k.go2 │ │ ├── k_test.go2 │ │ ├── mk │ │ └── p.go2 ├── gw │ ├── go.mod │ ├── go.sum │ ├── gw.go │ ├── jmain │ ├── ktye │ │ └── mk │ └── test │ │ ├── gen.go │ │ ├── mk │ │ └── t.w ├── i1 │ ├── README │ ├── a.go │ ├── a_test.go │ ├── e.go │ ├── go.mod │ ├── n.go │ ├── n_test.go │ ├── p.go │ ├── p_test.go │ ├── v.go │ ├── v_test.go │ └── ⍳ │ │ ├── README │ │ ├── dev.go │ │ ├── file.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── io.go │ │ ├── kb │ │ ├── run.go │ │ ├── time.go │ │ └── ⍳.go ├── i2 │ ├── alt │ ├── k- │ │ ├── a.go │ │ └── readme │ ├── k.go │ ├── k │ │ ├── a.go │ │ ├── p.go │ │ ├── readme │ │ ├── w.go │ │ └── w_windows.go │ ├── k_test.go │ ├── kui │ │ ├── f │ │ │ ├── 10x20.png │ │ │ ├── 16x32.png │ │ │ ├── f1.k │ │ │ ├── f2.k │ │ │ ├── f3.k │ │ │ ├── fonts.deck │ │ │ ├── gen.go │ │ │ ├── genface.go │ │ │ ├── k.png │ │ │ ├── lstr.10 │ │ │ ├── lstr.12 │ │ │ ├── luc11.k │ │ │ ├── luc13.k │ │ │ ├── lucx11.font │ │ │ ├── lucx11_6x11.png │ │ │ ├── lucx13.font │ │ │ ├── lucx13_7x13.png │ │ │ ├── mkdek │ │ │ ├── png.go │ │ │ ├── pnginfo.go │ │ │ ├── readme.md │ │ │ ├── vt220.k │ │ │ ├── vt220.png │ │ │ └── zevv_8x16.png │ │ ├── fb.k │ │ ├── readme.md │ │ ├── s.k │ │ └── t.k │ ├── kwm │ │ ├── a.go │ │ ├── f.go │ │ ├── h │ │ ├── k.ht │ │ ├── readme │ │ └── wx.js │ ├── readme.md │ └── t ├── i3 │ ├── k.go │ ├── k.w │ ├── w.k │ └── w │ │ ├── gasm_test.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── k_c │ │ ├── k_html │ │ ├── k_test.go │ │ ├── min_c │ │ ├── ngn.go │ │ ├── ops │ │ ├── p.go │ │ ├── readme │ │ ├── runcc │ │ ├── t │ │ ├── u_c │ │ ├── w.go │ │ ├── w_test.go │ │ ├── wagon_test.go │ │ └── wasi ├── i4 │ ├── k.k │ ├── readme │ └── t │ │ ├── k.go │ │ ├── mk │ │ ├── t │ │ ├── t.awk │ │ └── w.k ├── i5 │ ├── adverbs.go │ ├── aggregate.go │ ├── annotate.go │ ├── assign.go │ ├── call.go │ ├── cat.go │ ├── compare.go │ ├── csv.go │ ├── exec.go │ ├── find.go │ ├── go.mod │ ├── index.go │ ├── json.go │ ├── k │ │ └── k.go │ ├── k_test.go │ ├── lambda.go │ ├── mat.go │ ├── mat │ │ └── mat.go │ ├── numeric.go │ ├── parse.go │ ├── read.go │ ├── readme.md │ ├── sort.go │ ├── string.go │ ├── token.go │ ├── types.go │ └── verbs.go ├── i7 │ ├── 7.k │ ├── c.k │ ├── elf.txt │ ├── mk │ └── readme ├── i9 │ ├── k.c │ └── mk ├── j │ ├── go.mod │ ├── go.sum │ ├── h.go │ ├── j.go │ ├── j.w │ ├── j_test.go │ ├── jgo │ │ └── a.go │ ├── joy │ │ └── man │ ├── js.html │ ├── jwa │ │ └── j.go │ ├── main │ │ └── j.go │ ├── mk │ ├── pako.min.js │ ├── readme │ ├── serve.go │ ├── t │ └── x │ │ └── j.go ├── k │ ├── a.k │ ├── b.j │ ├── braille.k │ ├── example.j │ ├── example.k │ ├── fire.k │ ├── fmt.k │ ├── font.k │ ├── lines.k │ ├── m.k │ ├── mk │ ├── plot.k │ ├── r5.k │ ├── rand.k │ ├── raster.j │ ├── raster.k │ ├── sixel.k │ ├── ui.j │ └── ui.k ├── kdoc.go ├── ki │ ├── k.t │ ├── k_test.go │ ├── ki.go │ ├── mk │ ├── readme │ └── z.k ├── ku │ ├── go.mod │ ├── go.sum │ ├── main.go │ └── mk ├── kwac │ ├── Makefile │ ├── README │ ├── h │ ├── mkhtml │ ├── p │ └── s ├── listbox │ ├── csv.go │ ├── embed.go │ ├── go.mod │ ├── listbox.go │ ├── listbox.html │ ├── listbox.k │ ├── listbox.manifest │ ├── mk │ ├── plot.go │ └── unmain.go ├── mach │ ├── main.go │ ├── r5.go │ ├── r5.wat │ └── r5_test.go ├── plot │ ├── go.mod │ ├── h │ └── mk ├── ps │ ├── k.ps │ ├── ps.go │ └── ps_test.go └── readme ├── a.go ├── adverbs.go ├── alloc.go ├── apl ├── 66 ├── apl.html ├── apl.k └── out ├── call.go ├── cat.go ├── exec.go ├── f77 ├── mk ├── readme └── t.awk ├── find.go ├── go.mod ├── go.sum ├── index.go ├── intro.md ├── k.e ├── k.go ├── k.t ├── k_test.go ├── kcsv ├── csv_test.go ├── head ├── mk ├── readme ├── t.csv ├── tail └── unpack.k ├── kos ├── amas.k ├── arm.go ├── mk ├── orig.go ├── softfloat.go ├── softfloat.k └── softfloat_test.go ├── kx ├── head ├── mk ├── readme └── tail ├── main.go ├── math.go ├── mk ├── nomain.go ├── p.k ├── parse.go ├── ps.k ├── rand.go ├── readme ├── reduce.go ├── s.k ├── scalar.go ├── simd.go ├── simd0.go ├── simd4.go ├── simd5.go ├── size ├── sort.go ├── steps.md ├── string.go ├── system.go ├── tco.awk ├── test.go ├── tests ├── 66 ├── halfkey.k ├── math.k ├── mk ├── mm.k ├── q.k ├── qq.k ├── t.k ├── token.k └── z.k ├── token.go ├── v ├── a.go ├── c │ ├── head │ ├── mk │ ├── remove.awk │ └── tail ├── mk ├── v.go ├── v.wat └── w │ ├── ftab │ ├── mk │ └── v.go ├── verbs.go ├── x ├── cc.k ├── flt.go ├── flt.k ├── go.k ├── ikfmt.go ├── js.k ├── mk ├── pd.k ├── readme ├── tests │ ├── asn.c │ ├── asn.go │ ├── asn.js │ ├── asn.k │ ├── asn.q │ ├── asn.wa │ ├── cal.c │ ├── cal.go │ ├── cal.js │ ├── cal.k │ ├── cal.q │ ├── cal.wa │ ├── cast.c │ ├── cast.go │ ├── cast.js │ ├── cast.k │ ├── cast.q │ ├── cast.wa │ ├── cli.c │ ├── cli.go │ ├── cli.js │ ├── cli.k │ ├── cli.q │ ├── cli.wa │ ├── cnd.c │ ├── cnd.go │ ├── cnd.js │ ├── cnd.k │ ├── cnd.q │ ├── cnd.wa │ ├── cnd2.c │ ├── cnd2.go │ ├── cnd2.js │ ├── cnd2.k │ ├── cnd2.q │ ├── cnd2.wa │ ├── const.c │ ├── const.go │ ├── const.js │ ├── const.k │ ├── const.q │ ├── const.wa │ ├── cont.c │ ├── cont.go │ ├── cont.js │ ├── cont.k │ ├── cont.q │ ├── cont.wa │ ├── drp.c │ ├── drp.go │ ├── drp.js │ ├── drp.k │ ├── drp.q │ ├── drp.wa │ ├── fun.c │ ├── fun.go │ ├── fun.js │ ├── fun.k │ ├── fun.q │ ├── fun.wa │ ├── heap.c │ ├── heap.go │ ├── heap.js │ ├── heap.k │ ├── heap.q │ ├── heap.wa │ ├── ifret.c │ ├── ifret.go │ ├── ifret.js │ ├── ifret.k │ ├── ifret.q │ ├── ifret.wa │ ├── inc.c │ ├── inc.go │ ├── inc.js │ ├── inc.k │ ├── inc.q │ ├── inc.wa │ ├── kitest.sh │ ├── label.c │ ├── label.go │ ├── label.js │ ├── label.k │ ├── label.q │ ├── label.wa │ ├── lit.c │ ├── lit.go │ ├── lit.js │ ├── lit.k │ ├── lit.q │ ├── lit.wa │ ├── loop.c │ ├── loop.go │ ├── loop.js │ ├── loop.k │ ├── loop.q │ ├── loop.wa │ ├── loop2.c │ ├── loop2.go │ ├── loop2.js │ ├── loop2.k │ ├── loop2.q │ ├── loop2.wa │ ├── mem.c │ ├── mem.go │ ├── mem.js │ ├── mem.k │ ├── mem.q │ ├── mem.wa │ ├── mk │ ├── swtch.c │ ├── swtch.go │ ├── swtch.js │ ├── swtch.k │ ├── swtch.q │ ├── swtch.wa │ ├── swtch2.c │ ├── swtch2.go │ ├── swtch2.js │ ├── swtch2.k │ ├── swtch2.q │ ├── swtch2.wa │ ├── tab.c │ ├── tab.go │ ├── tab.js │ ├── tab.k │ ├── tab.q │ └── tab.wa ├── wa.k └── wb.k ├── z.go └── z.k /_/.k/six.k: -------------------------------------------------------------------------------- 1 | (w;h):(12;12) 2 | (r;g;b):|2 (256*)\1 3 | 4 | /example image 5 | f:{(y+(-/(*x)**x;2f**/*x);1+*|x)} /mandelbrot step 6 | l:{{{(4f>+/(*x)**x)&255>*|x}f[;y]/x}[(x;0);x]} /x y(`f) to n(0.255) 7 | c:{+/(r*x;g*x;255-((x-127)*255*x-128)/16384)} /colormap 8 | m:{c@*|l x} /pixel x y(`f) to color(`i) 9 | d:0.01%w 10 | x:d*!w;y:0.645-d*!h 11 | p:,/x,/:\:y /xy grid 12 | i:m'p /image(`i) 13 | 14 | /sixel encoding of image i 15 | rgb:{(g\x/r;g\x/g;g\x)} /`i to r g b 16 | o:"" //o:0x1b50303b303b387122313b31 /sixel header 17 | p:{$[255<#x;^?256#x;^?x]}i /palette used in image 18 | i:p?i /paletted image 19 | o,:,/{"#",(*$!#x),";2;",";"/:$_(100%255)*rgb x}'p /write palette 20 | o 21 | {wc}'((_h%6);6;w)#i 22 | -------------------------------------------------------------------------------- /_/1/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/ktye/i/_/1k 2 | 3 | go 1.16 4 | 5 | require github.com/ktye/wg v0.0.0 6 | 7 | replace github.com/ktye/wg => ../../wg 8 | -------------------------------------------------------------------------------- /_/1/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | if [ "$1" = install ]; then 5 | wg 1k.go|sed -e'2,5d' -e8d>k.wat 6 | wat2wasm k.wat -o 1k.wasm 7 | rm k.wat 8 | 9 | cp 1k.wasm /c/k/ktye.github.io/zoo/1k/ 10 | cp readme /c/k/ktye.github.io/zoo/1k/ 11 | awk -F" /" '/^ /{print $1}' readme>/c/k/ktye.github.io/zoo/1k/a 12 | awk -F" /" '/^ /{print $2}' readme>/c/k/ktye.github.io/zoo/1k/b 13 | wg -k 1k.go >/c/k/ktye.github.io/kweb/1k.k 14 | rm 1k.wasm 15 | fi 16 | 17 | 18 | # standalone wasm binary, run with e.g. wasm3, wavm, .. 19 | # $wasm3 1k.wasm 20 | # $wavm run 1k.wasm 21 | if [ "$1" = wasi ]; then 22 | cat <<' EOF' >k.wat 23 | (module 24 | (import "wasi_unstable" "fd_read" (func $R (param i32 i32 i32 i32) (result i32))) 25 | (import "wasi_unstable" "fd_write" (func $W (param i32 i32 i32 i32) (result i32))) 26 | EOF 27 | wg 1k.go | sed -e '1,8d' -e 's/$main/$main (export "_start")/' -e '/(func $o /q' >>k.wat 28 | cat << ' EOF' >>k.wat 29 | i32.const 12 local.get 0 i32.store 30 | i32.const 4 i32.const 12 i32.store 31 | i32.const 8 i32.const 1 i32.store 32 | i32.const 1 i32.const 4 i32.const 1 i32.const 16 call $W drop) 33 | (func $ReadIn (param $x i32) (param i32) (result i32) 34 | i32.const 4 local.get 0 i32.store 35 | i32.const 8 i32.const 1 i32.store 36 | i32.const 0 i32.const 4 i32.const 1 i32.const 16 call $R drop 37 | local.get 0 i32.load i32.const 10 i32.ne) 38 | EOF 39 | wg 1k.go | sed -e /table/p -e '1,/table/d' >> k.wat 40 | wat2wasm k.wat -o 1k.wasm 41 | fi 42 | -------------------------------------------------------------------------------- /_/a/#: -------------------------------------------------------------------------------- 1 | ksql # _ 2 | #[table;where;by;aggregate] 3 | 4 | select from t ()#t 5 | select from t where a>2 (:a>1)#t /dyadic take 6 | select from t where a>2,b<3 #[(:a>2;:b<3);t] /multiple where clauses as a list 7 | select c:a*b from t #[t;();`c!( :a*b)] /triadic, empty where clause is () 8 | select a*b from t #[t;();`b!( :a*b)] /default result column name is last symbol in expr 9 | select c:a*b,a from t #[t;();`c`a!( :a*b; :a)] /multiple result columns are comma separated 10 | t@`c`a!( :a*b; :a) /same as t@d 11 | select by a from t #[t;();`a!( :a);(0#`)!()] 12 | select +/b by a from t #[t;();`a!( :a);`b!( :+/b)] 13 | 14 | update a (by b) from t (where c) _[t; :c;`a!( :a);`b!( :b)] /same parsing rules as select 15 | 16 | delete from t where c>2 (:c>2)_t 17 | delete a from t (,`a)_t 18 | delete a,b from t `a`b_t 19 | 20 | ksql phrase starts with select|update|delete 21 | 1. update must, delete and select may be followed by an aggregate subphrase 22 | 2. select and update may have a by subphrase 23 | 3. all 3 must have a from subphrase 24 | 4. all may have a where phrase (delete only one of aggregate or where) 25 | 26 | by and aggregate subphrases 27 | separated by comma, if not encoded in braces, e.g. a,b,(c+1,d) 28 | if the subphrase starts with an assignment (name:..) it is used as a dict key 29 | with the rest of the subphrase stored as an :expression 30 | otherwise the default key name is the last symbol in the expression tree or 'x' if there is none 31 | return value is a dict s!l with en entry for each subphrase. catenation of ((0#`)!()) , (,key)!,(:expr) 32 | where subphrase is parsed as a simple :expression, an empty list or a list of expressions 33 | -------------------------------------------------------------------------------- /_/a/9b: -------------------------------------------------------------------------------- 1 | k9/`f@ 2 | 3 | monadic application 0x01XX20+f XX arg 4 | - 0 0x018022 5 | 80 80+constant #0 0 5 a f0 5 6 | 22 20+verb :+-*%&|<>=~!,^#_$?@.012 7 | 8 | operator application 0x01vv10+op vv arg as const 9 | +' 0x01801a 012 3 4 5 10 | 1a+op '/\ ': /: \: 11 | +'/\ 0x01801a1b1c 12 | 13 | dyadic application 0x02YYXXvv 14 | 1+2 0x02818041 right arg first 15 | 40+verb (as above) 16 | 1-+2 0x0281218042 17 | 18 | projection 19 | 1+ 0x038180820502 (2;+;') 20 | + 1 ' ??? 21 | 22 | variables 60+var(index) 23 | x+y 0x02616041 24 | {x+y} 0x020b0a41 0a+arg(index) 25 | 26 | assign 01yy00xxvv vv(modify) 27 | x:3 0x0180006000 28 | 3 : x 29 | x+:3 0x0180006001 (modified) 30 | x[i]:y 0x026261016000 (indexed) 31 | y i ? x 01(indexed) vs 00(normal) assign? -------------------------------------------------------------------------------- /_/a/qr.k: -------------------------------------------------------------------------------- 1 | /k9(2020.07.14) 2 | solve: {[A;b]qrsolve[qr A;b]} /solve A*x=b (#A)>#*A (overdetermined) 3 | qrsolve:{[q;b]qrslv[q;qrmul[q;b]]} /reuse qr 4 | 5 | qr:{[A]m:#A;A:+A;n:#A;r:(n;qrh)/:(,/A;!0;m;n;!m;!(n;m)) /qr decomposition 6 | `Q`D`m`n!(r 0;|r 1;r 2;r 3)} 7 | 8 | qrh:{[x]Q:x 0;ii:(1+x 2)*#D:x 1;row:x 4;sub:1_x 5 /householder step 9 | s:norm Q row;D,:d:s*1 -1@Q[ii]>0;s:1%sqrt s*s+abs Q ii;Q[ii]-:d;Q[row]*:s 10 | $[#sub;Q[,/sub]-:,/Q[row]*/+/'(Q[row]*/Q[sub]);] 11 | (Q;D;x 2;x 3;(x 2)+1_row;1_'sub)} 12 | 13 | qrmul:{[q;b]n:q`n;m:q`m;r:(n;qrml)/:(q`Q;b;!m;!m;m);n#r 1} /calculate QT*b 14 | qrml: {[x]Q:x 0;b:x 1;row:x 2;yi:x 3;m:x 4;b[yi]-:Q[row]*+/Q[row]*b[yi];(Q;b;m+1_row;1_yi;m)} 15 | 16 | qrslv:{[q;b]n:q`n;r:(n;qrsl)/:(q`Q;b;q`D;n-1;q`m;n;!0);r 1} /solve R*x=QT*b (back substitution) 17 | qrsl: {[x]Q:x 0;b:x 1;D:x 2;i:x 3;m:x 4;n:x 5;bi:x 6;col:i+m*bi 18 | $[#bi;b[i]-:+/Q[col]*b[bi];];b[i]%:*D 19 | (Q;b;1_D;i-1;m;n;i,bi)} 20 | 21 | norm::s*sqrt+/x*x%:s:|/x:abs x 22 | abs::x|-x 23 | 24 | 25 | /e.g. A*x=b (6x4) 26 | matvec::+/'x*\y 27 | A:4^?24 28 | x:?4 29 | b:matvec[A;x] 30 | (`err;|/abs x-solve[A;b]) 31 | -------------------------------------------------------------------------------- /_/a/readme: -------------------------------------------------------------------------------- 1 | ` : k7 binary format `@ `? 2 | # : k7 ksql doc 3 | 9b: k9 byte code -------------------------------------------------------------------------------- /_/avx/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | if [ "$1" = clean ]; then 5 | rm a b out.a out.b 6 | exit 0 7 | fi 8 | 9 | clang-13 -DXXX -Wno-psabi main.c -o a -lm 10 | clang-13 main.c -mbmi2 -mavx512f -mavx512bw -mavx512vbmi -mavx512vbmi2 -o b 11 | 12 | ./a > out.a 13 | ~/avx512/sde-external-9.21.1-2023-04-24-lin/sde64 -spr -- ./b > out.b 14 | diff out.a out.b 15 | 16 | -------------------------------------------------------------------------------- /_/g/gen.go: -------------------------------------------------------------------------------- 1 | // +build ignore 2 | 3 | package main 4 | 5 | import ( 6 | "bytes" 7 | "fmt" 8 | "io/ioutil" 9 | ) 10 | 11 | var buf *bytes.Buffer 12 | 13 | func main() { 14 | buf = bytes.NewBuffer(nil) 15 | 16 | // cut k-help from ../../readme:^0123457 17 | b, err := ioutil.ReadFile(`../../readme`) 18 | if err != nil { 19 | panic(err) 20 | } 21 | 22 | idx := panic1(bytes.Index(b, []byte("01234567"))) 23 | b = b[idx:] 24 | 25 | idx = panic1(bytes.Index(b, []byte("------"))) 26 | b = b[:idx] 27 | 28 | help := string(b) 29 | 30 | out("package main\n\n// generated by gen.go\n\n") 31 | 32 | fmt.Fprintf(buf, "var help string = %q\n", help) 33 | ioutil.WriteFile("s_.go", buf.Bytes(), 0744) 34 | } 35 | func panic1(n int) int { 36 | if n == -1 { 37 | panic("gen.go error") 38 | } 39 | return n 40 | } 41 | 42 | func out(s string) { 43 | buf.WriteString(s) 44 | } 45 | -------------------------------------------------------------------------------- /_/g/go.mod: -------------------------------------------------------------------------------- 1 | module k 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/ktye/plot v0.0.0 7 | golang.org/x/tools v0.0.0-20210115202250-e0d201561e39 // indirect 8 | ) 9 | 10 | replace github.com/ktye/plot => c:/k/plot 11 | -------------------------------------------------------------------------------- /_/g/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | ) 6 | 7 | // k='rlwrap -H /dev/null /path/to/k.exe -s `stty size`' 8 | func main() { 9 | version = "k/g" 10 | Main(os.Args[1:]) 11 | } 12 | -------------------------------------------------------------------------------- /_/g/mk: -------------------------------------------------------------------------------- 1 | 2 | go run gen.go 3 | cat ../../t/k.go | go run unmain.go printc > k_.go 4 | 5 | go fmt 6 | go build 7 | 8 | -------------------------------------------------------------------------------- /_/g/rand.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "math/rand" 4 | 5 | // shuffle x (char, int, float, complex, symbol) 6 | func rand1(x uint32) uint32 { 7 | xt, xn, _ := v1(x) 8 | if xt < 1 || xt > 5 { 9 | panic("shuffle: type") 10 | } 11 | r := use(x) 12 | p1 := int(r + 8) 13 | p2, p3, p4 := p1>>2, p1>>3, 1+p1>>3 14 | f1 := func(i, j int) { MC[p1+i], MC[p1+j] = MC[p1+j], MC[p1+i] } 15 | f4 := func(i, j int) { MI[p2+i], MI[p2+j] = MI[p2+j], MI[p2+i] } 16 | f8 := func(i, j int) { MF[p3+i], MF[p3+j] = MF[p3+j], MF[p3+i] } 17 | f16 := func(i, j int) { 18 | MF[p3+2*i], MF[p3+2*j] = MF[p3+2*j], MF[p3+2*i] 19 | MF[p4+2*i], MF[p4+2*j] = MF[p4+2*j], MF[p4+2*i] 20 | } 21 | switch xt { 22 | case 1: 23 | rand.Shuffle(int(xn), f1) 24 | case 2, 5: 25 | rand.Shuffle(int(xn), f4) 26 | case 3: 27 | rand.Shuffle(int(xn), f8) 28 | case 4: 29 | rand.Shuffle(int(xn), f16) 30 | default: 31 | panic("type") 32 | } 33 | return r 34 | } 35 | 36 | // 2 'r n n random integers (int32) randi n 37 | // n random integers 0..m-1 m/randi n 38 | // 3 'r n n uniform floats range 0..1 randf n 39 | //-3 'r n n normal distributed floats randn n 40 | // 4 'r n n binormal complex numbers randz n 41 | // 5 'r n n random 2-letter symbols rands n 42 | func rand2(x, y uint32) uint32 { 43 | if tp(x) != 2 || nn(x) != 1 || tp(y) != 2 || nn(y) != 1 { 44 | panic("rand2: type (x and y must be int atoms)") 45 | } 46 | a := MI[2+x>>2] 47 | dx(x) 48 | b := iK(y) 49 | switch a { 50 | case 2: 51 | if b < 0 { 52 | panic("rand2: y<0") 53 | } 54 | r := mk(2, uint32(b)) 55 | for i := uint32(0); i < uint32(b); i++ { 56 | MI[2+i+r>>2] = rand.Uint32() 57 | } 58 | return r 59 | case 4294967293: // -3 60 | r := make([]float64, b) 61 | for i := range r { 62 | r[i] = rand.NormFloat64() 63 | } 64 | return kF(r) 65 | case 3: 66 | r := make([]float64, b) 67 | for i := range r { 68 | r[i] = rand.Float64() 69 | } 70 | return kF(r) 71 | case 4: 72 | r := make([]complex128, b) 73 | for i := range r { 74 | r[i] = complex(rand.NormFloat64(), rand.NormFloat64()) 75 | } 76 | return kZ(r) 77 | case 5: 78 | r := make([]string, b) 79 | for i := range r { 80 | r[i] = string([]byte{'a' + byte(rand.Intn(26)), 'a' + byte(rand.Intn(26))}) 81 | } 82 | return kS(r) 83 | default: 84 | panic("rand2: x type") 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /_/g/t: -------------------------------------------------------------------------------- 1 | 1 /1 2 | 1a30=1a30 1a40 /1 0 3 | `$("alpha";"beta") /`alpha`beta 4 | (`a`b!(1 2;3 4)),5 6 /`a`b!(1 2 5;3 4 6) 5 | (!10)(5>) /0 1 2 3 4 6 | {x>3}#!7 /4 5 6 7 | {x>3}_!7 /0 1 2 3 8 | ?1 2 2 3 3 4 /1 2 3 4 9 | (1 1;1 2;1 1)?,(1 1) /0 10 | ?(1 1;1 2;1 1) /(1 1;1 2) 11 | ?(1 2;2 1;1 2;1 3 2;2 1) /(1 2;2 1;1 3 2) 12 | =(1 2;2 1;1 2;1 3 2;2 1) /((1 2;2 1;1 3 2);(0 2;1 4;3)) 13 | ?((`q;0);(`w;0);(`e;0);(`q;0);(`w;0);(`e;1);(`q;1);(`w;1);(`e;1);(`q;1)) /((`q;0);(`w;0);(`e;0);(`e;1);(`q;1);(`w;1)) 14 | =((`q;0);(`w;0);(`e;0);(`q;0);(`w;0);(`e;1);(`q;1);(`w;1);(`e;1);(`q;1)) /(((`q;0);(`w;0);(`e;0);(`e;1);(`q;1);(`w;1));(0 3;1 4;2;5 8;6 9;7)) 15 | `a`b!(!5;1+!5) /`a`b!(0 1 2 3 4;1 2 3 4 5) 16 | r:!18000;#r /18000 17 | -------------------------------------------------------------------------------- /_/g/t_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | var tinit = "t:`a`b`c!(1+!10;2+!10;`q`w`e`q`w`e`q`w`e`q);T:`a`b`c`d!(1+!10;2+!10;`q`w`e`q`w`e`q`w`e`q;0 0 0 0 0 1 1 1 1 1)" 8 | var ttests = [][2]string{ 9 | {"{a>5}#t", "`a`b`c!(6 7 8 9 10;7 8 9 10 11;`e`q`w`e`q)"}, 10 | {"{`sum`prod!(a+b;a*b)}#t", "`sum`prod!(3 5 7 9 11 13 15 17 19 21;2 6 12 20 30 42 56 72 90 110)"}, 11 | {"t{~c=`q}", "`a`b`c!(2 3 5 6 8 9;3 4 6 7 9 10;`w`e`w`e`w`e)"}, 12 | {"+/'`a`b#t", "`a`b!(55;65)"}, 13 | {"(*;+/)'`a`b#t", "``a`b!(`*`+{;(1;55);(2;65))"}, 14 | {"(`first`sum!(*;+/))'`a`b#t", "``a`b!(`first`sum;(1;55);(2;65))"}, 15 | {"`c=t", "(`c!,`q`w`e;`a`b!((1 4 7 10;2 5 8;3 6 9);(2 5 8 11;3 6 9;4 7 10)))"}, 16 | {"`c+/'t", "`c`a`b!(`q`w`e;22 15 18;26 18 21)"}, 17 | {"=&T`c`d", "(((`q;0);(`w;0);(`e;0);(`e;1);(`q;1);(`w;1));(0 3;1 4;2;5 8;6 9;7))"}, 18 | {"`c`d=T", "(`c`d!(`q`w`e`e`q`w;0 0 0 1 1 1);`a`b!((1 4;2 5;3;6 9;7 10;8);(2 5;3 6;4;7 10;8 11;9)))"}, 19 | {"`c`d+/'T", "`c`d`a`b!(`q`w`e`e`q`w;0 0 0 1 1 1;5 7 3 15 17 8;7 9 4 17 19 9)"}, 20 | {"`c`d(*;+/)'T", "`c`d`*a`+{a`*b`+{b!(`q`w`e`e`q`w;0 0 0 1 1 1;1 2 3 6 7 8;5 7 3 15 17 8;2 3 4 7 8 9;7 9 4 17 19 9)"}, 21 | {"`c`d(`\"first \"`\"sum \"!(*;+/))'T", "`c`d`first a`sum a`first b`sum b!(`q`w`e`e`q`w;0 0 0 1 1 1;1 2 3 6 7 8;5 7 3 15 17 8;2 3 4 7 8 9;7 9 4 17 19 9)"}, 22 | {"t:`a`b`i!(1 2 3 4;5 6 7 8;0 0 1 1);b:`i`n`s!(0 1 2;`alpha`beta`gamma;2.1 2.2 3.3);ej[`i;t;b]", "`a`b`i`n`s!(1 2 3 4;5 6 7 8;0 0 1 1;`alpha`alpha`beta`beta;2.1 2.1 2.2 2.2)"}, 23 | } 24 | 25 | func TestT(t *testing.T) { 26 | kinit() 27 | dx(eval(tinit)) 28 | for _, tc := range ttests { 29 | s := kstval(tc[0]) 30 | if s != tc[1] { 31 | t.Fatalf("%s\ngot:%s\nexp:%s\n", tc[0], s, tc[1]) 32 | } 33 | } 34 | bleak() 35 | } 36 | -------------------------------------------------------------------------------- /_/g/unmain.go: -------------------------------------------------------------------------------- 1 | // +build ignore 2 | 3 | // Unmain reads a go file from stdin and removes the main function. 4 | package main 5 | 6 | import ( 7 | "bytes" 8 | "go/ast" 9 | "go/format" 10 | "go/parser" 11 | "go/token" 12 | "os" 13 | 14 | "golang.org/x/tools/imports" 15 | ) 16 | 17 | func main() { 18 | del := make(map[string]bool) 19 | for _, f := range append([]string{"main"}, os.Args[1:]...) { 20 | del[f] = true 21 | } 22 | 23 | fset := token.NewFileSet() 24 | f, e := parser.ParseFile(fset, "", os.Stdin, parser.ParseComments) 25 | fatal(e) 26 | 27 | // remove main function 28 | j := 0 29 | for _, a := range f.Decls { 30 | if d, o := a.(*ast.FuncDecl); o && del[d.Name.Name] { 31 | } else { 32 | f.Decls[j] = a 33 | j++ 34 | } 35 | } 36 | f.Decls = f.Decls[:j] 37 | 38 | // fix imports 39 | var buf bytes.Buffer 40 | fatal(format.Node(&buf, fset, f)) 41 | out, e := imports.Process("k.go", buf.Bytes(), nil) 42 | fatal(e) 43 | os.Stdout.Write(out) 44 | } 45 | func fatal(e error) { 46 | if e != nil { 47 | panic(e) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /_/g2/k.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type A interface { 8 | Ref() 9 | Unref() 10 | Zero() A 11 | Empty() A 12 | } 13 | 14 | type numeric interface { 15 | type int, float64, complex128 16 | } 17 | 18 | func NewFixed[T numeric]() A { 19 | var a Fixed[T] 20 | a.a = make([]T, 0) 21 | return &a 22 | } 23 | 24 | type Fixed[T numeric] struct { 25 | a []T 26 | rc int 27 | } 28 | type Atom[T numeric] T 29 | 30 | func (a Atom[T]) Ref() {} 31 | func (a Atom[T]) Unref() {} 32 | func (a Atom[T]) Zero() A { 33 | a = 0 34 | return a 35 | } 36 | func (A Atom[T]) Empty() A { 37 | return NewFixed[T]() 38 | } 39 | 40 | func (a *Fixed[T]) Ref() { a.rc++ } 41 | func (a *Fixed[T]) Unref() { a.rc-- } 42 | func (a *Fixed[T]) Zero() A { 43 | var z Atom[T] 44 | return z 45 | } 46 | func (a *Fixed[T]) Empty() A { 47 | return NewFixed[T]() 48 | } 49 | 50 | func main() { 51 | var a Fixed[int] 52 | fmt.Println(a) 53 | } 54 | 55 | -------------------------------------------------------------------------------- /_/g2/parse/go.mod: -------------------------------------------------------------------------------- 1 | module k 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /_/g2/parse/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io/ioutil" 7 | "os" 8 | ) 9 | 10 | func main() { 11 | if len(os.Args) < 2 { 12 | scn := bufio.NewScanner(os.Stdin) 13 | for scn.Scan() { 14 | scan([]byte(scn.Text())) 15 | } 16 | } else { 17 | b, e := ioutil.ReadFile(os.Args[1]) 18 | fatal(e) 19 | scan(b) 20 | } 21 | } 22 | func scan(b []byte) { 23 | a, sp, e := token(b) 24 | if e != nil { 25 | fmt.Println(e) 26 | return 27 | } 28 | fmt.Println(a) 29 | fmt.Println(sp) 30 | } 31 | func fatal(e error) { 32 | if e != nil { 33 | panic(e) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /_/g2/parse/token_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "io/ioutil" 7 | "strings" 8 | "testing" 9 | ) 10 | 11 | type testCase struct { 12 | in, exp string 13 | } 14 | 15 | func tcFile(t *testing.T, f string) (tc []testCase) { 16 | b, e := ioutil.ReadFile(f) 17 | if e != nil { 18 | t.Fatal(e) 19 | } 20 | v := bytes.Split(b, []byte{10}) 21 | for _, u := range v { 22 | s := string(u) 23 | if strings.HasPrefix(s, "// ") { 24 | s = s[3:] 25 | arrow := " → " 26 | if idx := strings.Index(s, arrow); idx > 0 { 27 | tc = append(tc, testCase{s[:idx], s[idx+len(arrow):]}) 28 | } 29 | } 30 | } 31 | if tc == nil { 32 | t.Fatalf("%s has not test cases", f) 33 | } 34 | return tc 35 | } 36 | func TestToken(t *testing.T) { 37 | for _, tc := range tcFile(t, "token.go") { 38 | tok, _ := token([]byte(tc.in)) 39 | fmt.Printf("%s → ", tc.in) 40 | var r string 41 | if len(tok) == 1 { 42 | r = fmt.Sprint(tok[0]) 43 | } else { 44 | r = fmt.Sprint(tok) 45 | } 46 | if r != tc.exp { 47 | t.Fatalf("expected %s got %s", tc.exp, r) 48 | } 49 | fmt.Println(r) 50 | } 51 | } 52 | 53 | func TestFloat(t *testing.T) { 54 | testCases := []struct { 55 | s string 56 | n int 57 | }{ 58 | {"1.23", 4}, 59 | {"0.a", 2}, 60 | {"0.", 2}, 61 | {".0", 2}, 62 | {".", 0}, 63 | {"a123", 0}, 64 | {"1e2", 3}, 65 | {"1ea", 2}, 66 | {"1a30", 1}, 67 | } 68 | for _, tc := range testCases { 69 | fmt.Println(tc.s) 70 | if n := sFloat([]byte(tc.s)); n != tc.n { 71 | t.Fatalf("%s: expected %d got %d", tc.s, tc.n, n) 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /_/g2/parse/type.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strings" 5 | "time" 6 | ) 7 | 8 | type A interface{} 9 | type Byte []byte // "a" "abc" 0x12ef "" " " `b 32 10 | type Symbol []string // `a`b `"alpha" 0#` ` 11 | type Number []float64 // 1 1.0 1.1 -1.23e-002 !0 0n 12 | type Complex []complex128 // 1.2a310 2a 0#0a 0na 13 | type Time []time.Time // 2021.03.11T12:15:17 0#0T 0T 14 | type Duration []time.Duration // 2.5h 2ms 3us 0#0s 0s 15 | type List []A // (1;2 3;4.5) () "" 16 | type Dict [2]A // `a`b!(1;2 3) 17 | type Table [2]A // +`a`b!(1 2;3 4) 18 | type Verb string // + / ': 19 | type Func struct{} // {x+y} 20 | type Derived struct{ Verb, Adverb Verb } // +/ 21 | type Projection []A // +[;3] 2- 22 | type Composition []A // +- 23 | type F1 func(x A) A // native functions 24 | type F2 func(x, y A) A 25 | type F3 func(x, y, z A) A 26 | 27 | type SrcError struct { 28 | Src []byte 29 | Pos int 30 | Err error 31 | } 32 | 33 | func (s SrcError) Error() string { 34 | o := 0 35 | for i := 0; i < s.Pos; i++ { 36 | if s.Src[i] == 10 { 37 | o = i + 1 38 | } 39 | } 40 | s.Pos -= o 41 | s.Src = s.Src[o:] 42 | for i, c := range s.Src { 43 | if c == 10 && i > s.Pos { 44 | s.Src = s.Src[:i] 45 | break 46 | } 47 | } 48 | 49 | if s.Pos > 30 { 50 | n := s.Pos - 20 51 | s.Pos -= n 52 | s.Src = s.Src[20:] 53 | } 54 | if len(s.Src) > 70 { 55 | s.Src = append(s.Src[:70], '.', '.') 56 | } 57 | space := "" 58 | if s.Pos > 0 { 59 | space = strings.Repeat(" ", s.Pos) 60 | } 61 | return string(s.Src) + "\n" + space + "^" + s.Err.Error() 62 | } 63 | -------------------------------------------------------------------------------- /_/g2/src/a/a_test.go2: -------------------------------------------------------------------------------- 1 | package a 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestA(t *testing.T) { 9 | o := fmt.Println 10 | o(Til(5)) 11 | o(Take(2, Til(5))) 12 | o(Take(7, Til(5))) 13 | o(Take(-2, Til(5))) 14 | o(Add([]int{1, 2, 3}, []int{4, 5, 6})) 15 | o(Add1([]int{1, 2, 3}, 4)) 16 | o(Sub1([]float64{1, 2, 3}, 4)) 17 | o(Enlist(Til(5))) 18 | o(Negate([]uint8{1, 2, 3})) 19 | o(Cut(5, []int{1,2,3})) 20 | o(Cut(3, Til(15))) 21 | } -------------------------------------------------------------------------------- /_/g2/src/a/mk: -------------------------------------------------------------------------------- 1 | go2go build 2 | go2go test 3 | -------------------------------------------------------------------------------- /_/g2/src/k/k.go2: -------------------------------------------------------------------------------- 1 | package k 2 | 3 | import ( 4 | "a" 5 | "strconv" 6 | ) 7 | 8 | type K struct { 9 | A interface{} 10 | count int 11 | } 12 | type I struct { 13 | Monads map[string][10]Monad 14 | Tokenizers []Tokenizer 15 | } 16 | type V byte 17 | 18 | func New() *I { 19 | var i I 20 | i.Tokenizers = []Tokenizer{Whitespace, Hex, Number, Verb, i.Builtins} 21 | return &i 22 | } 23 | 24 | type Monad func(K) K 25 | 26 | var Monadic = map[byte][12]Monad{ 27 | '*': {id, id, id, id, id, id, first[bool], first[byte], first[int], first[float64], first[complex128], first[string]}, 28 | } 29 | 30 | func Eval(s string) K { 31 | b := []byte(s) 32 | f := Monadic[b[0]] 33 | v := parse(string(b[1:])) 34 | t := Type(v) 35 | return f[t](v) 36 | } 37 | 38 | func parse(s string) K { 39 | if n, err := strconv.Atoi(s); err == nil { 40 | return K{A: n} 41 | } 42 | if n, err := strconv.ParseFloat(s, 64); err == nil { 43 | return K{A: n} 44 | } 45 | // ... 46 | return K{} 47 | } 48 | 49 | func Type(x K) int { 50 | switch x.A.(type) { 51 | case bool: 52 | return 0 53 | case byte: 54 | return 1 55 | case int: 56 | return 2 57 | case float64: 58 | return 3 59 | case complex128: 60 | return 4 61 | case string: 62 | return 5 63 | case []bool: 64 | return 6 65 | case []byte: 66 | return 7 67 | case []int: 68 | return 8 69 | case []float64: 70 | return 9 71 | case []complex128: 72 | return 10 73 | case []string: 74 | return 11 75 | default: 76 | return -1 77 | } 78 | } 79 | 80 | func id(x K) K { return x } 81 | 82 | func first[T any](x K) (r K) { 83 | r.A = a.First(x.A.([]T)) 84 | return r 85 | } 86 | 87 | 88 | 89 | /* 90 | func I() int { 91 | return First(Reverse(Til(5))) 92 | } 93 | */ -------------------------------------------------------------------------------- /_/g2/src/k/k_test.go2: -------------------------------------------------------------------------------- 1 | package k 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestK(t *testing.T) { 9 | o := fmt.Println 10 | o(Eval("*3")) 11 | o(Eval("*3.14")) 12 | } 13 | -------------------------------------------------------------------------------- /_/g2/src/k/mk: -------------------------------------------------------------------------------- 1 | export GO2PATH=/c/k/i/_/g2 2 | go2go build 3 | go2go test 4 | -------------------------------------------------------------------------------- /_/gw/go.mod: -------------------------------------------------------------------------------- 1 | module gw 2 | 3 | go 1.16 4 | 5 | require github.com/go-interpreter/wagon v0.6.0 6 | -------------------------------------------------------------------------------- /_/gw/go.sum: -------------------------------------------------------------------------------- 1 | github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw= 2 | github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= 3 | github.com/go-interpreter/wagon v0.6.0 h1:BBxDxjiJiHgw9EdkYXAWs8NHhwnazZ5P2EWBW5hFNWw= 4 | github.com/go-interpreter/wagon v0.6.0/go.mod h1:5+b/MBYkclRZngKF5s6qrgWxSLgE9F5dFdO1hAueZLc= 5 | github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI= 6 | github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A= 7 | golang.org/x/sys v0.0.0-20190306220234-b354f8bf4d9e h1:UndnRDGP/JcdZX1LBubo1fJ3Jt6GnKREteLJvysiiPE= 8 | golang.org/x/sys v0.0.0-20190306220234-b354f8bf4d9e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 9 | -------------------------------------------------------------------------------- /_/gw/ktye/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | w < ../../i3/k.w | gw | gofmt > k_.go 4 | go build 5 | 6 | # func names: w -fout < ../../i3/k.w 7 | # f152 is fxp 8 | -------------------------------------------------------------------------------- /_/gw/test/gen.go: -------------------------------------------------------------------------------- 1 | //+build ignore 2 | 3 | package main 4 | 5 | // generate out_test.go from t.w 6 | 7 | import ( 8 | "bufio" 9 | "bytes" 10 | "fmt" 11 | "io" 12 | "io/ioutil" 13 | "os" 14 | "strings" 15 | ) 16 | 17 | func main() { 18 | w := os.Stdout 19 | w.Write([]byte(head)) 20 | b, e := ioutil.ReadFile("t.w") 21 | fatal(e) 22 | scn := bufio.NewScanner(bytes.NewReader(b)) 23 | for scn.Scan() { 24 | s := scn.Text() 25 | ci := strings.Index(s, ":") 26 | if len(s) > 0 && s[0] == 'f' && ci > 0 { 27 | f := s[:ci] 28 | v := strings.Split(scn.Text(), "\t") 29 | if len(v) > 2 { 30 | arg := v[len(v)-2][1:] 31 | exp := v[len(v)-1] 32 | a := strings.Split(arg, " ") 33 | writeTest(f, a, exp, w) 34 | } 35 | } 36 | } 37 | } 38 | func fatal(e error) { 39 | if e != nil { 40 | panic(e) 41 | } 42 | } 43 | func writeTest(f string, a []string, exp string, w io.Writer) { 44 | fmt.Fprintf(w, "func TestF%s(t *testing.T) {\n\tr := %s(%s)\n", f, f, strings.Join(a, ", ")) 45 | fmt.Fprintf(w, "\t"+`if s := fmt.Sprintf("%%v", r); s != "%s" { t.Fatalf("%s: %s != %%s", s)`, exp, f, exp) 46 | fmt.Fprintf(w, "\n\t} else { fmt.Println(%q, r) }\n}\n", f) 47 | } 48 | 49 | const head = `package main 50 | import ( 51 | "testing" 52 | "fmt" 53 | ) 54 | 55 | // imported functions 56 | func f0(x uint32) uint32 { i := int32(x); return uint32(-i) } 57 | func f1(x, y uint32) uint32 { return x+y } 58 | 59 | ` 60 | -------------------------------------------------------------------------------- /_/gw/test/mk: -------------------------------------------------------------------------------- 1 | go run gen.go > out_test.go 2 | w < t.w | gw > out.go 3 | go fmt 4 | go test 5 | -------------------------------------------------------------------------------- /_/gw/test/t.w: -------------------------------------------------------------------------------- 1 | neg:I:I{}add:I:II{} 2 | f2:I:II{x+y} /1 2 3 3 | f3:I:II{x-y} /1 2 4294967295 4 | f4:I:I{a:x;1+a} /3 4 5 | f5:I:II{x f2 y} /1 2 3 6 | f6:I:II{x add y} /1 2 3 7 | f7:I:I{neg 0-x} /3 3 8 | f8:I:II{(I.2*x)(x;y)} /5 2 7 9 | f9:I:II{r:0;x/(r+:y);r} /3 5 15 10 | f10:I:I{I x} /0 1234 11 | f11:I:I{I x} /4 5678 12 | f12:F:I{F x} /8 1.2e-125 13 | f13:I:III{x? :y;z} /1 2 3 2 14 | f14:I:III{x? :y;z} /0 2 3 3 15 | f15:I:III{$[0; :1;x; :y; :z];7} /0 1 2 2 16 | f16:I:III{$[0; :1;x; :y; :z];7} /1 2 3 2 17 | f17:I:I{~x} /1 0 18 | f18:I:II{x~y} /2 2 1 19 | f19:I:II{x'y-3} /1 2 1 22 | f22:I:II{x>y} /1 2 0 23 | 24 | 0!{d20400002e160000d6609c29013f0026}/1234 5678 1.2e-125 25 | 10:{f2;f3;f4} -------------------------------------------------------------------------------- /_/i1/a_test.go: -------------------------------------------------------------------------------- 1 | package i 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestEx(t *testing.T) { 8 | testCases := []struct { 9 | i v 10 | t rT 11 | r v 12 | }{ 13 | {zv{1, 2, 3}, rtyp([]bool{}), []bool{true, true, true}}, 14 | {iv{1, 2, 3}, rtyp([]uint32{}), []uint32{1, 2, 3}}, 15 | {zv{1, 2 + 1i, 3}, rtyp([]float64{}), fv{1, 2, 3}}, 16 | {[2]l{l{"B", "F", "V"}, l{-0.0, 2.3, zv{3, 4, 5}}}, rtyp(Mystruct{}), Mystruct{false, 2.3, []myint{3, 4, 5}}}, 17 | { 18 | [2]l{l{"A", "S"}, l{c(3, 0), [2]l{l{"B", "F", "V"}, l{true, 2.3, zv{1, 2, 3}}}}}, 19 | rtyp(nested{}), 20 | nested{3, Mystruct{true, 2.3, []myint{1, 2, 3}}}, 21 | }, 22 | { 23 | [2]l{l{"Mystruct"}, l{[2]l{l{"F", "V"}, l{2.3, zv{1, 2, 3}}}}}, 24 | rtyp(embed{}), 25 | embed{Mystruct: Mystruct{F: 2.3, V: []myint{1, 2, 3}}}, 26 | }, 27 | { 28 | [2]l{l{"alpha", "beta"}, l{1.0, 2.0}}, 29 | rtyp(mymap{}), 30 | mymap{"alpha": 1, "beta": 2}, 31 | }, 32 | { 33 | [2]l{l{"Type", "Lines"}, l{"abc", l{[2]l{l{"A"}, l{1}}, [2]l{l{"A"}, l{2.0}}}}}, 34 | rtyp(Plot{}), 35 | Plot{Type: "abc", Lines: []Line{Line{1}, Line{2}}}, 36 | }, 37 | } 38 | for _, tc := range testCases { 39 | r := ex(tc.i, tc.t) 40 | tt(t, tc.r, r, "ex %+v %s: %+v\n", tc.i, tc.t, tc.r) 41 | } 42 | } 43 | 44 | type Plot struct { 45 | Type string 46 | Lines []Line 47 | } 48 | type Line struct { 49 | A int 50 | } 51 | -------------------------------------------------------------------------------- /_/i1/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/ktye/i 2 | 3 | go 1.12 4 | -------------------------------------------------------------------------------- /_/i1/n_test.go: -------------------------------------------------------------------------------- 1 | package i 2 | 3 | import "testing" 4 | 5 | func TestSn(t *testing.T) { 6 | testCases := []struct { 7 | n s 8 | x v 9 | c bool 10 | r zv 11 | }{ 12 | {"sn", "", false, zv{0}}, 13 | {"sn", "a", false, zv{0}}, 14 | {"sn", sv{"a", "b"}, true, zv{0, 1}}, 15 | {"sn", sv{"b", "a"}, true, zv{1, 0}}, 16 | {"sn", sv{"x", "x", "", "b", "w", "b"}, true, zv{3, 3, 0, 1, 2, 1}}, 17 | } 18 | for _, tc := range testCases { 19 | r, c, ok := sn(tc.x) 20 | if !ok { 21 | t.Fatal() 22 | } else if c != tc.c { 23 | t.Fatalf("%s: scalar/vector test failed", tc.n) 24 | } 25 | tt(t, tc.r, r, "sn %+v: %+v\n", tc.x, r) 26 | } 27 | } 28 | func TestSn2(t *testing.T) { 29 | testCases := []struct { 30 | n s 31 | x, y v 32 | rx, ry v 33 | }{ 34 | {"sn2", "", "", z0, z0}, 35 | {"sn2", "a", 13, "a", 13}, 36 | {"sn2", "a", "a", z0, z0}, 37 | {"sn2", "a", "b", z0, z1}, 38 | {"sn2", sv{"b", "a"}, "a", zv{1, 0}, zv{0}}, 39 | {"sn2", sv{"x", "x", "", "b", "w", "b"}, sv{"b", "a", "c", "z", "z", "z"}, zv{5, 5, 0, 2, 4, 2}, zv{2, 1, 3, 6, 6, 6}}, 40 | } 41 | for _, tc := range testCases { 42 | rx, ry := sn2(tc.x, tc.y) 43 | tt(t, tc.rx, rx, "sn2x %+v: %+v\n", tc.x, rx) 44 | tt(t, tc.rx, rx, "sn2y %+v: %+v\n", tc.y, ry) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /_/i1/⍳/README: -------------------------------------------------------------------------------- 1 | 'i' by itself is a go package. It does not run on it's own. 2 | It does not even have a system interface (no read, write, argv...). 3 | 4 | ⍳ is a command line application, one example of using 'i'. 5 | 6 | go build 7 | ./⍳ 8 | 9 | A gui front-end including an interactive plotter and an editor is at: 10 | github.com/ktye/ui/examples/interpret 11 | 12 | Extensions: time.go is an example how to add an external type (go time type) to i. 13 | By providing this interface, the interpreter can construct and convert time values 14 | with $. 15 | Go methods on custom types can be called as well, without having to provide an interface. 16 | -------------------------------------------------------------------------------- /_/i1/⍳/dev.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // devvars(device variables) 4 | // ⍳ defines: iv(k-version), io(read,write) 5 | // others: im(image:pixel buffer), pl(ot), ed(itor) planned for github.com/ktye/ui/examples/interpret 6 | 7 | func iv(x, y v) v { // k-version. ⍳ returns 1 8 | if x == nil { 9 | return 1 10 | } 11 | return nil 12 | } 13 | 14 | func devvars(a map[v]v) { // register devvars in k-tree 15 | a["devvars"] = map[s]func(v, v) v{ 16 | "iv": iv, 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /_/i1/⍳/file.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "strconv" 6 | "strings" 7 | 8 | "github.com/ktye/i" 9 | ) 10 | 11 | func file(name string, b []byte, a map[v]v) { 12 | file := name 13 | defer func() { 14 | if c := recover(); c != nil { 15 | stk, err := stack(c) 16 | println(stk) 17 | println(file + ":" + err) 18 | } 19 | }() 20 | lines := bytes.Split(b, []byte("\n")) 21 | for n := range lines { 22 | file = name + ":" + strconv.Itoa(n+1) 23 | s := string(lines[n]) 24 | if strings.TrimSpace(s) == "" { 25 | continue 26 | } 27 | i.E(i.P(s), a) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /_/i1/⍳/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/ktye/i/⍳ 2 | 3 | go 1.12 4 | 5 | require github.com/ktye/i v0.0.0 6 | 7 | replace github.com/ktye/i => ../ 8 | -------------------------------------------------------------------------------- /_/i1/⍳/go.sum: -------------------------------------------------------------------------------- 1 | github.com/ktye/i v0.0.0-20190511092213-9c22ef09a4ee h1:tQA2OPdgblGzcqLhllcxtxgtbnkBO+0tmB4hx2QXaWM= 2 | github.com/ktye/i v0.0.0-20190511092213-9c22ef09a4ee/go.mod h1:PD++Z9OoJZXZ/xu8bUZd8uf8GWPYHM8ofUW+XR34qJM= 3 | github.com/ktye/i v0.0.0-20190514150949-32fedfeefbda h1:QOsTSiz3vcU6DMGbBOSHgxylGiSfTYgEDAssPDEymgk= 4 | github.com/ktye/i v0.0.0-20190514150949-32fedfeefbda/go.mod h1:PD++Z9OoJZXZ/xu8bUZd8uf8GWPYHM8ofUW+XR34qJM= 5 | github.com/ktye/i v0.0.0-20190514152634-592d196ee5bc h1:1ige0scByxza8XMbUEbyn/lJnP1HZ8gXql6Z3bjGs80= 6 | github.com/ktye/i v0.0.0-20190514152634-592d196ee5bc/go.mod h1:PD++Z9OoJZXZ/xu8bUZd8uf8GWPYHM8ofUW+XR34qJM= 7 | github.com/ktye/i v0.0.0-20190515165356-d0f5b33b5e4f h1:rWRolwyv8ltB8Vyc6O+EzPLFR4R7KOiySHFZrpLiYhk= 8 | github.com/ktye/i v0.0.0-20190515165356-d0f5b33b5e4f/go.mod h1:PD++Z9OoJZXZ/xu8bUZd8uf8GWPYHM8ofUW+XR34qJM= 9 | -------------------------------------------------------------------------------- /_/i1/⍳/io.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | // in converts the string b (read from stdin) to data depending on x. 8 | // The function is present in the variable 'i' if ⍳ is used in filter mode: 9 | // cat data | ⍳ 'i 0' 10 | // Conversions 11 | // i 0 → sv{} 12 | // i "" → l{sv{}} / split at whitespace 13 | // i ";" → l{sv{}} / split at ; 14 | // i`n → as a numeric table: l{zv{...}} 15 | // i`n"," → split at "," 16 | // i`d`n`n`s";" → dict from csv header line / TODO 17 | // i`j → json / TODO 18 | func in(x v, b string) v { 19 | if b[len(b)-1] == '\n' { 20 | b = b[:len(b)-1] 21 | } 22 | lines := strings.Split(b, "\n") 23 | var xv []string 24 | if xs, o := x.(string); o { 25 | xv = []string{xs} 26 | } else { 27 | if xx, o := x.([]string); !o || len(xx) == 0 { 28 | return lines 29 | } else { 30 | xv = xx 31 | } 32 | } 33 | sep := "" 34 | if s := xv[len(xv)-1]; s == "\t" || s == ";" || s == "," || s == "|" { 35 | sep = s 36 | xv = xv[:len(xv)-1] 37 | } 38 | var r []v 39 | for i := range lines { 40 | var t []string 41 | if sep == "" { 42 | t = strings.Fields(lines[i]) 43 | } else { 44 | t = strings.Split(lines[i], sep) 45 | } 46 | row := make([]v, len(t)) 47 | for k := range row { 48 | row[k] = t[k] 49 | if (len(xv) == 1 && xv[0] == "n") || (len(xv) > i && xv[i] == "n") { 50 | row[k] = num(t[k]) 51 | } 52 | } 53 | r = append(r, uf(row)) 54 | } 55 | return r 56 | } 57 | 58 | func uf(x []v) v { 59 | n := make([]complex128, len(x)) 60 | for i := range x { 61 | if z, o := x[i].(complex128); o { 62 | n[i] = z 63 | } else { 64 | return x 65 | } 66 | } 67 | return n 68 | } 69 | -------------------------------------------------------------------------------- /_/i1/⍳/run.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io/ioutil" 5 | "os" 6 | "runtime/debug" 7 | "strings" 8 | 9 | "github.com/ktye/i" 10 | ) 11 | 12 | type s = string 13 | type v = interface{} 14 | type l = []v 15 | 16 | var fmt func(v) v 17 | var jon func(v, v) v 18 | var num func(v) v 19 | 20 | func stack(c interface{}) (stk, err string) { 21 | for _, s := range strings.Split(string(debug.Stack()), "\n") { 22 | if strings.HasPrefix(s, "\t") { 23 | stk += "\n" + s[1:] 24 | } 25 | } 26 | err = "?" 27 | if s, o := c.(string); o { 28 | err = s 29 | } else if e, o := c.(error); o { 30 | err = e.Error() 31 | } 32 | return stk, err 33 | } 34 | func run(t string, a map[v]v) (r interface{}) { 35 | defer func() { 36 | if c := recover(); c != nil { 37 | a, b := stack(c) 38 | r = a + "\n" + b 39 | } 40 | }() 41 | pr := i.P(t) 42 | return i.E(pr, a) 43 | } 44 | 45 | func kinit() map[v]v { 46 | a := make(map[v]v) 47 | i.E(l{}, a) 48 | fmt = a["$:"].(func(x v) v) 49 | jon = a["jon"].(func(x, y v) v) 50 | num = a["num"].(func(x v) v) 51 | 52 | a["i"] = func(x v) v { 53 | b, _ := ioutil.ReadAll(os.Stdin) 54 | return in(x, string(b)) 55 | } 56 | // Custom output formatters for interactive use: o$... 57 | a["o"] = map[v]v{ 58 | "p": 6, // precision 59 | "a": 0, // polar complex degree precision 60 | "t": 1, // tables if possible 61 | "d": 1, // multiline dicts 62 | "m": 1, // matrix 63 | "l": 0, // nested list 64 | "q": 1, // auto quote 65 | } 66 | a["t"] = regtime() 67 | a["x"] = T(1.0) 68 | devvars(a) 69 | return a 70 | } 71 | 72 | type T float64 73 | 74 | func (t T) Inc() T { return t + 1.0 } 75 | func (t T) Add(b T) T { return t + b } 76 | -------------------------------------------------------------------------------- /_/i1/⍳/time.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "time" 4 | 5 | func regtime() timestamp { 6 | return timestamp{Time: time.Now()} 7 | } 8 | 9 | type timestamp struct { 10 | time.Time 11 | } 12 | 13 | func (t timestamp) cpy() timestamp { 14 | return t 15 | } 16 | 17 | func (t timestamp) String() string { 18 | return t.Format("2006.01.02T15:04:05") 19 | } 20 | 21 | // ConvertTo is used to convert values to time by dyadic $ 22 | // t$0 / current time 23 | // t$20340293 / seconds since y2k 24 | // t$"2019.04.04T21:22:33" / from string 25 | // t$2019 04 04 / from a numeric vector ([]complex128) 26 | // By default "t" stores a time value. 27 | func (t timestamp) ConvertTo(u v) v { 28 | if s, o := u.(s); o { 29 | tm, err := time.Parse(s, "2006.01.02T15:04:05") 30 | if err != nil { 31 | panic(err) 32 | } 33 | return timestamp{tm} 34 | } else if z, o := u.(complex128); o { 35 | sec := real(z) 36 | if sec == 0 { 37 | return timestamp{time.Now()} 38 | } 39 | return timestamp{y2k.Add(time.Duration(float64(time.Second) * sec))} 40 | } else if vec, o := u.([]complex128); o { 41 | w := [8]int{0, 1, 1, 0, 0, 0, 0, 0} 42 | for i := range vec { 43 | w[i] = int(real(vec[i])) 44 | } 45 | return timestamp{time.Date(w[0], time.Month(w[1]), w[2], w[3], w[4], w[5], w[6], nil)} 46 | } else { 47 | panic("type") 48 | } 49 | } 50 | 51 | var y2k time.Time 52 | 53 | func init() { 54 | y2k, _ = time.Parse("2006.01.02", "2000.01.01") 55 | } 56 | -------------------------------------------------------------------------------- /_/i1/⍳/⍳.go: -------------------------------------------------------------------------------- 1 | // ⍳ interpret 2 | package main 3 | 4 | import ( 5 | "bufio" 6 | "io/ioutil" 7 | "os" 8 | 9 | "github.com/ktye/i" 10 | ) 11 | 12 | // args: 13 | // 0: read from stdin, execute each line, continue on error 14 | // filename: execute file, exit on error 15 | // else: exec argv 16 | 17 | func main() { 18 | a := kinit() 19 | a["print"] = p 20 | 21 | if len(os.Args) > 1 { 22 | if b, err := ioutil.ReadFile(os.Args[1]); err == nil { 23 | file(os.Args[1], b, a) 24 | } else { 25 | p(i.E(i.P(jon(" ", os.Args[1:]).(string)), a)) 26 | } 27 | return 28 | } 29 | 30 | r := bufio.NewScanner(os.Stdin) 31 | for r.Scan() { 32 | p(run(r.Text(), a)) 33 | } 34 | } 35 | func p(x v) { // print 36 | if x == nil { 37 | return 38 | } 39 | s, o := x.(string) 40 | if !o { 41 | s = fmt(x).(string) 42 | } 43 | // s = sxl(s) convert "data:image/png;base64..." to sixel (see github.com/ktye/ui/examples/interpret/sixel.go) 44 | println(s) 45 | } 46 | -------------------------------------------------------------------------------- /_/i2/alt: -------------------------------------------------------------------------------- 1 | com/exe 2 | 3 | 0 n /create list from n args if top==0 4 | 'a' sf /assign top to s(symbol), modify with f if ~0 5 | 'A' sf /amend top+1 to s at index(top) 6 | 'B' sf /dmend top+1 to s at depth-index(top) 7 | 'c' x /push x on stack 8 | 'g' n /get global, push on stack (nyi by-index) 9 | ';' /dec top 10 | 'i' n /jump +n if is0(top) 11 | 'j' n /jump +n 12 | 'D' op /create derived from func on top with op 13 | 'd' f /call(derived) f with 1 arg from top 14 | 'd'+dy f /call(derived) f with 2 args from top 15 | 'x' n /call top with n args (on stack before top) 16 | u /call(monadic) u if u<128 with arg from top 17 | v /call(dyadic) v if v<256 with 2 args from top 18 | 19 | -------------------------------------------------------------------------------- /_/i2/k-/a.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "syscall" 4 | 5 | func main() { 6 | ini(make([]f, 1<<13)) 7 | tab1['1'] = red 8 | tab1['q'] = exi 9 | tab2['1'] = wrt 10 | for { 11 | evp(red(wrt(inc(nans), enl(mkc(' '))))) // r: 1: (` 1: ," ") 12 | } 13 | } 14 | func grw(c k) { 15 | if 2*len(m.f) <= cap(m.f) { 16 | m.f = m.f[:2*len(m.f)] 17 | } else { 18 | x := make([]f, 2*len(m.f), c/4) 19 | copy(x, m.f) 20 | m.f = x 21 | } 22 | } 23 | func red(x k) (r k) { // 1:x 24 | var a [1024]c // don't write longer lines than this 25 | b := a[:] 26 | if n, err := syscall.Read(syscall.Stdin, b); err != nil { 27 | panic(err) 28 | } else if n > 1 { 29 | return dex(x, mkb(b[:n-1])) 30 | } else if n == 1 { 31 | return dex(x, mk(C, 0)) 32 | } else { 33 | exi(0) 34 | } 35 | return 0 36 | } 37 | func wrt(x, y k) (r k) { // x 1:y 38 | t, n := typ(y) 39 | if t != C || n == atom { 40 | panic("type") 41 | } 42 | p := 8 + y<<2 43 | print(s(m.c[p : p+n])) // stderr only 44 | return dex(y, x) 45 | } 46 | func exi(x k) (r k) { panic("ciao") } 47 | -------------------------------------------------------------------------------- /_/i2/k-/readme: -------------------------------------------------------------------------------- 1 | # k. interactive, no filesystem, no argv, panics 2 | # uses packages unsafe, math, syscall(for stdin) 3 | # no os, fmt, strconv.. 4 | 5 | # install go-1.13 6 | # sh readme 7 | cp ../k.go . 8 | printf "module github.com/ktye/i/_/k\n\ngo 1.13" > go.mod 9 | GOPATH="" GO111MODULE=on go build -ldflags="-s -w" 10 | # there should now be native binary k(.exe) ~1MB 11 | -------------------------------------------------------------------------------- /_/i2/k/readme: -------------------------------------------------------------------------------- 1 | # k 2 | 3 | # install go-1.13 4 | # sh readme 5 | cp ../k.go . 6 | printf "module github.com/ktye/i/_/k\n\ngo 1.13\n\nrequire github.com/ktye/plot v0.0.0-20190730163230-f3b68a4d84f8" > go.mod 7 | 8 | GOPATH="" GO111MODULE=on go build -ldflags="-s -w" 9 | # there should now be an executable k(.exe) 10 | -------------------------------------------------------------------------------- /_/i2/k/w.go: -------------------------------------------------------------------------------- 1 | // +build !windows 2 | 3 | package main 4 | 5 | func drw(x, y k) (r k) { // x 9: y 6 | panic("no-display") 7 | } 8 | -------------------------------------------------------------------------------- /_/i2/kui/f/10x20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ktye/i/d9e35635b885e93573353fa32d2ab0176cef432b/_/i2/kui/f/10x20.png -------------------------------------------------------------------------------- /_/i2/kui/f/16x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ktye/i/d9e35635b885e93573353fa32d2ab0176cef432b/_/i2/kui/f/16x32.png -------------------------------------------------------------------------------- /_/i2/kui/f/k.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ktye/i/d9e35635b885e93573353fa32d2ab0176cef432b/_/i2/kui/f/k.png -------------------------------------------------------------------------------- /_/i2/kui/f/lstr.10: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ktye/i/d9e35635b885e93573353fa32d2ab0176cef432b/_/i2/kui/f/lstr.10 -------------------------------------------------------------------------------- /_/i2/kui/f/lstr.12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ktye/i/d9e35635b885e93573353fa32d2ab0176cef432b/_/i2/kui/f/lstr.12 -------------------------------------------------------------------------------- /_/i2/kui/f/lucx11.font: -------------------------------------------------------------------------------- 1 | 11 9 2 | 0x0000 0x00ff lstr.10 3 | -------------------------------------------------------------------------------- /_/i2/kui/f/lucx11_6x11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ktye/i/d9e35635b885e93573353fa32d2ab0176cef432b/_/i2/kui/f/lucx11_6x11.png -------------------------------------------------------------------------------- /_/i2/kui/f/lucx13.font: -------------------------------------------------------------------------------- 1 | 13 11 2 | 0x0000 0x00FF lstr.12 3 | -------------------------------------------------------------------------------- /_/i2/kui/f/lucx13_7x13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ktye/i/d9e35635b885e93573353fa32d2ab0176cef432b/_/i2/kui/f/lucx13_7x13.png -------------------------------------------------------------------------------- /_/i2/kui/f/mkdek: -------------------------------------------------------------------------------- 1 | fn() 2 | { 3 | echo "${1}:\"${2}\"" 4 | } 5 | 6 | cat << EOD 7 | {deck} 8 | version:1 9 | {fonts} 10 | EOD 11 | 12 | fn f10x20 `go run gen.go 10x20.png decker 10 20` 13 | fn f16x32 `go run gen.go 16x32.png decker 16 32` 14 | fn vt220 `go run gen.go vt220.png decker 10 20` 15 | fn zevv `go run gen.go zevv_8x16.png decker 8 16` 16 | fn lucx11 `go run gen.go lucx11_6x11.png decker 6 11` 17 | fn lucx13 `go run gen.go lucx13_7x13.png decker 7 13` 18 | -------------------------------------------------------------------------------- /_/i2/kui/f/pnginfo.go: -------------------------------------------------------------------------------- 1 | // +build ignore 2 | 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | "strconv" 8 | "image/png" 9 | "bytes" 10 | "os" 11 | ) 12 | 13 | func main() { 14 | if len(os.Args) != 2 { 15 | panic("args") 16 | } 17 | b, e := os.ReadFile(os.Args[1]) 18 | fatal(e) 19 | m, e := png.Decode(bytes.NewReader(b)) 20 | fatal(e) 21 | 22 | hist := make(map[string]int) 23 | 24 | rect := m.Bounds() 25 | for x := rect.Min.X; x=!~,^#_$?@.0123456789'/\;`"(){}[] 8 | 26 abcdefghijklmnopqrstuvwxyz 9 | 26 ABCDEFGHIJKLMNOPQRSTUVWXYZ 10 | 11 | k font files are generated with: 12 | go run gen.go lucx11_6x11.png k 6 11 > luc11.k 13 | go run gen.go lucx13_7x13.png k 7 13 > luc13.k 14 | go run gen.go zevv_8x16.png k 8 16 > f1.k 15 | go run gen.go 10x20.png k 10 20 > f2.k 16 | go run gen.go 16x32.png k 16 32 > f3.k 17 | go run gen.go vt220.png k 10 20 > vt220.k 18 | 19 | /unpack (linear black-pixel indexes) 20 | \l f2.k 21 | font:{&,/+(8#2)\:'0+x}'font 22 | ``` 23 | 24 | # 6x11, 7x13 25 | ![6x11](lucx11_6x11.png) 26 | ![7x13](lucx13_7x13.png) 27 | ``` 28 | from plan9 (lstr.10, lstr.12) (lucidasans?) 29 | go run png.go lucx11.font 30 | go run png.go lucx13.font 31 | ``` 32 | 33 | # 8x16 34 | ![8x16](zevv_8x16.png) 35 | ``` 36 | from zevv, 1px line with 37 | http://zevv.nl/play/code/zevv-peep/zevv-peep-iso8859-1-08x16.bdf > zevv.bdf 38 | go run png.go zevv.bdf 39 | ``` 40 | 41 | # 10x20 42 | ![10x20](10x20.png) 43 | ``` 44 | manual work, 2px line width, modified misc-10x20, taller thicker characters 45 | ``` 46 | 47 | ![vt220](vt220.png) 48 | ``` 49 | from https://vt100.net/dec/vt220/glyphs 50 | ``` 51 | 52 | # 16x32 53 | ![16x32](16x32.png) 54 | ``` 55 | manual work, 3px line width, proportions inspired by PragmataPro 56 | ``` 57 | -------------------------------------------------------------------------------- /_/i2/kui/f/vt220.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ktye/i/d9e35635b885e93573353fa32d2ab0176cef432b/_/i2/kui/f/vt220.png -------------------------------------------------------------------------------- /_/i2/kui/f/zevv_8x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ktye/i/d9e35635b885e93573353fa32d2ab0176cef432b/_/i2/kui/f/zevv_8x16.png -------------------------------------------------------------------------------- /_/i2/kui/fb.k: -------------------------------------------------------------------------------- 1 | /k\ui linux framebuffer driver 2 | 3 | /start with 4 | / stdbuf -oL -eL showkey -a | k fb.k 5 | / (unbuffer pipe, send an input line for each key stroke) 6 | /quit with Cntrl-D 7 | 8 | /load ui application (github.com/ktye/i/u/t.k) 9 | \l t.k 10 | 11 | /this assumes 32 bit pixels (cat /sys/class/graphics/fb0/bits_per_pixel) 12 | (udw;udh):1920 1080 /screen size must match /sys/class/graphics/fb0/virtual_size 13 | 14 | draw:{`"/dev/fb0"1:8_`@x} 15 | draw us[udw;udh] 16 | ky:{v:" "\:x;k:*|v;$[5<#v;;256>k:0+. k;draw uk[k;!0];]} 17 | -1{ky@1:x;`}/` 18 | -------------------------------------------------------------------------------- /_/i2/kui/readme.md: -------------------------------------------------------------------------------- 1 | ``` 2 | k\ui 3 | 4 | the ui is written in k: 5 | uk /key handler 6 | um /mouse handler 7 | us /(re)size handler 8 | 9 | k receives an event, thinks and returns an updated screen. 10 | 11 | many possible backends. only requirements: 12 | - provide a screen (pixel array) 13 | - send resize, key and mouse events to k 14 | optionally 15 | - clipboard (paste) 16 | - drop files 17 | ``` 18 | 19 | # backend 20 | ``` 21 | possible backend implementations 22 | pure k: e.g. 23 | - web (via .Z.G or websocket?) s.k 24 | - /dev/fb0 (linux) fb.k 25 | - /dev/draw.. (plan9) 26 | - sixel (DEC, xterm..) 27 | native application: *.go 28 | - embed k in main or 29 | - connect to unmodified k binary 30 | ``` 31 | 32 | # demo application t.k 33 | ``` 34 | The demo application provides a k terminal with a custom font (f/f2.k). 35 | ``` 36 | 37 | # k interface 38 | ``` 39 | k\ui web(.Z.G) 40 | uk[key;(shift;alt;cntrl)] /k,97,0,0,0 41 | key:printable ascii 42 | bs(8),tab(9),ret(13),esc(27),del(46), 43 | pageUp,pageDown,end,home,left,up,right,down(14..21) 44 | um[button;(x0;x1);(y0;y1);(shift;alt;cntrl))] /m,0,50,50,60,60,0,0,0 45 | button: left,middle,right,wheelUp,wheelDown 0..4 46 | x0 y0 x1 y1: press and release positions (no motion) 47 | us[w;h] /s,1440,1080 48 | resize/layout.. 49 | 50 | events respond with nothing or a frame (row-major): 51 | (w*h)#0 / black screen 52 | (w*h)#255*256 / green.. (alpha byte is ignored) 53 | ``` 54 | -------------------------------------------------------------------------------- /_/i2/kwm/h: -------------------------------------------------------------------------------- 1 | k/wasm (github.com/ktye/i/_/kwm) 2 | 3 | terminal, editor, memfs, draw, animate 4 | 5 | complex: 1i2 2a30 1 2 3a cmplx expi real imag phase conj rand 3i(binormal) 6 | matrix : A\B(solve) A\0(qr) A\1(inv) diag cond 7 | stats : (x)med y(pct/erf/cum) dev z(principal) x var y var z(cov) x avg y(cum/win/exp) 8 | 9 | \h 10 | \v (list vars) 11 | \k (k-tree dump) 12 | \m (expr matrix display) 13 | \b (k memory stats) 14 | 15 | memory filesystem (.fs) <- drop files into the window 16 | \lf (list files, access with io verbs) 17 | \l file (load file k source) 18 | \w file (download file) 19 | \e file (edit ESC to write) 20 | 21 | terminal 22 | return executes current line (anywhere) or selected text(e.g. multiline) 23 | esc toggles hold mode (disable execution) and remove canvas 24 | \c (clear) reset textarea 25 | 26 | draw (w 9:x) 27 | (2/w) 9:(4/w*h) rand 255*256 28 | w and h are predefined with window size at page load 29 | interactive ui (canvas) 30 | .m mouse callback 31 | .k key callback (click to focus) 32 | 33 | animate 34 | \L 500 (ms delay) 35 | \L expr Loop k executing until ESC is pressed 36 | \L 500:expr 37 | -------------------------------------------------------------------------------- /_/i2/kwm/wx.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | // this is a modified version of tinygo/targets/wasm_exe.js 3 | // it writes error messages (panic output) to the kons textarea instead of console.log 4 | if (typeof window !== "undefined") { window.global = window; } else if (typeof self !== "undefined") { self.global = self; } else { throw new Error("cannot export Go (neither window nor self is defined)"); } 5 | global.pnk = ""; 6 | const decoder = new TextDecoder("utf-8"); 7 | var logLine = []; 8 | global.Go = class { 9 | constructor() { 10 | const mem = () => { return new DataView(this._inst.exports.memory.buffer); } 11 | this.importObject = { 12 | env: { 13 | io_get_stdout: function() { return 1; }, 14 | resource_write: function(fd, ptr, len) { 15 | if (fd == 1) { 16 | for (let i=0; i { this._resolveCallbackPromise = () => { if (this.exited) { throw new Error("bad callback: Go program has already exited"); } setTimeout(resolve, 0); }; }); 36 | this._inst.exports.cwa_main(); if (this.exited) { break; } 37 | await callbackPromise; 38 | } 39 | } 40 | _resume() { 41 | if (this.exited) { throw new Error("Go program has already exited"); } 42 | this._inst.exports.resume(); 43 | if (this.exited) { this._resolveExitPromise(); } 44 | } 45 | _makeFuncWrapper(id) { const go = this; return function () { const event = { id: id, this: this, args: arguments }; go._pendingEvent = event; go._resume(); return event.result; }; } 46 | } 47 | })(); 48 | -------------------------------------------------------------------------------- /_/i3/w/gasm_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "math" 7 | "reflect" 8 | "testing" 9 | 10 | "github.com/mathetake/gasm/hostfunc" 11 | "github.com/mathetake/gasm/wasm" 12 | ) 13 | 14 | func TestGasm(t *testing.T) { 15 | t.Skip() // wip 16 | newk := func(b []byte) K { 17 | m, e := wasm.DecodeModule(bytes.NewReader(b)) 18 | if e != nil { 19 | panic(e) 20 | } 21 | vm, e := wasm.NewVM(m, gasmImport()) 22 | if e != nil { 23 | panic(e) 24 | } 25 | return gk{m, vm} 26 | } 27 | ktest(newk, t) 28 | } 29 | 30 | type gk struct { 31 | m *wasm.Module 32 | vm *wasm.VirtualMachine 33 | } 34 | 35 | func (k gk) Memory() []byte { return k.vm.Memory } 36 | func (k gk) Call(s string, argv ...uint32) uint32 { 37 | args := make([]uint64, len(argv)) 38 | for i := range args { 39 | args[i] = uint64(argv[i]) 40 | } 41 | r, t, e := k.vm.ExecExportedFunction(s, args...) 42 | if e != nil { 43 | panic(e) 44 | } 45 | if len(t) != 1 || t[0] != wasm.ValueTypeI32 { 46 | panic("only a single i32 return value is supported") 47 | } 48 | return uint32(r[0]) 49 | } 50 | func gasmImport() map[string]*wasm.Module { 51 | m := hostfunc.NewModuleBuilder() 52 | f := func(m *hostfunc.ModuleBuilder, name string, f reflect.Value) { 53 | m.MustSetFunction("ext", name, func(vm *wasm.VirtualMachine) reflect.Value { return f }) 54 | } 55 | f(m, "sin", reflect.ValueOf(func(x float64) float64 { return math.Sin(x) })) 56 | f(m, "cos", reflect.ValueOf(func(x float64) float64 { return math.Cos(x) })) 57 | f(m, "log", reflect.ValueOf(func(x float64) float64 { return math.Log(x) })) 58 | f(m, "atan2", reflect.ValueOf(func(x, y float64) float64 { return math.Atan2(x, y) })) 59 | f(m, "hypot", reflect.ValueOf(func(x, y float64) float64 { return math.Hypot(x, y) })) 60 | f(m, "draw", reflect.ValueOf(func(x, y, z uint32) { panic("dummy-draw") })) 61 | f(m, "grow", reflect.ValueOf(func(x uint32) uint32 { panic("dummy-grow"); return x })) 62 | f(m, "printc", reflect.ValueOf(func(x, y uint32) { panic("dummy-printc") })) 63 | return m.Done() 64 | } 65 | 66 | func dump(m []byte, n uint32) { 67 | fmt.Printf("%.8x ", 0) 68 | for i := uint32(0); i < n; i++ { 69 | p := 4 * i 70 | x := get(m, p) 71 | fmt.Printf(" %.8x", x) 72 | if i > 0 && (i+1)%8 == 0 { 73 | fmt.Printf("\n%.8x ", p+4) 74 | } else if i > 0 && (i+1)%4 == 0 { 75 | fmt.Printf(" ") 76 | } 77 | } 78 | fmt.Println() 79 | } 80 | -------------------------------------------------------------------------------- /_/i3/w/go.mod: -------------------------------------------------------------------------------- 1 | module w 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/go-interpreter/wagon v0.6.1-0.20200511080254-d9964a5b7175 7 | github.com/mathetake/gasm v0.0.0-20200518122358-22ff530ffa02 8 | ) 9 | -------------------------------------------------------------------------------- /_/i3/w/go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 2 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw= 4 | github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= 5 | github.com/go-interpreter/wagon v0.6.0 h1:BBxDxjiJiHgw9EdkYXAWs8NHhwnazZ5P2EWBW5hFNWw= 6 | github.com/go-interpreter/wagon v0.6.0/go.mod h1:5+b/MBYkclRZngKF5s6qrgWxSLgE9F5dFdO1hAueZLc= 7 | github.com/go-interpreter/wagon v0.6.1-0.20200511080254-d9964a5b7175 h1:Cc2WA8sRCru/yeYnntiNJ0ig54FOzWk9Su+eVDU+YZw= 8 | github.com/go-interpreter/wagon v0.6.1-0.20200511080254-d9964a5b7175/go.mod h1:leeEozQ37ayo4gZ9IqOouSU+XHWaSjibCTNdOdJdAp8= 9 | github.com/mathetake/gasm v0.0.0-20200518122358-22ff530ffa02 h1:BZpy7i2uI3JcV1xyw9VqKYSwsueB3M3AlvskHkMtzYE= 10 | github.com/mathetake/gasm v0.0.0-20200518122358-22ff530ffa02/go.mod h1:EQ4O2t0/Bb8OMXSd+MHBiRNVYaJXvrJlL1P1lIF8fw4= 11 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 12 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 13 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 14 | github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= 15 | github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= 16 | github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc h1:RTUQlKzoZZVG3umWNzOYeFecQLIh+dbxXvJp1zPQJTI= 17 | github.com/twitchyliquid64/golang-asm v0.0.0-20190126203739-365674df15fc/go.mod h1:NoCfSFWosfqMqmmD7hApkirIK9ozpHjxRnRxs1l413A= 18 | golang.org/x/sys v0.0.0-20190306220234-b354f8bf4d9e h1:UndnRDGP/JcdZX1LBubo1fJ3Jt6GnKREteLJvysiiPE= 19 | golang.org/x/sys v0.0.0-20190306220234-b354f8bf4d9e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 20 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 21 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 22 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 23 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 24 | -------------------------------------------------------------------------------- /_/i3/w/min_c: -------------------------------------------------------------------------------- 1 | // minimal embedding example (execute argv[1], 64k) 2 | // $ w -c ../../k.w > k_h 3 | // $ tcc min_c -o min.exe #linux: -lm 4 | // $ ./min.exe '*|!10' ; echo $? 5 | 6 | #include 7 | #include 8 | 9 | #define R return 10 | typedef void V;typedef unsigned char C;typedef uint32_t I;typedef uint64_t J;typedef double F;typedef int32_t SI;typedef int64_t SJ; 11 | I __builtin_clz(I x){I r;__asm__("bsr %1, %0" : "=r" (r) : "rm" (x) : "cc");R r^31;} 12 | C M[1<<16];C *MC;I* MI;J* MJ;F *MF; 13 | V sC(I x,C y){MC[x]=y;}V sI(I x,I y){MI[x>>2]=y;}V sF(I x,F y){MF[x>>3]=y;}V sJ(I x,J y){MJ[x>>3]=y;}; 14 | 15 | V panic(){} 16 | I grow(I x){} 17 | V printc(I x, I y){} 18 | V draw(I x, I y, I z){} 19 | 20 | #undef abs 21 | #undef min 22 | #undef max 23 | 24 | #include"k_h" 25 | 26 | I main(int args, C **argv){ 27 | MC=M; MI=(I*)M; MJ=(J*)M; MF=(F*)M; 28 | for (I i=0;i<1<<(16-2);i++)MI[i]=0; 29 | 30 | mt_init(); //set function pointers 31 | ini(16); //init k memory system 32 | 33 | C *a=argv[1]; 34 | I n=0;for(I i=0;a[i];i++)n++; 35 | I x=mk(1,n);for(I i=0;i>2)]; 39 | } 40 | -------------------------------------------------------------------------------- /_/i3/w/readme: -------------------------------------------------------------------------------- 1 | # webassembly compiler 2 | # 3 | # write wasm by hand, but more compact than wat 4 | # 5 | # the input file compiles to a wasm module. 6 | # it contains a collection of functions. 7 | # 8 | # add:I:II{x+y} /exported function 9 | # sum.I:I {x/x+:i;x} /private function 10 | # mac: {a:3;b:5} /flat macro 11 | # 0: {add;sum} /function table 12 | # sin:F:F {} /imported function (from module "ext") 13 | # 256!{010203ff} /data section 14 | # 15 | # types are I(i32) F(f64) 16 | # no preference apl style x*a+b, but lhs is evaluted first: x a b + * 17 | # arguments x,y,z,x3,x4,.. are typed in the function declaration f:R:XYZ.. 18 | # locals are detected at evaluation order (type is derived) r:x+y 19 | # memory/heap C x;I x;F x (get from addr x) x::y (write y to addr x), also x::C?y 20 | # a;b;c sequence. c(last) returns a typed value all others must return null 21 | # x?y if x then y 22 | # $[a;b;c;d;e] if a then b elseif c then d else e 23 | # x/y n-loop execute-y x-times (x evaluates to I, y must be null) 24 | # creates implicit local i, and n if x is an expression. no nesting. 25 | # x?/y while x do y (y must have not type) 26 | # C?x I?x F?x I?'x type conversions ?'(signed) 27 | # sum x ; x add y named function call, dyad(infix), names can be defined later 28 | # 8:{f;g;h} add f,g,h to function table starting at offset 8 29 | # (I.x+1)(a;b) call indirect: return type I, function index x+1, args a b 30 | # 31 | # see ops for all wasm opcodes and their w operators 32 | 33 | # install go-1.13 34 | go build w.go 35 | ./w < ../k.w > k.wasm # wasm binary 36 | 37 | # or transpile to c or go (no wasm involved): 38 | ./w -c < in.w > src.c 39 | ./w -go < in.w > src.go 40 | 41 | # example: generate k_h and test it 42 | go test 43 | ./runcc t 44 | 45 | # build online interpreter k.html 46 | go test 47 | #browse to file:///.../k.html 48 | 49 | online at ktye.github.io 50 | 51 | # a complete wasm module (11 bytes input / 50 bytes output): 52 | $ echo "f:I:II{x+y}" | w | xxd 53 | 0000000: 0061 736d 0100 0000 0107 0160 027f 7f01 .asm.......`.... 54 | 0000010: 7f03 0201 0005 0301 0001 070b 0203 6d65 ..............me 55 | 0000020: 6d02 0001 6600 000a 0901 0700 2000 2001 m...f....... . . 56 | 0000030: 6a0b ^^^^ ^^^^ j. 57 | ^^add(i32) locX locY 58 | 59 | -------------------------------------------------------------------------------- /_/i3/w/runcc: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | tcc=/local/tcc/tcc 3 | if [ "$1" = "kc" ]; then 4 | $tcc -o kc.exe -DDRW k_c -luser32 -lgdi32 5 | elif [ "$1" = "u" ]; then 6 | $tcc -o u.exe u_c -luser32 -lgdi32 7 | elif [ "$1" = "t" ]; then 8 | sed '/^$/q' t | $tcc -run k_c t | awk '{ 9 | x=$0 10 | getline < "t" 11 | y=$0 12 | if (match(y, /^\//)) next 13 | gsub(/.* \//, "") 14 | if(x!=$0) { 15 | print y"\ngot: "x 16 | exit 17 | } 18 | print y,x 19 | }' 20 | elif [ "$1" = "w" ]; then 21 | $tcc -run k_c -fw k.wasm -fs ../k.w ../w.k -e 'b:wasm compile s;b~w' 22 | else 23 | # go test (generate k_c) 24 | $tcc -run k_c "$*" 25 | fi 26 | -------------------------------------------------------------------------------- /_/i3/w/wasi: -------------------------------------------------------------------------------- 1 | # build standalone wasi module of k.w 2 | set -x 3 | set -e 4 | 5 | wabt=/c/local/wabt 6 | tw=$wabt/wasm2wat 7 | wt=$wabt/wat2wasm 8 | vl=$wabt/wasm-validate 9 | 10 | ( 11 | cat << EOF 12 | write:I:IIII{}read:I:IIII{} 13 | sin.F:F{x}cos.F:F{x}exp.F:F{x}log.F:F{x}atan2.F:FF{x+y}hypot.F:FF{x+y}draw.V:III{x:x+y+z}grow.I:I{!;x} 14 | printc.V:II{p:2 mk 3;(p+8)::x;(p+12)::y;x:write(1;p+8;1;p+16);dx(p)} 15 | O.V:I{v1;xp printc xn;dx x} 16 | rd.I:I{b:1 mk 120;x:2 mk 3;(x+8)::b+8;(x+12)::120;(~0~read(0;x+8;1;x+16))? :0;b:b take (I x+16)-1;dx x;b} 17 | start:I:I{x?x:16;x:ini x;O mkc 32;nl:(mkc 10)cc 32;x:rd 1;x?/(x:kst val x;O x;dx x;rx nl;O nl;x:rd 1);0} 18 | EOF 19 | cat ../../k.w | sed 1d 20 | ) | w > k.wasii 21 | 22 | $tw k.wasii | sed 's/"ext" "/"wasi_unstable" "fd_/' | sed 's/export "mem"/export "memory"/' > k.wat 23 | $wt k.wat -o k.wasi 24 | $vl k.wasi 25 | 26 | ## the _start function signature must be V:V (which is not possible in w) 27 | # e.g. (func $main (export "_start") i32.const 16 call 13 drop) 28 | # 29 | #$ wasm3 k.wasi 30 | #wasm3> start 16 31 | # 1+!10 32 | #1 2 3 4 5 6 7 8 9 10 33 | -------------------------------------------------------------------------------- /_/i4/t/mk: -------------------------------------------------------------------------------- 1 | 2 | ./k.exe ../k.k -e go k > out.go && go fmt out.go && go build out.go 3 | 4 | -------------------------------------------------------------------------------- /_/i4/t/t.awk: -------------------------------------------------------------------------------- 1 | { 2 | x = $0 3 | sub(/^ /, "", x) 4 | getline < "t" 5 | y = $0 6 | if (match(y, /^\//)) next 7 | gsub(/.* \//, "") 8 | if (match($0, /^"[^"]*"$/)) x = "\"" x "\"" 9 | if (x != $0) { 10 | print "t:" NR ": " y " ? " x 11 | exit 1 12 | } 13 | if (x == "`ok") { 14 | print "t ok" 15 | exit 0 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /_/i5/annotate.go: -------------------------------------------------------------------------------- 1 | package k 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | godebug "runtime/debug" 7 | "strings" 8 | ) 9 | 10 | type ctx struct { 11 | src [][]byte 12 | pos []*int 13 | gostack []byte 14 | } 15 | 16 | func (c *ctx) push(s []byte, p *int) { 17 | c.src = append(c.src, s) 18 | c.pos = append(c.pos, p) 19 | } 20 | func (c *ctx) drop() { 21 | c.src = c.src[:len(c.src)-1] 22 | c.pos = c.pos[:len(c.pos)-1] 23 | } 24 | func (c *ctx) restore(f string) { 25 | if r := recover(); r != nil { 26 | c.gostack = godebug.Stack() 27 | c.annotate() 28 | c.drop() 29 | panic(r) 30 | } 31 | c.drop() 32 | } 33 | func (c ctx) annotate() { 34 | if n := len(c.src); n > 0 { 35 | b, p := c.src[n-1], c.pos[n-1] 36 | l, i := trimLine(b, *p) 37 | fmt.Println(l) 38 | sp := "" 39 | if i > 0 && i < len(l) { 40 | sp = strings.Repeat(" ", i) 41 | } 42 | fmt.Println(sp + "^") 43 | } 44 | } 45 | func trimLine(b []byte, p int) (string, int) { 46 | b, p = trimStart(b, p) 47 | i := bytes.IndexByte(b, 10) 48 | if i < 0 { 49 | return string(b), p 50 | } 51 | return string(b[:i]), p 52 | } 53 | func trimStart(b []byte, p int) ([]byte, int) { 54 | for { 55 | i := bytes.IndexByte(b, 10) 56 | if i < 0 || p < i { 57 | return b, p 58 | } 59 | b = b[1+i:] 60 | p -= 1 + i 61 | } 62 | } 63 | func (c ctx) Gostack() []byte { return c.gostack } 64 | -------------------------------------------------------------------------------- /_/i5/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/ktye/k 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /_/i5/json.go: -------------------------------------------------------------------------------- 1 | package k 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | ) 7 | 8 | //? `json?"1 " /1 9 | func decodeJson(x T) T { 10 | c, o := x.(C) 11 | if o == false { 12 | panic("type") 13 | } 14 | dx(x) 15 | 16 | var f interface{} 17 | e := json.Unmarshal(c.v, &f) 18 | if e != nil { 19 | panic(e) 20 | } 21 | return kj(f) 22 | } 23 | 24 | //@ `json 1 2 3 /"[1,2,3]" 25 | //@ `json(1;2 3;4.) /"[1,[2,3],4]" 26 | func encodeJson(x T) T { 27 | defer dx(x) 28 | b, e := json.Marshal(x) 29 | if e != nil { 30 | panic(e) 31 | } 32 | return KC(b) 33 | } 34 | 35 | func kj(f interface{}) T { 36 | switch v := f.(type) { 37 | case bool: 38 | return v 39 | case float64: 40 | if float64(int(v)) == v { 41 | return int(v) 42 | } 43 | return v 44 | case string: 45 | return KC([]byte(v)) 46 | case nil: 47 | return nil 48 | case []interface{}: 49 | r := make([]T, len(v)) 50 | for i := range r { 51 | r[i] = kj(v[i]) 52 | } 53 | return KL(r).uf() 54 | case map[string]interface{}: 55 | key := make([]string, len(v)) 56 | val := make([]T, len(v)) 57 | i := 0 58 | for s, u := range v { 59 | key[i] = s 60 | val[i] = kj(u) 61 | i++ 62 | } 63 | return dict(KS(key), KL(val).uf()) 64 | default: 65 | fmt.Printf("kj %T %v\n", v, v) 66 | panic("type") 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /_/i5/k/k.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | "strings" 8 | "text/tabwriter" 9 | "time" 10 | 11 | "github.com/ktye/k" 12 | ) 13 | 14 | func main() { 15 | k := k.New() 16 | for _, s := range os.Args[1:] { 17 | if strings.HasSuffix(s, ".k") { 18 | load(k, s) 19 | } else { 20 | panic("usage: k [file.k..]") 21 | } 22 | } 23 | repl(k) 24 | } 25 | 26 | func repl(k *k.K) { 27 | s := bufio.NewScanner(os.Stdin) 28 | fmt.Printf("ktye/k\n ") 29 | for s.Scan() { 30 | t := s.Text() 31 | switch t { 32 | case `\\`: 33 | os.Exit(0) 34 | case `\`, `\h`: 35 | fmt.Println("...") 36 | case `\v`: 37 | vars(k) 38 | case `\s`: 39 | fmt.Println(string(k.Gostack())) 40 | case `\d`: 41 | // 42 | default: 43 | var tic time.Time 44 | if strings.HasPrefix(t, `\t`) { 45 | t = t[2:] 46 | tic = time.Now() 47 | } 48 | line(k, t) 49 | if tic.IsZero() == false { 50 | fmt.Println(time.Since(tic)) 51 | } 52 | } 53 | fmt.Printf(" ") 54 | } 55 | } 56 | 57 | func line(k *k.K, s string) { 58 | r := k.Run([]byte(s)) 59 | if r != "" { 60 | fmt.Println(r) 61 | } 62 | } 63 | func load(k *k.K, file string) { 64 | k.Trap = true 65 | defer func() { k.Trap = false }() 66 | b, e := os.ReadFile(file) 67 | if e != nil { 68 | panic(e) 69 | } 70 | if s := k.Run(b); s != "" { 71 | fmt.Println(s) 72 | } 73 | } 74 | func vars(k *k.K) { 75 | name, typ, n, rc := k.Vars() 76 | w := tabwriter.NewWriter(os.Stdout, 2, 8, 1, ' ', 0) 77 | fmt.Fprintf(w, "name\ttype\tlen\trc\n") 78 | for i, s := range name { 79 | fmt.Fprintf(w, "%s\t%s\t%d\t%d\n", s, string(typ[i]), n[i], rc[i]) 80 | } 81 | w.Flush() 82 | } 83 | -------------------------------------------------------------------------------- /_/i5/lambda.go: -------------------------------------------------------------------------------- 1 | package k 2 | 3 | //λ {1+2} /{1+2} 4 | //λ {1+2}[] /3 5 | //λ {x+2}[2] /4 6 | //λ {x+y}[2;3] /5 7 | //λ {[a;b]3*a+b}[3;4] /21 8 | //λ {[]3}[] /3 9 | //λ {(a;y*a:x)}[2;3] /2 6 10 | //λ {a+y*a:x}[2;3] /8 11 | //λ a+{2*a:x}[2]+a:1 /6 (local assign) 12 | //λ a+{2*a::x}[2]+a:1 /7 (global assign) 13 | type λ struct { 14 | refcount 15 | src []byte 16 | code []token 17 | save []T 18 | loc []string 19 | ary int 20 | } 21 | 22 | func (l λ) call(k *K) T { 23 | for i, s := range l.loc { 24 | l.save[i] = k.Var[s] 25 | k.Var[s] = nil 26 | } 27 | for i := l.ary - 1; i >= 0; i-- { 28 | k.Var[l.loc[i]] = k.pop() 29 | } 30 | 31 | for i := range l.code { 32 | rx(l.code[i]) 33 | } 34 | r := k.exec(l.code, l.src) 35 | 36 | for i, s := range l.loc { 37 | if i < l.ary { 38 | dx(k.Var[s]) 39 | } 40 | k.Var[s] = l.save[i] 41 | } 42 | l.unref() 43 | return r 44 | } 45 | func (l λ) dict(k *K) T { 46 | for i, s := range l.loc { 47 | l.save[i] = k.Var[s] 48 | k.Var[s] = nil 49 | } 50 | 51 | for i := range l.code { 52 | rx(l.code[i]) 53 | } 54 | dx(k.exec(l.code, l.src)) 55 | 56 | key := KS(l.loc) 57 | val := KL(make([]T, len(l.loc))) 58 | 59 | for i, s := range l.loc { 60 | val.v[i] = k.Var[s] 61 | k.Var[s] = l.save[i] 62 | } 63 | l.unref() 64 | return dict(key, val.uf()) 65 | } 66 | 67 | func (l λ) unref() { 68 | if l.refcount.unref() == 0 { 69 | for _, c := range l.code { 70 | switch v := c.t.(type) { 71 | case refcounter: 72 | v.unref() 73 | } 74 | } 75 | } 76 | } 77 | 78 | func (l λ) String() string { return string(l.src) } 79 | -------------------------------------------------------------------------------- /_/i5/mat.go: -------------------------------------------------------------------------------- 1 | package k 2 | 3 | import "github.com/ktye/k/mat" 4 | 5 | func qr(x T) T { 6 | defer dx(x) 7 | a := matrix(x.(L)) 8 | switch v := a.(type) { 9 | case [][]float64: 10 | return mat.NewRQ(v) 11 | case [][]complex128: 12 | return mat.NewQR(v) 13 | default: 14 | panic("type") 15 | } 16 | } 17 | func solve(x, y T) T { 18 | if _, o := y.(I); o { 19 | y = uptype(y, floattype) 20 | } 21 | switch v := x.(type) { 22 | case mat.RQ: 23 | if _, o := y.(L); o { 24 | return eachright(f2(solve), x, y) 25 | } else { 26 | y = use(y.(vector)) 27 | return KF(v.Solve(y.(F).v)) 28 | } 29 | case mat.QR: 30 | if _, o := y.(L); o { 31 | return eachright(f2(solve), x, y) 32 | } else { 33 | y = use(y.(vector)) 34 | return KZ(v.Solve(y.(Z).v)) 35 | } 36 | case L: 37 | return solve(qr(x), y) 38 | default: 39 | panic("type") 40 | } 41 | } 42 | func matrix(x L) T { // column major [][]float64, [][]complex128 or panic. 43 | if len(x.v) == 0 { 44 | dx(x) 45 | return make([][]float64, 0) 46 | } 47 | switch v := x.v[0].(type) { 48 | case I: 49 | return matrix(add(x, 0.0).(L)) 50 | case F: 51 | return realmatrix(x, len(v.v)) 52 | case Z: 53 | return complexmatrix(x, len(v.v)) 54 | default: 55 | panic("type") 56 | } 57 | } 58 | func realmatrix(x L, n int) (r [][]float64) { 59 | r = make([][]float64, len(x.v)) 60 | for i := range x.v { 61 | xi := x.v[i].(F) 62 | if len(xi.v) != n { 63 | panic("uniform") 64 | } 65 | r[i] = make([]float64, n) 66 | copy(r[i], xi.v) 67 | } 68 | dx(x) 69 | return r 70 | } 71 | func complexmatrix(x L, n int) (r [][]complex128) { 72 | r = make([][]complex128, len(x.v)) 73 | for i := range x.v { 74 | xi := x.v[i].(Z) 75 | if len(xi.v) != n { 76 | panic("uniform") 77 | } 78 | r[i] = make([]complex128, n) 79 | copy(r[i], xi.v) 80 | } 81 | dx(x) 82 | return r 83 | } 84 | -------------------------------------------------------------------------------- /_/i5/read.go: -------------------------------------------------------------------------------- 1 | package k 2 | 3 | import ( 4 | "os" 5 | "path/filepath" 6 | "regexp" 7 | "strings" 8 | ) 9 | 10 | //read @read"readme.md" /`C (read file) 11 | //read #read(/"\\.md$") /1 (filter cwd) 12 | func read(x T) T { 13 | switch v := x.(type) { 14 | case string: 15 | if v == "" { 16 | v = "." 17 | } 18 | if v == "." || strings.HasSuffix(v, "/") { 19 | return readdir(nil, v) 20 | } 21 | return readfile(v) 22 | case C: 23 | dx(x) 24 | return read(string(v.v)) 25 | case *regexp.Regexp: 26 | return readdir(v, ".") 27 | case L: 28 | return each(f1(read), x) 29 | default: 30 | panic("type") 31 | } 32 | } 33 | 34 | //read @(/"\\.md$")read` /`L (filter dir) 35 | func readdir(x, y T) T { 36 | var re *regexp.Regexp 37 | if x != nil { 38 | var o bool 39 | re, o = x.(*regexp.Regexp) 40 | if !o { 41 | panic("type") 42 | } 43 | } 44 | switch v := y.(type) { 45 | case string: 46 | return readcwd(v, re) 47 | case C: 48 | dx(y) 49 | return readcwd(string(v.v), re) 50 | case L: 51 | return eachright(f2(readdir), re, y) 52 | default: 53 | panic("type") 54 | } 55 | } 56 | 57 | func readfile(f string) C { 58 | b, e := os.ReadFile(f) 59 | if e != nil { 60 | panic(e) 61 | } 62 | return KC(b) 63 | } 64 | func readcwd(dir string, re *regexp.Regexp) L { 65 | var r []T 66 | if dir == "" { 67 | dir = "." 68 | } 69 | d, e := os.ReadDir(dir) 70 | if e != nil { 71 | panic(e) 72 | } 73 | for _, f := range d { 74 | if f.IsDir() == true { 75 | continue // or recursive? 76 | } 77 | if re == nil || re.MatchString(f.Name()) { 78 | r = append(r, readfile(filepath.Join(dir, f.Name()))) 79 | } 80 | } 81 | return KL(r) 82 | } 83 | -------------------------------------------------------------------------------- /_/i7/7.k: -------------------------------------------------------------------------------- 1 | / 7#k ~ parse is compile 2 | 3 | main:{a:1+x;2*a} 4 | 5 | /main:{W[97;1];Q 0} 6 | Q: {s2[60;x] } 7 | W: {s4[1;1;x;y]} 8 | -------------------------------------------------------------------------------- /_/i7/c.k: -------------------------------------------------------------------------------- 1 | /c.k the compiler. 2 | 3 | print:{`<(`l@x),\"\n";x} 4 | 5 | /8(args/scratch/rax:return) 7(saved by callee) 6 | Rx:`rax`rdi`rsi`rdx`rcx`r8`r9`r11`rbp`r10`r11`r12`r13`r14`r15 7 | all:,`main 8 | compile:{(c1'x),\"\n"} 9 | 10 | c1:{t: print asin lift print tf(.x) 11 | ($x),":\n ;",($.x)," todo.."} 12 | 13 | tt:{{x,tr[#x;y]}/(,x),y} /t0;(t1;t2;..) catenate tables 14 | tr:{y[`p]:0|x+y`p} /translate parent vector 15 | t0:+`p`t`i`s!(!0;0#`;!0;0#`) 16 | t1:+`p`t`i`s!(,0N),,' 17 | tf:{ /table from func 18 | n:`n \#x /arity 19 | v:`v \(.x)2 /vars args,locals 20 | b:split(*.x) /body (instruction list) 21 | tt[t0;te'b]} /catenate expression tables 22 | te:{ /table from expression 23 | /mo:{(-1_x),,(t1`op1,1,(`$$y)),tr[1;*|x]} 24 | mo:{(-1_x),,tt[t1`op1,1,(`$$y); -1#x]} 25 | dy:{(-2_x),,tt[t1`op2,2,(`$$y);|-2#x]} 26 | no:{$[`i~t:@x;t1(`lit;x;`);`s~t;t1(`var;0;x);0+`noun]} 27 | f:{$[~`v~@y;x,,no y;64>c:0+y;mo[x;y];dy[x;y]]} 28 | *f/(,t0),x} 29 | 30 | drop:{y x_!#y} /drop xI from yT 31 | shft:{x[`p]-:+/p>/(p:x`p)y} /shift parent to remove yI 32 | lift:{drop[i]shft[x]i:&(`var=1_(x`t),`)&`"."=x`s} /lift var: . var -> var 33 | asin:{drop[i]{shft[y;x]}[i]x[i-1;`t`s]:(,(#i)#`asn),x[i:1+&`"`64"=x`s;`s]} 34 | /asin:{x} 35 | 36 | split:{1_'(&(`256)~/x)^x:(`256),x} 37 | 38 | O:`< /nathanotterness.com/2021/10/tiny_elf_modernized.html 39 | O"[bits 64]\na0: equ 4096 * 40 ;load address 40 | db 0x7f,'E','L','F' ;11 41 | db 2\ndb 1\ndb 1\ndb 0\ndq 0\ndw 2\ndw 0x3e\ndd 1 42 | dq main + a0\ndq phead\ndq 0\ndd 0\ndw 64\ndw 0x38 43 | dw 1\ndw 0x40\ndw 0\ndw 0\n\nphead: 44 | dd 1\ndd 5\ndq 0\ndq a0\ndq a0\ndq end\ndq end\ndq 0x200000\n\n" 45 | 46 | O compile all 47 | O"end:\n" 48 | 49 | /rax rcx rdx rbx rsp rbp rsi rdi r8..r15 50 | 51 | /main: 52 | / mov rax,97 53 | / call w 54 | / 55 | /q: 56 | / mov rax, 60 57 | / xor rdi, rdi 58 | / syscall 59 | / 60 | /w: 61 | / push rax 62 | / xor rax,rax 63 | / inc rax 64 | / mov edi,eax 65 | / mov rsi,rsp 66 | / mov edx,eax 67 | / syscall 68 | / pop rsi 69 | / ret 70 | 71 | -------------------------------------------------------------------------------- /_/i7/elf.txt: -------------------------------------------------------------------------------- 1 | [bits 64] 2 | a0: equ 4096 * 40 ;load address 3 | db 0x7f,'E','L','F' ;11 4 | db 2 5 | db 1 6 | db 1 7 | db 0 8 | dq 0 9 | dw 2 10 | dw 0x3e 11 | dd 1 12 | dq main + a0 13 | dq phead 14 | dq 0 15 | dd 0 16 | dw 64 17 | dw 0x38 18 | dw 1 19 | dw 0x40 20 | dw 0 21 | dw 0 22 | 23 | phead: 24 | dd 1 25 | dd 5 26 | dq 0 27 | dq a0 28 | dq a0 29 | dq end 30 | dq end 31 | dq 0x200000 32 | 33 | main: 34 | end: 35 | -------------------------------------------------------------------------------- /_/i7/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | 5 | k 7.k c.k -e >k.a 6 | nasm -fbin -oa k.a 7 | chmod +x a 8 | wc -c a 9 | 10 | rm k.a 11 | 12 | #nathanotterness.com/2021/10/tiny_elf_modernized.html 13 | -------------------------------------------------------------------------------- /_/i7/readme: -------------------------------------------------------------------------------- 1 | $ sh mk 2 | + k 7.k c.k -e 3 | + nasm -fbin -oa k.a 4 | + chmod +x a 5 | + wc -c a 6 | 120 a 7 | + rm k.a 8 | $ xxd a 9 | 00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............ 10 | 00000010: 0200 3e00 0100 0000 7880 0200 0000 0000 ..>.....x....... 11 | 00000020: 4000 0000 0000 0000 0000 0000 0000 0000 @............... 12 | 00000030: 0000 0000 4000 3800 0100 4000 0000 0000 ....@.8...@..... 13 | 00000040: 0100 0000 0500 0000 0000 0000 0000 0000 ................ 14 | 00000050: 0080 0200 0000 0000 0080 0200 0000 0000 ................ 15 | 00000060: 7800 0000 0000 0000 7800 0000 0000 0000 x.......x....... 16 | 00000070: 0000 2000 0000 0000 .. ..... 17 | $ █ 18 | 19 | 20 | l6 language 21 | syntax is k, meaning is asm (x86/64) 22 | it defines functions, only ints 64bit. 23 | f:{a:x+y;2*a} 24 | function has args, locals. no globals. 25 | all vars live in registers. 26 | push/pop only at function boundaries. 27 | register allocation is automatic. 28 | body is list of expressions. 29 | an expression is: 30 | assign: a:x+y 31 | memory: b[]:y b[i]:y b[8+8*i]:y base index displacement scale 32 | cond: $[c;a;b] 33 | loop: .. 34 | branch: .. 35 | label: `a 36 | asm: "mov rax,rdi" ? 37 | call: f g[x;y] 38 | -------------------------------------------------------------------------------- /_/i9/k.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define V5 __attribute((vector_size(32),aligned(1))) 6 | typedef uint32_t u; 7 | typedef uint64_t k; 8 | typedef uint32_t U V5; 9 | typedef float E V5; 10 | typedef double F V5; 11 | 12 | /*1<i) 21 | #define tx (x>>56) 22 | #define ix ((u)x) 23 | #define wx (tx&3) 24 | #define rx ((k*)cx)[-2] 25 | #define nx ((k*)cx)[-1] 26 | #define cx ((char*)((x<<8)>>8)) 27 | #define n5(x) (32+(x)>>5) 28 | 29 | k*ma(k n){k*x=(k*)malloc(16+n);R x+((k)x&31?2:0);} 30 | k mk(k t,k n){k x=n5(32+(n<>2); mt_init() } 15 | 16 | func New() interface { 17 | J(x I) I 18 | M() []I 19 | } { 20 | return jJ{} 21 | } 22 | 23 | type jJ struct{} 24 | 25 | func (o jJ) J(x I) I { return j(x) } 26 | func (o jJ) M() []I { return MI } 27 | 28 | func n32(x I) I { 29 | if x == 0 { 30 | return 1 31 | } else { 32 | return 0 33 | } 34 | } 35 | func i32b(x bool) I { 36 | if x { 37 | return 1 38 | } else { 39 | return 0 40 | } 41 | } 42 | func clz32(x I) I { return I(bits.LeadingZeros32(x)) } 43 | func stk(y I) { fmt.Println(x.X(MI, MI[1])) } 44 | func draw(x, y I) { fmt.Println("draw", x, y) } 45 | func xxx(x I) { panic("xxx") } 46 | -------------------------------------------------------------------------------- /_/j/jwa/j.go: -------------------------------------------------------------------------------- 1 | package jwa 2 | 3 | import ( 4 | "bytes" 5 | _ "embed" 6 | "encoding/binary" 7 | "fmt" 8 | "j/x" 9 | "reflect" 10 | 11 | "github.com/go-interpreter/wagon/exec" 12 | "github.com/go-interpreter/wagon/wasm" 13 | ) 14 | 15 | //go:embed j.wasm 16 | var jwasm []byte 17 | 18 | type I = uint32 19 | 20 | func New() interface { 21 | J(x I) I 22 | M() []I 23 | } { 24 | m, e := wasm.ReadModule(bytes.NewReader(jwasm), hostFuncs) 25 | fatal(e) 26 | vm, e := exec.NewVM(m) 27 | fatal(e) 28 | return wk{m, vm} 29 | } 30 | func fatal(e error) { 31 | if e != nil { 32 | panic(e) 33 | } 34 | } 35 | func hostFuncs(name string) (*wasm.Module, error) { // imported as module "ext" 36 | stk := func(proc *exec.Process, y uint32) { 37 | b := make([]byte, proc.MemSize()) 38 | _, e := proc.ReadAt(b, 0) 39 | if e != nil { 40 | panic(e) 41 | } 42 | m := make([]uint32, len(b)/4) 43 | for i := range m { 44 | m[i] = binary.LittleEndian.Uint32(b[4*i:]) 45 | } 46 | fmt.Println(x.X(m, m[1])) 47 | } 48 | draw := func(proc *exec.Process, x, y uint32) { fmt.Println("draw", x, y) } 49 | m := wasm.NewModule() 50 | m.Types = &wasm.SectionTypes{ 51 | Entries: []wasm.FunctionSig{ 52 | {Form: 0, ParamTypes: []wasm.ValueType{wasm.ValueTypeI32}, ReturnTypes: nil}, 53 | {Form: 0, ParamTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32}, ReturnTypes: nil}, 54 | }, 55 | } 56 | m.FunctionIndexSpace = []wasm.Function{ 57 | {Sig: &m.Types.Entries[0], Host: reflect.ValueOf(stk), Body: &wasm.FunctionBody{}}, 58 | {Sig: &m.Types.Entries[1], Host: reflect.ValueOf(draw), Body: &wasm.FunctionBody{}}, 59 | } 60 | m.Export = &wasm.SectionExports{ 61 | Entries: map[string]wasm.ExportEntry{ 62 | "stk": {FieldStr: "stk", Kind: wasm.ExternalFunction, Index: 0}, 63 | "draw": {FieldStr: "draw", Kind: wasm.ExternalFunction, Index: 1}, 64 | }, 65 | } 66 | return m, nil 67 | } 68 | 69 | type wk struct { 70 | m *wasm.Module 71 | vm *exec.VM 72 | } 73 | 74 | func (k wk) J(a uint32) uint32 { 75 | x, ok := k.m.Export.Entries["j"] 76 | if !ok { 77 | panic("no j") 78 | } 79 | res, e := k.vm.ExecCode(int64(x.Index), uint64(a)) 80 | if e != nil { 81 | panic(e) 82 | } 83 | return res.(uint32) 84 | } 85 | func (k wk) M() []uint32 { 86 | b := k.vm.Memory() 87 | r := make([]uint32, len(b)>>2) 88 | binary.Read(bytes.NewReader(b), binary.LittleEndian, &r) 89 | return r 90 | } 91 | -------------------------------------------------------------------------------- /_/j/main/j.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "flag" 6 | "j" 7 | "j/jgo" 8 | "j/jwa" 9 | "os" 10 | _ "embed" 11 | ) 12 | 13 | //go:embed j_.j 14 | var lib []byte 15 | 16 | func main() { 17 | var jj jer = jj{} 18 | var impl string 19 | var sz int 20 | flag.StringVar(&impl, "j", "go", "implementation: go|jgo|jwa") 21 | flag.IntVar(&sz, "sz", 16, "log2 mem size") 22 | flag.Parse() 23 | 24 | switch impl { 25 | case "go": 26 | case "jgo": 27 | jj = jgo.New() 28 | case "jwa": 29 | jj = jwa.New() 30 | default: 31 | panic("-j flag") 32 | } 33 | 34 | jj.J(uint32(sz)) 35 | for _, b := range lib { 36 | jj.J(uint32(b)) 37 | } 38 | jj.J(10) 39 | 40 | p() 41 | s := bufio.NewScanner(os.Stdin) 42 | for s.Scan() { 43 | b := []byte(s.Text()) 44 | for _, u := range b { 45 | jj.J(uint32(u)) 46 | } 47 | jj.J(10) 48 | p() 49 | } 50 | } 51 | 52 | func p() { os.Stdout.WriteString("j) ") } 53 | 54 | type jer interface { 55 | J(x uint32) uint32 56 | M() []uint32 57 | } 58 | 59 | type jj struct{} 60 | 61 | func (o jj) J(x uint32) uint32 { return j.J(x) } 62 | func (o jj) M() []uint32 { return j.M } 63 | -------------------------------------------------------------------------------- /_/j/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | w < j.w > j.wasm 5 | cp j.wasm jwa/j.wasm 6 | 7 | echo "package jgo" > jgo/j_.go 8 | w -go < j.w >> jgo/j_.go 9 | go fmt jgo/*.go 10 | 11 | go test 12 | go run h.go #writes j.html, j.j 13 | cp j.j main/j_.j 14 | -------------------------------------------------------------------------------- /_/j/readme: -------------------------------------------------------------------------------- 1 | j stack language parse list operators 2 | (comment) # length/non-list: -1 3 | 32-bit type system abc symbol (max 6) , cat 4 | ..000 list[quotation] 123 int (max 31 bit) []i@ index 5 | ....1 int x>>1 [..] list/quote [a]i v$store [a]"#v$ (append) 6 | ...10 symbol x>>2 7 | ..100 operator x>>3 numeric operators (dyadic) assign/quote/exec 8 | heap memory +-*/% arith [p][a]: assign [p] to symbol a 9 | 0 total memory(log) <=> compare a lookup symbol a and exec 10 | 1 stack [p]. exec 11 | 2 stack(swap) 12 | 3 key/val list stack operators control 13 | 4..32 free list ~"_| swap dup pop roll c[t][e]? (if c then t else e) 14 | 33... data(buckets) ! print stack } pop/push to swap stack 15 | unused '\^`; canvas draw [arg][func]& { pop/push from swap stack 16 | -------------------------------------------------------------------------------- /_/j/serve.go: -------------------------------------------------------------------------------- 1 | //+build ignore 2 | 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | "net/http" 8 | ) 9 | 10 | func main() { 11 | http.Handle("/", http.FileServer(http.Dir("."))) 12 | fmt.Println(http.ListenAndServe(":3000", nil)) 13 | } 14 | -------------------------------------------------------------------------------- /_/k/a.k: -------------------------------------------------------------------------------- 1 | kst:{$[#x;kstn;kst0]x} /roundtrip, single line, cannot handle null 2 | kst0:{(;_34 34;"!0";"0#0.";"0#0a";"0#`";"()";"0#`!0")@@x} 3 | kstn:{v:{" "/:$x};(($;kstc;v;v;v;ksts;kstl;kstd)@@x)x} 4 | kstd:{kst[!x],"!",kst[.x]} 5 | kstc:{c:_34;c,($x),c} 6 | ksts:{"`","`"/:$x} 7 | kstl:{$[1~#x;",",kst@*x;"(",(";"/:kst'x),")"]} 8 | print:kst 9 | 10 | hex: {,/{"0123456789abcdef"@,/(x%16;16/x)}'0+x} /hex"alpha" 11 | unhex:{,/{+/_16 1*"0123456789abcdef"?x}'(,/(2\#x;2))#x} 12 | bits: {2/128 64 32 16 8 4 2 1\x} 13 | 14 | rle: {(x i;,/1_-':(i:&1,1_~,/=':x),#x)} /rle"aabbb" /("ab";2 3) 15 | 16 | tab: {t:@x; \(_10)/:$[t~6;tochar'x;t~7;tabdict x; 'kx];} /matrix output 17 | rpad: {y#x,(y-#x)#" "} /rpad["alpha";10] 18 | tabdict:{k:$!x;pad:rpad[;|/#'k]; ((pad'k),'"|"),' 'k'.x} 19 | tochar: {$[1~@x;x; 'kx]} 20 | 21 | zip:{[f;l]f./:&l} /e.g. zip[f3;(x;y;z)] x y z:lists, does not extend 22 | compose:{[f;x]x{y x}/f} /e.g. f:({-x};{_x};..) 23 | 24 | odo:{(*/x)#/:&'x#'|*\-1_1,|x} 25 | 26 | std:`kst`hex`zip!(kst;hex;zip) 27 | 28 | -------------------------------------------------------------------------------- /_/k/b.j: -------------------------------------------------------------------------------- 1 | \listbox.k 2 | \m.k 3 | tmsolve:{[A;BT](qr A)qrsolve/:BT} 4 | 5 | ucal:{[s;t;r]n:#r;i:(u:?r)?r;X:tmsolve[t,'0a+i=(n#,!#u);&s];K:(#t 0)#'X} 6 | ucalTest:{[] 7 | s0:1a0 2a30 8 | s:(s0;s0+2a0 0a0;s0+0a0 2a0) 9 | t:(0a0 0a0;1a0 0a0;0a0 1a0) 10 | r:`a`a`a 11 | e:|//:+(2a0 0;0 2a0)-ucal[s;t;r] 12 | `ucal,$[1.e-15>e;`ok;`fail]} 13 | 14 | 15 | pcal:{[s;u]i:!2\#s;&tmsolve[(s i)-s i+1;&u i]} 16 | pcnl:{[s;u;f;n]i:!2\#s;pcal[s g:(n&#i)#>f s i;u g]} 17 | 18 | uidx:{[u;a]solve[1@0.+a;u]} 19 | uidxTest:{[] 20 | u0:2a30;ur:3a40 21 | u:u0+ur@0 90 180. 22 | a:(0 0;0 90;0 180) 23 | e:|/+uidx[u;a]-,/(u0;ur) 24 | `uidx,$[1.e-15>e;`ok;`fail]} 25 | 26 | Tags:`List`idx 27 | List:`u`a!(4.8a48 4.9a47 1.6a277 1.7a279;(0 0;0 0;0 180;0 180)) 28 | idx:{l:walk path;uidx[l`u;l`a]} 29 | 30 | 31 | Tags:`List`unb 32 | List:`s`t`r`c!((3a20 4a50;12.8a5 4.1a51;3.1a19 12.9a14;3a300 2a10);(0a0 0a0;10a0 0a0;0a0 10a0;0a0 0a0);`a`a`a`a;1 1 1 0) 33 | unb:{w:&List`c;s:List[`s;w];t:List[`t;w];r:List[`r;w];K:qr[ucal[s;t;r]];l:walk path;K qrsolve/l`s} 34 | 35 | tags: Tags 36 | -------------------------------------------------------------------------------- /_/k/braille.k: -------------------------------------------------------------------------------- 1 | /draw bitmap image as braille 2 | 3 | braille:`c@(-3$256\:)'14852224+(!256)+0 192 384 576@64/!256 /utf-8 block u+2800..u+28ff 4 | 5 | draw:{h:#x;x:,/x;w:h/#x 6 | x@:,/((4*w)*!4/h) +\ ,/(2*!2/w)+\,/0 1+/w*!4; 7 | x@:,/7 6 5 3 1 4 2 0+/8*!8/#x 8 | ""0:,/'(2/w)^braille@2/:'8^x} 9 | 10 | draw (011111000000000000000000b 11 | 011111000000000000000000b 12 | 011111000000000000000000b 13 | 011111000000111000000000b 14 | 011111000011111000000000b 15 | 011111001111000000000000b 16 | 011111111100000000000000b 17 | 011111110000000000000000b 18 | 011111111000000000000000b 19 | 011111001110000000000000b 20 | 011111000111100000000000b 21 | 011111000011110000000000b 22 | 011111000001111100000000b 23 | 011111000000111110000000b 24 | 011111000000011111000000b 25 | 011111000000011111000000b) 26 | 27 | 28 | -------------------------------------------------------------------------------- /_/k/example.j: -------------------------------------------------------------------------------- 1 | \listbox.k 2 | 3 | Tags:`List`Dict`Ints`Floats`Symbols`Tree`Table`draw`edit 4 | tags:Tags 5 | 6 | List:("alpha";"beta";"gamma") 7 | Dict:`alpha`b`c!(1 2;3 4 5;`symbol) 8 | Ints:10-!8 9 | Floats:129 'F!10 10 | Symbols:`abc`d`efghi 11 | Tree:(`alpha`beta`gamma!(1 2 3;`a`b`c!1 2 3;("first line";"second line"))) 12 | Table:`abc`def`g`h`s!(`x`y`zz;9+!3;"ABC";(`a`b!1 2;`c`d!5 6;`a`d!7 9);("abc";"def";"ghijk")) 13 | T:`a`b!(1 2 3;1.1 2.2 3.3) 14 | 15 | draw:{30 'd 'r1500;("one";"two";"three")} 16 | 17 | 18 | "this is a listbox-ui example application 19 | 20 | double-click on words in the tag bar; then select, navigate or edit. 21 | ESC is Back 22 | 23 | application source: ktye.github.io/example.k 24 | listbox source : ktye.github.io/listbox.html 25 | k.wasm binary : ktye.github.io/k.wasm 26 | k source : https://raw.githubusercontent.com/ktye/i/master/k.w" 27 | -------------------------------------------------------------------------------- /_/k/example.k: -------------------------------------------------------------------------------- 1 | /listbox.k 2 | path:() 3 | exec:{$[x~`edit;edit[];0~#x;1_,tags::Tags;0~@f:.*x;disp f walk path;disp walk $[5~@x;path::,x;x]]} 4 | walk:{$[0~#x;();atx[v;1_x;ist v:.*x]]} 5 | push:{exec path::path,,$[(1~#path)&ist u:walk path;-1+x^0;x]} 6 | back:{disp walk path::-1_path} 7 | edit:{tako 1;$[(1<#*|path)&(2~#path)&ist t:walk path;dis7 t; 'k t]} 8 | teds:{(!x)!*'.x} 9 | ok: {s:*path;asn[s;$[ist n:.s;tas[n;1_path;x];$[#p:1_path;@[n;$[1~#p;*p;p];x];x]]];} 10 | 11 | tako:{tags::(tags^`ok),$[x;`ok;0#`]} 12 | disp:{t:@x;tako t<6;$[ist x;dis8 x;6~t; 'k'x;7~t;dis7 x; 'kx]} 13 | dis7:{k:$!x;pad:rpad[;|/#'k]; ((pad'k),'"|"),' 'k'.x} /display dict 14 | dis8:{" "/:'&rpad''[l;|/'#''l:tst''(!x),'.x]} /display table 15 | rpad:{y#x,(y-#x)#" "} 16 | kst: {$[1~@x; x; 'kx]} 17 | tst: {$[5~@x;$x; 'kx]} 18 | ist: {$[~7~@x;0;~#x;0;&/(n:#*.x)~'#'.x;n>1;0]} /dict is table 19 | atx: {$[#y;atx[$[z;tix[x;*y];x@*y];1_y;0];x]} 20 | tix: {@[;y]'x} /index table x by row y 21 | 22 | data:`!() 23 | drop:{data,:x!,y;,/($x;": ";$#y;" bytes")} 24 | asn: {.(:;x;$[6~@y;,y;y])} 25 | tas: {$[0~#y;z;1~#y;@[x;(,!x),y;$[7~@z;.z;z]];@[x;@[y;0 1;y 1 0];z]]} 26 | tag: {" "/:$?tags} 27 | 28 | Tags:`List`Dict`Ints`Floats`Symbols`Tree`Table`draw`edit 29 | tags:Tags 30 | 31 | List:("alpha";"beta";"gamma") 32 | Dict:`alpha`b`c!(1 2;3 4 5;`symbol) 33 | Ints:10-!8 34 | Floats:129 'F!10 35 | Symbols:`abc`d`efghi 36 | Tree:(`alpha`beta`gamma!(1 2 3;`a`b`c!1 2 3;("first line";"second line"))) 37 | Table:`abc`def`g`h`s!(`x`y`zz;9+!3;"ABC";(`a`b!1 2;`c`d!5 6;`a`d!7 9);("abc";"def";"ghijk")) 38 | T:`a`b!(1 2 3;1.1 2.2 3.3) 39 | 40 | draw:{30 'd 'r1500;("one";"two";"three")} 41 | 42 | 43 | "this is a listbox-ui example application 44 | 45 | double-click on words in the tag bar; then select, navigate or edit. 46 | ESC is Back 47 | 48 | application source: ktye.github.io/example.k 49 | listbox source : ktye.github.io/listbox.html 50 | k.wasm binary : ktye.github.io/k.wasm 51 | k source : https://raw.githubusercontent.com/ktye/i/master/k.w" 52 | -------------------------------------------------------------------------------- /_/k/fire.k: -------------------------------------------------------------------------------- 1 | c:(3*!85),(255+768*!85),(65535+196608*!86) 2 | i:!m:(w:256)*h:256;o:(m#0),w#255 3 | {r:-3/ 'r#i;o[i]::0|r+o[w+(m-1)&i+r];w 'dc[o];1+x}/:0 4 | -------------------------------------------------------------------------------- /_/k/fmt.k: -------------------------------------------------------------------------------- 1 | fmt:{kst@&x} 2 | kst:{$[6~@x;lst x;(1~#x)&5~@x;$x; 'kx]} 3 | lst:{f:*x;$[0~#x;"()";1~#x; 'k*x;2~#x;mon.x;(128~_f)&~@f;seq 1_x;3~#x;dya.x;brk[*x;1_x]]} 4 | mon:{$[(1~#x)&0~@x;$[iad@*x;(kst y),$*x;,/kst'(x;y)];spc[kst x;kst y]]} 5 | dya:{$[(1~#x)&0~@x;(emb y),($x),kst z;brk[x;(y;z)]]} 6 | brk:{,/(kst x;"[";";"/:kst'y;"]")} 7 | emb:{$[6>@x;kst x;"(",(kst x),")"]} 8 | seq:{";"/:kst'x} 9 | iad:{6>adv?`$$x} 10 | adv:,/`$'"_"\:"'_/_\_':_/:_\:" 11 | chr:(_"0"+!10),(_"A"+!26),_"a"+!26 12 | spc:{x,$[(~iad@*|x)&62>chr?*y;" ",y;y]} 13 | 14 | test:{x~fmt x} 15 | t:test'("*x" 16 | "1 2 3" 17 | "{x+y}" 18 | "*x" 19 | "+/1 2 3" 20 | "f x" 21 | "f`a`b`c" 22 | "3+4" 23 | "{x+y}[1;2]" 24 | "1;2;3") 25 | -------------------------------------------------------------------------------- /_/k/lines.k: -------------------------------------------------------------------------------- 1 | /line drawing (k9) 2 | /single data point per column 3 | 4 | / 64*64 to 32*64 rows/cols with half blocks U+2580 U+2584 5 | line32:{o:(32*64)#0 6 | o[x+64*2/y]:1+2\y 7 | s:(" ";0xe29680;0xe29684) 8 | ""0:,/(,[;"\n"])',/'s@64^o} 9 | 10 | 11 | N:64; line32[!N;N/(!N)*!N] 12 | 13 | 14 | / 72*72 to 12*72 rows/cols with 6 legacy hlines U+1fb76..U+1fb7b 15 | line12:{o:(12*72)#0 16 | o[x+72*6/y]:1+6\y 17 | s:(" ";0xf09fadb6;0xf09fadb7;0xf09fadb8;0xf09fadb9;0xf09fadba;0xf09fadbb) 18 | ""0:,/(,[;"\n"])',/'s@72^o} 19 | 20 | 21 | N:72; line12[!N;N/(!N)*!N] 22 | 23 | 24 | / 64*64 to 16*64 rows/cols with scan lines U+23bb..U+23bd 25 | line16:{o:(16*64)#0 26 | o[x+64*4/y]:1+4\y 27 | s:" ",3^0xe28ebae28ebbe28ebce28ebd 28 | ""0:,/(,[;"\n"])',/'s@64^o} 29 | 30 | 31 | N:64; line16[!N;N/(!N)*!N] 32 | 33 | 34 | / 64*64 to 8*64 rows/cols with blocks U+2581..U+U2588 + vfill 35 | line8:{o:( 8*64)#0 36 | o[x+64*yy:8/y]:1+8\y 37 | s:" ",3^0xe29688e29687e29686e29685e29684e29683e29682e29681 38 | o[,/x+64*(1+yy)_\!8]:1 39 | ""0:,/(,[;"\n"])',/'s@64^o} 40 | 41 | w:64; line8[!w;64/(!w)*!w] 42 | 43 | \\ 44 | -------------------------------------------------------------------------------- /_/k/m.k: -------------------------------------------------------------------------------- 1 | sin:129 'F;cos:130 'F;exp:131 'F;log:132 'F;log10:{0.4342944819032518* 'Fx};pow10:{131 'F2.302585092994046*x} 2 | 3 | 4 | solve: {[A;b];qrsolve[qr A;b]} /solve A*x=b overdetermined, real or complex 5 | qrsolve:{[q;b];qrslv[q;qrmul[q;b]]} /e.g. reuse q:qr A 6 | 7 | qr: {m:#x;x:&x;n:#x;t:@Q:,/x;D:t$!0;row:!m;sub:(,/(n;m))#!n*m; f:$[3~t;qrh;qzh] /qr decomposition 8 | Q:(n;f)/:Q;`Q`D`m`n!(Q;|D;m;n)} 9 | qrh:{ii:i+m*i:#D;sub::1_sub /householder step(real) 10 | s:norm x row;D::D,:d:s*1 -1@(x ii)>0;s:1%%s*s++x ii;x[ii]-:d;x[row]*:s 11 | $[#sub;x[,/sub]-:,/(x row)*/:+/'((x row)*/:x sub);] 12 | row::m+1_row;sub::1_'sub;x} 13 | qzh:{ii:i+m*i:#D;sub::1_sub /householder step(complex) 14 | s:norm x row;d:-s@&x ii;D::D,:d:-s@&x ii;s:1%%s*s++x ii;x[ii]-:d;x[row]*:s 15 | $[#sub;x[,/sub]-:,/(x row)*/:+/'((%x row)*/:x sub);] 16 | row::m+1_row;sub::1_'sub;x} 17 | 18 | qrmul:{Q:x`Q;m:x`m;n:x`n;f:$[3~t:@x`Q;qrml;qzml];b:t$y;row:yi:!m;n#(n;f)/:b} /calculate QT*b 19 | qrml:{x[yi]-:Q[row]*+/Q[row]*x[yi];row::m+1_row;yi::1_yi;x} 20 | qzml:{x[yi]-:Q[row]*+/(%Q[row])*x[yi];row::m+1_row;yi::1_yi;x} 21 | 22 | qrslv:{n:x`n;m:x`m;Q:x`Q;D:x`D;i:n-1;bi:!0;(n;qrsl)/:y} /solve R*x=QT*b with back substitution 23 | qrsl:{col:i+m*bi;$[#bi;x[i]-:+/Q[col]*x[bi];];x[i]%:*D;bi::i,bi;D::1_D;i::i-1;x} 24 | 25 | norm:{s*%+/x*x%:s:|/x:+0.+x} /vector norm(l2) 26 | 27 | matvec:{[A;b],/+/'A*\:b} 28 | 29 | testM:{[]small:1.0e-14 30 | A: 0.+(1 -2 3;5 3 2;2 3 1;4 -1 1) 31 | r: 0.+1 2 3 32 | b: matvec[A;r] 33 | x: solve[A;b] 34 | $[small>e:|/+x-r; \(`ok;e); \(`fail;e)] 35 | A: 0a0+(1 -2a90 3;5a90 3 2;2 3 1;4 -1 1) 36 | r: 1 2 3a30 37 | b: matvec[A;r] 38 | x: solve[A;b] 39 | $[small>e:|/+x-r; \(`ok;e); \(`fail;e)] 40 | } 41 | 42 | /testM[] 43 | 44 | \ 45 | /matlab version(does not transpose A) 46 | function [A,d]=qrh(A); 47 | [m,n]=size(A); 48 | for i=1:n, 49 | s=norm(A(i:m,i)); 50 | if s==0, error(’rank(A)=0, d(i)=-s; else d(i)=s; end 52 | f=sqrt(s*(s+abs(A(i,i)))); 53 | A(i,i)=A(i,i)-d(i); 54 | A(i:m,i)=A(i:m,i)/f; 55 | if i0){if(l=="\\")x=0;if(x)print(l)};next}{print}' 5 | } 6 | for j in *.j; do 7 | k=`echo $j|sed 's/.$/k/'` 8 | kj>$k<$j 9 | done 10 | -------------------------------------------------------------------------------- /_/k/rand.k: -------------------------------------------------------------------------------- 1 | randi:{y/ 'rx} /x ints [0 y) 2 | randf:{( 'rx)%4.294967295e9} /x floats [-0.5 0.5) 3 | randn:{_randz x} /x floats normal distributed 4 | randz:{(%-2.* 'l0.5+randf x)@360.*randf x} /x binormal distributed complex 5 | shuffle:{x@< 'r#x} /x vector 6 | -------------------------------------------------------------------------------- /_/k/raster.j: -------------------------------------------------------------------------------- 1 | raster:{[wh;a]w:wh 0;h:wh 1;o:(w*h)#0;r:fg:0;r:0;pa:();(.'!a)@'.a;o} 2 | 3 | strokeStyle:{fg::icolor x;} 4 | beginPath: {[x]pa::();} 5 | moveTo: {pa::,_0.+x;} 6 | lineTo: {;pa::pa,,_0.+x;} 7 | stroke: {[x]f:iline[;;w];o[$[0~r;,/1_f':pa;&(r)>+iarc[r;pa;w;h]]]::fg;r::0;} 8 | fill: {[x]o[&0>iarc[r;pa;w;h]]::fg;r::0;} 9 | rect: {dx:,/(x 2;0);dy:,/(0;x 3);a:x 0 1;pa::(a;a+dx;a+dx+dy;a+dy;a);} 10 | arc: {pa::_x 0 1;r::_x 2;} 11 | fillText: {o[istr[x 1;x 2;w;x 0]]::fg;} 12 | fillStyle:strokeStyle 13 | clip:lineWidth:textBaseline:font:{x;} 14 | 15 | 16 | ihex: {[c]+/i*16\*\(n:#i:|"0123456789abcdef"?-c 4 5 2 3 0 1)#16} 17 | irgb: {[rgb]+/1 256 65536*rgb} 18 | icolor:{c:`white`black`gray`red`green`blue!irgb'(255;0;128;255 0 0;0 255 0;0 0 255);$[(#c)>i:(!c)?k:`$x;c k;ihex 1_x]} 19 | iline: {[a;b;w]$[(+*d)<+dy:(d:b-a)1;yline.$[0>dy;(b;a-b;w);(a;b-a;w)];xline.$[0>*d;(b;a-b;w);(a;b-a;w)]]} 20 | xline: {[a;d;w]dx:*d;m:(d 1)%0.+dx;(x+a 0)+w*y:_(a 1)+m*x:!dx} 21 | yline: {[a;d;w]dy:d 1;m:(*d)%0.+dy;(w*y+a 1)+x:_(a 0)+m*y:!dy} 22 | iarc: {[r;xy;w;h]`r`xy`w`h!(r;xy;w;h);x:(w/i:!w*h)-xy 0;y:(w\i)-xy 1;((x*x)+y*y)-r*r} 23 | istr: {[x;y;w;s],/(10*!#s)+(x+w*y-20)+(10/i)+w*10\i:glyphs@0+s} /font.k (10x20) 24 | \font.k 25 | 26 | rasterT:,/(`beginPath`strokeStyle`lineWidth`rect`stroke!(();"red";2;(10 10 80 50);()) 27 | `fillText!,("text";10;30;40) 28 | `beginPath`strokeStyle`arc`stroke!(();"red";80 50 10 0 2p;()) 29 | `beginPath`strokeStyle`arc`fill!(();"red";20 50 10 0 2p;())) 30 | rasterTest:{[](*wh) 'draster[wh:200 100;rasterT]} 31 | 32 | rasterT:`beginPath`lineWidth`strokeStyle`lineTo`lineTo`lineTo`lineTo`lineTo`lineTo`lineTo`stroke!(();2;"#8dd3c7";5.0 300.5379;25.52631 217.6333;46.05263 142.1343;66.57894 80.78503;87.10526 39.06561;107.6315 20.70275;128.1578 27.33673;()) 33 | /800 'draster[800 600;rasterT] 34 | -------------------------------------------------------------------------------- /_/k/sixel.k: -------------------------------------------------------------------------------- 1 | /sixel output (k9) 2 | /image data is rgb in int32 3 | 4 | six:{cmap:{{"#",($*x),";2;",";"/:$|1_x}'+(,!#x),+(255/100*-3$256\:)'x} 5 | u:?,/x;d:"-"/:,/'{"#",($x),"!6~"}''u?x 6 | (0x1b,"Pq",,/cmap u),d,0x1b,"\\"} 7 | 8 | / e.g. 9 | animal:+ 459001 8973467 11660987 989550 1382680 4875085 9738128 15922674@32^,/(-8$8\:)'0 82 4793490 4793417 0 117074 4793490 4756041 0 8126442 4793490 2396745 0 14680050 4793489 2396745 4 16744445 4793481 2396745 6 15879677 4793489 3595117 31 15879605 11084945 2551230 6 16449535 16065681 2555814 30 16515071 16426130 2547191 253 16252351 16688274 2416564 383 12056871 16688274 2416126 446 14121255 16688274 2416638 484 10419511 16688274 4496319 420 9895927 16327826 4496382 316 14670268 16065682 4494198 54 16769023 8987794 4756351 29 16412671 11084946 4756342 6 15888383 13182098 4756055 6 16743223 13182098 4756078 6 16771375 13182098 4788847 6 15880127 8987794 4788813 5 13786036 4793490 4788813 3 13791229 4793490 4788813 0 10485729 2696338 4788813 0 977641 2696338 4792905 0 25 2696338 11084361 0 5 2434194 11084361 0 3 2434194 11084361 0 0 10790034 13182025 0 0 7640210 4793417 0 0 1348754 13182025 0 0 184978 4793417 10 | 11 | ""0:six animal 12 | -------------------------------------------------------------------------------- /_/k/ui.j: -------------------------------------------------------------------------------- 1 | ui:{$[1~@x;key x;#o;mouse x;*(o::(*/x)#0;C::20\w::*x;R::20\h::x 1)]};o:!w:h:R:C:0 2 | key:{term x} // $[x~_10;(Y::Y+20;X::-10);X::X+10];o[istr[X;Y;w;x]]::white;o};X:-10;Y:0 3 | mouse:{o[iarc[x;10]]::red;o} 4 | 5 | 6 | X:0; 7 | term:{X+:10;o::@[@[o;block X;0];block X+10;white];o::$[10=x;scroll o;@[o;istr[X;(h-20);w;x];white]]} 8 | scroll:{x:@[x;block X+10;0];X::0; @[(n_x),(n:20*w)#0;block[10];white]} 9 | 10 | block:{[x]; (x+(#o)-20*w)+(10/i)+w*10\i:!10*20} /((10*x)+(#o)-20*h) 11 | iarc:{[xy;r]x:(w/i:!#o)+r-xy 0;y:(w\i)+r-xy 1;&r>((x*x)+y*y)-r*r} 12 | irgb:{[rgb]+/1 256 65536*rgb};white:irgb 255 255 255;red:irgb 255 0 0 13 | istr:{[x;y;w;c]i:glyphs 0+c; (10*!#s)+(x+w*y)+(10/i)+w*10\i} 14 | \font.k 15 | -------------------------------------------------------------------------------- /_/ki/mk: -------------------------------------------------------------------------------- 1 | set -x 2 | set -e 3 | 4 | go install 5 | 6 | if [ "$1" = "cover" ]; then 7 | go test -coverprofile=cov.out 8 | go tool cover -html=cov.out -o cov.html 9 | fi 10 | 11 | 12 | if [ "$1" = "zk" ]; then 13 | # embed z.k 14 | sed -e '/^\//d' -e 's, */.*,,' -e 's/^ *//' -e '/^$/d' z.k > k.k 15 | zn=`wc -c k.k | sed 's/ .*//'` 16 | zk=`sed -e 's/\\\/\\\\\\\/g' -e 's/"/\\\"/g' -e 's/$/\\\/g' k.k | tr '\n' 'n'` 17 | rm k.k 18 | cat << EOF 19 | Data(280, "$zk") 20 | zn := int32($zn) 21 | EOF 22 | fi 23 | 24 | 25 | wg ki.go | wat2wasm - -o k.wasm 26 | wasm-opt --enable-bulk-memory -Oz k.wasm -o z.wasm 27 | zstd -19 -f z.wasm 28 | wc -c k.wasm z.wasm z.wasm.zst 29 | 30 | rm k.wasm z.wasm z.wasm.zst 31 | 32 | -------------------------------------------------------------------------------- /_/ki/readme: -------------------------------------------------------------------------------- 1 | ktye/k integer version 2 | 3 | no float/complex 4 | ` 5 | 6 | 7 | ktye/k all nofloat 8 | k.wasm 34434 25858 my compiler 9 | z.wasm 32270 24287 with wasm-opt -Oz 10 | z.zstd 15034 11405 compressed 11 | -------------------------------------------------------------------------------- /_/ki/z.k: -------------------------------------------------------------------------------- 1 | / 64 72 80 88 96 104 112 120 2 | `k`l`a`b`while`"rf."`"rz."`"uqs."`"uqf."`"gdt."`"lin."`"grp."`"enc." 3 | 4 | `x:,/+"0123456789abcdef"@16 16\256! /`x@ hex 5 | `t:`39 /`t@ token 6 | `p:`46 /`p@ parse 7 | 8 | `enc:{$[#y;+(&'(|/c)-c:#'r),'r:{x\y}/[x;y];(#x)#,!0]} /x\Y 9 | `uqs:{x@&1,1_~x~'x@-1+!#x:^x} /?^x 10 | `uqf:{x@&(!#x)=x?x} /?xL 11 | `gdt:{[t;g]($[g;{x@>y x};{x@t grade table 12 | 13 | `grp:{(x@*'g)!g:(&~a~'a@-1+!#a:x i)^i:0 10 | i s>o 11 | 12 | mem: 13 | i -kwac > mem 14 | 15 | # https://github.com/WebAssembly/wabt/releases 16 | WABT=~/wabt 17 | valid: o 18 | $(WABT)/wasm-validate o 19 | 20 | dump: o 21 | $(WABT)/wasm-objdump -sd o 22 | 23 | wat: o 24 | $(WABT)/wasm2wat o 25 | 26 | -------------------------------------------------------------------------------- /_/kwac/README: -------------------------------------------------------------------------------- 1 | kwac - k webassembly compiler 2 | 3 | s is a k program (k7;i) that emits webassembly byte code. 4 | the resulting program is (not yet) a k interpreter itself (a subset of k7). 5 | 6 | i(../k.go) is a reference implementation that uses the same memory layout as the wasm version (only on little endian). 7 | 8 | Wasm reference 9 | webassembly.github.io/wabt/demo/wat2wasm (interactive) 10 | sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md 11 | -------------------------------------------------------------------------------- /_/kwac/mkhtml: -------------------------------------------------------------------------------- 1 | sed 1q h > index.html 2 | base64 -w 0 o | sed -e 's/^/var r = "/' -e 's/$/"\n/' >> index.html 3 | sed -n '3,$p' h >> index.html 4 | -------------------------------------------------------------------------------- /_/listbox/embed.go: -------------------------------------------------------------------------------- 1 | // +build ignore 2 | 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | "io/ioutil" 8 | "os" 9 | ) 10 | 11 | func main() { 12 | if len(os.Args) != 2 { 13 | panic("args") 14 | } 15 | name := os.Args[1] 16 | b, e := ioutil.ReadAll(os.Stdin) 17 | if e != nil { 18 | panic(e) 19 | } 20 | fmt.Printf("const %s = %q\n", name, string(b)) 21 | } 22 | -------------------------------------------------------------------------------- /_/listbox/go.mod: -------------------------------------------------------------------------------- 1 | module listbox 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/ktye/plot v0.0.0 7 | github.com/ktye/plot/plotui v0.0.0 8 | github.com/ktye/pptx v0.0.0 9 | github.com/ktye/pptx/pptxt v0.0.0 10 | github.com/lxn/walk v0.0.0-20201209144500-98655d01b2f1 11 | github.com/lxn/win v0.0.0-20201111105847-2a20daff6a55 12 | golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d // indirect 13 | golang.org/x/tools v0.0.0-20201206230334-368bee879bfd // indirect 14 | gopkg.in/Knetic/govaluate.v3 v3.0.0 // indirect 15 | ) 16 | 17 | replace ( 18 | github.com/ktye/plot => c:/k/plot 19 | github.com/ktye/plot/plotui => c:/k/plot/plotui 20 | github.com/ktye/pptx => c:/k/pptx 21 | github.com/ktye/pptx/pptxt => c:/k/pptx/pptxt 22 | ) 23 | -------------------------------------------------------------------------------- /_/listbox/listbox.k: -------------------------------------------------------------------------------- 1 | path:() 2 | exec:{$[x~`edit;edit[];0~#x;1_,tags::Tags;0~@f:.*x;disp f walk path;disp walk $[5~@x;path::,x;x]]} 3 | walk:{$[0~#x;();atx[v;1_x;ist v:.*x]]} 4 | push:{exec path::path,,$[(1~#path)&ist u:walk path;-1+x^0;x]} 5 | back:{disp walk path::-1_path} 6 | edit:{tako 1;$[(1<#*|path)&(2~#path)&ist t:walk path;dis7 t; 'k t]} 7 | teds:{(!x)!*'.x} 8 | ok: {s:*path;asn[s;$[ist n:.s;tas[n;1_path;x];$[#p:1_path;@[n;$[1~#p;*p;p];x];x]]];} 9 | 10 | tako:{tags::(tags^`ok),$[x;`ok;0#`]} 11 | disp:{t:@x;tako t<6;$[ist x;dis8 x;6~t; 'k'x;7~t;dis7 x; 'kx]} 12 | dis7:{k:$!x;pad:rpad[;|/#'k]; ((pad'k),'"|"),' 'k'.x} /display dict 13 | dis8:{" "/:'&rpad''[l;|/'#''l:tst''(!x),'.x]} /display table 14 | rpad:{y#x,(y-#x)#" "} 15 | kst: {$[1~@x; x; 'kx]} 16 | tst: {$[5~@x;$x; 'kx]} 17 | ist: {$[~7~@x;0;~#x;0;&/(n:#*.x)~'#'.x;n>1;0]} /dict is table 18 | atx: {$[#y;atx[$[z;tix[x;*y];x@*y];1_y;0];x]} 19 | tix: {@[;y]'x} /index table x by row y 20 | 21 | csv:"fa*" /"1;ifaz+cs" 22 | list:data:() 23 | drop:{list,:`file!x;data,:parse[x;y];} 24 | asn: {.(:;x;$[6~@y;,y;y])} 25 | tas: {$[0~#y;z;1~#y;@[x;(,!x),y;$[7~@z;.z;z]];@[x;@[y;0 1;y 1 0];z]]} 26 | tag: {" "/:$?tags} 27 | plot:{ 'px} 28 | 29 | tags:Tags:`Src`List`Files`Plot`dict`table 30 | dict:`alpha`beta`gamma!(1 2;3 4 5;"characters") 31 | table:`a`b`c`d!(!10;1.2+!10;,/`$'d;d:_"a"+!10) 32 | tabel:`a`b`c!(!1000;0.5+!1000;0.5-!1000) 33 | p:`a!,1a20 1.2a18 0.9a22 34 | Plot:{plot p} 35 | -------------------------------------------------------------------------------- /_/listbox/listbox.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | PerMonitorV2, PerMonitor 12 | True 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /_/listbox/mk: -------------------------------------------------------------------------------- 1 | #rsrc -manifest listbox.manifest -o listbox.syso 2 | 3 | k0=../../t/k.go 4 | kk=../../k.k 5 | go run $k0 $kk -e go k | go run unmain.go printc > k.go 6 | go run embed.go listbox < listbox.k >> k.go 7 | 8 | go build 9 | -------------------------------------------------------------------------------- /_/listbox/plot.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/ktye/plot" 7 | ) 8 | 9 | // ktye/plot interface for k. 10 | 11 | func plot1(x uint32) uint32 { 12 | plts, e := plot.KTablePlot(x, C, I, F) 13 | if e != nil { 14 | fmt.Println(e) 15 | panic(e) 16 | } 17 | O.Plots = plts 18 | return mki(1) 19 | } 20 | -------------------------------------------------------------------------------- /_/listbox/unmain.go: -------------------------------------------------------------------------------- 1 | // +build ignore 2 | 3 | // Unmain reads a go file from stdin and removes the main function. 4 | package main 5 | 6 | import ( 7 | "bytes" 8 | "go/ast" 9 | "go/format" 10 | "go/parser" 11 | "go/token" 12 | "os" 13 | 14 | "golang.org/x/tools/imports" 15 | ) 16 | 17 | func main() { 18 | del := make(map[string]bool) 19 | for _, f := range append([]string{"main"}, os.Args[1:]...) { 20 | del[f] = true 21 | } 22 | 23 | fset := token.NewFileSet() 24 | f, e := parser.ParseFile(fset, "", os.Stdin, 0) 25 | fatal(e) 26 | 27 | // remove main function 28 | j := 0 29 | for _, a := range f.Decls { 30 | if d, o := a.(*ast.FuncDecl); o && del[d.Name.Name] { 31 | } else { 32 | f.Decls[j] = a 33 | j++ 34 | } 35 | } 36 | f.Decls = f.Decls[:j] 37 | 38 | // fix imports 39 | var buf bytes.Buffer 40 | fatal(format.Node(&buf, fset, f)) 41 | out, e := imports.Process("k.go", buf.Bytes(), nil) 42 | fatal(e) 43 | os.Stdout.Write(out) 44 | } 45 | func fatal(e error) { 46 | if e != nil { 47 | panic(e) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /_/mach/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "bytes" 6 | "encoding/binary" 7 | "flag" 8 | "io/ioutil" 9 | "os" 10 | "strconv" 11 | ) 12 | 13 | func main() { 14 | var m string 15 | var start, size int 16 | var t, d bool 17 | flag.StringVar(&m, "m", "r5", "machine") 18 | flag.IntVar(&start, "start", 0, "start address") 19 | flag.IntVar(&size, "size", 32*1024, "memory(bytes)") 20 | flag.BoolVar(&t, "t", false, "text input (decimal uint32)") 21 | flag.BoolVar(&d, "d", false, "disassemble") 22 | flag.Parse() 23 | 24 | var b = make([]byte, size) 25 | var prog []byte 26 | var e error 27 | if flag.NArg() == 0 { 28 | prog, e = ioutil.ReadAll(os.Stdin) 29 | } else { 30 | prog, e = ioutil.ReadFile(flag.Arg(0)) 31 | } 32 | if e == nil && t { 33 | var buf bytes.Buffer 34 | s := bufio.NewScanner(bytes.NewReader(prog)) 35 | for s.Scan() { 36 | w, e := strconv.ParseInt(s.Text(), 10, 32) 37 | fatal(e) 38 | binary.Write(&buf, binary.LittleEndian, int32(w)) 39 | } 40 | prog = buf.Bytes() 41 | } 42 | fatal(e) 43 | copy(b, prog) 44 | 45 | M := map[string]machine{ 46 | "r5": &R5{}, 47 | } 48 | if x := M[m]; x == nil { 49 | panic("unknown machine: " + m) 50 | } else { 51 | if d { 52 | x.Dump(b, uint32(start)) 53 | } else { 54 | x.Start(b, uint32(start)) 55 | } 56 | } 57 | } 58 | func fatal(e error) { 59 | if e != nil { 60 | panic(e) 61 | } 62 | } 63 | 64 | type machine interface { 65 | Start([]byte, uint32) 66 | Dump([]byte, uint32) 67 | } 68 | -------------------------------------------------------------------------------- /_/plot/go.mod: -------------------------------------------------------------------------------- 1 | module k 2 | 3 | go 1.22.0 4 | 5 | require ( 6 | github.com/ktye/plot v0.0.0-20230925105801-a36e7a3c5421 7 | github.com/ktye/wg v0.0.0-20240815130816-69850581c913 8 | ) 9 | 10 | require ( 11 | github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect 12 | github.com/mattn/go-sixel v0.0.5 // indirect 13 | github.com/soniakeys/quant v1.0.0 // indirect 14 | golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect 15 | ) 16 | -------------------------------------------------------------------------------- /_/plot/mk: -------------------------------------------------------------------------------- 1 | (cat h;ls ../../*.go|grep -v main.go|grep -v _t|xargs cat|grep -v '^package'|awk '/import \(/{getline;getline;getline}/import ./{getline}{print}')>k_.go 2 | go build 3 | -------------------------------------------------------------------------------- /_/ps/k.ps: -------------------------------------------------------------------------------- 1 | /\\ { quit } bind def 2 | /. { dup == } bind def 3 | 4 | /flip { nyi } bind def 5 | /first { 0 get } bind def 6 | /were { mark exch 0 exch { { 1 copy } repeat 1 add } forall pop ] } bind def 7 | /reverse { mark exch aload length -1 2 { 1 roll } for ] } bind def 8 | /enlist { 1 array astore } bind def 9 | /til { 0 1 3 2 roll {} for array astore } bind def 10 | /take { exch 0 exch getinterval } bind def 11 | /at { mark 3 1 roll { exch dup 3 2 roll get exch } forall pop ] } bind def 12 | /cat { [ 2 index dup type (arraytype) eq { aload pop } if counttomark 1 add index dup type (arraytype) eq { aload pop } if ] exch pop exch pop} bind def 13 | /headtail { dup first exch dup length 1 exch 1 sub getinterval } bind def 14 | 15 | /grade { dup length til mrgsrt exch pop } bind def 16 | /mrgsrt { dup length 1 gt { 17 | dup dup length 2 div cvi 0 exch getinterval dup length 2 index length exch sub 1 index length exch 3 index 3 1 roll getinterval 3 2 roll pop 18 | 3 1 roll mrgsrt 3 1 roll exch mrgsrt 3 2 roll merge } if } bind def 19 | /merge { [ 4 1 roll { 1 index length 0 eq 1 index length 0 eq or { exit } if 20 | 2 index 2 index first get 3 index 2 index first get le { exch headtail exch 4 1 roll exch } { headtail exch 4 1 roll } ifelse 21 | } loop counttomark 1 add 3 roll ] exch cat exch cat } bind def 22 | /sort { dup grade at } bind def 23 | 24 | 25 | /ib { { 1 } { 0 } ifelse } bind def 26 | /find { mark 3 1 roll {eq ib} cvlit aload pop 3 array cvx astore forall ] were } bind def 27 | 28 | %vector-atom-func e.g. [1 2 3] 2 {add} vaf 29 | /vaf { mark 4 1 roll cvlit aload pop 2 array cvx astore forall ] } bind def 30 | /avf { 2 1 roll vaf } bind def 31 | /vvf { mark 0 1 4 index length 1 sub { 32 | dup dup 5 add index exch get exch dup 4 add index 1 index get exch 3 add index exec 33 | } for ] 4 1 roll pop pop pop } bind def 34 | 35 | 36 | /\ {(k.ps 37 | help.. 38 | ) print } bind def 39 | -------------------------------------------------------------------------------- /_/readme: -------------------------------------------------------------------------------- 1 | w :source of the w bootstrap compiler w.go (for k.w) 2 | i1 :the second interpreter (following apl/iv) 3 | i2 :the third interpreter (k7 era) 4 | i2/k- :minimal interactive interpreter (no filesystem/argv) only stdin/stdout 5 | i2/kwm :wasm build and js/html frontend to i2 6 | i3 :the 4th interpreter and the w language 7 | kwac :webassembly compiler stub written in early k7 8 | kui :k.w(go) + imgui(gioui) 9 | ngn :ngn/k +libc-asm+io (windows/macos/...) 10 | -------------------------------------------------------------------------------- /apl/66: -------------------------------------------------------------------------------- 1 | -7 2 | 'ALPHA⍴⍳' 3 | 1+2 4 | 3≤7 5 | 7≤3 6 | 1 2 3 4×4 3 2 1 7 | 2+1 2 3 4 8 | 1 2 3 4⌈2 9 | 1 2 3 10 | ⍳3 11 | )ORIGIN 0 12 | ⍳3 13 | )ORIGIN 1 14 | ⍳0 15 | 6-⍳6 16 | 2×⍳0 17 | 2×⍳6 18 | X,X←2 3 5 7 11 19 | ,2 3⍴⍳6 20 | ⍴1 21 | ⍴⍴1 22 | ⍴⍴⍴1 23 | ⍴1 2 3 24 | ⍴⍴1 2 3 25 | ⍴⍴⍴1 2 3 26 | ⍴2 3⍴⍳6 27 | ⍴⍴2 3⍴⍳6 28 | ⍴⍴⍴2 3⍴⍳6 29 | ⍴4 2 3⍴⍳6 30 | ⍴⍴4 2 3⍴⍳6 31 | ⍴⍴⍴4 2 3⍴⍳6 32 | 2 3⍴1 2 33 | ⍴'' 34 | '' 35 | 'X' 36 | 'CAN''T' 37 | A←'ABCDEFG' 38 | M←4 3⍴3 1 4 2 1 4 4 1 2 4 1 4 39 | A[M] 40 | (3 4⍴⍳12)[2;3] 41 | (M←3 4⍴⍳12)[1 3;2 3 4] 42 | M[2;] 43 | M[;2 1] 44 | M[M←4 3⍴3 1 4 2 1 4 4 1 2 4 1 4;] 45 | 3 3⍴1 0 0 0 46 | 3⍴1 47 | 2 3⍴⍳6 48 | 2 3 4⍴⍳6 49 | (1-2) 50 | (⍳3)+4 51 | X←⍳6 52 | X[2 1]←8 9 53 | X[2 1 3]←7 54 | 3 4⍳4 55 | 1 2 3 4⍳2 3⍴⍳6 56 | 'ABCDEFGH'⍳'GAFFE' 57 | 3 4 7∊⍳5 58 | 4∊⍳5 59 | 2 3 5 7∊⍳4 60 | (3 4⍴⍳12)∊2 3 5 7 61 | 0↑3 4 5 6 62 | 2↑3 4 5 6 63 | 2 3↑3 4⍴⍳12 64 | 2 ¯3↑3 4⍴⍳12 65 | 2↓3 4 5 6 66 | 1 2↓3 4⍴⍳12 67 | ¯1 1↓3 4⍴⍳12 68 | ⍋3 5 3 2 69 | )ORIGIN 0 70 | ⍋3 5 3 2 71 | )ORIGIN 1 72 | ⍒3 5 3 2 73 | P←2 3 5 7 74 | E←3 4⍴⍳12 75 | 1 0 1 0/P 76 | 1 0 1 0/E 77 | 1 0 1/[1]E 78 | 1 0 1⌿E 79 | 0 1 0 1/⍳4 80 | 1 0 1\⍳2 81 | 1 0 1 1 1\3 4⍴'ABCDEFGHIJKL' 82 | 1 0 1⍀2 3⍴⍳6 83 | ⍉2 3⍴⍳6 84 | 1 1⍉3 3⍴⍳9 85 | ⍴⍴(⍳0)⍉5 86 | ⌽X←3 4⍴'ABCDEFGHIJKL' 87 | ⌽X 88 | ⌽[1]3 4⍴'ABCDEFGHIJKL' 89 | ⊖X 90 | 3⌽2 3 5 7 91 | ¯1⌽2 3 5 7 92 | ¯7⌽'ABCDEF' 93 | 1⊖3 4⍴⍳12 94 | 1 0 ¯1⌽3 4⍴'ABCDEFGHIJKL' 95 | (2 2⍴0 ¯1 3 1)⌽[2]2 4 2⍴⍳16 96 | (2 4⍴0 1 ¯1 0 0 3 2 1)⌽[1]2 2 4⍴⍳16 97 | 10⊥1 7 7 6 98 | 24 60 60⊥1 2 3 99 | 24 60 60⊤3723 100 | 60 60⊤3723 101 | 1+-3 102 | A←4 103 | A+2+A←4 104 | 3 4[2] 105 | +/1 2 3 106 | +/⍳3 107 | +/2 3⍴⍳6 108 | +/[1]2 3⍴⍳6 109 | +⌿2 3⍴⍳6 110 | +/[2]2 4 3⍴⍳24 111 | 2 3 4+.×5 6 7 112 | 1 2 3∘.×1 2 3 4 113 | (⍳3)∘.×⍳4 114 | -------------------------------------------------------------------------------- /f77/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | if [ "$1" = "test" ]; then 5 | awk -f t.awk < ../k.t > k.t 6 | ./k22 k.t -e 7 | cp ../apl/66 . 8 | ./k22 66 ../apl/apl.k -e > out 9 | cmp out ../apl/out 10 | rm 66 out 11 | exit 0 12 | fi 13 | 14 | wg -f .. > k.f 15 | 16 | #gfortran -Wall -Wfatal-errors -Werror -Wno-unused-dummy-argument -o k k.f 17 | 18 | gfortran -ok22 k.f -fno-range-check 19 | 20 | -------------------------------------------------------------------------------- /f77/readme: -------------------------------------------------------------------------------- 1 | Apr 1, 2022 2 | 3 | let's take k asof 22 4 | compile with fortran 77 5 | to run apl like it was 1966 6 | -------------------------------------------------------------------------------- /f77/t.awk: -------------------------------------------------------------------------------- 1 | BEGIN{FS=" /"} 2 | $2~/3a300 0a/{next} 3 | $2~/6a240/{next} 4 | $2~/(3a80;4a80 5a80)/{next} 5 | $2~/1a80 1a280 1a90.2 1a/{next} 6 | $2~/3a70/{next} 7 | $2~/4a70/{next} 8 | {print} 9 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/ktye/i/k 2 | 3 | go 1.16 4 | 5 | require github.com/ktye/wg v0.0.0 6 | 7 | replace github.com/ktye/wg => ../wg 8 | -------------------------------------------------------------------------------- /k.e: -------------------------------------------------------------------------------- 1 | -'3 /Type Ech atom 2 | (+`a`b!(1 2;3 4)),'+`a`b!(1 2 3;4 5 6) /Length 3 | 1.2?2 3 /Type 4 | 1/[2;3] /Nyi 5 | {x}3./4 /Type 6 | 1\[2;3] /Rank 7 | 1/:[1;2] /Rank 8 | 1+`x /Type 9 | `c@1 /Type 10 | `i@"abc" /Length 11 | (`46)[`a;1] /Value 12 | +/:[1;2;3] /Rank 13 | +\:[1;2;3] /Rank 14 | 1\:[2;3] /Rank 15 | (2000000000)#1a20 /Grow 16 | +[1;2;3;4;5] /Rank 17 | 1$2 /Type 18 | (+-)[1;2;3] /Rank 19 | (1+)[2;3] /Rank 20 | {x+y}[1;2;3] /Rank 21 | 1 2 3?4. /Type 22 | (+`a`b!(1 2;3 4))?5 /Nyi 23 | find[1 2 3;4 5] /Type 24 | find["abc";"b"] /Type 25 | `p 2 /Type 26 | [2} /Parse 27 | ) /Parse 28 | {x+1] /Parse 29 | {[1;2]x} /Parse 30 | "a /Parse 31 | 3.'1 2 3 /Type 32 | .(1;`66) /Stack 33 | (`0).("x";5) /Type 34 | @[3;1 2;4] /Type 35 | .[3^!9;(0 1;0 1;0 1);9] /Rank 36 | .[+`a`b!(1 2;3 4);(`a`b;1);5] /Nyi 37 | `p 0b /Type 38 | +1 2!3 4 /Type 39 | abs[2;3] /Nyi 40 | `a+`b /Type 41 | `a+`b`c /Type 42 | `a`b+`c`d /Type 43 | -`a /Type 44 | -`a`b /Type 45 | -(-) /Type 46 | 1 2+3 4 5 /Length 47 | `x%`y /Type 48 | angle[2a45;1a] /Type 49 | log[2a45;3] /Type 50 | 3.5/4 /Type 51 | `a`b+1 2 /Type 52 | !"x" /Type 53 | ?`a`b!1 2 /Type 54 | `a`b`c!1 2 /Length 55 | 3_5 /Type 56 | 3.0_1 2 /Type 57 | 3.0^1 2 /Type 58 | &1 2 3. /Type 59 | . 1 /Type 60 | 3.0'1 2 /Type 61 | 1 2 3'3. /Type 62 | 1 -2 3^!10 /Value 63 | 3 4\:1 2 3 4 /Type 64 | "a"/:"alpha" /Type 65 | "a"/:("alpha";1 2) /Type 66 | ?`a /Type 67 | 3.?1 2 3 /Type 68 | 3.?4 /Type 69 | 10?3. /Type 70 | +/`a`b`c /Type 71 | */`a`b`c /Type 72 | @[!10;!3;!4] /Length 73 | @[!10;10;4] /Index 74 | @[!10;8+!3;1 2 3] /Index 75 | @[1 2 3;0 1.;4 5] /Type 76 | 2000000000#"a" /Grow 77 | 2000000000#1a /Grow 78 | +/1_."(!0;",(";"/:$!1000),")" /Stack 79 | 3 4@1a2 3a4 /Type 80 | 2 3!3 81 | 1 2!3 4 5 82 | "a"\1 2 3 83 | -------------------------------------------------------------------------------- /kcsv/csv_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "testing" 7 | ) 8 | 9 | var save []byte 10 | 11 | func newtest() { 12 | rand_ = 1592653589 13 | if save == nil { 14 | kinit() 15 | save = make([]byte, len(Bytes)) 16 | copy(save, Bytes) 17 | } else { 18 | Bytes = make([]byte, len(save)) 19 | copy(Bytes, save) 20 | pp, pe, sp = 0, 0, 2048 21 | } 22 | } 23 | 24 | func TestCsv(t *testing.T) { 25 | testCases := []struct { 26 | fm, fi, exp, fstr string 27 | }{ 28 | {"zz", file1, k1, ",0:22-2:22-4"}, 29 | {"", file2, k2a, ";0:18-1:18-2:18-3:18-4"}, 30 | {";i2ff", file2, k2b, ";0:19-2:21-3:21-4"}, 31 | {";2h0i3f", file2, k2c, ";0:19-3:21-4"}, 32 | {" csi", file3, k3, " 0:18-1:20-2:19-3"}, 33 | } 34 | for i, tc := range testCases { 35 | newtest() 36 | var buf bytes.Buffer 37 | x, f := kcsv(tc.fm, []byte(tc.fi), false) 38 | if s := f.fstr(); s != tc.fstr { 39 | t.Fatalf("tc %d format: exp %s got %s", i, tc.fstr, s) 40 | } 41 | wCK(&buf, Kst(x)) 42 | got := string(buf.Bytes()) 43 | exp := tc.exp 44 | if got != exp { 45 | t.Fatalf("tc %d\nexp\n%s\ngot\n%s", i, exp, got) 46 | } 47 | } 48 | } 49 | 50 | func (f *format) fstr() string { 51 | s := string(f.s) 52 | for i, t := range f.t { 53 | s += fmt.Sprintf("%d:%d-", f.i[i], t) 54 | } 55 | return s + fmt.Sprintf("%d", f.columns) 56 | } 57 | 58 | const file1 = ` 59 | 1,90,2,270 60 | 2,90,3,180 61 | ` 62 | const k1 = `(1a90 2a90;2a270 3a180)` 63 | 64 | const file2 = ` 65 | 12;"alpha";2,5;4.5 66 | 23;"beta"; 3,0;5.5 67 | 34;"gamma";4.0;6.5` 68 | 69 | const k2a = `(("12";"23";"34");("alpha";"beta";"gamma");("2,5";"3,0";"4.0");("4.5";"5.5";"6.5"))` 70 | const k2b = `(12 23 34;2.5 3. 4.;4.5 5.5 6.5)` 71 | const k2c = `(,34;,6.5)` 72 | 73 | const file3 = ` 74 | alpha hans 1 75 | beta peter 2 76 | gamma jochen 3 77 | ` 78 | const k3 = "((\"alpha\";\"beta\";\"gamma\");`hans`peter`jochen;1 2 3)" 79 | -------------------------------------------------------------------------------- /kcsv/head: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "bytes" 6 | "encoding/binary" 7 | "encoding/csv" 8 | "io" 9 | "math" 10 | "math/bits" 11 | "math/cmplx" 12 | "os" 13 | _ "embed" 14 | "regexp" 15 | "strconv" 16 | "strings" 17 | "text/tabwriter" 18 | ) 19 | -------------------------------------------------------------------------------- /kcsv/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | cat head > kcsv.go 5 | k ../x/k.k ../x/go.k -e 'x:`> kcsv.go 6 | cat tail >> kcsv.go 7 | 8 | if [ "$1" = "test" ];then 9 | go test 10 | else 11 | go install kcsv.go 12 | fi 13 | 14 | rm kcsv.go 15 | -------------------------------------------------------------------------------- /kcsv/readme: -------------------------------------------------------------------------------- 1 | $kcsv [fmt] f.csv [files.k] [-e expr] 2 | stores csv as inverted table(L of columns) to x:.. 3 | eval expr, or drop into k 4 | 5 | $kcsv [fmt] f.csv -t|-b 6 | -t writes inverted table in k-text format to stdout 7 | -b binary 8 | 9 | $ksv [fmt] f.csv -h 10 | print selected colnames from first line 11 | 12 | fmt 13 | ;0i4i ;-separated, column 0 and 4 as int 14 | ,2hifs skip 2 header lines 15 | 16 | i(int) f(float) s(symbol) c(chars) z(2 cols: abs,ang) 17 | default sep (1.row max# of ,;|t space:strings.Fields) 18 | -------------------------------------------------------------------------------- /kcsv/t.csv: -------------------------------------------------------------------------------- 1 | alpha;beta;gamma;degree 2 | 1;2.5;3.5;90 3 | 2;4.5;5.5;180 4 | 3;6.5;7.5;270 5 | -------------------------------------------------------------------------------- /kcsv/unpack.k: -------------------------------------------------------------------------------- 1 | / binary data 2 | / $kcsv '1hifz' t.csv -b > data 3 | 4 | b:<`data 5 | unpack:{s:x;g:{[n]r:n#s;s::n_s;r};n:{*`i@g 4};u:{x;$[(t:*g 1)':"bcifz";*(`$t)g n[];t~"s";`$u 0;t~"S";`$u 0;t~"L";u'!n[];t~"D";(u 0)!u 0;t~"T";+(u 0)!u 0;(`$_t+32)g n[]]};u 0} 6 | t:+`a`b`c!unpack b 7 | -------------------------------------------------------------------------------- /kos/amas.k: -------------------------------------------------------------------------------- 1 | /armv6 assembler 2 | 3 | /O R X Y M L A table: opcode,dst,src,arg,imm,label,addr 4 | /s s s s i s s label/addr are symbolic and resolved 5 | 6 | /conds, shifts, ...? 7 | 8 | op:2097152*`and`eor`sub`rsb`add`adc`sbc`src`tst`teq`cmp`cmn`orr`mov`bic`mvn? 9 | rm:`r0`r1`r2`r3`r4`r5`r6`r7`r8`r9`r10`fp`ip`sp`lr`pc`cpsc? 10 | rn:65536*rm@ 11 | rd: 4096*rm@ 12 | 13 | rot:{y@32/32-x-!32} 14 | mmi:{2//rot[2*y;(32#2)\\x]} 15 | imm:(,/mmi/:[!256;!16])? /encode possible immediates 16 | bit:+/(2^/:!32)@ /bit 1 12 (sets bits 1 12) 17 | 18 | al:3758096384 /always 0b111... 19 | im:33554432+imm@ 20 | orrr:al+/(op;rd;rn;rm)@' /orrr`add`r3`r2`r3 -> -528338941 e0823003 21 | orri:al+/(op;rd;rn;im)@' /orri`sub`r2`r3,256 -> -498914303 e2432c01 22 | push:-382926848+bit@rm@ /push`fp`lr(multi) -> -382908416 e92d4800 23 | pop: -390266880+bit@rm@ /pop`fp`lr -> -390248448 e8bd4800 24 | 25 | 26 | h:|`c@, 27 | /encode: go.dev/play/p/41_1hQtVWVW 28 | -------------------------------------------------------------------------------- /kos/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | mkdir -p t/ 5 | 6 | if [ "$1" = "softfloat" ]; then 7 | # (1) add softfloat.go +3316-wasm-bytes 8 | cp ../*.go t/ 9 | cp softfloat.go t/ 10 | 11 | # typecheck: 12 | wg t/ > out 13 | wat2wasm out > out.wasm 14 | 15 | wg -k t/ > k.k 16 | 17 | # test k.go+softfloat 18 | k k.k softfloat.k ../x/go.k -e 'x:`"k.go" t/a.d 33 | 34 | -------------------------------------------------------------------------------- /kos/softfloat.k: -------------------------------------------------------------------------------- 1 | /softfloat.k replaces all float-nodes with function calls in IR (modifies T P I S) 2 | 3 | del:{T::T n:(!#P)_x;P::@[P n;&(P n)>x-1;-;1];I::I n;S::S n;} /delete a node 4 | ins:{[i;x;y](i#x),y,i_x} /insert (todo vectorize) 5 | INS:{[i;x]T::ins[i;T;*x];P::ins[i;@[P;&P>i-1;+;1];x 1];P[1+i]:i;I::ins[i;I;x 2];S::ins[i;S;x 3]} 6 | 7 | 8 | /replace float-cast with calls 9 | S[i]:`$($S i:i@&`f=S 1+i:&T=`cst),\:"fcst";T[i]:`cal;del'|1+i /cast ji<-f [ij]fcst 10 | S[i]:`$"f",/:($S 1+i:&(`f=S)&T=`cst),\:"cst";T[i]:`cal;del'|1+i /cast f<-ji f[ij]cst 11 | 12 | 13 | /insert casts when for float load/store 14 | cast:{INS[z;(`cst;P z;0N;x)];INS[1+z;(`typ;z;0N;y)];P[2+z]-:1} /insert cast node: uint64(F64(x)) 15 | cast[`k;`f]'|i:&(S=`f)&(T=`lod) /load-f64: cast to k 16 | cast[`j;`f]@'{(&P=x)1}'|i:&(S=`f)&(T=`sto) /store: cast 2nd child 17 | 18 | 19 | /replace float types with u64 for many node types 20 | S[&(S=`f)&T':`lit`arg`loc`res`ret`cli`typ`cnd`swc]:`k /`f->`k 21 | S[&(S=`f)&T':`lod`sto ]:`j 22 | 23 | 24 | /replace ops with calls to softfloat implementations 25 | f:f!`$"f",/:$f:`add`sub`mul`div`les`lte`eql`neq`mor`gte /binary ops/cmp 26 | S[i]:f T i:i@&`f=S i:&T':!f;T[i]:`cal /softfloat calls 27 | S[i:&(T=`neg)&S=`f]:`fneg; T[i]:`cal /negate 28 | 29 | 30 | /replace float intrinsics (pseudo-functions) with softfloat implementations 31 | {S[&S=y]:x}'[`fabs`fsqr`flor`fmin`fmax`fcps;`$"F64",/:$`abs`sqrt`floor`min`max`copysign] 32 | 33 | 34 | /remove reinterpretations (now non-ops) and lift child nodes 35 | j:1+i:&S':`"F64reinterpret_i64"`"I64reinterpret_f64" 36 | T[i]:T j;I[i]:I j;S[i]:S j;del'|j 37 | -------------------------------------------------------------------------------- /kx/head: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/binary" 5 | "math" 6 | "math/bits" 7 | "os" 8 | "bufio" 9 | _ "embed" 10 | "math/cmplx" 11 | "regexp" 12 | "strconv" 13 | "strings" 14 | ) 15 | -------------------------------------------------------------------------------- /kx/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | cat head > kx.go 5 | k ../x/k.k ../x/go.k -e 'x:`> kx.go 6 | cat tail >> kx.go 7 | 8 | 9 | go install kx.go 10 | rm -f kx.go 11 | -------------------------------------------------------------------------------- /kx/readme: -------------------------------------------------------------------------------- 1 | input | kx [lib.k] '+\x' '#x' [-e ...] 2 | 3 | each row is split at whitespace assigned to x, row number is i: 4 | x:("col0";"col1";..) 5 | 6 | each argument is evaluated per line and joined by space 7 | 8 | unitype conversion for complete line 9 | xi short for (`i$x) 10 | xf (`f$x) 11 | xs (`$x) 12 | 13 | convert individual fields 14 | 0i short for (`i x 0) 15 | 2f (`f x 2) 16 | 3s (`s x 3) 17 | 3c (x 3) 18 | 3z ((`f x 3)@`f x 1+3) amplitude angle(deg) 19 | ° ' each (e.g. in quotes) 20 | 21 | filter 22 | /cond/prog short for $[cond;prog;] 23 | \cond\prog alternative, e.g if cond contains / 24 | 25 | aggregate 26 | sum+:x 27 | 28 | examples 29 | number lines: kx i x 30 | row sum: kx +/xf 31 | number of columns: kx '#x' 32 | amplitude angle as cmplx kx 0f@1f 33 | lines with first col peter kx '/`peter~0s/x' 34 | column average kx 'sum+:xf' -e 'sum%i' 35 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | //go:build !small 2 | 3 | package main 4 | 5 | import ( 6 | . "github.com/ktye/wg/module" 7 | ) 8 | 9 | func main() { // _start 10 | kinit() 11 | doargs() 12 | write(Ku(2932601077199979)) // "ktye/k\n" 13 | store() 14 | for { 15 | write(Ku(32)) 16 | x := readfile(mk(Ct, 0)) 17 | try(x) 18 | } 19 | } 20 | 21 | func store() { 22 | g := (1 << (I32(128) - 16)) - Memorysize2() 23 | if g > 0 { 24 | Memorygrow2(g) 25 | } 26 | Memorycopy2(0, 0, int32(1)< /tmp/k/k.c && cc -O3 -Wall -o /tmp/k/k /tmp/k/k.c -Wfatal-errors -lm 6 | #wg -tags simd5 -c . > /tmp/k/v.c && clang-17 -O3 -Wall -o /tmp/k/kv -mavx2 /tmp/k/v.c -Wfatal-errors -lm 7 | #exit 0 8 | 9 | # embed z.k in z.go 10 | sed -e '/^\//d' -e 's, */.*,,' -e 's/^ *//' -e '/^$/d' z.k > k.k 11 | zn=`wc -c k.k | sed 's/ .*//'` 12 | zk=`sed -e 's/\\\/\\\\\\\/g' -e 's/"/\\\"/g' -e 's/$/\\\/g' k.k | tr '\n' 'n'` 13 | rm k.k 14 | cat << EOF > z.go 15 | package main 16 | 17 | import . "github.com/ktye/wg/module" 18 | 19 | func zk() { 20 | Data(280, "$zk") 21 | zn := int32($zn) // should end before 2k 22 | x := mk(Ct, zn) 23 | Memorycopy(int32(x), 280, zn) 24 | dx(Val(x)) 25 | } 26 | EOF 27 | 28 | go install 29 | 30 | if [ "$1" = "cover" ]; then 31 | go test -coverprofile=cov.out 32 | go tool cover -html=cov.out -o cov.html 33 | fi 34 | 35 | if [ "$1" = "web" ]; then 36 | wg -tags small -nomain . | wat2wasm - -o /c/k/ktye.github.io/k.wasm 37 | wasm-opt -Oz --enable-bulk-memory /c/k/ktye.github.io/k.wasm -o - | wc -c 38 | go run ./_/kdoc.go > /c/k/ktye.github.io/kdoc.htm 39 | else 40 | wg -tags small -nomain . | wat2wasm - -o k.wasm 41 | wasm-opt -Oz --enable-bulk-memory k.wasm -o z.wasm 42 | zstd -19 -f z.wasm 43 | wc -c k.wasm z.wasm z.wasm.zst 44 | rm k.wasm z.wasm z.wasm.zst 45 | fi 46 | 47 | -------------------------------------------------------------------------------- /nomain.go: -------------------------------------------------------------------------------- 1 | //go:build small 2 | 3 | package main 4 | 5 | import ( 6 | . "github.com/ktye/wg/module" 7 | ) 8 | 9 | func main() {} 10 | -------------------------------------------------------------------------------- /p.k: -------------------------------------------------------------------------------- 1 | 2 | S:" \t" /space 3 | L:"({[;\n" /left 4 | R:")}]" /right 5 | A:"'/\\" /adverb 6 | V:"+-*%&|<>=^!~,#_$?@." /verb 7 | 8 | C:(S,L;R;V;A;".",_,/65 97+\!26;"0123456789"),"\"`-:e\\/\n" 9 | C:@[;;]/[-1+&256;0+C;!#C]@0+ 10 | 11 | t:1_'(&"\n"=t)^t:1_(" 12 | ;)+'a0q`-:e\/n 13 | ;;)+'a0q`-+a'/; 14 | );)+'a0q`++a''; 15 | +;)+'a0q`-+a''; 16 | ';)+'a0q`-:a''; 17 | a;)+'bbq`++b''; 18 | 0;)+'11q`+:e''; 19 | qrrrrrrtrrrrsrr 20 | `;)+'b0q`++b''; 21 | -;)+'a1q`-+a''; 22 | /ccccccccccccc; 23 | cccccccccccccc; 24 | :;)+'a0q`-+a''; 25 | b;)+'bbq`++b''; 26 | 1;)+'11q`++e''; 27 | e;)+'11q`1+1''; 28 | rrrrrrrtrrrrsrr 29 | srrrrrrrrrrrrrr 30 | t;)+'a0q`++a'';") 31 | 32 | T:t[;0]?/1_'t 33 | 34 | w:{x@&{$[(*x)?S;0;"/"=*x;1=#x;1]}'x:(&10>{T[x;y]}\C x)^x:" ",x} 35 | 36 | D:`plus`minus`times`dividedby`and`or`less`more`equal`power`mod`match`join`take`drop`form`find`at`dot 37 | M:`flip`negate`first`reciprocal`where`reverse`upgrade`downgrade`group`shape`enumerate`not`enlist`count`floor`format`unique`atom`value 38 | DMA:$D,M,`each`over`scan`eachprior`eachright`eachleft 39 | dma:(,'V),(V,'":"),(,'A),A,'":" 40 | 41 | q:{(*|x)?V} 42 | p:{x:w x 43 | c:*'x 44 | d:q'x 45 | e:c=":" 46 | b:1_(c?\"[;\n",R,A),1 47 | i:&d&(-1_1,d|e|c?\L,A)&~b 48 | x[i]:x[i],'":" 49 | x:x(&b&e&~-1_1,c?\L)_!#x 50 | -1_,/{$[0N~i:dma?x;x;DMA i]," "}'x} 51 | 52 | \p"1_'(&x=y)_y:x,y /comment" 53 | -------------------------------------------------------------------------------- /ps.k: -------------------------------------------------------------------------------- 1 | st:() 2 | ps:{ev'`t x;`0} 3 | pu:{st,:x} 4 | po:{r: *|st;st::-1_st;r} 5 | xy:{r:-2#st;st::-2_st;r} 6 | sy:{$[r:bi x;r[];x]} 7 | ev:{t:@x;$[t~`s;sy x;pu x]} 8 | dy:{pu@x/xy[]} 9 | ad:{dy(+)} 10 | ro:{(x#y),x_y} 11 | bi:{ 12 | stack:{ \st} 13 | add:{dy(+)} 14 | sub:{dy(-)} 15 | eq: {dy(~)} 16 | ne: {dy(~~)} 17 | dup:{pu[*|st]} 18 | pop:{po[]} 19 | copy:{[]n:-po[];st,:n#st} 20 | roll:{[]i:po[];n:-po[];st::(n_st),ro[-i;n#st]} 21 | exch:{pu'xy[]} } 22 | 23 | -------------------------------------------------------------------------------- /rand.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | . "github.com/ktye/wg/module" 5 | ) 6 | 7 | func rnd() int32 { 8 | r := rand_ 9 | r ^= (r << 13) 10 | r ^= (r >> 17) 11 | r ^= (r << 5) 12 | rand_ = r 13 | return r 14 | } 15 | func roll(x K) K { // ?x (atom) ?n(uniform 0..1) ?-n(normal) ?z(binormal) 16 | xt := tp(x) 17 | xp := int32(x) 18 | if xt == it { 19 | if xp > 0 { 20 | return kx(72, x) // .rf uniform 21 | } else { 22 | r := kx(80, Ki((1+-xp)/2)) 23 | SetI32(int32(r)-12, -xp) 24 | return ti(Ft, int32(r)) // normal 25 | } 26 | } 27 | if xt == zt { 28 | dx(x) 29 | return kx(80, Ki(int32(F64floor(F64(xp))))) //.rz binormal 30 | } 31 | trap() //type 32 | return 0 33 | } 34 | func deal(x, y K) K { // x?y (x atom) n?n(with replacement) -n?n(without) n?L (-#L)?L shuffle 35 | yt := tp(y) 36 | if yt > 16 { 37 | return In(x, y) 38 | } 39 | if tp(x) != it { 40 | trap() //type 41 | } 42 | xp := int32(x) 43 | if yt == ct { 44 | return Add(Kc(97), Flr(deal(x, Ki(int32(y)-96)))) 45 | } 46 | if yt == st { 47 | return Ech(17, l2(Ks(0), deal(x, Fst(cs(y))))) // `$'x?*$y 48 | } 49 | if yt != it { 50 | trap() //type 51 | } 52 | yp := int32(y) 53 | if xp > 0 { 54 | return randI(yp, xp) // n?m 55 | } 56 | // todo n<> 32) 72 | } 73 | func randI(i, n int32) K { 74 | r := mk(It, n) 75 | rp := int32(r) 76 | e := ep(r) 77 | if i == 0 { 78 | for rp < e { 79 | SetI32(rp, rnd()) 80 | rp += 4 81 | } 82 | } else { 83 | for rp < e { 84 | SetI32(rp, randi(i)) 85 | rp += 4 86 | } 87 | } 88 | return r 89 | } 90 | func shuffle(r K, m int32) K { // I, inplace 91 | rp := int32(r) 92 | n := nn(r) 93 | m = mini(n-1, m) 94 | for i := int32(0); i < m; i++ { 95 | j := rp + 4*randi(n-i) 96 | t := I32(rp) 97 | SetI32(rp, I32(j)) 98 | SetI32(j, t) 99 | rp += 4 100 | } 101 | return r 102 | } 103 | -------------------------------------------------------------------------------- /readme: -------------------------------------------------------------------------------- 1 | ktye/k ktye.github.io/k.html 2 | + flp add ' ech pri both bin 3 | - neg sub / ovr fix echright 4 | * fst mul \ scn fix eachleft 5 | % sqr div / join decode 6 | ! til key mod \ split encode 7 | & wer min $[a;b;...] cond 8 | | rev max while[c;a;b;d;e;..] 9 | < asc les f:{x+y} [bl;o;ck] 10 | > dsc mor "chars" c 11 | = grp eql 01234567 1 2 3 i 12 | ~ not mtc :+-*%&| .4 5 6. f 13 | , enl cat <>=~!,^# 2a300 z 14 | ^ srt cut _$?@. (1;2 3) L 15 | # cnt tak `a`b!5 6 D 16 | _ flr drp t,d t,t t,'t join 17 | $ str cst k!t key 18 | ? unq fnd in k?t group 19 | @ typ atx @[x;i;+;y] amend 20 | . val cal .[x;i;+;y] dmend 21 | 22 | abs sin cos exp log find angle 23 | imag conj types:cisfzLDTvcdlx 24 | ?n(uniform) ?-n(normal) ?z(bi) 25 | n?n(with) random -n?n(w/o) 26 | -------------------------------------------------------------------------------- /reduce.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | . "github.com/ktye/wg/module" 5 | ) 6 | 7 | func rd0(yp int32, t T, n int32) K { return 0 } 8 | func min(yp int32, t T, e int32) K { // &/x 9 | var xp int32 10 | switch t - 18 { 11 | case 0: // Ct 12 | xp = 127 13 | for yp < e { 14 | xp = mini(xp, I8(yp)) 15 | yp++ 16 | } 17 | return Kc(xp) 18 | case 1: // It 19 | return Ki(minis(2147483647, yp, e)) 20 | case 2: // St 21 | return Ks(minis((nn(K(I64(8)))<<3)-8, yp, e)) 22 | case 3: // Ft 23 | return Kf(minfs(yp, e)) 24 | default: 25 | return 0 26 | } 27 | } 28 | func max(yp int32, t T, e int32) K { // |/x 29 | var xp int32 30 | switch t - 18 { 31 | case 0: // Ct 32 | xp = -128 33 | for yp < e { 34 | xp = maxi(xp, I8(yp)) 35 | yp++ 36 | } 37 | return Kc(xp) 38 | case 1: // It 39 | return Ki(maxis(nai, yp, e)) 40 | case 2: // St 41 | return Ks(maxis(0, yp, e)) 42 | case 3: // Ft 43 | return Kf(maxfs(yp, e)) 44 | default: 45 | return 0 46 | } 47 | } 48 | func sum(yp int32, t T, e int32) K { // +/x 49 | xp := int32(0) 50 | switch t - 18 { 51 | case 0: // Ct 52 | for yp < e { 53 | xp += I8(yp) 54 | yp++ 55 | } 56 | return Kc(xp) 57 | case 1: // It 58 | return Ki(xp + sumi(yp, e)) 59 | case 2: // St 60 | return 0 61 | case 3: // Ft 62 | f := 0.0 63 | return Kf(f + sumf(yp, e)) 64 | case 4: // Zt 65 | r := Kz(0, 0) 66 | sumz(yp, e, int32(r)) 67 | return r 68 | default: 69 | return 0 70 | } 71 | } 72 | func prd(yp int32, t T, e int32) K { // */x 73 | xp := int32(1) 74 | switch t - 18 { 75 | case 0: // Ct 76 | for yp < e { 77 | xp *= I8(yp) 78 | yp++ 79 | } 80 | return Kc(xp) 81 | case 1: // It 82 | for yp < e { 83 | xp *= I32(yp) 84 | yp += 4 85 | } 86 | return Ki(xp) 87 | case 2: // St 88 | return 0 89 | case 3: // Ft 90 | f := 1.0 91 | for yp < e { 92 | f *= F64(yp) 93 | yp += 8 94 | } 95 | return Kf(f) 96 | default: 97 | return 0 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /s.k: -------------------------------------------------------------------------------- 1 | 2 | /s.k is used only in the small build. 3 | /it adds k replacement functions in addition to z.k. 4 | 5 | `".o0":{x:((0x00;0;0.;0a);;(0x01;1;1.);;(_127;0N-1;0w);(_-128;-0w))[`x \x];$[0N~i:(@'x)?@`y \y;y;x i]} 6 | -------------------------------------------------------------------------------- /simd4.go: -------------------------------------------------------------------------------- 1 | //go:build simd4 2 | 3 | package main 4 | 5 | // 16byte simd, e.g. wasm simd128 6 | 7 | import ( 8 | . "github.com/ktye/wg/module" 9 | ) 10 | 11 | func init() { 12 | Simd(4) 13 | } 14 | 15 | const b0 = 5 //smallest bucket 16 | const bs = 15 //lower bits set 17 | const vl = 16 //vector length 18 | 19 | func ev(ep int32) int32 { return (15 + ep) & -16 } 20 | func eu(ep int32) int32 { return ep & -16 } 21 | func sumz(x, e, r int32) { 22 | a := VFsplat(0.0) 23 | for x < e { 24 | a = a.Add(VFload(x)) 25 | x += vl 26 | } 27 | VFstore(r, a) 28 | } 29 | -------------------------------------------------------------------------------- /simd5.go: -------------------------------------------------------------------------------- 1 | //go:build simd5 2 | 3 | package main 4 | 5 | //gcc 32byte vector extensions(avx2..) 6 | 7 | import ( 8 | . "github.com/ktye/wg/module" 9 | ) 10 | 11 | func init() { 12 | Simd(5) 13 | } 14 | 15 | const b0 = 6 //smallest bucket 16 | const bs = 31 //lower bits set 17 | const vl = 32 //vector length 18 | 19 | func ev(ep int32) int32 { return (31 + ep) & -32 } 20 | func eu(ep int32) int32 { return ep & -32 } 21 | func sumz(x, e, r int32) { 22 | a := VFsplat(0.0) 23 | u := eu(e) 24 | for x < u { 25 | a = a.Add(VFload(x)) 26 | x += vl 27 | } 28 | re := a.HsumEven() 29 | im := a.HsumOdd() 30 | for x < e { 31 | re += F64(x) 32 | im += F64(x + 8) 33 | x += 16 34 | } 35 | SetF64(r, re) 36 | SetF64(r+8, im) 37 | } 38 | -------------------------------------------------------------------------------- /size: -------------------------------------------------------------------------------- 1 | 40701 36986 /initial 2 | 40637 36910 /renumber call table 3 | 40381 36661 /remove dict literals 4 | -------------------------------------------------------------------------------- /system.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | . "github.com/ktye/wg/module" 5 | ) 6 | 7 | func repl(x K) { 8 | c := I8(int32(x)) 9 | x = val(x) 10 | if x != 0 { 11 | if c == 32 { 12 | dx(Out(x)) 13 | } else { 14 | write(cat1(join(Kc(10), Lst(x)), Kc(10))) 15 | } 16 | } 17 | } 18 | 19 | func Out(x K) K { 20 | write(cat1(Kst(rx(x)), Kc(10))) 21 | return x 22 | } 23 | func Otu(x, y K) K { 24 | write(cat1(Kst(x), Kc(':'))) 25 | return Out(y) 26 | } 27 | func write(x K) { 28 | Write(0, 0, int32(x), nn(x)) 29 | dx(x) 30 | } 31 | func readfile(x K) K { // x C 32 | var r K 33 | if nn(x) == 0 { 34 | dx(x) 35 | r = mk(Ct, 496) 36 | r = ntake(ReadIn(int32(r), 496), r) 37 | return r 38 | } 39 | n := Read(int32(x), nn(x), 0) 40 | if n < 0 { 41 | dx(x) 42 | return mk(Ct, 0) 43 | } 44 | r = mk(Ct, n) 45 | Read(int32(x), nn(x), int32(r)) 46 | dx(x) 47 | return r 48 | } 49 | func writefile(x, y K) K { // x, y C 50 | r := Write(int32(x), nn(x), int32(y), nn(y)) 51 | if r != 0 { 52 | trap() //io 53 | } 54 | dx(x) 55 | return y 56 | } 57 | -------------------------------------------------------------------------------- /tco.awk: -------------------------------------------------------------------------------- 1 | #patch tail calls 2 | #substitute call and call indirect with return call 3 | #if they are the last instruction in a function. 4 | # (next line starts with ^\( 5 | #or if the next line is return 6 | #dont subsitute if the function does not have a result 7 | # 8 | # e.g. use: 9 | # wg -tags 'small,simd4' -nomain . |awk -f tco.awk| wat2wasm --enable-tail-call - -o /c/k/ktye.github.io/k.wasm 10 | # 11 | 12 | BEGIN{c=0;r=0} 13 | #{ print ">>>" $0 } 14 | /^\(func/{r=0} 15 | /^\(func.*result/{r=1} 16 | /^ *return/{ 17 | if(c&&r){print " return_" x; x=""} 18 | else{ 19 | print x 20 | x=$0 21 | } 22 | c=0 23 | next 24 | } 25 | /^\(/{ 26 | if(c&&r) print " return_" x 27 | else print x 28 | c=0 29 | x=$0 30 | next 31 | } 32 | /^ *call/{ 33 | c=1 34 | print x 35 | sub(/^ */,"") 36 | x=$0 37 | next 38 | } 39 | { 40 | print x 41 | c=0 42 | x=$0 43 | } 44 | END{print x} 45 | -------------------------------------------------------------------------------- /test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func test(x K) { 4 | if tp(x) != Ct { 5 | trap() //type 6 | } 7 | l := ndrop(-1, split(Kc(10), rx(x))) 8 | n := nn(l) 9 | dx(l) 10 | for i := int32(0); i < n; i++ { 11 | testi(rx(x), i) 12 | } 13 | dx(x) 14 | } 15 | func testi(l K, i int32) { 16 | x := split(Ku(12064), ati(split(Kc(10), l), i)) 17 | if nn(x) != 2 { 18 | trap() //length 19 | } 20 | y := x1(x) 21 | x = r0(x) 22 | dx(Out(ucat(ucat(rx(x), Ku(12064)), rx(y)))) 23 | x = Kst(val(x)) 24 | if match(x, y) == 0 { 25 | x = Out(x) 26 | trap() //test fails 27 | } 28 | dxy(x, y) 29 | } 30 | -------------------------------------------------------------------------------- /tests/66: -------------------------------------------------------------------------------- 1 | -7 2 | 'ALPHA⍴⍳' 3 | 1+2 4 | 3≤7 5 | 7≤3 6 | 1 2 3 4×4 3 2 1 7 | 2+1 2 3 4 8 | 1 2 3 4⌈2 9 | 1 2 3 10 | ⍳3 11 | )ORIGIN 0 12 | ⍳3 13 | )ORIGIN 1 14 | ⍳0 15 | 6-⍳6 16 | 2×⍳0 17 | 2×⍳6 18 | X,X←2 3 5 7 11 19 | ,2 3⍴⍳6 20 | ⍴1 21 | ⍴⍴1 22 | ⍴⍴⍴1 23 | ⍴1 2 3 24 | ⍴⍴1 2 3 25 | ⍴⍴⍴1 2 3 26 | ⍴2 3⍴⍳6 27 | ⍴⍴2 3⍴⍳6 28 | ⍴⍴⍴2 3⍴⍳6 29 | ⍴4 2 3⍴⍳6 30 | ⍴⍴4 2 3⍴⍳6 31 | ⍴⍴⍴4 2 3⍴⍳6 32 | 2 3⍴1 2 33 | ⍴'' 34 | '' 35 | 'X' 36 | 'CAN''T' 37 | A←'ABCDEFG' 38 | M←4 3⍴3 1 4 2 1 4 4 1 2 4 1 4 39 | A[M] 40 | (3 4⍴⍳12)[2;3] 41 | (M←3 4⍴⍳12)[1 3;2 3 4] 42 | M[2;] 43 | M[;2 1] 44 | M[M←4 3⍴3 1 4 2 1 4 4 1 2 4 1 4;] 45 | 3 3⍴1 0 0 0 46 | 3⍴1 47 | 2 3⍴⍳6 48 | 2 3 4⍴⍳6 49 | (1-2) 50 | (⍳3)+4 51 | X←⍳6 52 | X[2 1]←8 9 53 | X[2 1 3]←7 54 | 3 4⍳4 55 | 1 2 3 4⍳2 3⍴⍳6 56 | 'ABCDEFGH'⍳'GAFFE' 57 | 3 4 7∊⍳5 58 | 4∊⍳5 59 | 2 3 5 7∊⍳4 60 | (3 4⍴⍳12)∊2 3 5 7 61 | 0↑3 4 5 6 62 | 2↑3 4 5 6 63 | 2 3↑3 4⍴⍳12 64 | 2 ¯3↑3 4⍴⍳12 65 | 2↓3 4 5 6 66 | 1 2↓3 4⍴⍳12 67 | ¯1 1↓3 4⍴⍳12 68 | ⍋3 5 3 2 69 | )ORIGIN 0 70 | ⍋3 5 3 2 71 | )ORIGIN 1 72 | ⍒3 5 3 2 73 | P←2 3 5 7 74 | E←3 4⍴⍳12 75 | 1 0 1 0/P 76 | 1 0 1 0/E 77 | 1 0 1/[1]E 78 | 1 0 1⌿E 79 | 0 1 0 1/⍳4 80 | 1 0 1\⍳2 81 | 1 0 1 1 1\3 4⍴'ABCDEFGHIJKL' 82 | 1 0 1⍀2 3⍴⍳6 83 | ⍉2 3⍴⍳6 84 | 1 1⍉3 3⍴⍳9 85 | ⍴⍴(⍳0)⍉5 86 | ⌽X←3 4⍴'ABCDEFGHIJKL' 87 | ⌽X 88 | ⌽[1]3 4⍴'ABCDEFGHIJKL' 89 | ⊖X 90 | 3⌽2 3 5 7 91 | ¯1⌽2 3 5 7 92 | ¯7⌽'ABCDEF' 93 | 1⊖3 4⍴⍳12 94 | 1 0 ¯1⌽3 4⍴'ABCDEFGHIJKL' 95 | (2 2⍴0 ¯1 3 1)⌽[2]2 4 2⍴⍳16 96 | (2 4⍴0 1 ¯1 0 0 3 2 1)⌽[1]2 2 4⍴⍳16 97 | 10⊥1 7 7 6 98 | 24 60 60⊥1 2 3 99 | 24 60 60⊤3723 100 | 60 60⊤3723 101 | 1+-3 102 | A←4 103 | A+2+A←4 104 | 3 4[2] 105 | +/1 2 3 106 | +/⍳3 107 | +/2 3⍴⍳6 108 | +/[1]2 3⍴⍳6 109 | +⌿2 3⍴⍳6 110 | +/[2]2 4 3⍴⍳24 111 | 2 3 4+.×5 6 7 112 | 1 2 3∘.×1 2 3 4 113 | (⍳3)∘.×⍳4 114 | -------------------------------------------------------------------------------- /tests/math.k: -------------------------------------------------------------------------------- 1 | / ../intro.md /: \: ': // \\ / \ 2 | 3 | qr:{K:!m:#*x;I:!n:#x;j:0;r:n#0a;turn:$[`Z~@*x;{(-x)@angle y};{x*1. -1@y>0}] 4 | while[j|/abs x-solve[A;b] 22 | 23 | A: \+0a0+(1 -2a90 3;5a90 3 2;2 3 1;4 -1 1); 24 | \0.0001>|/abs r-solve[A;dot[+A;r:1a30 2a30 3a30]] 25 | 26 | lu:{[A]i:0;k:!#A;P:!#A 27 | while[1<#k 28 | j:i+*&a=m:|/a:abs A[k;i] 29 | P[(i;j)]:P[(j;i)] 30 | A[(i;j)]:A[(j;i)] 31 | A[k:1_k;i]%:A[i;i] 32 | A[k;k]-:A[k;i]*\A[i;k] 33 | i+:1] 34 | (A;P)} 35 | 36 | lusolve:{[LUP;b];A:*LUP;P:*|LUP 37 | r:{[x;i;a]x[i]-:+/(a k)*x k:!i}/[b P;!n:#A;A] 38 | {[x;i;a]x[i]:(x[i]-+/(a k)*x k:(1+i)_!#x)%a[i]}/[r;|!n;|A]} 39 | 40 | A: \5^?25 41 | x: \?5 42 | b: \(+/*)\[A;x] 43 | \x-lusolve[lu A;b] 44 | 45 | fft: {[f;x]{[x;p;k;e]@[x;k,j;(x[k]+x[j]*e),x[k]-x[j:k+p]*e]}/[x f 0;f 1;f 2;f 3]} 46 | fftn:{[n]l:*&|2\n;e:angle[1;-(i:!n)*360.%n] 47 | (2/|2\i;2^/!l;|&'~!l#2;e@|{h!2*x}\!h:n%2)} 48 | 49 | n:2^15 50 | f:fftn n 51 | r:fft[f;`z@?2*n] 52 | \`fft 53 | \#r 54 | \+/r 55 | 56 | avg:{(+/x)%0.+#x} 57 | var:{(+/x*x:(x-avg x))%-1+#x} 58 | std:{%var x} 59 | med: **|2^^ 60 | 61 | 62 | -------------------------------------------------------------------------------- /tests/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | mkdir -p out 5 | mkdir -p log 6 | 7 | kk=/c/k/ktye.github.io/kweb/k.k 8 | 9 | oldk math.k -e >out/math 2>log/math 10 | oldk 66 ../apl/apl.k -e >out/apl 2>log/apl 11 | oldk token.k -e >out/token 2>log/token 12 | oldk $kk halfkey.k mm.k -e >out/halfkey 2>log/halfkey 13 | oldk $kk ../web/kweb/compile.k -e 'x:`out/compile 2>log/compile 14 | oldk $kk ../x/go.k -e 'x:`out/go 2>log/go 15 | oldk $kk ../x/wa.k -e 'x:`out/wa 2>log/wa 16 | oldk $kk ../x/wb.k -e 'x:`out/wb 2>log/wb 17 | oldk $kk ../x/cc.k -e 'x:`out/cc 2>log/cc 18 | oldk $kk ../x/js.k -e 'x:`out/js 2>log/js 19 | oldk $kk ../x/qb.k -e 'x:`out/qb 2>log/qb 20 | oldk $kk ../kos/softfloat.k ../x/cc.k -e 'x:`out/softfloat 2>log/softfloat 21 | oldk q.k qq.k -e >out/q 2>log/q 22 | oldk t.k -e >out/t 2>log/t 23 | 24 | -------------------------------------------------------------------------------- /tests/mm.k: -------------------------------------------------------------------------------- 1 | /halfkey example 2 | 3 | A:`p"MM:{[a;w]Yk;yF;xF;f;rF;Rk /matmul 4 | Y:+w /transpose w 5 | l:an;m:wn;n:Yn /dimensions x:l*m y:m*n r:l*n 6 | R:l^0.+&Ki l*n /allocate result 7 | N[l;x:a i;r:R i;Nj[n;y:Y j;f:0.;Nk[m;f+:(x k)*y k];r[j]:f]]; 8 | x:rx x;y:rx y;r:rx r;R} /fix refcounts" 9 | 10 | codes:A 1+i:4*!(#(A:0,A))%4 11 | names:A 2+i 12 | 13 | \simple[] 14 | 15 | -------------------------------------------------------------------------------- /tests/qq.k: -------------------------------------------------------------------------------- 1 | \H /(0.707106 0.707106;0.707106 -0.707106) 2 | \K0 /(,`ket)!,1. 0. 3 | \K1 /(,`ket)!,0. 1. 4 | \ts[K0;K0] /(,`ket)!,1. 0. 0. 0. 5 | \ts[K1;K1] /(,`ket)!,0. 0. 0. 1. 6 | \ts[K0;K1] /(,`ket)!,0. 1. 0. 0. 7 | \apply[SWAP;ts[K0;K1]] /(,`ket)!,0. 0. 1. 0. 8 | \apply[X;K0] /(,`ket)!,0. 1. 9 | \_apply[SX;apply[SX;K0]] /(,`ket)!,0. 1. 10 | -------------------------------------------------------------------------------- /tests/token.k: -------------------------------------------------------------------------------- 1 | / ../web/kweb/tok.html 2 | 3 | S:" \t" /space 4 | L:"({[;\n" /left 5 | R:")}]" /right 6 | A:"'/\\" /adverb 7 | V:"+-*%&|<>=^!~,#_$?@." /verb 8 | 9 | 10 | t:"+'a0q`-:e\\/n 11 | ;;)+'a0q`-+a'/; 12 | );)+'a0q`++a''; 13 | +;)+'a0q`-+a''; 14 | ';)+'a0q`-:a''; 15 | a;)+'bbq`++b''; 16 | 0;)+'11q`+:e''; 17 | qrrrrrrtrrrrsrr 18 | `;)+'b0q`++b''; 19 | -;)+'a1q`-+a''; 20 | /ccccccccccccc; 21 | cccccccccccccc; 22 | :;)+'a0q`-+a''; 23 | b;)+'bbq`++b''; 24 | 1;)+'11q`++e''; 25 | e;)+'11q`1+1''; 26 | rrrrrrrtrrrrsrr 27 | srrrrrrrrrrrrrr 28 | t;)+'a0q`++a'';" 29 | 30 | 31 | C:(S,L;R;V;A;".",_,/65 97+\:!26 32 | "0123456789"),"\"`-:e\\/\n" 33 | C:@[;;]/[-1+&256;0+C;!#C]@0+ 34 | 35 | q:*'1_"\n"\:t 36 | T:t[;0]?/:1_'t:1_'(&"\n"=t)^t 37 | 38 | w:{q@0{T[x;y]}\C x} 39 | s:"-^"@10>q? 40 | 41 | 42 | 43 | in:"phi:(1+%5)%two:200e-2 /golden ratio" 44 | 45 | `<(q C in 46 | w in 47 | s w in),\:"\n" 48 | -------------------------------------------------------------------------------- /tests/z.k: -------------------------------------------------------------------------------- 1 | 2 | ``x`y`z`k`l`a`b`while`"rf."`"rz."`"uqs."`"uqf."`"gdt."`"lin."`"odo."`"grp." 3 | `"x.":{,/+"0123456789abcdef"@(x%16;16/x:256/256+x)} 4 | `"t.":`45 5 | `"p.":`46 6 | `"c.":(`46)[`c;] 7 | `"i.":(`46)[`i;] 8 | `"s.":(`46)[`s;] 9 | `"f.":(`46)[`f;] 10 | `"z.":(`46)[`z;] 11 | `"uqs.":{x@&~0~':x:^x} 12 | `"uqf.":{x@&(!#x)=x?x} 13 | `"gdt.":{[t;g](!#t)($[g;{x@>y x};{x@6 6 | # -alignment +16 bytes 7 | 8 | if [ "$1" = "clean" ]; then 9 | rm -f a.out _.c k.c out 10 | exit 0 11 | fi 12 | 13 | # wget github.com/ktye/i/releases/download/latest/k.c 14 | 15 | #new header (vector version) 16 | cp head _.c 17 | awk -f remove.awk k.c >> _.c 18 | cat tail >> _.c 19 | 20 | gcc -Wfatal-errors _.c -lm 21 | 22 | if [ "$1" = "t" ]; then 23 | ./a.out ../../k.t -e 24 | fi 25 | -------------------------------------------------------------------------------- /v/c/remove.awk: -------------------------------------------------------------------------------- 1 | BEGIN{x=-1} #skip header 2 | /^static void panic/{x=1;next} #last header line 3 | 4 | #remove these functions: 5 | /^static int32_t bucket\(.*{$/{x=0} 6 | 7 | /^static int32_t alloc\(.*{$/{x=0} 8 | /^static void mfree\(.*{$/{x=0} 9 | /^static uint64_t seq\(.*{$/{x=0} 10 | 11 | 12 | {if(x>0)print} 13 | /^}/{if(!x)x=1} #end of function 14 | -------------------------------------------------------------------------------- /v/c/tail: -------------------------------------------------------------------------------- 1 | static int32_t bucket(int32_t size){ return maxi(6,((int32_t)(32)-I32clz((31+size)))); } 2 | static int32_t alloc(int32_t n, int32_t s){ 3 | int32_t size,t,i,m,a,j8,u9; 4 | size=(n*s); 5 | t=bucket(size); 6 | if(((int64_t)(n)*(int64_t)(s))>2147483647ll){ 7 | trap(); 8 | } 9 | i=(4*t); 10 | m=(4*I32(128)); 11 | for(;(!I32(i));)if(i>=m){ 12 | m=(4*grow(i)); 13 | } else { 14 | i+=4; 15 | } 16 | a=I32(i); 17 | SetI32(i,I32(a)); 18 | { 19 | j8=(i-4); 20 | for(;(j8>=(4*t));j8=(j8-4)){ 21 | u9=(a+((int32_t)(1)<<(j8>>2))); 22 | SetI32(u9,I32(j8)); 23 | SetI32(j8,u9); 24 | } 25 | } 26 | if((a&31)){ 27 | trap(); 28 | } 29 | //printf("alloc %d*%d => %d\n",n,s,16+a); 30 | return 16+a; //align 31 | } 32 | static void mfree(int32_t x, int32_t bs){ 33 | int32_t t; 34 | x-=16; //align 35 | if((x&31)){ 36 | trap(); 37 | } 38 | t=(4*bs); 39 | SetI32(x,I32(t)); 40 | SetI32(t,x); 41 | } 42 | 43 | static int32_t pI(uint64_t x){return((int32_t)x)>>5;} 44 | static int32_t eI(uint64_t x){return(31+4*nn(x)+(int32_t)x)>>5;} 45 | static uint64_t seq(int32_t n){ 46 | I5 i={0,1,2,3,4,5,6,7}; 47 | uint64_t r; 48 | n=maxi(n,0); 49 | r=mk(It,n); 50 | int32_t p=pI(r); 51 | int32_t e=eI(r); 52 | while(p _.wat 4 | 5 | awk '/patch0/{exit 0}{print}' _.wat > k.wat 6 | cat v.wat >> k.wat 7 | awk 'BEGIN{x=0}/patch1/{x=1;getline;getline}{if(x)print}' _.wat >> k.wat 8 | 9 | /c/local/wabt/wat2wasm.exe k.wat -o v.wasm 10 | cp v.wasm /c/k/ktye.github.io/v.wasm 11 | 12 | rm _.wat 13 | rm k.wat 14 | rm v.wasm 15 | 16 | -------------------------------------------------------------------------------- /v/w/ftab: -------------------------------------------------------------------------------- 1 | todo: patch these to _a.go init() 2 | Functions(281, negI, negF, negF) 3 | Functions(284, absI, absF, nyi) 4 | Functions(287, ltC, eqC, gtC, ltI, eqI, gtI, ltI, eqI, gtI) 5 | Functions(296, ltcC, eqcC, gtcC, ltiI, eqiI, gtiI, ltiI, eqiI, gtiI) 6 | 7 | Functions(305, sqrF) 8 | Functions(306, addI, subI, mulI, nyi, minI, maxI) 9 | Functions(406, addiI, subiI, muliI, nyi, miniI, maxiI) 10 | -------------------------------------------------------------------------------- /v/w/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | 3 | if [ "$1" = "clean" ]; then 4 | rm -f _* 5 | exit 0 6 | fi 7 | 8 | 9 | ls ../../*.go|while read f; do 10 | g=`echo $f|sed 's,......,_,'` 11 | cp $f $g 12 | done 13 | 14 | #todo delete funcs 15 | #scalar.go: addz nm nc nd 16 | #verbs.go: seq 17 | 18 | set -x 19 | wg -small -nomain . > _.wat 20 | wat2wasm _.wat -o _.wasm 21 | 22 | 23 | 24 | 25 | #wg . > _.wat 26 | # 27 | #awk '/patch0/{exit 0}{print}' _.wat > k.wat 28 | #cat v.wat >> k.wat 29 | #awk 'BEGIN{x=0}/patch1/{x=1;getline;getline}{if(x)print}' _.wat >> k.wat 30 | # 31 | #/c/local/wabt/wat2wasm.exe k.wat -o v.wasm 32 | #cp v.wasm /c/k/ktye.github.io/v.wasm 33 | # 34 | #rm _.wat 35 | #rm k.wat 36 | #rm v.wasm 37 | 38 | -------------------------------------------------------------------------------- /x/flt.go: -------------------------------------------------------------------------------- 1 | //go:build ignore 2 | // +build ignore 3 | 4 | package main 5 | 6 | import ( 7 | "bytes" 8 | "fmt" 9 | "math" 10 | "os" 11 | "strings" 12 | ) 13 | 14 | // print dict for floating point literals that do not roundtrip 15 | // flt.out: 16 | // k k.k flt.k -e 17 | 18 | func rev(u uint64) (r uint64) { 19 | for i := uint64(0); i < 8; i++ { 20 | ui := (0xff & (u >> (8 * i))) 21 | j := 56 - 8*i 22 | r |= ui << j 23 | } 24 | return r 25 | } 26 | func xf(u uint64) float64 { 27 | return math.Float64frombits(rev(u)) 28 | } 29 | 30 | func main() { 31 | m := make(map[string]float64) 32 | /* 33 | var f float64 = 3.141592653589793 34 | u := math.Float64bits(f) 35 | fmt.Printf("%x %x\n", rev(u), u) 36 | fmt.Println(xf(0x399d52a246df913f)) 37 | */ 38 | b, e := os.ReadFile("flt.out") 39 | fatal(e) 40 | v := bytes.Split(b, []byte("\n")) 41 | for i := range v { 42 | var u uint64 43 | var f float64 44 | s := string(v[i]) 45 | if s == "" { 46 | continue 47 | } 48 | s = strings.TrimPrefix(s, "0x") 49 | if strings.HasSuffix(s, ".") { 50 | s += "0" 51 | } 52 | if n, e := fmt.Sscanf(strings.TrimPrefix(s, "0x"), "%x %f", &u, &f); n != 2 || e != nil { 53 | fmt.Println(string(v[i]), "=>", n, e) 54 | panic("scanf") 55 | } 56 | g := xf(u) 57 | //fmt.Printf("%s g=%v u=%x rev=%x\n", s, g, u, rev(u)) 58 | if f != g { 59 | s = "0x" + s[:16] 60 | m[s] = g 61 | } else { 62 | // fmt.Println(u, f, "roundtrip") 63 | } 64 | } 65 | var keys []string 66 | var vals []string 67 | for k, v := range m { 68 | keys = append(keys, k) 69 | vals = append(vals, fmt.Sprintf("\"%v\"", v)) 70 | } 71 | fmt.Println("(" + strings.Join(keys, ";") + ")!(" + strings.Join(vals, ";") + ")") 72 | } 73 | func fatal(e error) { 74 | if e != nil { 75 | panic(e) 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /x/flt.k: -------------------------------------------------------------------------------- 1 | /k k.k flt.k -e 2 | 3 | `"flt.out"<"\n"/{("0x",`x@C x+!8)," ",$*`f C x+!8}'I@&(`f=S)&`lit=T 4 | -------------------------------------------------------------------------------- /x/mk: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | #go install /c/k/wg/cmd/wg 5 | #wg -k .. > k.k 6 | 7 | if [ "$1" = "go" ]; then 8 | k k.k go.k -e 'x:` kg.go 9 | go fmt kg.go 10 | go build kg.go 11 | elif [ "$1" = "gotest" ]; then 12 | ./kg ../k.t -e 13 | elif [ "$1" = "flt" ]; then 14 | k k.k flt.k -e 15 | go run flt.go 16 | rm flt.out 17 | elif [ "$1" = "cc" ]; then 18 | k k.k cc.k -e 'x:` k.c 19 | gcc -Wall -Wfatal-errors -O3 -o kc k.c -lm 20 | #c:/local/zig/zig cc -Wall -Wno-parentheses-equality -fwrapv -ffast-math k.c -o kc.exe -lm 21 | elif [ "$1" = "ctest" ]; then 22 | ./kc ../k.t -e 23 | elif [ "$1" = "wa" ]; then 24 | k k.k wa.k -e 'x:` k.wa 25 | /c/local/wabt/wat2wasm k.wa 26 | elif [ "$1" = "wb" ]; then 27 | k k.k wb.k -e 'x:` k.wasm 28 | /c/local/wabt/wasm2wat k.wasm > k.wat 29 | elif [ "$1" = "js" ]; then 30 | k k.k js.k -e 'x:` k.js 31 | elif [ "$1" = "jstest" ]; then 32 | qjs --std k.js ../k.t -e 33 | elif [ "$1" = "libjs" ]; then 34 | k k.k js.k -e 'x:` kj.js 35 | mv kj.js /c/k/ktye.github.io/kj.js 36 | elif [ "$1" = "qb" ];then 37 | k k.k qb.k -e 'x:` k.q 38 | else 39 | exit 1 40 | fi 41 | 42 | -------------------------------------------------------------------------------- /x/tests/asn.c: -------------------------------------------------------------------------------- 1 | int32_t f(int32_t x){ 2 | int32_t r; 3 | r=(1+x); 4 | return r; 5 | } 6 | int32_t h(int32_t x){ 7 | int32_t r; 8 | f(x); 9 | r=f(x); 10 | return r; 11 | } 12 | -------------------------------------------------------------------------------- /x/tests/asn.go: -------------------------------------------------------------------------------- 1 | package asn 2 | 3 | func f(x int32) int32 { 4 | var r int32 5 | r = (1 + x) 6 | return r 7 | } 8 | func h(x int32) int32 { 9 | var r int32 10 | f(x) 11 | r = f(x) 12 | return r 13 | } 14 | -------------------------------------------------------------------------------- /x/tests/asn.js: -------------------------------------------------------------------------------- 1 | const f=function(x){ 2 | let r 3 | r=I(I(1)+x); 4 | return r; 5 | } 6 | const h=function(x){ 7 | let r 8 | f(x); 9 | r=f(x); 10 | return r; 11 | } 12 | -------------------------------------------------------------------------------- /x/tests/asn.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`res`loc`sym`ast`asn`add`lit`get`ret`get`fun`arg`sym`res`loc`sym`ast`drp`cal`get`asn`cal`get`ret`get 4 | P:0 0 1 2 1 1 5 1 7 8 9 9 7 12 0 14 15 14 14 18 14 20 21 22 20 24 25 20 27 5 | I:0 0 0N 0 0N 0N 0 0N 0 2 1 0N 0N 0N 0 0N 0 0N 0N 0 0N 0N 0N 0N 0 0N 0N 0N 0N 6 | S:`asn`f`i`x`i`i`r``r`i`i`x`i`r`h`i`x`i`i`r```f`x`r`f`x`i`r 7 | -------------------------------------------------------------------------------- /x/tests/asn.q: -------------------------------------------------------------------------------- 1 | function w $f(w %x){ 2 | @start 3 | %r =w add 1, %x 4 | ret %r 5 | } 6 | function w $h(w %x){ 7 | @start 8 | %.1 =w call $f(w %x) 9 | %r =w call $f(w %x) 10 | ret %r 11 | } 12 | -------------------------------------------------------------------------------- /x/tests/asn.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (param $x i32) (result i32) (local $r i32) 4 | i32.const 1 5 | local.get $x 6 | i32.add 7 | local.tee $r) 8 | (func $h (param $x i32) (result i32) (local $r i32) 9 | local.get $x 10 | call $f 11 | drop 12 | local.get $x 13 | call $f 14 | local.tee $r) 15 | ) 16 | -------------------------------------------------------------------------------- /x/tests/cal.c: -------------------------------------------------------------------------------- 1 | int32_t g(int32_t x, int32_t y){ 2 | return (x-y); 3 | } 4 | int32_t f(int32_t x){ 5 | return g(1,x); 6 | } 7 | -------------------------------------------------------------------------------- /x/tests/cal.go: -------------------------------------------------------------------------------- 1 | package cal 2 | 3 | func g(x int32, y int32) int32 { 4 | return (x - y) 5 | } 6 | func f(x int32) int32 { 7 | return g(1, x) 8 | } 9 | -------------------------------------------------------------------------------- /x/tests/cal.js: -------------------------------------------------------------------------------- 1 | const g=function(x,y){ 2 | return I(x-y); 3 | } 4 | const f=function(x){ 5 | return g(I(1),x); 6 | } 7 | -------------------------------------------------------------------------------- /x/tests/cal.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`arg`sym`res`ast`ret`sub`get`get`fun`arg`sym`res`ast`ret`cal`lit`get 4 | P:0 0 1 2 1 4 1 1 7 8 9 9 0 12 13 12 12 16 17 18 18 5 | I:0 0 0N 0 0N 0 0N 0N 0N 2 0N 0N 0 0N 0 0N 0N 0N 0N 1 0N 6 | S:`cal`g`i`x`i`y`i``i`i`x`y`f`i`x`i``i`g`i`x 7 | -------------------------------------------------------------------------------- /x/tests/cal.q: -------------------------------------------------------------------------------- 1 | function w $g(w %x, w %y){ 2 | @start 3 | %.1 =w sub %x, %y 4 | ret %.1 5 | } 6 | function w $f(w %x){ 7 | @start 8 | %.1 =w call $g(w 1, w %x) 9 | ret %.1 10 | } 11 | -------------------------------------------------------------------------------- /x/tests/cal.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $g (param $x i32) (param $y i32) (result i32) 4 | local.get $x 5 | local.get $y 6 | i32.sub) 7 | (func $f (param $x i32) (result i32) 8 | i32.const 1 9 | local.get $x 10 | call $g) 11 | ) 12 | -------------------------------------------------------------------------------- /x/tests/cast.c: -------------------------------------------------------------------------------- 1 | int64_t f(int32_t x){ 2 | return (int64_t)(x); 3 | } 4 | int32_t g(int64_t x){ 5 | return (int32_t)(x); 6 | } 7 | double h(int32_t x){ 8 | return (double)(x); 9 | } 10 | int32_t i(double x){ 11 | return (int32_t)(x); 12 | } 13 | uint32_t j(int32_t x){ 14 | return (uint32_t)(x); 15 | } 16 | int32_t k(uint32_t x){ 17 | return (int32_t)(x); 18 | } 19 | -------------------------------------------------------------------------------- /x/tests/cast.go: -------------------------------------------------------------------------------- 1 | package cast 2 | 3 | func f(x int32) int64 { 4 | return int64(x) 5 | } 6 | func g(x int64) int32 { 7 | return int32(x) 8 | } 9 | func h(x int32) float64 { 10 | return float64(x) 11 | } 12 | func i(x float64) int32 { 13 | return int32(x) 14 | } 15 | func j(x int32) uint32 { 16 | return uint32(x) 17 | } 18 | func k(x uint32) int32 { 19 | return int32(x) 20 | } 21 | -------------------------------------------------------------------------------- /x/tests/cast.js: -------------------------------------------------------------------------------- 1 | const f=function(x){ 2 | return _JI(x); 3 | } 4 | const g=function(x){ 5 | return _IJ(x); 6 | } 7 | const h=function(x){ 8 | return _FI(x); 9 | } 10 | const i=function(x){ 11 | return _IF(x); 12 | } 13 | const j=function(x){ 14 | return _UI(x); 15 | } 16 | const k=function(x){ 17 | return _IU(x); 18 | } 19 | -------------------------------------------------------------------------------- /x/tests/cast.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`res`ast`ret`cst`typ`get`fun`arg`sym`res`ast`ret`cst`typ`get`fun`arg`sym`res`ast`ret`cst`typ`get`fun`arg`sym`res`ast`ret`cst`typ`get`fun`arg`sym`res`ast`ret`cst`typ`get`fun`arg`sym`res`ast`ret`cst`typ`get 4 | P:0 0 1 2 1 1 5 6 7 7 0 10 11 10 10 14 15 16 16 0 19 20 19 19 23 24 25 25 0 28 29 28 28 32 33 34 34 0 37 38 37 37 41 42 43 43 0 46 47 46 46 50 51 52 52 5 | I:0 0 0N 0 0N 0N 0N 0N 0N 0N 0 0N 0 0N 0N 0N 0N 0N 0N 0 0N 0 0N 0N 0N 0N 0N 0N 0 0N 0 0N 0N 0N 0N 0N 0N 0 0N 0 0N 0N 0N 0N 0N 0N 0 0N 0 0N 0N 0N 0N 0N 0N 6 | S:`cast`f`i`x`j``j`j`i`x`g`j`x`i``i`i`j`x`h`i`x`f``f`f`i`x`i`f`x`i``i`i`f`x`j`i`x`u``u`u`i`x`k`u`x`i``i`i`u`x 7 | -------------------------------------------------------------------------------- /x/tests/cast.q: -------------------------------------------------------------------------------- 1 | function l $f(w %x){ 2 | @start 3 | %.1 =l extsw %x 4 | ret %.1 5 | } 6 | function w $g(l %x){ 7 | @start 8 | ret %x 9 | } 10 | function d $h(w %x){ 11 | @start 12 | %.1 =l extsw %x 13 | %.2 =d sltof %.1 14 | ret %.2 15 | } 16 | function w $i(d %x){ 17 | @start 18 | %.1 =w dtosi %x 19 | ret %.1 20 | } 21 | function w $j(w %x){ 22 | @start 23 | ret %x 24 | } 25 | function w $k(w %x){ 26 | @start 27 | ret %x 28 | } 29 | -------------------------------------------------------------------------------- /x/tests/cast.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (param $x i32) (result i64) 4 | local.get $x 5 | i64.extend_i32_s) 6 | (func $g (param $x i64) (result i32) 7 | local.get $x 8 | i32.wrap_i64) 9 | (func $h (param $x i32) (result f64) 10 | local.get $x 11 | f64.convert_i32_s) 12 | (func $i (param $x f64) (result i32) 13 | local.get $x 14 | i32.trunc_f64_s) 15 | (func $j (param $x i32) (result i32) 16 | local.get $x) 17 | (func $k (param $x i32) (result i32) 18 | local.get $x) 19 | ) 20 | -------------------------------------------------------------------------------- /x/tests/cli.c: -------------------------------------------------------------------------------- 1 | int32_t f(int32_t x, int32_t y){ 2 | return ((int32_t(*)(int32_t ))_F[x])(y); 3 | } 4 | -------------------------------------------------------------------------------- /x/tests/cli.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import . "github.com/ktye/wg/module" 4 | 5 | func f(x int32, y int32) int32 { 6 | return Func[x].(func(int32) int32)(y) 7 | } 8 | -------------------------------------------------------------------------------- /x/tests/cli.js: -------------------------------------------------------------------------------- 1 | const f=function(x,y){ 2 | return I(_F[x](y)); 3 | } 4 | -------------------------------------------------------------------------------- /x/tests/cli.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`arg`sym`res`ast`ret`cli`get`get`arg 4 | P:0 0 1 2 1 4 1 1 7 8 9 9 9 5 | I:0 0 0N 0 0N 0 0N 0N 0N 1 0N 0N 0N 6 | S:`cli`f`i`x`i`y`i``i`i`x`y`i 7 | -------------------------------------------------------------------------------- /x/tests/cli.q: -------------------------------------------------------------------------------- 1 | function w $f(w %x, w %y){ 2 | @start 3 | %.1 =l mul 8, %x 4 | %.2 =l add %_F, %.1 5 | %.3 =l loadl %.2 6 | %.4 =w call %.3(w %y) 7 | ret %.4 8 | } 9 | -------------------------------------------------------------------------------- /x/tests/cli.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (param $x i32) (param $y i32) (result i32) 4 | local.get $y 5 | local.get $x 6 | call_indirect (param i32) (result i32)) 7 | ) 8 | -------------------------------------------------------------------------------- /x/tests/cnd.c: -------------------------------------------------------------------------------- 1 | int32_t f(int32_t x){ 2 | if(x>1){ 3 | return x; 4 | } 5 | return (x>3)?(x):(-x); 6 | } 7 | -------------------------------------------------------------------------------- /x/tests/cnd.go: -------------------------------------------------------------------------------- 1 | package cnd 2 | 3 | func f(x int32) int32 { 4 | if x > 1 { 5 | return x 6 | } 7 | if x > 3 { 8 | return x 9 | } else { 10 | return -x 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /x/tests/cnd.js: -------------------------------------------------------------------------------- 1 | const f=function(x){ 2 | if(x>I(1)){ 3 | return x; 4 | } 5 | return (x>I(3))?(x):(I(-x)); 6 | } 7 | -------------------------------------------------------------------------------- /x/tests/cnd.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`res`ast`cnd`mor`get`lit`ret`get`ret`cnd`mor`get`lit`get`neg`get 4 | P:0 0 1 2 1 1 5 6 7 7 6 10 5 12 13 14 14 13 13 18 5 | I:0 0 0N 0 0N 0N 0N 2 0N 1 0N 0N 0N 0N 2 0N 3 0N 1 0N 6 | S:`cnd`f`i`x`i```i`x`i`i`x`i`i`i`x`i`x`i`x 7 | -------------------------------------------------------------------------------- /x/tests/cnd.q: -------------------------------------------------------------------------------- 1 | function w $f(w %x){ 2 | @start 3 | %.3 =w sgt %x, 1 4 | jnz %.3, @i2, @e2 5 | @i2 6 | ret %x 7 | @e2 8 | %.2 =w sgt %x, 3 9 | jnz %.2, @i1, @e1 10 | @i1 11 | ret %x 12 | jmp @f1 13 | @e1 14 | %.1 =w sub 0, %x 15 | ret %.1 16 | @f1 17 | } 18 | -------------------------------------------------------------------------------- /x/tests/cnd.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (param $x i32) (result i32) 4 | local.get $x 5 | i32.const 1 6 | i32.gt_s 7 | if 8 | local.get $x 9 | return 10 | end 11 | local.get $x 12 | i32.const 3 13 | i32.gt_s 14 | if (result i32) 15 | local.get $x 16 | else 17 | i32.const 0 18 | local.get $x 19 | i32.sub 20 | end) 21 | ) 22 | -------------------------------------------------------------------------------- /x/tests/cnd2.c: -------------------------------------------------------------------------------- 1 | int32_t f(int32_t x){ 2 | if(x>3){ 3 | return x; 4 | } else { 5 | x=(2*x); 6 | return -x; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /x/tests/cnd2.go: -------------------------------------------------------------------------------- 1 | package cnd2 2 | 3 | func f(x int32) int32 { 4 | if x > 3 { 5 | return x 6 | } else { 7 | x = (2 * x) 8 | return -x 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /x/tests/cnd2.js: -------------------------------------------------------------------------------- 1 | const f=function(x){ 2 | if(x>I(3)){ 3 | return x; 4 | } else { 5 | x=I(I(2)*x); 6 | return I(-x); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /x/tests/cnd2.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`res`ast`ret`cnd`mor`get`lit`get`stm`asn`mul`lit`get`neg`get 4 | P:0 0 1 2 1 1 5 6 7 8 8 7 7 12 13 14 14 12 17 5 | I:0 0 0N 0 0N 0N 0N 0N 2 0N 3 0N 0N 0 2 2 0N 1 0N 6 | S:`cnd2`f`i`x`i``i`i`i`x`i`x``x`i`i`x`i`x 7 | -------------------------------------------------------------------------------- /x/tests/cnd2.q: -------------------------------------------------------------------------------- 1 | function w $f(w %x){ 2 | @start 3 | %.2 =w sgt %x, 3 4 | jnz %.2, @i1, @e1 5 | @i1 6 | ret %x 7 | jmp @f1 8 | @e1 9 | %x =w mul 2, %x 10 | %.1 =w sub 0, %x 11 | ret %.1 12 | @f1 13 | } 14 | -------------------------------------------------------------------------------- /x/tests/cnd2.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (param $x i32) (result i32) 4 | local.get $x 5 | i32.const 3 6 | i32.gt_s 7 | if (result i32) 8 | local.get $x 9 | else 10 | i32.const 2 11 | local.get $x 12 | i32.mul 13 | local.set $x 14 | i32.const 0 15 | local.get $x 16 | i32.sub 17 | end) 18 | ) 19 | -------------------------------------------------------------------------------- /x/tests/const.c: -------------------------------------------------------------------------------- 1 | const double F=1.23; 2 | const double p=3.141592653589793; 3 | const int32_t I=-4; 4 | const uint32_t U=4; 5 | const uint32_t u2=0xfffffffe; 6 | const int64_t J=-5ll; 7 | const uint64_t K=0xfedcba0123456789ull; 8 | -------------------------------------------------------------------------------- /x/tests/const.go: -------------------------------------------------------------------------------- 1 | package constants 2 | 3 | const F float64 = 1.23 4 | const p float64 = 3.141592653589793 5 | const I int32 = -4 6 | const U uint32 = 4 7 | const u2 uint32 = 0xfffffffe 8 | const J int64 = -5 9 | const K uint64 = 0xfedcba0123456789 10 | -------------------------------------------------------------------------------- /x/tests/const.js: -------------------------------------------------------------------------------- 1 | const F=F(1.23); 2 | const p=F(3.141592653589793); 3 | const I=I(-4); 4 | const U=U(4n); 5 | const u2=U(-2n); 6 | const J=J(-5n); 7 | const K=K(BigInt("0xfedcba0123456789")); 8 | -------------------------------------------------------------------------------- /x/tests/const.k: -------------------------------------------------------------------------------- 1 | C:0xae47e17a14aef33f182d4454fb210940fbffffffffffffff8967452301badcfe 2 | D:0x 3 | T:`prg`con`lit`con`lit`con`lit`con`lit`con`lit`con`lit`con`lit 4 | P:0 0 1 0 3 0 5 0 7 0 9 0 11 0 13 5 | I:0 0N 0 0N 8 0N -4 0N 4 0N 4294967294 0N 16 0N 24 6 | S:`constants`F`f`p`f`I`i`U`u`u2`u`J`j`K`k 7 | -------------------------------------------------------------------------------- /x/tests/const.q: -------------------------------------------------------------------------------- 1 | data $F = { d 4608218246714312622 } 2 | data $p = { d 4614256656552045848 } 3 | data $I = { w -4 } 4 | data $U = { w 4 } 5 | data $u2 = { w -2 } 6 | data $J = { l 18446744073709551611 } 7 | data $K = { l 18364757894559524745 } 8 | -------------------------------------------------------------------------------- /x/tests/const.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (global $F f64 (f64.const 1.23)) 4 | (global $p f64 (f64.const 3.141592653589793)) 5 | (global $I i32 (i32.const -4)) 6 | (global $U i32 (i32.const 4)) 7 | (global $u2 i32 (i32.const 0xfffffffe)) 8 | (global $J i64 (i64.const -5)) 9 | (global $K i64 (i64.const 0xfedcba0123456789)) 10 | ) 11 | -------------------------------------------------------------------------------- /x/tests/cont.c: -------------------------------------------------------------------------------- 1 | void f(){ 2 | int32_t i; 3 | i=(int32_t)(0); 4 | for(;;){ 5 | i++; 6 | if(i<2){ 7 | continue; 8 | } 9 | i=(i*2); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /x/tests/cont.go: -------------------------------------------------------------------------------- 1 | package cont 2 | 3 | func f() { 4 | var i int32 5 | i = int32(0) 6 | for { 7 | i = (i + 1) 8 | if i < 2 { 9 | continue 10 | } 11 | i = (i * 2) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /x/tests/cont.js: -------------------------------------------------------------------------------- 1 | const f=function(){ 2 | let i 3 | i=I(0); 4 | for(;;){ 5 | i++; 6 | if(i0)?(1):(2); 3 | } 4 | -------------------------------------------------------------------------------- /x/tests/ifret.go: -------------------------------------------------------------------------------- 1 | package ifret 2 | 3 | func f(x int32) int32 { 4 | if x > 0 { 5 | return 1 6 | } else { 7 | return 2 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /x/tests/ifret.js: -------------------------------------------------------------------------------- 1 | const f=function(x){ 2 | return (x>I(0))?(I(1)):(I(2)); 3 | } 4 | -------------------------------------------------------------------------------- /x/tests/ifret.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`res`ast`ret`cnd`mor`get`lit`lit`lit 4 | P:0 0 1 2 1 1 5 6 7 8 8 7 7 5 | I:0 0 0N 0 0N 0N 0N 0N 2 0N 0 1 2 6 | S:`ifret`f`i`x`i``i`i`i`x`i`i`i 7 | -------------------------------------------------------------------------------- /x/tests/ifret.q: -------------------------------------------------------------------------------- 1 | function w $f(w %x){ 2 | @start 3 | %.1 =w sgt %x, 0 4 | jnz %.1, @i1, @e1 5 | @i1 6 | ret 1 7 | jmp @f1 8 | @e1 9 | ret 2 10 | @f1 11 | } 12 | -------------------------------------------------------------------------------- /x/tests/ifret.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (param $x i32) (result i32) 4 | local.get $x 5 | i32.const 0 6 | i32.gt_s 7 | if (result i32) 8 | i32.const 1 9 | else 10 | i32.const 2 11 | end) 12 | ) 13 | -------------------------------------------------------------------------------- /x/tests/inc.c: -------------------------------------------------------------------------------- 1 | int32_t f(int32_t x){ 2 | x++; 3 | return x; 4 | } 5 | -------------------------------------------------------------------------------- /x/tests/inc.go: -------------------------------------------------------------------------------- 1 | package asn 2 | 3 | func f(x int32) int32 { 4 | x = (x + 1) 5 | return x 6 | } 7 | -------------------------------------------------------------------------------- /x/tests/inc.js: -------------------------------------------------------------------------------- 1 | const f=function(x){ 2 | x++; 3 | return x; 4 | } 5 | -------------------------------------------------------------------------------- /x/tests/inc.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`res`ast`asn`add`get`lit`ret`get 4 | P:0 0 1 2 1 1 5 6 7 7 5 10 5 | I:0 0 0N 0 0N 0N 0 2 0N 1 0N 0N 6 | S:`asn`f`i`x`i``x`i`x`i`i`x 7 | -------------------------------------------------------------------------------- /x/tests/inc.q: -------------------------------------------------------------------------------- 1 | function w $f(w %x){ 2 | @start 3 | %x =w add %x, 1 4 | ret %x 5 | } 6 | -------------------------------------------------------------------------------- /x/tests/inc.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (param $x i32) (result i32) 4 | local.get $x 5 | i32.const 1 6 | i32.add 7 | local.tee $x) 8 | ) 9 | -------------------------------------------------------------------------------- /x/tests/kitest.sh: -------------------------------------------------------------------------------- 1 | # kitest asn 2 | # kitest cal.. 3 | p="$1" 4 | k ../ki.k -e 'x:kixx@<`"'$p'.i"' > out1 5 | k ${p}.k -e 'x:`<"\n"/:(`lxy 40 100)@+`T`P`I`S!(T;P;I;S)' > out2 6 | 7 | vimdiff out1 out2 8 | -------------------------------------------------------------------------------- /x/tests/label.c: -------------------------------------------------------------------------------- 1 | void f(){ 2 | int32_t i; 3 | i=0; 4 | for(;;){ 5 | i++; 6 | if(i>3){ 7 | break; 8 | } 9 | } 10 | } 11 | void g(){ 12 | int32_t i; 13 | i=0; 14 | for(;;){ 15 | i++; 16 | if(i>3){ 17 | break; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /x/tests/label.go: -------------------------------------------------------------------------------- 1 | package label 2 | 3 | func f() { 4 | var i int32 5 | i = 0 6 | L: 7 | for { 8 | i = (i + 1) 9 | if i > 3 { 10 | break L 11 | } 12 | } 13 | } 14 | func g() { 15 | var i int32 16 | i = 0 17 | for { 18 | i = (i + 1) 19 | if i > 3 { 20 | break 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /x/tests/label.js: -------------------------------------------------------------------------------- 1 | const f=function(){ 2 | let i 3 | i=I(0); 4 | for(;;){ 5 | i++; 6 | if(i>I(3)){ 7 | break; 8 | } 9 | } 10 | } 11 | const g=function(){ 12 | let i 13 | i=I(0); 14 | for(;;){ 15 | i++; 16 | if(i>I(3)){ 17 | break; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /x/tests/label.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`loc`sym`ast`asn`lit`for`nop`nop`stm`asn`add`get`lit`cnd`mor`get`lit`jmp`fun`loc`sym`ast`asn`lit`for`nop`nop`stm`asn`add`get`lit`cnd`mor`get`lit`jmp 4 | P:0 0 1 2 1 4 5 4 7 7 7 10 11 12 12 10 15 16 16 15 0 20 21 20 23 24 23 26 26 26 29 30 31 31 29 34 35 35 34 5 | I:0 0 0N 0 0N 0 0 0 0N 0N 0N 0 2 0N 1 0N 2 0N 3 1 0 0N 0 0N 0 0 0 0N 0N 0N 0 2 0N 1 0N 2 0N 3 1 6 | S:`label`f`i`i``i`i`L````i`i`i`i``i`i`i`L`g`i`i``i`i`````i`i`i`i``i`i`i` 7 | -------------------------------------------------------------------------------- /x/tests/label.q: -------------------------------------------------------------------------------- 1 | function $f(){ 2 | @start 3 | %i =w copy 0 4 | @L 5 | %i =w add %i, 1 6 | %.1 =w sgt %i, 3 7 | jnz %.1, @i1, @e1 8 | @i1 9 | jmp @Le 10 | @e1 11 | jmp @L 12 | @Le 13 | } 14 | function $g(){ 15 | @start 16 | %i =w copy 0 17 | @f26 18 | %i =w add %i, 1 19 | %.1 =w sgt %i, 3 20 | jnz %.1, @i1, @e1 21 | @i1 22 | jmp @f26e 23 | @e1 24 | jmp @f26 25 | @f26e 26 | } 27 | -------------------------------------------------------------------------------- /x/tests/label.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (local $i i32) 4 | i32.const 0 5 | local.set $i 6 | block $L1 7 | loop $L0 8 | local.get $i 9 | i32.const 1 10 | i32.add 11 | local.tee $i 12 | i32.const 3 13 | i32.gt_s 14 | if 15 | br $L1 16 | end 17 | br 0 18 | end 19 | end) 20 | (func $g (local $i i32) 21 | i32.const 0 22 | local.set $i 23 | block 24 | loop 25 | local.get $i 26 | i32.const 1 27 | i32.add 28 | local.tee $i 29 | i32.const 3 30 | i32.gt_s 31 | if 32 | br 2 33 | end 34 | br 0 35 | end 36 | end) 37 | ) 38 | -------------------------------------------------------------------------------- /x/tests/lit.c: -------------------------------------------------------------------------------- 1 | double F = 1.23; 2 | double p = 3.141592653589793; 3 | int32_t I = -4; 4 | uint32_t U = 4; 5 | uint32_t u2 = 0xfffffffe; 6 | int64_t J = -5ll; 7 | uint64_t K = 0xfedcba0123456789ull; 8 | -------------------------------------------------------------------------------- /x/tests/lit.go: -------------------------------------------------------------------------------- 1 | package lit 2 | 3 | var F float64 = 1.23 4 | var p float64 = 3.141592653589793 5 | var I int32 = -4 6 | var U uint32 = 4 7 | var u2 uint32 = 0xfffffffe 8 | var J int64 = -5 9 | var K uint64 = 0xfedcba0123456789 10 | -------------------------------------------------------------------------------- /x/tests/lit.js: -------------------------------------------------------------------------------- 1 | let F = F(1.23); 2 | let p = F(3.141592653589793); 3 | let I = I(-4); 4 | let U = U(4n); 5 | let u2 = U(-2n); 6 | let J = J(-5n); 7 | let K = K(BigInt("0xfedcba0123456789")); 8 | -------------------------------------------------------------------------------- /x/tests/lit.k: -------------------------------------------------------------------------------- 1 | C:0xae47e17a14aef33f182d4454fb210940fbffffffffffffff8967452301badcfe 2 | D:0x 3 | T:`prg`var`lit`var`lit`var`lit`var`lit`var`lit`var`lit`var`lit 4 | P:0 0 1 0 3 0 5 0 7 0 9 0 11 0 13 5 | I:0 0N 0 0N 8 0N -4 0N 4 0N 4294967294 0N 16 0N 24 6 | S:`lit`F`f`p`f`I`i`U`u`u2`u`J`j`K`k 7 | -------------------------------------------------------------------------------- /x/tests/lit.q: -------------------------------------------------------------------------------- 1 | data $F = { d 4608218246714312622 } 2 | data $p = { d 4614256656552045848 } 3 | data $I = { w -4 } 4 | data $U = { w 4 } 5 | data $u2 = { w -2 } 6 | data $J = { l 18446744073709551611 } 7 | data $K = { l 18364757894559524745 } 8 | -------------------------------------------------------------------------------- /x/tests/lit.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (global $F (mut f64) (f64.const 1.23)) 4 | (global $p (mut f64) (f64.const 3.141592653589793)) 5 | (global $I (mut i32) (i32.const -4)) 6 | (global $U (mut i32) (i32.const 4)) 7 | (global $u2 (mut i32) (i32.const 0xfffffffe)) 8 | (global $J (mut i64) (i64.const -5)) 9 | (global $K (mut i64) (i64.const 0xfedcba0123456789)) 10 | ) 11 | -------------------------------------------------------------------------------- /x/tests/loop.c: -------------------------------------------------------------------------------- 1 | void f(){ 2 | int32_t n,i; 3 | n=0; 4 | i=0; 5 | for(;(i<3);i++)n++; 6 | } 7 | -------------------------------------------------------------------------------- /x/tests/loop.go: -------------------------------------------------------------------------------- 1 | package loop 2 | 3 | func f() { 4 | var n, i int32 5 | n = 0 6 | i = 0 7 | for ; i < 3; i = (i + 1) { 8 | n = (n + 1) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /x/tests/loop.js: -------------------------------------------------------------------------------- 1 | const f=function(){ 2 | let n,i 3 | n=I(0); 4 | i=I(0); 5 | for(;(i $k 11 | done 12 | elif [ "$1" = go ]; then 13 | for x in *.k; do 14 | g=`echo $x | sed 's/.k$/.go/'` 15 | k $x ../go.k -e 'x:` out.go 16 | go fmt out.go 17 | grep -v '^import' $g > in.go 18 | go fmt in.go 19 | cmp out.go in.go 20 | rm out.go in.go 21 | done 22 | elif [ "$1" = wa ]; then 23 | for x in *.k; do 24 | w=`echo $x | sed 's/.k$/.wa/'` 25 | k $x ../wa.k -e 'x:` out 26 | cmp out $w 27 | rm out 28 | done 29 | elif [ "$1" = wb ]; then 30 | #cli.k heap.k 31 | for x in asn.k cal.k cast.k cnd.k cnd2.k const.k cont.k drp.k fun.k ifret.k inc.k label.k lit.k loop.k loop2.k mem.k swtch.k swtch2.k tab.k; do 32 | k $x ../wb.k -e 'x:` out 33 | /c/local/wabt/wasm2wat out 34 | # /c/local/wabt/wasm-objdump.exe -s -d -x out 35 | done 36 | elif [ "$1" = cc ]; then 37 | for x in *.k; do 38 | c=`echo $x | sed 's/.k$/.c/'` 39 | k $x ../cc.k -e 'x:` out 40 | cmp out $c 41 | rm out 42 | done 43 | elif [ "$1" = js ]; then 44 | for x in *.k; do 45 | j=`echo $x | sed 's/.k$/.js/'` 46 | k $x ../js.k -e 'x:` out 47 | cmp out $j 48 | rm out 49 | done 50 | elif [ "$1" = qb ]; then 51 | for x in *.k; do 52 | q=`echo $x|sed 's/.k$/.q/'` 53 | k $x ../qb.k -e 'x:` out 54 | cmp out $q 55 | rm out 56 | done 57 | elif [ "$1" = walidate ]; then 58 | for x in *.wa; do 59 | /c/local/wabt/wat2wasm $x 60 | done 61 | rm *.wasm 62 | else 63 | echo 1 64 | fi 65 | 66 | -------------------------------------------------------------------------------- /x/tests/swtch.c: -------------------------------------------------------------------------------- 1 | int32_t f(int32_t x){ 2 | switch(x){ 3 | case 0:return (1+x); 4 | break; 5 | default:return x; 6 | break; 7 | } 8 | } 9 | int32_t g(int32_t x){ 10 | switch(x){ 11 | case 0:return (1+x); 12 | break; 13 | case 1:return x; 14 | break; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /x/tests/swtch.go: -------------------------------------------------------------------------------- 1 | package swtch 2 | 3 | func f(x int32) int32 { 4 | switch x { 5 | case 0: 6 | return (1 + x) 7 | default: 8 | return x 9 | } 10 | } 11 | func g(x int32) int32 { 12 | switch x { 13 | case 0: 14 | return (1 + x) 15 | case 1: 16 | return x 17 | } 18 | return 0 19 | } 20 | -------------------------------------------------------------------------------- /x/tests/swtch.js: -------------------------------------------------------------------------------- 1 | const f=function(x){ 2 | switch(x){ 3 | case 0:return I(I(1)+x); 4 | break; 5 | default:return x; 6 | break; 7 | } 8 | } 9 | const g=function(x){ 10 | switch(x){ 11 | case 0:return I(I(1)+x); 12 | break; 13 | case 1:return x; 14 | break; 15 | } 16 | return I(0); 17 | } 18 | -------------------------------------------------------------------------------- /x/tests/swtch.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`res`ast`ret`swc`get`add`lit`get`get`fun`arg`sym`res`ast`swc`get`ret`add`lit`get`ret`get`ret`lit 4 | P:0 0 1 2 1 1 5 6 7 7 9 9 7 0 13 14 13 13 17 18 18 20 21 21 18 24 17 26 5 | I:0 0 0N 0 0N 0N 0N 1 0N 2 1 0N 0N 0 0N 0 0N 0N 0 0N 0N 2 1 0N 0N 0N 0N 0 6 | S:`swtch`f`i`x`i``i`i`x`i`i`x`x`g`i`x`i```x`i`i`i`x`i`x`i`i 7 | -------------------------------------------------------------------------------- /x/tests/swtch.q: -------------------------------------------------------------------------------- 1 | function w $f(w %x){ 2 | @start 3 | %.2 =w eq %x, 0 4 | jnz %.2, @i1, @e1 5 | @i1 6 | %.1 =w add 1, %x 7 | ret %.1 8 | jmp @f1 9 | @e1 10 | ret %x 11 | @f1 12 | } 13 | function w $g(w %x){ 14 | @start 15 | %.3 =w eq %x, 0 16 | jnz %.3, @i2, @e2 17 | @i2 18 | %.1 =w add 1, %x 19 | ret %.1 20 | jmp @f2 21 | @e2 22 | %.2 =w eq %x, 1 23 | jnz %.2, @i1, @e1 24 | @i1 25 | ret %x 26 | jmp @f1 27 | @e1 28 | @f1 29 | @f2 30 | ret 0 31 | } 32 | -------------------------------------------------------------------------------- /x/tests/swtch.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (param $x i32) (result i32) 4 | block (result i32) 5 | block 6 | block 7 | local.get $x 8 | br_table 0 1 9 | end 10 | i32.const 1 11 | local.get $x 12 | i32.add 13 | br 1 14 | end 15 | local.get $x 16 | br 0 17 | end) 18 | (func $g (param $x i32) (result i32) 19 | block 20 | block 21 | block 22 | block 23 | local.get $x 24 | br_table 0 1 2 25 | end 26 | i32.const 1 27 | local.get $x 28 | i32.add 29 | return 30 | br 2 31 | end 32 | local.get $x 33 | return 34 | br 1 35 | end 36 | br 0 37 | end 38 | i32.const 0) 39 | ) 40 | -------------------------------------------------------------------------------- /x/tests/swtch2.c: -------------------------------------------------------------------------------- 1 | int32_t f(int32_t x){ 2 | return x; 3 | } 4 | int32_t g(int32_t x){ 5 | int32_t r; 6 | switch(x){ 7 | case 0:r=f(x); 8 | break; 9 | default:{ 10 | r=(x>5)?((x-3)):((x-2)); 11 | } 12 | break; 13 | } 14 | return r; 15 | } 16 | -------------------------------------------------------------------------------- /x/tests/swtch2.go: -------------------------------------------------------------------------------- 1 | package swtch2 2 | 3 | func f(x int32) int32 { 4 | return x 5 | } 6 | func g(x int32) int32 { 7 | var r int32 8 | switch x { 9 | case 0: 10 | r = f(x) 11 | default: 12 | if x > 5 { 13 | r = (x - 3) 14 | } else { 15 | r = (x - 2) 16 | } 17 | r = r 18 | } 19 | return r 20 | } 21 | -------------------------------------------------------------------------------- /x/tests/swtch2.js: -------------------------------------------------------------------------------- 1 | const f=function(x){ 2 | return x; 3 | } 4 | const g=function(x){ 5 | let r 6 | switch(x){ 7 | case 0:r=f(x); 8 | break; 9 | default:{ 10 | r=(x>I(5))?(I(x-I(3))):(I(x-I(2))); 11 | } 12 | break; 13 | } 14 | return r; 15 | } 16 | -------------------------------------------------------------------------------- /x/tests/swtch2.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`fun`arg`sym`res`ast`ret`get`fun`arg`sym`res`loc`sym`ast`asn`swc`get`cal`get`stm`asn`cnd`mor`get`lit`sub`get`lit`sub`get`lit`get`ret`get 4 | P:0 0 1 2 1 1 5 6 0 8 9 8 8 12 8 14 15 16 16 18 16 20 21 22 23 23 22 26 26 22 29 29 20 14 33 5 | I:0 0 0N 0 0N 0N 0N 0N 0 0N 0 0N 0N 0 0N 0 1 0N 0N 0N 0N 0 0N 2 0N 5 2 0N 3 2 0N 2 0N 0N 0N 6 | S:`swtch2`f`i`x`i``i`x`g`i`x`i`i`r``r`i`x`f`x``r`i`i`x`i`i`x`i`i`x`i`r`i`r 7 | -------------------------------------------------------------------------------- /x/tests/swtch2.q: -------------------------------------------------------------------------------- 1 | function w $f(w %x){ 2 | @start 3 | ret %x 4 | } 5 | function w $g(w %x){ 6 | @start 7 | %.2 =w eq %x, 0 8 | jnz %.2, @i2, @e2 9 | @i2 10 | %r =w call $f(w %x) 11 | jmp @f2 12 | @e2 13 | %.1 =w sgt %x, 5 14 | jnz %.1, @i1, @e1 15 | @i1 16 | %r =w sub %x, 3 17 | jmp @f1 18 | @e1 19 | %r =w sub %x, 2 20 | @f1 21 | %r =w copy %r 22 | @f2 23 | ret %r 24 | } 25 | -------------------------------------------------------------------------------- /x/tests/swtch2.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f (param $x i32) (result i32) 4 | local.get $x) 5 | (func $g (param $x i32) (result i32) (local $r i32) 6 | block (result i32) 7 | block 8 | block 9 | local.get $x 10 | br_table 0 1 11 | end 12 | local.get $x 13 | call $f 14 | br 1 15 | end 16 | local.get $x 17 | i32.const 5 18 | i32.gt_s 19 | if (result i32) 20 | local.get $x 21 | i32.const 3 22 | i32.sub 23 | else 24 | local.get $x 25 | i32.const 2 26 | i32.sub 27 | end 28 | local.tee $r 29 | br 0 30 | end 31 | local.tee $r) 32 | ) 33 | -------------------------------------------------------------------------------- /x/tests/tab.c: -------------------------------------------------------------------------------- 1 | void f(){ 2 | } 3 | int32_t g(int32_t x){ 4 | return x; 5 | } 6 | int32_t h(int32_t x, int32_t y){ 7 | return (x+y); 8 | } 9 | void init(){ 10 | _F=malloc(3*sizeof(void*)); 11 | _F[0]=f; 12 | _F[1]=g; 13 | _F[2]=h; 14 | } 15 | -------------------------------------------------------------------------------- /x/tests/tab.go: -------------------------------------------------------------------------------- 1 | package tab 2 | 3 | import . "github.com/ktye/wg/module" 4 | 5 | func init() { 6 | 7 | Functions(0, f) 8 | Functions(1, g) 9 | Functions(2, h) 10 | 11 | } 12 | 13 | func f() { 14 | } 15 | func g(x int32) int32 { 16 | return x 17 | } 18 | func h(x int32, y int32) int32 { 19 | return (x + y) 20 | } 21 | -------------------------------------------------------------------------------- /x/tests/tab.js: -------------------------------------------------------------------------------- 1 | const f=function(){ 2 | } 3 | const g=function(x){ 4 | return x; 5 | } 6 | const h=function(x,y){ 7 | return I(x+y); 8 | } 9 | const init=function(){ 10 | _F=Array(3); 11 | _F[0]=f; 12 | _F[1]=g; 13 | _F[2]=h; 14 | } 15 | -------------------------------------------------------------------------------- /x/tests/tab.k: -------------------------------------------------------------------------------- 1 | C:0x 2 | D:0x 3 | T:`prg`tab`tab`tab`fun`ast`fun`arg`sym`res`ast`ret`get`fun`arg`sym`arg`sym`res`ast`ret`add`get`get 4 | P:0 0 0 0 0 4 0 6 7 6 6 10 11 0 13 14 13 16 13 13 19 20 21 21 5 | I:0 0 1 2 0 0N 0 0N 0 0N 0N 0N 0N 0 0N 0 0N 0 0N 0N 0N 2 0N 0N 6 | S:`tab`f`g`h`f``g`i`x`i``i`x`h`i`x`i`y`i``i`i`x`y 7 | -------------------------------------------------------------------------------- /x/tests/tab.q: -------------------------------------------------------------------------------- 1 | function $f(){ 2 | @start 3 | 4 | } 5 | function w $g(w %x){ 6 | @start 7 | ret %x 8 | } 9 | function w $h(w %x, w %y){ 10 | @start 11 | %.1 =w add %x, %y 12 | ret %.1 13 | } 14 | function init(){ 15 | @start 16 | 17 | 18 | 19 | %_F =l call $malloc(24); 20 | %fi =l add %_F, 0 21 | storel $f, %fi 22 | %fi =l add %_F, 8 23 | storel $g, %fi 24 | %fi =l add %_F, 16 25 | storel $h, %fi 26 | 27 | } 28 | -------------------------------------------------------------------------------- /x/tests/tab.wa: -------------------------------------------------------------------------------- 1 | (module 2 | (memory (export "memory") 1) 3 | (func $f 4 | ) 5 | (func $g (param $x i32) (result i32) 6 | local.get $x) 7 | (func $h (param $x i32) (param $y i32) (result i32) 8 | local.get $x 9 | local.get $y 10 | i32.add) 11 | (table (export "table") 3 funcref) 12 | (elem (i32.const 0) func $f $g $h) 13 | ) 14 | -------------------------------------------------------------------------------- /z.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import . "github.com/ktye/wg/module" 4 | 5 | func zk() { 6 | Data(280, "`k`l`a`b`while`\"rf.\"`\"rz.\"`\"gdt.\"`\"grp.\"`\"enc.\"\n`x:,/+\"0123456789abcdef\"@16 16\\256!\n`t:`39\n`p:`46\n`enc:{$[#y;+(&','(|/c)-c:#'r),'r:{x\\y}/[x;y];(#x)#,!0]}\n`gdt:{[t;g]($[g;{x@>y x};{x@y x};{x@t grade table 10 | 11 | `grp:{(x@*'g)!g:(&~a~'a@-1+!#a:x i)^i: