aboutsummaryrefslogtreecommitdiff
path: root/src/eu.bqn
blob: 0f3d0a203329fcb4f46ba2e8d3ea9e53f98ccfb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Expression explainer: generates a Unicode box-drawing character diagram to
# show how a BQN program is evaluated

wh  11

Explain  {
  bconstblkbdy(ie)tok  𝕨
  babcbobp  (⊏/¨1)'0'-˜ # For each instruction, number of:
    "11411311121111111315114131131111=111"  # Codes until next opcode
    "111000111100000000002221100000000111"  # Arguments
    "000111////23232303230000011022232111"  # Stack values consumed
    "111000111111111101111111101101111111"  # Stack values output
    "000000000011111101010000000000010000"  # Position determiner
  
  m  { # Mask of instruction starts
    n  (↕≠b)+1+ba(⊣⊏˜>×⊢)b
    Se(>/⊢){(˜𝕨)𝕊((¯1))𝕩𝕩𝕨}0˙
    (≠↑·/Se) n
  }
  mbmi  0¯1 ¨ m/¨ bi
  la¯1=nambbc
  na(b˜1+⊣)(la/)˜/m
  no1(¯1)mbbo
  r+`no-na
  ! 1=¯1r
  d(+`no×)×f0<na
  p((¯1r)(r+⊒))⊏/na¬no
  kp↑¬f

  srctokstiii  tok {
    ···se  𝕨
    ilf  /𝕩=@+10
    𝕩  ((ilfs)" ⋄")(ilf)𝕩
    ti  sk/mi
    𝕩, 𝕩˜1-˜s(⊣×>)(+`(𝕩)↑/)1+e, ti, ti(s+e)÷2
  } 𝕩

  j¯1((1-˜+`k)ii)˜˜{s𝕩˜g𝕩(g/˜(s)=smbbp)((s))↕≠mb}p
  jej¯1
  q(/kjpje-la) (⊏≍⊣) p

  pd¨1+⌽<˜˘˜˝ q  wh×jed
  dimwh×𝕩,1+⌈´d+10

  Path  {𝕊 (sxsy)(exey):
    R{(𝕨𝕩)+↕1+|𝕨-𝕩}
    xssx R ex  yssy R ey
    p¯1×sx>ex
    (xs(¯1ys))(pxs)ys
  }
  Draw{𝕊g: {g∧𝕏g}¨«,«˘,»,»˘}
  T{tx 𝕊 s: px-⌊(2÷˜t)  (ps)t(s˜p+≠t) }
  repr" ╷╶┌╵│└├╴┐─┬┘┤┴┼"
  tp1 ii (10+wh×≍)¨ (k/p)d
  paths1 Path¨<˘pd
  graphs{1¨(𝕩) 0˜1+⌽dim}¨paths
  out1repr˜ +(2×)´ ´Draw¨graphs
  {yx𝕊ti: out (titoks)xT(y)}´˘tpti # place tokens
  out ='╷''│'¨ # fix first row to look better
  <˘(" "src" ")out
}