diff options
author | Bad Diode <bd@badd10de.dev> | 2021-10-26 08:55:12 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-10-26 08:55:12 +0200 |
commit | 9f1f2ff307bb3da8bfc6809ae05be589e1e37ed9 (patch) | |
tree | e58427f27162f673903c480e7858d77c3a9f6b23 | |
parent | 46356365270b71be94097b3c408d5f35a9ebd6ed (diff) | |
download | bdl-9f1f2ff307bb3da8bfc6809ae05be589e1e37ed9.tar.gz bdl-9f1f2ff307bb3da8bfc6809ae05be589e1e37ed9.zip |
Add OP_DROP and improve error reporting
-rwxr-xr-x | src/bytecode/compiler.h | 1 | ||||
-rwxr-xr-x | src/bytecode/debug.h | 2 | ||||
-rwxr-xr-x | src/bytecode/errors.c | 20 | ||||
-rwxr-xr-x | src/bytecode/errors.h | 1 | ||||
-rwxr-xr-x | src/bytecode/main.c | 20 | ||||
-rwxr-xr-x | src/bytecode/ops.h | 2 | ||||
-rwxr-xr-x | src/bytecode/vm.h | 6 |
7 files changed, 33 insertions, 19 deletions
diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index 6dab1fe..9fefd2a 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h | |||
@@ -504,6 +504,7 @@ compile(Token *tokens) { | |||
504 | Token start_tok = peek_token(&compiler); | 504 | Token start_tok = peek_token(&compiler); |
505 | while (has_next_token(&compiler) && peek_token(&compiler).type != TOKEN_EOF) { | 505 | while (has_next_token(&compiler) && peek_token(&compiler).type != TOKEN_EOF) { |
506 | parse_tree(chunk, &compiler); | 506 | parse_tree(chunk, &compiler); |
507 | add_code(chunk, OP_DROP, start_tok.line, start_tok.column); | ||
507 | } | 508 | } |
508 | add_code(chunk, OP_RETURN, start_tok.line, start_tok.column); | 509 | add_code(chunk, OP_RETURN, start_tok.line, start_tok.column); |
509 | return chunk; | 510 | return chunk; |
diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h index 1e0d4d6..8c4a2eb 100755 --- a/src/bytecode/debug.h +++ b/src/bytecode/debug.h | |||
@@ -39,6 +39,8 @@ static const char* ops_str[] = { | |||
39 | // Procedures. | 39 | // Procedures. |
40 | [OP_CALL] = "OP_CALL", | 40 | [OP_CALL] = "OP_CALL", |
41 | [OP_RETURN] = "OP_RETURN", | 41 | [OP_RETURN] = "OP_RETURN", |
42 | // Clear stack after each statement. | ||
43 | [OP_DROP] = "OP_DROP", | ||
42 | }; | 44 | }; |
43 | 45 | ||
44 | void | 46 | void |
diff --git a/src/bytecode/errors.c b/src/bytecode/errors.c index c2ab77f..b4595a1 100755 --- a/src/bytecode/errors.c +++ b/src/bytecode/errors.c | |||
@@ -29,3 +29,23 @@ error_push(Error error) { | |||
29 | errors[errors_n++] = error; | 29 | errors[errors_n++] = error; |
30 | } | 30 | } |
31 | } | 31 | } |
32 | |||
33 | void | ||
34 | report_errors(char *file_name) { | ||
35 | for (size_t i = 0; i < errors_n; i++) { | ||
36 | Error err = errors[i]; | ||
37 | fprintf(stderr, "%s", file_name); | ||
38 | if (err.line != 0) { | ||
39 | fprintf(stderr, ":%ld:%ld", err.line, err.col); | ||
40 | } | ||
41 | switch (err.type) { | ||
42 | case ERR_TYPE_LEXER: { fprintf(stderr, ": [lexer]"); } break; | ||
43 | case ERR_TYPE_COMPILER: { fprintf(stderr, ": [compiler]"); } break; | ||
44 | case ERR_TYPE_RUNTIME: { fprintf(stderr, ": [runtime]"); } break; | ||
45 | case ERR_TYPE_PARSER: { fprintf(stderr, ": [parser]"); } break; | ||
46 | default: break; | ||
47 | } | ||
48 | fprintf(stderr, " %s\n", error_msgs[err.value]); | ||
49 | } | ||
50 | errors_n = 0; | ||
51 | } | ||
diff --git a/src/bytecode/errors.h b/src/bytecode/errors.h index 7b3446e..425c768 100755 --- a/src/bytecode/errors.h +++ b/src/bytecode/errors.h | |||
@@ -38,6 +38,7 @@ typedef struct Error { | |||
38 | } Error; | 38 | } Error; |
39 | 39 | ||
40 | void error_push(Error error); | 40 | void error_push(Error error); |
41 | void report_errors(char *file_name); | ||
41 | 42 | ||
42 | #define ERR_MAX_NUMBER 16 | 43 | #define ERR_MAX_NUMBER 16 |
43 | 44 | ||
diff --git a/src/bytecode/main.c b/src/bytecode/main.c index 7cb0a2a..7f2042e 100755 --- a/src/bytecode/main.c +++ b/src/bytecode/main.c | |||
@@ -126,15 +126,7 @@ run_file(char *file_name) { | |||
126 | 126 | ||
127 | // Check if there were any errors. | 127 | // Check if there were any errors. |
128 | if (errors_n != 0 && !supress_errors) { | 128 | if (errors_n != 0 && !supress_errors) { |
129 | for (size_t i = 0; i < errors_n; i++) { | 129 | report_errors(file_name); |
130 | Error err = errors[i]; | ||
131 | fprintf(stderr, "%s", file_name); | ||
132 | if (err.line != 0) { | ||
133 | fprintf(stderr, ":%ld:%ld", err.line, err.col); | ||
134 | } | ||
135 | fprintf(stderr, ": %s\n", error_msgs[err.value]); | ||
136 | } | ||
137 | errors_n = 0; | ||
138 | } | 130 | } |
139 | 131 | ||
140 | free(source); | 132 | free(source); |
@@ -164,15 +156,7 @@ run_stdin(void) { | |||
164 | 156 | ||
165 | // Check if there were any errors. | 157 | // Check if there were any errors. |
166 | if (errors_n != 0 && !supress_errors) { | 158 | if (errors_n != 0 && !supress_errors) { |
167 | for (size_t i = 0; i < errors_n; i++) { | 159 | report_errors("stdin"); |
168 | Error err = errors[i]; | ||
169 | fprintf(stderr, "stdin"); | ||
170 | if (err.line != 0) { | ||
171 | fprintf(stderr, ":%ld:%ld", err.line, err.col); | ||
172 | } | ||
173 | fprintf(stderr, ": %s\n", error_msgs[err.value]); | ||
174 | } | ||
175 | errors_n = 0; | ||
176 | } | 160 | } |
177 | 161 | ||
178 | array_free(source); | 162 | array_free(source); |
diff --git a/src/bytecode/ops.h b/src/bytecode/ops.h index d046ca2..7a43b91 100755 --- a/src/bytecode/ops.h +++ b/src/bytecode/ops.h | |||
@@ -33,6 +33,8 @@ typedef enum Ops { | |||
33 | // Procedures. | 33 | // Procedures. |
34 | OP_CALL, | 34 | OP_CALL, |
35 | OP_RETURN, | 35 | OP_RETURN, |
36 | // Clear stack after each statement. | ||
37 | OP_DROP, | ||
36 | } Ops; | 38 | } Ops; |
37 | 39 | ||
38 | #endif // BDL_OPS_H | 40 | #endif // BDL_OPS_H |
diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h index 63eedfa..d654e2c 100755 --- a/src/bytecode/vm.h +++ b/src/bytecode/vm.h | |||
@@ -64,7 +64,8 @@ vm_reset(VM *vm) { | |||
64 | .value = ERR_WRONG_ARG_TYPE, \ | 64 | .value = ERR_WRONG_ARG_TYPE, \ |
65 | .line = frame->chunk->lines[vm->pc - frame->chunk->code - 1].line, \ | 65 | .line = frame->chunk->lines[vm->pc - frame->chunk->code - 1].line, \ |
66 | .col = frame->chunk->lines[vm->pc - frame->chunk->code - 1].col, \ | 66 | .col = frame->chunk->lines[vm->pc - frame->chunk->code - 1].col, \ |
67 | }) | 67 | }); \ |
68 | return | ||
68 | 69 | ||
69 | #define SYMBOL_NOT_FOUND_ERR() \ | 70 | #define SYMBOL_NOT_FOUND_ERR() \ |
70 | error_push((Error){ \ | 71 | error_push((Error){ \ |
@@ -268,6 +269,9 @@ vm_interpret(VM *vm) { | |||
268 | array_head(vm->frames)->size--; | 269 | array_head(vm->frames)->size--; |
269 | frame = &vm->frames[array_size(vm->frames) - 1]; | 270 | frame = &vm->frames[array_size(vm->frames) - 1]; |
270 | } break; | 271 | } break; |
272 | case OP_DROP: { | ||
273 | array_head(vm->stack)->size = 0; | ||
274 | } break; | ||
271 | default: { | 275 | default: { |
272 | error_push((Error){ | 276 | error_push((Error){ |
273 | .type = ERR_TYPE_RUNTIME, | 277 | .type = ERR_TYPE_RUNTIME, |