Added is_body to AST and Code types

This commit is contained in:
Edward R. Gonzalez 2024-12-01 12:48:58 -05:00
parent 31691b1466
commit 8ef982003a
9 changed files with 109 additions and 75 deletions

View File

@ -90,6 +90,7 @@ CodeBody parse_file( const char* path )
{ {
FileContents file = file_read_contents( GlobalAllocator, true, path ); FileContents file = file_read_contents( GlobalAllocator, true, path );
CodeBody code = parse_global_body( { file.size, (char const*)file.data } ); CodeBody code = parse_global_body( { file.size, (char const*)file.data } );
log_fmt("\nParsed: %s\n", path);
return code; return code;
} }
@ -126,7 +127,7 @@ int gen_main()
header.print( debug ); header.print( debug );
CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" );
CodeBody memory = def_body(ECode::Struct_Body); CodeBody memory = def_body(ECode::Global_Body);
for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry ) for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry )
{ {
switch (entry->Type) switch (entry->Type)
@ -145,12 +146,32 @@ int gen_main()
CodeFn fn = entry.cast<CodeFn>(); CodeFn fn = entry.cast<CodeFn>();
s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr ); s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr );
if (constexpr_found > -1) { if (constexpr_found > -1) {
log_fmt("Found constexpr proc\n"); log_fmt("Found constexpr: %S\n", entry->to_string());
fn->Specs.append(ESpecifier::Inline); fn->Specs.append(ESpecifier::Inline);
} }
memory.append(entry); memory.append(entry);
} }
break; break;
case ECode::Template:
{
CodeTemplate tmpl = entry.cast<CodeTemplate>();
if ( tmpl->Declaration->Name.contains(txt("swap")))
{
CodeBody macro_swap = parse_global_body( txt(R"(
#define swap( a, b ) \
do \
{ \
typeof( a ) temp = ( a ); \
( a ) = ( b ); \
( b ) = temp; \
} while ( 0 )
)"
));
memory.append(macro_swap);
log_fmt( "\nmacro swap: %S\n", macro_swap.to_string() );
}
}
break;
case ECode::Class: case ECode::Class:
case ECode::Struct: case ECode::Struct:
{ {
@ -175,17 +196,26 @@ int gen_main()
break; break;
case ECode::Preprocess_If: case ECode::Preprocess_If:
{ {
ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory );
if (found) break;
memory.append(entry);
} }
break; break;
case ECode::Preprocess_IfDef: case ECode::Preprocess_IfDef:
{ {
ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory );
if (found) break;
memory.append(entry);
} }
break; break;
case ECode::Preprocess_Pragma: case ECode::Preprocess_Pragma:
{ {
swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory); b32 found = swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory);
if (found) break;
memory.append(entry);
} }
break; break;
default: { default: {
@ -194,26 +224,28 @@ int gen_main()
break; break;
} }
} }
header.print( dump_to_scratch_and_retireve(memory) );
header.print( memory );
Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" ); Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" );
header.print( string_ops ); header.print( string_ops );
CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" ); CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" );
CodeBody printing = def_body(ECode::Struct_Body); CodeBody printing = def_body(ECode::Global_Body);
for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry ) for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry )
{ {
switch (entry->Type) switch (entry->Type)
{ {
case ECode::Preprocess_IfDef: case ECode::Preprocess_IfDef:
{ {
ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed );
} if (found) break;
}
if (entry->Type == ECode::Variable && printing.append(entry);
contains(entry->Name, txt("Msg_Invalid_Value"))) }
break;
case ECode::Variable:
{
if (contains(entry->Name, txt("Msg_Invalid_Value")))
{ {
CodeDefine define = def_define(entry->Name, entry->Value->Content); CodeDefine define = def_define(entry->Name, entry->Value->Content);
printing.append(define); printing.append(define);
@ -221,42 +253,55 @@ int gen_main()
} }
printing.append(entry); printing.append(entry);
} }
header.print(printing);
CodeBody parsed_containers = parse_file( project_dir "dependencies/containers.hpp" );
CodeBody containers = def_body(ECode::Struct_Body);
for ( Code entry = parsed_containers.begin(); entry != parsed_containers.end(); ++ entry )
{
switch ( entry->Type )
{
case ECode::Preprocess_Pragma:
{
bool found = false;
found = swap_pragma_region_implementation( txt("Array"), gen_array_base, entry, containers);
if (found) {
break; break;
} default:
printing.append(entry);
found = swap_pragma_region_implementation( txt("HashTable"), gen_hashtable_base, entry, containers);
if (found) {
break;
}
containers.append(entry);
}
break; break;
} }
} }
header.print(dump_to_scratch_and_retireve(printing));
header.print(containers); CodeBody containers = def_body(ECode::Global_Body);
{
containers.append( def_pragma(code(region Containers)));
containers.append( gen_array_base() );
containers.append( gen_hashtable_base() );
containers.append( def_pragma(code(endregion Containers)));
}
header.print(fmt_newline);
header.print(dump_to_scratch_and_retireve(containers));
Code hashing = scan_file( project_dir "dependencies/hashing.hpp" );
header.print( hashing );
CodeBody parsed_strings = parse_file( project_dir "dependencies/strings.hpp" );
CodeBody strings = def_body(ECode::Global_Body);
for ( Code entry = parsed_strings.begin(); entry != parsed_strings.end(); ++ entry )
{
switch (entry->Type)
{
case ECode::Preprocess_IfDef:
{
ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings );
}
break;
default:
strings.append(entry);
break;
}
}
header.print(dump_to_scratch_and_retireve(strings));
header.print_fmt( roll_own_dependencies_guard_end );
} }
header.print( pop_ignores ); header.print( pop_ignores );
header.write(); header.write();
format_file( "gen/gen.h" ); // format_file( "gen/gen.h" );
gen::deinit(); gen::deinit();
return 0; return 0;

