From: Brendan Hansen Date: Wed, 7 Jul 2021 04:00:13 +0000 (-0500) Subject: refactored to remove AstAlias; have a better use for that name X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=05ce5c0412441cef6dbb1a8d2030a17da82691db;p=onyx.git refactored to remove AstAlias; have a better use for that name --- diff --git a/bin/onyx b/bin/onyx index b37e2794..668b9636 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/type_info/helper.onyx b/core/type_info/helper.onyx index faa0704b..7801d82d 100644 --- a/core/type_info/helper.onyx +++ b/core/type_info/helper.onyx @@ -113,3 +113,18 @@ write_type_name :: (writer: ^io.Writer, t: type_expr) { } } } + + +offset_of :: (T: type_expr, member: str) -> u32 { + info := get_type_info(T); + if info == null do return 0; + if info.kind != .Struct do return 0; + + struct_info := cast(^Type_Info_Struct) info; + for ^m: struct_info.members { + if m.name == member do return m.offset; + } + + // Should this return something else if the member was not found? + return 0; +} \ No newline at end of file diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 2dd0327c..cf57b922 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -71,7 +71,6 @@ NODE(MemRes) \ NODE(Include) \ NODE(UsePackage) \ - NODE(Alias) \ NODE(Global) \ NODE(Param) \ NODE(Function) \ @@ -106,7 +105,6 @@ typedef enum AstKind { Ast_Kind_Package, Ast_Kind_Load_File, Ast_Kind_Load_Path, - Ast_Kind_Alias, Ast_Kind_Memres, Ast_Kind_Binding, @@ -616,11 +614,16 @@ struct AstDirectiveSolidify { // Intruction Node struct AstReturn { AstNode_base; AstTyped* expr; }; struct AstJump { AstNode_base; JumpType jump; u32 count; }; + +typedef struct QualifiedUse { + OnyxToken* symbol_name; + OnyxToken* as_name; +} QualifiedUse; struct AstUse { AstNode_base; AstTyped* expr; - bh_arr(AstAlias *) only; + bh_arr(QualifiedUse) only; }; // Structure Nodes @@ -1065,6 +1068,9 @@ typedef struct Entity { Package *package; Scope *scope; + // TODO: This is incomplete. Add proper cycle detection and halting. + // struct Entity *waiting_on; + union { AstDirectiveError *error; AstInclude *include; diff --git a/modules/immediate_mode/immediate_renderer.onyx b/modules/immediate_mode/immediate_renderer.onyx index 1f750c50..98f751ae 100644 --- a/modules/immediate_mode/immediate_renderer.onyx +++ b/modules/immediate_mode/immediate_renderer.onyx @@ -94,17 +94,19 @@ Immediate_Renderer :: struct { gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer); + use package builtin.type_info { offset_of }; + // Position gl.enableVertexAttribArray(0); - gl.vertexAttribPointer(0, 2, gl.FLOAT, false, sizeof Immediate_Vertex, 0); + gl.vertexAttribPointer(0, 2, gl.FLOAT, false, sizeof Immediate_Vertex, offset_of(Immediate_Vertex, "position")); // Color gl.enableVertexAttribArray(1); - gl.vertexAttribPointer(1, 4, gl.FLOAT, false, sizeof Immediate_Vertex, 2 * sizeof f32); + gl.vertexAttribPointer(1, 4, gl.FLOAT, false, sizeof Immediate_Vertex, offset_of(Immediate_Vertex, "color")); // Texture gl.enableVertexAttribArray(2); - gl.vertexAttribPointer(2, 2, gl.FLOAT, false, sizeof Immediate_Vertex, 6 * sizeof f32); + gl.vertexAttribPointer(2, 2, gl.FLOAT, false, sizeof Immediate_Vertex, offset_of(Immediate_Vertex, "texture")); gl.bindBuffer(gl.ARRAY_BUFFER, -1); diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index 09168c19..94a60c45 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -7,7 +7,6 @@ static const char* ast_node_names[] = { "PACKAGE", "INCLUDE FILE", "INCLUDE FOLDER", - "ALIAS", "MEMORY RESERVATION", "BINDING", diff --git a/src/onyxclone.c b/src/onyxclone.c index 2d4145d4..bef0eec6 100644 --- a/src/onyxclone.c +++ b/src/onyxclone.c @@ -27,7 +27,6 @@ static inline i32 ast_kind_to_size(AstNode* node) { case Ast_Kind_Package: return sizeof(AstPackage); case Ast_Kind_Load_File: return sizeof(AstInclude); case Ast_Kind_Load_Path: return sizeof(AstInclude); - case Ast_Kind_Alias: return sizeof(AstAlias); case Ast_Kind_Memres: return sizeof(AstMemRes); case Ast_Kind_Binding: return sizeof(AstBinding); case Ast_Kind_Function: return sizeof(AstFunction); diff --git a/src/onyxparser.c b/src/onyxparser.c index 4dfee9d5..e42fc860 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -1215,15 +1215,15 @@ static AstNode* parse_use_stmt(OnyxParser* parser) { while (!consume_token_if_next(parser, '}')) { if (parser->hit_unexpected_token) return NULL; - AstAlias* alias = make_node(AstAlias, Ast_Kind_Alias); - alias->token = expect_token(parser, Token_Type_Symbol); + QualifiedUse qu; + qu.symbol_name = expect_token(parser, Token_Type_Symbol); if (consume_token_if_next(parser, Token_Type_Keyword_As)) - alias->alias = expect_token(parser, Token_Type_Symbol); + qu.as_name = expect_token(parser, Token_Type_Symbol); else - alias->alias = alias->token; + qu.as_name = qu.symbol_name; - bh_arr_push(use_node->only, alias); + bh_arr_push(use_node->only, qu); if (parser->curr->type != '}') expect_token(parser, ','); diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 410444d5..b5e7c2fb 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -9,15 +9,14 @@ static Scope* curr_scope = NULL; bh_arr(AstBlock *) block_stack = NULL; static b32 report_unresolved_symbols = 1; +// Everything related to waiting on is imcomplete at the moment. +static Entity* waiting_on = NULL; + #define SYMRES(kind, ...) do { \ SymresStatus ss = symres_ ## kind (__VA_ARGS__); \ if (ss > Symres_Errors_Start) return ss; \ } while (0) -#define SYMRES_IF_SYMBOL(node_ptr) do { \ - if ((*(node_ptr))->kind == Ast_Kind_Symbol) SYMRES(expression, node_ptr); \ - } while (0); - typedef enum SymresStatus { Symres_Success, Symres_Complete, @@ -598,20 +597,20 @@ static SymresStatus symres_use(AstUse* use) { scope_include(curr_scope, package->package->scope, pos); } else { - bh_arr_each(AstAlias *, alias, use->only) { - AstNode* thing = symbol_resolve(package->package->scope, (*alias)->token); + bh_arr_each(QualifiedUse, qu, use->only) { + AstNode* thing = symbol_resolve(package->package->scope, qu->symbol_name); if (thing == NULL) { // :SymresStall if (report_unresolved_symbols) { - onyx_report_error((*alias)->token->pos, + onyx_report_error(qu->symbol_name->pos, "The symbol '%b' was not found in this package.", - (*alias)->token->text, (*alias)->token->length); + qu->symbol_name->text, qu->symbol_name->length); return Symres_Error; } else { return Symres_Yield_Macro; } } - symbol_introduce(curr_scope, (*alias)->alias, thing); + symbol_introduce(curr_scope, qu->as_name, thing); } } @@ -637,16 +636,16 @@ static SymresStatus symres_use(AstUse* use) { scope_include(curr_scope, st->scope, use->token->pos); } else { - bh_arr_each(AstAlias *, alias, use->only) { - AstNode* thing = symbol_resolve(st->scope, (*alias)->token); + bh_arr_each(QualifiedUse, qu, use->only) { + AstNode* thing = symbol_resolve(st->scope, qu->symbol_name); if (thing == NULL) { - onyx_report_error((*alias)->token->pos, + onyx_report_error(qu->symbol_name->pos, "The symbol '%b' was not found in this scope.", - (*alias)->token->text, (*alias)->token->length); + qu->symbol_name->text, qu->symbol_name->length); return Symres_Error; } - symbol_introduce(curr_scope, (*alias)->alias, thing); + symbol_introduce(curr_scope, qu->as_name, thing); } }