C-library gen progress: Header files mostly done, starting dep c impl and fixes to generic selection generation

This commit is contained in:
2024-12-08 16:37:04 -05:00
parent c016e245eb
commit 65c3fabc52
26 changed files with 732 additions and 445 deletions

View File

@ -6,6 +6,18 @@
#include "gen/especifier.hpp"
#endif
/*
______ ______ ________ __ __ ______ __
/ \ / \| \ | \ | \ / \ | \
| ▓▓▓▓▓▓\ ▓▓▓▓▓▓\\▓▓▓▓▓▓▓▓ | ▓▓\ | ▓▓ | ▓▓▓▓▓▓\ ______ ____| ▓▓ ______
| ▓▓__| ▓▓ ▓▓___\▓▓ | ▓▓ | ▓▓▓\| ▓▓ | ▓▓ \▓▓/ \ / ▓▓/ \
| ▓▓ ▓▓\▓▓ \ | ▓▓ | ▓▓▓▓\ ▓▓ | ▓▓ | ▓▓▓▓▓▓\ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓\
| ▓▓▓▓▓▓▓▓_\▓▓▓▓▓▓\ | ▓▓ | ▓▓\▓▓ ▓▓ | ▓▓ __| ▓▓ | ▓▓ ▓▓ | ▓▓ ▓▓ ▓▓
| ▓▓ | ▓▓ \__| ▓▓ | ▓▓ | ▓▓ \▓▓▓▓ | ▓▓__/ \ ▓▓__/ ▓▓ ▓▓__| ▓▓ ▓▓▓▓▓▓▓▓
| ▓▓ | ▓▓\▓▓ ▓▓ | ▓▓ | ▓▓ \▓▓▓ \▓▓ ▓▓\▓▓ ▓▓\▓▓ ▓▓\▓▓ \
\▓▓ \▓▓ \▓▓▓▓▓▓ \▓▓ \▓▓ \▓▓ \▓▓▓▓▓▓ \▓▓▓▓▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓
*/
struct AST;
struct AST_Body;
struct AST_Attributes;
@ -84,6 +96,7 @@ typedef AST* Code;
#else
struct Code;
#endif
Define_Code(Body);
// These are to offer ease of use and optionally strong type safety for the AST.
Define_Code(Attributes);
@ -150,6 +163,7 @@ Define_Code(Typedef);
Define_Code(Union);
Define_Code(Using);
Define_Code(Var);
#undef Define_Code
GEN_NS_PARSER_BEGIN
@ -172,6 +186,7 @@ template< class Type> forceinline Type tmpl_cast( Code self ) { return * rcast(
#endif
#pragma region Code C-Interface
void code_append (Code code, Code other );
char const* code_debug_str (Code code);
Code code_duplicate (Code code);
@ -185,6 +200,7 @@ String code_to_string (Code self );
void code_to_string_ptr(Code self, String* result );
char const* code_type_str (Code self );
bool code_validate_body(Code self );
#pragma endregion Code C-Interface
#if GEN_COMPILER_CPP
@ -298,10 +314,11 @@ constexpr static
int AST_ArrSpecs_Cap =
(
AST_POD_Size
- sizeof(Code)
- sizeof(StringCached)
- sizeof(AST*) * 3
- sizeof(Code) * 2
- sizeof(Token*)
- sizeof(AST*)
- sizeof(Code)
- sizeof(CodeType)
- sizeof(ModuleFlag)
- sizeof(u32)

View File

@ -4,6 +4,22 @@
#endif
#pragma region AST Types
/*
______ ______ ________ ________
/ \ / \| \ | \
| ▓▓▓▓▓▓\ ▓▓▓▓▓▓\\▓▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓▓__ __ ______ ______ _______
| ▓▓__| ▓▓ ▓▓___\▓▓ | ▓▓ | ▓▓ | \ | \/ \ / \ / \
| ▓▓ ▓▓\▓▓ \ | ▓▓ | ▓▓ | ▓▓ | ▓▓ ▓▓▓▓▓▓\ ▓▓▓▓▓▓\ ▓▓▓▓▓▓▓
| ▓▓▓▓▓▓▓▓_\▓▓▓▓▓▓\ | ▓▓ | ▓▓ | ▓▓ | ▓▓ ▓▓ | ▓▓ ▓▓ ▓▓\▓▓ \
| ▓▓ | ▓▓ \__| ▓▓ | ▓▓ | ▓▓ | ▓▓__/ ▓▓ ▓▓__/ ▓▓ ▓▓▓▓▓▓▓▓_\▓▓▓▓▓▓\
| ▓▓ | ▓▓\▓▓ ▓▓ | ▓▓ | ▓▓ \▓▓ ▓▓ ▓▓ ▓▓\▓▓ \ ▓▓
\▓▓ \▓▓ \▓▓▓▓▓▓ \▓▓ \▓▓ _\▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓\▓▓▓▓▓▓▓
| \__| ▓▓ ▓▓
\▓▓ ▓▓ ▓▓
\▓▓▓▓▓▓ \▓▓
*/
/*
Show only relevant members of the AST for its type.
AST* fields are replaced with Code types.
@ -12,7 +28,9 @@
struct AST_Body
{
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
Code Front;
Code Back;
@ -550,7 +568,9 @@ static_assert( sizeof(AST_Fn) == sizeof(AST), "ERROR: AST_Fn is not the same siz
struct AST_Module
{
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
union {
char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ];
};
StringCached Name;
Code Prev;
Code Next;
@ -693,7 +713,7 @@ static_assert( sizeof(AST_PreprocessCond) == sizeof(AST), "ERROR: AST_Preprocess
struct AST_Specifiers
{
Specifier ArrSpecs[ AST_ArrSpecs_Cap ];
Specifier ArrSpecs[ AST_ArrSpecs_Cap ];
StringCached Name;
CodeSpecifiers NextSpecs;
Code Prev;

View File

@ -3,7 +3,20 @@
#include "ast.hpp"
#endif
/*
______ __ ______ __ ______
/ \ | \ | \ | \ / \
| ▓▓▓▓▓▓\ ______ ____| ▓▓ ______ \▓▓▓▓▓▓_______ _| ▓▓_ ______ ______ | ▓▓▓▓▓▓\ ______ _______ ______
| ▓▓ \▓▓/ \ / ▓▓/ \ | ▓▓ | \| ▓▓ \ / \ / \| ▓▓_ \▓▓| \ / \/ \
| ▓▓ | ▓▓▓▓▓▓\ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓\ | ▓▓ | ▓▓▓▓▓▓▓\\▓▓▓▓▓▓ | ▓▓▓▓▓▓\ ▓▓▓▓▓▓\ ▓▓ \ \▓▓▓▓▓▓\ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓\
| ▓▓ __| ▓▓ | ▓▓ ▓▓ | ▓▓ ▓▓ ▓▓ | ▓▓ | ▓▓ | ▓▓ | ▓▓ __| ▓▓ ▓▓ ▓▓ \▓▓ ▓▓▓▓ / ▓▓ ▓▓ | ▓▓ ▓▓
| ▓▓__/ \ ▓▓__/ ▓▓ ▓▓__| ▓▓ ▓▓▓▓▓▓▓▓ _| ▓▓_| ▓▓ | ▓▓ | ▓▓| \ ▓▓▓▓▓▓▓▓ ▓▓ | ▓▓ | ▓▓▓▓▓▓▓ ▓▓_____| ▓▓▓▓▓▓▓▓
\▓▓ ▓▓\▓▓ ▓▓\▓▓ ▓▓\▓▓ \ | ▓▓ \ ▓▓ | ▓▓ \▓▓ ▓▓\▓▓ \ ▓▓ | ▓▓ \▓▓ ▓▓\▓▓ \\▓▓ \
\▓▓▓▓▓▓ \▓▓▓▓▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓\▓▓ \▓▓ \▓▓▓▓ \▓▓▓▓▓▓▓\▓▓ \▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓
*/
#pragma region Code Type C-Interface
void body_append ( CodeBody body, Code other );
void body_append_body ( CodeBody body, CodeBody other );
String body_to_string ( CodeBody body );
@ -14,7 +27,7 @@ Code begin_CodeBody( CodeBody body);
Code end_CodeBody ( CodeBody body );
Code next_CodeBody ( CodeBody body, Code entry_iter );
void class_add_interface( CodeClass self, CodeType interface );
void class_add_interface( CodeClass self, CodeTypename interface );
String class_to_string ( CodeClass self );
void class_to_string_def( CodeClass self, String* result );
void class_to_string_fwd( CodeClass self, String* result );
@ -39,7 +52,7 @@ Specifier* begin_CodeSpecifiers(CodeSpecifiers specifiers);
Specifier* end_CodeSpecifiers (CodeSpecifiers specifiers);
Specifier* next_CodeSpecifiers (CodeSpecifiers specifiers, Specifier* spec_iter);
void struct_add_interface(CodeStruct self, CodeType interface);
void struct_add_interface(CodeStruct self, CodeTypename interface);
String struct_to_string (CodeStruct self);
void struct_to_string_fwd(CodeStruct self, String* result);
void struct_to_string_def(CodeStruct self, String* result);
@ -126,6 +139,7 @@ void using_to_string_ns (CodeUsing op_cast, String* result );
String var_to_string (CodeVar self);
void var_to_string_ref(CodeVar self, String* result);
#pragma endregion Code Type C-Interface
#if GEN_COMPILER_CPP
@ -214,24 +228,6 @@ struct CodeSpecifiers
AST_Specifiers* ast;
};
struct CodeStruct
{
#if ! GEN_C_LIKE_CPP
Using_Code( CodeStruct );
forceinline void add_interface( CodeType interface ) { return struct_add_interface(* this, interface); }
forceinline String to_string() { return struct_to_string(* this); }
forceinline void to_string_fwd( String& result ) { return struct_to_string_fwd(* this, & result); }
forceinline void to_string_def( String& result ) { return struct_to_string_def(* this, & result); }
#endif
Using_CodeOps( CodeStruct );
forceinline operator Code() { return * rcast( Code*, this ); }
forceinline AST_Struct* operator->() {
GEN_ASSERT(ast);
return ast;
}
AST_Struct* ast;
};
struct CodeAttributes
{
#if ! GEN_C_LIKE_CPP
@ -907,6 +903,24 @@ struct CodeVar
AST_Var* ast;
};
struct CodeStruct
{
#if ! GEN_C_LIKE_CPP
Using_Code( CodeStruct );
forceinline void add_interface( CodeTypename interface ) { return struct_add_interface(* this, interface); }
forceinline String to_string() { return struct_to_string(* this); }
forceinline void to_string_fwd( String& result ) { return struct_to_string_fwd(* this, & result); }
forceinline void to_string_def( String& result ) { return struct_to_string_def(* this, & result); }
#endif
Using_CodeOps( CodeStruct );
forceinline operator Code() { return * rcast( Code*, this ); }
forceinline AST_Struct* operator->() {
GEN_ASSERT(ast);
return ast;
}
AST_Struct* ast;
};
#undef Define_CodeType
#undef Using_Code
#undef Using_CodeOps
@ -982,6 +996,7 @@ struct NullCode_ImplicitCaster
#if ! GEN_C_LIKE_CPP
GEN_OPTIMIZE_MAPPINGS_BEGIN
forceinline void append ( CodeBody body, Code other ) { return body_append(body, other); }
forceinline void append ( CodeBody body, CodeBody other ) { return body_append_body(body, other); }
forceinline String to_string ( CodeBody body ) { return body_to_string(body); }
@ -992,7 +1007,7 @@ forceinline Code begin( CodeBody body) { return begin_CodeBody
forceinline Code end ( CodeBody body ) { return end_CodeBody(body); }
forceinline Code next ( CodeBody body, Code entry_iter ) { return next_CodeBody(body, entry_iter); }
forceinline void add_interface( CodeClass self, CodeType interface ) { return class_add_interface(self, interface); }
forceinline void add_interface( CodeClass self, CodeTypename interface ) { return class_add_interface(self, interface); }
forceinline String to_string ( CodeClass self ) { return class_to_string(self); }
forceinline void to_string_def( CodeClass self, String& result ) { return class_to_string_def(self, & result); }
forceinline void to_string_fwd( CodeClass self, String& result ) { return class_to_string_fwd(self, & result); }
@ -1017,10 +1032,10 @@ forceinline Specifier* begin(CodeSpecifiers specifiers) {
forceinline Specifier* end (CodeSpecifiers specifiers) { return end_CodeSpecifiers(specifiers); }
forceinline Specifier* next (CodeSpecifiers specifiers, Specifier& spec_iter) { return next_CodeSpecifiers(specifiers, & spec_iter); }
forceinline void add_interface(CodeStruct self, CodeType interface) { return struct_add_interface(self, interface); }
forceinline String to_string (CodeStruct self) { return struct_to_string(self); }
forceinline void to_string_fwd(CodeStruct self, String& result) { return struct_to_string_fwd(self, & result); }
forceinline void to_string_def(CodeStruct self, String& result) { return struct_to_string_def(self, & result); }
forceinline void add_interface(CodeStruct self, CodeTypename interface) { return struct_add_interface(self, interface); }
forceinline String to_string (CodeStruct self) { return struct_to_string(self); }
forceinline void to_string_fwd(CodeStruct self, String& result) { return struct_to_string_fwd(self, & result); }
forceinline void to_string_def(CodeStruct self, String& result) { return struct_to_string_def(self, & result); }
forceinline String to_string(CodeAttributes attributes) { return attributes_to_string(attributes); }
forceinline void to_string(CodeAttributes attributes, String& result) { return attributes_to_string_ref(attributes, & result); }
@ -1104,6 +1119,7 @@ forceinline void to_string_ns(CodeUsing op_cast, String& result ) { return usi
forceinline String to_string(CodeVar self) { return var_to_string(self); }
forceinline void to_string(CodeVar self, String& result) { return var_to_string_ref(self, & result); }
GEN_OPITMIZE_MAPPINGS_END
#endif //if GEN_C_LIKE_CPP

View File

@ -68,7 +68,8 @@ enum CodeType : u32
CT_Using,
CT_Using_Namespace,
CT_Variable,
CT_NumTypes
CT_NumTypes,
CT_UnderlyingType = GEN_U32_MAX
};
inline StrC codetype_to_str( CodeType type )

View File

@ -54,7 +54,8 @@ enum Operator : u32
Op_NewArray,
Op_Delete,
Op_DeleteArray,
NumOps
Op_NumOps,
Op_UnderlyingType = 0xffffffffu
};
inline StrC operator_to_str( Operator op )

View File

@ -33,7 +33,8 @@ enum Specifier : u32
Spec_Override,
Spec_Pure,
Spec_Volatile,
Spec_NumSpecifiers
Spec_NumSpecifiers,
Spec_UnderlyingType = 0xffffffffu
};
inline StrC spec_to_str( Specifier type )

View File

@ -6,13 +6,20 @@
See Readme.md for more information from the project repository.
Public Address:
https://github.com/Ed94/gencpp
! ----------------------------------------------------------------------- VERSION: v0.20-Alpha !
! ============================================================================================ !
! WARNING: THIS IS AN ALPHA VERSION OF THE LIBRARY, USE AT YOUR OWN DISCRETION !
! NEVER DO CODE GENERATION WITHOUT AT LEAST HAVING CONTENT IN A CODEBASE UNDER VERSION CONTROL !
! ============================================================================================ !
https://github.com/Ed94/gencpp --------------------------------------------------------------.
| _____ _____ _ _ |
| / ____) / ____} | | | |
| | / ___ ___ _ __ ___ _ __ _ __ | {___ | |__ _ _, __ _, ___ __| | |
| | |{_ |/ _ \ '_ \ / __} '_ l| '_ l `\___ \| __/ _` |/ _` |/ _ \/ _` | |
| | l__j | ___/ | | | {__; |+l } |+l | ____) | l| (_| | {_| | ___/ (_| | |
| \_____|\___}_l |_|\___} ,__/| ,__/ (_____/ \__\__/_|\__, |\___}\__,_l |
| | | | | __} | |
| l_l l_l {___/ |
! ----------------------------------------------------------------------- VERSION: v0.20-Alpha |
! ============================================================================================ |
! WARNING: THIS IS AN ALPHA VERSION OF THE LIBRARY, USE AT YOUR OWN DISCRETION |
! NEVER DO CODE GENERATION WITHOUT AT LEAST HAVING CONTENT IN A CODEBASE UNDER VERSION CONTROL |
! ============================================================================================ /
*/
#if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME)
# error Gen.hpp : GEN_TIME not defined

View File

@ -104,7 +104,7 @@ forceinline
char const* code_type_str(Code self)
{
GEN_ASSERT(self != nullptr);
return codetype_to_str( self->Type );
return codetype_to_str( self->Type ).Ptr;
}
#pragma endregion Code
@ -138,7 +138,8 @@ Code begin_CodeBody( CodeBody body) {
GEN_ASSERT(body);
if ( body != nullptr )
return body->Front;
return { nullptr };
return NullCode;
}
forceinline
Code end_CodeBody(CodeBody body ){
@ -170,7 +171,7 @@ void class_add_interface( CodeClass self, CodeTypename type )
while ( possible_slot != nullptr )
{
possible_slot = possible_slot->Next;
possible_slot = cast(CodeTypename, possible_slot->Next);
}
possible_slot = type;
@ -212,7 +213,7 @@ CodeParam params_get(CodeParam self, s32 idx )
do
{
if ( ++ param != nullptr )
return { nullptr };
return NullCode;
param = cast(CodeParam, cast(Code, param)->Next);
}
@ -246,7 +247,7 @@ forceinline
CodeParam end_CodeParam(CodeParam params)
{
// return { (AST_Param*) rcast( AST*, ast)->Last };
return { nullptr };
return NullCode;
}
forceinline
CodeParam next_CodeParam(CodeParam params, CodeParam param_iter)
@ -347,7 +348,7 @@ Specifier* next_CodeSpecifiers(CodeSpecifiers self, Specifier* spec_iter)
#pragma region CodeStruct
inline
void add_interface(CodeStruct self, CodeTypename type )
void struct_add_interface(CodeStruct self, CodeTypename type )
{
CodeTypename possible_slot = self->ParentType;
if ( possible_slot != nullptr )
@ -360,7 +361,7 @@ void add_interface(CodeStruct self, CodeTypename type )
while ( possible_slot != nullptr )
{
possible_slot = possible_slot->Next;
possible_slot = cast(CodeTypename, possible_slot->Next);
}
possible_slot = type;
@ -385,7 +386,7 @@ CodeBody def_body( CodeType type )
break;
default:
log_failure( "def_body: Invalid type %s", (char const*)codetype_to_str(type) );
log_failure( "def_body: Invalid type %s", codetype_to_str(type).Ptr );
return (CodeBody)Code_Invalid;
}
@ -407,6 +408,7 @@ StrC token_fmt_impl( ssize num, ... )
ssize result = token_fmt_va(buf, GEN_PRINTF_MAXLEN, num, va);
va_end(va);
return { result, buf };
StrC str = { result, buf };
return str;
}
#pragma endregion Interface

View File

@ -4,6 +4,16 @@
#endif
#pragma region Gen Interface
/*
/ \ | \ | \ / \
| ▓▓▓▓▓▓\ ______ _______ \▓▓▓▓▓▓_______ _| ▓▓_ ______ ______ | ▓▓▓▓▓▓\ ______ _______ ______
| ▓▓ __\▓▓/ \| \ | ▓▓ | \| ▓▓ \ / \ / \| ▓▓_ \▓▓| \ / \/ \
| ▓▓| \ ▓▓▓▓▓▓\ ▓▓▓▓▓▓▓\ | ▓▓ | ▓▓▓▓▓▓▓\\▓▓▓▓▓▓ | ▓▓▓▓▓▓\ ▓▓▓▓▓▓\ ▓▓ \ \▓▓▓▓▓▓\ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓\
| ▓▓ \▓▓▓▓ ▓▓ ▓▓ ▓▓ | ▓▓ | ▓▓ | ▓▓ | ▓▓ | ▓▓ __| ▓▓ ▓▓ ▓▓ \▓▓ ▓▓▓▓ / ▓▓ ▓▓ | ▓▓ ▓▓
| ▓▓__| ▓▓ ▓▓▓▓▓▓▓▓ ▓▓ | ▓▓ _| ▓▓_| ▓▓ | ▓▓ | ▓▓| \ ▓▓▓▓▓▓▓▓ ▓▓ | ▓▓ | ▓▓▓▓▓▓▓ ▓▓_____| ▓▓▓▓▓▓▓▓
\▓▓ ▓▓\▓▓ \ ▓▓ | ▓▓ | ▓▓ \ ▓▓ | ▓▓ \▓▓ ▓▓\▓▓ \ ▓▓ | ▓▓ \▓▓ ▓▓\▓▓ \\▓▓ \
\▓▓▓▓▓▓ \▓▓▓▓▓▓▓\▓▓ \▓▓ \▓▓▓▓▓▓\▓▓ \▓▓ \▓▓▓▓ \▓▓▓▓▓▓▓\▓▓ \▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓
*/
// Initialize the library.
// This currently just initializes the CodePool.
@ -164,7 +174,7 @@ struct Opts_def_variable
CodeVar def_variable( CodeTypename type, StrC name, Opts_def_variable opts GEN_PARAM_DEFAULT );
// Constructs an empty body. Use AST::validate_body() to check if the body is was has valid entries.
CodeBody def_body( CodeTypename type );
CodeBody def_body( CodeType type );
// There are two options for defining a struct body, either varadically provided with the args macro to auto-deduce the arg num,
/// or provide as an array of Code objects.
@ -199,23 +209,23 @@ CodeBody def_union_body ( s32 num, Code* codes );
// TODO(Ed) : Implmeent the new parser API design.
#if 0
namespace parser {
struct StackNode
{
StackNode* Prev;
GEN_NS_PARSER_BEGIN
struct StackNode
{
StackNode* Prev;
Token Start;
Token Name; // The name of the AST node (if parsed)
StrC FailedProc; // The name of the procedure that failed
};
// Stack nodes are allocated the error's allocator
Token Start;
Token Name; // The name of the AST node (if parsed)
StrC FailedProc; // The name of the procedure that failed
};
// Stack nodes are allocated the error's allocator
struct Error
{
String message;
StackNode* context_stack;
};
}
struct Error
{
String message;
StackNode* context_stack;
};
GEN_NS_PARSER_END
struct ParseInfo
{
@ -223,9 +233,9 @@ struct ParseInfo
Arena TokMem;
Arena CodeMem;
FileContents FileContent;
Array<parser::Token> Tokens;
Array<parser::Error> Errors;
FileContents FileContent;
Array<Token> Tokens;
Array<Error> Errors;
// Errors are allocated to a dedicated general arena.
};

View File

@ -1224,7 +1224,7 @@ CodeStruct def_struct( StrC name, Opts_def_struct p )
{
for (s32 idx = 0; idx < num_interfaces; idx++ )
{
add_interface(result, interfaces[idx] );
struct_add_interface(result, interfaces[idx] );
}
}

View File

@ -3010,15 +3010,21 @@ Code parse_simple_preprocess( TokType which, bool dont_consume_braces )
eat( Tok_Statement_End );
// <Macro>;
tok.Length += prevtok.Length;
// TODO(Ed): Reveiw the context for this? (ESPECIALLY THIS)
if ( currtok_noskip.Type == Tok_Comment && currtok_noskip.Line == stmt_end.Line )
{
eat( Tok_Comment );
// <Macro>; <InlineCmt>
tok.Length += prevtok.Length;
}
}
}
else if (
strc_contains(calling_proc, txt("parse_global_nspace"))
&& strc_contains(calling_proc, txt("parse_class_struct_body"))
|| strc_contains(calling_proc, txt("parse_class_struct_body"))
)
{
if (peektok.Type == Tok_Statement_End)
@ -3026,10 +3032,11 @@ Code parse_simple_preprocess( TokType which, bool dont_consume_braces )
Token stmt_end = currtok;
eat( Tok_Statement_End );
// <Macro>;
tok.Length += prevtok.Length;
}
}
tok.Length = ( (sptr)currtok_noskip.Text + currtok_noskip.Length ) - (sptr)tok.Text;
}
// tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)tok.Text;
}
Leave_Scope_Early:

