From 2b24511f7d12e3d664828c853072d08824693c7f Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 00:34:40 -0500 Subject: [PATCH] Code::is_equal reduction --- project/components/ast.cpp | 52 ++--- project/components/ast.hpp | 6 +- project/components/gen/ast_inlines.hpp | 261 ------------------------- project/components/inlines.hpp | 12 ++ project/helpers/helper.hpp | 11 -- 5 files changed, 42 insertions(+), 300 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index acc572f..3d20a7b 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -592,7 +592,7 @@ void AST::to_string( String& result ) } } -bool AST::is_equal( AST* other ) +bool is_equal( AST* self, AST* other ) { /* AST values are either some u32 value, a cached string, or a pointer to another AST. @@ -603,31 +603,31 @@ bool AST::is_equal( AST* other ) */ if ( other == nullptr ) { - log_fmt( "AST::is_equal: other is null\nAST: %S", debug_str() ); + log_fmt( "AST::is_equal: other is null\nAST: %S", debug_str(self) ); return false; } - if ( Type != other->Type ) + if ( self->Type != other->Type ) { log_fmt("AST::is_equal: Type check failure with other\nAST: %S\nOther: %S" - , debug_str() + , debug_str(self) , other->debug_str() ); return false; } - switch ( Type ) + switch ( self->Type ) { using namespace ECode; #define check_member_val( val ) \ - if ( val != other->val ) \ + if ( self->val != other->val ) \ { \ log_fmt("\nAST::is_equal: Member - " #val " failed\n" \ "AST : %S\n" \ "Other: %S\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ ); \ \ @@ -635,12 +635,12 @@ bool AST::is_equal( AST* other ) } #define check_member_str( str ) \ - if ( str != other->str ) \ + if ( self->str != other->str ) \ { \ log_fmt("\nAST::is_equal: Member string - "#str " failed\n" \ "AST : %S\n" \ "Other: %S\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ ); \ \ @@ -648,12 +648,12 @@ bool AST::is_equal( AST* other ) } #define check_member_content( content ) \ - if ( content != other->content ) \ + if ( self->content != other->content ) \ { \ log_fmt("\nAST::is_equal: Member content - "#content " failed\n" \ "AST : %S\n" \ "Other: %S\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ ); \ \ @@ -661,13 +661,13 @@ bool AST::is_equal( AST* other ) "so it must be verified by eye for now\n" \ "AST Content:\n%S\n" \ "Other Content:\n%S\n" \ - , visualize_whitespace(content) \ + , visualize_whitespace(self->content) \ , visualize_whitespace(other->content) \ ); \ } #define check_member_ast( ast ) \ - if ( ast ) \ + if ( self->ast ) \ { \ if ( other->ast == nullptr ) \ { \ @@ -675,24 +675,24 @@ bool AST::is_equal( AST* other ) "AST : %s\n" \ "Other: %s\n" \ "For ast member: %s\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ - , ast->debug_str() \ + , self->ast->debug_str() \ ); \ \ return false; \ } \ \ - if ( ! ast->is_equal( other->ast ) ) \ + if ( ! self->ast->is_equal( other->ast ) ) \ { \ log_fmt( "\nAST::is_equal: Failed for " #ast"\n" \ "AST : %S\n" \ "Other: %S\n" \ "For ast member: %S\n" \ "other's ast member: %S\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ - , ast->debug_str() \ + , self->ast->debug_str() \ , other->ast->debug_str() \ ); \ \ @@ -907,9 +907,9 @@ bool AST::is_equal( AST* other ) case Parameters: { - if ( NumEntries > 1 ) + if ( self->NumEntries > 1 ) { - AST* curr = this; + AST* curr = self; AST* curr_other = other; while ( curr != nullptr ) { @@ -934,7 +934,7 @@ bool AST::is_equal( AST* other ) "Other: %S\n" "For ast member: %S\n" "other's ast member: %S\n" - , debug_str() + , debug_str(self) , other->debug_str() , curr->debug_str() , curr_other->debug_str() @@ -949,7 +949,7 @@ bool AST::is_equal( AST* other ) "Other: %S\n" "For ast member: %S\n" "other's ast member: %S\n" - , debug_str() + , debug_str(self) , other->debug_str() , curr->debug_str() , curr_other->debug_str() @@ -964,7 +964,7 @@ bool AST::is_equal( AST* other ) "Other: %S\n" "For ast member: %S\n" "other's ast member: %S\n" - , debug_str() + , debug_str(self) , other->debug_str() , curr->debug_str() , curr_other->debug_str() @@ -1020,7 +1020,7 @@ bool AST::is_equal( AST* other ) { check_member_val( NumEntries ); check_member_str( Name ); - for ( s32 idx = 0; idx < NumEntries; ++idx ) + for ( s32 idx = 0; idx < self->NumEntries; ++idx ) { check_member_val( ArrSpecs[ idx ] ); } @@ -1103,7 +1103,7 @@ bool AST::is_equal( AST* other ) check_member_ast( Front ); check_member_ast( Back ); - AST* curr = Front; + AST* curr = self->Front; AST* curr_other = other->Front; while ( curr != nullptr ) { @@ -1126,7 +1126,7 @@ bool AST::is_equal( AST* other ) "Other: %S\n" "For ast member: %S\n" "other's ast member: %S\n" - , debug_str() + , debug_str(self) , other->debug_str() , curr->debug_str() , curr_other->debug_str() diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 2b14275..6cec65d 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -171,7 +171,7 @@ struct Code # 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 ); \ + bool is_equal( Code other ) { return GEN_NS is_equal(* this, other); } \ bool is_body() { return GEN_NS is_body(* this); } \ bool is_valid(); \ void set_global(); \ @@ -267,12 +267,14 @@ AST* duplicate ( AST* self ); Code* entry ( AST* self, u32 idx ); bool has_entries( AST* self ); bool is_body ( AST* self ); +bool is_equal ( AST* self, AST* other ); String to_string ( AST* self ); char const* type_str ( AST* self ); #if GEN_CPP_SUPPORT_REFERENCES void append ( AST& self, AST& other ) { return append(& self, & other); } bool is_body ( AST& self ) { return is_body(& self); } +bool is_equal ( AST& self, AST& other ) { return is_equal(& self, & other); } char const* debug_str( AST& self ) { return debug_str( & self ); } String to_string( AST& self ) { return to_string( & self ); } char const* type_str ( AST& self ) { return type_str( & self ); } @@ -290,7 +292,7 @@ struct AST AST* duplicate () { return GEN_NS duplicate(this); } Code* entry ( u32 idx ) { return GEN_NS entry(this, idx); } bool has_entries(); - bool is_equal ( AST* other ); + bool is_equal ( AST* other ) { return GEN_NS is_equal(this, other); } bool is_body() { return GEN_NS is_body(this); } char const* type_str() { return GEN_NS type_str(this); } bool validate_body(); diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index bd41bdf..b336234 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -7,15 +7,6 @@ #pragma region generated code inline implementation -inline bool Code::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool Code::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -47,15 +38,6 @@ inline Code::operator bool() return ast != nullptr; } -inline bool CodeBody::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeBody::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -87,15 +69,6 @@ inline CodeBody::operator bool() return ast != nullptr; } -inline bool CodeAttributes::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeAttributes::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -147,15 +120,6 @@ inline AST_Attributes* CodeAttributes::operator->() return ast; } -inline bool CodeComment::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeComment::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -207,15 +171,6 @@ inline AST_Comment* CodeComment::operator->() return ast; } -inline bool CodeConstructor::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeConstructor::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -267,15 +222,6 @@ inline AST_Constructor* CodeConstructor::operator->() return ast; } -inline bool CodeClass::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeClass::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -307,15 +253,6 @@ inline CodeClass::operator bool() return ast != nullptr; } -inline bool CodeDefine::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeDefine::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -367,15 +304,6 @@ inline AST_Define* CodeDefine::operator->() return ast; } -inline bool CodeDestructor::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeDestructor::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -427,15 +355,6 @@ inline AST_Destructor* CodeDestructor::operator->() return ast; } -inline bool CodeEnum::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeEnum::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -487,15 +406,6 @@ inline AST_Enum* CodeEnum::operator->() return ast; } -inline bool CodeExec::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeExec::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -547,15 +457,6 @@ inline AST_Exec* CodeExec::operator->() return ast; } -inline bool CodeExtern::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeExtern::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -607,15 +508,6 @@ inline AST_Extern* CodeExtern::operator->() return ast; } -inline bool CodeFriend::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeFriend::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -667,15 +559,6 @@ inline AST_Friend* CodeFriend::operator->() return ast; } -inline bool CodeFn::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeFn::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -727,15 +610,6 @@ inline AST_Fn* CodeFn::operator->() return ast; } -inline bool CodeInclude::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeInclude::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -787,15 +661,6 @@ inline AST_Include* CodeInclude::operator->() return ast; } -inline bool CodeModule::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeModule::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -847,15 +712,6 @@ inline AST_Module* CodeModule::operator->() return ast; } -inline bool CodeNS::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeNS::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -907,15 +763,6 @@ inline AST_NS* CodeNS::operator->() return ast; } -inline bool CodeOperator::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeOperator::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -967,15 +814,6 @@ inline AST_Operator* CodeOperator::operator->() return ast; } -inline bool CodeOpCast::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeOpCast::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1027,15 +865,6 @@ inline AST_OpCast* CodeOpCast::operator->() return ast; } -inline bool CodeParam::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeParam::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1067,15 +896,6 @@ inline CodeParam::operator bool() return ast != nullptr; } -inline bool CodePragma::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodePragma::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1127,15 +947,6 @@ inline AST_Pragma* CodePragma::operator->() return ast; } -inline bool CodePreprocessCond::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodePreprocessCond::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1187,15 +998,6 @@ inline AST_PreprocessCond* CodePreprocessCond::operator->() return ast; } -inline bool CodeSpecifiers::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeSpecifiers::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1227,15 +1029,6 @@ inline CodeSpecifiers::operator bool() return ast != nullptr; } -inline bool CodeStruct::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeStruct::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1267,15 +1060,6 @@ inline CodeStruct::operator bool() return ast != nullptr; } -inline bool CodeTemplate::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeTemplate::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1327,15 +1111,6 @@ inline AST_Template* CodeTemplate::operator->() return ast; } -inline bool CodeType::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeType::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1387,15 +1162,6 @@ inline AST_Type* CodeType::operator->() return ast; } -inline bool CodeTypedef::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeTypedef::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1447,15 +1213,6 @@ inline AST_Typedef* CodeTypedef::operator->() return ast; } -inline bool CodeUnion::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeUnion::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1507,15 +1264,6 @@ inline AST_Union* CodeUnion::operator->() return ast; } -inline bool CodeUsing::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeUsing::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1567,15 +1315,6 @@ inline AST_Using* CodeUsing::operator->() return ast; } -inline bool CodeVar::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeVar::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 0713c14..e80c567 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -121,6 +121,18 @@ bool is_body(Code code) return false; } +inline +bool is_equal( Code self, Code other ) +{ + if ( self.ast == nullptr || other.ast == nullptr ) + { + // Just check if they're both null. + // log_failure( "Code::is_equal: Cannot compare code, AST is null!" ); + return self.ast == nullptr && other.ast == nullptr; + } + return is_equal( self.ast, other.ast ); +} + inline Code& Code::operator ++() { diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 819c31f..fc91e95 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -354,17 +354,6 @@ CodeBody gen_ast_inlines() char const* code_impl_tmpl = stringize( \n inline - bool ::is_equal( Code other ) - { - if ( ast == nullptr || other.ast == nullptr ) - { - // Just check if they're both null. - // log_failure( "Code::is_equal: Cannot compare code, AST is null!" ); - return ast == nullptr && other.ast == nullptr; - } - return rcast(AST*, ast)->is_equal( other.ast ); - } - inline bool ::is_valid() { return (AST*) ast != nullptr && rcast( AST*, ast)->Type != CodeT::Invalid;