diff options
author | metamuffin <metamuffin@disroot.org> | 2024-09-17 00:52:06 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-09-17 00:52:06 +0200 |
commit | 45aba31a75b4fe732567f0b091f919ad1a5e6c15 (patch) | |
tree | e20e5db6cf90744db8aa796ef910343a655c20f6 | |
parent | b55818a50f6e37d6b7348d426b63db892925ad93 (diff) | |
download | attocc-45aba31a75b4fe732567f0b091f919ad1a5e6c15.tar attocc-45aba31a75b4fe732567f0b091f919ad1a5e6c15.tar.bz2 attocc-45aba31a75b4fe732567f0b091f919ad1a5e6c15.tar.zst |
continue parsing
-rw-r--r-- | attocc.c | 85 |
1 files changed, 53 insertions, 32 deletions
@@ -183,12 +183,12 @@ enum seperator { }; enum token_kind { + TOK_END, TOK_IDENTIFIER, TOK_KEYWORD, TOK_CONSTANT, TOK_SEPERATOR, TOK_OPERATOR, - TOK_END, }; enum constant_kind { @@ -401,10 +401,12 @@ struct token *tokenize(char *source) { str = realloc_failsafe(str, str_len); str[str_len - 1] = c; } + // null termination + str_len++; + str = realloc_failsafe(str, str_len); + str[str_len - 1] = '\0'; struct token *new_token = token_push(&tokens, &num_tokens); - if (!new_token) - return NULL; new_token->kind = TOK_CONSTANT; new_token->data.constant_kind = CONST_STR; new_token->data.constant_str_value = str; @@ -427,8 +429,6 @@ struct token *tokenize(char *source) { return fprintf(stderr, "expected '\n"), NULL; struct token *new_token = token_push(&tokens, &num_tokens); - if (!new_token) - return NULL; new_token->kind = TOK_CONSTANT; new_token->data.constant_kind = CONST_CHAR; new_token->data.constant_char_value = chr; @@ -447,8 +447,6 @@ struct token *tokenize(char *source) { ident_str[ident_len] = '\0'; struct token *new_token = token_push(&tokens, &num_tokens); - if (!new_token) - return NULL; new_token->kind = TOK_IDENTIFIER; new_token->data.identifier = ident_str; @@ -460,8 +458,6 @@ struct token *tokenize(char *source) { } struct token *new_token = token_push(&tokens, &num_tokens); - if (!new_token) - return NULL; new_token->kind = TOK_END; return tokens; @@ -493,10 +489,7 @@ void debug_tokens(struct token *tokens) { for (int i = 0; tokens[i].kind != TOK_END; i++) { switch (tokens[i].kind) { case TOK_IDENTIFIER: - if (tokens[i].data.identifier < 100) - printf("TOK_IDENTIFIER:%p, ", tokens[i].data.identifier); - else - printf("TOK_IDENTIFIER:%s, ", tokens[i].data.identifier); + printf("TOK_IDENTIFIER:%s, ", tokens[i].data.identifier); break; case TOK_KEYWORD: printf("TOK_KEYWORD:%s, ", KEYWORD_NAMES[tokens[i].data.keyword]); @@ -543,11 +536,11 @@ struct node { NO_FUNCTION, } kind; union node_data { - struct { + struct node_data_binop { struct node *lhs; struct node *rhs; } binop; - struct { + struct node_data_enum { char *name; char is_decl; unsigned long num_members; @@ -585,28 +578,38 @@ char parse_enum_decl(struct token *tokens, struct node **node) { ident = tokens[p].data.identifier; p++; } - printf("3\n"); + printf("3 %i %i\n", tokens[p].kind, tokens[p].data.seperator); char is_decl = 0; + struct enum_member *members; + unsigned long num_members; + if (tokens[p].kind == TOK_SEPERATOR && - tokens[p].data.seperator != SEP_LCURLY) { + tokens[p].data.seperator == SEP_LCURLY) { p++; is_decl = 1; printf("4\n"); - struct enum_member *members; - unsigned long num_members; int value = 0; - if (tokens[p].kind != TOK_IDENTIFIER) - return 0; + while (tokens[p].kind != TOK_SEPERATOR && + tokens[p].data.seperator != SEP_RCURLY) { + if (tokens[p].kind == TOK_END) + return 1; + + if (tokens[p].kind != TOK_IDENTIFIER) + return 1; + } + + return 0; char *m_ident = tokens[p].data.identifier; p++; if (tokens[p].kind == TOK_OPERATOR && tokens[p].data.operator== OP_ASSIGN) { p++; } - if (tokens[p].kind != TOK_SEPERATOR || tokens[p].data.operator== SEP_COMMA) + if (tokens[p].kind != TOK_SEPERATOR || + tokens[p].data.seperator == SEP_COMMA) return 1; num_members++; @@ -614,6 +617,12 @@ char parse_enum_decl(struct token *tokens, struct node **node) { struct enum_member *new_member = &members[num_members - 1]; new_member->value = value++; new_member->name = strdup(m_ident); + } else if (tokens[p].kind == TOK_SEPERATOR && + tokens[p].data.seperator == SEP_SEMICOLON) { + + } else { + printf("expected members or semicolon\n"); + return 1; } *node = malloc(sizeof(struct node)); @@ -621,8 +630,11 @@ char parse_enum_decl(struct token *tokens, struct node **node) { fprintf(stderr, "malloc failed\n"); return 1; } + (**node).kind = NO_ENUM; (**node).data.enum_.is_decl = is_decl; (**node).data.enum_.name = strdup(ident); + (**node).data.enum_.num_members = num_members; + (**node).data.enum_.members = members; return 0; } @@ -636,28 +648,34 @@ void debug_node(struct node *node) { switch (node->kind) { case NO_ENUM: printf("NO_ENUM\n"); - // printf(" name=%s\n", node->data.enum_.name); - // printf(" is_decl=%i\n", node->data.enum_.is_decl); - // printf(" num_members=%li\n", node->data.enum_.num_members); + printf(" name=%s\n", node->data.enum_.name); + printf(" is_decl=%i\n", node->data.enum_.is_decl); + printf(" num_members=%li\n", node->data.enum_.num_members); break; case NO_BINOP: printf("NO_BINOP\n"); - // printf(" lhs: "); - // debug_node(node->data.binop.lhs); - // printf("\n"); - // printf(" rhs: "); - // debug_node(node->data.binop.rhs); - // printf("\n"); + printf(" lhs: "); + debug_node(node->data.binop.lhs); + printf("\n"); + printf(" rhs: "); + debug_node(node->data.binop.rhs); + printf("\n"); + break; case NO_MEMBER_ACCESS: printf("NO_MEMBER_ACCESS\n"); + break; case NO_FUNCTION: printf("NO_FUNCTION\n"); + break; case NO_STRUCT: printf("NO_STRUCT\n"); + break; case NO_UNION: printf("NO_UNION\n"); + break; case NO_UNOP: printf("NO_UNOP\n"); + break; default: break; } @@ -720,7 +738,10 @@ int main(int argc, char **argv) { printf("parse failed\n"); return 1; } - printf("Hello %p\n", nodes); + if (!nodes) { + printf("parse failed"); + return 1; + } #ifdef DEBUG debug_node(nodes); |