From 4fe1a4da655092f4b27b67bfc205df85c724397c Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 15 Dec 2024 01:27:57 -0500 Subject: [PATCH] c_library compiles and generates --- base/components/interface.hpp | 1 + base/components/interface.parsing.cpp | 15 ++ base/components/lexer.cpp | 56 ++++--- base/components/parser.cpp | 77 +++++++-- base/components/parser_types.hpp | 10 +- base/dependencies/memory.hpp | 2 +- gen_c_library/c_library.cpp | 150 +++++++++++++++--- gen_c_library/c_library.refactor | 1 + gen_c_library/components/containers.array.hpp | 6 +- .../components/memory.fixed_arena.hpp | 9 +- gen_c_library/components/misc.hpp | 2 +- 11 files changed, 260 insertions(+), 69 deletions(-) diff --git a/base/components/interface.hpp b/base/components/interface.hpp index d18937e..01d3815 100644 --- a/base/components/interface.hpp +++ b/base/components/interface.hpp @@ -336,6 +336,7 @@ CodeBody parse_file( Str path ); GEN_API CodeClass parse_class ( Str class_def ); GEN_API CodeConstructor parse_constructor ( Str constructor_def ); +GEN_API CodeDefine parse_define ( Str define_def ); GEN_API CodeDestructor parse_destructor ( Str destructor_def ); GEN_API CodeEnum parse_enum ( Str enum_def ); GEN_API CodeBody parse_export_body ( Str export_def ); diff --git a/base/components/interface.parsing.cpp b/base/components/interface.parsing.cpp index d96e0d6..eea7def 100644 --- a/base/components/interface.parsing.cpp +++ b/base/components/interface.parsing.cpp @@ -82,6 +82,21 @@ CodeConstructor parse_constructor( Str def ) return result; } +CodeDefine parse_define( Str def ) +{ + check_parse_args( def ); + + TokArray toks = lex( def ); + if ( toks.Arr == nullptr ) + return InvalidCode; + + _ctx->parser.Tokens = toks; + push_scope(); + CodeDefine result = parser_parse_define(); + parser_pop(& _ctx->parser); + return result; +} + CodeDestructor parse_destructor( Str def ) { check_parse_args( def ); diff --git a/base/components/lexer.cpp b/base/components/lexer.cpp index 5a0a807..78c414b 100644 --- a/base/components/lexer.cpp +++ b/base/components/lexer.cpp @@ -123,19 +123,19 @@ s32 lex_preprocessor_define( LexContext* ctx ) name.Text.Len++; } - Specifier spec = str_to_specifier( name.Text ); - TokType attrib = str_to_toktype( name.Text ); + Specifier spec = str_to_specifier( name.Text ); + TokType attrib = str_to_toktype( name.Text ); b32 not_specifier = spec == Spec_Invalid; b32 not_attribute = attrib <= Tok___Attributes_Start; - PreprocessorMacro macro = { name.Text, MT_Statement, (MacroFlags)0 }; + PreprocessorMacro macro = { name.Text, MT_Expression, (MacroFlags)0 }; PreprocessorMacro* registered_macro = lookup_preprocess_macro(name.Text); if ( registered_macro == nullptr && not_specifier && not_attribute ) { - log_fmt("Warning: '%S' was not registered before the lexer processed its #define directive, it will be registered as a statement macro\n" + log_fmt("Warning: '%S' was not registered before the lexer processed its #define directive, it will be registered as a expression macro\n" , name.Text ); - GEN_DEBUG_TRAP(); + // GEN_DEBUG_TRAP(); } array_append( _ctx->Lexer_Tokens, name ); @@ -145,7 +145,7 @@ s32 lex_preprocessor_define( LexContext* ctx ) log_fmt("Warning: %S registered macro is not flagged as functional yet the definition detects opening parenthesis '(' for arguments\n" , name.Text ); - GEN_DEBUG_TRAP(); + // GEN_DEBUG_TRAP(); } else { macro.Flags |= MF_Functional; @@ -526,35 +526,41 @@ void lex_found_token( LexContext* ctx ) } PreprocessorMacro* macro = lookup_preprocess_macro( ctx->token.Text ); - if ( macro ) + b32 has_args = ctx->left && (* ctx->scanner) == '('; + b32 resolved_to_macro = false; + if (macro) { + ctx->token.Type = macrotype_to_toktype(macro->Type); + b32 is_functional = macro_is_functional(* macro); + resolved_to_macro = has_args ? is_functional : ! is_functional; + } + if ( resolved_to_macro ) { - ctx->token.Type = macrotype_to_toktype(macro->Type); - // TODO(Ed): When we introduce a macro AST (and expression support), we'll properly lex this section. // Want to ignore any arguments the define may have as they can be execution expressions. - if ( ctx->left && (* ctx->scanner) == '(' ) + if ( has_args ) { ctx->token.Flags |= TF_Macro_Functional; - move_forward(); - ctx->token.Text.Len++; + // move_forward(); + // ctx->token.Text.Len++; - s32 level = 0; - while ( ctx->left && ((* ctx->scanner) != ')' || level > 0) ) - { - if ( (* ctx->scanner) == '(' ) - level++; + // s32 level = 0; + // while ( ctx->left && ((* ctx->scanner) != ')' || level > 0) ) + // { + // if ( (* ctx->scanner) == '(' ) + // level++; - else if ( (* ctx->scanner) == ')' && level > 0 ) - level--; + // else if ( (* ctx->scanner) == ')' && level > 0 ) + // level--; - move_forward(); - ctx->token.Text.Len++; - } + // move_forward(); + // ctx->token.Text.Len++; + // } - move_forward(); - ctx->token.Text.Len++; + // move_forward(); + // ctx->token.Text.Len++; } + //if ( (* ctx->scanner) == '\r' && ctx->scanner[1] == '\n' ) //{ // move_forward(); @@ -1277,7 +1283,7 @@ TokArray lex( Str content ) { lex_found_token( ctx ); TokType last_type = array_back(_ctx->Lexer_Tokens)->Type; - if ( last_type == Tok_Preprocess_Macro_Stmt ) + if ( last_type == Tok_Preprocess_Macro_Stmt || last_type == Tok_Preprocess_Macro_Expr ) { Token thanks_c = { { c.scanner, 0 }, Tok_Invalid, c.line, c.column, TF_Null }; c.token = thanks_c; diff --git a/base/components/parser.cpp b/base/components/parser.cpp index 749661c..40a5713 100644 --- a/base/components/parser.cpp +++ b/base/components/parser.cpp @@ -93,7 +93,15 @@ bool lex__eat(TokArray* self, TokType type ) self->Idx ++; } - if ( at_idx.Type != type ) + b32 not_accepted = at_idx.Type != type; + b32 is_identifier = at_idx.Type == Tok_Identifier; + if ( not_accepted ) + { + PreprocessorMacro* macro = lookup_preprocess_macro(at_idx.Text); + b32 accept_as_identifier = macro && bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Identifier ); + not_accepted = type == Tok_Identifier && accept_as_identifier ? false : true; + } + if ( not_accepted ) { Token tok = * lex_current( self, lex_skip_formatting ); log_failure( "Parse Error, TokArray::eat, Expected: ' %s ' not ' %.*s ' (%d, %d)`\n%s" @@ -188,7 +196,7 @@ internal CodeComment parse_comment (); internal Code parse_complicated_definition ( TokType which ); internal CodeBody parse_class_struct_body ( TokType which, Token name ); internal Code parse_class_struct ( TokType which, bool inplace_def ); -internal CodeDefine parse_define (); +internal CodeDefine parser_parse_define (); internal Code parse_expression (); internal Code parse_forward_or_definition ( TokType which, bool is_inplace ); internal CodeFn parse_function_after_name ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename ret_type, Token name ); @@ -902,7 +910,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) break; } case Tok_Preprocess_Define: { - member = cast(Code, parse_define()); + member = cast(Code, parser_parse_define()); // #define break; } @@ -1286,9 +1294,14 @@ Code parse_complicated_definition( TokType which ) } internal inline -CodeDefine parse_define() +CodeDefine parser_parse_define() { push_scope(); + if ( check(Tok_Preprocess_Hash)) { + // If parse_define is called by the user the hash reach here. + eat(Tok_Preprocess_Hash); + } + eat( Tok_Preprocess_Define ); // #define @@ -1698,7 +1711,7 @@ CodeBody parse_global_nspace( CodeType which ) break; case Tok_Preprocess_Define: - member = cast(Code, parse_define()); + member = cast(Code, parser_parse_define()); // #define ... break; @@ -2933,6 +2946,32 @@ Code parse_simple_preprocess( TokType which ) // PreprocessorMacro* macro = lookup_preprocess_macro( full_macro.Text ); + if ( which != Tok_Preprocess_Unsupported && macro == nullptr ) { + log_failure("Expected the macro %S to be registered\n%S", full_macro, parser_to_strbuilder(_ctx->parser)); + return NullCode; + } + + // TODO(Ed) : Parse this properly later (expression and statement support) + if ( macro && macro_is_functional(* macro) ) + { + eat( Tok_Capture_Start ); + + s32 level = 0; + while ( left && ( currtok.Type != Tok_Capture_End || level > 0 ) ) + { + if ( currtok.Type == Tok_Capture_Start ) + level++; + + else if ( currtok.Type == Tok_Capture_End && level > 0 ) + level--; + + eat( currtok.Type ); + } + eat( Tok_Capture_End ); + // ( ) + + full_macro.Text.Len = ( (sptr)prevtok.Text.Ptr + prevtok.Text.Len ) - (sptr)full_macro.Text.Ptr; + } if ( macro && macro_expects_body(* macro) && peektok.Type == Tok_BraceCurly_Open ) { @@ -3619,7 +3658,7 @@ CodeEnum parser_parse_enum( bool inplace_def ) if ( str_contains( tok_to_str(currtok), enum_underlying_macro.Name) ) { use_macro_underlying = true; - underlying_macro = parse_simple_preprocess( Tok_Preprocess_Macro_Typename ); + underlying_macro = parse_simple_preprocess( Tok_Preprocess_Macro_Expr ); } } @@ -3660,7 +3699,7 @@ CodeEnum parser_parse_enum( bool inplace_def ) break; case Tok_Preprocess_Define: - member = cast(Code, parse_define()); + member = cast(Code, parser_parse_define()); // #define break; @@ -4916,22 +4955,32 @@ CodeTypedef parser_parse_typedef() #if GEN_PARSER_DISABLE_MACRO_TYPEDEF if ( false ) #else - if ( check( Tok_Preprocess_Macro_Typename )) + b32 valid_macro = false; + valid_macro |= left && currtok.Type == Tok_Preprocess_Macro_Typename; + valid_macro |= left && currtok.Type == Tok_Preprocess_Macro_Stmt; + // if (currtok.Type == Tok_Preprocess_Macro_Stmt) + // { + // PreprocessMacro* macro = lookup_preprocess_macro(currtok.Text); + // valid_macro |= macro && macro_expects_body(* macro)); + // } + + if ( valid_macro ) #endif { - type = cast(Code, t_empty); - name = currtok; + type = cast(Code, t_empty); + Code macro = parse_simple_preprocess(currtok.Type); + name = currtok; + name.Text.Len = macro->Content.Len; _ctx->parser.Scope->Name = name.Text; - eat( Tok_Preprocess_Macro_Typename ); // typedef if ( currtok.Type == Tok_Identifier ) { - type = untyped_str(name.Text); + type = macro; name = currtok; eat(Tok_Identifier); + // typedef } - // typedef } else { @@ -5178,7 +5227,7 @@ CodeUnion parser_parse_union( bool inplace_def ) break; case Tok_Preprocess_Define: - member = cast(Code, parse_define()); + member = cast(Code, parser_parse_define()); break; case Tok_Preprocess_If: diff --git a/base/components/parser_types.hpp b/base/components/parser_types.hpp index ad15bf4..4421615 100644 --- a/base/components/parser_types.hpp +++ b/base/components/parser_types.hpp @@ -129,8 +129,8 @@ struct ParseContext enum MacroType : u16 { - MT_Statement, // A macro is assumed to be a statement if not resolved. - MT_Expression, + MT_Expression, // A macro is assumed to be a expression if not resolved. + MT_Statement, MT_Typename, MT_Attribute, // More of a note to the parser than anythign else (attributes should be defined in the user attribues def). MT_Specifier, // More of a note to the parser than anythign else (specifiers should be defined in the user attribues def). @@ -175,8 +175,10 @@ Str macrotype_to_str( MacroType type ) enum EMacroFlags : u16 { - MF_Functional = bit(0), // Macro has parameters (args expected to be passed) - MF_Expects_Body = bit(1), // Expects to assign a braced scope to its body. + MF_Functional = bit(0), // Macro has parameters (args expected to be passed) + MF_Expects_Body = bit(1), // Expects to assign a braced scope to its body. + MF_Allow_As_Identifier = bit(2), // lex__eat wil treat this macro as an identifier if the parser attempts to consume it as one. + // ^^^ This is a sort of kludge because we don't support push/pop macro programs rn. ^^^ MF_Null = 0, MF_UnderlyingType = GEN_U16_MAX, diff --git a/base/dependencies/memory.hpp b/base/dependencies/memory.hpp index 9304b21..c28cb09 100644 --- a/base/dependencies/memory.hpp +++ b/base/dependencies/memory.hpp @@ -225,7 +225,7 @@ forceinline ssize size_remaining(Arena& arena, ssize alignment) { return // This id is defined by Unreal for asserts #pragma push_macro("check") #undef check -forceinline void check(Arena& arena) { return arena_check(& arena); }; +forceinline void check(Arena& arena) { return arena_check(& arena); } #pragma pop_macro("check") #endif diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 8c7e2b3..9ed579b 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -65,19 +65,123 @@ int gen_main() Context ctx {}; gen::init(& ctx); - ctx.PreprocessorDefines.append(txt("GEN_API_C_BEGIN")); - ctx.PreprocessorDefines.append(txt("GEN_API_C_END")); - ctx.PreprocessorDefines.append(txt("Array(")); - ctx.PreprocessorDefines.append(txt("HashTable(")); - ctx.PreprocessorDefines.append(txt("GEN_NS_PARSER")); - ctx.PreprocessorDefines.append(txt("GEN_NS_PARSER_BEGIN")); - ctx.PreprocessorDefines.append(txt("GEN_NS_PARSER_END")); - ctx.PreprocessorDefines.append(txt("Using_Code(")); - ctx.PreprocessorDefines.append(txt("Using_CodeOps(")); - ctx.PreprocessorDefines.append(txt("GEN_OPTIMIZE_MAPPINGS_BEGIN")); - ctx.PreprocessorDefines.append(txt("GEN_OPITMIZE_MAPPINGS_END")); - ctx.PreprocessorDefines.append(txt("GEN_PARAM_DEFAULT")); - //PreprocessorDefines.append(txt("GEN_EXECUTION_EXPRESSION_SUPPORT")); + register_preprocess_macros( args( + (PreprocessorMacro { txt("bit"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("bitfield_is_set"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("GEN_C_LIKE_CPP"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("cast"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("ccast"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("rcast"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("pcast"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("scast"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("stringize_va"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("stringize"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("do_once"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("do_once_defer"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("do_once_start"), MT_Statement, MF_Null }), + (PreprocessorMacro { txt("do_once_end"), MT_Statement, MF_Null }), + (PreprocessorMacro { txt("labeled_scope_start"), MT_Statement, MF_Null }), + (PreprocessorMacro { txt("labeled_scope_end"), MT_Statement, MF_Null }), + (PreprocessorMacro { txt("compiler_decorated_func_name"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("num_args_impl"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("num_args"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("count_of"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("clamp"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("is_between"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("size_of"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("min"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("max"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("offset_of"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("static_assert"), MT_Statement, MF_Functional }), + (PreprocessorMacro { txt("typeof"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("GEN_API_C_BEGIN"), MT_Statement, MF_Null }), + (PreprocessorMacro { txt("GEN_API_C_END"), MT_Statement, MF_Null }), + (PreprocessorMacro { txt("nullptr"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("GEN_REMOVE_PTR"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("GEN_PARAM_DEFAULT"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("struct_init"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("GEN_OPTIMIZE_MAPPINGS_BEGIN"), MT_Statement, MF_Null }), + (PreprocessorMacro { txt("GEN_OPITMIZE_MAPPINGS_END"), MT_Statement, MF_Null }), + (PreprocessorMacro { txt("Array"), MT_Typename, MF_Functional }), + (PreprocessorMacro { txt("HashTable"), MT_Typename, MF_Functional }), + (PreprocessorMacro { txt("Using_Code"), MT_Statement, MF_Functional }), + (PreprocessorMacro { txt("Using_CodeOps"), MT_Statement, MF_Functional }), + (PreprocessorMacro { txt("kilobytes"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("megabytes"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("gigabytes"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("terabytes"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("GEN__ONES"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("GEN__HIGHS"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("GEN__HAS_ZERO"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("zero_item"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("zero_array"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("GEN_DEFAULT_MEMORY_ALIGNMENT"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("GEN_DEFAULT_ALLOCATOR_FLAGS"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("alloc_item"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("alloc_array"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("malloc"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("mfree"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("GEN_PRINTF_MAXLEN"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("cast_to_str"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("current"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("txt"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("GEN_FILE_OPEN_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), + (PreprocessorMacro { txt("GEN_FILE_READ_AT_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), + (PreprocessorMacro { txt("GEN_FILE_WRITE_AT_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), + (PreprocessorMacro { txt("GEN_FILE_SEEK_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), + (PreprocessorMacro { txt("GEN_FILE_CLOSE_PROC"), MT_Statement, MF_Functional | MF_Expects_Body }), + (PreprocessorMacro { txt("log_failure"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("operator"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("InvalidCode"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("NullCode"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("Verify_POD"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("gen_main"), MT_Statement, MF_Null }) + )); + register_preprocess_macros( args( + (PreprocessorMacro { txt("name"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("code"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("args"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("code_str"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("code_fmt"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("parse_fmt"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("token_fmt"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("check_member_val"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("check_member_str"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("check_member_content"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("check_member_ast"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("check_params"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("check_param_eq_ret"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("specs"), MT_Expression, MF_Functional | MF_Allow_As_Identifier }), + (PreprocessorMacro { txt("name_check"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("null_check"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("def_body_start"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("def_body_code_array_start"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("move_forward"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("skip_whitespace"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("end_line"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("check_parse_args"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("currtok_noskip"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("currtok"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("peektok"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("prevtok"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("nexttok"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("nexttok_noskip"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("eat"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("left"), MT_Expression, MF_Null | MF_Allow_As_Identifier }), + (PreprocessorMacro { txt("def_assign"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("CHECK_WAS_DEFINED"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("check_noskip"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("check"), MT_Expression, MF_Functional | MF_Allow_As_Identifier }), + (PreprocessorMacro { txt("push_scope"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("cut_length"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("cut_ptr"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("pos"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("move_fwd"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("Entry"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("CheckEndParams"), MT_Expression, MF_Functional }), + (PreprocessorMacro { txt("UseTemplateCapture"), MT_Expression, MF_Null }), + (PreprocessorMacro { txt("check_current"), MT_Expression, MF_Functional }) + )); Code push_ignores = scan_file( path_base "helpers/push_ignores.inline.hpp" ); Code pop_ignores = scan_file( path_base "helpers/pop_ignores.inline.hpp" ); @@ -139,8 +243,8 @@ int gen_main() CodeTemplate tmpl = cast(CodeTemplate, entry); if ( tmpl->Declaration->Name.contains(txt("swap"))) { - log_fmt("SWAPPED"); - CodeBody macro_swap = parse_global_body( txt(R"( + register_preprocess_macro({ txt("swap"), MT_Expression, MF_Functional }); + CodeDefine macro_swap = parse_define( txt(R"( #define swap( a, b ) \ do \ { \ @@ -241,7 +345,8 @@ do \ { if ( str_contains(entry->Name, txt("Msg_Invalid_Value"))) { - CodeDefine define = def_define(entry->Name, entry->Value->Content); + Opts_def_define opts = { {}, entry->Value->Content }; + CodeDefine define = def_define(entry->Name, MT_Expression, opts ); header_printing.append(define); continue; } @@ -407,7 +512,8 @@ do \ CodeVar var = cast(CodeVar, entry); if (var->Specs.has(Spec_Constexpr) > -1) { - CodeDefine define = def_define(entry->Name, entry->Value->Content); + Opts_def_define opts = { {}, entry->Value->Content }; + CodeDefine define = def_define(entry->Name, MT_Expression, opts); header_filesystem.append(define); continue; } @@ -811,7 +917,8 @@ R"(#define AST_ArrSpecs_Cap \ ast.append(def); break; } - CodeDefine def = def_define(var->Name, var->Value.to_strbuilder()); + Opts_def_define opts = { {}, var->Value.to_strbuilder() }; + CodeDefine def = def_define(var->Name, MT_Expression, opts ); ast.append(def); break; } @@ -832,6 +939,7 @@ R"(#define AST_ArrSpecs_Cap \ txt("CodeClass"), txt("CodeConstructor"), txt("CodeDefine"), + txt("CodeDefineParams"), txt("CodeDestructor"), txt("CodeEnum"), txt("CodeExec"), @@ -1209,8 +1317,10 @@ R"(#define ( code ) _Generic( (code), \ if (var->Specs) { s32 constexpr_found = var->Specs.remove( Spec_Constexpr ); - if (constexpr_found > -1) { - CodeDefine define = def_define(entry->Name, entry->Value->Content); + if (constexpr_found > -1) + { + Opts_def_define opts = { {}, entry->Value->Content }; + CodeDefine define = def_define(entry->Name, MT_Expression, opts ); header_end.append(define); continue; } diff --git a/gen_c_library/c_library.refactor b/gen_c_library/c_library.refactor index c743e5c..569be07 100644 --- a/gen_c_library/c_library.refactor +++ b/gen_c_library/c_library.refactor @@ -325,6 +325,7 @@ word CodeComment, gen_CodeComment word CodeClass, gen_CodeClass word CodeConstructor, gen_CodeConstructor word CodeDefine, gen_CodeDefine +word CodeDefineParams, gen_CodeDefineParams word CodeDestructor, gen_CodeDestructor word CodeEnum, gen_CodeEnum word CodeExec, gen_CodeExec diff --git a/gen_c_library/components/containers.array.hpp b/gen_c_library/components/containers.array.hpp index 2317375..e2fd8b3 100644 --- a/gen_c_library/components/containers.array.hpp +++ b/gen_c_library/components/containers.array.hpp @@ -22,9 +22,9 @@ CodeBody gen_array_base() Code get_header = untyped_str( txt( "#define array_get_header( self ) ( (ArrayHeader*)( self ) - 1)\n" )); Code type_define = untyped_str( txt( "#define Array(Type) gen_Array_##Type\n")); - Code array_begin = def_define(txt("array_begin(array)"), code( (array) )); - Code array_end = def_define(txt("array_end(array)"), code( (array + array_get_header(array)->Num ) )); - Code array_next = def_define(txt("array_next(array, entry)"), code( (entry + 1) )); + Code array_begin = def_define(txt("array_begin(array)"), MT_Expression, { {}, code( (array) ) } ); + Code array_end = def_define(txt("array_end(array)"), MT_Expression, { {}, code( (array + array_get_header(array)->Num ) ) } ); + Code array_next = def_define(txt("array_next(array, entry)"), MT_Expression, { {}, code( (entry + 1) ) } ); return def_global_body( args( fmt_newline, diff --git a/gen_c_library/components/memory.fixed_arena.hpp b/gen_c_library/components/memory.fixed_arena.hpp index 262f06d..c9fd7ab 100644 --- a/gen_c_library/components/memory.fixed_arena.hpp +++ b/gen_c_library/components/memory.fixed_arena.hpp @@ -89,7 +89,14 @@ CodeBody gen_fixed_arenas() result.append(arena_interface_2mb); result.append(arena_interface_4mb); - CodeDefine def = def_define(txt("fixed_arena_allocator_info(fixed_arena)"), txt("( (AllocatorInfo) { arena_allocator_proc, & (fixed_arena)->arena } )")); + register_preprocess_macros( args( + ( PreprocessorMacro { txt("fixed_arena_allocator_info"), MT_Expression, MF_Functional }), + ( PreprocessorMacro { txt("fixed_arena_init"), MT_Expression, MF_Functional }), + ( PreprocessorMacro { txt("fixed_arena_free"), MT_Expression, MF_Functional }), + ( PreprocessorMacro { txt("fixed_arena_size_remaining"), MT_Expression, MF_Functional }) + )); + + CodeDefine def = parse_define(txt("#define fixed_arena_allocator_info(fixed_arena) ( (AllocatorInfo) { arena_allocator_proc, & (fixed_arena)->arena } )\n")); result.append(def); result.append(fmt_newline); diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index 727d2b6..b288663 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -7,7 +7,7 @@ void convert_cpp_enum_to_c( CodeEnum to_convert, CodeBody to_append ) { #pragma push_macro("enum_underlying") #undef enum_underlying - StrCached type = to_convert->UnderlyingType ? to_convert->UnderlyingType : to_convert->Name + StrCached type = to_convert->UnderlyingType ? to_convert->UnderlyingType.to_strbuilder().to_str() : to_convert->Name; CodeTypedef tdef = parse_typedef(token_fmt("type", type, "name", to_convert->Name, stringize( typedef enum ; ))); if (to_convert->UnderlyingType) {