builder compiles in c (some issues with filtering out preprocess #if)

This commit is contained in:
Edward R. Gonzalez 2024-12-10 07:24:32 -05:00
parent 0ccffe3f80
commit a4143b537d
5 changed files with 270 additions and 189 deletions

View File

@ -1067,6 +1067,64 @@ R"(#define <interface_name>( code ) _Generic( (code), \
break;
}
CodeBody parsed_header_builder = parse_file( project_dir "auxillary/builder.hpp" );
CodeBody header_builder = def_body(CT_Global_Body);
for ( Code entry = parsed_header_builder.begin(); entry != parsed_header_builder.end(); ++ entry ) switch( entry->Type )
{
case CT_Preprocess_IfDef:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_header_builder, header_builder );
if (found) break;
header_builder.append(entry);
}
break;
case CT_Preprocess_If:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP"), entry, parsed_header_builder, header_builder );
if (found) break;
found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP"), entry, parsed_header_builder, header_builder );
if (found) break;
header_builder.append(entry);
}
break;
case CT_Struct:
{
CodeBody body = cast(CodeBody, entry->Body);
CodeBody new_body = def_body(CT_Struct_Body);
for ( Code body_entry = body.begin(); body.end(); ++ body_entry ) switch(body_entry->Type)
{
case CT_Preprocess_If:
{
b32 found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP"), body_entry, body, new_body );
if (found) break;
new_body.append(body_entry);
}
break;
default:
new_body.append(body_entry);
break;
}
if ( new_body->NumEntries > 0 ) {
entry->Body = new_body;
}
header_builder.append(entry);
}
break;
default:
header_builder.append(entry);
break;
}
s32 idx = 0;
CodeBody parsed_header_end = parse_file( project_dir "components/header_end.hpp" );
CodeBody header_end = def_body(CT_Global_Body);
@ -1217,8 +1275,6 @@ R"(#define <interface_name>( code ) _Generic( (code), \
break;
}
// CodeBody hashtable_strc = gen_hashtable(txt("StrC"), txt("HashTable_StrC"));
CodeBody parsed_src_lexer = parse_file( project_dir "components/lexer.cpp" );
CodeBody src_lexer = def_body(CT_Global_Body);
for ( Code entry = parsed_src_lexer.begin(); entry != parsed_src_lexer.end(); ++ entry ) switch( entry ->Type )
@ -1448,6 +1504,10 @@ R"(#define <interface_name>( code ) _Generic( (code), \
header.print( format_code_to_untyped(header_end) );
header.print_fmt( "\n#pragma region Builder\n" );
header.print( format_code_to_untyped(header_builder) );
header.print_fmt( "#pragma endregion Builder\n" );
header.print_fmt( "\nGEN_API_C_END\n" );
header.print_fmt( "GEN_NS_END\n\n" );
@ -1461,6 +1521,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
#pragma region Print Dependencies
header.print_fmt( roll_own_dependencies_guard_start );
header.print_fmt( "GEN_NS_BEGIN\n\n");
header.print_fmt( "GEN_API_C_BEGIN\n\n" );
header.print( src_impl_start );
header.print( src_debug );
@ -1513,13 +1574,15 @@ R"(#define <interface_name>( code ) _Generic( (code), \
header.print( src_untyped );
header.print_fmt( "\n#pragma endregion Interface\n\n");
// header.print_fmt( "#pragma region Builder\n" );
// header.print( scan_file( project_dir "auxillary/builder.cpp" ) );
// header.print_fmt( "\n#pragma endregion Builder\n\n" );
header.print_fmt( "#pragma region Builder\n" );
header.print( scan_file( project_dir "auxillary/builder.cpp" ) );
header.print_fmt( "\n#pragma endregion Builder\n\n" );
// header.print_fmt( "\n#pragma region Scanner\n" );
// header.print( scan_file( project_dir "auxillary/scanner.hpp" ) );
// header.print_fmt( "#pragma endregion Scanner\n\n" );
header.print_fmt( "\nGEN_API_C_END\n" );
#pragma endregion Print Components
header.print_fmt( implementation_guard_end );

