Compare commits

..

No commits in common. "b4f765167bdd9ce1db2224effb699ce86491ea89" and "4ddcbc89adff8e23f57056d38de31d8d37eb81f5" have entirely different histories.

4 changed files with 31 additions and 44 deletions

View File

@ -148,17 +148,14 @@ pub struct Signature {
pub struct TypeBody(pub Vec<Variant>);
#[derive(Debug, PartialEq, Clone)]
pub struct Variant {
pub name: Rc<String>,
pub kind: VariantKind,
}
#[derive(Debug, PartialEq, Clone)]
pub enum VariantKind {
UnitStruct,
TupleStruct(Vec<TypeIdentifier>),
Record(Vec<(Rc<String>, TypeIdentifier)>),
pub enum Variant {
UnitStruct(Rc<String>),
TupleStruct(Rc<String>, Vec<TypeIdentifier>),
Record {
name: Rc<String>,
members: Vec<(Rc<String>, TypeIdentifier)>,
}
}
#[derive(Debug, Derivative, Clone)]
#[derivative(PartialEq)]

View File

@ -447,22 +447,20 @@ impl Parser {
#[recursive_descent_method]
fn variant_specifier(&mut self) -> ParseResult<Variant> {
use self::Variant::*;
let name = self.identifier()?;
let kind = match self.token_handler.peek_kind() {
match self.token_handler.peek_kind() {
LParen => {
let tuple_members = delimited!(self, LParen, type_name, Comma, RParen);
VariantKind::TupleStruct(tuple_members)
Ok(TupleStruct(name, tuple_members))
},
LCurlyBrace => {
let typed_identifier_list = delimited!(self, LCurlyBrace, typed_identifier, Comma, RCurlyBrace);
VariantKind::Record(typed_identifier_list)
Ok(Record {name, members: typed_identifier_list })
},
_ => VariantKind::UnitStruct
};
Ok(Variant {
name,
kind
})
_ => Ok(UnitStruct(name))
}
}
#[recursive_descent_method]

View File

@ -12,7 +12,7 @@ use super::Signature;
use super::TypeIdentifier::*;
use super::TypeSingletonName;
use super::ExpressionKind::*;
use super::VariantKind::*;
use super::Variant::*;
use super::ForBody::*;
fn make_parser(input: &str) -> Parser {
@ -322,27 +322,24 @@ 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![UnitStruct(rc!(Yolo))]), mutable: false} ));
parse_test_wrap_ast!("type mut Yolo = Yolo", decl!(TypeDecl { name: tys!("Yolo"), body: TypeBody(vec![UnitStruct(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 {
name: rc!(Alejandro),
kind: TupleStruct(vec![
UnitStruct(rc!(Miguel)),
TupleStruct(rc!(Alejandro), vec![
Singleton(TypeSingletonName { name: rc!(Int), params: vec![] }),
Singleton(TypeSingletonName { name: rc!(Option), params: vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })] }),
])
},
Variant {
]),
Record{
name: rc!(Esperanza),
kind: Record(vec![
members: vec![
(rc!(a), Singleton(TypeSingletonName { name: rc!(Int), params: vec![] })),
(rc!(b), Singleton(TypeSingletonName { name: rc!(String), params: vec![] })),
])
]
}
]),
mutable: false
@ -352,7 +349,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![UnitStruct(rc!(Diego)), TupleStruct(rc!(Kike), vec![Singleton(TypeSingletonName { name: rc!(a), params: vec![] })])]),
mutable: false
}
)

View File

@ -5,7 +5,7 @@ use std::rc::Rc;
use crate::ast;
use crate::ast::{
Declaration, ItemId, ModuleSpecifier, Statement, StatementKind, TypeBody, TypeSingletonName,
Variant, VariantKind,
Variant,
};
use crate::tokenizing::Location;
use crate::typechecking::TypeName;
@ -123,9 +123,6 @@ pub struct SymbolTable {
/// some basic information about what that symbol is and (ideally) references to other tables
/// (e.g. typechecking tables) with more information about that symbol.
fqsn_to_symbol: HashMap<Fqsn, Symbol>,
//TODO this should probably eventually replace the pair of id_to_fqsn/fqsn_to_symbol
id_to_symbol: HashMap<ItemId, Symbol>,
}
impl SymbolTable {
@ -136,7 +133,6 @@ impl SymbolTable {
types: NameTable::new(),
id_to_fqsn: HashMap::new(),
fqsn_to_symbol: HashMap::new(),
id_to_symbol: HashMap::new(),
}
}
@ -422,9 +418,8 @@ impl SymbolTable {
scope_stack.push(new_scope);
for (index, variant) in variants.iter().enumerate() {
let Variant { name, kind } = variant;
match kind {
VariantKind::UnitStruct => {
match variant {
Variant::UnitStruct(name) => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
let spec = SymbolSpec::DataConstructor {
index,
@ -433,7 +428,7 @@ impl SymbolTable {
};
register(fq_name, spec);
}
VariantKind::TupleStruct(items) => {
Variant::TupleStruct(name, items) => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
let spec = SymbolSpec::DataConstructor {
index,
@ -442,7 +437,7 @@ impl SymbolTable {
};
register(fq_name, spec);
}
VariantKind::Record(members) => {
Variant::Record { name, members } => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
let mut seen_members = HashMap::new();