fix path_dir, allow REPL to load relative files

This commit is contained in:
dzaima 2021-05-15 14:17:19 +03:00
parent 87138ee523
commit 721017f6c0
3 changed files with 7 additions and 3 deletions

View File

@ -49,10 +49,13 @@ B path_resolve(B base, B rel) { // consumes rel; may error; assumes base is a ch
}
B path_dir(B path) { // consumes; returns directory part of file path, with trailing slash; may error
assert(isArr(path) || isNothing(path));
if (isNothing(path)) return path;
BS2B pgetU = TI(path).getU;
usz pia = a(path)->ia;
if (pia==0) thrM("Empty file path");
for (usz i = 0; i < pia; i++) if (!isC32(pgetU(path, i))) thrM("Paths must be character vectors");
for (usz i = pia-1; i >= 0; i--) {
for (i64 i = (i64)pia-1; i >= 0; i--) {
if (o2cu(pgetU(path, i))=='/') {
B r = TI(path).slice(path, 0);
arr_shVec(r, i+1);

View File

@ -24,7 +24,7 @@ B bqn_fmt(B x) { // consumes
#endif
void load_gcFn() { mm_visit(comp_currPath); }
Block* bqn_comp(B str, B path) { // consumes
Block* bqn_comp(B str, B path) { // consumes both
comp_currPath = path;
Block* r = load_compObj(c2(load_comp, inc(load_compArg), inc(str)), str);
dec(path);

View File

@ -88,12 +88,13 @@ int main(int argc, char* argv[]) {
vm_pst(envCurr, envStart+envPrevHeight);
dec(catchMessage);
}
B replPath = m_str32(U"REPL"); gc_add(replPath);
while (true) { // exit by evaluating an empty expression
char* ln = NULL;
size_t gl = 0;
getline(&ln, &gl, stdin);
if (ln[0]==0 || ln[0]==10) break;
Block* block = bqn_comp(fromUTF8(ln, strlen(ln)), bi_N);
Block* block = bqn_comp(fromUTF8(ln, strlen(ln)), replPath);
free(ln);
#ifdef TIME