View File

@ -2,7 +2,7 @@
# include "builder.hpp"
#endif
Builder Builder::open( char const* path )
Builder builder_open( char const* path )
{
Builder result;
@ -19,41 +19,38 @@ Builder Builder::open( char const* path )
return result;
}
void Builder::pad_lines( s32 num )
void builder_pad_lines( Builder* builder, s32 num )
{
string_append_strc( & Buffer, txt("\n") );
string_append_strc( & builder->Buffer, txt("\n") );
}
void Builder::print( Code code )
void builder_print( Builder* builder, Code code )
{
String str = code_to_string(code);
// const ssize len = str.length();
// log_fmt( "%s - print: %.*s\n", File.filename, len > 80 ? 80 : len, str.Data );
string_append_string( & Buffer, str );
string_append_string( & builder->Buffer, str );
}
void Builder::print_fmt( char const* fmt, ... )
void builder_print_fmt_va( Builder* builder, char const* fmt, va_list va )
{
ssize res;
char buf[ GEN_PRINTF_MAXLEN ] = { 0 };
va_list va;
va_start( va, fmt );
res = str_fmt_va( buf, count_of( buf ) - 1, fmt, va ) - 1;
va_end( va );
// log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf );
string_append_c_str_len( (String*) & Buffer, (char const*)buf, res);
string_append_c_str_len( (String*) & builder->Buffer, (char const*)buf, res);
}
void Builder::write()
void builder_write(Builder* builder)
{
b32 result = file_write( & File, Buffer, string_length(Buffer) );
b32 result = file_write( & builder->File, builder->Buffer, string_length(builder->Buffer) );
if ( result == false )
log_failure("gen::File::write - Failed to write to file: %s\n", file_name( & File ) );
log_failure("gen::File::write - Failed to write to file: %s\n", file_name( & builder->File ) );
log_fmt( "Generated: %s\n", File.filename );
file_close( & File );
string_free(& Buffer);
log_fmt( "Generated: %s\n", builder->File.filename );
file_close( & builder->File );
string_free(& builder->Buffer);
}

View File

@ -3,18 +3,48 @@
# include "gen.hpp"
#endif
struct Builder;
typedef struct Builder Builder;
Builder builder_open ( char const* path );
void builder_pad_lines ( Builder* builder, s32 num );
void builder_print ( Builder* builder, Code code );
void builder_print_fmt_va( Builder* builder, char const* fmt, va_list va );
void builder_print_fmt ( Builder* builder, char const* fmt, ... ) {
va_list va;
va_start( va, fmt );
builder_print_fmt_va( builder, fmt, va );
va_end( va );
}
void builder_write( Builder* builder );
struct Builder
{
FileInfo File;
String Buffer;
static Builder open( char const* path );
#if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP
forceinline static Builder open( char const* path ) { return builder_open(path); }
void pad_lines( s32 num );
forceinline void pad_lines( s32 num ) { return builder_pad_lines(this, num); }
void print( Code );
void print_fmt( char const* fmt, ... );
forceinline void print( Code code ) { return builder_print(this, code); }
forceinline void print_fmt( char const* fmt, ... ) {
void write();
}
forceinline void write() { return builder_write(this); }
#endif
};
#if GEN_COMPILER_CPP && ! GEN_C_LIKE_CPP
void builder_pad_lines( Builder& builder, s32 num ) { return builder_pad_lines(& builder, num); }
void builder_print ( Builder& builder, Code code ) { return builder_print(& builder, code); }
void builder_write ( Builder& builder ) { return builder_write(& builder ); }
void builder_print_fmt( Builder& builder, char const* fmt, ...) {
va_list va;
va_start( va, fmt );
builder_print_fmt_va( & builder, fmt, va );
va_end( va );
}
#endif

View File

