QualifiedName with id

This commit is contained in:
greg 2019-09-19 01:34:21 -07:00
parent 56e6eb44f9
commit c9052e0a3b
6 changed files with 43 additions and 27 deletions

View File

@ -101,8 +101,13 @@ pub enum StatementKind {
pub type Block = Vec<Meta<Statement>>; pub type Block = Vec<Meta<Statement>>;
pub type ParamName = Rc<String>; pub type ParamName = Rc<String>;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, Derivative, Clone)]
pub struct QualifiedName(pub Vec<Rc<String>>); #[derivative(PartialEq)]
pub struct QualifiedName {
#[derivative(PartialEq="ignore")]
pub id: ItemId,
pub components: Vec<Rc<String>>,
}
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct FormalParam { pub struct FormalParam {

View File

@ -775,17 +775,17 @@ impl Parser {
#[recursive_descent_method] #[recursive_descent_method]
fn qualified_identifier(&mut self) -> ParseResult<QualifiedName> { fn qualified_identifier(&mut self) -> ParseResult<QualifiedName> {
let mut vec = vec![self.identifier()?]; let mut components = vec![self.identifier()?];
loop { loop {
match (self.token_handler.peek_kind(), self.token_handler.peek_kind_n(1)) { match (self.token_handler.peek_kind(), self.token_handler.peek_kind_n(1)) {
(Colon, Colon) => { (Colon, Colon) => {
self.token_handler.next(); self.token_handler.next(); self.token_handler.next(); self.token_handler.next();
vec.push(self.identifier()?); components.push(self.identifier()?);
}, },
_ => break, _ => break,
} }
} }
Ok(QualifiedName(vec)) Ok(QualifiedName { id: self.id_store.fresh(), components })
} }
#[recursive_descent_method] #[recursive_descent_method]

View File

@ -30,8 +30,19 @@ macro_rules! parse_test_wrap_ast {
macro_rules! parse_error { macro_rules! parse_error {
($string:expr) => { assert!(parse($string).is_err()) } ($string:expr) => { assert!(parse($string).is_err()) }
} }
macro_rules! qname {
( $( $component:expr),* ) => {
{
let mut components = vec![];
$(
components.push(rc!($component));
)*
QualifiedName { components, id: ItemIdStore::new_id() }
}
};
}
macro_rules! val { macro_rules! val {
($var:expr) => { Value(Meta::new(QualifiedName(vec![Rc::new($var.to_string())]))) }; ($var:expr) => { Value(Meta::new(QualifiedName { components: vec![Rc::new($var.to_string())], id: ItemIdStore::new_id() })) };
} }
macro_rules! ty { macro_rules! ty {
($name:expr) => { Singleton(tys!($name)) } ($name:expr) => { Singleton(tys!($name)) }
@ -163,10 +174,10 @@ fn parsing_identifiers() {
parse_test_wrap_ast!("None", exst!(val!("None"))); parse_test_wrap_ast!("None", exst!(val!("None")));
parse_test_wrap_ast!("Pandas { a: x + y }", parse_test_wrap_ast!("Pandas { a: x + y }",
exst!(NamedStruct { name: Meta::new(QualifiedName(vec![rc!(Pandas)])), fields: vec![(rc!(a), ex!(m binexp!("+", val!("x"), val!("y"))))]}) exst!(NamedStruct { name: Meta::new(qname!(Pandas)), fields: vec![(rc!(a), ex!(m binexp!("+", val!("x"), val!("y"))))]})
); );
parse_test_wrap_ast! { "Pandas { a: n, b: q, }", parse_test_wrap_ast! { "Pandas { a: n, b: q, }",
exst!(NamedStruct { name: Meta::new(QualifiedName(vec![rc!(Pandas)])), fields: exst!(NamedStruct { name: Meta::new(qname!(Pandas)), fields:
vec![(rc!(a), ex!(m val!("n"))), (rc!(b), ex!(m val!("q")))] vec![(rc!(a), ex!(m val!("n"))), (rc!(b), ex!(m val!("q")))]
} }
) )
@ -179,13 +190,13 @@ fn qualified_identifiers() {
parse_test_wrap_ast! { parse_test_wrap_ast! {
"let q_q = Yolo::Swaggins", "let q_q = Yolo::Swaggins",
Meta::new(decl!(Binding { name: rc!(q_q), constant: true, type_anno: None, Meta::new(decl!(Binding { name: rc!(q_q), constant: true, type_anno: None,
expr: Meta::new(Expression::new(ItemIdStore::new_id(), Value(Meta::new(QualifiedName(vec![rc!(Yolo), rc!(Swaggins)]))))), expr: Meta::new(Expression::new(ItemIdStore::new_id(), Value(Meta::new(qname!(Yolo, Swaggins))))),
})) }))
} }
parse_test_wrap_ast! { parse_test_wrap_ast! {
"thing::item::call()", "thing::item::call()",
exst!(Call { f: bx![ex!(m Value(Meta::new(QualifiedName(vec![rc!(thing), rc!(item), rc!(call)]))))], arguments: vec![] }) exst!(Call { f: bx![ex!(m Value(Meta::new(qname!(thing, item, call))))], arguments: vec![] })
} }
} }
@ -609,8 +620,8 @@ fn patterns() {
"if x is Some(a) then { 4 } else { 9 }", exst!( "if x is Some(a) then { 4 } else { 9 }", exst!(
IfExpression { IfExpression {
discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))),
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(Meta::new(QualifiedName(vec![rc!(Some)])), body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(Meta::new(qname!(Some)),
vec![Pattern::VarOrName(Meta::new(QualifiedName(vec![rc!(a)])))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) } vec![Pattern::VarOrName(Meta::new(qname!(a)))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) }
) )
} }
@ -618,8 +629,8 @@ fn patterns() {
"if x is Some(a) then 4 else 9", exst!( "if x is Some(a) then 4 else 9", exst!(
IfExpression { IfExpression {
discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))),
body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(Meta::new(QualifiedName(vec![rc!(Some)])), body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(Meta::new(qname!(Some)),
vec![Pattern::VarOrName(Meta::new(QualifiedName(vec![rc!(a)])))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) } vec![Pattern::VarOrName(Meta::new(qname!(a)))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) }
) )
} }
@ -628,9 +639,9 @@ fn patterns() {
IfExpression { IfExpression {
discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))), discriminator: bx!(Discriminator::Simple(Meta::new(ex!(s "x")))),
body: bx!(IfExpressionBody::SimplePatternMatch( body: bx!(IfExpressionBody::SimplePatternMatch(
Pattern::Record(Meta::new(QualifiedName(vec![rc!(Something)])), vec![ Pattern::Record(Meta::new(qname!(Something)), vec![
(rc!(a),Pattern::Literal(PatternLiteral::StringPattern(rc!(a)))), (rc!(a),Pattern::Literal(PatternLiteral::StringPattern(rc!(a)))),
(rc!(b),Pattern::VarOrName(Meta::new(QualifiedName(vec![rc!(x)])))) (rc!(b),Pattern::VarOrName(Meta::new(qname!(x))))
]), ]),
vec![exst!(s "4")], Some(vec![exst!(s "9")]))) vec![exst!(s "4")], Some(vec![exst!(s "9")])))
} }

View File

@ -360,9 +360,9 @@ fn handle_symbol(symbol: Option<&Symbol>, inner_patterns: &Vec<Pattern>, symbol_
if symbol_exists { if symbol_exists {
None None
} else { } else {
let QualifiedName(name_elems) = meta_name.node(); let QualifiedName { components, .. } = meta_name.node();
if name_elems.len() == 1 { if components.len() == 1 {
Some(name_elems[0].clone()) Some(components[0].clone())
} else { } else {
panic!("Bad variable name in pattern"); panic!("Bad variable name in pattern");
} }
@ -415,11 +415,11 @@ impl Pattern {
fn to_subpattern(&self, symbol_table: &SymbolTable) -> Subpattern { fn to_subpattern(&self, symbol_table: &SymbolTable) -> Subpattern {
use self::Pattern::*; use self::Pattern::*;
match self { match self {
TupleStruct( Meta { n: QualifiedName(vec), fqsn, .. }, inner_patterns) => { TupleStruct( Meta { n: QualifiedName{ components, .. }, fqsn, .. }, inner_patterns) => {
match fqsn.as_ref().and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) { match fqsn.as_ref().and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) {
Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table), Some(symbol) => handle_symbol(Some(symbol), inner_patterns, symbol_table),
None => { None => {
panic!("Symbol {:?} not found", QualifiedName(vec.to_vec())); panic!("Symbol {:?} not found", components);
} }
} }
}, },
@ -429,16 +429,16 @@ impl Pattern {
}, },
Ignored => Subpattern { tag: None, subpatterns: vec![], guard: None, bound_vars: vec![] }, Ignored => Subpattern { tag: None, subpatterns: vec![], guard: None, bound_vars: vec![] },
Literal(lit) => lit.to_subpattern(symbol_table), Literal(lit) => lit.to_subpattern(symbol_table),
VarOrName(Meta { n: QualifiedName(vec), fqsn, .. }) => { VarOrName(Meta { n: QualifiedName { components, .. }, fqsn, .. }) => {
// if fqsn is Some, treat this as a symbol pattern. If it's None, treat it // if fqsn is Some, treat this as a symbol pattern. If it's None, treat it
// as a variable. // as a variable.
println!("Calling VarOrName reduction with : {:?}", vec); println!("Calling VarOrName reduction with : {:?}", components);
match fqsn.as_ref().and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) { match fqsn.as_ref().and_then(|fqsn| symbol_table.lookup_by_fqsn(&fqsn)) {
Some(symbol) => handle_symbol(Some(symbol), &vec![], symbol_table), Some(symbol) => handle_symbol(Some(symbol), &vec![], symbol_table),
None => { None => {
let name = if vec.len() == 1 { let name = if components.len() == 1 {
vec[0].clone() components[0].clone()
} else { } else {
panic!("check this line of code yo"); panic!("check this line of code yo");
}; };

View File

@ -152,7 +152,7 @@ impl<'a> ScopeResolver<'a> {
//TODO this is incomplete //TODO this is incomplete
fn lookup_name_in_scope(sym_name: &QualifiedName) -> FullyQualifiedSymbolName { fn lookup_name_in_scope(sym_name: &QualifiedName) -> FullyQualifiedSymbolName {
let QualifiedName(vec) = sym_name; let QualifiedName { components: vec, .. } = sym_name;
let len = vec.len(); let len = vec.len();
let new_vec: Vec<ScopeSegment> = vec.iter().enumerate().map(|(i, name)| { let new_vec: Vec<ScopeSegment> = vec.iter().enumerate().map(|(i, name)| {
let kind = if i == (len - 1) { let kind = if i == (len - 1) {

View File

@ -414,7 +414,7 @@ impl<'a> TypeContext<'a> {
} }
fn handle_value(&mut self, val: &QualifiedName) -> InferResult<Type> { fn handle_value(&mut self, val: &QualifiedName) -> InferResult<Type> {
let QualifiedName(vec) = val; let QualifiedName { components: vec, .. } = val;
let var = &vec[0]; let var = &vec[0];
match self.variable_map.lookup(var) { match self.variable_map.lookup(var) {
Some(ty) => Ok(ty.clone()), Some(ty) => Ok(ty.clone()),