aboutsummaryrefslogtreecommitdiff
path: root/src/eu.bqn
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu.bqn')
-rw-r--r--src/eu.bqn79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/eu.bqn b/src/eu.bqn
new file mode 100644
index 00000000..0961c269
--- /dev/null
+++ b/src/eu.bqn
@@ -0,0 +1,79 @@
+# Expression explainer: generates a Unicode box-drawing character diagram to
+# show how a BQN program is evaluated
+
+wh ← 1‿1
+
+Explain ← {
+ b‿const‿blk‿bdy‿(i‿e)‿tok ← 𝕨
+ ba‿bc‿bo‿bp ← (⊏/¨1⊸↓)'0'-˜⟨ # For each instruction, number of:
+ "114113141111111315114131131111=111" # Codes until next opcode
+ "1110001100000000002221100000000111" # Arguments
+ "000111//23232303230000011022232111" # Stack values consumed
+ "1110001111111101111111101101111111" # Stack values output
+ "0000000011111101010000000000010000" # Position determiner
+ ⟩
+ m ← { # Mask of instruction starts
+ n ← (↕≠b)+1+ba(⊣⊏˜≠⊸>×⊢)b
+ Se←{(⊏˜𝕨)Se 1¨⌾((𝕩/𝕨)⊸⊏)𝕩}⍟(0=¯1⊑⊢)
+ (≠↑∾⟜≠Se 1∾0¨) n
+ }
+ mb‿mi ← 0‿¯1 ↓¨ m⊸/¨ b‿i
+ la←¯1=na←mb⊏bc
+ na(b⊏˜1+⊣)⌾(la⊸/)˜↩/m
+ no←1⌾(¯1⊸⊑)mb⊏bo
+ r←+`no-na
+ ! 1=¯1⊑r
+ d←(+`no⊸×)⊸×f←0<na
+ p←((¯1↓r)⍋⊸⊏○⍋(⊏⟜r+⊒))⊸⊏/na¬no
+ k←p≠⊸↑¬f
+
+ src‿toks‿ti‿ii ← tok {
+ ·‿·‿·‿s‿e ← 𝕨
+ ilf ← /𝕩=@+10
+ 𝕩 ↩ ((ilf∊s)⊏" ⋄")⌾(ilf⊸⊏)𝕩
+ ti ← s⊐k/mi
+ ⟨𝕩, 𝕩⊔˜1-˜s(⊣×>)○(+`(≠𝕩)↑/⁼)1+e, ti, ti⊏(s+e)÷2⟩
+ } 𝕩
+
+ j←¯1↓((1-˜+`k)⊏ii)⊏˜⊏˜⍟≠{s←𝕩⊏˜g←⍋𝕩⋄(g/˜(⊒s)=s⊏mb⊏bp)⌾((⍷s)⊸⊏)↕≠mb}p
+ je←j∾¯1
+ q←(/k≤j≠p⊏je-la) (⊏≍⊣) p
+
+ pd←⌊¨1+⌽∘∾○<˜˘˜˝ q ⊏ ⍉wh×je≍d
+ dim←wh×⟨≠𝕩,1+⌈´d⟩+1‿0
+
+ Path ← {𝕊 (sx‿sy)‿(ex‿ey):
+ R←{(𝕨⌊𝕩)+↕1+|𝕨-𝕩}
+ xs←sx R ex ⋄ ys←sy R ey
+ p←¯1×sx>ex
+ (xs∾⌜(¯1⊑ys))∾(p⊑xs)∾⌜ys
+ }
+ Draw←{𝕊g: g×+˝(2⋆↕4)×>{𝕏g}¨⟨«,«˘,»,»˘⟩}
+ T←{t‿x 𝕊 s: p←x-⌊(2÷˜≠t) ⋄ ∾(p↑s)‿t‿(s↓˜p+≠t) }
+ repr←" ╷╶┌╵│└├╴┐─┬┘┤┴┼"
+ repri←⥊[
+ 0‿1‿2‿3‿4‿5‿6‿7‿8‿9‿10‿11‿12‿13‿14‿15
+ 1‿1‿3‿3‿5‿5‿7‿7‿9‿9‿11‿11‿13‿13‿15‿15
+ 2‿3‿2‿3‿6‿7‿6‿7‿10‿11‿10‿11‿14‿15‿14‿15
+ 3‿3‿3‿3‿7‿7‿7‿7‿11‿11‿11‿11‿15‿15‿15‿15
+ 4‿5‿6‿7‿4‿5‿6‿7‿12‿13‿14‿15‿12‿13‿14‿15
+ 5‿5‿7‿7‿7‿5‿7‿7‿13‿13‿15‿15‿13‿13‿15‿15
+ 6‿7‿6‿7‿6‿7‿6‿7‿14‿15‿14‿15‿14‿15‿14‿15
+ 7‿7‿7‿7‿7‿7‿7‿7‿15‿15‿15‿15‿15‿15‿15‿15
+ 8‿9‿10‿11‿12‿13‿14‿15‿8‿9‿10‿11‿12‿13‿14‿15
+ 9‿9‿11‿11‿13‿13‿15‿15‿9‿9‿11‿11‿13‿13‿15‿15
+ 10‿11‿10‿11‿14‿15‿14‿15‿10‿11‿10‿11‿14‿15‿14‿15
+ 11‿11‿11‿11‿15‿15‿15‿15‿11‿11‿11‿11‿15‿15‿15‿15
+ 12‿13‿14‿15‿12‿13‿14‿15‿12‿13‿14‿15‿12‿13‿14‿15
+ 13‿13‿15‿15‿13‿13‿15‿15‿13‿13‿15‿15‿13‿13‿15‿15
+ 14‿15‿14‿15‿14‿15‿14‿15‿14‿15‿14‿15‿14‿15‿14‿15
+ 15‿15‿15‿15‿15‿15‿15‿15‿15‿15‿15‿15‿15‿15‿15‿15
+ ]
+ tp←⌽⚇1 ⌈ii (1‿0+wh×≍)¨ (k/p)⊏d
+ paths←⌽⚇1 Path¨<˘pd
+ graphs←{1¨⌾(𝕩⊸⊑) 0⥊˜1+⌽dim}¨paths
+ out←1↓⊏⟜repr{⊑⟜repri¨(𝕨×≠repr)+𝕩}´Draw¨graphs
+ {y‿x𝕊ti: out (ti⊑toks)‿x⊸T⌾(y⊸⊏)↩}´˘⍉tp≍ti # place tokens
+ out =⟜'╷'◶⊢‿'│'¨⌾⊏↩ # fix first row to look better
+ <˘∾(∾" "‿src‿" ")‿out
+}