diff options
author | Bad Diode <bd@badd10de.dev> | 2024-06-21 12:58:58 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2024-06-21 12:58:58 +0200 |
commit | d7fce26ee176c3b62c769b26781edf39105cf8d1 (patch) | |
tree | 94347a9a1cac956679c1c72e6c184b203af595b7 | |
parent | 4646adb64119d9bd761447024a2f35cc0c9c2115 (diff) | |
download | bdl-d7fce26ee176c3b62c769b26781edf39105cf8d1.tar.gz bdl-d7fce26ee176c3b62c769b26781edf39105cf8d1.zip |
Remove unnecessary scope creation.
-rw-r--r-- | src/main.c | 91 |
1 files changed, 59 insertions, 32 deletions
@@ -23,12 +23,19 @@ init(void) { | |||
23 | } | 23 | } |
24 | 24 | ||
25 | typedef enum { | 25 | typedef enum { |
26 | SYM_VAR, | 26 | SYM_UNKNOWN, |
27 | SYM_BUILTIN, | ||
27 | SYM_FUN, | 28 | SYM_FUN, |
29 | SYM_VAR, | ||
28 | SYM_PARAM, | 30 | SYM_PARAM, |
29 | SYM_BUILTIN, | ||
30 | } SymbolKind; | 31 | } SymbolKind; |
31 | 32 | ||
33 | Str sym_kind_str[] = { | ||
34 | [SYM_UNKNOWN] = cstr("UNKNOWN "), [SYM_BUILTIN] = cstr("BUILTIN "), | ||
35 | [SYM_FUN] = cstr("FUNCTION "), [SYM_VAR] = cstr("VARIABLE "), | ||
36 | [SYM_PARAM] = cstr("PARAMETER "), | ||
37 | }; | ||
38 | |||
32 | typedef struct Symbol { | 39 | typedef struct Symbol { |
33 | SymbolKind kind; | 40 | SymbolKind kind; |
34 | } Symbol; | 41 | } Symbol; |
@@ -102,7 +109,15 @@ analyzer_symbols(Analyzer *a, Node *node, Scope *scope) { | |||
102 | symmap_insert(&scope->symbols, param->param_name->value.str, | 109 | symmap_insert(&scope->symbols, param->param_name->value.str, |
103 | (Symbol){.kind = SYM_PARAM}, a->storage); | 110 | (Symbol){.kind = SYM_PARAM}, a->storage); |
104 | } | 111 | } |
105 | analyzer_symbols(a, node->func_body, scope); | 112 | if (node->func_body->kind == NODE_BLOCK) { |
113 | node = node->func_body; | ||
114 | for (sz i = 0; i < array_size(node->elements); i++) { | ||
115 | Node *expr = node->elements[i]; | ||
116 | analyzer_symbols(a, expr, scope); | ||
117 | } | ||
118 | } else { | ||
119 | analyzer_symbols(a, node->func_body, scope); | ||
120 | } | ||
106 | } break; | 121 | } break; |
107 | case NODE_COND: | 122 | case NODE_COND: |
108 | case NODE_MATCH: { | 123 | case NODE_MATCH: { |
@@ -114,7 +129,12 @@ analyzer_symbols(Analyzer *a, Node *node, Scope *scope) { | |||
114 | analyzer_symbols(a, expr, scope); | 129 | analyzer_symbols(a, expr, scope); |
115 | } | 130 | } |
116 | } break; | 131 | } break; |
117 | case NODE_RETURN: | 132 | case NODE_RETURN: { |
133 | for (sz i = 0; i < array_size(node->elements); i++) { | ||
134 | Node *expr = node->elements[i]; | ||
135 | analyzer_symbols(a, expr, scope); | ||
136 | } | ||
137 | } break; | ||
118 | // TODO: Struct field initializers need to be checked... when we | 138 | // TODO: Struct field initializers need to be checked... when we |
119 | // get to that. | 139 | // get to that. |
120 | // case NODE_VAL_FIELD: | 140 | // case NODE_VAL_FIELD: |
@@ -131,24 +151,36 @@ analyzer_symbols(Analyzer *a, Node *node, Scope *scope) { | |||
131 | case NODE_CASE: { | 151 | case NODE_CASE: { |
132 | analyzer_symbols(a, node->case_value, scope); | 152 | analyzer_symbols(a, node->case_value, scope); |
133 | if (node->case_expr) { | 153 | if (node->case_expr) { |
134 | Scope *next = scope_alloc(a, scope); | 154 | if (node->case_expr->kind != NODE_BLOCK) { |
135 | analyzer_symbols(a, node->case_expr, next); | 155 | scope = scope_alloc(a, scope); |
156 | } | ||
157 | analyzer_symbols(a, node->case_expr, scope); | ||
136 | } | 158 | } |
137 | } break; | 159 | } break; |
138 | case NODE_IF: { | 160 | case NODE_IF: { |
139 | analyzer_symbols(a, node->cond_if, scope); | 161 | analyzer_symbols(a, node->cond_if, scope); |
140 | if (node->cond_expr) { | 162 | if (node->cond_expr) { |
141 | Scope *next = scope_alloc(a, scope); | 163 | if (node->cond_expr->kind == NODE_BLOCK) { |
142 | analyzer_symbols(a, node->cond_expr, next); | 164 | analyzer_symbols(a, node->cond_expr, scope); |
165 | } else { | ||
166 | Scope *next = scope_alloc(a, scope); | ||
167 | analyzer_symbols(a, node->cond_expr, next); | ||
168 | } | ||
143 | } | 169 | } |
144 | if (node->cond_else) { | 170 | if (node->cond_else) { |
145 | Scope *next = scope_alloc(a, scope); | 171 | if (node->cond_else->kind == NODE_BLOCK) { |
146 | analyzer_symbols(a, node->cond_else, next); | 172 | analyzer_symbols(a, node->cond_else, scope); |
173 | } else { | ||
174 | Scope *next = scope_alloc(a, scope); | ||
175 | analyzer_symbols(a, node->cond_else, next); | ||
176 | } | ||
147 | } | 177 | } |
148 | } break; | 178 | } break; |
149 | case NODE_WHILE: { | 179 | case NODE_WHILE: { |
150 | analyzer_symbols(a, node->while_cond, scope); | 180 | analyzer_symbols(a, node->while_cond, scope); |
151 | scope = scope_alloc(a, scope); | 181 | if (node->while_expr->kind != NODE_BLOCK) { |
182 | scope = scope_alloc(a, scope); | ||
183 | } | ||
152 | analyzer_symbols(a, node->while_expr, scope); | 184 | analyzer_symbols(a, node->while_expr, scope); |
153 | } break; | 185 | } break; |
154 | case NODE_FUNCALL: { | 186 | case NODE_FUNCALL: { |
@@ -159,10 +191,9 @@ analyzer_symbols(Analyzer *a, Node *node, Scope *scope) { | |||
159 | " scope ", | 191 | " scope ", |
160 | a->file_name, node->line, node->col, symbol); | 192 | a->file_name, node->line, node->col, symbol); |
161 | } | 193 | } |
162 | Scope *next = scope_alloc(a, scope); | ||
163 | for (sz i = 0; i < array_size(node->elements); i++) { | 194 | for (sz i = 0; i < array_size(node->elements); i++) { |
164 | Node *expr = node->elements[i]; | 195 | Node *expr = node->elements[i]; |
165 | analyzer_symbols(a, expr, next); | 196 | analyzer_symbols(a, expr, scope); |
166 | } | 197 | } |
167 | } break; | 198 | } break; |
168 | case NODE_SYMBOL_IDX: | 199 | case NODE_SYMBOL_IDX: |
@@ -263,6 +294,9 @@ symbolic_analysis(Analyzer *a, Parser *parser) { | |||
263 | 294 | ||
264 | void | 295 | void |
265 | process_file(Str path) { | 296 | process_file(Str path) { |
297 | #if DEBUG == 1 | ||
298 | println("%s", path); | ||
299 | #endif | ||
266 | Arena lexer_arena = arena_create(LEXER_MEM, os_allocator); | 300 | Arena lexer_arena = arena_create(LEXER_MEM, os_allocator); |
267 | 301 | ||
268 | FileContents file = platform_read_file(path, &lexer_arena); | 302 | FileContents file = platform_read_file(path, &lexer_arena); |
@@ -329,25 +363,18 @@ process_file(Str path) { | |||
329 | }; | 363 | }; |
330 | symbolic_analysis(&analyzer, &parser); | 364 | symbolic_analysis(&analyzer, &parser); |
331 | 365 | ||
332 | // DEBUG: Printing all symbols on the HT. | 366 | // Printing symbol tables. |
333 | // StrSetIter iter = strset_iterator(root_scope.symbols, &lexer_arena); | 367 | for (sz i = 0; i < array_size(analyzer.scopes); i++) { |
334 | // StrSet *val = strset_next(&iter, &lexer_arena); | 368 | Arena scratch = lexer_arena; |
335 | // while (val) { | 369 | Scope *scope = analyzer.scopes[i]; |
336 | // println("SYMBOL: %s", val->key); | 370 | SymbolMapIter iter = symmap_iterator(scope->symbols, &scratch); |
337 | // val = strset_next(&iter, &lexer_arena); | 371 | SymbolMap *sym = symmap_next(&iter, &scratch); |
338 | // } | 372 | while (sym) { |
339 | 373 | println("%s: SCOPE: %d DEPTH: %d\t%s %s", path, scope->id, | |
340 | // StrIntMap *map = NULL; | 374 | scope->depth, sym_kind_str[sym->val.kind], sym->key); |
341 | // strintmap_insert(&map, cstr("test"), 1, &lexer_arena); | 375 | sym = symmap_next(&iter, &lexer_arena); |
342 | // strintmap_insert(&map, cstr("toast"), 9, &lexer_arena); | 376 | } |
343 | // strintmap_insert(&map, cstr("waaaa"), 420, &lexer_arena); | 377 | } |
344 | // strintmap_insert(&map, cstr("test"), 69, &lexer_arena); | ||
345 | // StrIntMapIter iter = strintmap_iterator(map, &lexer_arena); | ||
346 | // StrIntMap *val = strintmap_next(&iter, &lexer_arena); | ||
347 | // while (val) { | ||
348 | // println("KEY: %s value: %d", val->key, val->val); | ||
349 | // val = strintmap_next(&iter, &lexer_arena); | ||
350 | // } | ||
351 | 378 | ||
352 | // TODO: Type checking. | 379 | // TODO: Type checking. |
353 | 380 | ||