start of cc.bqn/precompiled format refactor

This commit is contained in:
dzaima 2024-09-14 18:28:51 +03:00
parent d508bca78d
commit f2efa657b7
5 changed files with 88 additions and 79 deletions

View File

@ -1,56 +1,63 @@
#!/usr/bin/env bqn #!/usr/bin/env bqn
# Modified version of https://github.com/mlochbaum/BQN/blob/master/src/cjs.bqn # Modified version of https://github.com/mlochbaum/BQN/blob/master/src/cjs.bqn
args•args args •args
"Usage: ./cc.bqn path/to/mlochbaum/BQN [-i] <(r|r0|r1|r1x|c|cc|f|e|p) or an expression>"!2args "Usage: ./cc.bqn path/to/mlochbaum/BQN [-i] [-n name] <(r|r0|r1|r1x|c|cc|f|e|p) or an expression>"!2args
return 1args NextArg {𝕊: (args˜1) args}
args˜ return TryArg {𝕩args? 1 NextArg@; 0}
path(args)"/src/"
args˜1
Import {𝕨•Import path𝕩}
FChars {𝕨•FChars path𝕩}
FLines {𝕨•FLines path𝕩}
return TryArg 1
bqnpath (NextArg@) •file.At "src"
srcmap TryArg "-i"
name TryArg"(precompiled)"NextArg "-n"
F •Repr # Format number Import {𝕨 •Import bqnpath •file.At 𝕩}
# Escape the special characters that appear in BQN sources. FChars {𝕨 •FChars bqnpath •file.At 𝕩}
Esc{ FLines {𝕨 •FLines bqnpath •file.At 𝕩}
in (@+091013)"'""" # Null, Tab, LF, CR, and quotes
out "0tnr" # Whitespace characters changed to letters F •Repr # Format number
i in𝕩 Join {(-𝕨) 𝕨¨ 𝕩}
𝕩 i out((i<out)/) 𝕩 # Replace
(i<in) /"\"¨ 𝕩 # Insert \ 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 𝕩}
OStr { # A BQN string
a´𝕩<128+@
a"m_c32vec""m_c8vec","(",a¬"U","",CStr𝕩,",",•Repr U32Len 𝕩,")"
}
OChr {"m_c32(U'"(Esc𝕩)"')"} # A BQN character
ONum {s"-"/˜𝕩<0 =|"m_f64("")"˜sF| "m_f64("s"1.0/0.0)"𝕩} # Format number
OAny @ONumOChr, OStr,
List (0<)"",1·","¨
ShortList {"m_lv"𝕨"_"(F𝕩)"("(List𝕩)")"}
LongList {"m_ca"𝕨"("(F𝕩)",("𝕨"[]){"(List𝕩)"})"}
ConstList {"m_ca"𝕨"("(F𝕩)",({static "𝕨" l_[]={"(List𝕩)"};l_;}))"}
_makeList {𝕨 (>4˙)ShortList,𝕗LongListConstList 𝕩}
Li {"i32" 1 _makeList F¨𝕩}
_Lo {"B" 0 _makeList 𝔽¨𝕩}
a ((𝕩)Li OAny¨ =ONum,Li¨¨¨ (Li 2)¨ Li¨ OStr) {𝕎𝕩}¨ 𝕩
a {2=•Type𝕩? 𝕩; 𝕊_Lo 𝕩}¨
"load_importBlock", srcmap/"_src", "(", CStr name, ",", @+10,
(","@+10) Join a
@+10
");"
} }
U32Len ( - ·+´ 55296 56319)-@ # for dzaima/BQN; shouldn't break things in proper BQN impls, as surrogate values don't map to valid codepoints
OStr { # A BQN string
a´𝕩<128+@
a"m_c32vec""m_c8vec","(",a¬"U","""",Esc𝕩,""",",•Repr U32Len 𝕩,")"
}
OChr {"m_c32(U'"(Esc𝕩)"')"} # A BQN character
ONum {s"-"/˜𝕩<0 =|"m_f64("")"˜sF| "m_f64("s"1.0/0.0)"𝕩} # Format number
OAny @ONumOChr, OStr,
List (0<)"",1·","¨
ShortList {"m_lv"𝕨"_"(F𝕩)"("(List𝕩)")"}
LongList {"m_ca"𝕨"("(F𝕩)",("𝕨"[]){"(List𝕩)"})"}
ConstList {"m_ca"𝕨"("(F𝕩)",({static "𝕨" l_[]={"(List𝕩)"};l_;}))"}
_makeList {𝕨 (>4˙)ShortList,𝕗LongListConstList 𝕩}
Li {"i32" 1 _makeList F¨𝕩}
_Lo {"B" 0 _makeList 𝔽¨𝕩}
glyphs Import "glyphs.bqn"
_getComp { (4+2×useInd) <˜ 5 (𝕗 Import "c.bqn"){𝔽} }
useInd "-i"args args˜useInd
Comp ((<"incG(runtime["F"])"˙)¨glyphs) glyphs _getComp
J (@+10)¨
Fout {((𝕩)Li OAny _Lo =ONum,Li _Lo _Lo _Lo (Li 2) _Lo Li _Lo OStr) {𝕎𝕩}¨ 𝕩}
Frun 1Fout
Long {¯2𝕩¨<","@+10}
LFC LongFoutComp
primN primN
"add", "sub", "mul", "div", "pow", "root", "floor", "ceil", "stile", "not" "add", "sub", "mul", "div", "pow", "root", "floor", "ceil", "stile", "not"
"and", "or", "lt", "gt", "ne", "eq", "le", "ge", "feq", "fne" "and", "or", "lt", "gt", "ne", "eq", "le", "ge", "feq", "fne"
@ -67,16 +74,21 @@ fullC ← isFull/primC
extraC pre (¬˜/) fullC extraC pre (¬˜/) fullC
# •Out List "bi_"⊸∾¨ (primC∊extraC) / primN # •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 { RT {
ext 2=𝕩 ext 2=𝕩
𝕩 𝕩 𝕩 𝕩
srcneedinputs((ext𝕩1) ext/fullC, extraC) Import "pr.bqn" srcneedinputs((ext𝕩1) ext/fullC, extraC) Import "pr.bqn"
pr"runtime_0""provide"{(𝕨<(<{"incG("𝕨"["𝕩"])"}F¨)()¨𝕩)˜(𝕩)need}((-1+1=𝕩))inputs pr"runtime_0""provide"{(𝕨<(<{"incG("𝕨"["𝕩"])"}F¨)()¨𝕩)˜(𝕩)need}((-1+1=𝕩))inputs
Long Fout pr need _getComp src FmtComp pr need _getComp src
} }
CArg {J (¯5𝕩˙) FLines "c.bqn"} CArg {(@+10) Join (¯5𝕩˙) FLines "c.bqn"}
SVG {"Modify←GetHighlights←⊢⋄" FChars".bqn"¨ "../svg"𝕩} SVG {"Modify←GetHighlights←⊢⋄" FChars".bqn"¨ "../svg"𝕩}
LFC FmtCompComp
(@+10)˜ •Out(¬return) ("r""r0""r1""r1x""c""cc""f""e""eu""p") (@+10)˜ •Out(¬return) ("r""r0""r1""r1x""c""cc""f""e""eu""p")
RT2, RT0, RT1, RT1'x' RT2, RT0, RT1, RT1'x'
{𝕩LFC CArg "⟨""⟩"«","¨'"'(˜)¨glyphs} {𝕩LFC CArg "⟨""⟩"«","¨'"'(˜)¨glyphs}
@ -86,5 +98,4 @@ SVG ← {∾⟨"Modify←GetHighlights←⊢⋄"⟩∾ FChars∘∾⟜".bqn"¨ "
{𝕩LFC FChars "eu.bqn"} {𝕩LFC FChars "eu.bqn"}
{𝕩LFC SVG "p"} {𝕩LFC SVG "p"}
LFC¨ LFC¨
# ¯1 ↓ · J L∘Fout∘Comp¨
args args

View File

@ -5,11 +5,12 @@ bqnSrc‿outputPath ← •wdpath⊸•file.At¨ 2↑•args
rtSrc ← ⊑(<"src")∊2↓•args rtSrc ← ⊑(<"src")∊2↓•args
CC ← {𝕨 •FChars (⟨1,bqnSrc⟩∾(rtSrc/⟨"-i"⟩)∾⟨𝕩⟩) •Import "cc.bqn"} CC ← { dst‿type‿name:
(outputPath•file.At "gen/src" )•FChars "#define RT_SRC "∾('0'+rtSrc)∾@+10 (outputPath•file.At dst) •FChars (⟨1, bqnSrc⟩∾(rtSrc/⟨"-i"⟩)∾⟨"-n", name, type⟩) •Import "cc.bqn"
(outputPath•file.At "gen/compiles" )CC"cc" }
(outputPath•file.At "gen/runtime0" )CC"r0" CC ⟨"gen/compiles", "cc", "(compiler)"⟩
(outputPath•file.At "gen/runtime1" )CC"r1" CC ⟨"gen/runtime0", "r0", "(self-hosted runtime0)"⟩
(outputPath•file.At "gen/runtime1x")CC"r1x" CC ⟨"gen/runtime1", "r1", "(self-hosted runtime1)"⟩
(outputPath•file.At "gen/formatter")CC"f" CC ⟨"gen/runtime1x", "r1x", "(self-hosted runtime1x)"⟩
(outputPath•file.At "gen/explain" )CC"eu" CC ⟨"gen/formatter", "f", "(formatter)"⟩
CC ⟨"gen/explain", "eu", "(explain)"⟩

View File

@ -1,3 +1,3 @@
#!./BQN #!/usr/bin/env bqn
# a version of genRuntime that also generates source maps, so that errors in the runtime or compiler get error messages # a version of genRuntime that also generates source maps, so that errors in the runtime or compiler get error messages
({𝕩∾<"src"}⍟(2≤≠) •args) •Import "genRuntime" ({𝕩∾<"src"}⍟(2≤≠) •args) •Import "genRuntime"

View File

@ -97,12 +97,14 @@ static void bitarr_init(void) {
TIi(t_bitarr,canStore) = bitarr_canStore; TIi(t_bitarr,canStore) = bitarr_canStore;
} }
INIT_GLOBAL Arr* staticSliceRoot;
void tyarr_init(void) { void tyarr_init(void) {
i8arr_init(); i16arr_init(); i32arr_init(); bitarr_init(); i8arr_init(); i16arr_init(); i32arr_init(); bitarr_init();
c8arr_init(); c16arr_init(); c32arr_init(); f64arr_init(); c8arr_init(); c16arr_init(); c32arr_init(); f64arr_init();
{ u64* tmp; bi_emptyIVec = m_bitarrv(&tmp, 0); gc_add(bi_emptyIVec); } { u64* tmp; gc_add(bi_emptyIVec = m_bitarrv(&tmp, 0)); }
{ u8* tmp; bi_emptyCVec = m_c8arrv (&tmp, 0); gc_add(bi_emptyCVec); } { u8* tmp; gc_add(bi_emptyCVec = m_c8arrv (&tmp, 0)); }
{ u8* tmp; gc_add(taga(arr_shVec(staticSliceRoot = m_c8arrp(&tmp, 0)))); }
Arr* emptySVec = arr_shVec(m_fillarrpEmpty(emptyCVec())); Arr* emptySVec = arr_shVec(m_fillarrpEmpty(emptyCVec()));
bi_emptySVec = taga(emptySVec); gc_add(bi_emptySVec); bi_emptySVec = taga(emptySVec); gc_add(bi_emptySVec);

View File

@ -23,6 +23,8 @@ FOR_INIT(F)
GLOBAL u64 mm_heapMax = HEAP_MAX; GLOBAL u64 mm_heapMax = HEAP_MAX;
GLOBAL u64 mm_heapAlloc; GLOBAL u64 mm_heapAlloc;
extern Arr* staticSliceRoot;
// compiler result: // compiler result:
// [ // [
// [...bytecode], // [...bytecode],
@ -142,19 +144,12 @@ Block* load_buildBlock(B x, B src, B path, B name, Scope* sc, i32 nsResult) { //
return r; return r;
} }
#if !(ONLY_NATIVE_COMP && !FORMATTER && NO_RT && NO_EXPLAIN) static NOINLINE Block* load_importBlock_src(char* name, B bc, B objs, B blocks, B bodies, B inds, B src) { // consumes all
#include PRECOMPILED_FILE(src)
#endif
#if RT_SRC
Block* load_importBlock(char* name, B bc, B objs, B blocks, B bodies, B inds, B src) { // consumes all
return compileAll(bc, objs, blocks, bodies, inds, bi_N, src, m_c8vec_0(name), NULL, 0); return compileAll(bc, objs, blocks, bodies, inds, bi_N, src, m_c8vec_0(name), NULL, 0);
} }
#else static NOINLINE Block* load_importBlock(char* name, B bc, B objs, B blocks, B bodies) { // consumes all
Block* load_importBlock(char* name, B bc, B objs, B blocks, B bodies) { // consumes all
return compileAll(bc, objs, blocks, bodies, bi_N, bi_N, bi_N, m_c8vec_0(name), NULL, 0); return compileAll(bc, objs, blocks, bodies, bi_N, bi_N, bi_N, m_c8vec_0(name), NULL, 0);
} }
#endif
GLOBAL B load_compgen; GLOBAL B load_compgen;
GLOBAL B def_re; GLOBAL B def_re;
@ -465,20 +460,20 @@ void load_init() { // very last init function
#if !ALL_R0 #if !ALL_R0
B runtime_0[] = {bi_floor,bi_ceil,bi_stile,bi_lt,bi_gt,bi_ne,bi_ge,bi_rtack,bi_ltack,bi_join,bi_pair,bi_take,bi_drop,bi_select,bi_const,bi_swap,bi_each,bi_fold,bi_atop,bi_over,bi_before,bi_after,bi_cond,bi_repeat}; B runtime_0[] = {bi_floor,bi_ceil,bi_stile,bi_lt,bi_gt,bi_ne,bi_ge,bi_rtack,bi_ltack,bi_join,bi_pair,bi_take,bi_drop,bi_select,bi_const,bi_swap,bi_each,bi_fold,bi_atop,bi_over,bi_before,bi_after,bi_cond,bi_repeat};
#else #else
Block* runtime0_b = load_importBlock("(self-hosted runtime0)", Block* runtime0_b = ({
#include PRECOMPILED_FILE(runtime0) #include PRECOMPILED_FILE(runtime0)
); });
HArr* r0r = toHArr(evalFunBlockConsume(runtime0_b)); HArr* r0r = toHArr(evalFunBlockConsume(runtime0_b));
B* runtime_0 = r0r->a; B* runtime_0 = r0r->a;
#endif #endif
Block* runtime_b = load_importBlock("(self-hosted runtime1)", Block* runtime_b = ({
#if ALL_R0 || ALL_R1 || NO_EXTENDED_PROVIDE || RT_VERIFY #if ALL_R0 || ALL_R1 || NO_EXTENDED_PROVIDE || RT_VERIFY
#include PRECOMPILED_FILE(runtime1) #include PRECOMPILED_FILE(runtime1)
#else #else
#include PRECOMPILED_FILE(runtime1x) #include PRECOMPILED_FILE(runtime1x)
#endif #endif
); });
#if ALL_R0 #if ALL_R0
ptr_dec(r0r); ptr_dec(r0r);
@ -579,9 +574,9 @@ void load_init() { // very last init function
#else #else
B prevAsrt = runtime[n_asrt]; B prevAsrt = runtime[n_asrt];
runtime[n_asrt] = bi_casrt; // horrible but GC is off so it's fiiiiiine runtime[n_asrt] = bi_casrt; // horrible but GC is off so it's fiiiiiine
Block* comp_b = load_importBlock("(compiler)", Block* comp_b = ({
#include PRECOMPILED_FILE(compiles) #include PRECOMPILED_FILE(compiles)
); });
runtime[n_asrt] = prevAsrt; runtime[n_asrt] = prevAsrt;
gc_add(load_compgen = evalFunBlockConsume(comp_b)); gc_add(load_compgen = evalFunBlockConsume(comp_b));
@ -601,9 +596,9 @@ void load_init() { // very last init function
gc_add(def_re = ps.b); gc_add(def_re = ps.b);
#if FORMATTER #if FORMATTER
Block* fmt_b = load_importBlock("(formatter)", Block* fmt_b = ({
#include PRECOMPILED_FILE(formatter) #include PRECOMPILED_FILE(formatter)
); });
B fmtM = evalFunBlockConsume(fmt_b); B fmtM = evalFunBlockConsume(fmt_b);
B fmtR = c1(fmtM, m_caB(4, (B[]){incG(bi_type), incG(bi_decp), incG(bi_glyph), incG(bi_repr)})); B fmtR = c1(fmtM, m_caB(4, (B[]){incG(bi_type), incG(bi_decp), incG(bi_glyph), incG(bi_repr)}));
decG(fmtM); SGet(fmtR) decG(fmtM); SGet(fmtR)
@ -647,9 +642,9 @@ B bqn_explain(B str) {
B* o = harr_ptr(def_re); B* o = harr_ptr(def_re);
if (load_explain.u==0) { if (load_explain.u==0) {
B* runtime = harr_ptr(o[re_rt]); B* runtime = harr_ptr(o[re_rt]);
Block* expl_b = load_importBlock("(explain)", Block* expl_b = ({
#include PRECOMPILED_FILE(explain) #include PRECOMPILED_FILE(explain)
); });
gc_add(load_explain = evalFunBlockConsume(expl_b)); gc_add(load_explain = evalFunBlockConsume(expl_b));
} }