Fix more crashing bugs
diff --git a/include/parse.h b/include/parse.h
index 6bdd692..66e6302 100644
--- a/include/parse.h
+++ b/include/parse.h
@@ -149,7 +149,7 @@
BcStatus bc_parse_pushName(BcParse* p, char *name);
BcStatus bc_parse_pushIndex(BcParse* p, size_t idx);
BcStatus bc_parse_number(BcParse *p, BcInst *prev, size_t* nexs);
-void bc_parse_file(BcParse *p, const char *file);
+BcStatus bc_parse_text(BcParse *p, const char *text);
#ifdef BC_ENABLED
extern const bool bc_parse_exprs[];
diff --git a/src/parse.c b/src/parse.c
index 78e54e1..f3b3e49 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -84,10 +84,9 @@
return s;
}
-void bc_parse_file(BcParse *p, const char *file) {
- bc_lex_file(&p->l, file);
- p->func = BC_PROG_MAIN;
+BcStatus bc_parse_text(BcParse *p, const char *text) {
p->code = BC_PARSE_CODE(p);
+ return bc_lex_text(&p->l, text);
}
BcStatus bc_parse_reset(BcParse *p, BcStatus s) {
@@ -134,7 +133,7 @@
p->parse = parse;
p->prog = prog;
p->func = func;
- p->code = &(((BcFunc*) bc_vec_item(&prog->fns, func))->code);
+ p->code = BC_PARSE_CODE(p);
p->auto_part = (p->nbraces = 0);
return s;
diff --git a/src/program.c b/src/program.c
index f89decb..a375503 100644
--- a/src/program.c
+++ b/src/program.c
@@ -271,8 +271,8 @@
if ((s = bc_io_getline(&buf, "read> "))) goto io_err;
if ((s = p->parse_init(&parse, p, BC_PROG_READ))) goto io_err;
- bc_parse_file(&parse, bc_program_stdin_name);
- if ((s = bc_lex_text(&parse.l, buf.v))) goto exec_err;
+ bc_lex_file(&parse.l, bc_program_stdin_name);
+ if ((s = bc_parse_text(&parse, buf.v))) goto exec_err;
if ((s = p->parse_expr(&parse, BC_PARSE_NOREAD))) return s;
@@ -1241,7 +1241,7 @@
if (!f->code.len) {
if ((s = p->parse_init(&prs, p, fidx))) goto exit;
- if ((s = bc_lex_text(&prs.l, *str))) goto err;
+ if ((s = bc_parse_text(&prs, *str))) goto err;
if ((s = p->parse_expr(&prs, BC_PARSE_NOCALL))) goto err;
@@ -1263,6 +1263,7 @@
err:
bc_parse_free(&prs);
+ f = bc_vec_item(&p->fns, fidx);
bc_vec_npop(&f->code, f->code.len);
exit:
bc_vec_pop(&p->results);
diff --git a/src/vm.c b/src/vm.c
index fc7b565..62efacb 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -140,7 +140,7 @@
BcStatus bc_vm_process(BcVm *vm, const char *text) {
- BcStatus s = bc_lex_text(&vm->prs.l, text);
+ BcStatus s = bc_parse_text(&vm->prs, text);
if ((s = bc_vm_error(s, vm->prs.l.f, vm->prs.l.line))) return s;
@@ -190,7 +190,7 @@
vm->prog.file = file;
if ((s = bc_io_fread(file, &data))) return bc_vm_error(s, file, 0);
- bc_parse_file(&vm->prs, file);
+ bc_lex_file(&vm->prs.l, file);
if ((s = bc_vm_process(vm, data))) goto err;
main_func = bc_vec_item(&vm->prog.fns, BC_PROG_MAIN);
@@ -213,7 +213,7 @@
bool comment = false, notend;
vm->prog.file = bc_program_stdin_name;
- bc_parse_file(&vm->prs, bc_program_stdin_name);
+ bc_lex_file(&vm->prs.l, bc_program_stdin_name);
if ((s = bc_vec_init(&buffer, sizeof(char), NULL))) return s;
if ((s = bc_vec_init(&buf, sizeof(char), NULL))) goto buf_err;
@@ -291,8 +291,8 @@
#ifdef BC_ENABLED
if (vm->flags & BC_FLAG_L) {
- bc_parse_file(&vm->prs, bc_lib_name);
- if ((s = bc_lex_text(&vm->prs.l, bc_lib))) return s;
+ bc_lex_file(&vm->prs.l, bc_lib_name);
+ if ((s = bc_parse_text(&vm->prs, bc_lib))) return s;
while (!s && vm->prs.l.t.t != BC_LEX_EOF) s = vm->prs.parse(&vm->prs);