From 2dcc968c3948680feb072883f450184b401c567a Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 01:56:49 -0500 Subject: [PATCH] Preparing for reductions on code_types.hpp --- project/components/ast.hpp | 177 +++++++------ project/components/code_serialization.cpp | 6 +- project/components/code_types.hpp | 302 ++++++++++++++++------ 3 files changed, 319 insertions(+), 166 deletions(-) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index ee33940..51bef49 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -73,73 +73,84 @@ struct AST_Union; struct AST_Using; struct AST_Var; -struct Code; -struct CodeBody; -// These are to offer ease of use and optionally strong type safety for the AST. -struct CodeAttributes; -// struct CodeBaseClass; -struct CodeComment; -struct CodeClass; -struct CodeConstructor; -struct CodeDefine; -struct CodeDestructor; -struct CodeEnum; -struct CodeExec; -struct CodeExtern; -struct CodeInclude; -struct CodeFriend; -struct CodeFn; -struct CodeModule; -struct CodeNS; -struct CodeOperator; -struct CodeOpCast; -struct CodeParam; -struct CodePreprocessCond; -struct CodePragma; -struct CodeSpecifiers; - -#if GEN_EXECUTION_EXPRESSION_SUPPORT -struct CodeExpr; -struct CodeExpr_Assign; -struct CodeExpr_Alignof; -struct CodeExpr_Binary; -struct CodeExpr_CStyleCast; -struct CodeExpr_FunctionalCast; -struct CodeExpr_CppCast; -struct CodeExpr_Element; -struct CodeExpr_ProcCall; -struct CodeExpr_Decltype; -struct CodeExpr_Comma; -struct CodeExpr_AMS; // Access Member Symbol -struct CodeExpr_Sizeof; -struct CodeExpr_Subscript; -struct CodeExpr_Ternary; -struct CodeExpr_UnaryPrefix; -struct CodeExpr_UnaryPostfix; - -struct CodeStmt; -struct CodeStmt_Break; -struct CodeStmt_Case; -struct CodeStmt_Continue; -struct CodeStmt_Decl; -struct CodeStmt_Do; -struct CodeStmt_Expr; -struct CodeStmt_Else; -struct CodeStmt_If; -struct CodeStmt_For; -struct CodeStmt_Goto; -struct CodeStmt_Label; -struct CodeStmt_Switch; -struct CodeStmt_While; +#if GEN_COMPILER_C +#define Define_Code(Type) typedef AST_##Type* Code##Type +#else +#define Define_Code(Type) struct Code##Type #endif -struct CodeStruct; -struct CodeTemplate; -struct CodeType; -struct CodeTypedef; -struct CodeUnion; -struct CodeUsing; -struct CodeVar; +#if GEN_COMPILER_C +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); +// struct CodeBaseClass; +Define_Code(Comment); +Define_Code(Class); +Define_Code(Constructor); +Define_Code(Define); +Define_Code(Destructor); +Define_Code(Enum); +Define_Code(Exec); +Define_Code(Extern); +Define_Code(Include); +Define_Code(Friend); +Define_Code(Fn); +Define_Code(Module); +Define_Code(NS); +Define_Code(Operator); +Define_Code(OpCast); +Define_Code(Param); +Define_Code(PreprocessCond); +Define_Code(Pragma); +Define_Code(Specifiers); + +#if GEN_EXECUTION_EXPRESSION_SUPPORT +Define_Code(Expr); +Define_Code(Expr_Assign); +Define_Code(Expr_Alignof); +Define_Code(Expr_Binary); +Define_Code(Expr_CStyleCast); +Define_Code(Expr_FunctionalCast); +Define_Code(Expr_CppCast); +Define_Code(Expr_Element); +Define_Code(Expr_ProcCall); +Define_Code(Expr_Decltype); +Define_Code(Expr_Comma); +Define_Code(Expr_AMS); // Access Member Symbol +Define_Code(Expr_Sizeof); +Define_Code(Expr_Subscript); +Define_Code(Expr_Ternary); +Define_Code(Expr_UnaryPrefix); +Define_Code(Expr_UnaryPostfix); + +Define_Code(Stmt); +Define_Code(Stmt_Break); +Define_Code(Stmt_Case); +Define_Code(Stmt_Continue); +Define_Code(Stmt_Decl); +Define_Code(Stmt_Do); +Define_Code(Stmt_Expr); +Define_Code(Stmt_Else); +Define_Code(Stmt_If); +Define_Code(Stmt_For); +Define_Code(Stmt_Goto); +Define_Code(Stmt_Label); +Define_Code(Stmt_Switch); +Define_Code(Stmt_While); +#endif + +Define_Code(Struct); +Define_Code(Template); +Define_Code(Type); +Define_Code(Typedef); +Define_Code(Union); +Define_Code(Using); +Define_Code(Var); +#undef Define_Code namespace parser { @@ -159,6 +170,7 @@ bool is_valid (Code code); void set_global(Code code); String to_string (Code code); +#if ! GEN_COMPILER_C /* AST* wrapper - Not constantly have to append the '*' as this is written often.. @@ -168,32 +180,32 @@ struct Code { AST* ast; -# define Using_Code( Typename ) \ - char const* debug_str() { return GEN_NS debug_str(* this); } \ - Code duplicate() { return GEN_NS duplicate(* this); } \ +# define Using_Code( Typename ) \ + char const* debug_str() { return GEN_NS debug_str(* this); } \ + Code duplicate() { return GEN_NS duplicate(* this); } \ bool is_equal( Code other ) { return GEN_NS is_equal(* this, other); } \ - bool is_body() { return GEN_NS is_body(* this); } \ - bool is_valid() { return GEN_NS is_valid(* this); } \ - void set_global() { return GEN_NS set_global(* this); } \ - String to_string(); \ - Typename& operator = ( AST* other ); \ - Typename& operator = ( Code other ); \ + bool is_body() { return GEN_NS is_body(* this); } \ + bool is_valid() { return GEN_NS is_valid(* this); } \ + void set_global() { return GEN_NS set_global(* this); } + +# define Using_CodeOps( Typename ) \ + Typename& operator = ( AST* other ); \ + Typename& operator = ( Code other ); \ bool operator ==( Code other ) { return (AST*)ast == other.ast; } \ bool operator !=( Code other ) { return (AST*)ast != other.ast; } \ operator bool(); +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( Code ); + String to_string() { return GEN_NS to_string(* this); } +#endif + + Using_CodeOps( Code ); template< class Type > - forceinline Type code_cast() - { - return * rcast( Type*, this ); - } + forceinline Type code_cast() { return * rcast( Type*, this ); } - AST* operator ->() - { - return ast; - } + AST* operator ->() { return ast; } Code& operator ++(); // TODO(Ed) : Remove this overload. @@ -242,6 +254,7 @@ struct Code operator CodeVar() const; #undef operator }; +#endif #pragma region Statics // Used to identify ASTs that should always be duplicated. (Global constant ASTs) diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index d8c90c5..c08144b 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -3,14 +3,14 @@ #include "ast.cpp" #endif -String Code::to_string() +String to_string(Code self) { - if ( ast == nullptr ) + if ( self.ast == nullptr ) { log_failure( "Code::to_string: Cannot convert code to string, AST is null!" ); return { nullptr }; } - return rcast( AST*, ast )->to_string(); + return rcast( AST*, self.ast )->to_string(); } String CodeAttributes::to_string() diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index 6d211d0..d823de8 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -4,9 +4,13 @@ #endif #pragma region Code Types +// These structs are not used at all by the C vairant. +#if ! GEN_COMPILER_C +// stati_assert( GEN_COMPILER_C, "This should not be compiled with the C-library" ); struct CodeBody { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeBody ); void append( Code other ) @@ -28,19 +32,20 @@ struct CodeBody bool has_entries() { return GEN_NS has_entries(rcast( AST*, ast )); } AST* raw() { return rcast( AST*, ast ); } - void to_string( String& result ); - void to_string_export( String& result ); - - AST_Body* operator->() { return ast; } + String to_string(); + void to_string( String& result ); + void to_string_export( String& result ); +#endif + Using_CodeOps( CodeBody ); operator Code() { return * rcast( Code*, this ); } + AST_Body* operator->() { return ast; } #pragma region Iterator Code begin() { if ( ast ) return { rcast( AST*, ast)->Front }; - return { nullptr }; } Code end() @@ -54,21 +59,20 @@ struct CodeBody struct CodeClass { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeClass ); void add_interface( CodeType interface ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); - AST* raw() - { - return rcast( AST*, ast ); - } - operator Code() - { - return * rcast( Code*, this ); - } + AST* raw() { return rcast( AST*, ast ); } +#endif + + Using_CodeOps( CodeClass ); + operator Code() { return * rcast( Code*, this ); } AST_Class* operator->() { if ( ast == nullptr ) @@ -83,13 +87,17 @@ struct CodeClass struct CodeParam { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeParam ); - void append( CodeParam other ); - + void append( CodeParam other ); CodeParam get( s32 idx ); - bool has_entries(); - void to_string( String& result ); + bool has_entries(); + String to_string(); + void to_string( String& result ); +#endif + + Using_CodeOps( CodeParam ); AST* raw() { return rcast( AST*, ast ); @@ -132,6 +140,7 @@ struct CodeParam struct CodeSpecifiers { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeSpecifiers ); bool append( SpecifierT spec ) @@ -202,7 +211,11 @@ struct CodeSpecifiers } return result; } - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + + Using_CodeOps(CodeSpecifiers); AST* raw() { return rcast( AST*, ast ); @@ -239,13 +252,17 @@ struct CodeSpecifiers struct CodeStruct { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeStruct ); void add_interface( CodeType interface ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_fwd( String& result ); + void to_string_def( String& result ); +#endif + Using_CodeOps( CodeStruct ); AST* raw() { return rcast( AST*, ast ); @@ -266,27 +283,47 @@ struct CodeStruct AST_Struct* ast; }; -#define Define_CodeType( Typename ) \ - struct Code##Typename \ - { \ - Using_Code( Code ## Typename ); \ - AST* raw(); \ - operator Code(); \ - AST_##Typename* operator->(); \ - AST_##Typename* ast; \ - } +struct CodeAttributes +{ +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 + Using_Code(CodeAttributes); + String to_string(); +#endif + + Using_CodeOps(CodeAttributes); + AST *raw(); + operator Code(); + AST_Attributes *operator->(); + AST_Attributes *ast; +}; -Define_CodeType( Attributes ); // Define_CodeType( BaseClass ); -Define_CodeType( Comment ); + +struct CodeComment +{ +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 + Using_Code(CodeComment); + String to_string(); +#endif + + Using_CodeOps(CodeComment); + AST *raw(); + operator Code(); + AST_Comment *operator->(); + AST_Comment *ast; +}; struct CodeConstructor { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeConstructor ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeConstructor); AST* raw(); operator Code(); AST_Constructor* operator->(); @@ -295,10 +332,14 @@ struct CodeConstructor struct CodeDefine { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeDefine ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeDefine); AST* raw(); operator Code(); AST_Define* operator->(); @@ -307,11 +348,15 @@ struct CodeDefine struct CodeDestructor { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeDestructor ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeDestructor); AST* raw(); operator Code(); AST_Destructor* operator->(); @@ -320,20 +365,36 @@ struct CodeDestructor struct CodeEnum { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeEnum ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); - void to_string_class_def( String& result ); - void to_string_class_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); + void to_string_class_def( String& result ); + void to_string_class_fwd( String& result ); +#endif + Using_CodeOps(CodeEnum); AST* raw(); operator Code(); AST_Enum* operator->(); AST_Enum* ast; }; -Define_CodeType( Exec ); +struct CodeExec +{ +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 + Using_Code(CodeExec); + String to_string(); +#endif + + Using_CodeOps(CodeExec); + AST *raw(); + operator Code(); + AST_Exec *operator->(); + AST_Exec *ast; +}; #if GEN_EXECUTION_EXPRESSION_SUPPORT struct CodeExpr @@ -543,10 +604,13 @@ struct CodeExpr_UnaryPostfix struct CodeExtern { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeExtern ); void to_string( String& result ); +#endif + Using_CodeOps(CodeExtern); AST* raw(); operator Code(); AST_Extern* operator->(); @@ -555,10 +619,14 @@ struct CodeExtern struct CodeInclude { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeInclude ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeInclude); AST* raw(); operator Code(); AST_Include* operator->(); @@ -567,10 +635,14 @@ struct CodeInclude struct CodeFriend { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeFriend ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeFriend); AST* raw(); operator Code(); AST_Friend* operator->(); @@ -579,11 +651,15 @@ struct CodeFriend struct CodeFn { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeFn ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeFn); AST* raw(); operator Code(); AST_Fn* operator->(); @@ -592,10 +668,14 @@ struct CodeFn struct CodeModule { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeModule ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeModule); AST* raw(); operator Code(); AST_Module* operator->(); @@ -604,10 +684,14 @@ struct CodeModule struct CodeNS { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeNS ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeNS); AST* raw(); operator Code(); AST_NS* operator->(); @@ -616,11 +700,15 @@ struct CodeNS struct CodeOperator { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeOperator ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeOperator); AST* raw(); operator Code(); AST_Operator* operator->(); @@ -629,11 +717,15 @@ struct CodeOperator struct CodeOpCast { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeOpCast ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeOpCast); AST* raw(); operator Code(); AST_OpCast* operator->(); @@ -642,10 +734,14 @@ struct CodeOpCast struct CodePragma { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodePragma ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps( CodePragma ); AST* raw(); operator Code(); AST_Pragma* operator->(); @@ -654,15 +750,19 @@ struct CodePragma struct CodePreprocessCond { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodePreprocessCond ); - void to_string_if( String& result ); - void to_string_ifdef( String& result ); - void to_string_ifndef( String& result ); - void to_string_elif( String& result ); - void to_string_else( String& result ); - void to_string_endif( String& result ); + String to_string(); + void to_string_if( String& result ); + void to_string_ifdef( String& result ); + void to_string_ifndef( String& result ); + void to_string_elif( String& result ); + void to_string_else( String& result ); + void to_string_endif( String& result ); +#endif + Using_CodeOps( CodePreprocessCond ); AST* raw(); operator Code(); AST_PreprocessCond* operator->(); @@ -674,7 +774,8 @@ struct CodeStmt { Using_Code( CodeStmt ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -686,7 +787,8 @@ struct CodeStmt_Break { Using_Code( CodeStmt_Break ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -698,7 +800,8 @@ struct CodeStmt_Case { Using_Code( CodeStmt_Case ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -710,7 +813,8 @@ struct CodeStmt_Continue { Using_Code( CodeStmt_Continue ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -722,7 +826,8 @@ struct CodeStmt_Decl { Using_Code( CodeStmt_Decl ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -734,7 +839,8 @@ struct CodeStmt_Do { Using_Code( CodeStmt_Do ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -746,7 +852,8 @@ struct CodeStmt_Expr { Using_Code( CodeStmt_Expr ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -758,7 +865,8 @@ struct CodeStmt_Else { Using_Code( CodeStmt_Else ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -770,7 +878,8 @@ struct CodeStmt_If { Using_Code( CodeStmt_If ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -782,7 +891,8 @@ struct CodeStmt_For { Using_Code( CodeStmt_For ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -794,7 +904,8 @@ struct CodeStmt_Goto { Using_Code( CodeStmt_Goto ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -806,7 +917,8 @@ struct CodeStmt_Label { Using_Code( CodeStmt_Label ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -818,7 +930,8 @@ struct CodeStmt_Switch { Using_Code( CodeStmt_Switch ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -830,7 +943,8 @@ struct CodeStmt_While { Using_Code( CodeStmt_While ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -841,10 +955,14 @@ struct CodeStmt_While struct CodeTemplate { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeTemplate ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps( CodeTemplate ); AST* raw(); operator Code(); AST_Template* operator->(); @@ -853,10 +971,14 @@ struct CodeTemplate struct CodeType { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeType ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps( CodeType ); AST* raw(); operator Code(); AST_Type* operator->(); @@ -865,10 +987,14 @@ struct CodeType struct CodeTypedef { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeTypedef ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps( CodeTypedef ); AST* raw(); operator Code(); AST_Typedef* operator->(); @@ -877,10 +1003,14 @@ struct CodeTypedef struct CodeUnion { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeUnion ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeUnion); AST* raw(); operator Code(); AST_Union* operator->(); @@ -889,11 +1019,15 @@ struct CodeUnion struct CodeUsing { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeUsing ); - void to_string( String& result ); - void to_string_ns( String& result ); + String to_string(); + void to_string( String& result ); + void to_string_ns( String& result ); +#endif + Using_CodeOps(CodeUsing); AST* raw(); operator Code(); AST_Using* operator->(); @@ -902,10 +1036,14 @@ struct CodeUsing struct CodeVar { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeVar ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeVar); AST* raw(); operator Code(); AST_Var* operator->(); @@ -914,5 +1052,7 @@ struct CodeVar #undef Define_CodeType #undef Using_Code +#undef Using_CodeOps +#endif //if ! GEN_COMPILER_C #pragma endregion Code Types