diff --git a/base/components/ast_types.hpp b/base/components/ast_types.hpp index 8899174..43a0b80 100644 --- a/base/components/ast_types.hpp +++ b/base/components/ast_types.hpp @@ -151,7 +151,7 @@ struct AST_Define char _PAD_PROPERTIES_ [ sizeof(AST*) * 4 ]; CodeDefineParams Params; Code Body; // Should be completely serialized for now to a: StrCached Content. - char _PAD_PROPERTIES_ [ sizeof(AST*) * 1 ]; + char _PAD_PROPERTIES_2_ [ sizeof(AST*) * 1 ]; }; }; StrCached Name; diff --git a/base/components/code_serialization.cpp b/base/components/code_serialization.cpp index b219e80..c9678a1 100644 --- a/base/components/code_serialization.cpp +++ b/base/components/code_serialization.cpp @@ -252,7 +252,7 @@ void define_to_strbuilder_ref(CodeDefine define, StrBuilder* result ) GEN_ASSERT(define->Body); GEN_ASSERT(define->Body->Content); if (define->Params) { - StrBuilder params_builder = define_params_to_strbuilder(define->Params) + StrBuilder params_builder = define_params_to_strbuilder(define->Params); strbuilder_append_fmt( result, "#define %S(%S) %S", define->Name, strbuilder_to_str(params_builder), define->Body->Content ); } else { @@ -268,7 +268,7 @@ StrBuilder define_params_to_strbuilder(CodeDefineParams params) return result; } -void define_params_to_strbuilder_ref(CodeDefineParams params, StrBuilder* result) +void define_params_to_strbuilder_ref(CodeDefineParams self, StrBuilder* result) { GEN_ASSERT(self); GEN_ASSERT(result); @@ -278,9 +278,9 @@ void define_params_to_strbuilder_ref(CodeDefineParams params, StrBuilder* result } if ( self->NumEntries - 1 > 0 ) { - for ( CodeParams param = begin_CodeDefineParams(self->Next); param != end_CodeDefineParams(self->Next); param = next_CodeDefineParams(self->Next, param) ) + for ( CodeDefineParams param = begin_CodeDefineParams(self->Next); param != end_CodeDefineParams(self->Next); param = next_CodeDefineParams(self->Next, param) ) { - strbuilder_append_fmt( result, ", %SB", params_to_strbuilder(param) ); + strbuilder_append_fmt( result, ", %SB", define_params_to_strbuilder(param) ); } } } diff --git a/base/components/code_types.hpp b/base/components/code_types.hpp index 2ab1941..d1885ff 100644 --- a/base/components/code_types.hpp +++ b/base/components/code_types.hpp @@ -232,15 +232,15 @@ struct CodeDefineParams forceinline void to_strbuilder( StrBuilder& result ) { return define_params_to_strbuilder_ref(* this, & result); } #endif Using_CodeOps( CodeDefineParams ); - forceinline CodeDefineParams begin() { return begin_CodeParams( cast(CodeParams, * this)); } - forceinline CodeDefineParams end() { return end_CodeParams( cast(CodeParams, * this)); } + forceinline CodeDefineParams begin() { return (CodeDefineParams) (Code) begin_CodeParams( cast(CodeParams, * this)); } + forceinline CodeDefineParams end() { return (CodeDefineParams) (Code) end_CodeParams( cast(CodeParams, * this)); } forceinline operator Code() { return { (AST*)ast }; } forceinline CodeDefineParams operator *() { return * this; } // Required to support for-range iteration. forceinline AST_DefineParams* operator->() { GEN_ASSERT(ast); return ast; } - forceinline CodeDefineParams& operator++() { return cast(CodeParams, * this).operator ++() }; + forceinline CodeDefineParams& operator++() { return (CodeDefineParams) (Code) cast(CodeParams, * this).operator ++(); }; AST_DefineParams* ast; }; diff --git a/base/components/gen/ast_inlines.hpp b/base/components/gen/ast_inlines.hpp index 46e80ed..8f187b8 100644 --- a/base/components/gen/ast_inlines.hpp +++ b/base/components/gen/ast_inlines.hpp @@ -179,6 +179,22 @@ inline AST_Define* CodeDefine::operator->() return ast; } +inline CodeDefineParams& CodeDefineParams::operator=( Code other ) +{ + if ( other.ast != nullptr && other->Parent != nullptr ) + { + ast = rcast( decltype( ast ), code_duplicate( other ).ast ); + ast->Parent = { nullptr }; + } + ast = rcast( decltype( ast ), other.ast ); + return *this; +} + +inline CodeDefineParams::operator bool() +{ + return ast != nullptr; +} + inline CodeDestructor& CodeDestructor::operator=( Code other ) { if ( other.ast != nullptr && other->Parent != nullptr ) @@ -851,6 +867,11 @@ forceinline Code::operator CodeDefine() const return { (AST_Define*)ast }; } +forceinline Code::operator CodeDefineParams() const +{ + return { (AST_DefineParams*)ast }; +} + forceinline Code::operator CodeDestructor() const { return { (AST_Destructor*)ast }; diff --git a/base/components/gen/ecodetypes.hpp b/base/components/gen/ecodetypes.hpp index c7ad1d0..5973002 100644 --- a/base/components/gen/ecodetypes.hpp +++ b/base/components/gen/ecodetypes.hpp @@ -75,7 +75,7 @@ enum CodeType : u32 inline Str codetype_to_str( CodeType type ) { - local_persist Str lookup[61] = { + local_persist Str lookup[] = { { "Invalid", sizeof( "Invalid" ) - 1 }, { "Untyped", sizeof( "Untyped" ) - 1 }, { "NewLine", sizeof( "NewLine" ) - 1 }, @@ -144,7 +144,7 @@ inline Str codetype_to_str( CodeType type ) inline Str codetype_to_keyword_str( CodeType type ) { - local_persist Str lookup[61] = { + local_persist Str lookup[] = { { "__NA__", sizeof( "__NA__" ) - 1 }, { "__NA__", sizeof( "__NA__" ) - 1 }, { "__NA__", sizeof( "__NA__" ) - 1 }, diff --git a/base/components/gen/eoperator.hpp b/base/components/gen/eoperator.hpp index b447d7a..6a28f2b 100644 --- a/base/components/gen/eoperator.hpp +++ b/base/components/gen/eoperator.hpp @@ -60,7 +60,7 @@ enum Operator : u32 inline Str operator_to_str( Operator op ) { - local_persist Str lookup[47] = { + local_persist Str lookup[] = { { "INVALID", sizeof( "INVALID" ) - 1 }, { "=", sizeof( "=" ) - 1 }, { "+=", sizeof( "+=" ) - 1 }, diff --git a/base/components/gen/etoktype.hpp b/base/components/gen/etoktype.hpp index dc4a400..5b43e44 100644 --- a/base/components/gen/etoktype.hpp +++ b/base/components/gen/etoktype.hpp @@ -114,105 +114,105 @@ enum TokType : u32 inline Str toktype_to_str( TokType type ) { local_persist Str lookup[] = { - { "__invalid__", sizeof( "__invalid__" ) - 1 }, - { "private", sizeof( "private" ) - 1 }, - { "protected", sizeof( "protected" ) - 1 }, - { "public", sizeof( "public" ) - 1 }, - { ".", sizeof( "." ) - 1 }, - { "::", sizeof( "::" ) - 1 }, - { "&", sizeof( "&" ) - 1 }, - { "&&", sizeof( "&&" ) - 1 }, - { ":", sizeof( ":" ) - 1 }, - { "[[", sizeof( "[[" ) - 1 }, - { "]]", sizeof( "]]" ) - 1 }, - { "{", sizeof( "{" ) - 1 }, - { "}", sizeof( "}" ) - 1 }, - { "[", sizeof( "[" ) - 1 }, - { "]", sizeof( "]" ) - 1 }, - { "(", sizeof( "(" ) - 1 }, - { ")", sizeof( ")" ) - 1 }, - { "__comment__", sizeof( "__comment__" ) - 1 }, - { "__comment_end__", sizeof( "__comment_end__" ) - 1 }, - { "__comment_start__", sizeof( "__comment_start__" ) - 1 }, - { "__character__", sizeof( "__character__" ) - 1 }, - { ",", sizeof( "," ) - 1 }, - { "class", sizeof( "class" ) - 1 }, - { "__attribute__", sizeof( "__attribute__" ) - 1 }, - { "__declspec", sizeof( "__declspec" ) - 1 }, - { "enum", sizeof( "enum" ) - 1 }, - { "extern", sizeof( "extern" ) - 1 }, - { "friend", sizeof( "friend" ) - 1 }, - { "module", sizeof( "module" ) - 1 }, - { "namespace", sizeof( "namespace" ) - 1 }, - { "operator", sizeof( "operator" ) - 1 }, - { "struct", sizeof( "struct" ) - 1 }, - { "template", sizeof( "template" ) - 1 }, - { "typedef", sizeof( "typedef" ) - 1 }, - { "using", sizeof( "using" ) - 1 }, - { "union", sizeof( "union" ) - 1 }, - { "__identifier__", sizeof( "__identifier__" ) - 1 }, - { "import", sizeof( "import" ) - 1 }, - { "export", sizeof( "export" ) - 1 }, - { "__new_line__", sizeof( "__new_line__" ) - 1 }, - { "__number__", sizeof( "__number__" ) - 1 }, - { "__operator__", sizeof( "__operator__" ) - 1 }, - { "#", sizeof( "#" ) - 1 }, - { "define", sizeof( "define" ) - 1 }, - { "__define_param__", sizeof( "__define__param__" ) - 1 }, - { "if", sizeof( "if" ) - 1 }, - { "ifdef", sizeof( "ifdef" ) - 1 }, - { "ifndef", sizeof( "ifndef" ) - 1 }, - { "elif", sizeof( "elif" ) - 1 }, - { "else", sizeof( "else" ) - 1 }, - { "endif", sizeof( "endif" ) - 1 }, - { "include", sizeof( "include" ) - 1 }, - { "pragma", sizeof( "pragma" ) - 1 }, - { "__macro_content__", sizeof( "__macro_content__" ) - 1 }, - { "__macro_expression__", sizeof( "__macro_expression__" ) - 1 }, - { "__macro_statment__", sizeof( "__macro_statment__" ) - 1 }, - { "__macro_typename__", sizeof( "__macro_typename__" ) - 1 }, - { "__unsupported__", sizeof( "__unsupported__" ) - 1 }, - { "alignas", sizeof( "alignas" ) - 1 }, - { "const", sizeof( "const" ) - 1 }, - { "consteval", sizeof( "consteval" ) - 1 }, - { "constexpr", sizeof( "constexpr" ) - 1 }, - { "constinit", sizeof( "constinit" ) - 1 }, - { "explicit", sizeof( "explicit" ) - 1 }, - { "extern", sizeof( "extern" ) - 1 }, - { "final", sizeof( "final" ) - 1 }, - { "forceinline", sizeof( "forceinline" ) - 1 }, - { "global", sizeof( "global" ) - 1 }, - { "inline", sizeof( "inline" ) - 1 }, - { "internal", sizeof( "internal" ) - 1 }, - { "local_persist", sizeof( "local_persist" ) - 1 }, - { "mutable", sizeof( "mutable" ) - 1 }, - { "neverinline", sizeof( "neverinline" ) - 1 }, - { "override", sizeof( "override" ) - 1 }, - { "static", sizeof( "static" ) - 1 }, - { "thread_local", sizeof( "thread_local" ) - 1 }, - { "volatile", sizeof( "volatile" ) - 1 }, - { "virtual", sizeof( "virtual" ) - 1 }, - { "*", sizeof( "*" ) - 1 }, - { ";", sizeof( ";" ) - 1 }, - { "static_assert", sizeof( "static_assert" ) - 1 }, - { "__string__", sizeof( "__string__" ) - 1 }, - { "typename", sizeof( "typename" ) - 1 }, - { "unsigned", sizeof( "unsigned" ) - 1 }, - { "signed", sizeof( "signed" ) - 1 }, - { "short", sizeof( "short" ) - 1 }, - { "long", sizeof( "long" ) - 1 }, - { "bool", sizeof( "bool" ) - 1 }, - { "char", sizeof( "char" ) - 1 }, - { "int", sizeof( "int" ) - 1 }, - { "double", sizeof( "double" ) - 1 }, - { "__int8", sizeof( "__int8" ) - 1 }, - { "__int16", sizeof( "__int16" ) - 1 }, - { "__int32", sizeof( "__int32" ) - 1 }, - { "__int64", sizeof( "__int64" ) - 1 }, - { "_W64", sizeof( "_W64" ) - 1 }, - { "...", sizeof( "..." ) - 1 }, - { "__attrib_start__", sizeof( "__attrib_start__" ) - 1 }, - { "GEN_API", sizeof( "GEN_API" ) - 1 }, + { "__invalid__", sizeof( "__invalid__" ) - 1 }, + { "private", sizeof( "private" ) - 1 }, + { "protected", sizeof( "protected" ) - 1 }, + { "public", sizeof( "public" ) - 1 }, + { ".", sizeof( "." ) - 1 }, + { "::", sizeof( "::" ) - 1 }, + { "&", sizeof( "&" ) - 1 }, + { "&&", sizeof( "&&" ) - 1 }, + { ":", sizeof( ":" ) - 1 }, + { "[[", sizeof( "[[" ) - 1 }, + { "]]", sizeof( "]]" ) - 1 }, + { "{", sizeof( "{" ) - 1 }, + { "}", sizeof( "}" ) - 1 }, + { "[", sizeof( "[" ) - 1 }, + { "]", sizeof( "]" ) - 1 }, + { "(", sizeof( "(" ) - 1 }, + { ")", sizeof( ")" ) - 1 }, + { "__comment__", sizeof( "__comment__" ) - 1 }, + { "__comment_end__", sizeof( "__comment_end__" ) - 1 }, + { "__comment_start__", sizeof( "__comment_start__" ) - 1 }, + { "__character__", sizeof( "__character__" ) - 1 }, + { ",", sizeof( "," ) - 1 }, + { "class", sizeof( "class" ) - 1 }, + { "__attribute__", sizeof( "__attribute__" ) - 1 }, + { "__declspec", sizeof( "__declspec" ) - 1 }, + { "enum", sizeof( "enum" ) - 1 }, + { "extern", sizeof( "extern" ) - 1 }, + { "friend", sizeof( "friend" ) - 1 }, + { "module", sizeof( "module" ) - 1 }, + { "namespace", sizeof( "namespace" ) - 1 }, + { "operator", sizeof( "operator" ) - 1 }, + { "struct", sizeof( "struct" ) - 1 }, + { "template", sizeof( "template" ) - 1 }, + { "typedef", sizeof( "typedef" ) - 1 }, + { "using", sizeof( "using" ) - 1 }, + { "union", sizeof( "union" ) - 1 }, + { "__identifier__", sizeof( "__identifier__" ) - 1 }, + { "import", sizeof( "import" ) - 1 }, + { "export", sizeof( "export" ) - 1 }, + { "__new_line__", sizeof( "__new_line__" ) - 1 }, + { "__number__", sizeof( "__number__" ) - 1 }, + { "__operator__", sizeof( "__operator__" ) - 1 }, + { "#", sizeof( "#" ) - 1 }, + { "define", sizeof( "define" ) - 1 }, + { "__define_param__", sizeof( "__define_param__" ) - 1 }, + { "if", sizeof( "if" ) - 1 }, + { "ifdef", sizeof( "ifdef" ) - 1 }, + { "ifndef", sizeof( "ifndef" ) - 1 }, + { "elif", sizeof( "elif" ) - 1 }, + { "else", sizeof( "else" ) - 1 }, + { "endif", sizeof( "endif" ) - 1 }, + { "include", sizeof( "include" ) - 1 }, + { "pragma", sizeof( "pragma" ) - 1 }, + { "__macro_content__", sizeof( "__macro_content__" ) - 1 }, + { "__macro_expression__", sizeof( "__macro_expression__" ) - 1 }, + { "__macro_statment__", sizeof( "__macro_statment__" ) - 1 }, + { "__macro_typename__", sizeof( "__macro_typename__" ) - 1 }, + { "__unsupported__", sizeof( "__unsupported__" ) - 1 }, + { "alignas", sizeof( "alignas" ) - 1 }, + { "const", sizeof( "const" ) - 1 }, + { "consteval", sizeof( "consteval" ) - 1 }, + { "constexpr", sizeof( "constexpr" ) - 1 }, + { "constinit", sizeof( "constinit" ) - 1 }, + { "explicit", sizeof( "explicit" ) - 1 }, + { "extern", sizeof( "extern" ) - 1 }, + { "final", sizeof( "final" ) - 1 }, + { "forceinline", sizeof( "forceinline" ) - 1 }, + { "global", sizeof( "global" ) - 1 }, + { "inline", sizeof( "inline" ) - 1 }, + { "internal", sizeof( "internal" ) - 1 }, + { "local_persist", sizeof( "local_persist" ) - 1 }, + { "mutable", sizeof( "mutable" ) - 1 }, + { "neverinline", sizeof( "neverinline" ) - 1 }, + { "override", sizeof( "override" ) - 1 }, + { "static", sizeof( "static" ) - 1 }, + { "thread_local", sizeof( "thread_local" ) - 1 }, + { "volatile", sizeof( "volatile" ) - 1 }, + { "virtual", sizeof( "virtual" ) - 1 }, + { "*", sizeof( "*" ) - 1 }, + { ";", sizeof( ";" ) - 1 }, + { "static_assert", sizeof( "static_assert" ) - 1 }, + { "__string__", sizeof( "__string__" ) - 1 }, + { "typename", sizeof( "typename" ) - 1 }, + { "unsigned", sizeof( "unsigned" ) - 1 }, + { "signed", sizeof( "signed" ) - 1 }, + { "short", sizeof( "short" ) - 1 }, + { "long", sizeof( "long" ) - 1 }, + { "bool", sizeof( "bool" ) - 1 }, + { "char", sizeof( "char" ) - 1 }, + { "int", sizeof( "int" ) - 1 }, + { "double", sizeof( "double" ) - 1 }, + { "__int8", sizeof( "__int8" ) - 1 }, + { "__int16", sizeof( "__int16" ) - 1 }, + { "__int32", sizeof( "__int32" ) - 1 }, + { "__int64", sizeof( "__int64" ) - 1 }, + { "_W64", sizeof( "_W64" ) - 1 }, + { "...", sizeof( "..." ) - 1 }, + { "__attrib_start__", sizeof( "__attrib_start__" ) - 1 }, + { "GEN_API", sizeof( "GEN_API" ) - 1 }, }; return lookup[type]; } diff --git a/base/components/inlines.hpp b/base/components/inlines.hpp index b84ab3b..e6b4b09 100644 --- a/base/components/inlines.hpp +++ b/base/components/inlines.hpp @@ -259,12 +259,12 @@ CodeParams next_CodeParams(CodeParams params, CodeParams param_iter) #pragma region CodeDefineParams forceinline void define_params_append (CodeDefineParams appendee, CodeDefineParams other ) { params_append( cast(CodeParams, appendee), cast(CodeParams, other) ); } -forceinline CodeDefineParams define_params_get (CodeDefineParams self, s32 idx ) { return params_get( cast(CodeParams, self), idx); } +forceinline CodeDefineParams define_params_get (CodeDefineParams self, s32 idx ) { return (CodeDefineParams) (Code) params_get( cast(CodeParams, self), idx); } forceinline bool define_params_has_entries(CodeDefineParams self) { return params_has_entries( cast(CodeParams, self)); } -CodeDefineParams begin_CodeDefineParams(CodeDefineParams params) { return begin_CodeParams( cast(CodeParams, params)); } -CodeDefineParams end_CodeDefineParams (CodeDefineParams params) { return end_CodeParams ( cast(CodeParams, params)); } -CodeDefineParams next_CodeDefineParams (CodeDefineParams params, CodeDefineParams entry_iter) { return next_CodeParams ( cast(CodeParams, params), cast(CoeParams, entry_iter)); } +CodeDefineParams begin_CodeDefineParams(CodeDefineParams params) { return (CodeDefineParams) (Code) begin_CodeParams( cast(CodeParams, (Code)params)); } +CodeDefineParams end_CodeDefineParams (CodeDefineParams params) { return (CodeDefineParams) (Code) end_CodeParams ( cast(CodeParams, (Code)params)); } +CodeDefineParams next_CodeDefineParams (CodeDefineParams params, CodeDefineParams entry_iter) { return (CodeDefineParams) (Code) next_CodeParams ( cast(CodeParams, (Code)params), cast(CodeParams, (Code)entry_iter)); } #pragma endregion CodeDefineParams #pragma region CodeSpecifiers diff --git a/base/components/interface.cpp b/base/components/interface.cpp index 6a2d648..69d6482 100644 --- a/base/components/interface.cpp +++ b/base/components/interface.cpp @@ -272,8 +272,8 @@ void init(Context* ctx) ctx->CodePool_NumBlocks = kilobytes(16); } - if (ctx->InitSize_LexArena == 0 ) { - ctx->InitSize_LexArena = megabytes(4); + if (ctx->InitSize_LexerTokens == 0 ) { + ctx->InitSize_LexerTokens = kilobytes(64); } if (ctx->SizePer_StringArena == 0) { ctx->SizePer_StringArena = megabytes(1); @@ -315,7 +315,7 @@ void init(Context* ctx) if ( ctx->StrCache.Entries == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the StringCache"); - ctx->PreprocessorMacros = hashtable_init(PreprocessorMacros, ctx->Allocator_DyanmicContainers); + ctx->PreprocessorMacros = hashtable_init(PreprocessorMacro, ctx->Allocator_DyanmicContainers); if (ctx->PreprocessorMacros.Hashes == nullptr || ctx->PreprocessorMacros.Entries == nullptr) { GEN_FATAL( "gen::init: Failed to initialize the PreprocessMacros table" ); } diff --git a/base/components/interface.hpp b/base/components/interface.hpp index e56a87a..f3e13fd 100644 --- a/base/components/interface.hpp +++ b/base/components/interface.hpp @@ -60,7 +60,7 @@ struct Context u32 CodePool_NumBlocks; // TODO(Ed): Review these... (No longer needed if using the proper allocation strategy) - u32 InitSize_LexArena; + u32 InitSize_LexerTokens; u32 SizePer_StringArena; // TODO(Ed): Symbol Table @@ -87,9 +87,6 @@ struct Context StringTable StrCache; // TODO(Ed): This needs to be just handled by a parser context - - // Arena LexArena; - // StringTable Lexer_defines; Array(Token) Lexer_Tokens; // TODO(Ed): Active parse context vs a parse result need to be separated conceptually @@ -109,8 +106,8 @@ GEN_API void reset(Context* ctx); GEN_API void set_context(Context* ctx); -// Mostly used to verify a macro entry exists for the given name -GEN_API PreprocessMacro* lookup_preprocess_macro( Str Name ); +// Mostly intended for the parser +GEN_API PreprocessorMacro* lookup_preprocess_macro( Str Name ); // Alternative way to add a preprocess define entry for the lexer & parser to utilize // if the user doesn't want to use def_define @@ -154,9 +151,9 @@ struct Opts_def_constructor { GEN_API CodeConstructor def_constructor( Opts_def_constructor opts GEN_PARAM_DEFAULT ); struct Opts_def_define { - MacroFlags flags; CodeDefineParams params; Str content; + MacroFlags flags; b32 dont_register_to_preprocess_macros; }; GEN_API CodeDefine def_define( Str name, MacroType type, Opts_def_define opts GEN_PARAM_DEFAULT ); @@ -273,7 +270,7 @@ GEN_API CodeBody def_body( CodeType type ); GEN_API CodeBody def_class_body ( s32 num, ... ); GEN_API CodeBody def_class_body ( s32 num, Code* codes ); GEN_API CodeDefineParams def_define_params ( s32 num, ... ); -GEN_API CodeDefineParams def_define_params ( s32 num, CodeDefineParams* codes ) +GEN_API CodeDefineParams def_define_params ( s32 num, CodeDefineParams* codes ); GEN_API CodeBody def_enum_body ( s32 num, ... ); GEN_API CodeBody def_enum_body ( s32 num, Code* codes ); GEN_API CodeBody def_export_body ( s32 num, ... ); diff --git a/base/components/interface.upfront.cpp b/base/components/interface.upfront.cpp index 1e4b6b1..271bb68 100644 --- a/base/components/interface.upfront.cpp +++ b/base/components/interface.upfront.cpp @@ -572,13 +572,13 @@ CodeDefine def_define( Str name, MacroType type, Opts_def_define p ) result->Name = cache_str( name ); result->Params = p.params; if ( p.content.Len <= 0 || p.content.Ptr == nullptr ) - result->Content = cache_str( txt("\n") ); + result->Body = untyped_str( txt("\n") ); else - result->Content = cache_str( strbuilder_to_str(strbuilder_fmt_buf(_ctx->Allocator_Temp, "%S\n", content)) ); + result->Body = untyped_str( strbuilder_to_str(strbuilder_fmt_buf(_ctx->Allocator_Temp, "%S\n", p.content)) ); b32 register_define = ! p.dont_register_to_preprocess_macros; if ( register_define ) { - macro_entry = { result->Name, type, p.flags }; + PreprocessorMacro macro_entry = { result->Name, type, p.flags }; register_preprocess_macro(macro_entry); } return result; diff --git a/base/components/lexer.cpp b/base/components/lexer.cpp index 6dc5ff9..97f3c42 100644 --- a/base/components/lexer.cpp +++ b/base/components/lexer.cpp @@ -118,7 +118,7 @@ s32 lex_preprocessor_define( LexContext* ctx ) Token name = { { ctx->scanner, 1 }, Tok_Identifier, ctx->line, ctx->column, TF_Preprocess }; move_forward(); - PreprocessorMacro macro = { name.Text, MT_Statement, 0 }; + PreprocessorMacro macro = { name.Text, MT_Statement, (MacroFlags)0 }; PreprocessorMacro* registered_macro = lookup_preprocess_macro(name.Text); if ( registered_macro == nullptr ) { log_fmt("Warning: %S is was not registered before the lexer processed its #define directive, it will be registered as a statement macro" @@ -146,6 +146,7 @@ s32 lex_preprocessor_define( LexContext* ctx ) array_append( _ctx->Lexer_Tokens, opening_paren ); move_forward(); + Token last_parameter; // We need to tokenize the define's arguments now: while( ctx->left && * ctx->scanner != ')') { @@ -162,6 +163,7 @@ s32 lex_preprocessor_define( LexContext* ctx ) } array_append(_ctx->Lexer_Tokens, parameter); skip_whitespace(); + last_parameter = parameter; } else { log_failure("lex_preprocessor_define(%d, %d): Expected a '_' or alpha character for a parameter name for %S\n" @@ -176,7 +178,7 @@ s32 lex_preprocessor_define( LexContext* ctx ) log_failure("lex_preprocessor_define(%d, %d): Expected a comma after parameter %S for %S\n" , ctx->line , ctx->column - , parameter.Text + , last_parameter.Text , name.Text ); return Lex_ReturnNull; @@ -190,7 +192,7 @@ s32 lex_preprocessor_define( LexContext* ctx ) log_failure("lex_preprocessor_define(%d, %d): Expected a ')' after last_parameter %S for %S (ran out of characters...)\n" , ctx->line , ctx->column - , parameter.Text + , last_parameter.Text , name.Text ); return Lex_ReturnNull; @@ -527,7 +529,6 @@ TokArray lex( Str content ) c.content = content; c.left = content.Len; c.scanner = content.Ptr; - c.defines = _ctx->Lexer_defines; char const* word = c.scanner; s32 word_length = 0; diff --git a/base/components/parser.cpp b/base/components/parser.cpp index e911fc1..46c71c2 100644 --- a/base/components/parser.cpp +++ b/base/components/parser.cpp @@ -118,11 +118,7 @@ bool lex__eat(TokArray* self, TokType type ) internal void parser_init() { - _ctx->Lexer_Tokens = array_init_reserve(Token, arena_allocator_info( & _ctx->LexArena) - , ( _ctx->InitSize_LexArena - sizeof( ArrayHeader ) ) / sizeof(Token) - ); - - _ctx->Lexer_defines = hashtable_init_reserve(Str, _ctx->Allocator_DyanmicContainers, 256 ); + _ctx->Lexer_Tokens = array_init_reserve(Token, _ctx->Allocator_DyanmicContainers, _ctx->InitSize_LexerTokens ); } internal @@ -942,7 +938,6 @@ CodeBody parse_class_struct_body( TokType which, Token name ) member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Macro_Stmt )); break; } - case Tok_Preprocess_Macro_Expr: { log_failure("Unbounded macro expression residing in class/struct body\n%S", parser_to_strbuilder(_ctx->parser)); return InvalidCode; @@ -961,7 +956,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) } case Tok_Preprocess_Unsupported: { - member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Unsupported, parser_consume_braces )); + member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Unsupported )); // # break; } @@ -1317,10 +1312,10 @@ CodeDefine parse_define() CodeDefineParams params; if ( left && currtok.Type != Tok_Capture_End ) { params = (CodeDefineParams) make_code(); - params.Type = CT_Parameters_Define; - params.Name = currtok.text; + params->Type = CT_Parameters_Define; + params->Name = currtok.Text; - define->params = params; + define->Params = params; eat( Tok_Preprocess_Define_Param ); // #define ( ) @@ -1328,8 +1323,8 @@ CodeDefine parse_define() while( left && currtok.Type != Tok_Capture_End ) { CodeDefineParams next_param = (CodeDefineParams) make_code(); - next_param.Type = CT_Parameters_Define; - next_param.Name = currtok.Text; + next_param->Type = CT_Parameters_Define; + next_param->Name = currtok.Text; define_params_append(params, next_param); // #define ( , ... @@ -1349,7 +1344,7 @@ CodeDefine parse_define() if ( currtok.Text.Len == 0 ) { - define->Content = cache_str( tok_to_str(currtok) ); + define->Body = untyped_str( tok_to_str(currtok) ); eat( Tok_Preprocess_Content ); // #define ( ) @@ -1357,7 +1352,7 @@ CodeDefine parse_define() return define; } - define->Content = cache_str( strbuilder_to_str( parser_strip_formatting( tok_to_str(currtok), parser_strip_formatting_dont_preserve_newlines )) ); + define->Body = untyped_str( strbuilder_to_str( parser_strip_formatting( tok_to_str(currtok), parser_strip_formatting_dont_preserve_newlines )) ); eat( Tok_Preprocess_Content ); // #define ( ) @@ -1729,11 +1724,13 @@ CodeBody parse_global_nspace( CodeType which ) // #endif break; - case Tok_Preprocess_Macro: { - // - macro_found = true; - goto Preprocess_Macro_Bare_In_Body; - // TODO(Ed): MACRO UPDATE + case Tok_Preprocess_Macro_Stmt: { + member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Macro_Stmt )); + break; + } + case Tok_Preprocess_Macro_Expr: { + log_failure("Unbounded macro expression residing in class/struct body\n%S", parser_to_strbuilder(_ctx->parser)); + return InvalidCode; } case Tok_Preprocess_Pragma: { @@ -1743,7 +1740,7 @@ CodeBody parse_global_nspace( CodeType which ) break; case Tok_Preprocess_Unsupported: { - member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Unsupported, parser_consume_braces )); + member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Unsupported )); // # ... } break; @@ -1828,6 +1825,7 @@ CodeBody parse_global_nspace( CodeType which ) } //! Fallthrough intentional case Tok_Identifier: + case Tok_Preprocess_Macro_Typename: case Tok_Spec_Const: case Tok_Type_Long: case Tok_Type_Short: @@ -1879,28 +1877,6 @@ CodeBody parse_global_nspace( CodeType which ) } } - if (macro_found) - { - Preprocess_Macro_Bare_In_Body: - b32 lone_macro = nexttok.Type == Tok_Statement_End || nexttok_noskip.Type == Tok_NewLine; - if (lone_macro) - { - member = parse_simple_preprocess( Tok_Preprocess_Macro, parser_consume_braces ); - // ; - - if ( member == Code_Invalid ) - { - log_failure( "Failed to parse member\n%s", parser_to_strbuilder(_ctx->parser) ); - parser_pop(& _ctx->parser); - return InvalidCode; - } - goto Member_Resolved_To_Lone_Macro; - } - - // We have a macro but its most likely behaving as a typename - // ... } @@ -2943,7 +2919,7 @@ CodePreprocessCond parse_preprocess_cond() } internal -Code parse_simple_preprocess( TokType which, bool dont_consume_braces ) +Code parse_simple_preprocess( TokType which ) { // TODO(Ed): We can handle a macro a bit better than this. It's AST can be made more robust.. // Make an AST_Macro, it should have an Name be the macro itself, with the function body being an optional function body node. @@ -2954,7 +2930,7 @@ Code parse_simple_preprocess( TokType which, bool dont_consume_braces ) eat( which ); // - PreprocessorMacro macro = * lookup_preprocess_macro( name.Text ); + PreprocessorMacro macro = * lookup_preprocess_macro( full_macro.Text ); if ( macro_expects_body(macro) && peektok.Type == Tok_BraceCurly_Open ) { @@ -3043,7 +3019,7 @@ Code parse_simple_preprocess( TokType which, bool dont_consume_braces ) Leave_Scope_Early: Code result = untyped_str( full_macro.Text ); - _ctx->parser.Scope->Name = tok.Text; + _ctx->parser.Scope->Name = full_macro.Text; parser_pop(& _ctx->parser); return result; @@ -3145,7 +3121,7 @@ CodeVar parse_variable_after_name( Code array_expr = parse_array_decl(); Code expr = NullCode; - Code bitfield_expr = NulLCode; + Code bitfield_expr = NullCode; b32 using_constructor_initializer = false; @@ -3635,13 +3611,13 @@ CodeEnum parser_parse_enum( bool inplace_def ) } // enum : } - else if ( currtok.Type == Tok_Preprocess_Macro ) + else if ( currtok.Type == Tok_Preprocess_Macro_Expr ) { // We'll support the enum_underlying macro - if ( str_contains( tok_to_str(currtok), enum_underlying_sig) ) + if ( str_contains( tok_to_str(currtok), enum_underlying_macro.Name) ) { use_macro_underlying = true; - underlying_macro = parse_simple_preprocess( Tok_Preprocess_Macro, parser_dont_consume_braces ); + underlying_macro = parse_simple_preprocess( Tok_Preprocess_Macro_Typename ); } } @@ -5226,7 +5202,7 @@ CodeUnion parser_parse_union( bool inplace_def ) break; case Tok_Preprocess_Macro_Stmt: - member = parse_simple_preprocess( Tok_Preprocess_Macro ); + member = parse_simple_preprocess( Tok_Preprocess_Macro_Stmt ); break; case Tok_Preprocess_Pragma: @@ -5234,7 +5210,7 @@ CodeUnion parser_parse_union( bool inplace_def ) break; case Tok_Preprocess_Unsupported: - member = parse_simple_preprocess( Tok_Preprocess_Unsupported, parser_consume_braces ); + member = parse_simple_preprocess( Tok_Preprocess_Unsupported ); break; default: diff --git a/base/components/parser_types.hpp b/base/components/parser_types.hpp index c7eeae3..f198aa9 100644 --- a/base/components/parser_types.hpp +++ b/base/components/parser_types.hpp @@ -4,6 +4,7 @@ #include "gen/ecode.hpp" #include "gen/eoperator.hpp" #include "gen/especifier.hpp" +#include "gen/etoktype.hpp" #endif enum TokFlags : u32 @@ -135,11 +136,11 @@ enum MacroType : u16 MT_Block_End, // Not Supported yet MT_Case_Statement, // Not Supported yet - MF_UnderlyingType = GEN_U16_Max, + MT_UnderlyingType = GEN_U16_MAX, }; forceinline -TokenType macrotype_to_toktype( MacroType type ) { +TokType macrotype_to_toktype( MacroType type ) { switch ( type ) { case MT_Statement : return Tok_Preprocess_Macro_Stmt; case MT_Expression : return Tok_Preprocess_Macro_Expr; @@ -152,7 +153,7 @@ TokenType macrotype_to_toktype( MacroType type ) { Str macrotype_to_str( MacroType type ) { local_persist - Str lookup[ (u32)Num_ModuleFlags ] = { + Str lookup[] = { { "Statement", sizeof("Statement") - 1 }, { "Expression", sizeof("Expression") - 1 }, { "Typename", sizeof("Typename") - 1 }, @@ -161,21 +162,22 @@ Str macrotype_to_str( MacroType type ) { "Case_Statement", sizeof("Case_Statement") - 1 }, }; local_persist - Str invalid_flag = { "Invalid", sizeof("Invalid") }; - if ( flag > ModuleFlag_Import ) - return invalid_flag; + Str invalid = { "Invalid", sizeof("Invalid") }; + if ( type > MT_Case_Statement ) + return invalid; - return lookup[ (u32)flag ]; + return lookup[ type ]; } -enum MacroFlags : u16 +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_Null = 0, - MF_UnderlyingType = GEN_U16_Max, + MF_UnderlyingType = GEN_U16_MAX, }; +typedef u16 MacroFlags; struct PreprocessorMacro { @@ -184,12 +186,12 @@ struct PreprocessorMacro MacroFlags Flags; }; -forceinine +forceinline b32 macro_is_functional( PreprocessorMacro macro ) { - return bitfield_is_set( macro->Flags, MF_Functional ); + return bitfield_is_set( b16, macro.Flags, MF_Functional ); } forceinline b32 macro_expects_body( PreprocessorMacro macro ) { - return bitfield_is_set( macro->Flags, MF_Expects_Body ); -} \ No newline at end of file + return bitfield_is_set( b16, macro.Flags, MF_Expects_Body ); +} diff --git a/base/helpers/base_codegen.hpp b/base/helpers/base_codegen.hpp index 90fb32a..1a405f3 100644 --- a/base/helpers/base_codegen.hpp +++ b/base/helpers/base_codegen.hpp @@ -47,7 +47,7 @@ CodeBody gen_ecode( char const* path, bool use_c_definition = false ) Str codetype_to_str( CodeType type ) { local_persist - Str lookup[] = { + Str lookup[] = { }; return lookup[ type ]; @@ -57,7 +57,7 @@ CodeBody gen_ecode( char const* path, bool use_c_definition = false ) Str codetype_to_keyword_str( CodeType type ) { local_persist - Str lookup[ ] = { + Str lookup[] = { }; return lookup[ type ]; @@ -139,7 +139,7 @@ CodeBody gen_eoperator( char const* path, bool use_c_definition = false ) Str operator_to_str( Operator op ) { local_persist - Str lookup[] = { + Str lookup[] = { }; @@ -353,7 +353,7 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path, bool use_c_ #pragma push_macro("GEN_DEFINE_ATTRIBUTE_TOKENS") #undef GEN_DEFINE_ATTRIBUTE_TOKENS - CodeDefine attribute_entires_def = def_define( name(GEN_DEFINE_ATTRIBUTE_TOKENS), strbuilder_to_str(attribute_define_entries) ); + CodeDefine attribute_entires_def = def_define( name(GEN_DEFINE_ATTRIBUTE_TOKENS), MT_Statement, { {}, strbuilder_to_str(attribute_define_entries) } ); #pragma pop_macro("GEN_DEFINE_ATTRIBUTE_TOKENS") // We cannot parse this enum, it has Attribute names as enums