Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r45e753c r561354f  
    279279} // DeclarationNode::newEnum
    280280
     281DeclarationNode * DeclarationNode::newAdt( const string * name, DeclarationNode * constructors ) {
     282        assert( name );
     283        DeclarationNode * newnode = new DeclarationNode;
     284        newnode->type = new TypeData( TypeData::Adt );
     285        newnode->type->adt.name = name;
     286        newnode->type->adt.data_constructors = constructors;
     287        return newnode;
     288} // DeclarationNode::newAdt
     289
     290
    281291DeclarationNode * DeclarationNode::newName( const string * name ) {
    282292        DeclarationNode * newnode = new DeclarationNode;
     
    305315        } // if
    306316} // DeclarationNode::newEnumValueGeneric
     317
     318DeclarationNode * DeclarationNode::newDataConstructor( const string * name ) {
     319        DeclarationNode * newnode = newName(name);
     320        return newnode;
     321}
    307322
    308323DeclarationNode * DeclarationNode::newEnumInLine( const string name ) {
     
    10831098        }
    10841099        return nullptr;
     1100}
     1101
     1102std::vector<ast::ptr<ast::StructDecl>> buildDataConstructors( DeclarationNode * firstNode ) {
     1103        std::vector<ast::ptr<ast::StructDecl>> outputList;
     1104        std::back_insert_iterator<std::vector<ast::ptr<ast::StructDecl>>> out( outputList );
     1105        for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
     1106                // td->kind == TypeData::Symbolic
     1107                assert( cur->type->kind == TypeData::Symbolic );
     1108                const std::string * name = cur->name;
     1109                auto ctor = new ast::StructDecl( cur->location,
     1110                        std::string(*name),
     1111                        ast::AggregateDecl::Aggregate::Struct
     1112                );
     1113                ctor->set_body(true);
     1114                TypeData * td = cur->type;
     1115                TypeData::Symbolic_t st = td->symbolic;
     1116                DeclarationNode * params = st.params;
     1117               
     1118                if ( params ) {
     1119                        buildList( params, ctor->members );
     1120                }
     1121
     1122                for ( std::size_t i = 0; i < ctor->members.size(); ++i ) {
     1123                        assert(ctor->members[i]->name == "");
     1124                        ast::Decl * member = ctor->members[i].get_and_mutate();
     1125                        member->name = "field_" + std::to_string(i);
     1126                }
     1127                *out++ = ctor;         
     1128        }
     1129        return outputList;
     1130}
     1131
     1132ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
     1133        ast::UnionDecl * out = new ast::UnionDecl( loc, "temp_data_union" );
     1134        // size_t index = 0;
     1135        if ( typeList.size() > 0 ) out->set_body( true );
     1136        size_t i = 0;
     1137        for (const ast::ptr<ast::StructDecl> structDecl : typeList ) {
     1138                ast::StructInstType * inst = new ast::StructInstType(structDecl);
     1139                ast::ObjectDecl * instObj = new ast::ObjectDecl(
     1140                        structDecl->location,
     1141                        "option_" + std::to_string(i),
     1142                        inst
     1143                );
     1144                i++;
     1145                out->members.push_back( instObj );
     1146
     1147        }
     1148        return out;
     1149}
     1150
     1151ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::StructDecl>> & typeList ) {
     1152        ast::EnumDecl * out = new ast::EnumDecl( loc, "temp_data_tag" );
     1153        if ( typeList.size() > 0 ) out->set_body( true );
     1154        for ( const ast::ptr<ast::StructDecl> structDecl : typeList ) {
     1155                ast::EnumInstType * inst = new ast::EnumInstType( out );
     1156                assert( inst->base != nullptr );
     1157                ast::ObjectDecl * instObj = new ast::ObjectDecl(
     1158                        structDecl->location,
     1159                        structDecl->name,
     1160                        inst
     1161                );
     1162                out->members.push_back( instObj );
     1163        }
     1164        return out;
     1165}
     1166
     1167ast::StructDecl * buildTaggedUnions( const TypeData * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union ) {
     1168        assert( tags->members.size() == data_union->members.size() );
     1169        ast::StructDecl * out = new ast::StructDecl( data->location, *(data->adt.name) );
     1170        out->kind = ast::AggregateDecl::Adt;
     1171
     1172        out->set_body( true );
     1173
     1174        ast::EnumInstType * tag = new ast::EnumInstType( tags );
     1175        ast::ObjectDecl * tag_obj = new ast::ObjectDecl(
     1176                data->location,
     1177                "tag",
     1178                tag
     1179        );
     1180        ast::UnionInstType * value = new ast::UnionInstType( data_union );
     1181        ast::ObjectDecl * value_obj = new ast::ObjectDecl(
     1182                data->location,
     1183                "value",
     1184                value
     1185        );
     1186
     1187        out->members.push_back( value_obj );
     1188        out->members.push_back( tag_obj );
     1189        return out;
    10851190}
    10861191
Note: See TracChangeset for help on using the changeset viewer.