diff --git a/WL.c b/WL.c index 7adedd2..cda680b 100644 --- a/WL.c +++ b/WL.c @@ -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); - printf("\n"); -*/ + + 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); +} diff --git a/WL.h b/WL.h index d806438..f0118bb 100644 --- a/WL.h +++ b/WL.h @@ -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); diff --git a/main.c b/main.c index fcac6f7..79d5571 100644 --- a/main.c +++ b/main.c @@ -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); @@ -63,7 +66,10 @@ int main(int argc, char **argv) assert(result.type == WL_COMPILE_RESULT_FILE); path = result.path; - } + } + + for (int i = 0; i < num_loaded_files; i++) + free(loaded_files[i]); WL_Compiler_free(compiler); @@ -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); diff --git a/src/assemble.c b/src/assemble.c index d8cbedd..6660606 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -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; diff --git a/src/compile.c b/src/compile.c index 82006fe..8f71e3a 100644 --- a/src/compile.c +++ b/src/compile.c @@ -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); +} \ No newline at end of file diff --git a/src/compile.h b/src/compile.h index e5def46..0022ceb 100644 --- a/src/compile.h +++ b/src/compile.h @@ -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); diff --git a/src/eval.c b/src/eval.c index f719f99..2c4f3fe 100644 --- a/src/eval.c +++ b/src/eval.c @@ -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); - printf("\n"); -*/ + + 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) { diff --git a/src/parse.c b/src/parse.c index 241da09..91f3281 100644 --- a/src/parse.c +++ b/src/parse.c @@ -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 }; } diff --git a/src/value.c b/src/value.c index 3eb0d48..5fbd922 100644 --- a/src/value.c +++ b/src/value.c @@ -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;