From 99f37de695119fd643b36f373771bcdcc4a626af Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sat, 30 Jan 2021 20:22:47 -0500 Subject: Move direct command-line execution from test/js to docs/bqn.js --- docs/bqn.js | 12 ++++++++++++ docs/running.html | 4 ++-- running.md | 4 ++-- test/README.txt | 10 +++++----- test/js | 41 +++++++++++++++-------------------------- 5 files changed, 36 insertions(+), 35 deletions(-) mode change 100644 => 100755 docs/bqn.js diff --git a/docs/bqn.js b/docs/bqn.js old mode 100644 new mode 100755 index 406c20f8..c3c2d1ba --- a/docs/bqn.js +++ b/docs/bqn.js @@ -1,3 +1,5 @@ +#! /usr/bin/env node + "use strict"; // Virtual machine let has = x => x!==undefined; @@ -286,4 +288,14 @@ let fmtErr = (s,e) => { if (typeof module!=='undefined') { bqn.fmt=fmt; bqn.fmtErr=fmtErr; bqn.compile=compile; bqn.run=run; module.exports=bqn; + if (!module.parent) { + let args = process.argv.slice(2); + args.map(a=>{ + try { + console.log(fmt(bqn(a))) + } catch(e) { + console.error(''+fmtErr(Array.from(a),e)+''); + } + }); + } } diff --git a/docs/running.html b/docs/running.html index 45695dbc..bb1b1fe3 100644 --- a/docs/running.html +++ b/docs/running.html @@ -7,8 +7,8 @@

How to run BQN

There are currently two active BQN implementations: the self-hosted one in this repository, and the independent dzaima/BQN. Neither is entirely complete but they are quite capable for pure programming tasks (say, implementing a compiler). For scripting, only dzaima/BQN has the required I/O such as file functions. I tend to develop parts of applications in the online REPL and move to dzaima/BQN scripts in order to run them.

BQN

-

