From d61782a1d33e6aca31802ceda918de0864d4c979 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 16 Jun 2024 11:31:58 +0200 Subject: fiix a few lexing crashes --- attocc.c | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) (limited to 'attocc.c') diff --git a/attocc.c b/attocc.c index b9a1ff9..5c92ac7 100644 --- a/attocc.c +++ b/attocc.c @@ -335,8 +335,8 @@ struct token *tokenize(char *source) { match = 0; for (int i = 0; i < NUM_KEYWORDS; i++) { char *kw = KEYWORDS[i]; - if (remaining >= strlen(kw)) { - if (strncmp(kw, p, strlen(kw)) == 0) { + if (remaining >= strlen(kw) + 1) { + if (strncmp(kw, p, strlen(kw)) == 0 && !is_ident(p[strlen(kw)])) { struct token *new_token = token_push(&tokens, &num_tokens); if (!new_token) return NULL; @@ -403,7 +403,7 @@ struct token *tokenize(char *source) { struct token *new_token = token_push(&tokens, &num_tokens); if (!new_token) return NULL; - new_token->kind = TOK_IDENTIFIER; + new_token->kind = TOK_CONSTANT; new_token->data.constant_kind = CONST_STR; new_token->data.constant_str_value = str; continue; @@ -428,7 +428,7 @@ struct token *tokenize(char *source) { if (!new_token) return NULL; new_token->kind = TOK_CONSTANT; - new_token->data.constant_kind = CONST_STR; + new_token->data.constant_kind = CONST_CHAR; new_token->data.constant_char_value = chr; continue; } @@ -534,29 +534,53 @@ void debug_tokens(struct token *tokens) { } #endif -enum expr_kind { +enum node_kind { NO_BINOP, NO_UNOP, NO_MEMBER_ACCESS, + NO_STRUCT, + NO_UNION, + NO_ENUM, + NO_FUNCTION }; -union expr_data { +union node_data { struct { struct expr *lhs; struct expr *rhs; } binop; }; -struct expr { - enum expr_kind kind; - union expr_data data; +struct node { + enum node_kind kind; + union node_data data; }; -enum decl_kind { DECL_STRUCT, DECL_UNION, DECL_ENUM, DECL_FUNCTION }; +char parse_enum_decl(struct token *tokens, struct node **node) { + int p = 0; + if (tokens[p].kind != TOK_KEYWORD || tokens[p].data.keyword != KW_ENUM) + return 1; + p++; -struct decl { - enum decl_kind kind; -}; + char *ident = NULL; + if (tokens[p].kind == TOK_IDENTIFIER) { + ident = tokens[p].data.identifier; + p++; + } + + if (tokens[p].kind != TOK_SEPERATOR || tokens[p].data.seperator != SEP_LCURLY) + return 1; + p++; + + *node = malloc(sizeof(struct node)); + if (!*node) { + fprintf(stderr, "malloc failed\n"); + return 1; + } + // (**node).data.; + + return 0; +} struct decl *parse(struct token *tokens) { return NULL; } -- cgit v1.2.3-70-g09d2