Add ids to type Variants

This commit is contained in:
Greg Shuflin 2021-10-21 20:00:26 -07:00
parent b4f765167b
commit b342213826
4 changed files with 14 additions and 6 deletions

View File

@ -147,8 +147,11 @@ pub struct Signature {
#[derive(Debug, PartialEq, Clone)]
pub struct TypeBody(pub Vec<Variant>);
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, Derivative, Clone)]
#[derivative(PartialEq)]
pub struct Variant {
#[derivative(PartialEq="ignore")]
pub id: ItemId,
pub name: Rc<String>,
pub kind: VariantKind,
}

View File

@ -460,6 +460,7 @@ impl Parser {
_ => VariantKind::UnitStruct
};
Ok(Variant {
id: self.id_store.fresh(),
name,
kind
})

View File

@ -322,15 +322,18 @@ fn parsing_strings() {
#[test]
fn parsing_types() {
parse_test_wrap_ast!("type Yolo = Yolo", decl!(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![Variant { kind: UnitStruct, name: rc!(Yolo) }]), mutable: false} ));
parse_test_wrap_ast!("type mut Yolo = Yolo", decl!(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![Variant { kind: UnitStruct, name: rc!(Yolo) }]), mutable: true} ));
parse_test_wrap_ast!("type Yolo = Yolo", decl!(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![Variant { id: ItemId::default(), kind: UnitStruct, name: rc!(Yolo) }]), mutable: false} ));
parse_test_wrap_ast!("type mut Yolo = Yolo", decl!(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![Variant { id: ItemId::default(), kind: UnitStruct, name: rc!(Yolo) }]), mutable: true} ));
parse_test_wrap_ast!("type alias Sex = Drugs", decl!(TypeAlias { alias: rc!(Sex), original: rc!(Drugs) }));
parse_test_wrap_ast!("type Sanchez = Miguel | Alejandro(Int, Option<a>) | Esperanza { a: Int, b: String }",
decl!(TypeDecl {
name: tys!("Sanchez"),
body: TypeBody(vec![
Variant { kind: UnitStruct, name: rc!(Miguel) },
Variant {
id: ItemId::default(),
kind: UnitStruct, name: rc!(Miguel) },
Variant {
id: ItemId::default(),
name: rc!(Alejandro),
kind: TupleStruct(vec![
Singleton(TypeSingletonName { name: rc!(Int), params: vec![] }),
@ -338,6 +341,7 @@ fn parsing_types() {
])
},
Variant {
id: ItemId::default(),
name: rc!(Esperanza),
kind: Record(vec![
(rc!(a), Singleton(TypeSingletonName { name: rc!(Int), params: vec![] })),
@ -352,7 +356,7 @@ fn parsing_types() {
"type Jorge<a> = Diego | Kike(a)",
decl!(TypeDecl{
name: TypeSingletonName { name: rc!(Jorge), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] },
body: TypeBody(vec![Variant{ kind: UnitStruct, name: rc!(Diego) }, Variant { name: rc!(Kike), kind: TupleStruct( vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })]) }]),
body: TypeBody(vec![Variant{ id: ItemId::default(), kind: UnitStruct, name: rc!(Diego) }, Variant { id: ItemId::default(), name: rc!(Kike), kind: TupleStruct( vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })]) }]),
mutable: false
}
)

View File

@ -422,7 +422,7 @@ impl SymbolTable {
scope_stack.push(new_scope);
for (index, variant) in variants.iter().enumerate() {
let Variant { name, kind } = variant;
let Variant { name, kind, id } = variant;
match kind {
VariantKind::UnitStruct => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());