From 1b4975d31806f045d966109d96bdffb1da0f3259 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Mon, 1 Nov 2021 20:08:39 -0400 Subject: Add a new bqn.bqn that uses the VM --- bqn.bqn | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ docs/running.html | 2 +- running.md | 2 +- 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100755 bqn.bqn 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 @@

Javascript

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 -- cgit v1.2.3