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();
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())),
LocalVariable => Expression::Lookup(Lookup::LocalVar(def_id.unwrap())),
FunctionParam(n) => Expression::Lookup(Lookup::Param(n)),

View File

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

View File

@ -1,5 +1,6 @@
use std::rc::Rc;
use std::convert::TryFrom;
use std::fmt;
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> {
variable_map: ScopeStack<'a, Rc<String>, Type>,