From f7b0ed78ef1f06bce45ab391be79ff9db8524e62 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 4 Oct 2022 19:17:04 -0500 Subject: [PATCH] added #auto typed local variables --- compiler/src/parser.c | 8 +++++++- compiler/src/wasm_emit.c | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/compiler/src/parser.c b/compiler/src/parser.c index 246c9582..8c878f61 100644 --- a/compiler/src/parser.c +++ b/compiler/src/parser.c @@ -1341,7 +1341,13 @@ static i32 parse_possible_symbol_declaration(OnyxParser* parser, AstNode** ret) AstType* type_node = NULL; if (parser->curr->type != '=') { - type_node = parse_type(parser); + if (parse_possible_directive(parser, "auto")) { + // Do nothing here. + // This allows for "x: #auto" to declare an x that will automatically be + // typed on the first assignment. + } else { + type_node = parse_type(parser); + } } AstLocal* local = make_local(parser->allocator, symbol, type_node); diff --git a/compiler/src/wasm_emit.c b/compiler/src/wasm_emit.c index 6a9b9d8c..6978c6dd 100644 --- a/compiler/src/wasm_emit.c +++ b/compiler/src/wasm_emit.c @@ -709,6 +709,17 @@ EMIT_FUNC(statement, AstNode* stmt) { } EMIT_FUNC(local_allocation, AstTyped* stmt) { + // + // If the statement does not have a type, it should not + // be emitted. The only case this should be used by is + // when a local is declared with "#auto" type and is + // never used, therefore never declaring its type. + if (stmt->type == NULL) { + assert(stmt->kind == Ast_Kind_Local); + onyx_report_warning(stmt->token->pos, "Unused local variable with unassigned type."); + return; + } + u64 local_idx = local_allocate(mod->local_alloc, stmt); bh_imap_put(&mod->local_map, (u64) stmt, local_idx); -- 2.25.1