From: Brendan Hansen Date: Wed, 22 Mar 2023 14:18:34 +0000 (-0500) Subject: added: enum documentation X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=e4b405473a1a536ffe707ca02ecf5c246805200b;p=onyx.git added: enum documentation --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index 0a637908..4ce17e98 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1611,6 +1611,7 @@ enum Runtime { typedef struct OnyxDocInfo { bh_arr(AstBinding *) procedures; bh_arr(AstBinding *) structures; + bh_arr(AstBinding *) enumerations; Table(u32) file_ids; u32 next_file_id; diff --git a/compiler/src/doc.c b/compiler/src/doc.c index c96ba761..71b467c7 100644 --- a/compiler/src/doc.c +++ b/compiler/src/doc.c @@ -173,6 +173,10 @@ void onyx_docs_submit(OnyxDocInfo *docs, AstBinding *binding) { if (node->kind == Ast_Kind_Poly_Struct_Type) { bh_arr_push(docs->structures, binding); } + + if (node->kind == Ast_Kind_Enum_Type) { + bh_arr_push(docs->enumerations, binding); + } } #define Doc_Magic_Bytes "ODOC" @@ -554,6 +558,45 @@ static b32 write_doc_structure(bh_buffer *buffer, AstBinding *binding, AstNode * return 1; } +static b32 write_doc_enum(bh_buffer *buffer, AstBinding *binding, AstNode *node) { + AstEnumType *enum_node = (void *) node; + + write_entity_header(buffer, binding, node->token->pos); + + bh_buffer_write_u32(buffer, bh_arr_length(enum_node->values)); + bh_arr_each(AstEnumValue *, pvalue, enum_node->values) { + AstEnumValue * value = *pvalue; + + write_string(buffer, value->token->length, value->token->text); + + assert(value->value->kind == Ast_Kind_NumLit); + AstNumLit *num = (AstNumLit *) value->value; + bh_buffer_write_u64(buffer, num->value.l); + } + + bh_buffer_write_u32(buffer, enum_node->is_flags ? 1 : 0); + + return 1; +} + +static void write_doc_entity_array(bh_buffer *buffer, bh_arr(AstBinding *) arr, + b32 (*write_doc)(bh_buffer *buffer, AstBinding *, AstNode*), + u32 offset_write_location) { + *((u32 *) bh_pointer_add(buffer->data, offset_write_location)) = buffer->length; + + u32 count_patch = buffer->length; + bh_buffer_write_u32(buffer, 0); + + u32 count = 0; + bh_arr_each(AstBinding *, pbind, arr) { + if (write_doc(buffer, *pbind, (*pbind)->node)) { + count++; + } + } + + *((u32 *) bh_pointer_add(buffer->data, count_patch)) = count; +} + void onyx_docs_emit_odoc(const char *dest) { bh_file doc_file; if (bh_file_create(&doc_file, dest) != BH_FILE_ERROR_NONE) { @@ -578,6 +621,7 @@ void onyx_docs_emit_odoc(const char *dest) { bh_buffer_write_u32(&doc_buffer, 0); bh_buffer_write_u32(&doc_buffer, 0); bh_buffer_write_u32(&doc_buffer, 0); + bh_buffer_write_u32(&doc_buffer, 0); // // Package Info @@ -608,45 +652,25 @@ void onyx_docs_emit_odoc(const char *dest) { // // Procedure Info // - *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 4)) = doc_buffer.length; - - u32 proc_count_patch = doc_buffer.length; - bh_buffer_write_u32(&doc_buffer, 0); - - u32 proc_count = 0; - bh_arr(AstBinding *) procs = context.doc_info->procedures; - bh_arr_each(AstBinding *, pbind, procs) { - if (write_doc_procedure(&doc_buffer, *pbind, (*pbind)->node)) { - proc_count++; - } - } - - *((u32 *) bh_pointer_add(doc_buffer.data, proc_count_patch)) = proc_count; + write_doc_entity_array(&doc_buffer, context.doc_info->procedures, write_doc_procedure, offset_table_index + 4); // // Structure Info // - *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 8)) = doc_buffer.length; - - u32 struct_count_patch = doc_buffer.length; - bh_buffer_write_u32(&doc_buffer, 0); + write_doc_entity_array(&doc_buffer, context.doc_info->structures, write_doc_structure, offset_table_index + 8); - u32 struct_count = 0; - bh_arr(AstBinding *) structs = context.doc_info->structures; - bh_arr_each(AstBinding *, pbind, structs) { - if (write_doc_structure(&doc_buffer, *pbind, (*pbind)->node)) { - struct_count++; - } - } - *((u32 *) bh_pointer_add(doc_buffer.data, struct_count_patch)) = struct_count; + // + // Enum Info + // + write_doc_entity_array(&doc_buffer, context.doc_info->enumerations, write_doc_enum, offset_table_index + 12); // // File Info // - *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 12)) = doc_buffer.length; + *((u32 *) bh_pointer_add(doc_buffer.data, offset_table_index + 16)) = doc_buffer.length; bh_buffer_write_u32(&doc_buffer, shlenu(context.doc_info->file_ids)); fori (i, 0, shlen(context.doc_info->file_ids)) { diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index 5c78f4ea..43ff6a5c 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -403,6 +403,7 @@ static void context_init(CompileOptions* opts) { memset(context.doc_info, 0, sizeof(OnyxDocInfo)); bh_arr_new(global_heap_allocator, context.doc_info->procedures, 128); bh_arr_new(global_heap_allocator, context.doc_info->structures, 128); + bh_arr_new(global_heap_allocator, context.doc_info->enumerations, 128); } } diff --git a/core/builtin.onyx b/core/builtin.onyx index 2d354a2b..3902de7e 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -412,10 +412,10 @@ Optional :: struct (Value_Type: type_expr) { #doc """ - This structure represents the result of a '#callsite' expression. Currently, - #callsite is only valid (and parsed) as a default value for a procedure parameter. - It allows the function to get the address of the calling site, which can be - used for error printing, unique hashes, and much more. + This structure represents the result of a '#callsite' expression. Currently, #callsite + is only valid (and parsed) as a default value for a procedure parameter. It allows + the function to get the address of the calling site, which can be used for error + printing, unique hashes, and much more. """ CallSite :: struct { file : str; diff --git a/core/doc/doc.onyx b/core/doc/doc.onyx index 1a0112f4..44150245 100644 --- a/core/doc/doc.onyx +++ b/core/doc/doc.onyx @@ -3,10 +3,11 @@ package core.doc Doc :: struct { header: Doc_Header; - packages: [] Doc_Package; - procedures: [] Doc_Procedure; - structures: [] Doc_Structure; - files: [] Doc_File; + packages: [] Doc_Package; + procedures: [] Doc_Procedure; + structures: [] Doc_Structure; + enumerations: [] Doc_Enum; + files: [] Doc_File; } @@ -22,6 +23,7 @@ Doc_Header :: struct { packages_info_start: u32; procedures_info_start: u32; structures_info_start: u32; + enumerations_info_start: u32; files_info_start: u32; } @@ -106,3 +108,18 @@ Doc_Structure_Member :: struct { flags: Flags; } +Doc_Enum :: struct { + use base: Doc_Entity; + + values: [] Doc_Enum_Value; + + Flags :: enum #flags { + Is_Flags; + } + flags: Flags; +} + +Doc_Enum_Value :: struct { + name: str; + value: u64; +} \ No newline at end of file