aboutsummaryrefslogtreecommitdiff
path: root/bqn.bqn
blob: 095249669a5af114b69382a309e5110d221812f0 (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
#! /usr/bin/env bqn

# A BQN interpreter implemented in BQN
# ./bqn.bqn ( files | -e source )

# Unlike a compiler, a self-hosted interpreter is kind of silly. This
# script is mostly just evidence that the BQN components found in this
# repository do add up to a full implementation. The same idea more
# usefully applied in test/unit.bqn, which can build an interpreter
# using some or all of them in order to test it.

# BQN's compiler is truly self-hosting, but targets a BQN-specific
# object code rather than machine code (as does Java). The virtual
# machine interprets this object code. Finally, the compiler takes
# primitive values as input in order to include them in the object code.
# As all primitives are available in BQN we could just use these, but
# for more self-hostingness the BQN-based runtime is used instead, so
# that the interpreter relies on a smaller set of core functions.

#⌜
# Assemble the components
gl  •Import "src/glyphs.bqn"
compile  gl •Import "src/c.bqn"  # Compiler: source → object code
vm          •Import "vm.bqn"     # VM: interprets object code
runtime     •Import "rt.bqn"     # Runtime: primitives available to VM

# Define system values later, to include BQN
BQN  runtime, {System 𝕩}  (VM Compile)

#⌜
# System values
IsPrim  runtime<
Glyph  runtime<⊑(gl)˙
Decompose  IsPrim•Decompose,0

# Formatter
tn  "*"(∾∾⊣)¨"array""function""1-modifier""2-modifier""namespace"
FmtRepr  (•Import "src/f.bqn"){𝔽} 
  •Type
  Decompose
  IsPrimtn˜•Type-2˙, Glyph # Format operation/namespace
  •Repr                        # Format number

Show  •OutFmt⊢⊢

# Lookup table
FindSys  {
  i  𝕨𝕩
  { ! "Unknown system value",(1≠≠𝕩)/"s",":"" •"¨𝕩 }/𝕩(´) i=≠𝕨
  i
}
system  {𝕨FindSys⊏𝕩˙}´
  "bqn""type""glyph""decompose""repr""fmt""out""show"
   BQN •Type  Glyph  Decompose  Repr  Fmt •Out  Show


#⌜
# Evaluate
{
  0 < ≠•args ?
  t   "-e""-p"  ⊏•args
  Show(t=1)BQN¨ (t<2)•file.Chars¨, 1 •args
;@}

BQN  # Return the evaluator so it can be •Include d from BQN