aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-12 17:26:26 +0200
committerBad Diode <bd@badd10de.dev>2021-10-12 17:26:26 +0200
commitaf372e9cb20abdd4645a3b4987589e30cc44bcf3 (patch)
tree4d9c0885f1fced949dd6591daac1dbf5f2a368dd
parentf71eaa8cf5212bbacc5b7a6d1260f4af7f257c58 (diff)
downloadbdl-af372e9cb20abdd4645a3b4987589e30cc44bcf3.tar.gz
bdl-af372e9cb20abdd4645a3b4987589e30cc44bcf3.zip
Add ast building and display to processing func
-rwxr-xr-xsrc/bootstrap/main.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c
index 7323629..e6155b4 100755
--- a/src/bootstrap/main.c
+++ b/src/bootstrap/main.c
@@ -10,6 +10,7 @@
10#include "errors.c" 10#include "errors.c"
11#include "lexer.c" 11#include "lexer.c"
12#include "objects.c" 12#include "objects.c"
13#include "parser.c"
13 14
14void 15void
15init(void) { 16init(void) {
@@ -17,6 +18,7 @@ init(void) {
17 obj_nil = alloc_object(OBJ_TYPE_NIL); 18 obj_nil = alloc_object(OBJ_TYPE_NIL);
18 obj_true = alloc_object(OBJ_TYPE_BOOL); 19 obj_true = alloc_object(OBJ_TYPE_BOOL);
19 obj_false = alloc_object(OBJ_TYPE_BOOL); 20 obj_false = alloc_object(OBJ_TYPE_BOOL);
21 obj_err = alloc_object(OBJ_TYPE_ERR);
20} 22}
21 23
22void 24void
@@ -29,10 +31,19 @@ process_source(const StringView *source) {
29 return; 31 return;
30 } 32 }
31 33
32 // Print tokens. 34 Visitor visitor = (Visitor){
33 for (size_t i = 0; i < tokens.size; i++) { 35 .tokens = tokens,
34 Token tok = tokens.buf[i]; 36 .current = 0,
35 print_token(tok); 37 };
38 while (has_next_token(&visitor) && peek_token(&visitor).type != TOKEN_EOF) {
39 Object *root = parse_tree(&visitor);
40 if (root == obj_err || errors_n != 0) {
41 free_objects(root);
42 break;
43 }
44 display(root);
45 printf("\n");
46 free_objects(root);
36 } 47 }
37 48
38 if (tokens.buf != NULL) { 49 if (tokens.buf != NULL) {
@@ -69,7 +80,6 @@ run_repl(void) {
69 errors_n = 0; 80 errors_n = 0;
70 continue; 81 continue;
71 } 82 }
72
73 if (sv.n != 0) { 83 if (sv.n != 0) {
74 printf("\n"); 84 printf("\n");
75 } 85 }
@@ -104,11 +114,11 @@ run_file(char *file_name) {
104 if (errors_n != 0) { 114 if (errors_n != 0) {
105 for (size_t i = 0; i < errors_n; i++) { 115 for (size_t i = 0; i < errors_n; i++) {
106 Error err = errors[i]; 116 Error err = errors[i];
107 printf("%s", file_name); 117 fprintf(stderr, "%s", file_name);
108 if (err.line != 0) { 118 if (err.line != 0) {
109 printf(":%ld:%ld", err.line, err.col); 119 fprintf(stderr, ":%ld:%ld: ", err.line, err.col);
110 } 120 }
111 printf("%s\n", error_msgs[err.value]); 121 fprintf(stderr, "%s\n", error_msgs[err.value]);
112 } 122 }
113 errors_n = 0; 123 errors_n = 0;
114 } 124 }
@@ -146,11 +156,11 @@ run_stdin(void) {
146 if (errors_n != 0) { 156 if (errors_n != 0) {
147 for (size_t i = 0; i < errors_n; i++) { 157 for (size_t i = 0; i < errors_n; i++) {
148 Error err = errors[i]; 158 Error err = errors[i];
149 printf("stdin"); 159 fprintf(stderr, "stdin");
150 if (err.line != 0) { 160 if (err.line != 0) {
151 printf(":%ld:%ld: ", err.line, err.col); 161 fprintf(stderr, ":%ld:%ld: ", err.line, err.col);
152 } 162 }
153 printf("%s\n", error_msgs[err.value]); 163 fprintf(stderr, "%s\n", error_msgs[err.value]);
154 } 164 }
155 errors_n = 0; 165 errors_n = 0;
156 } 166 }