From: Brendan Hansen Date: Sun, 26 Jul 2020 19:40:51 +0000 (-0500) Subject: Added flags directive to enums X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=d1c2514965ad48ed34660d85e702b90c481e0ecd;p=onyx.git Added flags directive to enums --- diff --git a/docs/plan b/docs/plan index fd4b099f..aa24c32e 100644 --- a/docs/plan +++ b/docs/plan @@ -117,7 +117,7 @@ HOW: [X] Package system - [ ] Enum types + [X] Enum types [ ] Static pointers to sized data diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 1393417b..785cda91 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -135,6 +135,9 @@ typedef enum AstFlags { // Type flags Ast_Flag_Type_Is_Resolved = BH_BIT(8), + + // Enum flags + Ast_Flag_Enum_Is_Flags = BH_BIT(11), } AstFlags; typedef enum UnaryOp { diff --git a/onyx b/onyx index 5aad47df..90f0812b 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/ez.onyx b/progs/ez.onyx index 47784d99..0ab71b5f 100644 --- a/progs/ez.onyx +++ b/progs/ez.onyx @@ -17,19 +17,13 @@ asdf :: proc (pa: PrintableArray) { for i: 0, pa.len print(pa.data[i] as i32); } -SomeType :: enum (u16) { - Value1; - Value2; - Value3 = 80; - Value4; -} +SomeType :: enum #flags { Value1; Value2; Value3; Value4; } print_st :: proc (st: SomeType) { print(st as i32); } proc #export "main" { - st := SomeType.Value4; print_st(st); diff --git a/src/onyxparser.c b/src/onyxparser.c index 10b93d0e..9c54464d 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -1077,6 +1077,19 @@ static AstEnumType* parse_enum_declaration(OnyxParser* parser) { bh_arr_new(global_heap_allocator, enum_node->values, 4); + while (parser->curr->type == '#') { + if (parse_possible_directive(parser, "flags")) { + enum_node->flags |= Ast_Flag_Enum_Is_Flags; + } else { + OnyxToken* directive_token = expect_token(parser, '#'); + OnyxToken* symbol_token = expect_token(parser, Token_Type_Symbol); + + onyx_message_add(Msg_Type_Unknown_Directive, + directive_token->pos, + symbol_token->text, symbol_token->length); + } + } + AstType* backing = (AstType *) &basic_type_u32; if (parser->curr->type == '(') { consume_token(parser); @@ -1096,8 +1109,10 @@ static AstEnumType* parse_enum_declaration(OnyxParser* parser) { evalue->token = expect_token(parser, Token_Type_Symbol); evalue->type_node = (AstType *) enum_node; - if (parser->curr->type == '=') { + if (parser->curr->type == ':') { consume_token(parser); + expect_token(parser, ':'); + evalue->value = parse_numeric_literal(parser); } diff --git a/src/onyxsymres.c b/src/onyxsymres.c index c7a04c41..029a062b 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -421,7 +421,7 @@ static void symres_enum(AstEnumType* enum_node) { enum_node->backing_type = type_build_from_ast(semstate.allocator, enum_node->backing); enum_node->scope = scope_create(semstate.node_allocator, NULL); - u64 next_assign_value = 0; + u64 next_assign_value = (enum_node->flags & Ast_Flag_Enum_Is_Flags) ? 1 : 0; bh_arr_each(AstEnumValue *, value, enum_node->values) { symbol_introduce(enum_node->scope, (*value)->token, (AstNode *) *value); @@ -446,7 +446,11 @@ static void symres_enum(AstEnumType* enum_node) { (*value)->value = num; } - next_assign_value++; + if (enum_node->flags & Ast_Flag_Enum_Is_Flags) { + next_assign_value <<= 1; + } else { + next_assign_value++; + } } }