aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2021-01-28 01:12:02 -0500
committerMarshall Lochbaum <mwlochbaum@gmail.com>2021-01-28 01:17:19 -0500
commit22daab1f5eae23dd03fcfa84375de9193063ad47 (patch)
treec0195a361ee7ea058243053a3b07b820f8846dd8 /src
parent86cec7aa680700afacb202f72e6262eca73a4ec1 (diff)
Separate out and comment JSON formatting in cjs.bqn
Diffstat (limited to 'src')
-rwxr-xr-xsrc/cjs.bqn32
1 files changed, 23 insertions, 9 deletions
diff --git a/src/cjs.bqn b/src/cjs.bqn
index 1e57b220..b2cf597f 100755
--- a/src/cjs.bqn
+++ b/src/cjs.bqn
@@ -1,17 +1,31 @@
#! /usr/bin/env dbqn
+# Javascript/JSON formatting
+L ← "["∾"]"∾˜1↓·∾","⊸∾¨ # Native list/array
+Ind ← {∾𝕨‿"["‿𝕩‿"]"} # Native list/array indexing
+Cat ← {∾𝕨‿".concat("‿𝕩‿")"} # Native list/array concatenation (like ∾)
+# Escape the special characters that appear in BQN sources.
+Esc←{
+ in ← (@+9‿10‿13)∾"'""" # Tab, LF, CR, and quotes
+ out ← "tnr" # Whitespace characters changed to letters
+ i ← in⊐𝕩
+ 𝕩 ↩ i ⊏⟜out⌾((i<≠out)⊸/) 𝕩 # Replace
+ ∾(i<≠in) /⟜"\"⊸∾¨ 𝕩 # Insert \
+}
+Str ← "str("""∾Esc∾""")"˜ # A BQN string
+Char ← "'"(∾∾⊣)Esc∘⥊ # A BQN character
+FP ← ∞⊸=◶⟨⍕,"Infinity"⟩ # Format positive number
+Num ← 0⊸≤◶⟨"-"∾FP∘|,FP⟩ # Format number
+
+F ← ⍕ # Format number
+
compile ← •Import "c.bqn"
useInd ← "-i"≡⊑args←•args ⋄ args↓˜↩useInd
-Comp ← (3+useInd) ↑ ((<"runtime["∾⍕∾"]"˙)¨↕62)⊸Compile
-FP ← ∞⊸=◶⟨⍕,"Infinity"⟩
-Esc ← (⟨"\t","\n","\r","\'","\"""⟩∾<∘⥊)⊑˜("'"""∾˜@+9‿10‿13)⊑∘⊐⊢
-Str ← "str("""∾(∾Esc¨)∾""")"˜
-Char ← "'"(∾∾⊣)Esc
-Fconst ← ≡◶⟨(⊑⍋)∘(∾⟜∞)◶⟨0⊸≤◶⟨"-"∾FP∘|,FP⟩,Char⟩⋄Str⋄⊑⟩
-L ← "["∾"]"∾˜1↓·∾","⊸∾⟜⍕¨
+Comp ← (3+useInd) ↑ ((<"runtime" Ind ⍕)¨↕62)⊸Compile
J ← ∾∾⟜(@+10)¨
-Fout ← (≠↑⟨⊢,Fconst,L,⊢⟩˙) {L𝕎¨𝕩}¨ ⊢
-Frun ← ("provide.concat("∾∾⟜")")⌾(1⊸⊑) Fout
+Fconst ← ≡◶⟨@⊸≤◶Num‿Char, Str, ⊑⟩
+Fout ← (≠↑⟨F,Fconst,L F¨,F⟩˙) {L𝕎¨𝕩}¨ ⊢
+Frun ← "provide"⊸Cat⌾(1⊸⊑) Fout
Long ← ∾ (≠↑1‿3/⟨" "⊸∾⋄((@+10)∾" ,")⊸∾⟩˙) {𝕎𝕩}¨ ⊢
•Out (⊑"r"‿"c"‿"f"‿"e"⊐⊏)◶⟨
{𝕩⋄ref‿len←•Import"pr.bqn"⋄Long Frun len⊸↓⌾(1⊸⊑)Comp ref}