@ -2,7 +2,7 @@
#define GEN_ENFORCE_STRONG_CODE_TYPES
#define GEN_EXPOSE_BACKEND
#define GEN_C_LIKE_CPP 1
#include "gen.cpp"
#include "../project/gen.cpp"
#include "helpers/push_ignores.inline.hpp"
#include "helpers/helper.hpp"
@ -52,9 +52,9 @@ void format_file( char const* path )
Code dump_to_scratch_and_retireve( Code code )
{
Builder ecode_file_temp = Builder::open("gen/scratch.hpp");
ecode_file_temp.print(code);
ecode_file_temp.write();
Builder ecode_file_temp = builder_open("gen/scratch.hpp");
builder_print( & ecode_file_temp, code);
builder_write(& ecode_file_temp);
format_file("gen/scratch.hpp");
Code result = scan_file( "gen/scratch.hpp" );
remove("gen/scratch.hpp");
@ -85,27 +85,27 @@ int gen_main()
Code filesystem = scan_file( "dependencies/filesystem.hpp" );
Code timing = scan_file( "dependencies/timing.hpp" );
Builder
header = Builder::open("gen/gen.dep.hpp");
header.print_fmt( generation_notice );
header.print_fmt( "// This file is intended to be included within gen.hpp (There is no pragma diagnostic ignores)\n" );
header.print( platform );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
Builder _header = builder_open("gen/gen.dep.hpp");
Builder* header = & _header;
builder_print_fmt( header, generation_notice );
builder_print_fmt( header, "// This file is intended to be included within gen.hpp (There is no pragma diagnostic ignores)\n" );
builder_print( header, platform );
builder_print_fmt( header, "\nGEN_NS_BEGIN\n" );
header.print( macros );
header.print( basic_types );
header.print( debug );
header.print( memory );
header.print( string_ops );
header.print( printing );
header.print( containers );
header.print( hashing );
header.print( strings );
header.print( filesystem );
header.print( timing );
builder_print( header, macros );
builder_print( header, basic_types );
builder_print( header, debug );
builder_print( header, memory );
builder_print( header, string_ops );
builder_print( header, printing );
builder_print( header, containers );
builder_print( header, hashing );
builder_print( header, strings );
builder_print( header, filesystem );
builder_print( header, timing );
header.print_fmt( "\nGEN_NS_END\n" );
header.write();
builder_print_fmt( header, "\nGEN_NS_END\n" );
builder_write(header);
}
// gen_dep.cpp
@ -120,24 +120,24 @@ int gen_main()
Code filesystem = scan_file( "dependencies/filesystem.cpp" );
Code timing = scan_file( "dependencies/timing.cpp" );
Builder
src = Builder::open( "gen/gen.dep.cpp" );
src.print_fmt( generation_notice );
src.print_fmt( "// This file is intended to be included within gen.cpp (There is no pragma diagnostic ignores)\n" );
src.print( src_start );
src.print_fmt( "\nGEN_NS_BEGIN\n" );
Builder _src = builder_open( "gen/gen.dep.cpp" );
Builder* src = & _src;
builder_print_fmt(src, generation_notice );
builder_print_fmt( src, "// This file is intended to be included within gen.cpp (There is no pragma diagnostic ignores)\n" );
builder_print( src, src_start );
builder_print_fmt( src, "\nGEN_NS_BEGIN\n" );
src.print( debug );
src.print( string_ops );
src.print( printing );
src.print( hashing );
src.print( memory );
src.print( strings );
src.print( filesystem );
src.print( timing );
builder_print( src, debug );
builder_print( src, string_ops );
builder_print( src, printing );
builder_print( src, hashing );
builder_print( src, memory );
builder_print( src, strings );
builder_print( src, filesystem );
builder_print( src, timing );
src.print_fmt( "\nGEN_NS_END\n" );
src.write();
builder_print_fmt( src, "\nGEN_NS_END\n" );
builder_write(src);
}
CodeBody gen_component_header = def_global_body( args(
@ -165,68 +165,64 @@ int gen_main()
CodeBody especifier = gen_especifier( "enums/ESpecifier.csv" );
CodeBody ast_inlines = gen_ast_inlines();
Builder
header = Builder::open( "gen/gen.hpp" );
header.print_fmt( generation_notice );
header.print_fmt( "#pragma once\n\n" );
header.print( push_ignores );
header.print( header_start );
header.print_fmt( "\nGEN_NS_BEGIN\n\n" );
Builder _header = builder_open( "gen/gen.hpp" );
Builder* header = & _header;
builder_print_fmt( header, generation_notice );
builder_print_fmt( header, "#pragma once\n\n" );
builder_print( header, push_ignores );
builder_print( header, header_start );
builder_print_fmt( header, "\nGEN_NS_BEGIN\n\n" );
header.print_fmt( "#pragma region Types\n" );
header.print( types );
header.print( fmt_newline);
header.print( dump_to_scratch_and_retireve(ecode) );
header.print( fmt_newline);
header.print( dump_to_scratch_and_retireve(eoperator) );
header.print( fmt_newline);
header.print( dump_to_scratch_and_retireve(especifier) );
header.print( fmt_newline);
header.print_fmt( "#pragma endregion Types\n\n" );
builder_print_fmt(header, "#pragma region Types\n" );
builder_print( header, types );
builder_print( header, fmt_newline);
builder_print( header, dump_to_scratch_and_retireve(ecode) );
builder_print( header, fmt_newline);
builder_print( header, dump_to_scratch_and_retireve(eoperator) );
builder_print( header, fmt_newline);
builder_print( header, dump_to_scratch_and_retireve(especifier) );
builder_print( header, fmt_newline);
builder_print_fmt( header, "#pragma endregion Types\n\n" );
header.print_fmt( "#pragma region AST\n" );
header.print( ast );
header.print( code_types );
header.print( ast_types );
header.print_fmt( "\n#pragma endregion AST\n" );
builder_print_fmt( header, "#pragma region AST\n" );
builder_print( header, ast );
builder_print( header, code_types );
builder_print( header, ast_types );
builder_print_fmt( header, "\n#pragma endregion AST\n" );
header.print( interface );
builder_print( header, interface );
header.print_fmt( "\n#pragma region Inlines\n" );
header.print( inlines );
header.print( fmt_newline );
header.print( dump_to_scratch_and_retireve(ast_inlines) );
header.print( fmt_newline );
header.print_fmt( "#pragma endregion Inlines\n" );
builder_print_fmt( header, "\n#pragma region Inlines\n" );
builder_print( header, inlines );
builder_print( header, fmt_newline );
builder_print( header, dump_to_scratch_and_retireve(ast_inlines) );
builder_print( header, fmt_newline );
builder_print_fmt( header, "#pragma endregion Inlines\n" );
header.print( header_end );
header.print_fmt( "GEN_NS_END\n\n" );
header.print( pop_ignores );
header.write();
builder_print( header, header_end );
builder_print_fmt( header, "GEN_NS_END\n\n" );
builder_print( header, pop_ignores );
builder_write(header);
Builder
header_ecode = Builder::open( "components/gen/ecode.hpp" );
header_ecode.print( gen_component_header );
header_ecode.print( ecode );
header_ecode.write();
Builder header_ecode = builder_open( "components/gen/ecode.hpp" );
builder_print( & header_ecode, gen_component_header );
builder_print( & header_ecode, ecode );
builder_write( & header_ecode);
Builder
header_eoperator = Builder::open( "components/gen/eoperator.hpp" );
header_eoperator.print( gen_component_header );
header_eoperator.print( eoperator );
header_eoperator.write();
Builder header_eoperator = builder_open( "components/gen/eoperator.hpp" );
builder_print( & header_eoperator, gen_component_header );
builder_print( & header_eoperator, eoperator );
builder_write( & header_eoperator );
Builder
header_especifier = Builder::open( "components/gen/especifier.hpp" );
header_especifier.print( gen_component_header );
header_especifier.print( especifier );
header_especifier.write();
Builder header_especifier = builder_open( "components/gen/especifier.hpp" );
builder_print( & header_especifier, gen_component_header );
builder_print( & header_especifier, especifier );
builder_write( & header_especifier);
Builder
header_ast_inlines = Builder::open( "components/gen/ast_inlines.hpp" );
header_ast_inlines.print( gen_component_header );
header_ast_inlines.print( ast_inlines );
header_ast_inlines.write();
Builder header_ast_inlines = builder_open( "components/gen/ast_inlines.hpp" );
builder_print( & header_ast_inlines, gen_component_header );
builder_print( & header_ast_inlines, ast_inlines );
builder_write( & header_ast_inlines);
}
// gen.cpp
@ -249,88 +245,84 @@ int gen_main()
etoktype
));
Builder
src = Builder::open( "gen/gen.cpp" );
src.print_fmt( generation_notice );
src.print( push_ignores );
src.print( src_start );
src.print_fmt( "\nGEN_NS_BEGIN\n");
Builder _src = builder_open( "gen/gen.cpp" );
Builder* src = & _src;
builder_print_fmt( src, generation_notice );
builder_print( src, push_ignores );
builder_print( src, src_start );
builder_print_fmt( src, "\nGEN_NS_BEGIN\n");
src.print( static_data );
builder_print( src, static_data );
src.print_fmt( "\n#pragma region AST\n\n" );
src.print( ast_case_macros );
src.print( ast );
src.print( code_serialization );
src.print_fmt( "\n#pragma endregion AST\n" );
builder_print_fmt( src, "\n#pragma region AST\n\n" );
builder_print( src, ast_case_macros );
builder_print( src, ast );
builder_print( src, code_serialization );
builder_print_fmt( src, "\n#pragma endregion AST\n" );
src.print_fmt( "\n#pragma region Interface\n" );
src.print( interface );
src.print( upfront );
src.print_fmt( "\n#pragma region Parsing\n\n" );
src.print( dump_to_scratch_and_retireve(nspaced_etoktype) );
src.print( lexer );
src.print( parser );
src.print( parsing_interface );
src.print( untyped );
src.print_fmt( "\n#pragma endregion Parsing\n\n" );
src.print_fmt( "#pragma endregion Interface\n\n" );
builder_print_fmt( src, "\n#pragma region Interface\n" );
builder_print( src, interface );
builder_print( src, upfront );
builder_print_fmt( src, "\n#pragma region Parsing\n\n" );
builder_print( src, dump_to_scratch_and_retireve(nspaced_etoktype) );
builder_print( src, lexer );
builder_print( src, parser );
builder_print( src, parsing_interface );
builder_print( src, untyped );
builder_print_fmt( src, "\n#pragma endregion Parsing\n\n" );
builder_print_fmt( src, "#pragma endregion Interface\n\n" );
src.print_fmt( "GEN_NS_END\n\n");
src.print( pop_ignores );
src.write();
builder_print_fmt( src, "GEN_NS_END\n\n");
builder_print( src, pop_ignores );
builder_write(src);
Builder
src_etoktype = Builder::open( "components/gen/etoktype.cpp" );
src_etoktype.print( gen_component_header );
src_etoktype.print( nspaced_etoktype );
src_etoktype.write();
Builder src_etoktype = builder_open( "components/gen/etoktype.cpp" );
builder_print( & src_etoktype, gen_component_header );
builder_print( & src_etoktype, nspaced_etoktype );
builder_write( & src_etoktype);
}
// gen_builder.hpp
{
Code builder = scan_file( "auxillary/builder.hpp" );
Builder
header = Builder::open( "gen/gen.builder.hpp" );
header.print_fmt( generation_notice );
header.print_fmt( "#pragma once\n\n" );
header.print( def_include( txt("gen.hpp") ));
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( builder );
header.print_fmt( "GEN_NS_END\n" );
header.write();
Builder header = builder_open( "gen/gen.builder.hpp" );
builder_print_fmt( & header, generation_notice );
builder_print_fmt( & header, "#pragma once\n\n" );
builder_print( & header, def_include( txt("gen.hpp") ));
builder_print_fmt( & header, "\nGEN_NS_BEGIN\n" );
builder_print( & header, builder );
builder_print_fmt( & header, "GEN_NS_END\n" );
builder_write( & header);
}
// gen_builder.cpp
{
Code builder = scan_file( "auxillary/builder.cpp" );
Builder
src = Builder::open( "gen/gen.builder.cpp" );
src.print_fmt( generation_notice );
src.print( def_include( txt("gen.builder.hpp") ) );
src.print_fmt( "\nGEN_NS_BEGIN\n" );
src.print( builder );
src.print_fmt( "\nGEN_NS_END\n" );
src.write();
}
Builder src = builder_open( "gen/gen.builder.cpp" );
builder_print_fmt( & src, generation_notice );
builder_print( & src, def_include( txt("gen.builder.hpp") ) );
builder_print_fmt( & src, "\nGEN_NS_BEGIN\n" );
builder_print( & src, builder );
builder_print_fmt( & src, "\nGEN_NS_END\n" );
builder_write( & src);
// gen_scanner.hpp
{
Code parsing = scan_file( "dependencies/parsing.hpp" );
Code scanner = scan_file( "auxillary/scanner.hpp" );
Builder
header = Builder::open( "gen/gen.scanner.hpp" );
header.print_fmt( generation_notice );
header.print_fmt( "#pragma once\n\n" );
header.print( def_include( txt("gen.hpp") ) );
header.print_fmt( "\nGEN_NS_BEGIN\n" );
header.print( parsing );
header.print( scanner );
header.print_fmt( "\nGEN_NS_END\n" );
header.write();
Builder header = builder_open( "gen/gen.scanner.hpp" );
builder_print_fmt( & header, generation_notice );
builder_print_fmt( & header, "#pragma once\n\n" );
builder_print( & header, def_include( txt("gen.hpp") ) );
builder_print_fmt( & header, "\nGEN_NS_BEGIN\n" );
builder_print( & header, parsing );
builder_print( & header, scanner );
builder_print_fmt( & header, "\nGEN_NS_END\n" );
builder_write(& header);
}
// gen_scanner.cpp
@ -338,15 +330,14 @@ int gen_main()
Code parsing = scan_file( "dependencies/parsing.cpp" );
Code scanner = scan_file( "auxillary/scanner.cpp" );
Builder
src = Builder::open( "gen/gen.scanner.cpp" );
src.print_fmt( generation_notice );
src.print( def_include( txt("gen.scanner.hpp") ) );
src.print_fmt( "\nGEN_NS_BEGIN\n" );
src.print( parsing );
// src.print( scanner );
src.print_fmt( "GEN_NS_END\n" );
src.write();
Builder src = builder_open( "gen/gen.scanner.cpp" );
builder_print_fmt( & src, generation_notice );
builder_print( & src, def_include( txt("gen.scanner.hpp") ) );
builder_print_fmt( & src, "\nGEN_NS_BEGIN\n" );
builder_print( & src, parsing );
builder_print( & src, scanner );
builder_print_fmt( & src, "GEN_NS_END\n" );
builder_write( & src);
}
gen::deinit();

View File

@ -215,7 +215,7 @@ struct Arena
#endif
};
#if GEN_COMPILER_CPP
#if GEN_COMPILER_CPP && ! GEN_C_LIKECPP
forceinline AllocatorInfo allocator_info(Arena& arena ) { return arena_allocator_info(& arena); }
forceinline Arena init_sub (Arena& parent, ssize size) { return arena_init_sub( & parent, size); }
forceinline ssize alignment_of (Arena& arena, ssize alignment) { return arena_alignment_of( & arena, alignment); }