diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-02-16 16:13:57 -0500 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-02-16 16:13:57 -0500 |
| commit | 1a83b6105b0d808b0b20f657b812c564c16bb5ec (patch) | |
| tree | 6330887e26031c1c48c73cb915e103f00898be36 | |
| parent | cf142346760cb30c781e28d7178ad88772e3b5a7 (diff) | |
Merge primitive/replacement processing code for dzref and pr.bqn
| -rwxr-xr-x | dzref | 40 | ||||
| -rw-r--r-- | src/pp.bqn | 43 | ||||
| -rwxr-xr-x | src/pr.bqn | 51 |
3 files changed, 60 insertions, 74 deletions
@@ -79,48 +79,22 @@ _withRef ← ˙ "" "⚇" ⟩ - nc ← ≠¨chrs - chr ← ∾chrs - itr ← 0⥊˜≠chr - - init ← " "⊸∾¨(/⟜"_"¨nc/0‿1‿1)∾¨(nc/"FMD")∾¨(nc+´⊸↑⥊"AB"∾⌜•a) - post ← ∾⟜" "¨/⟜"_"¨nc/0‿0‿1 - names ← init∾¨'0'∾¨post - - Inc ← { - i←⊑chr⊐𝕩 - n←0 ⋄ itr↩{n↩1+𝕩}⌾(i⊑⊢)itr - names↩((i⊑init)∾('0'+n)∾i⊑post)⌾(i⊑⊢)names - } # built-in assumptions - (Raw (chr⊸⊐⌾<⊑names˙)∾" ← "∾⥊)¨ "↕⊐" - chr∾↩"⍎" ⋄ names∾↩⟨"⍎_withRef "⟩ - - - # checks if line is a builtin redefinition - E_isdef ← (3≤≠)◶⟨0,∧´⟨chr," ","←↩"⟩∊˜¨3⊸↑⟩ - - # removes comments and replaces built-ins with names - E_proc ← { - l←≠chr - q←≠`𝕩∊"""'" ⋄ f←(𝕩=lf)≥○(↕∘≠⊸(⌈`×))q<𝕩='#' - ∾ (((l×f/q)+chr⊸⊐) (≥⟜l)◶⟨⊑⟜names,⥊∘⊢⟩¨ ⊢) f/𝕩 + GetRepls ← { + c‿n ← 𝕩 ⋄ k←"↕⊐" + ⟨c,(⥊¨k)⌾(((⊑¨c)⊐k)⊸⊏)n⟩ ∾⟜<¨ "⍎"‿"⍎_withRef " } - E_redef ← { # handles [fmd] [←↩] - tail ← E_proc 3↓𝕩 # must use old def - Inc ⊑𝕩 - (E_proc 1↑𝕩) ∾ "←" ∾ tail - } + ⟨E_proc,E_nodef⟩ ← chrs‿GetRepls •Import "src/pp.bqn" lf ← @+10 - pre ← E_isdef◶E_proc‿E_redef¨ lf((⊢-˜¬×+`)∘=⊔⊢)impl + pre ← E_proc¨ lf((⊢-˜¬×+`)∘=⊔⊢)impl ExecFile←{ - src ← E_proc¨ •FLines 𝕩 + src ← E_nodef¨ •FLines 𝕩 ⟨⟨⟩,"",(∨`⌾⌽'/'=𝕩)/𝕩⟩ Raw ∾ ∾⟜lf¨ src } - _withRef ↩ {𝔽∘E_proc⊘(𝔽⟜E_proc)} + _withRef ↩ {𝔽∘E_nodef⊘(𝔽⟜E_nodef)} X↩Raw _withRef Raw ∾ ∾⟜lf¨ pre ≠◶(X˙)‿{ExecFile ⊑𝕩}‿{ExecFile ⊑𝕩 ⋄ X 1⊑𝕩} •args diff --git a/src/pp.bqn b/src/pp.bqn new file mode 100644 index 00000000..cb614dcb --- /dev/null +++ b/src/pp.bqn @@ -0,0 +1,43 @@ +# Primitive processor +# Used in dzref and pr.bqn for handling source code with primitive +# redefinitions. +# Defining a primitive shadows previous definitions, so earlier uses +# of the primitive still use the old definition. +# This is handled by using a new name each time it's defined. + +⟨chrs, GetReplacements⟩ ← •args +nc ← ≠¨chrs +chr ← ∾chrs + +# Initial primitive replacement names +init ← (nc/(' '∾¨1‿2/↑"_")∾¨"FMD")∾¨('A'+nc+´⊸↑⥊3∾⌜○↕26) +post ← nc/(2‿1/↑"_")∾¨' ' +pn ← init∾¨'0'∾¨post + +# All replacements: input and output +⟨in,out⟩ ← GetReplacements ⟨⥊¨chr,pn⟩ + +# Make a new name for primitive 𝕩 +itr ← 0⥊˜≠chr +Shadow ← { + i←⊑chr⊐𝕩 + n←0 ⋄ itr↩{n↩1+𝕩}⌾(i⊑⊢)itr + out↩((i⊑init)∾('0'+n)∾i⊑post)⌾(i⊑⊢)out +} + +# Does the expression define a primitive? +E_isdef ← (3≤≠)◶⟨0,∧´⟨chr," ","←↩"⟩∊˜¨3⊸↑⟩ +# Process ordinary expression +E_nodef ⇐ { + idChars ← "_¯.π∞"∾∾"0aA"+⟜↕¨10‿26‿26 + q←≠`𝕩='"' ⋄ q∨↩≠`q<𝕩=''' # Quotes + f←(𝕩=@+10)≥○((1+↕∘≠)⊸(⌈`×))q<𝕩='#' # Comments + t ← (¯1+`·¬(»f/q)∨·»⊸∧∊⟜idChars)⊸⊔ f/𝕩 # Tokenize + ∾ (in⊸⊐ ⊑⟜out⍟(<⟜(≠in))¨ ⊢) t # Replace +} +# Process expression, possibly redefining a primitive +E_proc ⇐ E_isdef◶E_nodef‿{ + tail ← E_proc 3↓𝕩 # RHS + Shadow ⊑𝕩 # New name + (E_proc 1↑𝕩) ∾ "←" ∾ tail # LHS +} @@ -1,10 +1,6 @@ #!/usr/bin/env dbqn # Process BQN runtime -# r.bqn begins with provided values and defines primitives as it goes. -# Defining a primitive shadows previous definitions, so earlier uses -# of the primitive still use the old definition. -# This is handled by using a new name each time it's defined. # All primitives chrs←⟨ @@ -12,50 +8,23 @@ chrs←⟨ "˙˜˘¨⌜⁼´˝`" "∘○⊸⟜⌾⊘◶⎉⚇⍟" ⟩ -nc ← ≠¨chrs -glyphs ⇐ chr ← ∾chrs +glyphs ⇐ ∾chrs # Provided values, to be passed in through the constants array def ← ⟨"Type","Decompose","Glyph","Fill","Log","GroupLen","GroupOrd","_fillBy_"⟩ keep ← "!+-×÷⋆⌊=≤≢⥊⊑↕⌜`⊘" len ⇐ def+○≠keep -ki ← chr⊐keep -dt ← ⊔⟜⊒ (+´·∧`'_'=0‿¯1⊸⊏)¨ def -ns ← » ne←+`nc -kt ← ∾ (ns+≠¨dt) + ⊔⟜⊒ne⍋ki -# Initial primitive replacement names -init ← (nc/(' '∾¨1‿2/↑"_")∾¨"FMD")∾¨('A'+nc+´⊸↑⥊3∾⌜○↕26) -post ← nc/(2‿1/↑"_")∾¨' ' -pn ← init∾¨'0'∾¨post - -# All replacements: input and output -chrt ← ⥊¨chr -in ← chrt ∾ def -out ← ((kt⊏chrt)⌾(ki⊸⊏)pn) ∾ (∾ns+dt)⊏chrt -# Make a new name for primitive 𝕩 -itr ← 0⥊˜≠chr -Shadow ← { - i←⊑chr⊐𝕩 - n←0 ⋄ itr↩{n↩1+𝕩}⌾(i⊑⊢)itr - out↩((i⊑init)∾('0'+n)∾i⊑post)⌾(i⊑⊢)out +GetRepls ← { + c‿n ← 𝕩 + ki ← (⊑¨c)⊐keep + dt ← ⊔⟜⊒ (+´·∧`'_'=0‿¯1⊸⊏)¨ def + ns ← » ne←+`≠¨chrs + kt ← ∾ (ns+≠¨dt) + ⊔⟜⊒ne⍋ki + ⟨c,(kt⊏c)⌾(ki⊸⊏)n⟩∾¨⟨def,(∾ns+dt)⊏c⟩ } -# Does the expression define a primitive? -E_isdef ← (3≤≠)◶⟨0,∧´⟨chr," ","←↩"⟩∊˜¨3⊸↑⟩ -# Process ordinary expression -E_nodef ← { - idChars ← "_¯.π∞"∾∾"0aA"+⟜↕¨10‿26‿26 - q←≠`𝕩='"' ⋄ q∨↩≠`q<𝕩=''' ⋄ f←¬∨`q<𝕩='#' # Quotes and comments - t ← (¯1+`·¬(»f/q)∨·»⊸∧∊⟜idChars)⊸⊔ f/𝕩 # Tokenize - ∾ (in⊸⊐ ⊑⟜out⍟(<⟜(≠in))¨ ⊢) t # Replace -} -# Process expression, possibly redefining a primitive -E_proc ← E_isdef◶E_nodef‿{ - tail ← E_proc 3↓𝕩 # RHS - Shadow ⊑𝕩 # New name - (E_proc 1↑𝕩) ∾ "←" ∾ tail # LHS -} +⟨E_proc⟩ ← chrs‿GetRepls •Import "pp.bqn" -rslt ← "⟨"∾"⟩"«∾","⊸∾¨chrt # Output all primitives +rslt ← "⟨"∾"⟩"«∾","⊸∾¨glyphs # Output all primitives ref ⇐ ∾∾⟜(@+10)¨ E_proc¨ (•FLines "r.bqn")∾<rslt |
