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

Save progress

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.