diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-11-01 20:08:39 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-11-01 20:08:39 -0400 |
| commit | 1b4975d31806f045d966109d96bdffb1da0f3259 (patch) | |
| tree | 65d289006d074f5590791a82dfe8141e0e623b48 /bqn.bqn | |
| parent | c601aa5d2d49f572ddd4240bf1c7bbca12d7b7a6 (diff) | |
Add a new bqn.bqn that uses the VM
Diffstat (limited to 'bqn.bqn')
| -rwxr-xr-x | bqn.bqn | 60 |
1 files changed, 60 insertions, 0 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 |
