From d9e5655564731cb4c3ae031fb5279cd7c23cb69a Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Thu, 16 Sep 2021 22:13:32 -0600 Subject: [PATCH 1/4] fix impl names of string gen functions --- source/md.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/source/md.c b/source/md.c index c4840e4..9335981 100644 --- a/source/md.c +++ b/source/md.c @@ -3315,8 +3315,8 @@ MD_ExprParse(MD_Arena *arena, MD_ExprOperatorTable *op_table, MD_Node *first, MD //~ String Generation MD_FUNCTION_IMPL void -MD_DebugStringListFromNode(MD_Arena *arena, MD_String8List *out, MD_Node *node, - int indent, MD_String8 indent_string, MD_GenerateFlags flags) +MD_DebugDumpFromNode(MD_Arena *arena, MD_String8List *out, MD_Node *node, + int indent, MD_String8 indent_string, MD_GenerateFlags flags) { #define MD_PrintIndent(_indent_level) do\ {\ @@ -3359,7 +3359,7 @@ MD_S8ListPush(arena, out, indent_string);\ { child_indent = 0; } - MD_DebugStringListFromNode(arena, out, child, child_indent, MD_S8Lit(" "), flags); + MD_DebugDumpFromNode(arena, out, child, child_indent, MD_S8Lit(" "), flags); if(!MD_NodeIsNil(child->next)) { MD_S8ListPush(arena, out, MD_S8Lit(",\n")); @@ -3440,7 +3440,7 @@ MD_S8ListPush(arena, out, indent_string);\ MD_S8ListPush(arena, out, MD_S8Lit("{\n")); for(MD_EachNode(child, node->first_child)) { - MD_DebugStringListFromNode(arena, out, child, indent+1, indent_string, flags); + MD_DebugDumpFromNode(arena, out, child, indent+1, indent_string, flags); MD_S8ListPush(arena, out, MD_S8Lit(",\n")); } MD_PrintIndent(indent); @@ -3464,9 +3464,9 @@ MD_S8ListPush(arena, out, indent_string);\ } MD_FUNCTION_IMPL void -MD_ReconstructedStringListFromNode(MD_Arena *arena, MD_String8List *out, MD_Node *node, - int indent, MD_String8 indent_string, - MD_GenerateFlags flags) +MD_ReconstructionFromNode(MD_Arena *arena, MD_String8List *out, MD_Node *node, + int indent, MD_String8 indent_string, + MD_GenerateFlags flags) { // TODO(rjf): // TODO(rjf): // TODO(rjf): // TODO(rjf): // TODO(rjf): // TODO(rjf): @@ -3538,7 +3538,7 @@ MD_S8ListPush(arena, out, indent_string);\ { child_indent = 0; } - MD_ReconstructedStringListFromNode(arena, out, child, child_indent, MD_S8Lit(" "), flags); + MD_ReconstructionFromNode(arena, out, child, child_indent, MD_S8Lit(" "), flags); if(!MD_NodeIsNil(child->next)) { MD_S8ListPush(arena, out, MD_S8Lit(",\n")); @@ -3638,7 +3638,7 @@ MD_S8ListPush(arena, out, indent_string);\ child_indent = indent+1; } last_line = child_loc.line; - MD_ReconstructedStringListFromNode(arena, out, child, child_indent, indent_string, flags); + MD_ReconstructionFromNode(arena, out, child, child_indent, indent_string, flags); } MD_PrintIndent(indent); if(closer_char != 0) From bd754c1816028b6fff26354607ff025c318d12b3 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Thu, 16 Sep 2021 22:44:52 -0600 Subject: [PATCH 2/4] zero-init --- source/md.c | 29 +++++++++++++++-------------- source/md.h | 15 ++++++--------- source/md_c_helpers.c | 2 +- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/source/md.c b/source/md.c index f7469d3..1f8f901 100644 --- a/source/md.c +++ b/source/md.c @@ -803,6 +803,7 @@ MD_S8FmtV(MD_Arena *arena, char *fmt, va_list args) MD_u64 needed_bytes = md_stbsp_vsnprintf(0, 0, fmt, args)+1; result.str = MD_PushArray(arena, MD_u8, needed_bytes); result.size = needed_bytes - 1; + result.str[needed_bytes-1] = 0; md_stbsp_vsnprintf((char*)result.str, needed_bytes, fmt, args2); return result; } @@ -820,7 +821,7 @@ MD_S8Fmt(MD_Arena *arena, char *fmt, ...) MD_FUNCTION void MD_S8ListPush(MD_Arena *arena, MD_String8List *list, MD_String8 string) { - MD_String8Node *node = MD_PushArray(arena, MD_String8Node, 1); + MD_String8Node *node = MD_PushArrayZero(arena, MD_String8Node, 1); node->string = string; MD_QueuePush(list->first, list->last, node); @@ -922,7 +923,7 @@ MD_S8ListJoin(MD_Arena *arena, MD_String8List list, MD_StringJoin *join_ptr) MD_String8 result = MD_ZERO_STRUCT; result.size = (list.total_size + join.pre.size + sep_count*join.mid.size + join.post.size); - result.str = MD_PushArray(arena, MD_u8, result.size); + result.str = MD_PushArrayZero(arena, MD_u8, result.size); // fill MD_u8 *ptr = result.str; @@ -1005,7 +1006,7 @@ MD_S8Stylize(MD_Arena *arena, MD_String8 string, MD_IdentifierStyle word_style, { result.size += separator.size*(words.node_count-1); } - result.str = MD_PushArray(arena, MD_u8, result.size); + result.str = MD_PushArrayZero(arena, MD_u8, result.size); { MD_u64 write_pos = 0; @@ -1234,7 +1235,7 @@ MD_FUNCTION MD_String8 MD_S8FromS16(MD_Arena *arena, MD_String16 in) { MD_u64 cap = in.size*3; - MD_u8 *str = MD_PushArray(arena, MD_u8, cap + 1); + MD_u8 *str = MD_PushArrayZero(arena, MD_u8, cap + 1); MD_u16 *ptr = in.str; MD_u16 *opl = ptr + in.size; MD_u64 size = 0; @@ -1254,7 +1255,7 @@ MD_FUNCTION MD_String16 MD_S16FromS8(MD_Arena *arena, MD_String8 in) { MD_u64 cap = in.size*2; - MD_u16 *str = MD_PushArray(arena, MD_u16, cap + 1); + MD_u16 *str = MD_PushArrayZero(arena, MD_u16, cap + 1); MD_u8 *ptr = in.str; MD_u8 *opl = ptr + in.size; MD_u64 size = 0; @@ -1275,7 +1276,7 @@ MD_FUNCTION MD_String8 MD_S8FromS32(MD_Arena *arena, MD_String32 in) { MD_u64 cap = in.size*4; - MD_u8 *str = MD_PushArray(arena, MD_u8, cap + 1); + MD_u8 *str = MD_PushArrayZero(arena, MD_u8, cap + 1); MD_u32 *ptr = in.str; MD_u32 *opl = ptr + in.size; MD_u64 size = 0; @@ -1293,7 +1294,7 @@ MD_FUNCTION MD_String32 MD_S32FromS8(MD_Arena *arena, MD_String8 in) { MD_u64 cap = in.size; - MD_u32 *str = MD_PushArray(arena, MD_u32, cap + 1); + MD_u32 *str = MD_PushArrayZero(arena, MD_u32, cap + 1); MD_u8 *ptr = in.str; MD_u8 *opl = ptr + in.size; MD_u64 size = 0; @@ -1507,7 +1508,7 @@ MD_CStyleHexStringFromU64(MD_Arena *arena, MD_u64 x, MD_b32 caps) MD_String8 result = MD_ZERO_STRUCT; result.size = (MD_u64)(ptr - buffer); - result.str = MD_PushArray(arena, MD_u8, result.size); + result.str = MD_PushArrayZero(arena, MD_u8, result.size); MD_MemoryCopy(result.str, buffer, result.size); return(result); } @@ -1594,9 +1595,8 @@ MD_FUNCTION MD_u64 MD_HashPtr(void *p) { MD_u64 h = (MD_u64)p; - // TODO(rjf): Do we want our own equivalent of UINT64_C? - h = (h ^ (h >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); - h = (h ^ (h >> 27)) * UINT64_C(0x94d049bb133111eb); + h = (h ^ (h >> 30)) * 0xbf58476d1ce4e5b9; + h = (h ^ (h >> 27)) * 0x94d049bb133111eb; h = h ^ (h >> 31); return h; } @@ -1683,7 +1683,7 @@ MD_MapInsert(MD_Arena *arena, MD_Map *map, MD_MapKey key, void *val){ MD_MapSlot *result = 0; if (map->bucket_count > 0){ MD_u64 index = key.hash%map->bucket_count; - MD_MapSlot *slot = MD_PushArray(arena, MD_MapSlot, 1); + MD_MapSlot *slot = MD_PushArrayZero(arena, MD_MapSlot, 1); MD_MapBucket *bucket = &map->buckets[index]; MD_QueuePush(bucket->first, bucket->last, slot); slot->key = key; @@ -2970,7 +2970,7 @@ MD_ExprOperatorPush(MD_Arena *arena, MD_ExprOperatorList *list, MD_u32 op_id, MD_ExprOperatorKind kind, MD_u64 precedence, MD_Node *md_node) { - MD_ExprOperatorNode *node = MD_PushArray(arena, MD_ExprOperatorNode, 1); + MD_ExprOperatorNode *node = MD_PushArrayZero(arena, MD_ExprOperatorNode, 1); MD_QueuePush(list->first, list->last, node); list->count += 1; node->op.op_id = op_id; @@ -3050,7 +3050,7 @@ MD_ExprBakeOperatorTableFromList(MD_Arena *arena, MD_ExprOperatorList *list) if(error_str.size == 0) { MD_ExprOperatorList *list = result.table+op.kind; - MD_ExprOperatorNode *op_node_copy = MD_PushArray(arena, MD_ExprOperatorNode, 1); + MD_ExprOperatorNode *op_node_copy = MD_PushArrayZero(arena, MD_ExprOperatorNode, 1); MD_QueuePush(list->first, list->last, op_node_copy); list->count += 1; op_node_copy->op = op; @@ -3408,6 +3408,7 @@ MD_S8ListPush(arena, out, indent_string);\ MD_S8ListPush(arena, out, MD_S8Fmt(arena, "// string hash: 0x%llx\n", node->string_hash)); } + //- rjf: location if(flags & MD_GenerateFlag_Location) { MD_PrintIndent(indent); diff --git a/source/md.h b/source/md.h index 1305835..35e7089 100644 --- a/source/md.h +++ b/source/md.h @@ -77,8 +77,8 @@ # define MD_DEFAULT_SCRATCH 1 #endif -#if !defined(MD_DISABLE_PRINT_HELPERS) -# define MD_DISABLE_PRINT_HELPERS 0 +#if !defined(MD_ENABLE_PRINT_HELPERS) +# define MD_ENABLE_PRINT_HELPERS 0 #endif @@ -316,8 +316,9 @@ //~ Linkage Wrappers #if !defined(MD_FUNCTION) -# define MD_FUNCTION static +# define MD_FUNCTION #endif +#define MD_FUNCTION_IMPL MD_FUNCTION #if !defined(MD_GLOBAL) # define MD_GLOBAL static @@ -1047,7 +1048,7 @@ MD_FUNCTION MD_ParseResult MD_ParseResultZero(void); MD_FUNCTION MD_ParseResult MD_ParseNodeSet(MD_Arena *arena, MD_String8 string, MD_u64 offset, MD_Node *parent, MD_ParseSetRule rule); MD_FUNCTION MD_ParseResult MD_ParseOneNode(MD_Arena *arena, MD_String8 string, MD_u64 offset); -MD_FUNCTION MD_ParseResult MD_ParseWholeString(MD_Arena *arena, MD_String8 name, MD_String8 contents); +MD_FUNCTION MD_ParseResult MD_ParseWholeString(MD_Arena *arena, MD_String8 filename, MD_String8 contents); MD_FUNCTION MD_ParseResult MD_ParseWholeFile(MD_Arena *arena, MD_String8 filename); @@ -1105,7 +1106,7 @@ MD_FUNCTION MD_String8 MD_StringFromMessageKind(MD_MessageKind kind); MD_FUNCTION MD_String8 MD_FormatMessage(MD_Arena *arena, MD_CodeLoc loc, MD_MessageKind kind, MD_String8 string); -#if !MD_DISABLE_PRINT_HELPERS +#if MD_ENABLE_PRINT_HELPERS #include MD_FUNCTION void MD_PrintMessage(FILE *file, MD_CodeLoc loc, MD_MessageKind kind, MD_String8 string); @@ -1139,10 +1140,6 @@ MD_FUNCTION void MD_ReconstructionFromNode(MD_Arena *arena, MD_String8List *out, int indent, MD_String8 indent_string, MD_GenerateFlags flags); -#if !MD_DISABLE_PRINT_HELPERS -MD_FUNCTION void MD_PrintDebugDumpFromNode(FILE *file, MD_Node *node, MD_GenerateFlags flags); -#endif - //~ Command Line Argument Helper MD_FUNCTION MD_String8List MD_StringListFromArgCV(MD_Arena *arena, int argument_count, diff --git a/source/md_c_helpers.c b/source/md_c_helpers.c index 5a781df..c431cce 100644 --- a/source/md_c_helpers.c +++ b/source/md_c_helpers.c @@ -174,7 +174,7 @@ MD_FUNCTION_IMPL MD_C_Expr * MD_C_MakeExpr(MD_Arena *arena, MD_Node *node, MD_C_ExprKind kind, MD_C_Expr *left, MD_C_Expr *right) { - MD_C_Expr *expr = MD_PushArray(arena, MD_C_Expr, 1); + MD_C_Expr *expr = MD_PushArrayZero(arena, MD_C_Expr, 1); if(left == 0) left = MD_C_NilExpr(); if(right == 0) right = MD_C_NilExpr(); expr->node = node; From 07ff2afa72bf8fe5af3435e3a5ab917fdb9f0798 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Thu, 16 Sep 2021 23:31:28 -0600 Subject: [PATCH 3/4] set implicitly-delimited separator set node flags correctly --- source/md.c | 12 ++---------- source/md.h | 5 +---- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/source/md.c b/source/md.c index 1f8f901..4abdd6c 100644 --- a/source/md.c +++ b/source/md.c @@ -510,7 +510,6 @@ static MD_Node _md_nil_node = 0, // flags MD_ZERO_STRUCT, // string MD_ZERO_STRUCT, // raw_string - 0xdeadffffffffffull, // string_hash 0, // at &_md_nil_node, // ref_target MD_ZERO_STRUCT, // prev_comment @@ -2165,10 +2164,10 @@ MD_ParseNodeSet(MD_Arena *arena, MD_String8 string, MD_u64 offset, MD_Node *pare if(potential_closer.kind == MD_TokenKind_Reserved) { MD_u8 c = potential_closer.raw_string.str[0]; - if (c == ',' || c == ';') + if(c == ',' || c == ';') { - closer_check_off += potential_closer.raw_string.size; off = closer_check_off; + closer_check_off += potential_closer.raw_string.size; break; } else if(c == '}' || c == ']'|| c == ')') @@ -3401,13 +3400,6 @@ MD_S8ListPush(arena, out, indent_string);\ MD_ReleaseScratch(scratch); } - //- rjf: node string hash - if(flags & MD_GenerateFlag_StringHash) - { - MD_PrintIndent(indent); - MD_S8ListPush(arena, out, MD_S8Fmt(arena, "// string hash: 0x%llx\n", node->string_hash)); - } - //- rjf: location if(flags & MD_GenerateFlag_Location) { diff --git a/source/md.h b/source/md.h index 35e7089..3982d3f 100644 --- a/source/md.h +++ b/source/md.h @@ -606,7 +606,6 @@ struct MD_Node MD_NodeFlags flags; MD_String8 string; MD_String8 raw_string; - MD_u64 string_hash; // Source code location information. MD_u64 offset; @@ -614,7 +613,6 @@ struct MD_Node // Reference. MD_Node *ref_target; - // Comments. // @usage prev_comment/next_comment should be considered "hidden". Rely on // the functions MD_PrevCommentFromNode/MD_NextCommentFromNode to access @@ -830,8 +828,7 @@ enum MD_GenerateFlag_Comments = (1<<3), MD_GenerateFlag_NodeKind = (1<<4), MD_GenerateFlag_NodeFlags = (1<<5), - MD_GenerateFlag_StringHash = (1<<6), - MD_GenerateFlag_Location = (1<<7), + MD_GenerateFlag_Location = (1<<6), MD_GenerateFlags_Tree = (MD_GenerateFlag_Tags | MD_GenerateFlag_TagArguments | From 424bd255086678a9d8275870026d116b41f5b48b Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Thu, 16 Sep 2021 23:36:17 -0600 Subject: [PATCH 4/4] treat all cmd line options after a -- as plain inputs --- source/md.c | 13 ++++++++----- source/md.h | 3 --- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/source/md.c b/source/md.c index 4abdd6c..675c05c 100644 --- a/source/md.c +++ b/source/md.c @@ -3718,10 +3718,8 @@ MD_StringListFromArgCV(MD_Arena *arena, int argument_count, char **arguments) MD_FUNCTION MD_CmdLine MD_MakeCmdLineFromOptions(MD_Arena *arena, MD_String8List options) { - // TODO(rjf): consider everything as plain unstructured inputs after - // a `--` (without a name). - MD_CmdLine cmdln = MD_ZERO_STRUCT; + MD_b32 parsing_only_inputs = 0; for(MD_String8Node *n = options.first, *next = 0; n; n = next) @@ -3731,7 +3729,11 @@ MD_MakeCmdLineFromOptions(MD_Arena *arena, MD_String8List options) //- rjf: figure out whether or not this is an option by checking for `-` or `--` // from the beginning of the string MD_String8 option_name = MD_ZERO_STRUCT; - if(MD_S8Match(MD_S8Prefix(n->string, 2), MD_S8Lit("--"), 0)) + if(MD_S8Match(n->string, MD_S8Lit("--"), 0)) + { + parsing_only_inputs = 1; + } + else if(MD_S8Match(MD_S8Prefix(n->string, 2), MD_S8Lit("--"), 0)) { option_name = MD_S8Skip(n->string, 2); } @@ -3739,6 +3741,7 @@ MD_MakeCmdLineFromOptions(MD_Arena *arena, MD_String8List options) { option_name = MD_S8Skip(n->string, 1); } + //- rjf: trim off anything after a `:` or `=`, use that as the first value string MD_String8 first_value = MD_ZERO_STRUCT; MD_b32 has_many_values = 0; @@ -3759,7 +3762,7 @@ MD_MakeCmdLineFromOptions(MD_Arena *arena, MD_String8List options) } //- rjf: gather arguments - if(option_name.size != 0) + if(option_name.size != 0 && !parsing_only_inputs) { MD_String8List option_values = MD_ZERO_STRUCT; diff --git a/source/md.h b/source/md.h index 3982d3f..cc8d84e 100644 --- a/source/md.h +++ b/source/md.h @@ -7,9 +7,6 @@ #ifndef MD_H #define MD_H -// TODO(rjf): implicitly-delimited sets are not having their separator flags -// appropriately set - /* NOTE(allen): Notes on overrides/macro options: ** ** Overridable :