WIP - Got it somewhat working, still have issues with multiple files.

This commit is contained in:
2023-03-17 18:12:20 -04:00
parent 7e120ae5e9
commit 97967e56d9
15 changed files with 356 additions and 1089 deletions

View File

@ -1,5 +1,5 @@
#define BLOAT_IMPL
#include "bloat.hpp"
#include "Bloat.hpp"
@ -35,3 +35,133 @@ namespace Memory
zpl_arena_free( & Global_Arena);
}
}
bool opts_custom_add(zpl_opts* opts, zpl_opts_entry *t, char* b)
{
if (t->type != ZPL_OPTS_STRING)
{
return false;
}
t->text = zpl_string_append_length(t->text, " ", 1);
t->text = zpl_string_appendc( t->text, b );
return true;
}
b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
{
zpl_b32 had_errors = false;
for (int i = 1; i < argc; ++i)
{
char* arg = argv[i];
if (*arg)
{
arg = cast(char*)zpl_str_trim(arg, false);
if (*arg == '-')
{
zpl_opts_entry* entry = 0;
zpl_b32 checkln = false;
if ( *(arg + 1) == '-')
{
checkln = true;
++arg;
}
char *b = arg + 1, *e = b;
while (zpl_char_is_alphanumeric(*e) || *e == '-' || *e == '_') {
++e;
}
entry = zpl__opts_find(opts, b, (e - b), checkln);
if (entry)
{
char *ob = b;
b = e;
/**/
if (*e == '=')
{
if (entry->type == ZPL_OPTS_FLAG)
{
*e = '\0';
zpl__opts_push_error(opts, ob, ZPL_OPTS_ERR_EXTRA_VALUE);
had_errors = true;
continue;
}
b = e = e + 1;
}
else if (*e == '\0')
{
char *sp = argv[i+1];
if (sp && *sp != '-' && (zpl_array_count(opts->positioned) < 1 || entry->type != ZPL_OPTS_FLAG))
{
if (entry->type == ZPL_OPTS_FLAG)
{
zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_EXTRA_VALUE);
had_errors = true;
continue;
}
arg = sp;
b = e = sp;
++i;
}
else
{
if (entry->type != ZPL_OPTS_FLAG)
{
zpl__opts_push_error(opts, ob, ZPL_OPTS_ERR_MISSING_VALUE);
had_errors = true;
continue;
}
entry->met = true;
continue;
}
}
e = cast(char *)zpl_str_control_skip(e, '\0');
zpl__opts_set_value(opts, entry, b);
if ( (i + 1) < argc )
{
for ( b = argv[i + 1]; i < argc && b[0] != '-'; i++, b = argv[i + 1] )
{
opts_custom_add(opts, entry, b );
}
}
}
else
{
zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_OPTION);
had_errors = true;
}
}
else if (zpl_array_count(opts->positioned))
{
zpl_opts_entry *l = zpl_array_back(opts->positioned);
zpl_array_pop(opts->positioned);
zpl__opts_set_value(opts, l, arg);
}
else
{
zpl__opts_push_error(opts, arg, ZPL_OPTS_ERR_VALUE);
had_errors = true;
}
}
}
return !had_errors;
}

View File

