compiling again...

This commit is contained in:
Edward R. Gonzalez 2024-12-14 21:21:13 -05:00
parent 1b4f9a2e77
commit f93250da07
15 changed files with 197 additions and 200 deletions

View File

@ -151,7 +151,7 @@ struct AST_Define
char _PAD_PROPERTIES_ [ sizeof(AST*) * 4 ]; char _PAD_PROPERTIES_ [ sizeof(AST*) * 4 ];
CodeDefineParams Params; CodeDefineParams Params;
Code Body; // Should be completely serialized for now to a: StrCached Content. 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; StrCached Name;

View File

@ -252,7 +252,7 @@ void define_to_strbuilder_ref(CodeDefine define, StrBuilder* result )
GEN_ASSERT(define->Body); GEN_ASSERT(define->Body);
GEN_ASSERT(define->Body->Content); GEN_ASSERT(define->Body->Content);
if (define->Params) { 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 ); strbuilder_append_fmt( result, "#define %S(%S) %S", define->Name, strbuilder_to_str(params_builder), define->Body->Content );
} }
else { else {
@ -268,7 +268,7 @@ StrBuilder define_params_to_strbuilder(CodeDefineParams params)
return result; 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(self);
GEN_ASSERT(result); GEN_ASSERT(result);
@ -278,9 +278,9 @@ void define_params_to_strbuilder_ref(CodeDefineParams params, StrBuilder* result
} }
if ( self->NumEntries - 1 > 0 ) 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) );
} }
} }
} }

View File

@ -232,15 +232,15 @@ struct CodeDefineParams
forceinline void to_strbuilder( StrBuilder& result ) { return define_params_to_strbuilder_ref(* this, & result); } forceinline void to_strbuilder( StrBuilder& result ) { return define_params_to_strbuilder_ref(* this, & result); }
#endif #endif
Using_CodeOps( CodeDefineParams ); Using_CodeOps( CodeDefineParams );
forceinline CodeDefineParams begin() { return begin_CodeParams( cast(CodeParams, * this)); } forceinline CodeDefineParams begin() { return (CodeDefineParams) (Code) begin_CodeParams( cast(CodeParams, * this)); }
forceinline CodeDefineParams end() { return end_CodeParams( cast(CodeParams, * this)); } forceinline CodeDefineParams end() { return (CodeDefineParams) (Code) end_CodeParams( cast(CodeParams, * this)); }
forceinline operator Code() { return { (AST*)ast }; } forceinline operator Code() { return { (AST*)ast }; }
forceinline CodeDefineParams operator *() { return * this; } // Required to support for-range iteration. forceinline CodeDefineParams operator *() { return * this; } // Required to support for-range iteration.
forceinline AST_DefineParams* operator->() { forceinline AST_DefineParams* operator->() {
GEN_ASSERT(ast); GEN_ASSERT(ast);
return ast; return ast;
} }
forceinline CodeDefineParams& operator++() { return cast(CodeParams, * this).operator ++() }; forceinline CodeDefineParams& operator++() { return (CodeDefineParams) (Code) cast(CodeParams, * this).operator ++(); };
AST_DefineParams* ast; AST_DefineParams* ast;
}; };

View File

