execute argument file

This commit is contained in:
dzaima 2021-05-15 12:39:12 +03:00
parent 77531e2d8f
commit ca090efd07
3 changed files with 56 additions and 46 deletions

View File

@ -153,4 +153,8 @@ static inline void load_init() {
#endif #endif
gc_enable(); gc_enable();
#endif // NO_COMP #endif // NO_COMP
}
B bqn_execFile(B path) {
return bqn_exec(file_chars(path));
} }

View File

@ -50,7 +50,7 @@
#include "rtPerf.c" #include "rtPerf.c"
#include "load.c" #include "load.c"
int main() { int main(int argc, char* argv[]) {
cbqn_init(); cbqn_init();
// expects a copy of mlochbaum/BQN/src/c.bqn to be at the execution directory (with •args replaced with the array in glyphs.bqn) // expects a copy of mlochbaum/BQN/src/c.bqn to be at the execution directory (with •args replaced with the array in glyphs.bqn)
@ -81,48 +81,51 @@ int main() {
} }
#endif #endif
if (argc==1) {
while (CATCH) { while (CATCH) {
printf("Error: "); print(catchMessage); putchar('\n'); printf("Error: "); print(catchMessage); putchar('\n');
vm_pst(envCurr, envStart+envPrevHeight); vm_pst(envCurr, envStart+envPrevHeight);
dec(catchMessage); dec(catchMessage);
} }
while (true) { // exit by evaluating an empty expression while (true) { // exit by evaluating an empty expression
char* ln = NULL; char* ln = NULL;
size_t gl = 0; size_t gl = 0;
getline(&ln, &gl, stdin); getline(&ln, &gl, stdin);
if (ln[0]==0 || ln[0]==10) break; if (ln[0]==0 || ln[0]==10) break;
Block* block = bqn_comp(fromUTF8(ln, strlen(ln))); Block* block = bqn_comp(fromUTF8(ln, strlen(ln)));
free(ln); free(ln);
#ifdef TIME #ifdef TIME
u64 sns = nsTime(); u64 sns = nsTime();
B res = m_funBlock(block, 0); B res = m_funBlock(block, 0);
u64 ens = nsTime(); u64 ens = nsTime();
printf("%fms\n", (ens-sns)/1e6); printf("%fms\n", (ens-sns)/1e6);
#else #else
B res = m_funBlock(block, 0); B res = m_funBlock(block, 0);
#endif #endif
ptr_dec(block); ptr_dec(block);
#ifdef FORMATTER #ifdef FORMATTER
B resFmt = bqn_fmt(res); B resFmt = bqn_fmt(res);
printRaw(resFmt); dec(resFmt); printRaw(resFmt); dec(resFmt);
printf("\n"); printf("\n");
#else #else
print(res); putchar('\n'); fflush(stdout); print(res); putchar('\n'); fflush(stdout);
dec(res); dec(res);
#endif #endif
#ifdef HEAP_VERIFY #ifdef HEAP_VERIFY
heapVerify(); heapVerify();
#endif #endif
gc_maybeGC(); gc_maybeGC();
#ifdef DEBUG #ifdef DEBUG
#endif #endif
}
popCatch();
} else {
bqn_execFile(m_str8(strlen(argv[1]), argv[1]));
} }
rtPerf_print(); rtPerf_print();
popCatch();
CTR_FOR(CTR_PRINT) CTR_FOR(CTR_PRINT)
// printf("done\n");fflush(stdout); while(1); // printf("done\n");fflush(stdout); while(1);
printAllocStats(); printAllocStats();

View File

@ -151,7 +151,7 @@ typedef struct TmpFile { // to be turned into a proper I8Arr
i8 a[]; i8 a[];
} TmpFile; } TmpFile;
TmpFile* readFile(B path) { // consumes TmpFile* file_bytes(B path) { // consumes; may throw
u64 plen = utf8lenB(path); u64 plen = utf8lenB(path);
char p[plen+1]; char p[plen+1];
toUTF8(path, p); toUTF8(path, p);
@ -168,15 +168,18 @@ TmpFile* readFile(B path) { // consumes
dec(path); dec(path);
return src; return src;
} }
B file_chars(B path) { // consumes; may throw
B fchars_c1(B t, B x) { TmpFile* c = file_bytes(path);
TmpFile* c = readFile(x);
B r = fromUTF8((char*)c->a, c->ia); B r = fromUTF8((char*)c->a, c->ia);
ptr_dec(c); ptr_dec(c);
return r; return r;
} }
B fchars_c1(B t, B x) {
return file_chars(x);
}
B fbytes_c1(B t, B x) { B fbytes_c1(B t, B x) {
TmpFile* f = readFile(x); usz ia = f->ia; u8* p = (u8*)f->a; TmpFile* f = file_bytes(x); usz ia = f->ia; u8* p = (u8*)f->a;
u32* rp; B r = m_c32arrv(&rp, ia); u32* rp; B r = m_c32arrv(&rp, ia);
for (i64 i = 0; i < ia; i++) rp[i] = p[i]; for (i64 i = 0; i < ia; i++) rp[i] = p[i];
ptr_dec(f); ptr_dec(f);