This commit is contained in:
nora 2022-10-24 16:09:03 +02:00
parent 4abbd3ca76
commit 1407eb299a
No known key found for this signature in database
2 changed files with 67 additions and 18 deletions

View file

@ -7,11 +7,12 @@ pub type Symbol = Spur;
pub type Ident = Spanned<Symbol>; pub type Ident = Spanned<Symbol>;
pub struct Hir<'hir> { pub struct Hir<'hir> {
x: &'hir (), defs: Vec<Def>,
__: &'hir (),
} }
pub struct HirId(u32); #[derive(Clone, Copy)]
pub struct DefId(HirId); pub struct DefId(u32);
pub enum IntTySignedness { pub enum IntTySignedness {
Signed, Signed,
@ -37,16 +38,31 @@ pub struct IntTy {
pub kind: IntTyKind, 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 { pub struct UnionTy {
variants: IndexMap<Symbol, Ty>, pub def_id: DefId,
pub variants: IndexMap<Symbol, Ty>,
} }
pub struct StructTy { pub struct StructTy {
fields: IndexMap<Symbol, Ty>, pub def_id: DefId,
pub fields: IndexMap<Symbol, Ty>,
} }
pub struct EnumTy { pub struct EnumTy {
variants: IndexMap<Symbol, i128>, pub def_id: DefId,
pub variants: IndexMap<Symbol, i128>,
} }
pub enum TyKind { pub enum TyKind {
@ -65,12 +81,7 @@ pub enum TyKind {
} }
pub struct Ty { pub struct Ty {
def_id: DefId, kind: TyKind,
}
pub struct Node {
id: HirId,
kind: NodeKind,
} }
pub enum NodeKind { pub enum NodeKind {
@ -81,7 +92,6 @@ pub struct ExternalDecl;
pub struct FunctionDef { pub struct FunctionDef {
name: Symbol, name: Symbol,
} }
pub struct Expr<'hir> { pub struct Expr<'hir> {

View file

@ -1,12 +1,50 @@
use parser::{ast, Span}; use parser::{ast, Span};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use crate::hir::{self, Symbol}; use crate::hir::{self, DefId, Symbol};
pub struct LowerCtx<'hir> { pub struct LowerCtx<'hir> {
hir_symbol_intern: lasso::Rodeo, hir_symbol_intern: lasso::Rodeo,
hir_arena: &'hir bumpalo::Bump, hir_arena: &'hir bumpalo::Bump,
global_symbols: FxHashMap<Symbol, &'hir hir::ExternalDecl>, global_symbols: FxHashMap<Symbol, &'hir hir::ExternalDecl>,
scope: Scope,
}
struct Scope {
parent: Option<Box<Scope>>,
variables: FxHashMap<Symbol, DefId>,
}
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<DefId> {
self.variables
.get(&sym)
.copied()
.or_else(|| self.parent.as_ref().and_then(|parent| parent.lookup(sym)))
}
} }
impl<'hir> LowerCtx<'hir> { impl<'hir> LowerCtx<'hir> {
@ -16,10 +54,13 @@ impl<'hir> LowerCtx<'hir> {
todo!() 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 { match decl {
ast::ExternalDecl::Decl(_) => todo!(), 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,12 +76,10 @@ impl<'hir> LowerCtx<'hir> {
let name_sym = self.hir_symbol_intern.get_or_intern(name_ident); let name_sym = self.hir_symbol_intern.get_or_intern(name_ident);
if self.global_symbols.contains_key(&name_sym) { if self.global_symbols.contains_key(&name_sym) {
panic!("function declarated twice! return this error properly! lol!") panic!("function declarated twice! return this error properly! lol!")
} }
todo!() todo!()
} }
} }