Bug fixes

This commit is contained in:
2025-08-08 15:14:37 +02:00
parent acf2ea7b97
commit 0e5163cc18
9 changed files with 108 additions and 26 deletions
+45 -11
View File
@@ -689,12 +689,6 @@ Token next_token(Parser *p)
if (streq(kword, S("false"))) return (Token) { .type=TOKEN_KWORD_FALSE };
if (streq(kword, S("include"))) return (Token) { .type=TOKEN_KWORD_INCLUDE };
kword = copystr(kword, p->a);
if (kword.len == 0) {
parser_report(p, "Out of memory");
return (Token) { .type=TOKEN_ERROR };
}
return (Token) { .type=TOKEN_IDENT, .sval=kword };
}
@@ -4040,6 +4034,28 @@ char *print_instruction(char *p, char *data)
case OPCODE_PUSHFL:
printf("PUSHFL");
break;
case OPCODE_FOR:
{
uint8_t a;
memcpy(&a, p, sizeof(uint8_t));
p += sizeof(uint8_t);
uint8_t b;
memcpy(&b, p, sizeof(uint8_t));
p += sizeof(uint8_t);
uint8_t c;
memcpy(&c, p, sizeof(uint8_t));
p += sizeof(uint8_t);
uint32_t d;
memcpy(&d, p, sizeof(uint32_t));
p += sizeof(uint32_t);
printf("FOR %u %u %u %u", a, b, c, d);
}
break;
}
return p;
@@ -4572,7 +4588,7 @@ static void value_to_string_inner(Value v, ToStringContext *tostr)
int cursor = 0;
while (batch) {
int num = ITEMS_PER_MAP_BATCH;
int num = ITEMS_PER_ARRAY_BATCH;
if (batch->next == NULL)
num = a->tail_count;
@@ -4644,6 +4660,8 @@ struct WL_State {
String data;
int off;
bool trace;
WL_Arena *a;
char *errbuf;
@@ -4693,6 +4711,8 @@ void eval_report(WL_State *state, char *fmt, ...)
static uint8_t read_u8(WL_State *state)
{
assert(state->off >= 0);
assert(state->off < state->code.len);
return state->code.ptr[state->off++];
}
@@ -4726,11 +4746,12 @@ static double read_f64(WL_State *state)
int step(WL_State *state)
{
uint8_t opcode = read_u8(state);
/*
printf("%-3d: ", e->off);
print_instruction(state->code.ptr + e->off, e->data.ptr);
if (state->trace) {
printf("%-3d: ", state->off-1);
print_instruction(state->code.ptr + state->off - 1, state->data.ptr);
printf("\n");
*/
}
switch (opcode) {
@@ -5505,6 +5526,8 @@ int step(WL_State *state)
uint8_t var_2 = read_u8(state);
uint32_t end = read_u32(state);
printf("for end %u\n", end); // TODO
int base;
{
int group = state->frames[state->num_frames-1].group;
@@ -5578,6 +5601,7 @@ WL_State *WL_State_init(WL_Arena *a, WL_Program p, char *err, int errmax)
.code=code,
.data=data,
.off=0,
.trace=false,
.a=a,
.errbuf=err,
.errmax=errmax,
@@ -5601,6 +5625,11 @@ void WL_State_free(WL_State *state)
// TODO
}
void WL_State_trace(WL_State *state, int trace)
{
state->trace = (trace != 0);
}
WL_Result WL_eval(WL_State *state)
{
if (state->sysvar.len > 0) {
@@ -6105,3 +6134,8 @@ WL_CompileResult WL_compile(WL_Compiler *compiler, WL_String file, WL_String con
return (WL_CompileResult) { .type=WL_COMPILE_RESULT_DONE, .program=ares.program };
}
void WL_dump_program(WL_Program program)
{
print_program(program);
}
+3
View File
@@ -64,8 +64,11 @@ void WL_Compiler_free (WL_Compiler *compiler);
WL_CompileResult WL_compile (WL_Compiler *compiler, WL_String file, WL_String content);
WL_State* WL_State_init (WL_Arena *a, WL_Program p, char *err, int errmax);
void WL_State_free (WL_State *state);
void WL_State_trace (WL_State *state, int trace);
WL_Result WL_eval (WL_State *state);
void WL_dump_program(WL_Program program);
int WL_streq (WL_String a, char *b, int blen);
int WL_peeknone (WL_State *state, int off);
int WL_peekint (WL_State *state, int off, long long *x);
+9 -1
View File
@@ -23,6 +23,9 @@ int main(int argc, char **argv)
assert(0); // TODO
}
int num_loaded_files = 0;
char *loaded_files[128];
WL_CompileResult result;
WL_String path = { file, strlen(file) };
for (int i = 0;; i++) {
@@ -51,7 +54,7 @@ int main(int argc, char **argv)
result = WL_compile(compiler, path, (WL_String) { file_data, file_size });
free(file_data);
loaded_files[num_loaded_files++] = file_data;
if (result.type == WL_COMPILE_RESULT_ERROR) {
printf("Compilation of '%.*s' failed\n", path.len, path.ptr);
@@ -65,6 +68,9 @@ int main(int argc, char **argv)
path = result.path;
}
for (int i = 0; i < num_loaded_files; i++)
free(loaded_files[i]);
WL_Compiler_free(compiler);
if (result.type == WL_COMPILE_RESULT_ERROR) {
@@ -75,6 +81,8 @@ int main(int argc, char **argv)
WL_State *state = WL_State_init(&a, program, err, (int) sizeof(err));
WL_State_trace(state, 0);
for (bool done = false; !done; ) {
WL_Result result = WL_eval(state);
+22
View File
@@ -1594,6 +1594,28 @@ char *print_instruction(char *p, char *data)
case OPCODE_PUSHFL:
printf("PUSHFL");
break;
case OPCODE_FOR:
{
uint8_t a;
memcpy(&a, p, sizeof(uint8_t));
p += sizeof(uint8_t);
uint8_t b;
memcpy(&b, p, sizeof(uint8_t));
p += sizeof(uint8_t);
uint8_t c;
memcpy(&c, p, sizeof(uint8_t));
p += sizeof(uint8_t);
uint32_t d;
memcpy(&d, p, sizeof(uint32_t));
p += sizeof(uint32_t);
printf("FOR %u %u %u %u", a, b, c, d);
}
break;
}
return p;
+5
View File
@@ -116,3 +116,8 @@ WL_CompileResult WL_compile(WL_Compiler *compiler, WL_String file, WL_String con
return (WL_CompileResult) { .type=WL_COMPILE_RESULT_DONE, .program=ares.program };
}
void WL_dump_program(WL_Program program)
{
print_program(program);
}
+3
View File
@@ -55,8 +55,11 @@ void WL_Compiler_free (WL_Compiler *compiler);
WL_CompileResult WL_compile (WL_Compiler *compiler, WL_String file, WL_String content);
WL_State* WL_State_init (WL_Arena *a, WL_Program p, char *err, int errmax);
void WL_State_free (WL_State *state);
void WL_State_trace (WL_State *state, int trace);
WL_Result WL_eval (WL_State *state);
void WL_dump_program(WL_Program program);
int WL_streq (WL_String a, char *b, int blen);
int WL_peeknone (WL_State *state, int off);
int WL_peekint (WL_State *state, int off, long long *x);
+17 -4
View File
@@ -20,6 +20,8 @@ struct WL_State {
String data;
int off;
bool trace;
WL_Arena *a;
char *errbuf;
@@ -69,6 +71,8 @@ void eval_report(WL_State *state, char *fmt, ...)
static uint8_t read_u8(WL_State *state)
{
assert(state->off >= 0);
assert(state->off < state->code.len);
return state->code.ptr[state->off++];
}
@@ -102,11 +106,12 @@ static double read_f64(WL_State *state)
int step(WL_State *state)
{
uint8_t opcode = read_u8(state);
/*
printf("%-3d: ", e->off);
print_instruction(state->code.ptr + e->off, e->data.ptr);
if (state->trace) {
printf("%-3d: ", state->off-1);
print_instruction(state->code.ptr + state->off - 1, state->data.ptr);
printf("\n");
*/
}
switch (opcode) {
@@ -881,6 +886,8 @@ int step(WL_State *state)
uint8_t var_2 = read_u8(state);
uint32_t end = read_u32(state);
printf("for end %u\n", end); // TODO
int base;
{
int group = state->frames[state->num_frames-1].group;
@@ -954,6 +961,7 @@ WL_State *WL_State_init(WL_Arena *a, WL_Program p, char *err, int errmax)
.code=code,
.data=data,
.off=0,
.trace=false,
.a=a,
.errbuf=err,
.errmax=errmax,
@@ -977,6 +985,11 @@ void WL_State_free(WL_State *state)
// TODO
}
void WL_State_trace(WL_State *state, int trace)
{
state->trace = (trace != 0);
}
WL_Result WL_eval(WL_State *state)
{
if (state->sysvar.len > 0) {
-6
View File
@@ -255,12 +255,6 @@ Token next_token(Parser *p)
if (streq(kword, S("false"))) return (Token) { .type=TOKEN_KWORD_FALSE };
if (streq(kword, S("include"))) return (Token) { .type=TOKEN_KWORD_INCLUDE };
kword = copystr(kword, p->a);
if (kword.len == 0) {
parser_report(p, "Out of memory");
return (Token) { .type=TOKEN_ERROR };
}
return (Token) { .type=TOKEN_IDENT, .sval=kword };
}
+1 -1
View File
@@ -470,7 +470,7 @@ static void value_to_string_inner(Value v, ToStringContext *tostr)
int cursor = 0;
while (batch) {
int num = ITEMS_PER_MAP_BATCH;
int num = ITEMS_PER_ARRAY_BATCH;
if (batch->next == NULL)
num = a->tail_count;