6 Commits
bloat ... main

Author SHA1 Message Date
Ed_
50033cc819 Changed ignore precedence to fix some bugs. 2023-11-18 20:18:16 -05:00
Ed_
22f74f3dc6 Namespace fix (making sure a namespace is a prefix only) 2023-11-17 19:24:27 -05:00
Ed_
6395d022f1 Merge branch 'main' of https://github.com/Ed94/refactor 2023-07-20 01:09:18 -04:00
Ed_
4d5cbd3e8d Fix for namespace swapping 2023-07-20 01:09:00 -04:00
Ed_
b21e1bf603 Merge pull request #5 from Ed94/Ed94-patch-1
Update LICENSE
2023-07-11 01:19:35 -04:00
Ed_
12779449a4 Update LICENSE 2023-07-11 01:19:11 -04:00
11 changed files with 176 additions and 171 deletions

6
.vscode/launch.json vendored
View File

@ -47,10 +47,10 @@
"request": "launch",
"program": "${workspaceFolder}/build/refactor.exe",
"args": [
"-source=./refactor.cpp",
"-destination=./Test/refactor.cpp",
"-src=./project/refactor.cpp",
"-dst=./Test/refactor.cpp",
"-specification=./Test/zpl.refactor"
"-spec=./Test/zpl.refactor"
],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",

View File

@ -1,6 +1,6 @@
BSD 3-Clause License
Copyright (c) 2023, Ed_
Copyright (c) 2023, Edward R. Gonzalez
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

View File