The online REPL is here. The file docs/bqn.js is zero-dependency Javascript and can be loaded from Node.js (it's tested this way), but there isn't any convenient way to use it offline right now.

-

This repository contains a version of BQN implemented mainly in BQN itself—the compiler is entirely self-hosted, while the runtime is built from a small number of starting functions using preprocessed BQN. This version supports all primitives, except that it doesn't have full support for inferred properties. It's still missing syntax support for function headers or multiple bodies. The Javascript-based compiler is also slow, taking about 0.05 seconds plus 1 second per kilobyte of source (this is purely due to the slow runtime, as dzaima+reference achieves 1ms/kB with the same compiler once warmed up).

+

The online REPL is here. The file docs/bqn.js is zero-dependency Javascript, and can be loaded from HTML or Node.js. It can also be called directly from the command line (using Node); in this case each argument is evaluated as BQN code and the result is printed.

+

The version of BQN in this repository is implemented mainly in BQN itself—the compiler is entirely self-hosted, while the runtime is built from a small number of starting functions using preprocessed BQN. It completely supports the core language except for block headers and multiple body syntax, and a few cases of structural Under (). The Javascript-based compiler is also slow, taking about 0.05 seconds plus 1 second per kilobyte of source (this is purely due to the slow runtime, as dzaima+reference achieves 1ms/kB with the same compiler once warmed up).

Because self-hosted BQN requires only a simple virtual machine to run, it is fairly easy to embed it in another programming language by implementing this virtual machine. The way data is represented is part of the VM implementation: it can use native arrays or a custom data structure, depending on what the language supports. An initial implementation will be very slow, but can be improved by replacing functions from the BQN-based runtime with native code. As the VM system can be hard to work with if you're not familiar with it, I advise you to contact me to discuss this option it you are interested.

dzaima/BQN

dzaima/BQN is an implementation in Java created by modifying the existing dzaima/APL. It should be easy to run on desktop Linux and Android. It is still in development and has almost complete syntax support but incomplete primitive support.

diff --git a/running.md b/running.md index 7d9f027a..843c0930 100644 --- a/running.md +++ b/running.md @@ -6,9 +6,9 @@ There are currently two active BQN implementations: the self-hosted one in this ### BQN -The online REPL is [here](https://mlochbaum.github.io/BQN/try.html). The file [docs/bqn.js](docs/bqn.js) is zero-dependency Javascript and can be loaded from Node.js (it's tested this way), but there isn't any convenient way to use it offline right now. +The online REPL is [here](https://mlochbaum.github.io/BQN/try.html). The file [docs/bqn.js](docs/bqn.js) is zero-dependency Javascript, and can be loaded from HTML or Node.js. It can also be called directly from the command line (using Node); in this case each argument is evaluated as BQN code and the result is printed. -This repository contains a version of BQN implemented mainly in BQN itself—the compiler is entirely self-hosted, while the runtime is built from a small number of starting functions using preprocessed BQN. This version supports all primitives, except that it doesn't have full support for [inferred](spec/inferred.md) properties. It's still missing syntax support for function headers or multiple bodies. The Javascript-based compiler is also slow, taking about 0.05 seconds plus 1 second per kilobyte of source (this is purely due to the slow runtime, as dzaima+reference achieves 1ms/kB with the same compiler once warmed up). +The version of BQN in this repository is implemented mainly in BQN itself—the compiler is entirely self-hosted, while the runtime is built from a small number of starting functions using preprocessed BQN. It completely supports the core language except for block headers and multiple body syntax, and a few cases of structural Under (`⌾`). The Javascript-based compiler is also slow, taking about 0.05 seconds plus 1 second per kilobyte of source (this is purely due to the slow runtime, as dzaima+reference achieves 1ms/kB with the same compiler once warmed up). Because self-hosted BQN requires only a simple virtual machine to run, it is [fairly easy](implementation/vm.md) to embed it in another programming language by implementing this virtual machine. The way data is represented is part of the VM implementation: it can use native arrays or a custom data structure, depending on what the language supports. An initial implementation will be very slow, but can be improved by replacing functions from the BQN-based runtime with native code. As the VM system can be hard to work with if you're not familiar with it, I advise you to contact me to discuss this option it you are interested. diff --git a/test/README.txt b/test/README.txt index 22a601b4..b61cf09d 100644 --- a/test/README.txt +++ b/test/README.txt @@ -1,12 +1,12 @@ Test scripts: Script Compiler host Output host/VM -- js [-prim] Self-host Javascript -- dz_comp [-prim] dzaima/BQN dzaima/BQN +- js Self-host Javascript +- dz_comp dzaima/BQN dzaima/BQN - dz_wasm.js dzaima/BQN WebAssembly dz_comp uses the self-hosted compiler ../src/c.bqn by default but not -the runtime ../src/r.bqn. Pass -rt to test with the runtime, and -comp +the runtime ../src/r.bqn. Pass -rt to test with the runtime, and -nocomp to test dzaima/BQN only (this doesn't pass as of the time of writing). Test cases (cases/): @@ -15,8 +15,8 @@ Test cases (cases/): - syntax.bqn * * - prim.bqn * -dz_comp can be run on a different set of tests by passing their names -as arguments; for js the argument "--" is needed, then the names. +js or dz_comp can be run on a specified set of tests by passing the test +names as arguments. Contents of bin/dbqn follow (3 lines). Replace "/path/to/dzaima/" with your path. diff --git a/test/js b/test/js index c2967328..d3aa70d2 100755 --- a/test/js +++ b/test/js @@ -6,31 +6,20 @@ let cases = f=>read(__dirname+'/cases/'+f+'.bqn','utf8').split('\n') .filter(x=>x.length>0 && x.charAt(0)!=='#') .map(x=>x.indexOf('%')===-1?[1,x]:x.split(' % ')); let args = process.argv.slice(2); -let onfiles = args[0]==='--'; -let files = onfiles ? args.slice(1) : ['simple','syntax']; +let files = args.length ? args : ['simple','syntax']; -if (onfiles || args.length==0) { - let t = [].concat.apply([],files.map(cases)); - let test = t.map(e=>e[1]); - let expt = t.map(e=>e[0]==='!'?null:+e[0]); +let t = [].concat.apply([],files.map(cases)); +let test = t.map(e=>e[1]); +let expt = t.map(e=>e[0]==='!'?null:+e[0]); - let rslt = test.map(t => {try{return bqn(t);}catch(e){return null;}}); - let pass = rslt.map((r,i)=>r===expt[i]); - let fail = pass.map((p,i)=>p?-1:i).filter(i=>i>=0); - console.log( - fail.length - ? fail.map(i=>'"'+test[i]+'": expected ' - +(e=>e===null?'to fail':e)(expt[i]) - +' but ' - +(r=>r===null?'evaluation failed':'received '+r)(rslt[i])) - : "All "+test.length+" passed!" - ); -} else { - args.map(a=>{ - try { - console.log(bqn.fmt(bqn(a))) - } catch(e) { - console.error(''+bqn.fmtErr(Array.from(a),e)+''); - } - }); -} +let rslt = test.map(t => {try{return bqn(t);}catch(e){return null;}}); +let pass = rslt.map((r,i)=>r===expt[i]); +let fail = pass.map((p,i)=>p?-1:i).filter(i=>i>=0); +console.log( + fail.length + ? fail.map(i=>'"'+test[i]+'": expected ' + +(e=>e===null?'to fail':e)(expt[i]) + +' but ' + +(r=>r===null?'evaluation failed':'received '+r)(rslt[i])) + : "All "+test.length+" passed!" +); -- cgit v1.2.3