aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2022-01-31 22:42:19 -0500
committerMarshall Lochbaum <mwlochbaum@gmail.com>2022-01-31 22:42:19 -0500
commit76bea994b9428ddfc71fa54acb2c0679cc9311f0 (patch)
treed4e50bd3848c44268514a3add19e599174d1543c
parent731f95355d5a52fa491c4b4d866abe89aba02038 (diff)
Example of making an executable from a •ReBQN function
-rw-r--r--doc/rebqn.md19
-rw-r--r--docs/doc/rebqn.html17
2 files changed, 36 insertions, 0 deletions
diff --git a/doc/rebqn.md b/doc/rebqn.md
index 617204d8..30f05800 100644
--- a/doc/rebqn.md
+++ b/doc/rebqn.md
@@ -71,3 +71,22 @@ What's allowed? The format for `primitives` is a list of pairs, where each pair
Above, `^` becomes a 1-modifier, so that it modifies `%` rather than being called directly on `1‿2` 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 {primitives⇐calcFns, repl⇐"loose"}
+ {
+ # Run on a file
+ 0 < ≠•args ?
+ f ← •wdpath •file.At ⊑•args
+ path‿name ← (∧`⌾⌽'/'⊸≠)⊸⊔f
+ ⟨path,name,1↓•args⟩ Calc •file.Chars f
+
+ # Run as REPL
+ ; _while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}
+ •GetLine∘•Show∘Calc _while_ (@⊸≢) •GetLine@
+ }
diff --git a/docs/doc/rebqn.html b/docs/doc/rebqn.html
index 932a647f..a3d0c97e 100644
--- a/docs/doc/rebqn.html
+++ b/docs/doc/rebqn.html
@@ -76,3 +76,20 @@
</pre>
<p>Above, <code><span class='Value'>^</span></code> becomes a 1-modifier, so that it modifies <code><span class='Value'>%</span></code> rather than being called directly on <code><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span></code> as a function.</p>
<p>The glyph can be any character that's not being used by BQN already. Characters like <code><span class='Value'>c</span></code> or <code><span class='Bracket'>⟩</span></code> or <code><span class='Head'>:</span></code> 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.</p>
+<h2 id="run-as-an-executable"><a class="header" href="#run-as-an-executable">Run as an executable</a></h2>
+<p>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 <code><span class='Value'>•args</span></code> to be passed in, or runs as a REPL if given no arguments.</p>
+<pre><span class='Comment'>#! /usr/bin/env bqn
+</span><span class='Value'>calcFns</span> <span class='Gets'>←</span> <span class='Bracket'>⟨</span><span class='String'>'+'</span><span class='Ligature'>‿</span><span class='Function'>+</span><span class='Separator'>,</span> <span class='String'>'-'</span><span class='Ligature'>‿</span><span class='Function'>-</span><span class='Separator'>,</span> <span class='String'>'*'</span><span class='Ligature'>‿</span><span class='Function'>×</span><span class='Separator'>,</span> <span class='String'>'/'</span><span class='Ligature'>‿</span><span class='Paren'>(</span><span class='Function'>↕</span><span class='Modifier2'>⊘</span><span class='Function'>÷</span><span class='Paren'>)</span><span class='Bracket'>⟩</span>
+<span class='Value'>calc</span> <span class='Gets'>←</span> <span class='Function'>•ReBQN</span> <span class='Brace'>{</span><span class='Value'>primitives</span><span class='Gets'>⇐</span><span class='Value'>calcFns</span><span class='Separator'>,</span> <span class='Value'>repl</span><span class='Gets'>⇐</span><span class='String'>&quot;loose&quot;</span><span class='Brace'>}</span>
+<span class='Brace'>{</span>
+ <span class='Comment'># Run on a file
+</span> <span class='Number'>0</span> <span class='Function'>&lt;</span> <span class='Function'>≠</span><span class='Value'>•args</span> <span class='Head'>?</span>
+ <span class='Value'>f</span> <span class='Gets'>←</span> <span class='Value'>•wdpath</span> <span class='Value'>•file.</span><span class='Function'>At</span> <span class='Function'>⊑</span><span class='Value'>•args</span>
+ <span class='Value'>path</span><span class='Ligature'>‿</span><span class='Value'>name</span> <span class='Gets'>←</span> <span class='Paren'>(</span><span class='Function'>∧</span><span class='Modifier'>`</span><span class='Modifier2'>⌾</span><span class='Function'>⌽</span><span class='String'>'/'</span><span class='Modifier2'>⊸</span><span class='Function'>≠</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>⊔</span><span class='Value'>f</span>
+ <span class='Bracket'>⟨</span><span class='Value'>path</span><span class='Separator'>,</span><span class='Value'>name</span><span class='Separator'>,</span><span class='Number'>1</span><span class='Function'>↓</span><span class='Value'>•args</span><span class='Bracket'>⟩</span> <span class='Function'>Calc</span> <span class='Value'>•file.</span><span class='Function'>Chars</span> <span class='Value'>f</span>
+
+ <span class='Comment'># Run as REPL
+</span> <span class='Head'>;</span> <span class='Modifier2'>_while_</span> <span class='Gets'>←</span> <span class='Brace'>{</span><span class='Function'>𝔽</span><span class='Modifier2'>⍟</span><span class='Function'>𝔾</span><span class='Modifier2'>∘</span><span class='Function'>𝔽</span><span class='Modifier2'>_𝕣_</span><span class='Function'>𝔾</span><span class='Modifier2'>∘</span><span class='Function'>𝔽</span><span class='Modifier2'>⍟</span><span class='Function'>𝔾</span><span class='Value'>𝕩</span><span class='Brace'>}</span>
+ <span class='Function'>•GetLine</span><span class='Modifier2'>∘</span><span class='Function'>•Show</span><span class='Modifier2'>∘</span><span class='Function'>Calc</span> <span class='Modifier2'>_while_</span> <span class='Paren'>(</span><span class='String'>@</span><span class='Modifier2'>⊸</span><span class='Function'>≢</span><span class='Paren'>)</span> <span class='Function'>•GetLine</span><span class='String'>@</span>
+<span class='Brace'>}</span>
+</pre>