View File

@ -3,6 +3,22 @@
#include "header_start.hpp"
#endif
/*
________ __ __ ________
| \ | \ | \ | \
| ▓▓▓▓▓▓▓▓_______ __ __ ______ ____ _______ | ▓▓\ | ▓▓ \▓▓▓▓▓▓▓▓__ __ ______ ______ _______
| ▓▓__ | \| \ | \ \ \ / \ | ▓▓▓\| ▓▓ | ▓▓ | \ | \/ \ / \ / \
| ▓▓ \ | ▓▓▓▓▓▓▓\ ▓▓ | ▓▓ ▓▓▓▓▓▓\▓▓▓▓\ ▓▓▓▓▓▓▓ | ▓▓▓▓\ ▓▓ | ▓▓ | ▓▓ | ▓▓ ▓▓▓▓▓▓\ ▓▓▓▓▓▓\ ▓▓▓▓▓▓▓
| ▓▓▓▓▓ | ▓▓ | ▓▓ ▓▓ | ▓▓ ▓▓ | ▓▓ | ▓▓\▓▓ \ | ▓▓\▓▓ ▓▓ | ▓▓ | ▓▓ | ▓▓ ▓▓ | ▓▓ ▓▓ ▓▓\▓▓ \
| ▓▓_____| ▓▓ | ▓▓ ▓▓__/ ▓▓ ▓▓ | ▓▓ | ▓▓_\▓▓▓▓▓▓\ | ▓▓ \▓▓▓▓ | ▓▓ | ▓▓__/ ▓▓ ▓▓__/ ▓▓ ▓▓▓▓▓▓▓▓_\▓▓▓▓▓▓\
| ▓▓ \ ▓▓ | ▓▓\▓▓ ▓▓ ▓▓ | ▓▓ | ▓▓ ▓▓ | ▓▓ \▓▓▓ | ▓▓ \▓▓ ▓▓ ▓▓ ▓▓\▓▓ \ ▓▓
\▓▓▓▓▓▓▓▓\▓▓ \▓▓ \▓▓▓▓▓▓ \▓▓ \▓▓ \▓▓\▓▓▓▓▓▓▓ \▓▓ \▓▓ \▓▓ _\▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓\▓▓▓▓▓▓▓
| \__| ▓▓ ▓▓
\▓▓ ▓▓ ▓▓
\▓▓▓▓▓▓ \▓▓
*/
using LogFailType = ssize(*)(char const*, ...);
// By default this library will either crash or exit if an error is detected while generating codes.

