Ignore:
Timestamp:
Feb 27, 2024, 12:28:58 PM (5 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
924534e
Parents:
4c0b674
Message:

Factored out the ParseNode?'s next field into a new child type. This is only type safe when used in the given one level curiously reoccurring template pattern, as it is now. This allowed most of the intermedate helpers to be removed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ParseNode.h

    r4c0b674 rdc3fbe5  
    3333
    3434struct DeclarationNode;
    35 class InitializerNode;
    36 class ExpressionNode;
     35struct InitializerNode;
     36struct ExpressionNode;
    3737struct StatementNode;
    3838
     
    4545extern YYLTYPE yylloc;
    4646
    47 class ParseNode {
    48   public:
    49         ParseNode() {};
    50         virtual ~ParseNode() { delete next; };
     47struct ParseNode {
     48        ParseNode() {}
     49        virtual ~ParseNode() {}
    5150        virtual ParseNode * clone() const = 0;
    5251
    53         ParseNode * get_next() const { return next; }
    54         ParseNode * set_next( ParseNode * newlink ) { next = newlink; return this; }
     52        virtual void print( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const {}
    5553
    56         ParseNode * get_last() {
    57                 ParseNode * current;
    58                 for ( current = this; current->get_next() != nullptr; current = current->get_next() );
     54        static int indent_by;
     55
     56        CodeLocation location = yylloc;
     57}; // ParseNode
     58
     59/// Only ever use in the form `struct NAME final : public ParseList<NAME>`!
     60template<typename Next>
     61struct ParseList : public ParseNode {
     62        ParseList() {}
     63        virtual ~ParseList() { delete next; };
     64        virtual ParseList<Next> * clone() const = 0;
     65
     66        Next * get_next() const { return next; }
     67        void set_next( Next * newlink ) { next = newlink; }
     68
     69        Next * get_last() {
     70                Next * current = static_cast<Next *>( this );
     71                while ( current->next != nullptr ) current = current->next;
    5972                return current;
    6073        }
    61         ParseNode * set_last( ParseNode * newlast ) {
    62                 if ( newlast != nullptr ) get_last()->set_next( newlast );
    63                 return this;
     74        Next * set_last( Next * newlast ) {
     75                if ( newlast != nullptr ) get_last()->next = newlast;
     76                return static_cast<Next *>( this );
    6477        }
    6578
    66         virtual void print( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const {}
    6779        virtual void printList( std::ostream & os, int indent = 0 ) const {
    6880                print( os, indent );
     
    7082        }
    7183
    72         static int indent_by;
    73 
    74         ParseNode * next = nullptr;
    75         CodeLocation location = yylloc;
    76 }; // ParseNode
     84        Next * next = nullptr;
     85};
    7786
    7887// Must harmonize with OperName.
Note: See TracChangeset for help on using the changeset viewer.