@ -79,8 +79,6 @@ With the refactors applied a meson configuraiton is setup (`meson.build` in test
* Can have a heavy memory cost, so most likely do not want on by default.
* Make comments ignored by default, and just have ability to specify custom comments.
* Would need a directive to add refactors to comments.
* Directive to add cpp namespaces on specific lines of a file, or near specific signatures.
* This can honestly be done also with placing words on specific lines..
* Provide a GUI build.
* Provide as a single-header library.
* Could add a test case where this library is refactored into pure C (most likely c99 or c11).

View File

@ -34,8 +34,8 @@ namespace zpl_random_, rng_
namespace zpl_pr, process_
namespace zpl__, __
namespace ZPL_ADT_, EADT
namespace ZPL_ALLOCATION_, EAllocation
namespace ZPL_CSV_ERROR, ECSV_Error
namespace ZPL_ALLOCATION_, EAllocation_
namespace ZPL_CSV_ERROR, ECSV_Error_
namespace ZPL_FILE_MODE_, EFileMode_
namespace ZPL_FILE_ERROR_, EFileError_
namespace ZPL_SEEK_WHENCE_, ESeekWhence_

View File

@ -9,7 +9,7 @@ namespace Global
namespace Memory
{
zpl_arena Global_Arena {};
void setup()
{
zpl_arena_init_from_allocator( & Global_Arena, zpl_heap(), Initial_Reserve );
@ -19,16 +19,16 @@ namespace Memory
zpl_assert_crash( "Failed to reserve memory for Tests:: Global_Arena" );
}
}
void resize( uw new_size )
{
void* new_memory = zpl_resize( zpl_heap(), Global_Arena.physical_start, Global_Arena.total_size, new_size );
void* new_memory = zpl_resize( zpl_heap(), Global_Arena.physical_start, Global_Arena.total_size, new_size );
if ( new_memory == nullptr )
{
fatal("Failed to resize global arena!");
}
Global_Arena.physical_start = new_memory;
Global_Arena.total_size = new_size;
}
@ -36,7 +36,7 @@ namespace Memory
void cleanup()
{
zpl_arena_free( & Global_Arena);
}
}
}
@ -44,7 +44,7 @@ bool opts_custom_add(zpl_opts* opts, zpl_opts_entry *t, char* b)
{
if (t->type != ZPL_OPTS_STRING)
{
return false;
return false;
}
t->text = zpl_string_append_length(t->text, " ", 1);
@ -53,23 +53,23 @@ bool opts_custom_add(zpl_opts* opts, zpl_opts_entry *t, char* b)
return true;
}
b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
{
zpl_b32 had_errors = false;
for (int i = 1; i < argc; ++i)
for (int i = 1; i < argc; ++i)
{
char* arg = argv[i];
if (*arg)
if (*arg)
{
arg = (char*)zpl_str_trim(arg, false);
if (*arg == '-')
if (*arg == '-')
{
zpl_opts_entry* entry = 0;
zpl_b32 checkln = false;
if ( *(arg + 1) == '-')
if ( *(arg + 1) == '-')
{
checkln = true;
++arg;
@ -83,15 +83,15 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
entry = zpl__opts_find(opts, b, (e - b), checkln);
if (entry)
if (entry)
{
char *ob = b;
b = e;
/**/
if (*e == '=')
/**/
if (*e == '=')
{
if (entry->type == ZPL_OPTS_FLAG)
if (entry->type == ZPL_OPTS_FLAG)
{
*e = '\0';
zpl__opts_push_error(opts, ob, ZPL_OPTS_ERR_EXTRA_VALUE);
@ -101,14 +101,14 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
}
b = e = e + 1;
}
else if (*e == '\0')
}
else if (*e == '\0')
{
char *sp = argv[i+1];
if (sp && *sp != '-' && (zpl_array_count(opts->positioned) < 1 || entry->type != ZPL_OPTS_FLAG))
if (sp && *sp != '-' && (zpl_array_count(opts->positioned) < 1 || entry->type != ZPL_OPTS_FLAG))
{
if (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;
@ -119,10 +119,10 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
arg = sp;
b = e = sp;
++i;
}
else
}
else
{
if (entry->type != ZPL_OPTS_FLAG)
if (entry->type != ZPL_OPTS_FLAG)
{
zpl__opts_push_error(opts, ob, ZPL_OPTS_ERR_MISSING_VALUE);
had_errors = true;
@ -145,20 +145,20 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
opts_custom_add(opts, entry, b );
}
}
}
else
}
else
{
zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_OPTION);
had_errors = true;
}
}
else if (zpl_array_count(opts->positioned))
}
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
}
else
{
zpl__opts_push_error(opts, arg, ZPL_OPTS_ERR_VALUE);
had_errors = true;

View File

@ -2,7 +2,7 @@
BLOAT.
*/
#pragma once
#pragma once
#ifdef BLOAT_IMPL
# define ZPL_IMPLEMENTATION
@ -10,7 +10,7 @@
#pragma region ZPL INCLUDE
#if __clang__
# pragma clang diagnostic push
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wmissing-braces"
# pragma clang diagnostic ignored "-Wbraced-scalar-init"
#endif
@ -111,25 +111,25 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv);
inline
sw log_fmt(char const *fmt, ...)
sw log_fmt(char const *fmt, ...)
{
if ( Global::ShouldShowDebug == false )
return 0;
sw res;
va_list va;
va_start(va, fmt);
res = zpl_printf_va(fmt, va);
va_end(va);
return res;
}
inline
void fatal(char const *fmt, ...)
void fatal(char const *fmt, ...)
{
zpl_local_persist zpl_thread_local
zpl_local_persist zpl_thread_local
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
va_list va;

View File

@ -4,13 +4,13 @@
namespace IO
{
using array_string = zpl_array( zpl_string );
namespace StaticData
{
array_string Sources = nullptr;
array_string Destinations = nullptr;
zpl_string Specification = nullptr;
// Current source and destination index.
// Used to keep track of which file get_next_source or write refer to.
sw Current = -1;
@ -22,12 +22,12 @@ namespace IO
zpl_arena MemSrc;
}
using namespace StaticData;
void prepare()
{
const sw num_srcs = zpl_array_count( Sources );
// Determine the largest content size.
sw left = num_srcs;
zpl_string* path = Sources;
@ -35,14 +35,14 @@ namespace IO
{
zpl_file src = {};
zpl_file_error error = zpl_file_open( & src, *path );
if ( error != ZPL_FILE_ERROR_NONE )
{
fatal("IO::Prepare - Could not open source file: %s", *path );
}
const sw fsize = zpl_file_size( & src );
if ( fsize > Largest_Src_Size )
{
Largest_Src_Size = fsize;
@ -51,12 +51,12 @@ namespace IO
zpl_file_close( & src );
}
while ( path++, left--, left > 0 );
uw persist_size = Largest_Src_Size * 2 + 8;
zpl_arena_init_from_allocator( & MemSrc, zpl_heap(), persist_size );
}
void cleanup()
{
zpl_arena_free( & MemSpec );

View File

@ -10,16 +10,16 @@ namespace IO
// Preps the IO by loading all the files and checking to see what the largest size is.
// The file with the largest size is used to determine the size of the persistent memory.
void prepare();
// Frees the persistent and transient memory arenas.
void cleanup();
void cleanup();
// Provides the content of the specification.
Array_Line get_specification();
// Provides the content of the next source, broken up as a series of lines.
char* get_next_source();
// Writes the refactored content ot the current corresponding destination.
void write( zpl_string refactored );
}
}

View File

@ -195,7 +195,7 @@ namespace Spec
{
continue;
}
find_next_token( type, token, line, length );
// First argument is signature.
@ -211,7 +211,7 @@ namespace Spec
case Tok::Word:
if ( ignore)
zpl_array_append( Ignore_Words, entry );
else
zpl_array_append( Words, entry );
break;
@ -248,7 +248,7 @@ namespace Spec
{
case Tok::Word:
zpl_array_append( Words, entry );
break;
break;
case Tok::Namespace:
zpl_array_append( Namespaces, entry );
@ -288,7 +288,7 @@ namespace Spec
{
case Tok::Word:
zpl_array_append( Words, entry );
break;
break;
case Tok::Namespace:
zpl_array_append( Namespaces, entry );

View File

@ -4,7 +4,7 @@
namespace Spec
{
enum Tok
enum Tok
{
Not,
Comment,
@ -19,7 +19,7 @@ namespace Spec
char const* str_tok( Tok tok )
{
static
char const* tok_to_str[ Tok::Num_Tok ] =
char const* tok_to_str[ Tok::Num_Tok ] =
{
"not",
"comments",
@ -35,7 +35,7 @@ namespace Spec
char strlen_tok( Tok tok )
{
static
const u8 tok_to_len[ Tok::Num_Tok ] =
const u8 tok_to_len[ Tok::Num_Tok ] =
{
3,
8,

View File

@ -2,8 +2,6 @@
#include "IO.cpp"
#include "Spec.cpp"
void parse_options( int num, char** arguments )
{
zpl_opts opts;
@ -28,32 +26,32 @@ void parse_options( int num, char** arguments )
Global::ShouldShowDebug = true;
}
if ( zpl_opts_has_arg( & opts, "num" ) )
{
num = zpl_opts_integer( & opts, "num", -1 );
uw global_reserve = num * sizeof(zpl_string) * IO::Path_Size_Largest * 2 + 8;
if ( global_reserve > zpl_megabytes(1) )
{
Memory::resize( global_reserve + zpl_megabytes(2) );
}
zpl_array_init_reserve( IO::Sources, g_allocator, num );
zpl_array_init_reserve( IO::Destinations, g_allocator, num );
zpl_array_init_reserve( IO::Destinations, g_allocator, num );
}
else
{
num = 1;
zpl_array_init_reserve( IO::Sources, g_allocator, 1 );
zpl_array_init_reserve( IO::Destinations, g_allocator, 1 );
zpl_array_init_reserve( IO::Destinations, g_allocator, 1 );
}
if ( zpl_opts_has_arg( & opts, "src" ) )
{
zpl_string opt = zpl_opts_string( & opts, "src", "INVALID SRC ARGUMENT" );
if ( num == 1 )
{
zpl_string path = zpl_string_make_length( g_allocator, opt, zpl_string_length( opt ));
@ -66,20 +64,20 @@ void parse_options( int num, char** arguments )
uw left = num;
do
{
char* path = buffer;
sw length = 0;
char* path = buffer;
sw length = 0;
do
{
path[length] = *opt;
}
}
while ( length++, opt++, *opt != ' ' && *opt != '\0' );
zpl_string path_string = zpl_string_make_length( g_allocator, path, length );
zpl_array_append( IO::Sources, path_string );
opt++;
}
}
while ( --left );
}
}
@ -110,14 +108,14 @@ void parse_options( int num, char** arguments )
do
{
path[length] = *opt;
}
}
while ( length++, opt++, *opt != ' ' && *opt != '\0' );
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 ) )
@ -132,7 +130,7 @@ void parse_options( int num, char** arguments )
do
{
zpl_array_append( IO::Destinations, IO::Sources[num - left] );
}
}
while ( --left );
}
@ -163,7 +161,7 @@ void parse_options( int num, char** arguments )
/*
Refactor will problably have the execution and arg parsing (main and opts)
Refactor will problably have the execution and arg parsing (main and opts)
moved to a separate file.
*/
@ -172,7 +170,7 @@ zpl_arena Refactor_Buffer;
void refactor()
{
ct static char const* include_sig = "include";
struct Token
{
u32 Start;
@ -280,7 +278,7 @@ void refactor()
{
Spec::Entry* ignore = Spec::Ignore_Includes;
sw ignores_left = zpl_array_count( Spec::Ignore_Includes);
Snapshot backup = { src, left, col, line };
Snapshot backup = { src, left, col, line };
if ( '#' != src[0] )
break;
@ -336,84 +334,9 @@ void refactor()
}
restore( backup );
}
}
while (false);
// Word Ignores
{
Spec::Entry* ignore = Spec::Ignore_Words;
sw ignores_left = zpl_array_count( Spec::Ignore_Words);
for ( ; ignores_left; ignores_left--, ignore++ )
{
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, src, sig_length );
if ( zpl_string_are_equal( ignore->Sig, current ) )
{
char before = src[-1];
char after = src[sig_length];
if ( zpl_char_is_alphanumeric( before ) || before == '_'
|| zpl_char_is_alphanumeric( after ) || after == '_' )
{
continue;
}
log_fmt("\nIgnored %-81s line %d, col %d", current, line, col );
move_forward( sig_length );
goto Skip;
}
}
}
// Namespace Ignores
{
Spec::Entry* ignore = Spec::Ignore_Namespaces;
sw ignores_left = zpl_array_count( Spec::Ignore_Namespaces);
for ( ; ignores_left; ignores_left--, ignore++ )
{
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, src, sig_length );
if ( zpl_string_are_equal( ignore->Sig, current ) )
{
u32 length = sig_length;
char const* ns_content = src + sig_length;
while ( zpl_char_is_alphanumeric( ns_content[0] ) || ns_content[0] == '_' )
{
length++;
ns_content++;
}
#if Build_Debug
zpl_string_clear( preview );
preview = zpl_string_append_length( preview, src, length );
log_fmt("\nIgnored %-40s %-40s line %d, column %d", preview, ignore->Sig, line, col );
#endif
move_forward( length );
goto Skip;
}
}
}
// Includes to match
do
{
@ -487,9 +410,43 @@ void refactor()
}
restore( backup );
}
}
while (false);
// Word Ignores
{
Spec::Entry* ignore = Spec::Ignore_Words;
sw ignores_left = zpl_array_count( Spec::Ignore_Words);
for ( ; ignores_left; ignores_left--, ignore++ )
{
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, src, sig_length );
if ( zpl_string_are_equal( ignore->Sig, current ) )
{
char before = src[-1];
char after = src[sig_length];
if ( zpl_char_is_alphanumeric( before ) || before == '_'
|| zpl_char_is_alphanumeric( after ) || after == '_' )
{
continue;
}
log_fmt("\nIgnored %-81s line %d, col %d", current, line, col );
move_forward( sig_length );
goto Skip;
}
}
}
// Words to match
{
Spec::Entry* word = Spec::Words;
@ -538,6 +495,53 @@ void refactor()
}
}
// Namespace Ignores
{
Spec::Entry* ignore = Spec::Ignore_Namespaces;
sw ignores_left = zpl_array_count( Spec::Ignore_Namespaces);
for ( ; ignores_left; ignores_left--, ignore++ )
{
if ( ignore->Sig[0] != src[0] )
{
ignore++;
continue;
}
if (( zpl_char_is_alphanumeric( src[-1] ) || src[-1] == '_') )
{
ignore++;
continue;
}
zpl_string_clear( current );
u32 sig_length = zpl_string_length( ignore->Sig );
current = zpl_string_append_length( current, src, sig_length );
if ( zpl_string_are_equal( ignore->Sig, current ) )
{
u32 length = sig_length;
char const* ns_content = src + sig_length;
while ( zpl_char_is_alphanumeric( ns_content[0] ) || ns_content[0] == '_' )
{
length++;
ns_content++;
}
#if Build_Debug
zpl_string_clear( preview );
preview = zpl_string_append_length( preview, src, length );
log_fmt("\nIgnored %-40s %-40s line %d, column %d", preview, ignore->Sig, line, col );
#endif
move_forward( length );
goto Skip;
}
}
}
// Namespaces to match
{
Spec::Entry* nspace = Spec::Namespaces;
@ -549,6 +553,9 @@ void refactor()
if ( nspace->Sig[0] != src[0] )
continue;
if (( zpl_char_is_alphanumeric( src[-1] ) || src[-1] == '_') )
continue;
zpl_string_clear( current );
u32 sig_length = zpl_string_length( nspace->Sig );
@ -568,7 +575,7 @@ void refactor()
Token entry {};
entry.Start = pos;
entry.End = pos + length;
entry.End = pos + sig_length;
entry.Sig = nspace->Sig;
buffer_size += sig_length;
@ -576,7 +583,7 @@ void refactor()
if ( nspace->Sub != nullptr )
{
entry.Sub = nspace->Sub;
buffer_size += zpl_string_length( entry.Sub ) - length;
buffer_size += zpl_string_length( entry.Sub ) - sig_length;
}
zpl_array_append( tokens, entry );
@ -595,7 +602,7 @@ void refactor()
Skip:
move_forward( 1 );
}
}
while ( left );
End_Search:
@ -607,7 +614,7 @@ End_Search:
// Prep data for building the content
left = zpl_array_count( tokens);
char* content = IO::Current_Content;
char* content = IO::Current_Content;
zpl_string refactored = zpl_string_make_reserve( zpl_arena_allocator( & Refactor_Buffer ), buffer_size );
@ -643,7 +650,7 @@ End_Search:
entry--;
if ( entry->End < IO::Current_Size )
if ( entry->End < IO::Current_Size )
{
refactored = zpl_string_append_length( refactored, content, IO::Current_Size - 1 - entry->End );
}
@ -660,7 +667,7 @@ End_Search:
int main( int num, char** arguments )
{
Memory::setup();
parse_options( num, arguments);
IO::prepare();
@ -676,7 +683,7 @@ int main( int num, char** arguments )
refactor();
zpl_printf("\nRefactored: %s", IO::Sources[IO::Current]);
}
}
while ( --left );
zpl_arena_free( & Refactor_Buffer );