Changeset 28f8f15 for src/Parser


Ignore:
Timestamp:
Apr 27, 2023, 3:13:24 PM (2 years ago)
Author:
JiadaL <j82liang@…>
Branches:
ADT
Children:
561354f
Parents:
b110bcc
Message:

Save progress

Location:
src/Parser
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rb110bcc r28f8f15  
    279279} // DeclarationNode::newEnum
    280280
     281DeclarationNode * DeclarationNode::newADT( const string * name, DeclarationNode * constructors ) {
     282        DeclarationNode * newnode = newEnum( name, nullptr, true, false );
     283        newnode->type->enumeration.isData = true;
     284        newnode->type->enumeration.data_constructors = constructors;
     285        return newnode;
     286}
     287
     288
    281289DeclarationNode * DeclarationNode::newName( const string * name ) {
    282290        DeclarationNode * newnode = new DeclarationNode;
     
    305313        } // if
    306314} // DeclarationNode::newEnumValueGeneric
     315
     316DeclarationNode * DeclarationNode::newDataConstructor( const string * name ) {
     317        DeclarationNode * newnode = newName(name);
     318        return newnode;
     319}
    307320
    308321DeclarationNode * DeclarationNode::newEnumInLine( const string name ) {
     
    10831096        }
    10841097        return nullptr;
     1098}
     1099
     1100void buildDataConstructors( DeclarationNode * firstNode, std::vector<ast::ptr<ast::StructDecl>> & outputList ) {
     1101        std::back_insert_iterator<std::vector<ast::ptr<ast::StructDecl>>> out( outputList );
     1102        for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
     1103                // td->kind == TypeData::Symbolic
     1104                assert( cur->type->kind == TypeData::Symbolic );
     1105                const std::string * name = cur->name;
     1106                auto ctor = new ast::StructDecl( cur->location,
     1107                        std::string(*name),
     1108                        ast::AggregateDecl::Aggregate::Struct
     1109                );
     1110                ctor->set_body(true);
     1111                TypeData * td = cur->type;
     1112                TypeData::Symbolic_t st = td->symbolic;
     1113                DeclarationNode * params = st.params;
     1114               
     1115                if ( params ) {
     1116                        buildList( params, ctor->members );
     1117                }
     1118
     1119                for ( std::size_t i = 0; i < ctor->members.size(); ++i ) {
     1120                        assert(ctor->members[i]->name == "");
     1121                        ast::Decl * member = ctor->members[i].get_and_mutate();
     1122                        member->name = "field_" + std::to_string(i);
     1123                }
     1124                *out++ = ctor;         
     1125        }
     1126}
     1127
     1128ast::UnionDecl * buildDataUnion( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
     1129        ast::UnionDecl * out = new ast::UnionDecl( data->location, "temp_data_union" );
     1130        // size_t index = 0;
     1131        if ( typeList.size() > 0 ) out->set_body( true );
     1132        size_t i = 0;
     1133        for (const ast::ptr<ast::StructDecl> structDecl : typeList ) {
     1134                ast::StructInstType * inst = new ast::StructInstType(structDecl);
     1135                ast::ObjectDecl * instObj = new ast::ObjectDecl(
     1136                        structDecl->location,
     1137                        "option_" + std::to_string(i),
     1138                        inst
     1139                );
     1140                i++;
     1141                out->members.push_back( instObj );
     1142
     1143        }
     1144        return out;
     1145}
     1146
     1147ast::EnumDecl * buildTag( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
     1148        ast::EnumDecl * out = new ast::EnumDecl( data->location, "temp_data_tag" );
     1149        if ( typeList.size() > 0 ) out->set_body( true );
     1150        for ( const ast::ptr<ast::StructDecl> structDecl : typeList ) {
     1151                ast::EnumInstType * inst = new ast::EnumInstType( out );
     1152                assert( inst->base != nullptr );
     1153                ast::ObjectDecl * instObj = new ast::ObjectDecl(
     1154                        structDecl->location,
     1155                        structDecl->name,
     1156                        inst
     1157                );
     1158                out->members.push_back( instObj );
     1159        }
     1160        return out;
     1161}
     1162
     1163ast::StructDecl * buildTaggedUnions( const ast::EnumDecl * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union ) {
     1164        assert( tags->members.size() == data_union->members.size() );
     1165        ast::StructDecl * out = new ast::StructDecl( data->location, data->name );
     1166        out->kind = ast::AggregateDecl::ADT;
     1167
     1168        out->set_body( true );
     1169
     1170        ast::EnumInstType * tag = new ast::EnumInstType( tags );
     1171        ast::ObjectDecl * tag_obj = new ast::ObjectDecl(
     1172                data->location,
     1173                "tag",
     1174                tag
     1175        );
     1176        ast::UnionInstType * value = new ast::UnionInstType( data_union );
     1177        ast::ObjectDecl * value_obj = new ast::ObjectDecl(
     1178                data->location,
     1179                "value",
     1180                value
     1181        );
     1182
     1183        out->members.push_back( value_obj );
     1184        out->members.push_back( tag_obj );
     1185        return out;
    10851186}
    10861187
  • src/Parser/DeclarationNode.h

    rb110bcc r28f8f15  
    7676        static DeclarationNode * newStaticAssert( ExpressionNode * condition, ast::Expr * message );
    7777
     78        // Experimental algebric data type
     79        static DeclarationNode * newADT( const std::string * name, DeclarationNode * constructors );
     80        static DeclarationNode * newDataConstructor( const std::string * name );
     81        // static DeclarationNode * newDataConstructor( const std::string * name, DeclarationNode * typeSpecifiers );
     82
    7883        DeclarationNode();
    7984        ~DeclarationNode();
     
    156161        ExpressionNode * bitfieldWidth = nullptr;
    157162        std::unique_ptr<ExpressionNode> enumeratorValue;
     163
    158164        bool hasEllipsis = false;
    159165        ast::Linkage::Spec linkage;
     
    210216void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList );
    211217void buildTypeList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::Type>> & outputList );
     218void buildDataConstructors( DeclarationNode * firstNode, std::vector<ast::ptr<ast::StructDecl>> & outputList );
     219ast::UnionDecl * buildDataUnion( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList );
     220ast::EnumDecl * buildTag( ast::EnumDecl * data, const std::vector<ast::ptr<ast::StructDecl>> & typeList );
     221ast::StructDecl * buildTaggedUnions( const ast::EnumDecl * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union );
    212222
    213223template<typename AstType, typename NodeType,
  • src/Parser/TypeData.cc

    rb110bcc r28f8f15  
    12601260        );
    12611261        buildList( td->enumeration.constants, ret->members );
     1262        if ( td->enumeration.data_constructors != nullptr ) {
     1263                buildDataConstructors( td->enumeration.data_constructors, ret->data_constructors );
     1264                ret->data_union = buildDataUnion( ret, ret->data_constructors );
     1265                ret->tag = buildTag( ret, ret->data_constructors );
     1266                ret->tag_union = buildTaggedUnions( ret, ret->tag.get(), ret->data_union.get() );
     1267        }
     1268
     1269        if ( ret->data_constructors.size() > 0 ) ret->isData = true;
    12621270        auto members = ret->members.begin();
    12631271        ret->hide = td->enumeration.hiding == EnumHiding::Hide ? ast::EnumDecl::EnumHiding::Hide : ast::EnumDecl::EnumHiding::Visible;
  • src/Parser/TypeData.h

    rb110bcc r28f8f15  
    2525struct TypeData {
    2626        enum Kind { Basic, Pointer, Reference, Array, Function, Aggregate, AggregateInst, Enum, EnumConstant, Symbolic,
    27                                 SymbolicInst, Tuple, Basetypeof, Typeof, Vtable, Builtin, GlobalScope, Qualified, Unknown };
     27                                SymbolicInst, Tuple, Basetypeof, Typeof, Vtable, Builtin, GlobalScope, Qualified, ADT, Ctor, Unknown };
    2828
    2929        struct Aggregate_t {
     
    5858                bool typed;
    5959                EnumHiding hiding;
     60                bool isData = false;
     61
     62                DeclarationNode * data_constructors = nullptr;
     63        };
     64
     65        struct ADT_t {
     66                const std::string * name = nullptr;
     67                DeclarationNode * constructors;
     68        };
     69
     70        struct Constructor_t {
     71                const std::string * name;
     72                DeclarationNode * type; // types?
    6073        };
    6174
     
    98111        Array_t array;
    99112        Enumeration_t enumeration;
     113        ADT_t adt;
     114        Constructor_t data_constructor;
     115
    100116        Function_t function;
    101117        Symbolic_t symbolic;
  • src/Parser/lex.ll

    rb110bcc r28f8f15  
    353353with                    { KEYWORD_RETURN(WITH); }                               // CFA
    354354zero_t                  { NUMERIC_RETURN(ZERO_T); }                             // CFA
     355_DATA_            { KEYWORD_RETURN(DATA); }                             // Experimental
    355356
    356357                                /* identifier */
  • src/Parser/parser.yy

    rb110bcc r28f8f15  
    339339%token SIZEOF TYPEOF VA_LIST VA_ARG AUTO_TYPE                   // GCC
    340340%token OFFSETOF BASETYPEOF TYPEID                                               // CFA
    341 %token ENUM STRUCT UNION
     341%token ENUM STRUCT UNION DATA
    342342%token EXCEPTION                                                                                // CFA
    343343%token GENERATOR COROUTINE MONITOR THREAD                               // CFA
     
    453453%type<decl> enumerator_list enum_type enum_type_nobody
    454454%type<init> enumerator_value_opt
     455
     456%type<decl> value_list
     457%type<decl> data_constructor type_specifier_list
    455458
    456459%type<decl> external_definition external_definition_list external_definition_list_opt
     
    24412444                }
    24422445        | enum_type
     2446        /* | algebric_data_type */
    24432447        ;
    24442448
     
    26942698                }
    26952699        | enum_type_nobody
    2696         ;
     2700        | DATA identifier
     2701        { typedefTable.makeTypedef( *$2 ); }
     2702         '{' value_list '}'
     2703         {
     2704                $$ = DeclarationNode::newADT( $2, $5 );
     2705         }
     2706        ;
     2707
     2708value_list:
     2709        data_constructor
     2710        {
     2711                $$ = $1;
     2712        }
     2713        /* | identifier_or_type_name '(' type_specifier ')'
     2714        {
     2715                $$ = DeclarationNode::newEnumValueGeneric( $1, nullptr );
     2716        } */
     2717        /* | data_constructor '|' value_list   */
     2718        | value_list '|' data_constructor
     2719        {
     2720                 { $$ = $1->appendList( $3 ); }
     2721        }
     2722        ;
     2723
     2724data_constructor:
     2725        identifier_or_type_name
     2726        {
     2727                typedefTable.makeTypedef( *$1 );
     2728                $$ =  DeclarationNode::newTypeDecl( $1, nullptr );;
     2729        }
     2730        | identifier_or_type_name '(' type_specifier_list ')'
     2731        {
     2732                typedefTable.makeTypedef( *$1 );
     2733                $$ = DeclarationNode::newTypeDecl( $1, $3 );
     2734        }
     2735
     2736type_specifier_list:
     2737        type_specifier
     2738        /* | type_specifier ',' type_specifier_list  */
     2739        | type_specifier_list ',' type_specifier
     2740        {
     2741                $$ = $1->appendList($3);
     2742        }
     2743        ;
     2744
    26972745
    26982746hide_opt:
Note: See TracChangeset for help on using the changeset viewer.