@ -13,23 +13,12 @@ namespace IO
// Current source and destination index.
// Used to keep track of which file get_next_source or write refer to.
uw Current = 0;
sw Current = -1;
char* Current_Content = nullptr;
uw Current_Size = 0;
uw Largest_Src_Size = 0;
/*
Will persist throughout loading different file content.
Should hold a bit more than the largest source file's content,
As an array of lines.
*/
zpl_arena MemPerist;
/*
Temporary memory held while procesisng files to get their content.
zpl_files are stored here
*/
// zpl_arena MemTransient;
}
using namespace StaticData;
@ -48,7 +37,7 @@ namespace IO
if ( error != ZPL_FILE_ERROR_NONE )
{
fatal("Could not open source file: %s", *path );
fatal("IO::Prepare - Could not open source file: %s", *path );
}
const sw fsize = zpl_file_size( & src );
@ -60,18 +49,16 @@ namespace IO
zpl_file_close( & src );
}
while ( --left );
while ( left--, left > 1 );
uw persist_size = ZPL_ARRAY_GROW_FORMULA( Largest_Src_Size );
zpl_arena_init_from_allocator( & MemPerist, zpl_heap(), persist_size );
// zpl_arena_init_from_allocator( & MemTransient, zpl_heap(), Largest_Src_Size );
}
void cleanup()
{
zpl_arena_free( & MemPerist );
// zpl_arena_free( & MemTransient );
}
Array_Line get_specification()
@ -102,25 +89,24 @@ namespace IO
return lines;
}
Array_Line get_next_source()
char* get_next_source()
{
// zpl_memset( MemTransient.physical_start, 0, MemTransient.total_allocated);
// MemTransient.total_allocated = 0;
// MemTransient.temp_count = 0;
zpl_memset( MemPerist.physical_start, 0, MemPerist.total_allocated);
MemPerist.total_allocated = 0;
MemPerist.temp_count = 0;
Current++;
zpl_file file {};
zpl_file_error error = zpl_file_open( & file, Specification);
zpl_file_error error = zpl_file_open( & file, Sources[Current]);
if ( error != ZPL_FILE_ERROR_NONE )
{
fatal("Could not open the source file: %s", Sources[Current]);
fatal("IO::get_next_source - Could not open the source file: %s", Sources[Current]);
}
Current_Size = scast( sw, zpl_file_size( & file ) );
auto size = zpl_file_size( & file );
Current_Size = scast( sw, size );
if ( Current_Size <= 0 )
return nullptr;
@ -133,8 +119,7 @@ namespace IO
Current_Content[Current_Size] = 0;
Current_Size++;
Array_Line lines = zpl_str_split_lines( zpl_arena_allocator( & MemPerist), Current_Content, ' ' );
return lines;
return Current_Content;
}
void write( zpl_string refacotred )

View File

@ -1,6 +1,6 @@
#pragma once
#include "bloat.hpp"
#include "Bloat.hpp"
namespace IO
@ -18,7 +18,7 @@ namespace IO
Array_Line get_specification();
// Provides the content of the next source, broken up as a series of lines.
Array_Line get_next_source();
char* get_next_source();
// Writes the refactored content ot the current corresponding destination.
void write( zpl_string refactored );

View File

@ -38,4 +38,4 @@ The `Path_Size_Largest` and `Token_Max_Length` are compile-time constraints that
`Array_Reserve_Num` is used to dictate the assumed amount of tokens will be held in total for any of spec's arrays holding ignores and refactor entries. If any of the array's exceed 4 KB they will grow triggering a resize which will bog down the speed of the refactor. Adjust if you think you can increase or lower for use case.
Initial Global arena size is a finicy thing, its most likely going to be custom allocator at one point so that it can handle growing properly, right now its just grows if the amount of memory file paths will need for sources is greater than 1 MB.
Initial Global arena size is a finicy thing, its most likely going to be custom allocator at one point so that it can handle growing properly, right now its just grows if the amount of memory file paths will need for sources is greater than 1 MB.

View File