View File

@ -19,7 +19,7 @@ CodeBody gen_array_base()
// Code grow_formula = untyped_str( txt( "#define gen_array_grow_formula( value ) ( 2 * value + 8 )\n" )); // Code grow_formula = untyped_str( txt( "#define gen_array_grow_formula( value ) ( 2 * value + 8 )\n" ));
Code get_header = untyped_str( txt( "#define array_get_header( Type, self ) ( (ArrayHeader*)( self ) - 1)\n" )); Code get_header = untyped_str( txt( "#define array_get_header( Type, self ) ( (ArrayHeader*)( self ) - 1)\n" ));
return def_global_body( args( td_header, header, get_header ) ); return def_global_body( args( fmt_newline, td_header, header, get_header, fmt_newline ) );
}; };
CodeBody gen_array( StrC type, StrC array_name ) CodeBody gen_array( StrC type, StrC array_name )

View File

@ -7,9 +7,13 @@ using SwapContentProc = CodeBody(void);
b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body ) b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body )
{ {
b32 found = false;
CodePreprocessCond cond = entry_iter.cast<CodePreprocessCond>(); CodePreprocessCond cond = entry_iter.cast<CodePreprocessCond>();
if ( cond->Content.contains(cond_sig) ) if ( cond->Content.contains(cond_sig) )
{ {
log_fmt("Preprocess cond found: %S\n", cond->Content);
found = true;
s32 depth = 1; s32 depth = 1;
++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch ++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch
(entry_iter->Type) { (entry_iter->Type) {
@ -34,7 +38,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod
} }
} }
return entry_iter != body.end(); return found;
} }
bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body )

View File

@ -4406,6 +4406,7 @@ CodeTemplate parse_template()
result->Params = params; result->Params = params;
result->Declaration = definition; result->Declaration = definition;
result->ModuleFlags = mflags; result->ModuleFlags = mflags;
// result->Name = definition->Name;
Context.pop(); Context.pop();
return result; return result;

View File

