From cd29798c1de6f37a205d1f4cf4e74f19ebca6645 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 12 Jun 2022 13:09:37 +0200 Subject: [PATCH] AST_STRICT for type aliases --- ast.cpp | 2 +- compiler.h | 6 +++--- parsing.cpp | 16 +++++++++++----- typechecking.cpp | 4 +++- typechecking.h | 1 + 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/ast.cpp b/ast.cpp index 004b391..83863f3 100644 --- a/ast.cpp +++ b/ast.cpp @@ -41,7 +41,7 @@ typedef U32 Ast_Flag; enum{ AST_EXPR = bit_flag(1), AST_STMT = bit_flag(2), - AST_BINDING = bit_flag(3), + AST_STRICT = bit_flag(3), AST_AGGREGATE = bit_flag(4), AST_AGGREGATE_CHILD = bit_flag(5), AST_ITEM_INCLUDED = bit_flag(6), diff --git a/compiler.h b/compiler.h index a5e4b96..ad8bd2e 100644 --- a/compiler.h +++ b/compiler.h @@ -161,7 +161,7 @@ Intern_String keyword_enum; Intern_String intern_void; Intern_String intern_foreign; -Intern_String intern_printf; +Intern_String intern_strict; struct Ast_Scope; @@ -209,9 +209,9 @@ lex_init(Allocator *token_string_arena, Allocator *map_allocator, Lexer *l){ l->interns.first_keyword = keyword_struct.str; l->interns.last_keyword = keyword_enum.str; - intern_foreign = intern_string(&l->interns, "#foreign"_s); + intern_foreign = intern_string(&l->interns, "foreign"_s); + intern_strict = intern_string(&l->interns, "strict"_s); intern_void = intern_string(&l->interns, "void"_s); - intern_printf = intern_string(&l->interns, "printf"_s); } function void diff --git a/parsing.cpp b/parsing.cpp index a06e847..ce34c17 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -80,10 +80,10 @@ token_is_keyword(Intern_String keyword, S64 lookahead = 0){ } function Token * -token_match_pound(String string){ +token_match_pound(Intern_String string){ Token *token = token_get(); if(token->kind == TK_Pound){ - if(string_compare(token->intern_val.s, string)){ + if(token->intern_val == string){ return token_next(); } } @@ -340,11 +340,8 @@ parse_lambda(Token *token){ } token_expect(TK_CloseParen); Ast_Expr *ret = parse_optional_type(); - - Token *foreign = token_match_pound("foreign"_s); Ast_Scope *scope = token_is(OPEN_SCOPE) ? parse_stmt_scope() : 0; Ast_Lambda *result = ast_lambda(token, params, ret, scope); - if(foreign) set_flag(result->flags, AST_FOREIGN); return result; } @@ -598,8 +595,16 @@ parse_decl(B32 is_global){ } } + Ast_Flag flags = 0; Token *tname = token_get(); if(token_match(TK_Identifier, TK_DoubleColon)){ + + if(token_match_pound(intern_strict)){ + set_flag(flags, AST_STRICT); + } else if(token_match_pound(intern_foreign)){ + set_flag(flags, AST_FOREIGN); + } + // @note parse struct binding if(token_match_keyword(keyword_struct)){ result = parse_struct(tname); @@ -638,6 +643,7 @@ parse_decl(B32 is_global){ if(result){ result->name = tname->intern_val; + set_flag(result->flags, flags); } return result; diff --git a/typechecking.cpp b/typechecking.cpp index 53d481e..be122ec 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -965,7 +965,9 @@ resolve_decl(Ast_Decl *ast){ node->value = op.value; if(op.value.type == type_type){ node->kind = AST_TYPE; - node->type_val = type_copy(pctx->perm, node->type_val); + if(is_flag_set(node->flags, AST_STRICT)){ + node->type_val = type_copy(pctx->perm, node->type_val); + } } else if(is_lambda(op.value.type)){ node->kind = AST_LAMBDA; } diff --git a/typechecking.h b/typechecking.h index c5d65d2..98d21bf 100644 --- a/typechecking.h +++ b/typechecking.h @@ -3,6 +3,7 @@ struct Operand{ INLINE_VALUE_FIELDS; U8 is_const : 1; U8 is_lvalue: 1; + U8 pound_strict: 1; }; FLAG32(Resolve_Flag){