aboutsummaryrefslogtreecommitdiff
path: root/src/pp.bqn
blob: 8e0fe7cc9e008c34a4c8225e3097258026a13173 (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
# 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/(' '¨12/↑"_")¨"FMD")¨('A'+nc+´↑⥊326)
post  nc/(21/↑"_")¨' '
pn  init¨'0'¨post

# All replacements: input and output
in,out  GetReplacements ¨chr,pn

# Make a new name for primitive 𝕩
itr  ¯1˜chr
Shadow  {
  ichr𝕩
  n0  itr{n1+𝕩}(i⊑⊢)itr
  out((iinit)('0'+n)ipost)(i⊑⊢)out
}

# Does the expression define a primitive?
E_isdef  (3≤≠)0,´chr," ","←↩"˜¨3
# Process ordinary expression
E_nodef  {
  idChars  "_¯.π∞"∾∾"0aA"+¨102626
  q`𝕩='"'  q`q<𝕩='''                 # Quotes
  f←(𝕩=@+10)≥○((1+↕∘≠)⊸(⌈`×))q<𝕩='#'       # Comments
  w('•'=(∨»∧⊢)idChars) f/𝕩            # Group words
  t(¯1+`·¬(»f/q)w) f/𝕩                 # Tokenize
   (in out(<(in))¨ ) t            # Replace
}
# Process expression, possibly redefining a primitive
E_proc  E_isdefE_nodef{
  tail  E_proc 3𝕩                        # RHS
  Shadow 𝕩                                # New name
  (E_proc 1𝕩)  "←"  tail                # LHS
}