what did this commit even do

This commit is contained in:
Michael Zhang 2021-02-03 02:30:41 -06:00
parent 02011dfe44
commit e836c6f110
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
5 changed files with 55 additions and 12 deletions

View file

@ -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,
)?;

View file

@ -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;

View file

@ -11,20 +11,44 @@ use petgraph::{algo, graphmap::DiGraphMap};
use crate::ast::*;
use crate::utils::Id;
type HDecls = HashMap<Id, HDecl>;
type Graph = DiGraphMap<Id, ()>;
type Scopes = Vec<HashSet<String>>;
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<Id, Func>,
types: HashMap<Id, Type>,
hfuncs: HashMap<Id, HFunc>,
func_names: HashMap<String, Id>,
type_names: HashMap<String, Id>,
}
#[derive(Debug)]
pub struct NamesResolved {
pub decls: HashMap<Id, HDecl>,
pub visit_order: Vec<HashSet<Id>>,
}
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) {

View file

@ -0,0 +1,6 @@
//! Type checking
//! ===
//!
//! This module implements a Hindley-Milner based type checker.
pub fn type_check() {}

View file

@ -8,6 +8,7 @@ lazy_static! {
static ref N: Arc<Mutex<usize>> = 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;