Refactor TypeId representation in symbol table

This commit is contained in:
Greg Shuflin 2021-10-25 16:12:24 -07:00
parent e18ddbded9
commit cac61ba093
3 changed files with 43 additions and 20 deletions

View File

@ -221,7 +221,7 @@ impl<'a> Reducer<'a> {
let def_id = symbol.def_id(); let def_id = symbol.def_id();
match symbol.spec() { match symbol.spec() {
Func(_) => Expression::Lookup(Lookup::Function(def_id.unwrap())), Func => Expression::Lookup(Lookup::Function(def_id.unwrap())),
GlobalBinding => Expression::Lookup(Lookup::GlobalVar(def_id.unwrap())), GlobalBinding => Expression::Lookup(Lookup::GlobalVar(def_id.unwrap())),
LocalVariable => Expression::Lookup(Lookup::LocalVar(def_id.unwrap())), LocalVariable => Expression::Lookup(Lookup::LocalVar(def_id.unwrap())),
FunctionParam(n) => Expression::Lookup(Lookup::Param(n)), FunctionParam(n) => Expression::Lookup(Lookup::Param(n)),

View File

@ -8,7 +8,7 @@ use crate::ast::{
Variant, VariantKind, Variant, VariantKind,
}; };
use crate::tokenizing::Location; use crate::tokenizing::Location;
use crate::typechecking::TypeName; use crate::typechecking::TypeId;
mod resolver; mod resolver;
mod symbol_trie; mod symbol_trie;
@ -265,16 +265,16 @@ impl fmt::Display for Symbol {
//function parameters (even though they are currently assigned). //function parameters (even though they are currently assigned).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum SymbolSpec { pub enum SymbolSpec {
Func(Vec<TypeName>), Func,
DataConstructor { DataConstructor {
index: usize, index: usize,
arity: usize, arity: usize,
type_name: TypeName, //TODO this eventually needs to be some kind of ID type_id: TypeId,
}, },
RecordConstructor { RecordConstructor {
index: usize, index: usize,
members: HashMap<Rc<String>, TypeName>, members: HashMap<Rc<String>, TypeId>,
type_name: TypeName, type_id: TypeId,
}, },
GlobalBinding, //Only for global variables, not for function-local ones or ones within a `let` scope context GlobalBinding, //Only for global variables, not for function-local ones or ones within a `let` scope context
LocalVariable, LocalVariable,
@ -285,22 +285,22 @@ impl fmt::Display for SymbolSpec {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::SymbolSpec::*; use self::SymbolSpec::*;
match self { match self {
Func(type_names) => write!(f, "Func({:?})", type_names), Func => write!(f, "Func"),
DataConstructor { DataConstructor {
index, index,
type_name, type_id,
arity, arity,
} => write!( } => write!(
f, f,
"DataConstructor(idx: {}, arity: {}, type: {})", "DataConstructor(idx: {}, arity: {}, type: {})",
index, arity, type_name index, arity, type_id
), ),
RecordConstructor { RecordConstructor {
type_name, index, .. type_id, index, ..
} => write!( } => write!(
f, f,
"RecordConstructor(idx: {})(<members> -> {})", "RecordConstructor(idx: {})(<members> -> {})",
index, type_name index, type_id
), ),
GlobalBinding => write!(f, "GlobalBinding"), GlobalBinding => write!(f, "GlobalBinding"),
LocalVariable => write!(f, "Local variable"), LocalVariable => write!(f, "Local variable"),
@ -420,7 +420,7 @@ impl SymbolTable {
self.add_symbol( self.add_symbol(
id, id,
fq_function, fq_function,
SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all? SymbolSpec::Func,
); );
} }
StatementKind::Declaration(Declaration::FuncDecl(signature, ..)) => { StatementKind::Declaration(Declaration::FuncDecl(signature, ..)) => {
@ -444,7 +444,7 @@ impl SymbolTable {
self.add_symbol( self.add_symbol(
id, id,
fq_function, fq_function,
SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all? SymbolSpec::Func,
); );
} }
StatementKind::Declaration(Declaration::TypeDecl { name, .. }) => { StatementKind::Declaration(Declaration::TypeDecl { name, .. }) => {
@ -518,13 +518,15 @@ impl SymbolTable {
for (index, variant) in variants.iter().enumerate() { for (index, variant) in variants.iter().enumerate() {
let Variant { name, kind, id } = variant; let Variant { name, kind, id } = variant;
let type_id = TypeId::lookup_name(name.as_ref());
match kind { match kind {
VariantKind::UnitStruct => { VariantKind::UnitStruct => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone()); let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
let spec = SymbolSpec::DataConstructor { let spec = SymbolSpec::DataConstructor {
index, index,
arity: 0, arity: 0,
type_name: name.clone(), type_id,
}; };
register(id, fq_name, spec); register(id, fq_name, spec);
} }
@ -533,7 +535,7 @@ impl SymbolTable {
let spec = SymbolSpec::DataConstructor { let spec = SymbolSpec::DataConstructor {
index, index,
arity: items.len(), arity: items.len(),
type_name: name.clone(), type_id,
}; };
register(id, fq_name, spec); register(id, fq_name, spec);
} }
@ -560,13 +562,13 @@ impl SymbolTable {
let spec = SymbolSpec::RecordConstructor { let spec = SymbolSpec::RecordConstructor {
index, index,
type_name: name.clone(), type_id,
members: members members: members
.iter() .iter()
.map(|(_, _)| { .map(|(member_name, _type_identifier)| {
( (
Rc::new("DUMMY_FIELD".to_string()), member_name.clone(),
Rc::new("DUMMY_TYPE_ID".to_string()), TypeId::lookup_name("DUMMY_TYPE_ID"),
) )
}) })
.collect(), .collect(),

View File

@ -1,5 +1,6 @@
use std::rc::Rc; use std::rc::Rc;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt;
use ena::unify::{UnifyKey, InPlaceUnificationTable, UnificationTable, EqUnifyValue}; use ena::unify::{UnifyKey, InPlaceUnificationTable, UnificationTable, EqUnifyValue};
@ -21,7 +22,27 @@ impl TypeData {
} }
} }
pub type TypeName = Rc<String>; //TODO need to hook this into the actual typechecking system somehow
#[derive(Debug, Clone)]
pub struct TypeId {
local_name: Rc<String>
}
impl TypeId {
//TODO this is definitely incomplete
pub fn lookup_name(name: &str) -> TypeId {
TypeId {
local_name: Rc::new(name.to_string())
}
}
}
impl fmt::Display for TypeId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TypeId:{}", self.local_name)
}
}
pub struct TypeContext<'a> { pub struct TypeContext<'a> {
variable_map: ScopeStack<'a, Rc<String>, Type>, variable_map: ScopeStack<'a, Rc<String>, Type>,