@ -14,20 +14,17 @@ template<class TType>
using TRemoveConst = typename RemoveConst<TType>::Type; using TRemoveConst = typename RemoveConst<TType>::Type;
#pragma region Array #pragma region Array
#if ! GEN_COMPILER_C
#define Array(Type) Array<Type> #define Array(Type) Array<Type>
// #define array_init(Type, ...) array_init <Type>(__VA_ARGS__) // #define array_init(Type, ...) array_init <Type>(__VA_ARGS__)
// #define array_init_reserve(Type, ...) array_init_reserve<Type>(__VA_ARGS__) // #define array_init_reserve(Type, ...) array_init_reserve<Type>(__VA_ARGS__)
#endif
struct ArrayHeader; struct ArrayHeader;
#if GEN_SUPPORT_CPP_MEMBER_FEATURES #if GEN_SUPPORT_CPP_MEMBER_FEATURES
template<class Type> struct Array; template<class Type> struct Array;
#else #else
template<class Type> template<class Type> using Array = Type*;
using Array = Type*;
#endif #endif
usize array_grow_formula(ssize value); usize array_grow_formula(ssize value);

View File

@ -205,7 +205,7 @@
# define foreach(Type, entry_id, iterable) for ( Type entry_id : iterable ) # define foreach(Type, entry_id, iterable) for ( Type entry_id : iterable )
#endif #endif
#if GENC_COMPILERC #if GEN_COMPILER_C
# if __STDC_VERSION__ >= 202311L # if __STDC_VERSION__ >= 202311L
# define enum_underlying(type) : type # define enum_underlying(type) : type
# else # else

View File

@ -14,23 +14,13 @@
#define GEN__HIGHS ( GEN__ONES * ( GEN_U8_MAX / 2 + 1 ) ) #define GEN__HIGHS ( GEN__ONES * ( GEN_U8_MAX / 2 + 1 ) )
#define GEN__HAS_ZERO( x ) ( ( ( x ) - GEN__ONES ) & ~( x ) & GEN__HIGHS ) #define GEN__HAS_ZERO( x ) ( ( ( x ) - GEN__ONES ) & ~( x ) & GEN__HIGHS )
#if ! GEN_COMPILER_C template< class Type >
template< class Type > void swap( Type& a, Type& b )
void swap( Type& a, Type& b ) {
{
Type tmp = a; Type tmp = a;
a = b; a = b;
b = tmp; b = tmp;
} }
#else
#define swap( a, b ) \
do { \
typeof(a) \
temp = (a); \
(a) = (b); \
(b) = temp; \
} while(0)
#endif
//! Checks if value is power of 2. //! Checks if value is power of 2.
b32 is_power_of_two( ssize x ); b32 is_power_of_two( ssize x );

View File

@ -106,7 +106,7 @@
# define GEN_GCC_VERSION_CHECK(major,minor,patch) (0) # define GEN_GCC_VERSION_CHECK(major,minor,patch) (0)
#endif #endif
#ifndef GEN_COMPIELR_C #ifndef GEN_COMPILER_C
# if defined(__STDC_VERSION__) # if defined(__STDC_VERSION__)
# define GEN_COMPILER_C 1 # define GEN_COMPILER_C 1
# else # else

View File

@ -11,8 +11,10 @@ struct StrC
ssize Len; ssize Len;
char const* Ptr; char const* Ptr;
#if ! GEN_COMPILER_C
operator char const* () const { return Ptr; } operator char const* () const { return Ptr; }
char const& operator[]( ssize index ) const { return Ptr[index]; } char const& operator[]( ssize index ) const { return Ptr[index]; }
#endif
}; };
#define cast_to_strc( str ) * rcast( StrC*, (str) - sizeof(ssize) ) #define cast_to_strc( str ) * rcast( StrC*, (str) - sizeof(ssize) )
@ -29,12 +31,7 @@ StrC to_str( char const* str ) {
// I kept it for simplicty of porting but its not necessary to keep it that way. // I kept it for simplicty of porting but its not necessary to keep it that way.
#pragma region String #pragma region String
struct StringHeader; struct StringHeader;
#if GEN_COMPILER_C
typedef char* String;
#else
struct String; struct String;
#endif
String string_make(AllocatorInfo allocator, char const* str); String string_make(AllocatorInfo allocator, char const* str);
String string_make(AllocatorInfo allocator, StrC str); String string_make(AllocatorInfo allocator, StrC str);