@ -179,6 +179,22 @@ inline AST_Define* CodeDefine::operator->()
return ast; 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 ) inline CodeDestructor& CodeDestructor::operator=( Code other )
{ {
if ( other.ast != nullptr && other->Parent != nullptr ) if ( other.ast != nullptr && other->Parent != nullptr )
@ -851,6 +867,11 @@ forceinline Code::operator CodeDefine() const
return { (AST_Define*)ast }; return { (AST_Define*)ast };
} }
forceinline Code::operator CodeDefineParams() const
{
return { (AST_DefineParams*)ast };
}
forceinline Code::operator CodeDestructor() const forceinline Code::operator CodeDestructor() const
{ {
return { (AST_Destructor*)ast }; return { (AST_Destructor*)ast };

View File

@ -75,7 +75,7 @@ enum CodeType : u32
inline Str codetype_to_str( CodeType type ) inline Str codetype_to_str( CodeType type )
{ {
local_persist Str lookup[61] = { local_persist Str lookup[] = {
{ "Invalid", sizeof( "Invalid" ) - 1 }, { "Invalid", sizeof( "Invalid" ) - 1 },
{ "Untyped", sizeof( "Untyped" ) - 1 }, { "Untyped", sizeof( "Untyped" ) - 1 },
{ "NewLine", sizeof( "NewLine" ) - 1 }, { "NewLine", sizeof( "NewLine" ) - 1 },
@ -144,7 +144,7 @@ inline Str codetype_to_str( CodeType type )
inline Str codetype_to_keyword_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 }, { "__NA__", sizeof( "__NA__" ) - 1 },
{ "__NA__", sizeof( "__NA__" ) - 1 }, { "__NA__", sizeof( "__NA__" ) - 1 },

View File

@ -60,7 +60,7 @@ enum Operator : u32
inline Str operator_to_str( Operator op ) inline Str operator_to_str( Operator op )
{ {
local_persist Str lookup[47] = { local_persist Str lookup[] = {
{ "INVALID", sizeof( "INVALID" ) - 1 }, { "INVALID", sizeof( "INVALID" ) - 1 },
{ "=", sizeof( "=" ) - 1 }, { "=", sizeof( "=" ) - 1 },
{ "+=", sizeof( "+=" ) - 1 }, { "+=", sizeof( "+=" ) - 1 },

View File

@ -158,7 +158,7 @@ inline Str toktype_to_str( TokType type )
{ "__operator__", sizeof( "__operator__" ) - 1 }, { "__operator__", sizeof( "__operator__" ) - 1 },
{ "#", sizeof( "#" ) - 1 }, { "#", sizeof( "#" ) - 1 },
{ "define", sizeof( "define" ) - 1 }, { "define", sizeof( "define" ) - 1 },
{ "__define_param__", sizeof( "__define__param__" ) - 1 }, { "__define_param__", sizeof( "__define_param__" ) - 1 },
{ "if", sizeof( "if" ) - 1 }, { "if", sizeof( "if" ) - 1 },
{ "ifdef", sizeof( "ifdef" ) - 1 }, { "ifdef", sizeof( "ifdef" ) - 1 },
{ "ifndef", sizeof( "ifndef" ) - 1 }, { "ifndef", sizeof( "ifndef" ) - 1 },

View File

@ -259,12 +259,12 @@ CodeParams next_CodeParams(CodeParams params, CodeParams param_iter)
#pragma region CodeDefineParams #pragma region CodeDefineParams
forceinline void define_params_append (CodeDefineParams appendee, CodeDefineParams other ) { params_append( cast(CodeParams, appendee), cast(CodeParams, other) ); } 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)); } 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 begin_CodeDefineParams(CodeDefineParams params) { return (CodeDefineParams) (Code) begin_CodeParams( cast(CodeParams, (Code)params)); }
CodeDefineParams end_CodeDefineParams (CodeDefineParams params) { return end_CodeParams ( cast(CodeParams, params)); } CodeDefineParams end_CodeDefineParams (CodeDefineParams params) { return (CodeDefineParams) (Code) end_CodeParams ( cast(CodeParams, (Code)params)); }
CodeDefineParams next_CodeDefineParams (CodeDefineParams params, CodeDefineParams entry_iter) { return next_CodeParams ( cast(CodeParams, params), cast(CoeParams, entry_iter)); } 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 endregion CodeDefineParams
#pragma region CodeSpecifiers #pragma region CodeSpecifiers

View File

@ -272,8 +272,8 @@ void init(Context* ctx)
ctx->CodePool_NumBlocks = kilobytes(16); ctx->CodePool_NumBlocks = kilobytes(16);
} }
if (ctx->InitSize_LexArena == 0 ) { if (ctx->InitSize_LexerTokens == 0 ) {
ctx->InitSize_LexArena = megabytes(4); ctx->InitSize_LexerTokens = kilobytes(64);
} }
if (ctx->SizePer_StringArena == 0) { if (ctx->SizePer_StringArena == 0) {
ctx->SizePer_StringArena = megabytes(1); ctx->SizePer_StringArena = megabytes(1);
@ -315,7 +315,7 @@ void init(Context* ctx)
if ( ctx->StrCache.Entries == nullptr ) if ( ctx->StrCache.Entries == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the StringCache"); 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) { if (ctx->PreprocessorMacros.Hashes == nullptr || ctx->PreprocessorMacros.Entries == nullptr) {
GEN_FATAL( "gen::init: Failed to initialize the PreprocessMacros table" ); GEN_FATAL( "gen::init: Failed to initialize the PreprocessMacros table" );
} }

View File

@ -60,7 +60,7 @@ struct Context
u32 CodePool_NumBlocks; u32 CodePool_NumBlocks;
// TODO(Ed): Review these... (No longer needed if using the proper allocation strategy) // TODO(Ed): Review these... (No longer needed if using the proper allocation strategy)
u32 InitSize_LexArena; u32 InitSize_LexerTokens;
u32 SizePer_StringArena; u32 SizePer_StringArena;
// TODO(Ed): Symbol Table // TODO(Ed): Symbol Table
@ -87,9 +87,6 @@ struct Context
StringTable StrCache; StringTable StrCache;
// TODO(Ed): This needs to be just handled by a parser context // TODO(Ed): This needs to be just handled by a parser context
// Arena LexArena;
// StringTable Lexer_defines;
Array(Token) Lexer_Tokens; Array(Token) Lexer_Tokens;
// TODO(Ed): Active parse context vs a parse result need to be separated conceptually // 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); GEN_API void set_context(Context* ctx);
// Mostly used to verify a macro entry exists for the given name // Mostly intended for the parser
GEN_API PreprocessMacro* lookup_preprocess_macro( Str Name ); GEN_API PreprocessorMacro* lookup_preprocess_macro( Str Name );
// Alternative way to add a preprocess define entry for the lexer & parser to utilize // Alternative way to add a preprocess define entry for the lexer & parser to utilize
// if the user doesn't want to use def_define // 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 ); GEN_API CodeConstructor def_constructor( Opts_def_constructor opts GEN_PARAM_DEFAULT );
struct Opts_def_define { struct Opts_def_define {
MacroFlags flags;
CodeDefineParams params; CodeDefineParams params;
Str content; Str content;
MacroFlags flags;
b32 dont_register_to_preprocess_macros; b32 dont_register_to_preprocess_macros;
}; };
GEN_API CodeDefine def_define( Str name, MacroType type, Opts_def_define opts GEN_PARAM_DEFAULT ); 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, ... );
GEN_API CodeBody def_class_body ( s32 num, Code* codes ); 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, ... );
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, ... );
GEN_API CodeBody def_enum_body ( s32 num, Code* codes ); GEN_API CodeBody def_enum_body ( s32 num, Code* codes );
GEN_API CodeBody def_export_body ( s32 num, ... ); GEN_API CodeBody def_export_body ( s32 num, ... );

View File

@ -572,13 +572,13 @@ CodeDefine def_define( Str name, MacroType type, Opts_def_define p )
result->Name = cache_str( name ); result->Name = cache_str( name );
result->Params = p.params; result->Params = p.params;
if ( p.content.Len <= 0 || p.content.Ptr == nullptr ) if ( p.content.Len <= 0 || p.content.Ptr == nullptr )
result->Content = cache_str( txt("\n") ); result->Body = untyped_str( txt("\n") );
else 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; b32 register_define = ! p.dont_register_to_preprocess_macros;
if ( register_define ) { if ( register_define ) {
macro_entry = { result->Name, type, p.flags }; PreprocessorMacro macro_entry = { result->Name, type, p.flags };
register_preprocess_macro(macro_entry); register_preprocess_macro(macro_entry);
} }
return result; return result;

View File

@ -118,7 +118,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
Token name = { { ctx->scanner, 1 }, Tok_Identifier, ctx->line, ctx->column, TF_Preprocess }; Token name = { { ctx->scanner, 1 }, Tok_Identifier, ctx->line, ctx->column, TF_Preprocess };
move_forward(); 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); PreprocessorMacro* registered_macro = lookup_preprocess_macro(name.Text);
if ( registered_macro == nullptr ) { 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" 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 ); array_append( _ctx->Lexer_Tokens, opening_paren );
move_forward(); move_forward();
Token last_parameter;
// We need to tokenize the define's arguments now: // We need to tokenize the define's arguments now:
while( ctx->left && * ctx->scanner != ')') while( ctx->left && * ctx->scanner != ')')
{ {
@ -162,6 +163,7 @@ s32 lex_preprocessor_define( LexContext* ctx )
} }
array_append(_ctx->Lexer_Tokens, parameter); array_append(_ctx->Lexer_Tokens, parameter);
skip_whitespace(); skip_whitespace();
last_parameter = parameter;
} }
else { else {
log_failure("lex_preprocessor_define(%d, %d): Expected a '_' or alpha character for a parameter name for %S\n" 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" log_failure("lex_preprocessor_define(%d, %d): Expected a comma after parameter %S for %S\n"
, ctx->line , ctx->line
, ctx->column , ctx->column
, parameter.Text , last_parameter.Text
, name.Text , name.Text
); );
return Lex_ReturnNull; 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" log_failure("lex_preprocessor_define(%d, %d): Expected a ')' after last_parameter %S for %S (ran out of characters...)\n"
, ctx->line , ctx->line
, ctx->column , ctx->column
, parameter.Text , last_parameter.Text
, name.Text , name.Text
); );
return Lex_ReturnNull; return Lex_ReturnNull;
@ -527,7 +529,6 @@ TokArray lex( Str content )
c.content = content; c.content = content;
c.left = content.Len; c.left = content.Len;
c.scanner = content.Ptr; c.scanner = content.Ptr;
c.defines = _ctx->Lexer_defines;
char const* word = c.scanner; char const* word = c.scanner;
s32 word_length = 0; s32 word_length = 0;

