From e836c6f110325b65d690fddeb6e0ff289bf3600f Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Wed, 3 Feb 2021 02:30:41 -0600 Subject: [PATCH] what did this commit even do --- src/codegen.rs | 4 ++-- src/main.rs | 7 ++++--- src/name_res.rs | 47 +++++++++++++++++++++++++++++++++++++++++------ src/type_check.rs | 6 ++++++ src/utils.rs | 3 ++- 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/codegen.rs b/src/codegen.rs index c0caa55..5fc263b 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -2,7 +2,7 @@ use anyhow::Result; use cranelift::prelude::*; use cranelift_module::{Linkage, Module}; -use super::{Decl, Program}; +use crate::ast::{Decl, Program}; impl Program { pub fn codegen(&self, mut module: impl Module) -> Result<()> { @@ -26,7 +26,7 @@ impl Program { ctx.func.signature.returns.push(AbiParam::new(int)); let func_id = module.declare_function( - &func.name, + &func.name.0, Linkage::Export, &ctx.func.signature, )?; diff --git a/src/main.rs b/src/main.rs index 5818467..8d9d4f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ pub mod ast; +pub mod codegen; pub mod name_res; pub mod parser; +pub mod type_check; pub mod utils; use std::fs::{File, OpenOptions}; @@ -31,11 +33,10 @@ fn main() -> Result<()> { }; let parsed = parser::parse(&contents)?; - println!("parsed: {:?}", parsed); let namespaces = Namespaces::create(parsed); - println!("namespaces: {:?}", namespaces); - let tree = namespaces.name_resolution(); + let resolved = namespaces.name_resolution(); + println!("resolved: {:?}", resolved); let isa = { use std::str::FromStr; diff --git a/src/name_res.rs b/src/name_res.rs index ee7ddcb..b1b4d61 100644 --- a/src/name_res.rs +++ b/src/name_res.rs @@ -11,20 +11,44 @@ use petgraph::{algo, graphmap::DiGraphMap}; use crate::ast::*; use crate::utils::Id; +type HDecls = HashMap; type Graph = DiGraphMap; type Scopes = Vec>; -pub enum HExpr {} +#[derive(Debug)] +pub enum HDecl { + Func(HFunc), +} + +#[derive(Debug)] +pub struct HFunc {} + +#[derive(Debug)] +pub struct HExpr {} + +#[derive(Debug)] +pub struct HStmt {} + +#[derive(Debug)] +pub struct HType {} #[derive(Debug)] pub struct Namespaces { funcs: HashMap, types: HashMap, + hfuncs: HashMap, + func_names: HashMap, type_names: HashMap, } +#[derive(Debug)] +pub struct NamesResolved { + pub decls: HashMap, + pub visit_order: Vec>, +} + impl Namespaces { pub fn create(program: Program) -> Self { let mut funcs = HashMap::new(); @@ -46,27 +70,35 @@ impl Namespaces { Namespaces { funcs, types, + hfuncs: HashMap::new(), func_names, type_names, } } - pub fn name_resolution(&self) { + pub fn name_resolution(&self) -> NamesResolved { + let mut hdecls = HashMap::new(); let mut graph = Graph::new(); for (id, func) in self.funcs.iter() { - walk_func(&mut graph, &self, *id, func); + walk_func(&mut hdecls, &mut graph, &self, *id, func); } - println!("graph: {:?}", graph); + let mut visit_order = Vec::new(); let scc = algo::tarjan_scc(&graph); for group in scc.iter() { - println!("group: {:?}", group); + let set = group.iter().cloned().collect(); + visit_order.push(set); + } + + NamesResolved { + decls: hdecls, + visit_order, } } } -fn walk_func(g: &mut Graph, n: &Namespaces, src: Id, f: &Func) { +fn walk_func(h: &mut HDecls, g: &mut Graph, n: &Namespaces, src: Id, f: &Func) { let mut scopes = Vec::new(); scopes.push(HashSet::new()); for s in f.stmts.iter() { @@ -77,6 +109,9 @@ fn walk_func(g: &mut Graph, n: &Namespaces, src: Id, f: &Func) { } scopes.pop(); assert!(scopes.is_empty()); + + let func = HFunc {}; + h.insert(src, HDecl::Func(func)); } fn walk_stmt(g: &mut Graph, n: &Namespaces, src: Id, sc: &mut Scopes, s: &Stmt) { diff --git a/src/type_check.rs b/src/type_check.rs index e69de29..22bbea8 100644 --- a/src/type_check.rs +++ b/src/type_check.rs @@ -0,0 +1,6 @@ +//! Type checking +//! === +//! +//! This module implements a Hindley-Milner based type checker. + +pub fn type_check() {} diff --git a/src/utils.rs b/src/utils.rs index 557b209..bf5e006 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,6 +8,7 @@ lazy_static! { static ref N: Arc> = Arc::new(Mutex::new(0)); } +/// An Id is a uniquely generated value, whose implementation is opaque. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct Id(usize); @@ -17,7 +18,7 @@ impl Default for Id { } } -pub fn gen_int() -> usize { +fn gen_int() -> usize { let mut n = N.lock(); let res = *n; *n += 1;