From cac61ba093b32cb74f1376034a528b9f898a8123 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 25 Oct 2021 16:12:24 -0700 Subject: [PATCH] Refactor TypeId representation in symbol table --- schala-lang/language/src/reduced_ir/mod.rs | 2 +- schala-lang/language/src/symbol_table/mod.rs | 38 ++++++++++---------- schala-lang/language/src/typechecking.rs | 23 +++++++++++- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/schala-lang/language/src/reduced_ir/mod.rs b/schala-lang/language/src/reduced_ir/mod.rs index b13da75..f6f43a2 100644 --- a/schala-lang/language/src/reduced_ir/mod.rs +++ b/schala-lang/language/src/reduced_ir/mod.rs @@ -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)), diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index f6ea403..9b5ff26 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -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), + 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, TypeName>, - type_name: TypeName, + members: HashMap, 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: {})( -> {})", - 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(), diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index 538840a..75780a8 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -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; +//TODO need to hook this into the actual typechecking system somehow +#[derive(Debug, Clone)] +pub struct TypeId { + local_name: Rc +} + +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, Type>,