From db8c46d6c5d5ef0636e137e494e46ffb7e30dc2c Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 4 May 2023 08:48:17 +0200 Subject: [PATCH] Addition of "distinct" and "inline" as keywords. Removal of "alias" keyword. --- src/compiler/compiler_internal.h | 2 -- src/compiler/enums.h | 4 +++- src/compiler/parse_global.c | 36 +++++++++++++------------------- src/compiler/parse_stmt.c | 3 ++- src/compiler/parser_internal.h | 9 +++++--- src/compiler/symtab.c | 4 ---- src/compiler/tokens.c | 6 ++++-- src/version.h | 2 +- 8 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 2900fcd08..f435bfadc 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -1872,12 +1872,10 @@ extern const char *kw_at_require; extern const char *kw_at_return; extern const char *kw_check_assign; extern const char *kw_deprecated; -extern const char *kw_distinct; extern const char *kw_finalize; extern const char *kw_in; extern const char *kw_incr; extern const char *kw_initialize; -extern const char *kw_inline; extern const char *kw_inout; extern const char *kw_kind; extern const char *kw_len; diff --git a/src/compiler/enums.h b/src/compiler/enums.h index 2e3d2e61c..57218f24a 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -508,7 +508,6 @@ typedef enum TOKEN_TYPEID, // Keywords - TOKEN_ALIAS, // Reserved TOKEN_ASSERT, TOKEN_ASM, TOKEN_BITSTRUCT, @@ -521,6 +520,7 @@ typedef enum TOKEN_DEFINE, TOKEN_DEFAULT, TOKEN_DEFER, + TOKEN_DISTINCT, TOKEN_DO, TOKEN_ELSE, TOKEN_ENUM, @@ -533,6 +533,7 @@ typedef enum TOKEN_FN, TOKEN_TLOCAL, TOKEN_IF, + TOKEN_INLINE, TOKEN_IMPORT, TOKEN_MACRO, TOKEN_MODULE, @@ -548,6 +549,7 @@ typedef enum TOKEN_UNION, TOKEN_VAR, TOKEN_WHILE, + TOKEN_LAST_NON_CT_KEYWORD = TOKEN_WHILE, TOKEN_CT_ALIGNOF, // $alignof TOKEN_CT_ASSERT, // $assert diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 8d670d144..5ebb89efe 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -203,7 +203,7 @@ static inline Path *parse_module_path(ParseContext *c) const char *string = symstr(c); if (!try_consume(c, TOKEN_IDENT)) { - if (token_is_keyword(c->tok)) + if (token_is_keyword_ident(c->tok)) { SEMA_ERROR_HERE("The module path cannot contain a reserved keyword, try another name."); return NULL; @@ -290,7 +290,7 @@ bool parse_module(ParseContext *c, AstId contracts) if (!tok_is(c, TOKEN_IDENT)) { - if (token_is_keyword(c->tok)) + if (token_is_keyword_ident(c->tok)) { RETURN_SEMA_ERROR_HERE("The module name cannot contain a reserved keyword, try another name."); } @@ -400,7 +400,7 @@ bool parse_module(ParseContext *c, AstId contracts) static bool consume_type_name(ParseContext *c, const char* type) { - if (tok_is(c, TOKEN_IDENT) || token_is_keyword(c->tok)) + if (tok_is(c, TOKEN_IDENT) || token_is_keyword_ident(c->tok)) { RETURN_SEMA_ERROR_HERE("Names of %ss must start with an uppercase letter.", type); } @@ -413,7 +413,7 @@ static bool consume_type_name(ParseContext *c, const char* type) bool consume_const_name(ParseContext *c, const char* type) { - if (tok_is(c, TOKEN_IDENT) || tok_is(c, TOKEN_TYPE_IDENT) || token_is_keyword(c->tok)) + if (tok_is(c, TOKEN_IDENT) || tok_is(c, TOKEN_TYPE_IDENT) || token_is_keyword_ident(c->tok)) { RETURN_SEMA_ERROR_HERE("Names of %ss must be all uppercase.", type); } @@ -1164,7 +1164,7 @@ static inline bool parse_enum_param_decl(ParseContext *c, Decl*** parameters) Decl *param = decl_new_var_current(c, type, VARDECL_PARAM); if (!try_consume(c, TOKEN_IDENT)) { - if (token_is_keyword(c->tok)) RETURN_SEMA_ERROR_HERE("Keywords cannot be used as member names."); + if (token_is_keyword_ident(c->tok)) RETURN_SEMA_ERROR_HERE("Keywords cannot be used as member names."); if (token_is_some_ident(c->tok)) RETURN_SEMA_ERROR_HERE("Expected a name starting with a lower-case letter."); RETURN_SEMA_ERROR_HERE("Expected a member name here."); } @@ -1501,7 +1501,7 @@ bool parse_struct_body(ParseContext *c, Decl *parent) continue; } bool was_inline = false; - if (token_type == TOKEN_IDENT && symstr(c) == kw_inline) + if (tok_is(c, TOKEN_INLINE)) { if (parent->decl_kind != DECL_STRUCT) { @@ -1761,23 +1761,15 @@ static inline Decl *parse_typedef_declaration(ParseContext *c) CONSUME_OR_RET(TOKEN_EQ, poisoned_decl); bool distinct = false; bool is_inline = false; - if (tok_is(c, TOKEN_IDENT)) + if (tok_is(c, TOKEN_INLINE)) { - if (symstr(c) == kw_inline) - { - SEMA_ERROR_HERE("'inline' must always follow 'distinct'."); - return poisoned_decl; - } - if (symstr(c) == kw_distinct) - { - distinct = true; - advance(c); - if (tok_is(c, TOKEN_IDENT) && symstr(c) == kw_inline) - { - is_inline = true; - advance(c); - } - } + SEMA_ERROR_HERE("'inline' must always follow 'distinct'."); + return poisoned_decl; + } + if (try_consume(c, TOKEN_DISTINCT)) + { + distinct = true; + is_inline = try_consume(c, TOKEN_INLINE); } // 1. Did we have `fn`? In that case it's a function pointer. diff --git a/src/compiler/parse_stmt.c b/src/compiler/parse_stmt.c index 4c136d1d1..8c25ebfc5 100644 --- a/src/compiler/parse_stmt.c +++ b/src/compiler/parse_stmt.c @@ -1290,7 +1290,6 @@ Ast *parse_stmt(ParseContext *c) case TOKEN_SHL: case TOKEN_SHR_ASSIGN: case TOKEN_SHL_ASSIGN: - case TOKEN_ALIAS: case TOKEN_ELSE: case TOKEN_QUESTQUEST: case TOKEN_ENUM: @@ -1323,6 +1322,8 @@ Ast *parse_stmt(ParseContext *c) case TOKEN_CT_ENDFOREACH: case TOKEN_CT_VASPLAT: case TOKEN_IMPLIES: + case TOKEN_INLINE: + case TOKEN_DISTINCT: case TOKEN_CT_INCLUDE: SEMA_ERROR_HERE("Unexpected '%s' found when expecting a statement.", token_type_to_string(c->tok)); diff --git a/src/compiler/parser_internal.h b/src/compiler/parser_internal.h index 677107e02..6ee33d4ac 100644 --- a/src/compiler/parser_internal.h +++ b/src/compiler/parser_internal.h @@ -130,9 +130,12 @@ INLINE bool token_is_some_ident(TokenType token_type) INLINE bool token_is_keyword(TokenType token_type) { - if (token_type >= TOKEN_VOID && token_type <= TOKEN_TYPEID) return true; - if (token_type >= TOKEN_ALIAS && token_type <= TOKEN_WHILE) return true; - return false; + return token_type >= TOKEN_FIRST_KEYWORD && token_type <= TOKEN_LAST_KEYWORD; +} + +INLINE bool token_is_keyword_ident(TokenType token_type) +{ + return token_type >= TOKEN_FIRST_KEYWORD && token_type <= TOKEN_LAST_NON_CT_KEYWORD; } static inline bool expect_ident(ParseContext *c, const char* name) diff --git a/src/compiler/symtab.c b/src/compiler/symtab.c index e1871b9d1..fc687ee11 100644 --- a/src/compiler/symtab.c +++ b/src/compiler/symtab.c @@ -51,12 +51,10 @@ const char *kw_at_require; const char *kw_at_return; const char *kw_check_assign; const char *kw_deprecated; -const char *kw_distinct; const char *kw_finalize; const char *kw_in; const char *kw_incr; const char *kw_initialize; -const char *kw_inline; const char *kw_inout; const char *kw_kind; const char *kw_len; @@ -141,12 +139,10 @@ void symtab_init(uint32_t capacity) kw_argv = KW_DEF("_$argv"); kw_check_assign = KW_DEF("check_assign"); kw_deprecated = KW_DEF("deprecated"); - kw_distinct = KW_DEF("distinct"); kw_finalize = KW_DEF("finalize"); kw_in = KW_DEF("in"); kw_initialize = KW_DEF("initialize"); kw_incr = KW_DEF("incr"); - kw_inline = KW_DEF("inline"); kw_inout = KW_DEF("inout"); kw_libc = KW_DEF("libc"); kw_mainstub = KW_DEF("_$start"); diff --git a/src/compiler/tokens.c b/src/compiler/tokens.c index 89521f6b1..f09d3417e 100644 --- a/src/compiler/tokens.c +++ b/src/compiler/tokens.c @@ -183,8 +183,6 @@ const char *token_type_to_string(TokenType type) return "DOC_COMMENT"; // Keywords - case TOKEN_ALIAS: - return "alias"; case TOKEN_ANYFAULT: return "anyfault"; case TOKEN_ASM: @@ -211,6 +209,8 @@ const char *token_type_to_string(TokenType type) return "defer"; case TOKEN_DEFINE: return "define"; + case TOKEN_DISTINCT: + return "distinct"; case TOKEN_DO: return "do"; case TOKEN_ELSE: @@ -233,6 +233,8 @@ const char *token_type_to_string(TokenType type) return "fn"; case TOKEN_IF: return "if"; + case TOKEN_INLINE: + return "inline"; case TOKEN_IMPORT: return "import"; case TOKEN_MACRO: diff --git a/src/version.h b/src/version.h index c0a043bac..3689aa497 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.4.502" \ No newline at end of file +#define COMPILER_VERSION "0.4.503" \ No newline at end of file