ReBQN

The function •BQN evaluates a source code string passed to it as BQN in an isolated environment and returns the result. There are a few things you might want to change about this. •ReBQN is a function that generates •BQN-like functions, using a namespace 𝕩 for configuration. It can create a "REPL" function that saves state between evaluations, or redefine primitives, and might support extra functionality like keywords in the future.

Here are some quick examples of •ReBQN functionality. First, let's make a REPL function by setting repl to "strict" (no re-definitions allowed) instead of the default "none". We can now define a variable in one Repl call and refer to it later.

↗️
    repl  •ReBQN {repl"strict"}  # Lowercase for function result

    Repl "a ← ↕10"                 # Uppercase to call
⟨ 0 1 2 3 4 5 6 7 8 9 ⟩

    Repl "⌽ a"
⟨ 9 8 7 6 5 4 3 2 1 0 ⟩

This option also powers the results shown on the BQN website: to process a document, a REPL function is created, and used to evaluate every line! Next, we can define a little ASCII calculator using a list of primitives. To mix it up a bit I've made monadic / here a Range function.

↗️
    calcFns  '+'+, '-'-, '*'×, '/'(÷)

    calc  •ReBQN {primitivescalcFns}

    Calc "7 / 9 - 2*2"  # BQN order of operations
1.4

    Calc "/ 9"
⟨ 0 1 2 3 4 5 6 7 8 ⟩

Options can be used in any combination. Here's a calculator REPL:

↗️
    calcRepl  •ReBQN {repl"strict", primitivescalcFns}

    CalcRepl "b ← 1 - a←6"
¯5

    CalcRepl "a * b"
¯30

REPL mode

The repl property can have the values "none", "strict", and "loose". If no value is given it's equivalent to "none", which means that the resulting function has no memory and each evaluation is independent from the others. But the values "strict" and "loose" make evaluations take place in a shared scope. Now a variable defined at the top level of one source string is visible when later ones are evaluated, and can be viewed and modified.

↗️
    do  •ReBQN {repl"loose"}

    Do¨ "a←4""⟨a,b←5⟩""{⟨a↩𝕩,b⟩}8"
⟨ 4 ⟨ 4 5 ⟩ ⟨ 8 5 ⟩ ⟩

A different •ReBQN result has its own scope and can't access these variables.

↗️
    doNot  •ReBQN {repl"loose"}

    DoNot "b" # surprised when this fails
Error: Undefined identifier

The difference in "strict" and "loose" is that a loose REPL can define a variable again, which just changes its value (under the covers, the is treated as a ).

↗️
    Do "a ← ¯1"
¯1
    Do "a ← b‿a"
⟨ 5 ¯1 ⟩

    (•ReBQN {repl"strict"})@¨ "a←1""a←2"  # Second one errors
⟨ 1 @ ⟩

Primitives

The primitives property specifies the full set of primitives available to the new interpreter. Without it, the existing set of primitives is kept. With it, they're all thrown out, and the ones provided are used instead. Since you often would rather extend or modify what's there, the system value •primitives returns the current primitives set in the form used by •ReBQN.

↗️
    3  •primitives
⟨ ⟨ '+' + ⟩ ⟨ '-' - ⟩ ⟨ '×' × ⟩ ⟩

    ext  •ReBQN {primitives•primitives'∑',+´'∏',×´}

    Ext "∏1+↕7"
5040
    Ext "∑(1↓↕)⊸(⊣/˜0=|)28"  # Sum of divisors
28

Appending to •primitives means everything from BQN's there in addition to the given functions.

What's allowed? The format for primitives is a list of pairs, where each pair contains one character—the glyph—and one function or modifier. A primitive can't be data, at least at present, because the compiler is only designed to handle the primitive types found in base BQN and extending this doesn't seem terribly important. The type of the primitive's value is very important, because it determines the role it has. Said another way, it's setting the rules of syntax!

↗️
    (•ReBQN {primitives'^'{𝕨𝔽𝕩𝔽𝕨},'%'}){𝔽} "0 %^ 1‿2"
⟨ 0 1 2 0 ⟩

Above, ^ becomes a 1-modifier, so that it modifies % rather than being called directly on 12 as a function.

The glyph can be any character that's not being used by BQN already. Characters like c or or : will result in an error, as they'd break BQN syntax. Other than that, the sky's the limit! Or rather, the Unicode consortium is the limit. If they don't recognize your symbol, you're going to have to petition to make it an emoji or something. Oh well.

Run as an executable

Of course, calling a function on a string isn't a great interface for programming. But BQN has the facilities to turn such a function into a proper script, which can then be run much like the BQN executable. The following example takes a filename followed by the •args to be passed in, or runs as a REPL if given no arguments.

#! /usr/bin/env bqn
calcFns  '+'+, '-'-, '*'×, '/'(÷)
calc  •ReBQN {primitivescalcFns, repl"loose"}
{
  # Run on a file
  0 < •args ?
    f  •wdpath •file.At •args
    pathname  (`'/')f
    path,name,1•args Calc •file.Chars f

  # Run as REPL
  ; _while_  {𝔽𝔾𝔽_𝕣_𝔾𝔽𝔾𝕩}
    •GetLine•ShowCalc _while_ (@) •GetLine@
}