From 01a6fecddbf7148fcff5a9adc606f188eb5e8bd2 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sun, 6 Mar 2022 22:27:17 +0200 Subject: [PATCH 01/47] BQN: initial commit --- Makefile.impls | 3 ++- impls/bqn/run | 2 ++ impls/bqn/step0_repl.bqn | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100755 impls/bqn/run create mode 100644 impls/bqn/step0_repl.bqn diff --git a/Makefile.impls b/Makefile.impls index 6ac35b23e5..9485863b9d 100644 --- a/Makefile.impls +++ b/Makefile.impls @@ -34,7 +34,7 @@ wasm_MODE = wasmtime # Implementation specific settings # -IMPLS = ada ada.2 awk bash basic bbc-basic c c.2 chuck clojure coffee common-lisp cpp crystal cs d dart \ +IMPLS = ada ada.2 awk bash basic bbc-basic bqn c c.2 chuck clojure coffee common-lisp cpp crystal cs d dart \ elisp elixir elm erlang es6 factor fantom fennel forth fsharp go groovy gnu-smalltalk \ guile haskell haxe hy io janet java java-truffle js jq julia kotlin livescript logo lua make mal \ matlab miniMAL nasm nim objc objpascal ocaml perl perl6 php picolisp pike plpgsql \ @@ -110,6 +110,7 @@ awk_STEP_TO_PROG = impls/awk/$($(1)).awk bash_STEP_TO_PROG = impls/bash/$($(1)).sh basic_STEP_TO_PROG = $(basic_STEP_TO_PROG_$(basic_MODE)) bbc-basic_STEP_TO_PROG = impls/bbc-basic/$($(1)).bas +bqn_STEP_TO_PROG = impls/bqn/$($(1)).bqn c_STEP_TO_PROG = impls/c/$($(1)) c.2_STEP_TO_PROG = impls/c.2/$($(1)) chuck_STEP_TO_PROG = impls/chuck/$($(1)).ck diff --git a/impls/bqn/run b/impls/bqn/run new file mode 100755 index 0000000000..606198a117 --- /dev/null +++ b/impls/bqn/run @@ -0,0 +1,2 @@ +#!/bin/bash +exec BQN $(dirname $0)/${STEP:-stepA_mal}.bqn "${@}" diff --git a/impls/bqn/step0_repl.bqn b/impls/bqn/step0_repl.bqn new file mode 100644 index 0000000000..1a50ad28d7 --- /dev/null +++ b/impls/bqn/step0_repl.bqn @@ -0,0 +1,11 @@ +READ ← ⊢ +EVAL ← ⊢ +PRINT ← ⊢ +Rep ← PRINT∘EVAL∘READ + +{ + •Out "user> " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +} @ From 2f39027a38bbffcfb296a071fca81c56169b06a5 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Mon, 7 Mar 2022 17:10:49 +0200 Subject: [PATCH 02/47] bqn: tokenizer --- impls/bqn/reader.bqn | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 impls/bqn/reader.bqn diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn new file mode 100644 index 0000000000..49d6e8bb58 --- /dev/null +++ b/impls/bqn/reader.bqn @@ -0,0 +1,55 @@ +Reader ← {𝕊 tokens: + { + ts ← tokens + Peek ⇐ {𝕊 ⋄ ⊑ tokens} + Next ⇐ {𝕊 + t ← ⊑ts + ts ↩ 1↓ts + t + } + } +} + +T ← {𝕊 s: + { + 0=≠s ? ⟨⟩ ; + "~@"≡2↑s ? (<"~@") ∾ T 2↓s ; + ⊑(⊑s)∊" ,"∾@+10 ? T 1↓s ; + ⊑(⊑s)∊"[]{}()'`~" ? (<≍⊑s) ∾ T 1↓s ; + ';'=⊑s ? + o←⟨⟩ + {𝕊 + o ∾↩ ⊑s + s ↩ 1↓s + (⊑s) ≠ @+10 + } •_while_⊢1 + ( Date: Mon, 7 Mar 2022 21:56:34 +0200 Subject: [PATCH 03/47] bqn: step1 with bugs --- impls/bqn/printer.bqn | 8 ++++++++ impls/bqn/reader.bqn | 34 ++++++++++++++++++++++++++++++---- impls/bqn/step1_read_print.bqn | 14 ++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 impls/bqn/printer.bqn create mode 100644 impls/bqn/step1_read_print.bqn diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn new file mode 100644 index 0000000000..c9dc11dd8f --- /dev/null +++ b/impls/bqn/printer.bqn @@ -0,0 +1,8 @@ +P ← { + 𝕊 3‿l: "("∾{𝕨∾" "∾𝕩}´(P¨l)∾")"; + 𝕊 1‿n: •Fmt n; + 𝕊 a‿x: x ; + •Show 𝕩 +} + +{PrStr ⇐ P} diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index 49d6e8bb58..b4d68af778 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -1,7 +1,7 @@ Reader ← {𝕊 tokens: { ts ← tokens - Peek ⇐ {𝕊 ⋄ ⊑ tokens} + Peek ⇐ {𝕊 ⋄ ⊑ ts} Next ⇐ {𝕊 t ← ⊑ts ts ↩ 1↓ts @@ -13,6 +13,7 @@ Reader ← {𝕊 tokens: T ← {𝕊 s: { 0=≠s ? ⟨⟩ ; + @=⊑s ? •Exit 0 ; "~@"≡2↑s ? (<"~@") ∾ T 2↓s ; ⊑(⊑s)∊" ,"∾@+10 ? T 1↓s ; ⊑(⊑s)∊"[]{}()'`~" ? (<≍⊑s) ∾ T 1↓s ; @@ -41,15 +42,40 @@ T ← {𝕊 s: {𝕊 o ∾↩ ⊑s s ↩ 1↓s - ¬⊑(⊑s)∊"[]{}()'""`~,; "∾@+10 + {0=≠s ? 0 ; ¬⊑(⊑s)∊"[]{}()'""`~,; "∾@+10} } •_while_⊢1 ( " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From fe842598728f542a700958e1e5f4054ef20d91ba Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Mon, 7 Mar 2022 22:15:35 +0200 Subject: [PATCH 04/47] bqn: add some error handling --- impls/bqn/printer.bqn | 4 +++- impls/bqn/reader.bqn | 24 ++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn index c9dc11dd8f..c8ba350961 100644 --- a/impls/bqn/printer.bqn +++ b/impls/bqn/printer.bqn @@ -1,5 +1,7 @@ P ← { - 𝕊 3‿l: "("∾{𝕨∾" "∾𝕩}´(P¨l)∾")"; + 𝕊 ¯1‿x: "ERROR: unbalanced parens!"; + 𝕊 3‿l: "("∾{0=≠l ? "" ; {𝕨∾" "∾𝕩}´(P¨l)}∾")"; + 𝕊 2‿s: '"'∾s∾'"'; 𝕊 1‿n: •Fmt n; 𝕊 a‿x: x ; •Show 𝕩 diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index b4d68af778..2a59b2082f 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -1,9 +1,9 @@ Reader ← {𝕊 tokens: - { + r ← { ts ← tokens - Peek ⇐ {𝕊 ⋄ ⊑ ts} + Peek ⇐ {𝕊 ⋄ 0=≠ts ? @; ⊑ts} Next ⇐ {𝕊 - t ← ⊑ts + t ← r.Peek@ ts ↩ 1↓ts t } @@ -22,9 +22,10 @@ T ← {𝕊 s: {𝕊 o ∾↩ ⊑s s ↩ 1↓s - (⊑s) ≠ @+10 + {0=≠s ? 0 ; (⊑s) ≠ @+10} } •_while_⊢1 ( Date: Tue, 8 Mar 2022 10:38:52 +0200 Subject: [PATCH 05/47] bqn: step 2 --- impls/bqn/printer.bqn | 6 +++--- impls/bqn/reader.bqn | 7 +++++-- impls/bqn/step2_eval.bqn | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 impls/bqn/step2_eval.bqn diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn index c8ba350961..fc9e1e6f61 100644 --- a/impls/bqn/printer.bqn +++ b/impls/bqn/printer.bqn @@ -1,10 +1,10 @@ P ← { - 𝕊 ¯1‿x: "ERROR: unbalanced parens!"; + 𝕊 ¯1‿x: "ERROR: "∾x; 𝕊 3‿l: "("∾{0=≠l ? "" ; {𝕨∾" "∾𝕩}´(P¨l)}∾")"; 𝕊 2‿s: '"'∾s∾'"'; - 𝕊 1‿n: •Fmt n; + 𝕊 1‿n: {n<0?"-";""}∾•Fmt |n; 𝕊 a‿x: x ; - •Show 𝕩 + "ERROR printing: "∾•Fmt 𝕩 } {PrStr ⇐ P} diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index 2a59b2082f..9e880b6395 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -55,9 +55,12 @@ T ← {𝕊 s: # 1 int # 2 string # 3 list +Int ← {10⊸×⊸+˜´'0'-˜⌽𝕩} +nums ← "0123456789" Atom ← {𝕊 a: {'"': 2∾<¯1↓1↓a ; - ⊑𝕩∊"0123456789" ? 1∾10⊸×⊸+˜´'0'-˜⌽a ; + ⊑𝕩∊nums ? 1∾Int a ; + (𝕩='-')∧1<≠a ? 1∾-Int 1↓a ; 0‿a }⊑a } @@ -66,7 +69,7 @@ ReadList ← {𝕊 r: t←3 {𝕊 {")": r.Next@ ⋄ 0; - @: t ↩ ¯1 ⋄ 0; + @: t ↩ ¯1 ⋄ l ↩ "unbalanced parens" ⋄ 0; l ∾↩ ⟨ + "+"‿1‿(+´) + "-"‿1‿(-´) + "*"‿1‿(×´) + "/"‿1‿(÷´) +⟩ +Undef ← {@‿¯1‿(<"undefined: "∾𝕩)} + +EvalAst ← {env 𝕊 t‿v: + {0: idx ← ⊑((⊏˘env)⊐( " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From 112a1045aa8ec290616dc59147ae5a4dbc23d5da Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sat, 12 Mar 2022 19:39:39 +0200 Subject: [PATCH 06/47] bqn: implement vectors --- impls/bqn/printer.bqn | 6 ++++-- impls/bqn/reader.bqn | 11 ++++++----- impls/bqn/step2_eval.bqn | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn index fc9e1e6f61..9e93d2a2d1 100644 --- a/impls/bqn/printer.bqn +++ b/impls/bqn/printer.bqn @@ -1,10 +1,12 @@ +Lst ← {0=≠𝕩 ? "" ; {𝕨∾" "∾𝕩}´(P¨𝕩)} P ← { 𝕊 ¯1‿x: "ERROR: "∾x; - 𝕊 3‿l: "("∾{0=≠l ? "" ; {𝕨∾" "∾𝕩}´(P¨l)}∾")"; + 𝕊 4‿l: "["∾(Lst l)∾"]"; + 𝕊 3‿l: "("∾(Lst l)∾")"; 𝕊 2‿s: '"'∾s∾'"'; 𝕊 1‿n: {n<0?"-";""}∾•Fmt |n; 𝕊 a‿x: x ; - "ERROR printing: "∾•Fmt 𝕩 + "ERROR printing: "∾(@+10)∾•Fmt 𝕩 } {PrStr ⇐ P} diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index 9e880b6395..df2f6407fa 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -55,6 +55,7 @@ T ← {𝕊 s: # 1 int # 2 string # 3 list +# 4 vector Int ← {10⊸×⊸+˜´'0'-˜⌽𝕩} nums ← "0123456789" Atom ← {𝕊 a: @@ -64,12 +65,11 @@ Atom ← {𝕊 a: 0‿a }⊑a } -ReadList ← {𝕊 r: +ReadList ← {𝕊 r‿t‿e: l←⟨⟩ - t←3 {𝕊 - {")": r.Next@ ⋄ 0; - @: t ↩ ¯1 ⋄ l ↩ "unbalanced parens" ⋄ 0; + {@: t ↩ ¯1 ⋄ l ↩ "unbalanced parens" ⋄ 0; + 𝕩≡e ? r.Next@ ⋄ 0; l ∾↩ Date: Sat, 12 Mar 2022 20:55:50 +0200 Subject: [PATCH 07/47] bqn: keywords and kinda hashmaps --- impls/bqn/printer.bqn | 2 ++ impls/bqn/reader.bqn | 19 ++++++++++++------- impls/bqn/step2_eval.bqn | 1 + 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn index 9e93d2a2d1..5f4854d4a8 100644 --- a/impls/bqn/printer.bqn +++ b/impls/bqn/printer.bqn @@ -1,6 +1,8 @@ Lst ← {0=≠𝕩 ? "" ; {𝕨∾" "∾𝕩}´(P¨𝕩)} P ← { 𝕊 ¯1‿x: "ERROR: "∾x; + 𝕊 6‿kw: kw; + 𝕊 5‿l: "{"∾(Lst l)∾"}"; 𝕊 4‿l: "["∾(Lst l)∾"]"; 𝕊 3‿l: "("∾(Lst l)∾")"; 𝕊 2‿s: '"'∾s∾'"'; diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index df2f6407fa..d1cfaa555f 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -1,3 +1,13 @@ +# Types: +# ¯1 error +# 0 symbol +# 1 int +# 2 string +# 3 list +# 4 vector +# 5 hashmap +# 6 keyword + Reader ← {𝕊 tokens: r ← { ts ← tokens @@ -49,17 +59,11 @@ T ← {𝕊 s: } } -# Types: -# ¯1 error -# 0 symbol -# 1 int -# 2 string -# 3 list -# 4 vector Int ← {10⊸×⊸+˜´'0'-˜⌽𝕩} nums ← "0123456789" Atom ← {𝕊 a: {'"': 2∾<¯1↓1↓a ; + ':': 6‿a ; ⊑𝕩∊nums ? 1∾Int a ; (𝕩='-')∧1<≠a ? 1∾-Int 1↓a ; 0‿a }⊑a @@ -79,6 +83,7 @@ ReadList ← {𝕊 r‿t‿e: ReadForm ← {𝕊 r: {"(": ReadList r‿3‿")" ; "[": ReadList r‿4‿"]" ; + "{": ReadList r‿5‿"}" ; Atom 𝕩 }r.Next@ } diff --git a/impls/bqn/step2_eval.bqn b/impls/bqn/step2_eval.bqn index 54ac5ebca5..2400cbc548 100644 --- a/impls/bqn/step2_eval.bqn +++ b/impls/bqn/step2_eval.bqn @@ -28,6 +28,7 @@ EVAL ← { t∾F 1⊸↓¨zs } ; env 𝕊 4‿xs: 4∾ Date: Tue, 15 Mar 2022 14:32:59 +0200 Subject: [PATCH 08/47] bqn: step3 --- impls/bqn/env.bqn | 12 +++++++++++ impls/bqn/step3_env.bqn | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 impls/bqn/env.bqn create mode 100644 impls/bqn/step3_env.bqn diff --git a/impls/bqn/env.bqn b/impls/bqn/env.bqn new file mode 100644 index 0000000000..0bdb913368 --- /dev/null +++ b/impls/bqn/env.bqn @@ -0,0 +1,12 @@ +Env ⇐ {𝕊 outer: + d ← ↕0‿2 + Set ⇐ {𝕊 k‿v: + d ↩ k‿v∾((𝕩} +ns.Set "-"‿{1∾-´1⊏˘>𝕩} +ns.Set "*"‿{1∾×´1⊏˘>𝕩} +ns.Set "/"‿{1∾⌊∘÷´1⊏˘>𝕩} + +EvalAst ← {env 𝕊 t‿v: + {0: env.Get v ; + 3: env⊸EVAL¨v ; + t‿v + }t +} + +READ ← ReadStr +PRINT ← PrStr +EVAL ← { + env 𝕊 3‿⟨⟩: 3‿⟨⟩ ; + env 𝕊 3‿⟨0‿"def!",0‿k,x⟩: + v ← env EVAL x + {¯1:@;env.Set ( " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From d2f91af37b5fa00e13883a51b8a2ec7a083d1822 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Thu, 17 Mar 2022 20:48:21 +0200 Subject: [PATCH 09/47] bqn: step4 core functions --- impls/bqn/core.bqn | 24 ++++++++++++++++++++ impls/bqn/printer.bqn | 3 +++ impls/bqn/reader.bqn | 9 ++++++-- impls/bqn/step4_if_fn_do.bqn | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 impls/bqn/core.bqn create mode 100644 impls/bqn/step4_if_fn_do.bqn diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn new file mode 100644 index 0000000000..884c1db115 --- /dev/null +++ b/impls/bqn/core.bqn @@ -0,0 +1,24 @@ +⟨Env⟩ ← •Import "env.bqn" + +ns ← ⟨ + "+"‿{1∾+´1⊏˘>𝕩} + "-"‿{1∾-´1⊏˘>𝕩} + "*"‿{1∾×´1⊏˘>𝕩} + "/"‿{1∾⌊∘÷´1⊏˘>𝕩} + + "="‿{7∾∧´≡´˘2↕𝕩} + "<"‿{7∾∧´<´˘2↕1⊸⊑¨𝕩} + "<="‿{7∾∧´≤´˘2↕1⊸⊑¨𝕩} + ">"‿{7∾∧´>´˘2↕1⊸⊑¨𝕩} + ">="‿{7∾∧´≥´˘2↕1⊸⊑¨𝕩} + + "list"‿{3‿𝕩} + "list?"‿{7∾3=⊑⊑𝕩} + "empty?"‿{7∾0=≠1⊑⊑𝕩} + "count"‿{1∾≠1⊑⊑𝕩} + + "bqn-show"‿{•Show ⊑ 𝕩} +⟩ + +core ⇐ Env@ +core.Set¨ns diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn index 5f4854d4a8..8d158ce948 100644 --- a/impls/bqn/printer.bqn +++ b/impls/bqn/printer.bqn @@ -1,6 +1,9 @@ Lst ← {0=≠𝕩 ? "" ; {𝕨∾" "∾𝕩}´(P¨𝕩)} P ← { 𝕊 ¯1‿x: "ERROR: "∾x; + 𝕊 8‿x: "nil"; + 𝕊 7‿0: "false"; + 𝕊 7‿1: "true"; 𝕊 6‿kw: kw; 𝕊 5‿l: "{"∾(Lst l)∾"}"; 𝕊 4‿l: "["∾(Lst l)∾"]"; diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index d1cfaa555f..b3e7a09dd4 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -7,6 +7,8 @@ # 4 vector # 5 hashmap # 6 keyword +# 7 boolean +# 8 nil Reader ← {𝕊 tokens: r ← { @@ -61,8 +63,11 @@ T ← {𝕊 s: Int ← {10⊸×⊸+˜´'0'-˜⌽𝕩} nums ← "0123456789" -Atom ← {𝕊 a: - {'"': 2∾<¯1↓1↓a ; +Atom ← { + 𝕊 "true": 7‿1 ; + 𝕊 "false": 7‿0 ; + 𝕊 "nil": 8‿⟨⟩ ; + 𝕊 a: {'"': 2∾<¯1↓1↓a ; ':': 6‿a ; ⊑𝕩∊nums ? 1∾Int a ; (𝕩='-')∧1<≠a ? 1∾-Int 1↓a ; diff --git a/impls/bqn/step4_if_fn_do.bqn b/impls/bqn/step4_if_fn_do.bqn new file mode 100644 index 0000000000..548a147ad2 --- /dev/null +++ b/impls/bqn/step4_if_fn_do.bqn @@ -0,0 +1,43 @@ +⟨Env⟩ ← •Import "env.bqn" +⟨ReadStr⟩ ← •Import "reader.bqn" +⟨PrStr⟩ ← •Import "printer.bqn" +⟨core⟩ ← •Import "core.bqn" + +EvalAst ← {env 𝕊 t‿v: + {0: env.Get v ; + 3: env⊸EVAL¨v ; + t‿v + }t +} + +READ ← ReadStr +PRINT ← PrStr +EVAL ← { + env 𝕊 3‿⟨⟩: 3‿⟨⟩ ; + env 𝕊 3‿⟨0‿"def!",0‿k,x⟩: + v ← env EVAL x + {¯1:@;env.Set ( " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From cae356ed62b2c82a17025de3ddb2f733ef8e87ee Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Thu, 17 Mar 2022 21:27:23 +0200 Subject: [PATCH 10/47] bqn: step4 if & fn* --- impls/bqn/core.bqn | 2 +- impls/bqn/env.bqn | 2 +- impls/bqn/printer.bqn | 1 + impls/bqn/reader.bqn | 5 ++++- impls/bqn/step4_if_fn_do.bqn | 24 +++++++++++++++++++++--- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 884c1db115..01f432125b 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -21,4 +21,4 @@ ns ← ⟨ ⟩ core ⇐ Env@ -core.Set¨ns +{𝕊 kw‿f: core.Set kw‿(9‿f)}¨ns diff --git a/impls/bqn/env.bqn b/impls/bqn/env.bqn index 0bdb913368..5e8e841fda 100644 --- a/impls/bqn/env.bqn +++ b/impls/bqn/env.bqn @@ -1,5 +1,5 @@ Env ⇐ {𝕊 outer: - d ← ↕0‿2 + d ⇐ ↕0‿2 Set ⇐ {𝕊 k‿v: d ↩ k‿v∾(( Date: Thu, 17 Mar 2022 21:39:34 +0200 Subject: [PATCH 11/47] bqn: step4 do + prn --- impls/bqn/core.bqn | 2 ++ impls/bqn/step4_if_fn_do.bqn | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 01f432125b..9d905aeb8b 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -1,4 +1,5 @@ ⟨Env⟩ ← •Import "env.bqn" +⟨PrStr⟩ ← •Import "printer.bqn" ns ← ⟨ "+"‿{1∾+´1⊏˘>𝕩} @@ -18,6 +19,7 @@ ns ← ⟨ "count"‿{1∾≠1⊑⊑𝕩} "bqn-show"‿{•Show ⊑ 𝕩} + "prn"‿{•Out∘PrStr¨𝕩 ⋄ 8‿⟨⟩} ⟩ core ⇐ Env@ diff --git a/impls/bqn/step4_if_fn_do.bqn b/impls/bqn/step4_if_fn_do.bqn index a4048caefb..a35d254a92 100644 --- a/impls/bqn/step4_if_fn_do.bqn +++ b/impls/bqn/step4_if_fn_do.bqn @@ -13,7 +13,7 @@ EvalAst ← {env 𝕊 t‿v: READ ← ReadStr PRINT ← PrStr EVAL ← { - # builtin symbols + # builtin special forms env 𝕊 3‿⟨0‿"def!",0‿k,x⟩: v ← env EVAL x {¯1:@;env.Set ( Date: Thu, 17 Mar 2022 21:58:33 +0200 Subject: [PATCH 12/47] bqn: variadic functions --- impls/bqn/step4_if_fn_do.bqn | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/impls/bqn/step4_if_fn_do.bqn b/impls/bqn/step4_if_fn_do.bqn index a35d254a92..204ad5ab12 100644 --- a/impls/bqn/step4_if_fn_do.bqn +++ b/impls/bqn/step4_if_fn_do.bqn @@ -29,12 +29,13 @@ EVAL ← { env EVAL {8‿x:f; 7‿0:f; t} env EVAL c ; env 𝕊 3‿⟨0‿"if",c,t⟩: env EVAL {8‿x:nil; 7‿0:nil; t} env EVAL c ; - outer 𝕊 3‿⟨0‿"fn*",3‿args,exp⟩: + outer 𝕊 3‿⟨0‿"fn*",t‿args,exp⟩: 9‿{𝕊 xs: as ← args inner ← Env outer {𝕊 - {0‿kw: inner.Set ( Date: Sat, 16 Apr 2022 13:45:48 +0300 Subject: [PATCH 13/47] bqn: add slurp and read-string --- impls/bqn/core.bqn | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 9d905aeb8b..68ef992f8e 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -1,5 +1,6 @@ ⟨Env⟩ ← •Import "env.bqn" ⟨PrStr⟩ ← •Import "printer.bqn" +⟨ReadStr⟩ ← •Import "reader.bqn" ns ← ⟨ "+"‿{1∾+´1⊏˘>𝕩} @@ -20,6 +21,9 @@ ns ← ⟨ "bqn-show"‿{•Show ⊑ 𝕩} "prn"‿{•Out∘PrStr¨𝕩 ⋄ 8‿⟨⟩} + + "read-string"‿{ReadStr 1⊑⊑𝕩} + "slurp"‿{2∾<•FChars 1⊑⊑𝕩} ⟩ core ⇐ Env@ From d6d5143b1b73412efff7d61fdf8aac9897e93388 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sat, 16 Apr 2022 17:36:27 +0300 Subject: [PATCH 14/47] bqn: step5: let*, do and if TCO --- impls/bqn/step5_tco.bqn | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 impls/bqn/step5_tco.bqn diff --git a/impls/bqn/step5_tco.bqn b/impls/bqn/step5_tco.bqn new file mode 100644 index 0000000000..43625ba0f4 --- /dev/null +++ b/impls/bqn/step5_tco.bqn @@ -0,0 +1,78 @@ +⟨Env⟩ ← •Import "env.bqn" +⟨ReadStr,nil⟩ ← •Import "reader.bqn" +⟨PrStr⟩ ← •Import "printer.bqn" +⟨core⟩ ← •Import "core.bqn" + +EvalAst ← {env 𝕊 t‿v: + {0: env.Get v ; + 3: env⊸EVAL¨v ; + t‿v + }t +} + +READ ← ReadStr +PRINT ← PrStr +EVAL ← {e 𝕊 ast: + # this loop runs as long as @ is returned, ie. typically once + {𝕊 ⋄ { + # builtin special forms + 𝕊 3‿⟨0‿"def!",0‿k,x⟩: + v ← e EVAL x + {¯1:@;e.Set ( " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From 351d446a1139c736f3ff44a569c14643961d0095 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sat, 16 Apr 2022 20:17:39 +0300 Subject: [PATCH 15/47] bqn: step5: TCO --- impls/bqn/printer.bqn | 1 + impls/bqn/step5_tco.bqn | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn index 23d515420b..97320b2dfb 100644 --- a/impls/bqn/printer.bqn +++ b/impls/bqn/printer.bqn @@ -1,6 +1,7 @@ Lst ← {0=≠𝕩 ? "" ; {𝕨∾" "∾𝕩}´(P¨𝕩)} P ← { 𝕊 ¯1‿x: "ERROR: "∾x; + 𝕊 10‿x‿y‿z‿f: "#function"; 𝕊 9‿x: "#function"; 𝕊 8‿x: "nil"; 𝕊 7‿0: "false"; diff --git a/impls/bqn/step5_tco.bqn b/impls/bqn/step5_tco.bqn index 43625ba0f4..302778791f 100644 --- a/impls/bqn/step5_tco.bqn +++ b/impls/bqn/step5_tco.bqn @@ -10,6 +10,15 @@ EvalAst ← {env 𝕊 t‿v: }t } +SetParams ← {env 𝕊 args‿xs: + {𝕊 + {0‿"&": env.Set (<1⊑1⊑args)∾<3∾ Date: Sun, 17 Apr 2022 00:10:34 +0300 Subject: [PATCH 16/47] bqn: implement string escapes and str functions --- impls/bqn/core.bqn | 13 ++++++++++-- impls/bqn/printer.bqn | 40 ++++++++++++++++++++++-------------- impls/bqn/reader.bqn | 21 ++++++++++++++++++- impls/bqn/step4_if_fn_do.bqn | 6 +++--- impls/bqn/step5_tco.bqn | 6 +++--- 5 files changed, 62 insertions(+), 24 deletions(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 68ef992f8e..5710cc794f 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -1,7 +1,13 @@ ⟨Env⟩ ← •Import "env.bqn" -⟨PrStr⟩ ← •Import "printer.bqn" +⟨PrStr,PrStrR⟩ ← •Import "printer.bqn" ⟨ReadStr⟩ ← •Import "reader.bqn" +nil ← 8‿⟨⟩ +Q ← {∾´'"'∾𝕩∾'"'} +J ← {0=≠𝕩 ? "" ; + 1=≠𝕩 ? ⊑𝕩 ; + ∾´(⊑𝕩)∾𝕨∾𝕨J 1↓𝕩} + ns ← ⟨ "+"‿{1∾+´1⊏˘>𝕩} "-"‿{1∾-´1⊏˘>𝕩} @@ -20,7 +26,10 @@ ns ← ⟨ "count"‿{1∾≠1⊑⊑𝕩} "bqn-show"‿{•Show ⊑ 𝕩} - "prn"‿{•Out∘PrStr¨𝕩 ⋄ 8‿⟨⟩} + "pr-str"‿{2∾<" "J PrStrR¨𝕩} + "str"‿{2∾<""J PrStr¨𝕩} + "prn"‿{•Out " "J PrStrR¨𝕩 ⋄ nil} + "println"‿{•Out " "J PrStr¨𝕩 ⋄ nil} "read-string"‿{ReadStr 1⊑⊑𝕩} "slurp"‿{2∾<•FChars 1⊑⊑𝕩} diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn index 97320b2dfb..08b830be76 100644 --- a/impls/bqn/printer.bqn +++ b/impls/bqn/printer.bqn @@ -1,19 +1,29 @@ -Lst ← {0=≠𝕩 ? "" ; {𝕨∾" "∾𝕩}´(P¨𝕩)} +Lst ← {0=≠𝕩 ? "" ; {𝕨∾" "∾𝕩}´(𝕨⊸P¨𝕩)} P ← { - 𝕊 ¯1‿x: "ERROR: "∾x; - 𝕊 10‿x‿y‿z‿f: "#function"; - 𝕊 9‿x: "#function"; - 𝕊 8‿x: "nil"; - 𝕊 7‿0: "false"; - 𝕊 7‿1: "true"; - 𝕊 6‿kw: kw; - 𝕊 5‿l: "{"∾(Lst l)∾"}"; - 𝕊 4‿l: "["∾(Lst l)∾"]"; - 𝕊 3‿l: "("∾(Lst l)∾")"; - 𝕊 2‿s: '"'∾s∾'"'; - 𝕊 1‿n: {n<0?"-";""}∾•Fmt |n; - 𝕊 a‿x: x ; + r 𝕊 ¯1‿x: "ERROR: "∾x; + r 𝕊 10‿x‿y‿z‿f: "#function"; + r 𝕊 9‿x: "#function"; + r 𝕊 8‿x: "nil"; + r 𝕊 7‿0: "false"; + r 𝕊 7‿1: "true"; + r 𝕊 6‿kw: kw; + r 𝕊 5‿l: "{"∾(r Lst l)∾"}"; + r 𝕊 4‿l: "["∾(r Lst l)∾"]"; + r 𝕊 3‿l: "("∾(r Lst l)∾")"; + 0 𝕊 2‿s: s; + 1 𝕊 2‿s: + ∾´{ + 𝕊 '"': "\"""; + 𝕊 ' +': "\n"; + 𝕊 '\': "\\"; + 𝕊 @: """"; + ""∾𝕩 + }¨(@∾s∾@); + r 𝕊 1‿n: {n<0?"-";""}∾•Fmt |n; + r 𝕊 a‿x: x ; "ERROR printing: "∾(@+10)∾•Fmt 𝕩 } -{PrStr ⇐ P} +{PrStr ⇐ 0⊸P + PrStrR ⇐ 1⊸P} diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index 191e741f6a..983d930284 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -62,6 +62,25 @@ T ← {𝕊 s: } } +ReadString ← { + 𝕊 "": ""; + 𝕊 ⟨c⟩: c; + 𝕊 s: { + '\'≠⊑s ? (⊑s)∾ReadString 1↓s ; + ({ + # NOTE: doesn't support octal etc. + 'a': @+7; + 'b': @+8; + 'e': @+27; + 'f': @+12; + 'n': @+10; + 'r': @+13; + 't': @+9; + 'v': @+11; + 𝕩 + }1⊑s) ∾ ReadString 2↓s} +} + Int ← {10⊸×⊸+˜´'0'-˜⌽𝕩} nil ← 8‿⟨⟩ nums ← "0123456789" @@ -69,7 +88,7 @@ Atom ← { 𝕊 "true": 7‿1 ; 𝕊 "false": 7‿0 ; 𝕊 "nil": nil ; - 𝕊 a: {'"': 2∾<¯1↓1↓a ; + 𝕊 a: {'"': 2∾ Date: Sun, 17 Apr 2022 00:17:27 +0300 Subject: [PATCH 17/47] bqn: step6: load-file --- impls/bqn/step6_file.bqn | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 impls/bqn/step6_file.bqn diff --git a/impls/bqn/step6_file.bqn b/impls/bqn/step6_file.bqn new file mode 100644 index 0000000000..5c4e9e7bba --- /dev/null +++ b/impls/bqn/step6_file.bqn @@ -0,0 +1,89 @@ +⟨Env⟩ ← •Import "env.bqn" +⟨ReadStr,nil⟩ ← •Import "reader.bqn" +⟨PrStrR⟩ ← •Import "printer.bqn" +⟨core⟩ ← •Import "core.bqn" + +EvalAst ← {env 𝕊 t‿v: + {0: env.Get v ; + 3: env⊸EVAL¨v ; + t‿v + }t +} + +SetParams ← {env 𝕊 args‿xs: + {𝕊 + {0‿"&": env.Set (<1⊑1⊑args)∾<3∾ " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From 55f2bf1b330f8cff16e6c1c823d04f38d66c4183 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sun, 17 Apr 2022 12:13:41 +0300 Subject: [PATCH 18/47] bqn: step6: atoms --- impls/bqn/core.bqn | 14 +++++++++++++- impls/bqn/globals.bqn | 6 ++++++ impls/bqn/printer.bqn | 1 + impls/bqn/reader.bqn | 5 +++-- impls/bqn/step6_file.bqn | 3 ++- 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 impls/bqn/globals.bqn diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 5710cc794f..3da13c2919 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -1,8 +1,8 @@ +⟨Atom,nil⟩ ← •Import "globals.bqn" ⟨Env⟩ ← •Import "env.bqn" ⟨PrStr,PrStrR⟩ ← •Import "printer.bqn" ⟨ReadStr⟩ ← •Import "reader.bqn" -nil ← 8‿⟨⟩ Q ← {∾´'"'∾𝕩∾'"'} J ← {0=≠𝕩 ? "" ; 1=≠𝕩 ? ⊑𝕩 ; @@ -33,6 +33,18 @@ ns ← ⟨ "read-string"‿{ReadStr 1⊑⊑𝕩} "slurp"‿{2∾<•FChars 1⊑⊑𝕩} + + "atom"‿{11∾Atom ⊑𝕩} + "atom?"‿{7∾11=⊑⊑𝕩} + "deref"‿{𝕊 ⟨11‿a⟩: a.Deref@; 1‿"deref: not an atom"} + "reset!"‿{ + 𝕊 ⟨11‿a,v⟩: a.Reset v; + 1‿"reset!: not an atom"} + "swap!"‿{ + ⟨t‿a,fn⟩ ← 2↑𝕩 + args ← ( Date: Sun, 17 Apr 2022 13:48:24 +0300 Subject: [PATCH 19/47] bqn: support comments --- impls/bqn/reader.bqn | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index 6d886ccc48..aa3a9858e5 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -92,6 +92,7 @@ Atom ← { 𝕊 "nil": nil ; 𝕊 a: {'"': 2∾ Date: Sun, 17 Apr 2022 13:56:36 +0300 Subject: [PATCH 20/47] bqn: add @ reader macro --- impls/bqn/reader.bqn | 2 ++ 1 file changed, 2 insertions(+) diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index aa3a9858e5..dcb8e8f2aa 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -30,6 +30,7 @@ T ← {𝕊 s: { 0=≠s ? ⟨⟩ ; @=⊑s ? •Exit 0 ; + '@'=⊑s ? (<"@") ∾ T 1↓s ; "~@"≡2↑s ? (<"~@") ∾ T 2↓s ; ⊑(⊑s)∊" ,"∾@+10 ? T 1↓s ; ⊑(⊑s)∊"[]{}()'`~" ? (<≍⊑s) ∾ T 1↓s ; @@ -115,6 +116,7 @@ ReadForm ← {𝕊 r: {"(": ReadList r‿3‿")" ; "[": ReadList r‿4‿"]" ; "{": ReadList r‿5‿"}" ; + "@": 3‿⟨0‿"deref", ReadForm r⟩; Atom 𝕩 }r.Next@ } From 2bbcdc25247c6bdac8159e93142a03df02d08d44 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Fri, 22 Apr 2022 16:52:37 +0300 Subject: [PATCH 21/47] bqn: step7: quote & quasiquote --- impls/bqn/core.bqn | 7 ++- impls/bqn/step7_quote.bqn | 109 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 impls/bqn/step7_quote.bqn diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 3da13c2919..c5a2631876 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -36,15 +36,18 @@ ns ← ⟨ "atom"‿{11∾Atom ⊑𝕩} "atom?"‿{7∾11=⊑⊑𝕩} - "deref"‿{𝕊 ⟨11‿a⟩: a.Deref@; 1‿"deref: not an atom"} + "deref"‿{𝕊 ⟨11‿a⟩: a.Deref@; ¯1‿"deref: not an atom"} "reset!"‿{ 𝕊 ⟨11‿a,v⟩: a.Reset v; - 1‿"reset!: not an atom"} + ¯1‿"reset!: not an atom"} "swap!"‿{ ⟨t‿a,fn⟩ ← 2↑𝕩 args ← ( " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From 85b8ffe1707d82244134795b509002dd2ba638bf Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Fri, 22 Apr 2022 17:03:53 +0300 Subject: [PATCH 22/47] bqn: step7: reader macros --- impls/bqn/reader.bqn | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index dcb8e8f2aa..a29d32e30f 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -117,6 +117,10 @@ ReadForm ← {𝕊 r: "[": ReadList r‿4‿"]" ; "{": ReadList r‿5‿"}" ; "@": 3‿⟨0‿"deref", ReadForm r⟩; + "'": 3‿⟨0‿"quote", ReadForm r⟩; + "`": 3‿⟨0‿"quasiquote", ReadForm r⟩; + "~": 3‿⟨0‿"unquote", ReadForm r⟩; + "~@": 3‿⟨0‿"splice-unquote", ReadForm r⟩; Atom 𝕩 }r.Next@ } From 73f898009497ac96267bb0b0f3cc065af5ef534b Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sat, 11 Jun 2022 14:56:14 +0300 Subject: [PATCH 23/47] bqn: step8: trivial macros --- impls/bqn/printer.bqn | 1 + impls/bqn/reader.bqn | 1 + impls/bqn/step8_macros.bqn | 127 +++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 impls/bqn/step8_macros.bqn diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn index 7de8e0e0d4..eb9a63c878 100644 --- a/impls/bqn/printer.bqn +++ b/impls/bqn/printer.bqn @@ -1,6 +1,7 @@ Lst ← {0=≠𝕩 ? "" ; {𝕨∾" "∾𝕩}´(𝕨⊸P¨𝕩)} P ← { r 𝕊 ¯1‿x: "ERROR: "∾x; + r 𝕊 12‿x: "#macro"; r 𝕊 11‿a: "(atom "∾(r P a.Deref@)∾")"; r 𝕊 10‿x‿y‿z‿f: "#function"; r 𝕊 9‿x: "#function"; diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index a29d32e30f..3d2a3d7bdc 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -13,6 +13,7 @@ # 9 function # 10 fn* # 11 atom +# 12 macro Reader ← {𝕊 tokens: r ← { diff --git a/impls/bqn/step8_macros.bqn b/impls/bqn/step8_macros.bqn new file mode 100644 index 0000000000..c4ca002978 --- /dev/null +++ b/impls/bqn/step8_macros.bqn @@ -0,0 +1,127 @@ +⟨nil⟩ ← •Import "globals.bqn" +⟨Env⟩ ← •Import "env.bqn" +⟨ReadStr⟩ ← •Import "reader.bqn" +⟨PrStrR⟩ ← •Import "printer.bqn" +⟨core⟩ ← •Import "core.bqn" + +EvalAst ← { + env 𝕊 t‿v: + {0: env.Get v ; + 3: env⊸EVAL¨v ; + t‿v + }t ; + 𝕩 +} + +SetParams ← {env 𝕊 args‿xs: + {𝕊 + {0‿"&": env.Set (<1⊑1⊑args)∾<3∾ " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From 77130819e5056c12455662c379df62ace0aad536 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sun, 19 Jun 2022 13:01:41 +0300 Subject: [PATCH 24/47] bqn: step8: full macro support --- impls/bqn/step8_macros.bqn | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/impls/bqn/step8_macros.bqn b/impls/bqn/step8_macros.bqn index c4ca002978..3f957b5796 100644 --- a/impls/bqn/step8_macros.bqn +++ b/impls/bqn/step8_macros.bqn @@ -15,8 +15,8 @@ EvalAst ← { SetParams ← {env 𝕊 args‿xs: {𝕊 - {0‿"&": env.Set (<1⊑1⊑args)∾<3∾ Date: Sun, 19 Jun 2022 13:06:34 +0300 Subject: [PATCH 25/47] bqn: step8: add 'not to pass tests --- impls/bqn/step8_macros.bqn | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/impls/bqn/step8_macros.bqn b/impls/bqn/step8_macros.bqn index 3f957b5796..2d32044e95 100644 --- a/impls/bqn/step8_macros.bqn +++ b/impls/bqn/step8_macros.bqn @@ -121,7 +121,8 @@ EVAL ← {e 𝕊 ast: Rep ← PRINT∘{core EVAL 𝕩}∘READ core.Set "eval"‿(9‿{core EVAL ⊑𝕩}) -Rep "(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) "" nil)"")))))" +Rep "(def! not (fn* (a) (if a false true))) +(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) "" nil)"")))))" { •Out "user> " From 7b2f69e8681b31de59f985aabfada25a28db302f Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sun, 19 Jun 2022 13:24:05 +0300 Subject: [PATCH 26/47] bqn: step8: add nth, first, rest, cond --- impls/bqn/core.bqn | 6 +++++- impls/bqn/step8_macros.bqn | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index c5a2631876..86e35a897c 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -46,8 +46,12 @@ ns ← ⟨ f ← {9‿f: f; 10‿x‿y‿z‿f: f}fn a.Reset F args} - "cons"‿{𝕊 x‿⟨3,xs⟩: 3∾<( (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))" { •Out "user> " From d74e9c85f650a9634e82139f0270a6d62a2621b5 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sun, 19 Jun 2022 19:27:11 +0300 Subject: [PATCH 27/47] =?UTF-8?q?bqn:=20step9:=20throw=20by=20returning=20?= =?UTF-8?q?type=20=C2=AF1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- impls/bqn/core.bqn | 12 ++-- impls/bqn/env.bqn | 4 +- impls/bqn/globals.bqn | 1 + impls/bqn/printer.bqn | 3 +- impls/bqn/step9_try.bqn | 139 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 impls/bqn/step9_try.bqn diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 86e35a897c..a783abdc39 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -1,4 +1,4 @@ -⟨Atom,nil⟩ ← •Import "globals.bqn" +⟨Atom,nil,Err⟩ ← •Import "globals.bqn" ⟨Env⟩ ← •Import "env.bqn" ⟨PrStr,PrStrR⟩ ← •Import "printer.bqn" ⟨ReadStr⟩ ← •Import "reader.bqn" @@ -36,22 +36,24 @@ ns ← ⟨ "atom"‿{11∾Atom ⊑𝕩} "atom?"‿{7∾11=⊑⊑𝕩} - "deref"‿{𝕊 ⟨11‿a⟩: a.Deref@; ¯1‿"deref: not an atom"} + "deref"‿{𝕊 ⟨11‿a⟩: a.Deref@; Err "deref: not an atom"} "reset!"‿{ 𝕊 ⟨11‿a,v⟩: a.Reset v; - ¯1‿"reset!: not an atom"} + Err "reset!: not an atom"} "swap!"‿{ ⟨t‿a,fn⟩ ← 2↑𝕩 args ← ( (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))" + +{ + •Out "user> " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From f3382b506e9e2bf27edb48056fe9cf77f8ba1cd3 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sun, 19 Jun 2022 20:17:54 +0300 Subject: [PATCH 28/47] bqn: step9: apply, map etc. --- impls/bqn/core.bqn | 24 ++++++++++++++++++++++-- impls/bqn/printer.bqn | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index a783abdc39..6c9639ccb9 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -21,11 +21,10 @@ ns ← ⟨ ">="‿{7∾∧´≥´˘2↕1⊸⊑¨𝕩} "list"‿{3‿𝕩} - "list?"‿{7∾3=⊑⊑𝕩} "empty?"‿{7∾0=≠1⊑⊑𝕩} "count"‿{1∾≠1⊑⊑𝕩} - "bqn-show"‿{•Show ⊑ 𝕩} + "bqn-show"‿{•Show ⊑ 𝕩 ⋄ 𝕩} "pr-str"‿{2∾<" "J PrStrR¨𝕩} "str"‿{2∾<""J PrStr¨𝕩} "prn"‿{•Out " "J PrStrR¨𝕩 ⋄ nil} @@ -54,6 +53,27 @@ ns ← ⟨ "rest"‿{𝕊 ⟨t‿⟨⟩⟩: 3‿⟨⟩; 𝕊 ⟨t‿xs⟩: 3∾<1↓xs; 3‿⟨⟩} "throw"‿{𝕊 ⟨x⟩: ¯1‿x} + + "apply"‿{𝕊 + fn ← {9‿f:f; 10‿b‿a‿e‿f: f}⊑𝕩 + args ← (¯1↓1↓𝕩)∾1⊑¯1⊑𝕩 + Fn args} + "map"‿{𝕊 + fn ← {9‿f:f; 10‿b‿a‿e‿f: f}⊑𝕩 + 3∾<{Fn ⟨𝕩⟩}¨ 1⊑1⊑𝕩} + + "nil?"‿{𝕊 ⟨8‿⟨⟩⟩: 7‿1; 7‿0} + "true?"‿{𝕊 ⟨7‿1⟩: 7‿1; 7‿0} + "false?"‿{𝕊 ⟨7‿0⟩: 7‿1; 7‿0} + "symbol?"‿{𝕊 ⟨0‿x⟩: 7‿1; 7‿0} + "keyword?"‿{𝕊 ⟨6‿x⟩: 7‿1; 7‿0} + "vector?"‿{𝕊 ⟨4‿x⟩: 7‿1; 7‿0} + "list?"‿{𝕊 ⟨3‿x⟩: 7‿1; 7‿0} + "map?"‿{𝕊 ⟨5‿x⟩: 7‿1; 7‿0} + "sequential?"‿{𝕊 ⟨3‿x⟩: 7‿1; 𝕊 ⟨4‿x⟩: 7‿1; 7‿0} + + "symbol"‿{0∾<1⊑⊑𝕩} + "keyword"‿{6∾<1⊑⊑𝕩} ⟩ core ⇐ Env@ diff --git a/impls/bqn/printer.bqn b/impls/bqn/printer.bqn index 36df170e4a..40090c26ee 100644 --- a/impls/bqn/printer.bqn +++ b/impls/bqn/printer.bqn @@ -23,6 +23,7 @@ P ← { ""∾𝕩 }¨(@∾s∾@); r 𝕊 1‿n: {n<0?"-";""}∾•Fmt |n; + r 𝕊 0‿x: x; "ERROR printing: "∾(@+10)∾•Fmt 𝕩 } From 29f12f96f94138cae215710f9f73b0a992da758e Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Tue, 21 Jun 2022 00:06:41 +0300 Subject: [PATCH 29/47] bqn: step9: support deep throws --- impls/bqn/core.bqn | 6 +++--- impls/bqn/globals.bqn | 2 ++ impls/bqn/step9_try.bqn | 24 +++++++++++------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 6c9639ccb9..85a317de5c 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -1,4 +1,4 @@ -⟨Atom,nil,Err⟩ ← •Import "globals.bqn" +⟨Atom,nil,Err,_chk,_chks⟩ ← •Import "globals.bqn" ⟨Env⟩ ← •Import "env.bqn" ⟨PrStr,PrStrR⟩ ← •Import "printer.bqn" ⟨ReadStr⟩ ← •Import "reader.bqn" @@ -57,10 +57,10 @@ ns ← ⟨ "apply"‿{𝕊 fn ← {9‿f:f; 10‿b‿a‿e‿f: f}⊑𝕩 args ← (¯1↓1↓𝕩)∾1⊑¯1⊑𝕩 - Fn args} + Fn _chks args} "map"‿{𝕊 fn ← {9‿f:f; 10‿b‿a‿e‿f: f}⊑𝕩 - 3∾<{Fn ⟨𝕩⟩}¨ 1⊑1⊑𝕩} + {3∾<𝕩}_chks{Fn ⟨𝕩⟩}¨ 1⊑1⊑𝕩} "nil?"‿{𝕊 ⟨8‿⟨⟩⟩: 7‿1; 7‿0} "true?"‿{𝕊 ⟨7‿1⟩: 7‿1; 7‿0} diff --git a/impls/bqn/globals.bqn b/impls/bqn/globals.bqn index 3136c053ae..e39c777bf9 100644 --- a/impls/bqn/globals.bqn +++ b/impls/bqn/globals.bqn @@ -5,3 +5,5 @@ Atom⇐ {𝕊 v: Deref ⇐ {𝕊⋄v} } Err ⇐ {¯1‿⟨2,𝕩⟩} +_chk ⇐ {¯1=⊑⎊0𝕩 ? 𝕩 ; 𝕨𝔽𝕩} +_chks ⇐ {⊑¯1∊⊑¨𝕩 ? (⊑(⊑¨𝕩)⊐¯1)⊑𝕩 ; 𝕨𝔽𝕩} diff --git a/impls/bqn/step9_try.bqn b/impls/bqn/step9_try.bqn index 6eaa117ffc..da30e4752a 100644 --- a/impls/bqn/step9_try.bqn +++ b/impls/bqn/step9_try.bqn @@ -1,4 +1,4 @@ -⟨Err,nil⟩ ← •Import "globals.bqn" +⟨Err,nil,_chk,_chks⟩ ← •Import "globals.bqn" ⟨Env⟩ ← •Import "env.bqn" ⟨ReadStr⟩ ← •Import "reader.bqn" ⟨PrStrR⟩ ← •Import "printer.bqn" @@ -74,11 +74,11 @@ EVAL ← {e 𝕊 ast: ast ↩ exp @; 𝕊 3‿⟨0‿"if",c,t,f⟩: - ast ↩ {8‿x:f; 7‿0:f; t} e EVAL c - @; + {ast ↩ {8‿x:f; 7‿0:f; t}𝕩 + @}_chk e EVAL c ; 𝕊 3‿⟨0‿"if",c,t⟩: - ast ↩ {8‿x:nil; 7‿0:nil; t} e EVAL c - @; + {ast ↩ {8‿x:nil; 7‿0:nil; t}𝕩 + @}_chk e EVAL c ; 𝕊 3‿⟨0‿"fn*",t‿args,exp⟩: 10‿exp‿args‿e‿{𝕊 xs: inner ← Env e @@ -100,26 +100,24 @@ EVAL ← {e 𝕊 ast: 𝕊 3‿⟨⟩: 3‿⟨⟩ ; 𝕊 3‿xs: { 0‿"do": - e⊸EVAL¨¯1↓1↓xs - ast ↩ ¯1⊑xs - @ + {𝕊 ⋄ ast ↩ ¯1⊑xs ⋄ @}_chks e⊸EVAL¨¯1↓1↓xs ; # function application ys ← e EvalAst 3‿xs f ← ⊑ys - {9‿f: F 1↓ys ; + {9‿f: F _chks 1↓ys ; 10‿exp‿args‿e2‿fn: ast ↩ exp e ↩ Env e2 - e SetParams args‿(1↓ys) - @; + {e SetParams args‿𝕩 ⋄ @}_chks 1↓ys ; ¯1‿x: f; Err "not a function: "∾PrStrR f} f ; e EvalAst 𝕩 }⊑xs ; # basic forms - 𝕊 4‿xs: 4∾ Date: Thu, 14 Jul 2022 12:06:04 +0300 Subject: [PATCH 30/47] bqn: implement basic hashmap functions TODO: add varargs support to dissoc and assoc --- impls/bqn/core.bqn | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 85a317de5c..52598e684e 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -20,11 +20,10 @@ ns ← ⟨ ">"‿{7∾∧´>´˘2↕1⊸⊑¨𝕩} ">="‿{7∾∧´≥´˘2↕1⊸⊑¨𝕩} - "list"‿{3‿𝕩} "empty?"‿{7∾0=≠1⊑⊑𝕩} "count"‿{1∾≠1⊑⊑𝕩} - "bqn-show"‿{•Show ⊑ 𝕩 ⋄ 𝕩} + "bqn-show"‿{•Show ⊑ 𝕩} "pr-str"‿{2∾<" "J PrStrR¨𝕩} "str"‿{2∾<""J PrStr¨𝕩} "prn"‿{•Out " "J PrStrR¨𝕩 ⋄ nil} @@ -74,6 +73,16 @@ ns ← ⟨ "symbol"‿{0∾<1⊑⊑𝕩} "keyword"‿{6∾<1⊑⊑𝕩} + "list"‿{3‿𝕩} + "vector"‿{4‿𝕩} + "hash-map"‿{5‿𝕩} + + "keys"‿{𝕊 ⟨5‿xs⟩: 3∾<⥊⊏˘∘‿2⥊xs} + "vals"‿{𝕊 ⟨5‿xs⟩: 3∾<⥊1⊏˘∘‿2⥊xs} + "get"‿{𝕊 ⟨5‿xs,k⟩: 1⊑@‿nil∾˜⥊(k⊸≡˘⊑˘)⊸/∘‿2⥊xs; nil} + "contains?"‿{𝕊 ⟨5‿xs,k⟩: 7∾×+´k⊸≡˘⊑˘∘‿2⥊xs} + "dissoc"‿{𝕊 ⟨5‿xs,k⟩: 5∾<⥊(k⊸≢˘⊑˘)⊸/∘‿2⥊xs} + "assoc"‿{𝕊 ⟨5‿xs,k,v⟩: 5∾ Date: Thu, 14 Jul 2022 12:25:07 +0300 Subject: [PATCH 31/47] bqn: support multi-argument dissoc and assoc --- impls/bqn/core.bqn | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 52598e684e..efa2069bef 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -81,8 +81,12 @@ ns ← ⟨ "vals"‿{𝕊 ⟨5‿xs⟩: 3∾<⥊1⊏˘∘‿2⥊xs} "get"‿{𝕊 ⟨5‿xs,k⟩: 1⊑@‿nil∾˜⥊(k⊸≡˘⊑˘)⊸/∘‿2⥊xs; nil} "contains?"‿{𝕊 ⟨5‿xs,k⟩: 7∾×+´k⊸≡˘⊑˘∘‿2⥊xs} - "dissoc"‿{𝕊 ⟨5‿xs,k⟩: 5∾<⥊(k⊸≢˘⊑˘)⊸/∘‿2⥊xs} - "assoc"‿{𝕊 ⟨5‿xs,k,v⟩: 5∾ Date: Tue, 19 Jul 2022 00:52:04 +0300 Subject: [PATCH 32/47] bqn: fix (keyword "string") --- impls/bqn/core.bqn | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index efa2069bef..8f11218679 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -72,7 +72,8 @@ ns ← ⟨ "sequential?"‿{𝕊 ⟨3‿x⟩: 7‿1; 𝕊 ⟨4‿x⟩: 7‿1; 7‿0} "symbol"‿{0∾<1⊑⊑𝕩} - "keyword"‿{6∾<1⊑⊑𝕩} + "keyword"‿{𝕊 ⟨6‿kw⟩: 6‿kw; + 𝕊 ⟨2‿s⟩: 6∾<':'∾s} "list"‿{3‿𝕩} "vector"‿{4‿𝕩} "hash-map"‿{5‿𝕩} From 7f4065bf909a5f2eb25d6e2f13daa188f64349c9 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Tue, 19 Jul 2022 01:06:40 +0300 Subject: [PATCH 33/47] bqn: stepA: implement readline --- impls/bqn/core.bqn | 7 ++ impls/bqn/stepA_mal.bqn | 138 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 impls/bqn/stepA_mal.bqn diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 8f11218679..9c4e1ed935 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -88,7 +88,14 @@ ns ← ⟨ "assoc"‿{𝕊 ⟨x⟩: x; ⟨t‿xs,k,v⟩ ← 3↑𝕩 𝕊 (3↓𝕩)∾˜<5∾ (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))" +Rep "(println (str ""Mal ["" *host-language* ""]""))" + +{ + •term.OutRaw "user> " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 +}@ From db7eb90b1068ad26a04c7350f15ccc3cf0badb3b Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Tue, 19 Jul 2022 01:24:08 +0300 Subject: [PATCH 34/47] bqn: stepA: add required functions, some NYI --- impls/bqn/core.bqn | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 9c4e1ed935..36c9a03999 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -70,6 +70,9 @@ ns ← ⟨ "list?"‿{𝕊 ⟨3‿x⟩: 7‿1; 7‿0} "map?"‿{𝕊 ⟨5‿x⟩: 7‿1; 7‿0} "sequential?"‿{𝕊 ⟨3‿x⟩: 7‿1; 𝕊 ⟨4‿x⟩: 7‿1; 7‿0} + "string?"‿{𝕊 ⟨2‿x⟩: 7‿1; 7‿0} + "number?"‿{𝕊 ⟨1‿x⟩: 7‿1; 7‿0} + "fn?"‿{7∾{9:1;10:1;0}⊑⊑𝕩} "symbol"‿{0∾<1⊑⊑𝕩} "keyword"‿{𝕊 ⟨6‿kw⟩: 6‿kw; @@ -93,6 +96,13 @@ ns ← ⟨ •term.OutRaw s in ← •GetLine 0 {in≡@ ? nil ; 2‿in}} + + "time-ms"‿{𝕊⋄1∾⌊1000וMonoTime@} + + "meta"‿(¯1‿"NYI") + "with-meta"‿(¯1‿"NYI") + "seq"‿(¯1‿"NYI") + "conj"‿(¯1‿"NYI") ⟩ core ⇐ Env@ From 6d63f01d72baf6c3b2fa6ff817ec243a64c03d83 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Wed, 27 Jul 2022 22:22:48 +0300 Subject: [PATCH 35/47] bqn: stepA: support loading file as CLI arg --- impls/bqn/stepA_mal.bqn | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/impls/bqn/stepA_mal.bqn b/impls/bqn/stepA_mal.bqn index e4d239cce1..77f3cf4fda 100644 --- a/impls/bqn/stepA_mal.bqn +++ b/impls/bqn/stepA_mal.bqn @@ -130,9 +130,13 @@ Rep "(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) "" nil) Rep "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))" Rep "(println (str ""Mal ["" *host-language* ""]""))" -{ - •term.OutRaw "user> " - line ← •GetLine 0 - •Out Rep line - 𝕊𝕩 -}@ +{0=≠•args ? + { + •term.OutRaw "user> " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 + }@ ; + Rep "(def! *ARGV* (list))" + Rep "(load-file """∾(⊑•args)∾""")" +} From a141f02976216d8051b76efc4a9391e2bb50715b Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Thu, 28 Jul 2022 00:35:40 +0300 Subject: [PATCH 36/47] =?UTF-8?q?bqn:=20rewrite=20tokenizer=20with=20?= =?UTF-8?q?=E2=80=A2=5Fwhile=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The MAL suggestion is to use a regex for tokenizing. But that's boring (and BQN doesn't have regex support yet). So I wrote a recursive tokenizer, but now self-hosting tokenizes long enough pieces of code to cause a stack overflow. So rewrite as a loop instead. --- impls/bqn/reader.bqn | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index 3d2a3d7bdc..5b27a48105 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -28,13 +28,13 @@ Reader ← {𝕊 tokens: } T ← {𝕊 s: - { - 0=≠s ? ⟨⟩ ; + v←⟨⟩ + {𝕊 @=⊑s ? •Exit 0 ; - '@'=⊑s ? (<"@") ∾ T 1↓s ; - "~@"≡2↑s ? (<"~@") ∾ T 2↓s ; - ⊑(⊑s)∊" ,"∾@+10 ? T 1↓s ; - ⊑(⊑s)∊"[]{}()'`~" ? (<≍⊑s) ∾ T 1↓s ; + '@'=⊑s ? v∾↩<"@" ⋄ s↩1↓s ; + "~@"≡2↑s ? v∾↩<"~@" ⋄ s↩2↓s ; + ⊑(⊑s)∊" ,"∾@+10 ? s↩1↓s ; + ⊑(⊑s)∊"[]{}()'`~" ? v∾↩<≍⊑s ⋄ s↩1↓s ; ';'=⊑s ? o←⟨⟩ {𝕊 @@ -42,8 +42,8 @@ T ← {𝕊 s: s ↩ 1↓s {0=≠s ? 0 ; (⊑s) ≠ @+10} } •_while_⊢1 - ( Date: Thu, 28 Jul 2022 12:12:43 +0300 Subject: [PATCH 37/47] bqn: fix some early-step regressions --- impls/bqn/step1_read_print.bqn | 4 ++-- impls/bqn/step2_eval.bqn | 4 ++-- impls/bqn/step4_if_fn_do.bqn | 5 ++++- impls/bqn/step5_tco.bqn | 5 ++++- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/impls/bqn/step1_read_print.bqn b/impls/bqn/step1_read_print.bqn index 7b67b5ad56..e30134b0c4 100644 --- a/impls/bqn/step1_read_print.bqn +++ b/impls/bqn/step1_read_print.bqn @@ -1,9 +1,9 @@ ⟨ReadStr⟩ ← •Import "reader.bqn" -⟨PrStr⟩ ← •Import "printer.bqn" +⟨PrStrR⟩ ← •Import "printer.bqn" READ ← ReadStr EVAL ← ⊢ -PRINT ← PrStr +PRINT ← PrStrR Rep ← PRINT∘EVAL∘READ { diff --git a/impls/bqn/step2_eval.bqn b/impls/bqn/step2_eval.bqn index 2400cbc548..6f227d59d3 100644 --- a/impls/bqn/step2_eval.bqn +++ b/impls/bqn/step2_eval.bqn @@ -1,5 +1,5 @@ ⟨ReadStr⟩ ← •Import "reader.bqn" -⟨PrStr⟩ ← •Import "printer.bqn" +⟨PrStrR⟩ ← •Import "printer.bqn" replEnv ← >⟨ "+"‿1‿(+´) @@ -18,7 +18,7 @@ EvalAst ← {env 𝕊 t‿v: } READ ← ReadStr -PRINT ← PrStr +PRINT ← PrStrR EVAL ← { env 𝕊 3‿⟨⟩: 3‿⟨⟩ ; env 𝕊 3‿xs: { diff --git a/impls/bqn/step4_if_fn_do.bqn b/impls/bqn/step4_if_fn_do.bqn index d64801c4ca..d95d778cb5 100644 --- a/impls/bqn/step4_if_fn_do.bqn +++ b/impls/bqn/step4_if_fn_do.bqn @@ -1,5 +1,6 @@ +⟨nil⟩ ← •Import "globals.bqn" ⟨Env⟩ ← •Import "env.bqn" -⟨ReadStr,nil⟩ ← •Import "reader.bqn" +⟨ReadStr⟩ ← •Import "reader.bqn" ⟨PrStrR⟩ ← •Import "printer.bqn" ⟨core⟩ ← •Import "core.bqn" @@ -59,6 +60,8 @@ EVAL ← { } Rep ← PRINT∘{core EVAL 𝕩}∘READ +Rep "(def! not (fn* (a) (if a false true)))" + { •Out "user> " line ← •GetLine 0 diff --git a/impls/bqn/step5_tco.bqn b/impls/bqn/step5_tco.bqn index b7ba499089..08172c8c82 100644 --- a/impls/bqn/step5_tco.bqn +++ b/impls/bqn/step5_tco.bqn @@ -1,5 +1,6 @@ +⟨nil⟩ ← •Import "globals.bqn" ⟨Env⟩ ← •Import "env.bqn" -⟨ReadStr,nil⟩ ← •Import "reader.bqn" +⟨ReadStr⟩ ← •Import "reader.bqn" ⟨PrStrR⟩ ← •Import "printer.bqn" ⟨core⟩ ← •Import "core.bqn" @@ -78,6 +79,8 @@ EVAL ← {e 𝕊 ast: Rep ← PRINT∘{core EVAL 𝕩}∘READ +Rep "(def! not (fn* (a) (if a false true)))" + { •Out "user> " line ← •GetLine 0 From 734692627a765dfddab51945b3f23623f1981542 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Thu, 28 Jul 2022 14:15:32 +0300 Subject: [PATCH 38/47] bqn: full *ARGV* support in stepA and step6 --- impls/bqn/core.bqn | 1 + impls/bqn/step6_file.bqn | 16 ++++++++++------ impls/bqn/stepA_mal.bqn | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 36c9a03999..3771a00650 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -109,3 +109,4 @@ core ⇐ Env@ {𝕊 kw‿f: core.Set kw‿(9‿f)}¨ns core.Set "*host-language*"‿(2‿"BQN") +core.Set "*ARGV*"‿⟨3,⟨⟩⟩ diff --git a/impls/bqn/step6_file.bqn b/impls/bqn/step6_file.bqn index 6967c0ce67..5a6fbe5ffd 100644 --- a/impls/bqn/step6_file.bqn +++ b/impls/bqn/step6_file.bqn @@ -82,9 +82,13 @@ Rep ← PRINT∘{core EVAL 𝕩}∘READ core.Set "eval"‿(9‿{core EVAL ⊑𝕩}) Rep "(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) "" nil)"")))))" -{ - •Out "user> " - line ← •GetLine 0 - •Out Rep line - 𝕊𝕩 -}@ +{0=≠•args ? + { + •term.OutRaw "user> " + line ← •GetLine 0 + •Out Rep line + 𝕊𝕩 + }@ ; + core.Set "*ARGV*"‿(3∾<2∾⟜<¨1↓•args) + Rep "(load-file """∾(⊑•args)∾""")" +} diff --git a/impls/bqn/stepA_mal.bqn b/impls/bqn/stepA_mal.bqn index 77f3cf4fda..e585a4fabc 100644 --- a/impls/bqn/stepA_mal.bqn +++ b/impls/bqn/stepA_mal.bqn @@ -137,6 +137,6 @@ Rep "(println (str ""Mal ["" *host-language* ""]""))" •Out Rep line 𝕊𝕩 }@ ; - Rep "(def! *ARGV* (list))" + core.Set "*ARGV*"‿(3∾<2∾⟜<¨1↓•args) Rep "(load-file """∾(⊑•args)∾""")" } From fb6a77a696432ae045d82078c80966e1adb14327 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Thu, 28 Jul 2022 14:40:18 +0300 Subject: [PATCH 39/47] bqn: implement list/vector equality --- impls/bqn/core.bqn | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 3771a00650..1cc07f4668 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -7,6 +7,11 @@ Q ← {∾´'"'∾𝕩∾'"'} J ← {0=≠𝕩 ? "" ; 1=≠𝕩 ? ⊑𝕩 ; ∾´(⊑𝕩)∾𝕨∾𝕨J 1↓𝕩} +E ← {𝕊 ⟨t‿a,s‿b⟩: + {(⊑t∊3‿4) ∧ ⊑s∊3‿4 ? + {(≠a)=≠b ? ∧´a E¨b ; 0}; + (t≡s)∧a≡b} ; + {2<≠𝕩 ? ∧´E´˘2↕𝕩 ; 1}𝕩} ns ← ⟨ "+"‿{1∾+´1⊏˘>𝕩} @@ -14,7 +19,7 @@ ns ← ⟨ "*"‿{1∾×´1⊏˘>𝕩} "/"‿{1∾⌊∘÷´1⊏˘>𝕩} - "="‿{7∾∧´≡´˘2↕𝕩} + "="‿{7∾E𝕩} "<"‿{7∾∧´<´˘2↕1⊸⊑¨𝕩} "<="‿{7∾∧´≤´˘2↕1⊸⊑¨𝕩} ">"‿{7∾∧´>´˘2↕1⊸⊑¨𝕩} From b0497fb44597a68a8ba571c443392bc1980d5426 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Thu, 28 Jul 2022 14:51:17 +0300 Subject: [PATCH 40/47] bqn: fix reading symbols starting with - --- impls/bqn/reader.bqn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index 5b27a48105..0036108e40 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -97,7 +97,7 @@ Atom ← { ':': 6‿a ; ';': @ ; ⊑𝕩∊nums ? 1∾Int a ; - (𝕩='-')∧1<≠a ? 1∾-Int 1↓a ; + (∧´(1↓a)∊nums)∧(𝕩='-')∧1<≠a ? 1∾-Int 1↓a ; 0‿a }⊑a } From e0e5a5e4c094bcdc81d254bc492c920ca573e277 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Thu, 28 Jul 2022 15:16:35 +0300 Subject: [PATCH 41/47] bqn: fix reading 1-length strings --- impls/bqn/reader.bqn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index 0036108e40..50ba6cc105 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -70,7 +70,7 @@ T ← {𝕊 s: ReadString ← { 𝕊 "": ""; - 𝕊 ⟨c⟩: c; + 𝕊 ⟨c⟩: ⟨c⟩; 𝕊 s: { '\'≠⊑s ? (⊑s)∾ReadString 1↓s ; ({ From 884208e67f3b6bac89ede5391421ac578ec8fe10 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Thu, 28 Jul 2022 15:21:06 +0300 Subject: [PATCH 42/47] bqn: implement vec --- impls/bqn/core.bqn | 1 + 1 file changed, 1 insertion(+) diff --git a/impls/bqn/core.bqn b/impls/bqn/core.bqn index 1cc07f4668..34741457ab 100644 --- a/impls/bqn/core.bqn +++ b/impls/bqn/core.bqn @@ -85,6 +85,7 @@ ns ← ⟨ "list"‿{3‿𝕩} "vector"‿{4‿𝕩} "hash-map"‿{5‿𝕩} + "vec"‿{𝕊 ⟨4‿x⟩: 4‿x; 𝕊 ⟨3‿x⟩: 4‿x} "keys"‿{𝕊 ⟨5‿xs⟩: 3∾<⥊⊏˘∘‿2⥊xs} "vals"‿{𝕊 ⟨5‿xs⟩: 3∾<⥊1⊏˘∘‿2⥊xs} From 158cc72049c2dae45000eed92c486e3a15558528 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Thu, 28 Jul 2022 15:21:06 +0300 Subject: [PATCH 43/47] bqn: fix REPL print --- impls/bqn/stepA_mal.bqn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/impls/bqn/stepA_mal.bqn b/impls/bqn/stepA_mal.bqn index e585a4fabc..884be542b4 100644 --- a/impls/bqn/stepA_mal.bqn +++ b/impls/bqn/stepA_mal.bqn @@ -128,9 +128,9 @@ core.Set "eval"‿(9‿{core EVAL ⊑𝕩}) Rep "(def! not (fn* (a) (if a false true)))" Rep "(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) "" nil)"")))))" Rep "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))" -Rep "(println (str ""Mal ["" *host-language* ""]""))" {0=≠•args ? + Rep "(println (str ""Mal ["" *host-language* ""]""))" { •term.OutRaw "user> " line ← •GetLine 0 From 43c9358fb36087b4716019039c40c719474280f4 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sat, 30 Jul 2022 16:25:05 +0300 Subject: [PATCH 44/47] bqn: add Dockerfile --- impls/bqn/Dockerfile | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 impls/bqn/Dockerfile diff --git a/impls/bqn/Dockerfile b/impls/bqn/Dockerfile new file mode 100644 index 0000000000..068d8622bf --- /dev/null +++ b/impls/bqn/Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:22.04 + +RUN apt-get update && \ + apt-get install -qqy git clang build-essential python3 python-is-python3 +RUN git clone https://github.com/dzaima/CBQN.git && \ + cd CBQN && \ + git reset --hard 88f65850fa6ac28bc50886c5942652f21d5be924 && \ + make && \ + make install && \ + ln -s bqn /usr/local/bin/BQN +RUN mkdir /mal +WORKDIR /mal From 4daedefdff53884a859b5f569ae94d131ca82e46 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sat, 30 Jul 2022 16:33:24 +0300 Subject: [PATCH 45/47] bqn: support ci.sh --- impls/bqn/Makefile | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 impls/bqn/Makefile diff --git a/impls/bqn/Makefile b/impls/bqn/Makefile new file mode 100644 index 0000000000..7af3113c71 --- /dev/null +++ b/impls/bqn/Makefile @@ -0,0 +1,3 @@ +all: + +clean: From 61dfddf7228a48d03d2b7c50c399172d3309ca6c Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sat, 30 Jul 2022 17:12:52 +0300 Subject: [PATCH 46/47] bqn: update README --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 3fa1c13df2..01a5a1d3d9 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ FAQ](docs/FAQ.md) where I attempt to answer some common questions. | [Bash 4](#bash-4) | [Joel Martin](https://github.com/kanaka) | | [BASIC](#basic-c64-and-qbasic) (C64 & QBasic) | [Joel Martin](https://github.com/kanaka) | | [BBC BASIC V](#bbc-basic-v) | [Ben Harris](https://github.com/bjh21) | +| [BQN](#bqn) | [Hannu Hartikainen](https://github.com/dancek) | | [C](#c) | [Joel Martin](https://github.com/kanaka) | | [C #2](#c2) | [Duncan Watts](https://github.com/fungiblecog) | | [C++](#c-1) | [Stephen Thirlwall](https://github.com/sdt) | @@ -289,6 +290,17 @@ Or in ARM BBC BASIC V under RISC OS 3 or later: *Run stepX_YYY ``` +### BQN + +The BQN implementation requires [CBQN](https://github.com/dzaima/CBQN) as the non-standard system values `•_while_`, `•GetLine` and `•term.OutRaw` are used. Removing them would be easy, though -- they're just for convenience and REPL usability. + +There are no releases of CBQN at the time of writing, but any version from mid 2022 should work. Specifically, commit `88f6585` is known to work. + +``` +cd impls/bqn +./run +``` + ### C The C implementation of mal requires the following libraries (lib and From 2cb51270d92c377eb091895a9cec64f18c01f316 Mon Sep 17 00:00:00 2001 From: Hannu Hartikainen Date: Sat, 30 Jul 2022 17:29:16 +0300 Subject: [PATCH 47/47] bqn: rewrite ReadString (thanks Marshall & suhr!) --- impls/bqn/reader.bqn | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/impls/bqn/reader.bqn b/impls/bqn/reader.bqn index 50ba6cc105..dd757fa6b6 100644 --- a/impls/bqn/reader.bqn +++ b/impls/bqn/reader.bqn @@ -68,23 +68,17 @@ T ← {𝕊 s: v } -ReadString ← { - 𝕊 "": ""; - 𝕊 ⟨c⟩: ⟨c⟩; - 𝕊 s: { - '\'≠⊑s ? (⊑s)∾ReadString 1↓s ; - ({ - # NOTE: doesn't support octal etc. - 'a': @+7; - 'b': @+8; - 'e': @+27; - 'f': @+12; - 'n': @+10; - 'r': @+13; - 't': @+9; - 'v': @+11; - 𝕩 - }1⊑s) ∾ ReadString 2↓s} +# Thanks to Marshall for writing this version of readString and +# suhr for suggesting the `out` value of Ctrl-* characters! (My +# version was verbose functional code while this is array style.) +readString ← { + in ← "abefnrtv" + out ← "GH[LJMIK"-64 + diff← (out-in)∾0 + { + m ← » bs ← <`𝕩='\' + (¬bs) / (diff⊏˜in⊸⊐)⊸+⌾(m⊸/) 𝕩 + } } Int ← {10⊸×⊸+˜´'0'-˜⌽𝕩}