From d046d92390548a6a343f06454ef276638b693818 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sun, 16 May 2021 13:42:58 +0300 Subject: [PATCH] optional source for precompiled programs --- cc.bqn | 6 +++--- src/heap.c | 4 ++-- src/load.c | 6 ++++++ src/stuff.c | 2 +- src/vm.c | 52 ++++++++++++++++++++++++++++------------------------ 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/cc.bqn b/cc.bqn index 78ade8ca..9429d580 100755 --- a/cc.bqn +++ b/cc.bqn @@ -1,4 +1,4 @@ -#! /usr/bin/env dbqn +#!/usr/bin/env bqn # Modified version of https://github.com/mlochbaum/BQN/blob/master/src/cjs.bqn, which is under the ISC license (https://github.com/mlochbaum/BQN/blob/master/LICENSE) args←•args @@ -28,12 +28,12 @@ FChars ← {𝕨•FChars path∾𝕩} FLines ← {𝕨•FLines path∾𝕩} glyphs ← Import "glyphs.bqn" -_getComp ← { (3+useInd) ↑ (𝕗 Import "c.bqn"){𝔽} } +_getComp ← { (3+2×useInd)↑ <∘⊢∾˜ 4↑ (𝕗 Import "c.bqn"){𝔽} } useInd ← "-i"≡⊑args ⋄ args↓˜↩useInd Comp ← ((<"inc(runtime["∾•Repr∾"])"˙)¨↕62) glyphs _getComp ⊢ J ← ∾∾⟜(@+10)¨ Fconst ← ≡◶⟨@⊸≤◶Num‿Char, Str, ⊑⟩ -Fout ← {((≠𝕩)↑⟨LI F¨,L Fconst¨,L (LI ·F¨ 4↑⊢)¨,L (L F¨)¨⟩) {𝕎𝕩}¨ 𝕩} +Fout ← {((≠𝕩)↑⟨LI F¨ ⋄ L Fconst¨ ⋄ L (LI ·F¨ 4↑⊢)¨ ⋄ L (LI F¨)¨ ⋄ Str⟩) {𝕎𝕩}¨ 𝕩} Frun ← 1⊸Fout Long ← {¯2↓∾𝕩∾¨<","∾@+10} LFC ← Long∘Fout∘Comp diff --git a/src/heap.c b/src/heap.c index 4c4aca81..ea12d433 100644 --- a/src/heap.c +++ b/src/heap.c @@ -21,7 +21,7 @@ void heapVerify_checkFn(Value* v) { printf("delta %d for %s: ", (i32)v->refc, format_type(v->type)); #endif heap_observed = v; - print(tag(v,OBJ_TAG)); puts(""); + print(tag(v,OBJ_TAG)); putchar('\n'); } } @@ -36,7 +36,7 @@ bool heapVerify_visitP(void* x) { Value* v = x; if(heapVerify_mode==0) v->refc--; else if(heapVerify_mode==1) v->refc++; - else if(heapVerify_mode==2) if (x==heap_observed) { printf("referee: %p ", heap_curr); print(tag(heap_curr,OBJ_TAG)); puts(""); } + else if(heapVerify_mode==2) if (x==heap_observed) { printf("referee: %p ", heap_curr); print(tag(heap_curr,OBJ_TAG)); putchar('\n'); } return true; } diff --git a/src/load.c b/src/load.c index 03529f2f..b1d8eac3 100644 --- a/src/load.c +++ b/src/load.c @@ -9,9 +9,15 @@ Block* load_compObj(B x, B src) { // consumes dec(x); return r; } +#ifdef RT_SRC +Block* load_compImport(B bc, B objs, B blocks, B inds, B src) { // consumes all + return compile(bc, objs, blocks, inds, bi_N, src); +} +#else Block* load_compImport(B bc, B objs, B blocks) { // consumes all return compile(bc, objs, blocks, bi_N, bi_N, bi_N); } +#endif B load_comp; B load_compArg; diff --git a/src/stuff.c b/src/stuff.c index f3819289..84cb186b 100644 --- a/src/stuff.c +++ b/src/stuff.c @@ -215,7 +215,7 @@ u8 fillElType(B x) { Value* VALIDATEP(Value* x) { if (x->refc<=0 || (x->refc>>28) == 'a' || x->type==t_empty) { printf("bad refcount for type %d: %d\nattempting to print: ", x->type, x->refc); fflush(stdout); - print(tag(x,OBJ_TAG)); puts(""); fflush(stdout); + print(tag(x,OBJ_TAG)); putchar('\n'); fflush(stdout); err(""); } if (ti[x->type].isArr) { diff --git a/src/vm.c b/src/vm.c index 616d9688..7b61da8f 100644 --- a/src/vm.c +++ b/src/vm.c @@ -373,7 +373,7 @@ B evalBC(Body* b, Scope* sc) { // doesn't consume vmStack[stackNum] = bcPos; for(i32 i = 0; i < bcDepth; i++) printf(" "); printBC(sbc); printf("@%d << ", bcPos); - for (i32 i = 0; i < b->maxStack; i++) { if(i)printf(" ⋄ "); print(gStack[i]); } puts(""); fflush(stdout); + for (i32 i = 0; i < b->maxStack; i++) { if(i)printf(" ⋄ "); print(gStack[i]); } putchar('\n'); fflush(stdout); bcCtr++; for (i32 i = 0; i < sc->varAm; i++) VALIDATE(sc->vars[i]); #endif @@ -497,7 +497,7 @@ B evalBC(Body* b, Scope* sc) { // doesn't consume #ifdef DEBUG_VM for(i32 i = 0; i < bcDepth; i++) printf(" "); printBC(sbc); printf("@%ld: ", sbc-c(I32Arr,b->comp->bc)->a); - for (i32 i = 0; i < b->maxStack; i++) { if(i)printf(" ⋄ "); print(gStack[i]); } puts(""); fflush(stdout); + for (i32 i = 0; i < b->maxStack; i++) { if(i)printf(" ⋄ "); print(gStack[i]); } putchar('\n'); fflush(stdout); #endif } end:; @@ -685,6 +685,31 @@ void popCatch() { #endif } +NOINLINE void vm_printPos(Comp* comp, i32 bcPos, i64 pos) { + B src = comp->src; + if (!isNothing(src) && !isNothing(comp->indices)) { + B inds = TI(comp->indices).getU(comp->indices, 0); usz cs = o2s(TI(inds).getU(inds,bcPos)); + B inde = TI(comp->indices).getU(comp->indices, 1); usz ce = o2s(TI(inde).getU(inde,bcPos))+1; + int start = pos==-1? 0 : printf("%ld: ", pos); + usz srcL = a(src)->ia; + BS2B srcGetU = TI(src).getU; + usz srcS = cs; + while (srcS>0 && o2cu(srcGetU(src,srcS-1))!='\n') srcS--; + usz srcE = srcS; + while (srcEsrcE) ce = srcE; + cs-= srcS; + ce-= srcS; + putchar('\n'); + for (i32 i = 0; i < cs+start; i++) putchar(' '); + for (i32 i = cs; i < ce; i++) putchar('^'); + putchar('\n'); + // printf(" inds:%d…%d\n", cinds, cinde); + } else { + if (pos!=-1) printf("%ld: ", pos); + printf("source unknown\n"); + } +} NOINLINE void vm_pst(Env* s, Env* e) { assert(s<=e); @@ -698,28 +723,7 @@ NOINLINE void vm_pst(Env* s, Env* e) { } i32 bcPos = c->bcV; Comp* comp = c->sc->body->comp; - B src = comp->src; - if (!isNothing(src) && !isNothing(comp->indices)) { - B inds = TI(comp->indices).getU(comp->indices, 0); usz cs = o2s(TI(inds).getU(inds,bcPos)); - B inde = TI(comp->indices).getU(comp->indices, 1); usz ce = o2s(TI(inde).getU(inde,bcPos))+1; - int start = printf("%ld: ", i); - usz srcL = a(src)->ia; - BS2B srcGetU = TI(src).getU; - usz srcS = cs; - while (srcS>0 && o2cu(srcGetU(src,srcS-1))!='\n') srcS--; - usz srcE = srcS; - while (srcEsrcE) ce = srcE; - cs-= srcS; - ce-= srcS; - putchar('\n'); - for (i32 i = 0; i < cs+start; i++) putchar(' '); - for (i32 i = cs; i < ce; i++) putchar('^'); - putchar('\n'); - // printf(" inds:%d…%d\n", cinds, cinde); - } else { - printf("%ld: source unknown\n", i); - } + vm_printPos(comp, bcPos, i); i--; } }