uCBQN/build/cc.bqn
2024-09-26 03:25:10 +03:00

147 lines
6.5 KiB
BQN
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bqn
# Modified version of https://github.com/mlochbaum/BQN/blob/master/src/cjs.bqn
args •args
"Usage: ./cc.bqn path/to/mlochbaum/BQN [-i] [-n name] <(r|r0|r1|r1x|c|cc|f|e|p) or an expression>"!2args
NextArg {𝕊: (args˜1) args}
TryArg {𝕩args? 1 NextArg@; 0}
return TryArg 1
bqnpath (NextArg@) •file.At "src"
srcmap TryArg "-i"
name TryArg"(precompiled)"NextArg "-n"
Import {𝕨 •Import bqnpath •file.At 𝕩}
FChars {𝕨 •FChars bqnpath •file.At 𝕩}
FLines {𝕨 •FLines bqnpath •file.At 𝕩}
F •Repr # Format number
Join {(-𝕨) 𝕨¨ 𝕩}
FmtComp {
# Escape the special characters that appear in BQN sources.
Esc {
in (@+091013)"'""" # Null, Tab, LF, CR, and quotes
out "0tnr" # Whitespace characters changed to letters
i in𝕩
𝕩 i out((i<out)/) 𝕩 # Replace
(i<in) /"\"¨ 𝕩 # Insert \
}
U32Len ( - ·+´ 55296 56319)-@ # for dzaima/BQN; shouldn't break things in proper BQN impls, as surrogate values don't map to valid codepoints
CStr {"""" () Esc 𝕩}
List (0<)"",1·","¨
lf @+10
Cache {
tag 𝕩
values
Add {(values <𝕩) tagvalues}
Done { 𝕊:
unq values
uncomp {(¨ 𝕩/values) /𝕩} unq # compressed index → canonical original index; sort by length to make length predictable
comp (uncompunq)unq # original index → compressed index
counts /comp # compressed index → count of such
compvals uncompvalues # compressed index → data
n uncomp
}
}
intarrCache Cache 1
Li intarrCache.Add
_Lo {{"m_lvB_", F𝕩, "(", List 𝕩, ")"} 𝔽¨𝕩}
OStr2C { # A BQN string
a´𝕩<128+@
a"m_c32vec""m_c8vec","(",a¬"U","",CStr𝕩,",",•Repr U32Len 𝕩,")"
}
OChr2C {"m_c32(U'"(Esc𝕩)"')"} # A BQN character
ONum2C {s"-"/˜𝕩<0 =|"m_f64("")"˜sF| "m_f64("s"1.0/0.0)"𝕩} # Format number
OAny2C @ONum2COChr2C, OStr2C,
TmpNum 0
# 0‿123: number
# 1‿n: int arr ref
# ⟨a,b,c⟩: obj arr
# "foo": literal code
a ((𝕩)Li OAny2C¨ {0=´=¨𝕩? Li 𝕩; =TmpNum,Li¨¨ 𝕩}¨ (Li 2)¨ Li¨ OStr2C) {𝕎𝕩}¨ 𝕩
iarrs intarrCache.Done@ Li!
hasEmpty iarrs.compvals
init
a {
1i: n iiarrs.comp (hasEmpty0=n) "iarrs[",F n,"]", "iarrs0"; # special-case first array (i.e. empty array) as there isn't enough aliasing info for the compiler to deduplicate loads
2=•Type𝕩? 𝕩;
0n: ONum2C n;
0a, 0b, c, d: cscsab? ´cs0? ´cs<4? "m_blockinfo(", F a+b×4, ", ", 𝕊 c, ", ", 𝕊 d, ")";
4𝕩? 𝕊_Lo 𝕩;
n "a" F init
np n"p"
init< lf
"B ", n, "; B* ", np, " = m_lvBn(&", n, ", ", F 𝕩, ");", lf
"assume_separate_storage(", np, ", ", "iarrs", ");", lf # allow the compiler to vectorize copying from iarrs to np if so beneficial
({'('𝕩}¨) {np,"[", F 𝕩, "] = ", 𝕨, ";", lf}¨ 𝕊¨ 𝕩
n
}¨
"B iarrs[", F iarrs.n, "];", lf
"static const i32 iarrs_data[] = {", lf
lf¨ {','¨ F¨ 𝕨, " // ", F 𝕩}¨ iarrs.compvals
"};", lf
"static const u32 iarrs_lens[] = {", ','Join F¨ iarrs.compvals, "};", lf
"init_intarrs(iarrs, iarrs_data, iarrs_lens, ", F iarrs.n, ");", lf
hasEmpty / "B iarrs0 = iarrs[0];", lf
{0𝕊𝕩: ""; "incByG(iarrs[", F 𝕩, "], ", F 𝕨, ");", lf}¨ iarrs.counts-1
init
"load_importBlock", srcmap/"_src", "(", CStr name, ",", lf
(','lf) Join " "¨ a
lf
");"
}
primN
"add", "sub", "mul", "div", "pow", "root", "floor", "ceil", "stile", "not"
"and", "or", "lt", "gt", "ne", "eq", "le", "ge", "feq", "fne"
"ltack", "rtack", "shape", "join", "couple", "pair", "take", "drop", "ud", "shifta"
"shiftb", "reverse", "transp", "slash", "gradeUp", "gradeDown", "select", "pick", "indexOf", "count"
"memberOf", "find", "group", "asrt", "const", "swap", "cell", "each", "tbl", "undo"
"fold", "insert", "scan", "atop", "over", "before", "after", "under", "val", "cond"
"rank", "depth", "repeat", "catch"
primC "+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍⋈↑↓↕«»⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!˙˜˘¨⌜⁼´˝`∘○⊸⟜⌾⊘◶⎉⚇⍟⎊"
pre "+-=×↕≢≤⊑⊘⌊⌜⥊!<>`|¨´÷˙˜↑↓∘∾≠≥⊢⊣⊸⋆⋈⌈⍟⎊○◶⟜"
isFull ¬ primC "⁼⊏/⊔⌾⍷⚇˝"
fullC isFull/primC
extraC pre (¬˜/) fullC
# •Out List "bi_"⊸∾¨ (primC∊extraC) / primN
glyphs Import "glyphs.bqn"
_getComp { (4+2×srcmap) <˜ 5 (𝕗 Import "c.bqn"){𝔽} }
Comp ((<"incG(runtime["F"])"˙)¨glyphs) glyphs _getComp
RT {
ext 2=𝕩
𝕩 𝕩
srcneedinputs((ext𝕩1) ext/fullC, extraC) Import "pr.bqn"
pr"runtime_0""provide"{(𝕨<(<{"incG("𝕨"["𝕩"])"}F¨)()¨𝕩)˜(𝕩)need}((-1+1=𝕩))inputs
FmtComp pr need _getComp src
}
CArg {(@+10) Join (¯5𝕩˙) FLines "c.bqn"}
SVG {"Modify←GetHighlights←⊢⋄" FChars".bqn"¨ "../svg"𝕩}
LFC FmtCompComp
(@+10)˜ •Out(¬return) ("r""r0""r1""r1x""c""cc""f""e""eu""p")
RT2, RT0, RT1, RT1'x'
{𝕩LFC CArg "⟨""⟩"«","¨'"'(˜)¨glyphs}
{𝕩LFC "{""}"˜CArg"𝕩"}
{𝕩LFC FChars "f.bqn"}
{𝕩LFC SVG "e"}
{𝕩LFC FChars "eu.bqn"}
{𝕩LFC SVG "p"}
LFC¨
args