mirror of
https://github.com/Ed94/metadesk.git
synced 2026-07-24 00:17:53 +00:00
coallpsed arena -> allocatorinfo : command_line.h/c
This commit is contained in:
+1
-135
@@ -34,39 +34,6 @@ cmd_line_opt_from_slot(CmdLineOpt** slot, String8 string) {
|
||||
return result;
|
||||
}
|
||||
|
||||
CmdLineOpt*
|
||||
cmd_line_insert_opt(Arena* arena, CmdLine* cmd_line, String8 string, String8List values)
|
||||
{
|
||||
#if MD_DONT_MAP_ARENA_TO_ALLOCATOR_IMPL
|
||||
CmdLineOpt *var = 0;
|
||||
CmdLineOpt **slot = cmd_line_slot_from_string(cmd_line, string);
|
||||
CmdLineOpt *existing_var = cmd_line_opt_from_slot(slot, string);
|
||||
if(existing_var != 0)
|
||||
{
|
||||
var = existing_var;
|
||||
}
|
||||
else
|
||||
{
|
||||
var = push_array(arena, CmdLineOpt, 1);
|
||||
var->hash_next = *slot;
|
||||
var->hash = cmd_line_hash_from_string(string);
|
||||
var->string = push_str8_copy(arena, string);
|
||||
var->value_strings = values;
|
||||
|
||||
StringJoin join = {0};
|
||||
join.pre = str8_lit("");
|
||||
join.sep = str8_lit(",");
|
||||
join.post = str8_lit("");
|
||||
var->value_string = str8_list_join(arena, &var->value_strings, &join);
|
||||
*slot = var;
|
||||
cmd_line_push_opt(&cmd_line->options, var);
|
||||
}
|
||||
return var;
|
||||
#else
|
||||
return cmd_line_insert_opt_alloc(arena_allocator(arena), cmd_line, string, values);
|
||||
#endif
|
||||
}
|
||||
|
||||
CmdLineOpt*
|
||||
cmd_line_insert_opt_alloc(AllocatorInfo ainfo, CmdLine* cmd_line, String8 string, String8List values)
|
||||
{
|
||||
@@ -97,108 +64,7 @@ cmd_line_insert_opt_alloc(AllocatorInfo ainfo, CmdLine* cmd_line, String8 string
|
||||
return var;
|
||||
}
|
||||
|
||||
CmdLine
|
||||
cmd_line_from_string_list(Arena* arena, String8List command_line)
|
||||
{
|
||||
#if MD_DONT_MAP_ARENA_TO_ALLOCATOR_IMPL
|
||||
CmdLine parsed = {0};
|
||||
parsed.exe_name = command_line.first->string;
|
||||
|
||||
// NOTE(rjf): Set up config option table.
|
||||
{
|
||||
parsed.option_table_size = 4096;
|
||||
parsed.option_table = push_array(arena, CmdLineOpt*, parsed.option_table_size);
|
||||
}
|
||||
|
||||
// NOTE(rjf): Parse command line.
|
||||
B32 after_passthrough_option = 0;
|
||||
B32 first_passthrough = 1;
|
||||
for (String8Node* node = command_line.first->next, *next = 0; node != 0; node = next)
|
||||
{
|
||||
next = node->next;
|
||||
String8 option_name = node->string;
|
||||
|
||||
// NOTE(rjf): Look at -- or - at the start of an argument to determine if it's
|
||||
// a flag option. All arguments after a single "--" (with no trailing string
|
||||
// on the command line will be considered as input files.
|
||||
B32 is_option = 1;
|
||||
if(after_passthrough_option == 0)
|
||||
{
|
||||
if (str8_match(node->string, str8_lit("--"), 0)) {
|
||||
after_passthrough_option = 1;
|
||||
is_option = 0;
|
||||
}
|
||||
else if (str8_match(str8_prefix(node->string, 2), str8_lit("--"), 0)) {
|
||||
option_name = str8_skip(option_name, 2);
|
||||
}
|
||||
else if (str8_match(str8_prefix(node->string, 1), str8_lit("-"), 0)) {
|
||||
option_name = str8_skip(option_name, 1);
|
||||
}
|
||||
else {
|
||||
is_option = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
is_option = 0;
|
||||
}
|
||||
|
||||
// NOTE(rjf): This string is an option.
|
||||
if(is_option)
|
||||
{
|
||||
B32 has_arguments = 0;
|
||||
U64 arg_signifier_position1 = str8_find_needle(option_name, 0, str8_lit(":"), 0);
|
||||
U64 arg_signifier_position2 = str8_find_needle(option_name, 0, str8_lit("="), 0);
|
||||
U64 arg_signifier_position = min(arg_signifier_position1, arg_signifier_position2);
|
||||
String8 arg_portion_this_string = str8_skip(option_name, arg_signifier_position+1);
|
||||
if (arg_signifier_position < option_name.size) {
|
||||
has_arguments = 1;
|
||||
}
|
||||
option_name = str8_prefix(option_name, arg_signifier_position);
|
||||
|
||||
String8List arguments = {0};
|
||||
|
||||
// NOTE(rjf): Parse arguments.
|
||||
if (has_arguments)
|
||||
{
|
||||
for (String8Node* n = node; n; n = n->next)
|
||||
{
|
||||
next = n->next;
|
||||
|
||||
String8 string = n->string;
|
||||
if (n == node) {
|
||||
string = arg_portion_this_string;
|
||||
}
|
||||
|
||||
U8 splits[] = { ',' };
|
||||
String8List args_in_this_string = str8_split(arena, string, splits, array_count(splits), 0);
|
||||
for (String8Node* sub_arg = args_in_this_string.first; sub_arg; sub_arg = sub_arg->next) {
|
||||
str8_list_push(arena, &arguments, sub_arg->string);
|
||||
}
|
||||
if ( !str8_match(str8_postfix(n->string, 1), str8_lit(","), 0) && (n != node || arg_portion_this_string.size != 0)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE(rjf): Register config variable.
|
||||
cmd_line_insert_opt(arena, &parsed, option_name, arguments);
|
||||
}
|
||||
|
||||
// NOTE(rjf): Default path, treat as a passthrough config option to be
|
||||
// handled by tool-specific code.
|
||||
else if ( !str8_match(node->string, str8_lit("--"), 0) || !first_passthrough) {
|
||||
str8_list_push(arena, &parsed.inputs, node->string);
|
||||
after_passthrough_option = 1;
|
||||
first_passthrough = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return parsed;
|
||||
#else
|
||||
return cmd_line_from_string_list_alloc(arena_allocator(arena), command_line);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
CmdLine
|
||||
cmd_line_from_string_list_alloc(AllocatorInfo ainfo, String8List command_line)
|
||||
|
||||
@@ -52,11 +52,25 @@ String8 cmd_line_string (CmdLine* cmd_line, String8 name);
|
||||
B32 cmd_line_has_flag (CmdLine* cmd_line, String8 name);
|
||||
B32 cmd_line_has_argument (CmdLine* cmd_line, String8 name);
|
||||
|
||||
MD_API CmdLineOpt* cmd_line_insert_opt (Arena* arena, CmdLine* cmd_line, String8 string, String8List values);
|
||||
inline CmdLineOpt* cmd_line_insert_opt_push (Arena* arena, CmdLine* cmd_line, String8 string, String8List values);
|
||||
MD_API CmdLineOpt* cmd_line_insert_opt_alloc (AllocatorInfo ainfo, CmdLine* cmd_line, String8 string, String8List values);
|
||||
MD_API CmdLine cmd_line_from_string_list (Arena* arena, String8List arguments);
|
||||
inline CmdLine cmd_line_from_string_list_push (Arena* arena, String8List arguments);
|
||||
MD_API CmdLine cmd_line_from_string_list_alloc(AllocatorInfo ainfo, String8List arguments);
|
||||
|
||||
#define cmd_line_insert_opt(allocator, cmd_line, string, values) \
|
||||
_Generic(allocator, \
|
||||
Arena*: cmd_line_insert_opt_push, \
|
||||
AllocatorInfo: cmd_line_insert_opt_alloc, \
|
||||
default: METADESK_NO_RESOLVED_GENERIC_SELECTION \
|
||||
) MD_RESOLVED_FUNCTION_CALL(allocator, cmd_line, string, values)
|
||||
|
||||
#define cmd_line_from_string_list(allocator, arguments) \
|
||||
_Generic(allocator, \
|
||||
Arena*: cmd_line_from_string_list_push, \
|
||||
AllocatorInfo: cmd_line_from_string_list_alloc, \
|
||||
default: METADESK_NO_RESOLVED_GENERIC_SELECTION \
|
||||
) MD_RESOLVED_FUNCTION_CALL(allocator, arguments)
|
||||
|
||||
inline U64
|
||||
cmd_line_hash_from_string(String8 string) {
|
||||
U64 result = 5381;
|
||||
@@ -66,6 +80,9 @@ cmd_line_hash_from_string(String8 string) {
|
||||
return result;
|
||||
}
|
||||
|
||||
force_inline CmdLineOpt* cmd_line_insert_opt_push (Arena* arena, CmdLine* cmd_line, String8 string, String8List values) { return cmd_line_insert_opt_alloc (arena_allocator(arena), cmd_line, string, values); }
|
||||
force_inline CmdLine cmd_line_from_string_list_push(Arena* arena, String8List command_line) { return cmd_line_from_string_list_alloc(arena_allocator(arena), command_line); }
|
||||
|
||||
inline CmdLineOpt* cmd_line_opt_from_string(CmdLine *cmd_line, String8 name) { return cmd_line_opt_from_slot(cmd_line_slot_from_string(cmd_line, name), name); }
|
||||
inline B32 cmd_line_has_flag (CmdLine *cmd_line, String8 name) { CmdLineOpt *var = cmd_line_opt_from_string(cmd_line, name); return(var != 0); }
|
||||
inline B32 cmd_line_has_argument (CmdLine *cmd_line, String8 name) { CmdLineOpt *var = cmd_line_opt_from_string(cmd_line, name); return(var != 0 && var->value_strings.node_count > 0); }
|
||||
|
||||
@@ -22,7 +22,7 @@ void main_thread_base_entry_point(MainThread_EntryPointProc* entry_point, char**
|
||||
TempArena scratch = scratch_begin(0, 0);
|
||||
|
||||
String8List command_line_argument_strings = os_string_list_from_argcv(scratch.arena, (int)arguments_count, arguments);
|
||||
CmdLine cmdline = cmd_line_from_string_list(scratch.arena, command_line_argument_strings);
|
||||
CmdLine cmdline = cmd_line_from_string_list_push(scratch.arena, command_line_argument_strings);
|
||||
B32 capture = cmd_line_has_flag(&cmdline, str8_lit("capture"));
|
||||
if (capture) {
|
||||
prof_begin_capture(arguments[0]);
|
||||
|
||||
@@ -42,7 +42,7 @@ void tctx_read_srcloc (char** file_name, U64* line_number);
|
||||
inline TempArena
|
||||
scratch__begin_alloc(AllocatorInfo ainfo) {
|
||||
Arena* arena = extract_arena(ainfo);
|
||||
TempArena scratch = temp_begin(tctx_get_scratch(arena, arena != nullptr));
|
||||
TempArena scratch = temp_begin(tctx_get_scratch(&arena, arena != nullptr));
|
||||
return scratch;
|
||||
}
|
||||
|
||||
|
||||
+8
-86
@@ -99,23 +99,7 @@ content_string_from_token_flags_str8(TokenFlags flags, String8 string)
|
||||
String8List
|
||||
string_list_from_token_flags(Arena* arena, TokenFlags flags)
|
||||
{
|
||||
#if MD_DONT_MAP_ANREA_TO_ALLOCATOR_IMPL
|
||||
String8List strs = {0};
|
||||
if (flags & TokenFlag_Identifier ){ str8_list_push(arena, &strs, str8_lit("Identifier" )); }
|
||||
if (flags & TokenFlag_Numeric ){ str8_list_push(arena, &strs, str8_lit("Numeric" )); }
|
||||
if (flags & TokenFlag_StringLiteral ){ str8_list_push(arena, &strs, str8_lit("StringLiteral" )); }
|
||||
if (flags & TokenFlag_Symbol ){ str8_list_push(arena, &strs, str8_lit("Symbol" )); }
|
||||
if (flags & TokenFlag_Reserved ){ str8_list_push(arena, &strs, str8_lit("Reserved" )); }
|
||||
if (flags & TokenFlag_Comment ){ str8_list_push(arena, &strs, str8_lit("Comment" )); }
|
||||
if (flags & TokenFlag_Whitespace ){ str8_list_push(arena, &strs, str8_lit("Whitespace" )); }
|
||||
if (flags & TokenFlag_Newline ){ str8_list_push(arena, &strs, str8_lit("Newline" )); }
|
||||
if (flags & TokenFlag_BrokenComment ){ str8_list_push(arena, &strs, str8_lit("BrokenComment" )); }
|
||||
if (flags & TokenFlag_BrokenStringLiteral ){ str8_list_push(arena, &strs, str8_lit("BrokenStringLiteral")); }
|
||||
if (flags & TokenFlag_BadCharacter ){ str8_list_push(arena, &strs, str8_lit("BadCharacter" )); }
|
||||
return strs;
|
||||
#else
|
||||
return string_list_from_token_flags_alloc(arena_allocator(arena), flags);
|
||||
#endif
|
||||
}
|
||||
|
||||
String8List
|
||||
@@ -139,21 +123,7 @@ string_list_from_token_flags_alloc(AllocatorInfo ainfo, TokenFlags flags)
|
||||
void
|
||||
token_chunk_list_push(Arena* arena, TokenChunkList* list, U64 cap, Token token)
|
||||
{
|
||||
#if MD_DONT_MAP_ANREA_TO_ALLOCATOR_IMPL
|
||||
TokenChunkNode* node = list->last;
|
||||
if (node == 0 || node->count >= node->cap) {
|
||||
node = push_array(arena, TokenChunkNode, 1);
|
||||
node->cap = cap;
|
||||
node->v = push_array_no_zero(arena, Token, cap);
|
||||
sll_queue_push(list->first, list->last, node);
|
||||
list->chunk_count += 1;
|
||||
}
|
||||
memory_copy_struct(&node->v[node->count], &token);
|
||||
node->count += 1;
|
||||
list->total_token_count += 1;
|
||||
#else
|
||||
token_chunk_list_alloc(arena_allocator(arena), list, cap, token);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
@@ -173,22 +143,9 @@ token_chunk_list_alloc(AllocatorInfo ainfo, TokenChunkList* list, U64 cap, Token
|
||||
}
|
||||
|
||||
TokenArray
|
||||
token_array_from_chunk_list(Arena* arena, TokenChunkList* chunks)
|
||||
token_array_from_chunk_list_push(Arena* arena, TokenChunkList* chunks)
|
||||
{
|
||||
#if MD_DONT_MAP_ANREA_TO_ALLOCATOR_IMPL
|
||||
TokenArray result = {0};
|
||||
result.count = chunks->total_token_count;
|
||||
result.v = push_array_no_zero(arena, Token, result.count);
|
||||
U64 write_idx = 0;
|
||||
for(TokenChunkNode *n = chunks->first; n != 0; n = n->next)
|
||||
{
|
||||
memory_copy(result.v + write_idx, n->v, size_of(Token) * n->count);
|
||||
write_idx += n->count;
|
||||
}
|
||||
return result;
|
||||
#else
|
||||
return token_array_from_chunk_list_alloc(arena_allocator(arena), chunks);
|
||||
#endif
|
||||
}
|
||||
|
||||
TokenArray
|
||||
@@ -328,42 +285,7 @@ tree_match(Node* a, Node* b, StringMatchFlags flags)
|
||||
Node*
|
||||
tree_copy(Arena* arena, Node* src_root)
|
||||
{
|
||||
#if MD_DONT_MAP_ANREA_TO_ALLOCATOR_IMPL
|
||||
Node* dst_root = nil_node();
|
||||
Node* dst_parent = dst_root;
|
||||
{
|
||||
NodeRec rec = {0};
|
||||
for(Node* src = src_root; !node_is_nil(src); src = rec.next)
|
||||
{
|
||||
Node* dst = push_array(arena, Node, 1);
|
||||
dst->first = dst->last = dst->parent = dst->next = dst->prev = nil_node();
|
||||
dst->first_tag = dst->last_tag = nil_node();
|
||||
dst->kind = src->kind;
|
||||
dst->flags = src->flags;
|
||||
dst->string = push_str8_copy(arena, src->string);
|
||||
dst->raw_string = push_str8_copy(arena, src->raw_string);
|
||||
dst->src_offset = src->src_offset;
|
||||
dst->parent = dst_parent;
|
||||
if (dst_parent != nil_node()) {
|
||||
dll_push_back_npz(nil_node(), dst_parent->first, dst_parent->last, dst, next, prev);
|
||||
}
|
||||
else {
|
||||
dst_root = dst_parent = dst;
|
||||
}
|
||||
|
||||
rec = node_rec_depth_first_pre(src, src_root);
|
||||
if (rec.push_count != 0) {
|
||||
dst_parent = dst;
|
||||
}
|
||||
else for (U64 idx = 0; idx < rec.pop_count; idx += 1) {
|
||||
dst_parent = dst_parent->parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dst_root;
|
||||
#else
|
||||
tree_copy_alloc(arena_allocator(arena), src_root);
|
||||
#endif
|
||||
return tree_copy_alloc(arena_allocator(arena), src_root);
|
||||
}
|
||||
|
||||
Node*
|
||||
@@ -408,7 +330,7 @@ tree_copy_alloc(AllocatorInfo ainfo, Node* src_root)
|
||||
|
||||
TokenizeResult
|
||||
tokenize_from_text(Arena* arena, String8 text) {
|
||||
tokenize_from_text_alloc(arena_allocator(arena), text);
|
||||
return tokenize_from_text_alloc(arena_allocator(arena), text);
|
||||
}
|
||||
|
||||
TokenizeResult
|
||||
@@ -791,12 +713,12 @@ void parse__work_pop(ParseWorkNode* work_top, ParseWorkNode* broken_work) {
|
||||
}
|
||||
|
||||
ParseResult
|
||||
parse_from_text_tokens(Arena* arena, String8 filename, String8 text, TokenArray tokens) {
|
||||
parse_from_text_tokens_push(Arena* arena, String8 filename, String8 text, TokenArray tokens) {
|
||||
return parse_from_text_tokens_alloc(arena_allocator(arena), filename, text, tokens);
|
||||
}
|
||||
|
||||
ParseResult
|
||||
parse_from_text_tokens(AllocatorInfo ainfo, String8 filename, String8 text, TokenArray tokens)
|
||||
parse_from_text_tokens_alloc(AllocatorInfo ainfo, String8 filename, String8 text, TokenArray tokens)
|
||||
{
|
||||
TempArena scratch = scratch_begin_alloc(ainfo);
|
||||
|
||||
@@ -952,7 +874,7 @@ parse_from_text_tokens(AllocatorInfo ainfo, String8 filename, String8 text, Toke
|
||||
|
||||
work_top->gathered_node_flags = 0;
|
||||
|
||||
Node* node = ainfo_node(ainfo, NodeKind_Main, flags, node_string, node_string_raw, token[0].range.min);
|
||||
Node* node = alloc_node(ainfo, NodeKind_Main, flags, node_string, node_string_raw, token[0].range.min);
|
||||
node->first_tag = work_top->first_gathered_tag;
|
||||
node->last_tag = work_top->last_gathered_tag;
|
||||
for (Node* tag = work_top->first_gathered_tag; !node_is_nil(tag); tag = tag->next) {
|
||||
@@ -1115,13 +1037,13 @@ ParseResult
|
||||
parse_from_text(Arena* arena, String8 filename, String8 text) {
|
||||
TempArena scratch = scratch_begin(&arena, 1);
|
||||
TokenizeResult tokenize = tokenize_from_text(scratch.arena, text);
|
||||
ParseResult parse = parse_from_text_tokens(arena, filename, text, tokenize.tokens);
|
||||
ParseResult parse = parse_from_text_tokens_push(arena, filename, text, tokenize.tokens);
|
||||
scratch_end(scratch);
|
||||
return parse;
|
||||
}
|
||||
|
||||
ParseResult
|
||||
parse_from_text(AllocatorInfo ainfo, String8 filename, String8 text) {
|
||||
parse_from_text_alloc(AllocatorInfo ainfo, String8 filename, String8 text) {
|
||||
TempArena scratch = scratch_begin_alloc(ainfo);
|
||||
TokenizeResult tokenize = tokenize_from_text(scratch.arena, text);
|
||||
ParseResult parse = parse_from_text_tokens_alloc(ainfo, filename, text, tokenize.tokens);
|
||||
|
||||
+5
-2
@@ -309,9 +309,12 @@ B32 token_match(Token a, Token b);
|
||||
|
||||
MD_API String8 content_string_from_token_flags_str8(TokenFlags flags, String8 string);
|
||||
|
||||
MD_API String8List string_list_from_token_flags(Arena* arena, TokenFlags flags);
|
||||
MD_API String8List string_list_from_token_flags(Arena* arena, TokenFlags flags);
|
||||
MD_API String8List string_list_from_token_flags_alloc(AllocatorInfo ainfo, TokenFlags flags);
|
||||
MD_API void token_chunk_list_push (Arena* arena, TokenChunkList* list, U64 cap, Token token);
|
||||
MD_API TokenArray token_array_from_chunk_list (Arena* arena, TokenChunkList* chunks);
|
||||
MD_API void token_chunk_list_alloc (AllocatorInfo ainfo, TokenChunkList* list, U64 cap, Token token);
|
||||
MD_API TokenArray token_array_from_chunk_list_push(Arena* arena, TokenChunkList* chunks);
|
||||
MD_API TokenArray token_array_from_chunk_list_alloc(AllocatorInfo aino, TokenChunkList* chunks);
|
||||
|
||||
inline Token
|
||||
token_make(Rng1U64 range, TokenFlags flags) {
|
||||
|
||||
+1
-1
@@ -18,5 +18,5 @@ int main()
|
||||
|
||||
printf("metadesk: got past init!");
|
||||
|
||||
deinit(& ctx);
|
||||
// deinit(& ctx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user