aboutsummaryrefslogtreecommitdiff
path: root/dzref
blob: c4a5c4e98f9034598faaffc6dd35f74e3931d827 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env dbqn

impl  "
#⌜
# LAYER 4: Operators

_ranks ← {⟨2⟩⊘⟨1,0⟩((⊣-1+|)˜⟜≠⊑¨<∘⊢)⥊∘𝔽}
_depthOp_←{
  neg←0>n←𝕨𝔾_ranks𝕩 ⋄ F←𝔽
  _d←{
    R←(𝕗+neg)_d
    𝕨(2⥊(neg∧𝕗≥0)∨(0⌈𝕗)≥≍○<○≡)◶(⟨R¨⋄R⟜𝕩¨∘⊣⟩≍⟨(𝕨R⊢)¨∘⊢⋄F⟩)𝕩
  }
  𝕨 n _d 𝕩
}
⚇ ← _depthOp_


#⌜
# LAYER 5: Structural functions

Nat←IsArray◶⟨0⊸≤∧⌊⊸=,0⟩

_onAxes_←{
  F←𝔽
  (𝔾<≡)∘⊣◶{ # One axis
    ! 1≤=𝕩
    𝕨F𝕩
  }‿{ # Multiple axes
    ! 1≥=𝕨
    ! 𝕨≤○≠≢𝕩
    R←{(⊑𝕨)F(1↓𝕨)⊸R˘𝕩}⍟{0<≠𝕨}
    𝕨R𝕩
  }
}

Windows←{
  ! IsArray 𝕩
  ! 1≥=𝕨
  ! 𝕨≤○≠≢𝕩
  ! ∧´Nat¨⥊𝕨
  s←(≠𝕨)↑≢𝕩
  ! ∧´𝕨≤1+s
  𝕨{(∾⟜(𝕨≠⊸↓≢𝕩)∘≢⥊>)<¨⊸⊏⟜𝕩¨s(¬+⌜○↕⊢)⥊𝕨}⍟(0<≠𝕨)𝕩
}

Rotate ← ⌽ _onAxes_ 0

↕ ↩ ↕              ⊘ Windows
⌽ ↩ ⌽              ⊘ Rotate


#⌜
# LAYER 6: Everything else


Join←{
  C←(<⟨⟩)⥊⊸∾⌜´⊢  # Cartesian array product
  ! IsArray 𝕩
  s←≢¨𝕩
  d←≠⊑s
  ! ∧´⥊d=≠¨s
  ! d≥=𝕩
  l←(≢𝕩){(𝕩⊑⟜≢a⊑˜(j=𝕩)⊸×)¨↕𝕨}¨j←↕r←=a←𝕩
  ! (r↑¨s)≡C l
  i←C{p←+´¨↑𝕩⋄(↕⊑⌽p)-𝕩/¯1↓p}¨l
  >i<¨⊸⊏¨l/𝕩
}⍟(0<≠∘⥊)

∾ ↩ Join           ⊘ ∾

# Searching
IndexOf←(1<⌈○=)◶⊐‿{
  c←1-˜=𝕨
  ! 0≤c
  𝕨 (0<≠𝕨)◶⟨0⎉c∘⊢,(+˝∧`)≢⎉c⎉c‿∞⟩ 𝕩
}
UniqueMask←{
  ! 1≤=𝕩
  u←0↑𝕩
  {(≠u)>⊑u IndexOf 𝕩}◶{u↩u∾𝕩⋄1}‿0˘𝕩
}
Find←{
  r←=𝕨
  ! r≤=𝕩
  𝕨 ≡⎉r (≢𝕨) ↕⎉r 𝕩
}

⊐ ← !∘0            ⊘ IndexOf
∊ ← UniqueMask     ⊘ (⊐˜<≠∘⊢)
⍷ ← ∊⊸/            ⊘ Find

ReorderAxes←{
  𝕩↩<⍟(0=≡)𝕩
  ! 1≥=𝕨
  𝕨↩⥊𝕨
  ! 𝕨≤○≠≢𝕩
  ! ∧´Nat¨⥊𝕨
  r←(=𝕩)-+´¬∊𝕨
  ! ∧´𝕨<r
  𝕨↩𝕨∾𝕨(¬∘∊˜/⊢)↕r
  (𝕨⊸⊏⊑𝕩˜)¨↕⌊´¨𝕨⊔≢𝕩
}
Transpose←(=-1˜)⊸ReorderAxes⍟(0<=)
⍉ ← Transpose      ⊘ ReorderAxes

OccurrenceCount ← ⊐˜(⊢-⊏)⍋∘⍋
ProgressiveIndexOf ← {𝕨⊐○(≍˘⟜OccurrenceCount𝕨⊸⊐)𝕩}

⊒ ← OccurrenceCount⊘ ProgressiveIndexOf
"

XRaw←{F:4}
{v:
  chrs←⟨
    "!∾↕⌽⍉⊐⊒∊⍷"
    ""
    "⚇"
  
  nc  ≠¨chrs
  chr  chrs
  itr  0⥊˜≠chr

  init  " "⊸∾¨(/"_"¨nc/011)∾¨(nc/"FMD")∾¨(nc+´⊸↑⥊"AB"∾⌜•a)
  post  ∾⟜" "¨/"_"¨nc/001
  names  init∾¨(UCS 48)∾¨post

  Inc  {
    i←⊑chr𝕩
    n0  itr↩{n1+𝕩}⌾(i⊑⊢)itr
    names((iinit)(UCS 48+n)ipost)(i⊑⊢)names
  }

  # built-in assumptions
  Mod  {((chr𝕨)names)  " ← "  𝕩}

  ⍎¨ 
    "IsArray ← 0≠≡"
    '!' Mod "{𝕩 ⋄ ≤1}⍟¬"
  ⟩∾Mod⟜⥊¨ "↕∾⌽⊐"


  # 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←¬∨`q¬⊸∧𝕩='#'
     (((l×f/q)+chr⊸⊐) (≥⟜l)◶⟨⊑⟜names,⥊∘⊢⟩¨ ) f/𝕩
  }

  E_redef  { # handles [fmd] [←↩]
    tail  E_proc 3𝕩 # must use old def
    Inc 𝕩
    (E_proc 1𝕩)  "←"  tail
  }

  lf  UCS 10
  pre  E_isdefE_procE_redef¨ lf((-˜¬×+`)=⊔⊢)impl
  Raw↩⍎
  ExecFile←{Raw  ∾⟜lf¨ E_proc¨ LNS 𝕩}
  XRawE_proc
    ∾⟜lf¨ pre
  ≠◶X‿{ExecFile 𝕩}‿{ExecFile 𝕩  X 1𝕩} args
}