View File

@ -118,11 +118,7 @@ bool lex__eat(TokArray* self, TokType type )
internal internal
void parser_init() void parser_init()
{ {
_ctx->Lexer_Tokens = array_init_reserve(Token, arena_allocator_info( & _ctx->LexArena) _ctx->Lexer_Tokens = array_init_reserve(Token, _ctx->Allocator_DyanmicContainers, _ctx->InitSize_LexerTokens );
, ( _ctx->InitSize_LexArena - sizeof( ArrayHeader ) ) / sizeof(Token)
);
_ctx->Lexer_defines = hashtable_init_reserve(Str, _ctx->Allocator_DyanmicContainers, 256 );
} }
internal internal
@ -942,7 +938,6 @@ CodeBody parse_class_struct_body( TokType which, Token name )
member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Macro_Stmt )); member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Macro_Stmt ));
break; break;
} }
case Tok_Preprocess_Macro_Expr: { case Tok_Preprocess_Macro_Expr: {
log_failure("Unbounded macro expression residing in class/struct body\n%S", parser_to_strbuilder(_ctx->parser)); log_failure("Unbounded macro expression residing in class/struct body\n%S", parser_to_strbuilder(_ctx->parser));
return InvalidCode; return InvalidCode;
@ -961,7 +956,7 @@ CodeBody parse_class_struct_body( TokType which, Token name )
} }
case Tok_Preprocess_Unsupported: { 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 ));
// #<UNKNOWN> // #<UNKNOWN>
break; break;
} }
@ -1317,10 +1312,10 @@ CodeDefine parse_define()
CodeDefineParams params; CodeDefineParams params;
if ( left && currtok.Type != Tok_Capture_End ) { if ( left && currtok.Type != Tok_Capture_End ) {
params = (CodeDefineParams) make_code(); params = (CodeDefineParams) make_code();
params.Type = CT_Parameters_Define; params->Type = CT_Parameters_Define;
params.Name = currtok.text; params->Name = currtok.Text;
define->params = params; define->Params = params;
eat( Tok_Preprocess_Define_Param ); eat( Tok_Preprocess_Define_Param );
// #define <Name> ( <param> ) // #define <Name> ( <param> )
@ -1328,8 +1323,8 @@ CodeDefine parse_define()
while( left && currtok.Type != Tok_Capture_End ) { while( left && currtok.Type != Tok_Capture_End ) {
CodeDefineParams next_param = (CodeDefineParams) make_code(); CodeDefineParams next_param = (CodeDefineParams) make_code();
next_param.Type = CT_Parameters_Define; next_param->Type = CT_Parameters_Define;
next_param.Name = currtok.Text; next_param->Name = currtok.Text;
define_params_append(params, next_param); define_params_append(params, next_param);
// #define <Name> ( <param>, <next_param> ... // #define <Name> ( <param>, <next_param> ...
@ -1349,7 +1344,7 @@ CodeDefine parse_define()
if ( currtok.Text.Len == 0 ) 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 ); eat( Tok_Preprocess_Content );
// #define <Name> ( <params> ) <Content> // #define <Name> ( <params> ) <Content>
@ -1357,7 +1352,7 @@ CodeDefine parse_define()
return 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 ); eat( Tok_Preprocess_Content );
// #define <Name> ( <params> ) <Content> // #define <Name> ( <params> ) <Content>
@ -1729,11 +1724,13 @@ CodeBody parse_global_nspace( CodeType which )
// #endif // #endif
break; break;
case Tok_Preprocess_Macro: { case Tok_Preprocess_Macro_Stmt: {
// <Macro> member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Macro_Stmt ));
macro_found = true; break;
goto Preprocess_Macro_Bare_In_Body; }
// TODO(Ed): MACRO UPDATE 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: { case Tok_Preprocess_Pragma: {
@ -1743,7 +1740,7 @@ CodeBody parse_global_nspace( CodeType which )
break; break;
case Tok_Preprocess_Unsupported: { 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 ));
// #<UNSUPPORTED> ... // #<UNSUPPORTED> ...
} }
break; break;
@ -1828,6 +1825,7 @@ CodeBody parse_global_nspace( CodeType which )
} }
//! Fallthrough intentional //! Fallthrough intentional
case Tok_Identifier: case Tok_Identifier:
case Tok_Preprocess_Macro_Typename:
case Tok_Spec_Const: case Tok_Spec_Const:
case Tok_Type_Long: case Tok_Type_Long:
case Tok_Type_Short: 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 );
// <Macro>;
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
// <Macro ...
}
member = parse_operator_function_or_variable( expects_function, attributes, specifiers ); member = parse_operator_function_or_variable( expects_function, attributes, specifiers );
// <Attributes> <Specifiers> ... // <Attributes> <Specifiers> ...
} }
@ -2943,7 +2919,7 @@ CodePreprocessCond parse_preprocess_cond()
} }
internal 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.. // 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. // 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 ); eat( which );
// <Macro> // <Macro>
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 ) 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: Leave_Scope_Early:
Code result = untyped_str( full_macro.Text ); Code result = untyped_str( full_macro.Text );
_ctx->parser.Scope->Name = tok.Text; _ctx->parser.Scope->Name = full_macro.Text;
parser_pop(& _ctx->parser); parser_pop(& _ctx->parser);
return result; return result;
@ -3145,7 +3121,7 @@ CodeVar parse_variable_after_name(
Code array_expr = parse_array_decl(); Code array_expr = parse_array_decl();
Code expr = NullCode; Code expr = NullCode;
Code bitfield_expr = NulLCode; Code bitfield_expr = NullCode;
b32 using_constructor_initializer = false; b32 using_constructor_initializer = false;
@ -3635,13 +3611,13 @@ CodeEnum parser_parse_enum( bool inplace_def )
} }
// enum <class> <Attributes> <Name> : <UnderlyingType> // enum <class> <Attributes> <Name> : <UnderlyingType>
} }
else if ( currtok.Type == Tok_Preprocess_Macro ) else if ( currtok.Type == Tok_Preprocess_Macro_Expr )
{ {
// We'll support the enum_underlying macro // 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; 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; break;
case Tok_Preprocess_Macro_Stmt: case Tok_Preprocess_Macro_Stmt:
member = parse_simple_preprocess( Tok_Preprocess_Macro ); member = parse_simple_preprocess( Tok_Preprocess_Macro_Stmt );
break; break;
case Tok_Preprocess_Pragma: case Tok_Preprocess_Pragma:
@ -5234,7 +5210,7 @@ CodeUnion parser_parse_union( bool inplace_def )
break; break;
case Tok_Preprocess_Unsupported: case Tok_Preprocess_Unsupported:
member = parse_simple_preprocess( Tok_Preprocess_Unsupported, parser_consume_braces ); member = parse_simple_preprocess( Tok_Preprocess_Unsupported );
break; break;
default: default:

View File

@ -4,6 +4,7 @@
#include "gen/ecode.hpp" #include "gen/ecode.hpp"
#include "gen/eoperator.hpp" #include "gen/eoperator.hpp"
#include "gen/especifier.hpp" #include "gen/especifier.hpp"
#include "gen/etoktype.hpp"
#endif #endif
enum TokFlags : u32 enum TokFlags : u32
@ -135,11 +136,11 @@ enum MacroType : u16
MT_Block_End, // Not Supported yet MT_Block_End, // Not Supported yet
MT_Case_Statement, // Not Supported yet MT_Case_Statement, // Not Supported yet
MF_UnderlyingType = GEN_U16_Max, MT_UnderlyingType = GEN_U16_MAX,
}; };
forceinline forceinline
TokenType macrotype_to_toktype( MacroType type ) { TokType macrotype_to_toktype( MacroType type ) {
switch ( type ) { switch ( type ) {
case MT_Statement : return Tok_Preprocess_Macro_Stmt; case MT_Statement : return Tok_Preprocess_Macro_Stmt;
case MT_Expression : return Tok_Preprocess_Macro_Expr; case MT_Expression : return Tok_Preprocess_Macro_Expr;
@ -152,7 +153,7 @@ TokenType macrotype_to_toktype( MacroType type ) {
Str macrotype_to_str( MacroType type ) Str macrotype_to_str( MacroType type )
{ {
local_persist local_persist
Str lookup[ (u32)Num_ModuleFlags ] = { Str lookup[] = {
{ "Statement", sizeof("Statement") - 1 }, { "Statement", sizeof("Statement") - 1 },
{ "Expression", sizeof("Expression") - 1 }, { "Expression", sizeof("Expression") - 1 },
{ "Typename", sizeof("Typename") - 1 }, { "Typename", sizeof("Typename") - 1 },
@ -161,21 +162,22 @@ Str macrotype_to_str( MacroType type )
{ "Case_Statement", sizeof("Case_Statement") - 1 }, { "Case_Statement", sizeof("Case_Statement") - 1 },
}; };
local_persist local_persist
Str invalid_flag = { "Invalid", sizeof("Invalid") }; Str invalid = { "Invalid", sizeof("Invalid") };
if ( flag > ModuleFlag_Import ) if ( type > MT_Case_Statement )
return invalid_flag; 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_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_Expects_Body = bit(1), // Expects to assign a braced scope to its body.
MF_Null = 0, MF_Null = 0,
MF_UnderlyingType = GEN_U16_Max, MF_UnderlyingType = GEN_U16_MAX,
}; };
typedef u16 MacroFlags;
struct PreprocessorMacro struct PreprocessorMacro
{ {
@ -184,12 +186,12 @@ struct PreprocessorMacro
MacroFlags Flags; MacroFlags Flags;
}; };
forceinine forceinline
b32 macro_is_functional( PreprocessorMacro macro ) { b32 macro_is_functional( PreprocessorMacro macro ) {
return bitfield_is_set( macro->Flags, MF_Functional ); return bitfield_is_set( b16, macro.Flags, MF_Functional );
} }
forceinline forceinline
b32 macro_expects_body( PreprocessorMacro macro ) { b32 macro_expects_body( PreprocessorMacro macro ) {
return bitfield_is_set( macro->Flags, MF_Expects_Body ); return bitfield_is_set( b16, macro.Flags, MF_Expects_Body );
} }

View File

@ -47,7 +47,7 @@ CodeBody gen_ecode( char const* path, bool use_c_definition = false )
Str codetype_to_str( CodeType type ) Str codetype_to_str( CodeType type )
{ {
local_persist local_persist
Str lookup[<num>] = { Str lookup[] = {
<entries> <entries>
}; };
return lookup[ type ]; 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 ) Str codetype_to_keyword_str( CodeType type )
{ {
local_persist local_persist
Str lookup[ <num> ] = { Str lookup[] = {
<keywords> <keywords>
}; };
return lookup[ type ]; return lookup[ type ];
@ -139,7 +139,7 @@ CodeBody gen_eoperator( char const* path, bool use_c_definition = false )
Str operator_to_str( Operator op ) Str operator_to_str( Operator op )
{ {
local_persist local_persist
Str lookup[<num>] = { Str lookup[] = {
<entries> <entries>
}; };
@ -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") #pragma push_macro("GEN_DEFINE_ATTRIBUTE_TOKENS")
#undef 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") #pragma pop_macro("GEN_DEFINE_ATTRIBUTE_TOKENS")
// We cannot parse this enum, it has Attribute names as enums // We cannot parse this enum, it has Attribute names as enums