diff --git a/analysis/src/hir.rs b/analysis/src/hir.rs index 55f4118..28cd93e 100644 --- a/analysis/src/hir.rs +++ b/analysis/src/hir.rs @@ -7,11 +7,12 @@ pub type Symbol = Spur; pub type Ident = Spanned; pub struct Hir<'hir> { - x: &'hir (), + defs: Vec, + __: &'hir (), } -pub struct HirId(u32); -pub struct DefId(HirId); +#[derive(Clone, Copy)] +pub struct DefId(u32); pub enum IntTySignedness { Signed, @@ -37,16 +38,31 @@ pub struct IntTy { pub kind: IntTyKind, } +pub struct Def { + pub name: Ident, + pub def_id: DefId, + pub kind: DefKind, +} + +pub enum DefKind { + Union(UnionTy), + Enum(EnumTy), + Struct(StructTy), +} + pub struct UnionTy { - variants: IndexMap, + pub def_id: DefId, + pub variants: IndexMap, } pub struct StructTy { - fields: IndexMap, + pub def_id: DefId, + pub fields: IndexMap, } pub struct EnumTy { - variants: IndexMap, + pub def_id: DefId, + pub variants: IndexMap, } pub enum TyKind { @@ -65,12 +81,7 @@ pub enum TyKind { } pub struct Ty { - def_id: DefId, -} - -pub struct Node { - id: HirId, - kind: NodeKind, + kind: TyKind, } pub enum NodeKind { @@ -81,7 +92,6 @@ pub struct ExternalDecl; pub struct FunctionDef { name: Symbol, - } pub struct Expr<'hir> { diff --git a/analysis/src/lower.rs b/analysis/src/lower.rs index 4e9f47c..545789b 100644 --- a/analysis/src/lower.rs +++ b/analysis/src/lower.rs @@ -1,12 +1,50 @@ use parser::{ast, Span}; use rustc_hash::FxHashMap; -use crate::hir::{self, Symbol}; +use crate::hir::{self, DefId, Symbol}; pub struct LowerCtx<'hir> { hir_symbol_intern: lasso::Rodeo, hir_arena: &'hir bumpalo::Bump, global_symbols: FxHashMap, + scope: Scope, +} + +struct Scope { + parent: Option>, + variables: FxHashMap, +} + +impl Scope { + fn new() -> Self { + Self { + parent: None, + variables: FxHashMap::default(), + } + } + + fn insert(&mut self, symbol: Symbol, def_id: DefId) { + self.variables.insert(symbol, def_id); + } + + fn enter_new(&mut self) { + let new = Self::new(); + let this = std::mem::replace(self, new); + self.parent = Some(Box::new(this)); + } + + fn leave(&mut self) { + let old = std::mem::replace(self, Self::new()); + let parent = old.parent.expect("parent not found when leaving scope"); + *self = *parent; + } + + fn lookup(&self, sym: Symbol) -> Option { + self.variables + .get(&sym) + .copied() + .or_else(|| self.parent.as_ref().and_then(|parent| parent.lookup(sym))) + } } impl<'hir> LowerCtx<'hir> { @@ -16,10 +54,13 @@ impl<'hir> LowerCtx<'hir> { todo!() } - fn lower_decl(&mut self, decl: &ast::ExternalDecl, _span: Span) -> hir::ExternalDecl { + fn lower_decl(&mut self, decl: &ast::ExternalDecl, span: Span) -> hir::ExternalDecl { match decl { ast::ExternalDecl::Decl(_) => todo!(), - ast::ExternalDecl::FunctionDef(_func) => todo!(), + ast::ExternalDecl::FunctionDef(def) => { + let _fn_def = self.lower_function_def(def, span); + hir::ExternalDecl + } } } @@ -35,11 +76,9 @@ impl<'hir> LowerCtx<'hir> { let name_sym = self.hir_symbol_intern.get_or_intern(name_ident); - if self.global_symbols.contains_key(&name_sym) { panic!("function declarated twice! return this error properly! lol!") } - todo!() }