View File

@ -459,6 +459,7 @@ FileContents file_read_contents( AllocatorInfo a, b32 zero_terminate, char const
return result;
}
typedef struct _memory_fd _memory_fd;
struct _memory_fd
{
u8 magic;
@ -505,7 +506,7 @@ b8 file_stream_new( FileInfo* file, AllocatorInfo allocator )
d->allocator = allocator;
d->flags = EFileStream_CLONE_WRITABLE;
d->cap = 0;
d->buf = array_init<u8>( allocator );
d->buf = array_init( u8, allocator );
if ( ! d->buf )
return false;
@ -531,7 +532,7 @@ b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, ssize
d->flags = flags;
if ( d->flags & EFileStream_CLONE_WRITABLE )
{
Array<u8> arr = array_init_reserve<u8>( allocator, size );
Array(u8) arr = array_init_reserve(u8, allocator, size );
d->buf = arr;
if ( ! d->buf )
@ -608,9 +609,9 @@ GEN_FILE_WRITE_AT_PROC( _memory_file_write )
if ( d->flags & EFileStream_CLONE_WRITABLE )
{
Array<u8> arr = { d->buf };
Array(u8) arr = { d->buf };
if ( array_get_header(arr)->Capacity < usize(new_cap) )
if ( array_get_header(arr)->Capacity < scast(usize, new_cap) )
{
if ( ! array_grow( & arr, ( s64 )( new_cap ) ) )
return false;
@ -622,7 +623,7 @@ GEN_FILE_WRITE_AT_PROC( _memory_file_write )
if ( ( d->flags & EFileStream_CLONE_WRITABLE ) && extralen > 0 )
{
Array<u8> arr = { d->buf };
Array(u8) arr = { d->buf };
mem_copy( d->buf + offset + rwlen, pointer_add_const( buffer, rwlen ), extralen );
d->cap = new_cap;
@ -646,7 +647,7 @@ GEN_FILE_CLOSE_PROC( _memory_file_close )
if ( d->flags & EFileStream_CLONE_WRITABLE )
{
Array<u8> arr = { d->buf };
Array(u8) arr = { d->buf };
array_free(arr);
}

View File

@ -284,14 +284,15 @@
#endif
#if GEN_COMPILER_C
// ------------------------ _Generic function overloading -----------------------------------------
// This implemnents macros for utilizing "The Naive Extendible _Generic Macro" explained in:
// https://github.com/JacksonAllan/CC/blob/main/articles/Better_C_Generics_Part_1_The_Extendible_Generic.md
// Since gencpp is used to generate the c-library, it was choosen over the more novel implementations to keep the macros as easy to understand and unopaque as possible.
// Extensive effort was put in below to make this as easy as possible to understand what is going on with this mess of a preoprocessor.
// Where the signature would be defined using:
#define GEN_TYPE_TO_EXP(type) (* (type*)NULL)
// ____ _ ______ _ _ ____ _ __ _
// / ___} (_) | ____} | | (_) / __ \ | | | |(_)
// | | ___ ___ _ __ ___ _ __ _ ___ | |__ _ _ _ __ ___| |_ _ ___ _ __ | | | |_ _____ _ __ | | ___ __ _ __| | _ _ __ __ _
// | |{__ |/ _ \ '_ \ / _ \ '__} |/ __| | __} | | | '_ \ / __} __} |/ _ \| '_ \ | | | \ \ / / _ \ '_ \| |/ _ \ / _` |/ _` || | '_ \ / _` |
// | |__j | __/ | | | __/ | | | (__ | | | |_| | | | | (__| l_| | (_) | | | | | l__| |\ V / __/ | | | | (_) | (_| | (_| || | | | | (_| |
// \____/ \___}_l l_l\___}_l l_l\___| l_l \__,_l_l l_l\___}\__}_l\___/l_l l_l \____/ \_/ \___}_l l_l_l\___/ \__,_l\__,_l|_|_| |_|\__, |
// This implemnents macros for utilizing "The Naive Extendible _Generic Macro" explained in: __| |
// https://github.com/JacksonAllan/CC/blob/main/articles/Better_C_Generics_Part_1_The_Extendible_Generic.md {___/
// Since gencpp is used to generate the c-library, it was choosen over the more novel implementations to keep the macros as easy to understand and unobfuscated as possible.
#define GEN_COMMA_OPERATOR , // The comma operator is used by preprocessor macros to delimit arguments, so we have to represent it via a macro to prevent parsing incorrectly.
@ -314,25 +315,23 @@
#define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( slot_exp ) GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER( slot_exp, GEN_GENERIC_SEL_ENTRY_TYPE( slot_exp, ): GEN_GENERIC_SEL_ENTRY_FUNCTION( slot_exp, ) GEN_COMMA_OPERATOR, , )
// ^ Selects the comma ^ is the type ^ is the function ^ Insert a comma
// The slot won't exist if that comma is not found. |
// |
// This is the same as above but it does not insert a comma V no comma here.
#define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( slot_exp ) GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER( slot_exp, GEN_GENERIC_SEL_ENTRY_TYPE( slot_exp, ): GEN_GENERIC_SEL_ENTRY_FUNCTION( slot_exp, ), , )
// Needed for the last slot as they don't allow trailing commas.
// For the occastion where an expression didn't resolve to a selection option the "default: <value>" wilbe set to:
struct NO_RESOLVED_GENERIC_SELECTION {
unsigned long long failure;
};
struct NO_RESOLVED_GENERIC_SELECTION const gen_generic_selection_fail = {};
// Which will provide the message: error: called object type 'struct NO_RESOLVED_GENERIC_SELECTION' is not a function or function pointer
// ----------------------------------------------------------------------------------------------------------------------------------
// Below are generated on demand for an overlaod depdendent on a type:
// ----------------------------------------------------------------------------------------------------------------------------------
#define GEN_FUNCTION_GENERIC_EXAMPLE( selector_arg ) _Generic( \
(selector_arg), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST(FunctionID__ARGS_SIG_1 ) \
// -----------------------------------------------------------------------------------------------------#define GEN_FUNCTION_GENERIC_EXAMPLE( selector_arg ) _Generic( k
#define GEN_FUNCTION_GENERIC_EXAMPLE( selector_arg ) _Generic( \
(selector_arg), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
default: gen_generic_selection_fail \
) GEN_RESOLVED_FUNCTION_CALL( selector_arg )
// ----------------------------------------------------------------------------------------------------------------------------------
@ -355,17 +354,18 @@ size_t gen_example_hash__P_long_long( long long val ) { return val * 2654435761u
// If using an Editor with support for syntax hightlighting macros: HASH__ARGS_SIG_1 and HASH_ARGS_SIG_2 should show color highlighting indicating the slot is enabled,
// or, "defined" for usage during the compilation pass that handles the _Generic instrinsic.
#define hash( function_arguments ) _Generic( \
(function_arguments), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_2 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_3 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_4 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_5 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_6 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_7 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( HASH__ARGS_SIG_8 ) \
#define hash( function_arguments ) _Generic( \
(function_arguments), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_2 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_3 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_4 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_5 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_6 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_7 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_8 ) \
default: gen_generic_selection_fail \
) GEN_RESOLVED_FUNCTION_CALL( function_arguments )
// Additional Variations:
@ -376,7 +376,8 @@ size_t gen_example_hash__P_long_long( long long val ) { return val * 2654435761u
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_2 ) \
... \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST(FunctionID__ARGS_SIG_N ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT(FunctionID__ARGS_SIG_N ) \
default: gen_generic_selection_fail \
) GEN_RESOLVED_FUNCTION_CALL( selector_arg, __VA_ARG__ )
// If the function does not take the arugment as a parameter:
@ -385,9 +386,13 @@ size_t gen_example_hash__P_long_long( long long val ) { return val * 2654435761u
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_2 ) \
/* ... */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST(FunctionID__ARGS_SIG_N ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT(FunctionID__ARGS_SIG_N ) \
default: gen_generic_selection_fail \
) GEN_RESOLVED_FUNCTION_CALL()
// Used to keep the _Generic keyword happy as bare types are not considered "expressions"
#define GEN_TYPE_TO_EXP(type) (* (type*)NULL)
// typedef void* GEN_GenericExampleType;
// GEN_FUNCTION_GENERIC_EXAMPLE_DIRECT_TYPE( GEN_GenericExampleType );

View File

@ -46,6 +46,7 @@ void const* mem_find( void const* data, u8 c, ssize n )
#define GEN_HEAP_STATS_MAGIC 0xDEADC0DE
typedef struct _heap_stats _heap_stats;
struct _heap_stats
{
u32 magic;
@ -80,6 +81,7 @@ void heap_stats_check( void )
GEN_ASSERT( _heap_stats_info.alloc_count == 0 );
}
typedef struct _heap_alloc_info _heap_alloc_info;
struct _heap_alloc_info
{
ssize size;

View File

@ -33,6 +33,7 @@ enum
GEN_FMT_INTS = GEN_FMT_CHAR | GEN_FMT_SHORT | GEN_FMT_INT | GEN_FMT_LONG | GEN_FMT_LLONG | GEN_FMT_SIZE | GEN_FMT_INTPTR
};
typedef struct _format_info _format_info;
struct _format_info
{
s32 base;
@ -429,7 +430,7 @@ neverinline ssize str_fmt_va( char* text, ssize max_len, char const* fmt, va_lis
break;
}
String gen_str = String { va_arg( va, char*) };
String gen_str = { va_arg( va, char*) };
info.precision = string_length(gen_str);
len = _print_string( text, remaining, &info, gen_str );

View File

@ -7,17 +7,21 @@
String string_make_length( AllocatorInfo allocator, char const* str, ssize length )
{
constexpr ssize header_size = sizeof( StringHeader );
ssize const header_size = sizeof( StringHeader );
s32 alloc_size = header_size + length + 1;
void* allocation = alloc( allocator, alloc_size );
if ( allocation == nullptr )
return { nullptr };
if ( allocation == nullptr ) {
String null_string = {nullptr};
return null_string;
}
StringHeader&
header = * rcast(StringHeader*, allocation);
header = { allocator, length, length };
StringHeader*
header = rcast(StringHeader*, allocation);
header->Allocator = allocator;
header->Capacity = length;
header->Length = length;
String result = { rcast( char*, allocation) + header_size };
@ -33,14 +37,15 @@ String string_make_length( AllocatorInfo allocator, char const* str, ssize lengt
String string_make_reserve( AllocatorInfo allocator, ssize capacity )
{
constexpr ssize header_size = sizeof( StringHeader );
ssize const header_size = sizeof( StringHeader );
s32 alloc_size = header_size + capacity + 1;
void* allocation = alloc( allocator, alloc_size );
if ( allocation == nullptr )
return { nullptr };
if ( allocation == nullptr ) {
String null_string = {nullptr};
return null_string;
}
mem_set( allocation, 0, alloc_size );
StringHeader*

View File

@ -7,12 +7,12 @@
struct StrC;
StrC to_strc_from_c_str (char const* bad_string);
bool strc_are_equal (StrC lhs, StrC rhs);
char const* strc_back (StrC str);
bool strc_contains (StrC str, StrC substring);
StrC strc_duplicate (StrC str, AllocatorInfo allocator);
b32 strc_starts_with (StrC str, StrC substring);
StrC strc_to_str (char const* bad_string);
StrC strc_visualize_whitespace(StrC str, AllocatorInfo allocator);
// Constant string with length.

View File

@ -41,13 +41,13 @@ CodeBody gen_ecode( char const* path, bool use_c_definition = false )
if (use_c_definition)
{
enum_code = parse_enum(token_fmt_impl((3 + 1) / 2, "entries", string_to_strc(enum_entries),
"enum CodeType enum_underlying(u32) { <entries> CT_NumTypes };"
"enum CodeType enum_underlying(u32) { <entries> CT_NumTypes, CT_UnderlyingType = GEN_U32_MAX };"
));
}
else
{
enum_code = parse_enum(token_fmt_impl((3 + 1) / 2, "entries", string_to_strc(enum_entries),
"enum CodeType : u32 { <entries> CT_NumTypes };"
"enum CodeType : u32 { <entries> CT_NumTypes, CT_UnderlyingType = GEN_U32_MAX };"
));
}
@ -140,7 +140,8 @@ CodeBody gen_eoperator( char const* path, bool use_c_definition = false )
enum Operator enum_underlying(u32)
{
<entries>
NumOps
Op_NumOps,
Op_UnderlyingType = GEN_U32_MAX
};
)));
#pragma pop_macro("enum_underlying")
@ -151,7 +152,8 @@ CodeBody gen_eoperator( char const* path, bool use_c_definition = false )
enum Operator : u32
{
<entries>
NumOps
Op_NumOps,
Op_UnderlyingType = GEN_U32_MAX
};
)));
}
@ -233,7 +235,8 @@ CodeBody gen_especifier( char const* path, bool use_c_definition = false )
enum Specifier enum_underlying(u32)
{
<entries>
Spec_NumSpecifiers
Spec_NumSpecifiers,
Spec_UnderlyingType = GEN_U32_MAX
};
)));
#pragma pop_macro("enum_underlying")
@ -244,7 +247,8 @@ CodeBody gen_especifier( char const* path, bool use_c_definition = false )
enum Specifier : u32
{
<entries>
Spec_NumSpecifiers
Spec_NumSpecifiers,
Spec_UnderlyingType = GEN_U32_MAX
};
)));
}