aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2021-11-01 20:08:39 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2021-11-01 20:08:39 -0400
commit1b4975d31806f045d966109d96bdffb1da0f3259 (patch)
tree65d289006d074f5590791a82dfe8141e0e623b48
parentc601aa5d2d49f572ddd4240bf1c7bbca12d7b7a6 (diff)
Add a new bqn.bqn that uses the VM
-rwxr-xr-xbqn.bqn60
-rw-r--r--docs/running.html2
-rw-r--r--running.md2
3 files changed, 62 insertions, 2 deletions
diff --git a/bqn.bqn b/bqn.bqn
new file mode 100755
index 00000000..086907e2
--- /dev/null
+++ b/bqn.bqn
@@ -0,0 +1,60 @@
+#! /usr/bin/env bqn
+
+# A BQN interpreter implemented in BQN
+# ./bqn.bqn ( files | -e source )
+
+# Unlike a compiler, a self-hosted interpreter is kind of silly. This
+# script is mostly just evidence that the BQN components found in this
+# repository do add up to a full implementation. The same idea more
+# usefully applied in test/unit.bqn, which can build an interpreter
+# using some or all of them in order to test it.
+
+# BQN's compiler is truly self-hosting, but targets a BQN-specific
+# object code rather than machine code (as does Java). The virtual
+# machine interprets this object code. Finally, the compiler takes
+# primitive values as input in order to include them in the object code.
+# As all primitives are available in BQN we could just use these, but
+# for more self-hostingness the BQN-based runtime is used instead, so
+# that the interpreter relies on a smaller set of core functions.
+
+#⌜
+# Assemble the components
+gl ← •Import "src/glyphs.bqn"
+compile ← gl •Import "src/c.bqn" # Compiler: source → object code
+vm ← •Import "vm.bqn" # VM: interprets object code
+runtime ← •Import "rt.bqn" # Runtime: primitives available to VM
+
+# Define system values later, to include BQN
+BQN ← ⟨runtime, {System 𝕩}⟩ ⊸ (VM Compile)
+
+#⌜
+# System values
+IsPrim ← ∊⟜runtime⌾<
+Glyph ← runtime⊸⊐⌾<⊑(∾gl)˙
+Decompose ← IsPrim◶⟨•Decompose,0⊸≍⟩
+
+# Formatter
+tn ← "*"⊸(∾∾⊣)¨"array"‿"function"‿"1-modifier"‿"2-modifier"‿"namespace"
+Fmt‿Repr ← (•Import "src/f.bqn"){𝔽} ⟨
+ •Type
+ Decompose
+ IsPrim◶⟨tn⊑˜•Type-2˙, Glyph⟩ # Format operation/namespace
+ •Repr # Format number
+⟩
+
+# Lookup table
+FindSys ← {
+ i ← 𝕨⊐𝕩
+ { ! ∾⟨"Unknown system value",(1≠≠𝕩)/"s",":"⟩∾" •"⊸∾¨𝕩 }∘/⟜𝕩⍟(∨´) i=≠𝕨
+ i
+}
+system ← {𝕨⊸FindSys⊏𝕩˙}´⟨
+ "bqn"‿"type"‿"glyph"‿"decompose"‿"repr"‿"fmt"‿"out"‿"show"
+ BQN ‿•Type ‿ Glyph ‿ Decompose ‿ Repr ‿ Fmt ‿•Out ‿(•Out Fmt)
+⟩
+
+#⌜
+# Evaluate:
+(BQN¨ ("-e"≡⊑)◶⟨•file.Chars¨, 1⊸↓⟩)⍟(0<≠) •args
+
+BQN # Return the evaluator so it can be •Include d from BQN
diff --git a/docs/running.html b/docs/running.html
index fc91a006..02e43805 100644
--- a/docs/running.html
+++ b/docs/running.html
@@ -14,7 +14,7 @@
<ul>
<li>Javascript, in this repository. Slow (compiles at ~5kB/s) but usable.</li>
<li><a href="https://github.com/dzaima/CBQN">C</a>, targetting high performance. Some parts are fast, some are not.</li>
-<li>BQN (<a href="https://github.com/mlochbaum/BQN/blob/master/vm.bqn">vm.bqn</a>), for testing the compiler easily.</li>
+<li>BQN (<a href="https://github.com/mlochbaum/BQN/blob/master/bqn.bqn">bqn.bqn</a>), for testing the compiler easily.</li>
<li><a href="https://github.com/cannadayr/ebqn">Erlang</a>, intended for embedding. Too slow to be practical; a <a href="https://github.com/cannadayr/ebqn-rs/">Rust version</a> is in progress to fix this.</li>
</ul>
<h4 id="javascript"><a class="header" href="#javascript">Javascript</a></h4>
diff --git a/running.md b/running.md
index 9582ce53..013836d7 100644
--- a/running.md
+++ b/running.md
@@ -15,7 +15,7 @@ This version of BQN is [implemented](implementation/README.md) mainly in BQN its
Support in the following languages has been implemented:
- Javascript, in this repository. Slow (compiles at ~5kB/s) but usable.
- [C](https://github.com/dzaima/CBQN), targetting high performance. Some parts are fast, some are not.
-- BQN ([vm.bqn](vm.bqn)), for testing the compiler easily.
+- BQN ([bqn.bqn](bqn.bqn)), for testing the compiler easily.
- [Erlang](https://github.com/cannadayr/ebqn), intended for embedding. Too slow to be practical; a [Rust version](https://github.com/cannadayr/ebqn-rs/) is in progress to fix this.
#### Javascript