@ -96,8 +96,6 @@ namespace Spec
left--;
lines++;
char token[ Token_Max_Length ];
do
{
char* line = * lines;

View File

@ -5,7 +5,7 @@
#pragma once
#ifdef BLOAT_IMPL
# define ZPL_IMPLEMEntATION
# define ZPL_IMPLEMENTATION
#endif
#if __clang__
@ -70,6 +70,7 @@ do \
while(0) \
using b32 = zpl_b32;
using s8 = zpl_i8;
using s32 = zpl_i32;
using s64 = zpl_i64;
@ -97,6 +98,9 @@ namespace Memory
void cleanup();
}
// Had to be made to support multiple sub-arguments per "opt" argument.
b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv);
inline
sw log_fmt(char const *fmt, ...)
{

View File

@ -1,5 +1,4 @@
#define BLOAT_IMPL
#include "bloat.hpp"
#include "Bloat.cpp"
#include "IO.cpp"
#include "Spec.cpp"
@ -16,7 +15,7 @@ void parse_options( int num, char** arguments )
zpl_opts_add( & opts, "dst" , "dst" , "File/s post refactor" , ZPL_OPTS_STRING);
zpl_opts_add( & opts, "spec", "spec", "Specification for refactoring", ZPL_OPTS_STRING);
if (zpl_opts_compile( & opts, num, arguments))
if (opts_custom_compile( & opts, num, arguments))
{
sw num = 0;
@ -36,7 +35,12 @@ void parse_options( int num, char** arguments )
else
{
num = 1;
zpl_array_init_reserve( IO::Sources, g_allocator, 1 );
zpl_array_init_reserve( IO::Destinations, g_allocator, 1 );
}
zpl_printf("NUM IS: %d", num);
if ( zpl_opts_has_arg( & opts, "src" ) )
{
@ -44,7 +48,8 @@ void parse_options( int num, char** arguments )
if ( num == 1 )
{
IO::Sources[0] = zpl_string_make_length( g_allocator, opt, zpl_string_length( opt) );
zpl_string path = zpl_string_make_length( g_allocator, opt, zpl_string_length( opt ));
zpl_array_append( IO::Sources, path );
}
else
{
@ -60,9 +65,10 @@ void parse_options( int num, char** arguments )
{
path[length] = *opt;
}
while ( length++, opt++, *opt != ' ' );
while ( length++, opt++, *opt != ' ' && *opt != '\0' );
IO::Sources[num - left] = zpl_string_make_length( g_allocator, path, length );
zpl_string path_string = zpl_string_make_length( g_allocator, path, length );
zpl_array_append( IO::Sources, path_string );
opt++;
}
@ -71,7 +77,7 @@ void parse_options( int num, char** arguments )
}
else
{
fatal( "-source not provided\n" );
fatal( "-src not provided\n" );
}
if ( zpl_opts_has_arg( & opts, "dst" ) )
@ -80,7 +86,8 @@ void parse_options( int num, char** arguments )
if ( num == 1 )
{
IO::Destinations[0] = zpl_string_make_length( g_allocator, opt, zpl_string_length( opt) );
zpl_string path = zpl_string_make_length( g_allocator, opt, zpl_string_length( opt) );
zpl_array_append( IO::Destinations, path );
}
else
{
@ -96,15 +103,30 @@ void parse_options( int num, char** arguments )
{
path[length] = *opt;
}
while ( length++, opt++, *opt != ' ' );
while ( length++, opt++, *opt != ' ' && *opt != '\0' );
IO::Destinations[num - left] = zpl_string_make_length( g_allocator, path, length );
zpl_string path_string = zpl_string_make_length( g_allocator, path, length );
zpl_array_append( IO::Destinations, path_string );
opt++;
}
while ( --left );
if ( zpl_array_count(IO::Destinations) != zpl_array_count( IO::Sources ) )
{
fatal("-dst count must match -src count");
}
}
}
else
{
uw left = num;
do
{
zpl_array_append( IO::Destinations, IO::Sources[num - left] );
}
while ( --left );
}
if ( zpl_opts_has_arg( & opts, "spec" ) )
{
@ -113,9 +135,18 @@ void parse_options( int num, char** arguments )
IO::Specification = zpl_string_make( g_allocator, "" );
IO::Specification = zpl_string_append( IO::Specification, opt );
}
else
{
fatal( "-spec not provided\n" );
}
}
else
{
zpl_printf("\nArguments: ");
for ( int index = 0; index < num; index++)
{
zpl_printf("\nArg[%d]: %s", index, arguments[index]);
}
fatal( "Failed to parse arguments\n" );
}
@ -127,7 +158,7 @@ zpl_arena Refactor_Buffer;
void refactor()
{
ct char const* include_sig = "#include \"";
ct static char const* include_sig = "#include \"";
struct Token
{
@ -157,61 +188,62 @@ void refactor()
}
// Prepare data and trackers.
Array_Line src = IO::get_next_source();
Array_Line lines = src;
char const* src = IO::get_next_source();
if ( src == nullptr )
return;
const sw num_lines = zpl_array_count( lines);
log_fmt("\n\nRefactoring: %s", IO::Sources[IO::Current]);
sw buffer_size = IO::Current_Size;
sw left = num_lines;
Line line = *lines;
uw pos = 0;
sw left = buffer_size;
uw col = 0;
uw line = 0;
#define pos (IO::Current_Size - left)
#define move_forward( Amount_ ) \
left -= Amount_; \
col += Amount_; \
src += Amount_ \
do
{
Continue_Line:
// Includes to ignore
{
Spec::Entry* ignore = Spec::Ignore_Words;
sw ignores_left = zpl_array_count( Spec::Ignore_Words);
Spec::Entry* ignore = Spec::Ignore_Includes;
sw ignores_left = zpl_array_count( Spec::Ignore_Includes);
do
for ( ; ignores_left; ignores_left--, ignore++ )
{
if ( include_sig[0] != line[0] )
if ( include_sig[0] != src[0] )
continue;
u32 sig_length = zpl_string_length( ignore->Sig );
current = zpl_string_set( current, include_sig );
current = zpl_string_append_length( current, line, sig_length );
current = zpl_string_append_length( current, "\"", 2 );
current = zpl_string_append_length( current, src, sig_length );
current = zpl_string_append_length( current, "\"", 1 );
// Formats current into: #include "<ignore->Sig>"
if ( zpl_string_are_equal( ignore->Sig, current ) )
{
log_fmt("\nIgnored %-81s line %d", current, num_lines - left );
log_fmt("\nIgnored %-81s line %d, col %d", current, line, col );
const sw length = zpl_string_length( current );
line += length;
pos += length;
move_forward( length );
// Force end of line.
while ( line != '\0' )
while ( src[0] != '\n' )
{
line++;
pos++;
move_forward( 1 );
}
goto Skip;
}
}
while ( ignore++, --ignores_left );
}
// Word Ignores
@ -219,20 +251,20 @@ void refactor()
Spec::Entry* ignore = Spec::Ignore_Words;
sw ignores_left = zpl_array_count( Spec::Ignore_Words);
do
for ( ; ignores_left; ignores_left--, ignore++ )
{
if ( ignore->Sig[0] != line[0] )
if ( ignore->Sig[0] != src[0] )
continue;
zpl_string_clear( current );
u32 sig_length = zpl_string_length( ignore->Sig );
current = zpl_string_append_length( current, line, sig_length );
current = zpl_string_append_length( current, src, sig_length );
if ( zpl_string_are_equal( ignore->Sig, current ) )
{
char before = line[-1];
char after = line[sig_length];
char before = src[-1];
char after = src[sig_length];
if ( zpl_char_is_alphanumeric( before ) || before == '_'
|| zpl_char_is_alphanumeric( after ) || after == '_' )
@ -240,14 +272,12 @@ void refactor()
continue;
}
log_fmt("\nIgnored %-81s line %d", current, num_lines - left );
log_fmt("\nIgnored %-81s line %d, col %d", current, line, col );
line += sig_length;
pos += sig_length;
move_forward( sig_length );
goto Skip;
}
}
while ( ignore++, --ignores_left );
}
// Namespace Ignores
@ -255,20 +285,23 @@ void refactor()
Spec::Entry* ignore = Spec::Ignore_Namespaces;
sw ignores_left = zpl_array_count( Spec::Ignore_Namespaces);
do
for ( ; ignores_left; ignores_left--, ignore++ )
{
if ( ignore->Sig[0] != line[0] )
if ( ignore->Sig[0] != src[0] )
{
ignore++;
continue;
}
zpl_string_clear( current );
u32 sig_length = zpl_string_length( ignore->Sig );
current = zpl_string_append_length( current, line, sig_length );
current = zpl_string_append_length( current, src, sig_length );
if ( zpl_string_are_equal( ignore->Sig, current ) )
{
u32 length = sig_length;
char* ns_content = line + sig_length;
u32 length = sig_length;
char const* ns_content = src + sig_length;
while ( zpl_char_is_alphanumeric( ns_content[0] ) || ns_content[0] == '_' )
{
@ -278,16 +311,14 @@ void refactor()
#if Build_Debug
zpl_string_clear( preview );
preview = zpl_string_append_length( preview, line, length );
log_fmt("\nIgnored %-40s %-40s line %d", preview, ignore->Sig, - left);
preview = zpl_string_append_length( preview, src, length );
log_fmt("\nIgnored %-40s %-40s line %d, column %d", preview, ignore->Sig, line, col );
#endif
line += length;
pos += length;
move_forward( length );
goto Skip;
}
}
while ( ignore++, --ignores_left );
}
// Includes to match
@ -296,16 +327,16 @@ void refactor()
sw includes_left = zpl_array_count ( Spec::Includes);
do
for ( ; includes_left; includes_left--, include++ )
{
if ( include_sig[0] != line[0] )
if ( include_sig[0] != src[0] )
continue;
u32 sig_length = zpl_string_length( include->Sig );
current = zpl_string_set( current, include_sig );
current = zpl_string_append_length( current, line, sig_length );
current = zpl_string_append_length( current, "\"", 2 );
current = zpl_string_append_length( current, src, sig_length );
current = zpl_string_append_length( current, "\"", 1 );
// Formats current into: #include "<ignore->Sig>"
if ( zpl_string_are_equal( include->Sig, current ) )
@ -326,22 +357,19 @@ void refactor()
zpl_array_append( tokens, entry );
log_fmt("\nFound %-81s line %d", current, num_lines - left);
log_fmt("\nFound %-81s line %d, column %d", current, line, col );
line += length;
pos += length;
move_forward( length );
// Force end of line.
while ( line != '\0' )
while ( src[0] != '\0' )
{
line++;
pos++;
move_forward( 1 );
}
goto Skip;
}
}
while ( include++, --includes_left );
}
// Words to match
@ -349,20 +377,20 @@ void refactor()
Spec::Entry* word = Spec::Words;
sw words_left = zpl_array_count ( Spec::Words);
do
for ( ; words_left; words_left--, word++ )
{
if ( word->Sig[0] != line[0] )
if ( word->Sig[0] != src[0] )
continue;
zpl_string_clear( current );
sw sig_length = zpl_string_length( word->Sig);
current = zpl_string_append_length( current, line, sig_length );
current = zpl_string_append_length( current, src, sig_length );
if ( zpl_string_are_equal( word->Sig, current ) )
{
char before = line[-1];
char after = line[sig_length];
char before = src[-1];
char after = src[sig_length];
if ( zpl_char_is_alphanumeric( before ) || before == '_'
|| zpl_char_is_alphanumeric( after ) || after == '_' )
@ -384,14 +412,12 @@ void refactor()
zpl_array_append( tokens, entry );
log_fmt("\nFound %-81s line %d", current, num_lines - left);
log_fmt("\nFound %-81s line %d, column %d", current, line, col );
line += sig_length;
pos += sig_length;
move_forward( sig_length );
goto Skip;
}
}
while ( word++, --words_left );
}
// Namespaces to match
@ -400,20 +426,20 @@ void refactor()
sw nspaces_left = zpl_array_count( Spec::Namespaces);
do
for ( ; nspaces_left; nspaces_left--, nspace++ )
{
if ( nspace->Sig[0] != line[0] )
if ( nspace->Sig[0] != src[0] )
continue;
zpl_string_clear( current );
u32 sig_length = zpl_string_length( nspace->Sig );
current = zpl_string_append_length( current, line, sig_length );
current = zpl_string_append_length( current, src, sig_length );
if ( zpl_string_are_equal( nspace->Sig, current ) )
{
u32 length = sig_length;
char* ns_content = line + sig_length;
u32 length = sig_length;
char const* ns_content = src + sig_length;
while ( zpl_char_is_alphanumeric( ns_content[0] ) || ns_content[0] == '_' )
{
@ -439,26 +465,29 @@ void refactor()
#if Build_Debug
zpl_string_clear( preview );
preview = zpl_string_append_length( preview, line, length);
log_fmt("\nFound %-40s %-40s line %d", preview, nspace->Sig, num_lines - left);
preview = zpl_string_append_length( preview, src, length);
log_fmt("\nFound %-40s %-40s line %d, column %d", preview, nspace->Sig, line, col );
#endif
line += length;
pos += length;
move_forward( length );
goto Skip;
}
}
while ( nspace++, --nspaces_left );
}
Skip:
if ( line != '\0' )
goto Continue_Line;
if ( src[0] == '\n' )
{
line++;
col = 0;
}
src++;
}
while ( lines++, line = *lines, left );
while ( --left );
// Prep data for building the content
left = IO::Current_Size;
left = zpl_array_count( tokens);
char* content = IO::Current_Content;
@ -475,17 +504,19 @@ void refactor()
sw sig_length = zpl_string_length( entry->Sig );
// Append between tokens
refactored = zpl_string_append_length( refactored, line, segment_length );
line += segment_length + sig_length;
refactored = zpl_string_append_length( refactored, content, segment_length );
content += segment_length + sig_length;
segment_length = entry->End - entry->Start - sig_length;
// Append token
if ( entry->Sub )
refactored = zpl_string_append( refactored, entry->Sub );
{
refactored = zpl_string_append( refactored, entry->Sub );
}
refactored = zpl_string_append_length( refactored, line, segment_length );
line += segment_length;
refactored = zpl_string_append_length( refactored, content, segment_length );
content += segment_length;
previous_end = entry->End;
entry++;
@ -496,11 +527,13 @@ void refactor()
if ( entry->End < IO::Current_Size )
{
refactored = zpl_string_append_length( refactored, line, IO::Current_Size - entry->End );
refactored = zpl_string_append_length( refactored, content, IO::Current_Size - 1 - entry->End );
}
}
IO::write( refactored );
zpl_string_clear( refactored );
}
int main( int num, char** arguments )
@ -520,6 +553,8 @@ int main( int num, char** arguments )
do
{
refactor();
zpl_printf("\nRefactored: %s", IO::Sources[IO::Current]);
}
while ( --left );