From b342213826456b0f7c54167c9909aa8764769d1c Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 21 Oct 2021 20:00:26 -0700 Subject: [PATCH] Add ids to type Variants --- schala-lang/language/src/ast/mod.rs | 5 ++++- schala-lang/language/src/parsing/mod.rs | 1 + schala-lang/language/src/parsing/test.rs | 12 ++++++++---- schala-lang/language/src/symbol_table/mod.rs | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/schala-lang/language/src/ast/mod.rs b/schala-lang/language/src/ast/mod.rs index ffd212c..3f54d24 100644 --- a/schala-lang/language/src/ast/mod.rs +++ b/schala-lang/language/src/ast/mod.rs @@ -147,8 +147,11 @@ pub struct Signature { #[derive(Debug, PartialEq, Clone)] pub struct TypeBody(pub Vec); -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Derivative, Clone)] +#[derivative(PartialEq)] pub struct Variant { + #[derivative(PartialEq="ignore")] + pub id: ItemId, pub name: Rc, pub kind: VariantKind, } diff --git a/schala-lang/language/src/parsing/mod.rs b/schala-lang/language/src/parsing/mod.rs index 1ea5171..c05a83f 100644 --- a/schala-lang/language/src/parsing/mod.rs +++ b/schala-lang/language/src/parsing/mod.rs @@ -460,6 +460,7 @@ impl Parser { _ => VariantKind::UnitStruct }; Ok(Variant { + id: self.id_store.fresh(), name, kind }) diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index d7b10f3..5ec8826 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -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) | 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 = 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 } ) diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index 6356cb8..ea965e8 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -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());