From: Brendan Hansen Date: Tue, 28 Jul 2020 16:38:07 +0000 (-0500) Subject: Added basic type aliasing X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=7c1a39c2cce5b35050b9c13f8501a5f569db6809;p=onyx.git Added basic type aliasing --- diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 72fb15eb..bde66fe2 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -41,6 +41,7 @@ typedef struct AstStructType AstStructType; typedef struct AstStructMember AstStructMember; typedef struct AstEnumType AstEnumType; typedef struct AstEnumValue AstEnumValue; +typedef struct AstTypeAlias AstTypeAlias; typedef struct AstBinding AstBinding; typedef struct AstMemRes AstMemRes; @@ -91,6 +92,7 @@ typedef enum AstKind { Ast_Kind_Array_Type, Ast_Kind_Struct_Type, Ast_Kind_Enum_Type, + Ast_Kind_Type_Alias, Ast_Kind_Type_End, Ast_Kind_Struct_Member, @@ -331,6 +333,7 @@ struct AstEnumType { Type *etcache; }; struct AstEnumValue { AstTyped_base; AstNumLit* value; }; +struct AstTypeAlias { AstType_base; AstType* to; }; // Top level nodes struct AstBinding { AstTyped_base; AstNode* node; }; @@ -399,7 +402,7 @@ typedef enum EntityType { Entity_Type_Use_Package, Entity_Type_String_Literal, Entity_Type_Enum, - Entity_Type_Struct, + Entity_Type_Type_Alias, Entity_Type_Memory_Reservation, Entity_Type_Function_Header, Entity_Type_Global_Header, @@ -420,7 +423,7 @@ typedef struct Entity { AstGlobal *global; AstTyped *expr; AstStrLit *strlit; - AstStructType *struct_type; + AstType *type_alias; AstEnumType *enum_type; AstMemRes *mem_res; }; diff --git a/onyx b/onyx index 48659236..1174de4d 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/fcf.onyx b/progs/fcf.onyx index 92585188..3fceb30e 100644 --- a/progs/fcf.onyx +++ b/progs/fcf.onyx @@ -4,7 +4,7 @@ use "progs/intrinsics" use package printing call_me :: proc (f: proc (i32) -> i32, val: i32) { - f(val); + f(val); } funcs : [5] proc (i32, i32) -> i32 @@ -15,8 +15,11 @@ mul :: proc (a: i32, b: i32) -> i32 { return a * b; } div :: proc (a: i32, b: i32) -> i32 { return a / b; } mod :: proc (a: i32, b: i32) -> i32 { return a % b; } +deferred_proc :: #type proc (i32, i32) -> i32 +my_int :: #type i32; + DeferredCall :: struct { - func : proc (i32, i32) -> i32; + func : deferred_proc; left : i32; right : i32; } @@ -43,9 +46,9 @@ minus_one :: proc (n: i32) -> i32 { return n - 1; } double :: proc (n: i32) -> i32 { return n << 1; } proc #export "main" { - call_me(echo, 10); + call_me(echo, 10); - print(add as i32); + print(add as my_int); funcs[0] = add; funcs[1] = sub; @@ -56,7 +59,7 @@ proc #export "main" { for i: 0, 5 print(funcs[i](10, 3)); dc := __heap_start as ^DeferredCall; - dc.func = mod; + dc.func = add; dc.left = 40; dc.right = 19; @@ -71,4 +74,35 @@ proc #export "main" { array_map(len, data, add_one); print(data as [] i32, len); + + cheese := Cheeses.Cheddar; + + // Closest thing to a switch statement at the moment + { + if cheese == Cheeses.Cheddar { + print(1); + break; + } + + print(2); + + if cheese == Cheeses.Muenster { + print(10); + break; + } + + print(2); + + if cheese == Cheeses.Mozerella { + print(100); + break; + } + } + } + +Cheeses :: enum { + Cheddar; + Muenster; + Mozerella; +} \ No newline at end of file diff --git a/src/onyx.c b/src/onyx.c index 4da00e57..6c12f62b 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -223,9 +223,10 @@ static void merge_parse_results(CompilerState* compiler_state, ParseResults* res break; } + case Ast_Kind_Type_Alias: case Ast_Kind_Struct_Type: { - ent.type = Entity_Type_Struct; - ent.struct_type = (AstStructType *) node; + ent.type = Entity_Type_Type_Alias; + ent.type_alias = (AstType *) node; bh_arr_push(compiler_state->prog_info.entities, ent); break; } diff --git a/src/onyxchecker.c b/src/onyxchecker.c index d73db301..4f228123 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -952,8 +952,9 @@ void onyx_type_check() { if (check_expression(&entity->expr)) return; break; - case Entity_Type_Struct: - if (check_struct(entity->struct_type)) return; + case Entity_Type_Type_Alias: + if (entity->type_alias->kind == Ast_Kind_Struct_Type) + if (check_struct((AstStructType *) entity->type_alias)) return; break; case Entity_Type_Enum: break; diff --git a/src/onyxparser.c b/src/onyxparser.c index 6ea6d391..ea134310 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -1201,6 +1201,11 @@ static AstTyped* parse_top_level_expression(OnyxParser* parser) { else if (parser->curr->type == Token_Type_Keyword_Struct) { return (AstTyped *) parse_struct(parser); } + else if (parse_possible_directive(parser, "type")) { + AstTypeAlias* alias = make_node(AstTypeAlias, Ast_Kind_Type_Alias); + alias->to = parse_type(parser); + return (AstTyped *) alias; + } else if (parser->curr->type == Token_Type_Keyword_Enum) { return (AstTyped *) parse_enum_declaration(parser); } diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 1d286f98..a2bb3097 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -78,6 +78,11 @@ static void scope_leave() { static AstType* symres_type(AstType* type) { if (type == NULL) return NULL; + if (type->kind == Ast_Kind_Type_Alias) { + ((AstTypeAlias *) type)->to = symres_type(((AstTypeAlias *) type)->to); + return type; + } + if (type->kind == Ast_Kind_Symbol) { return (AstType *) symbol_resolve(semstate.curr_scope, ((AstNode *) type)->token); } @@ -524,7 +529,7 @@ void onyx_resolve_symbols() { case Entity_Type_Overloaded_Function: symres_overloaded_function(entity->overloaded_function); break; case Entity_Type_Global: symres_global(entity->global); break; case Entity_Type_Expression: symres_expression(&entity->expr); break; - case Entity_Type_Struct: symres_type((AstType *) entity->struct_type); break; + case Entity_Type_Type_Alias: entity->type_alias = symres_type(entity->type_alias); break; case Entity_Type_Enum: symres_enum(entity->enum_type); break; case Entity_Type_Memory_Reservation: symres_memres(&entity->mem_res); break; diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 45b18079..1c94e1be 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -321,6 +321,9 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) { case Ast_Kind_Basic_Type: return ((AstBasicType *) type_node)->type; + case Ast_Kind_Type_Alias: + return type_build_from_ast(alloc, ((AstTypeAlias *) type_node)->to); + case Ast_Kind_Symbol: assert(("symbol node in type expression", 0)); return NULL; diff --git a/src/onyxutils.c b/src/onyxutils.c index ed3bd9da..a8da138b 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -34,7 +34,8 @@ static const char* ast_node_names[] = { "FUNCTION_TYPE", "ARRAY TYPE", "STRUCT TYPE", - "ENUM TYPE" + "ENUM TYPE", + "TYPE_ALIAS", "TYPE_END (BAD)", "STRUCT MEMBER",