M Cargo.lock +40 -0
@@ 242,6 242,19 @@ source = "registry+https://github.com/ru
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]]
+name = "env_logger"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
+dependencies = [
+ "atty",
+ "humantime",
+ "log",
+ "regex",
+ "termcolor",
+]
+
+[[package]]
name = "errno"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ 294,8 307,10 @@ dependencies = [
"codespan-reporting",
"criterion",
"lang_tester",
+ "log",
"logos",
"once_cell",
+ "pretty_env_logger",
"tempfile",
]
@@ 339,6 354,15 @@ dependencies = [
]
[[package]]
+name = "humantime"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
+dependencies = [
+ "quick-error",
+]
+
+[[package]]
name = "indexmap"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ 538,6 562,16 @@ dependencies = [
]
[[package]]
+name = "pretty_env_logger"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
+dependencies = [
+ "env_logger",
+ "log",
+]
+
+[[package]]
name = "proc-macro2"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ 547,6 581,12 @@ dependencies = [
]
[[package]]
+name = "quick-error"
+version = "1.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
+
+[[package]]
name = "quote"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
M Cargo.toml +2 -0
@@ 23,6 23,8 @@ argh = "0.1"
codespan-reporting = "0.11"
once_cell = "1"
logos = "0.12"
+log = "0.4"
+pretty_env_logger = "0.4"
[dev-dependencies]
M src/backend/rust.rs +4 -2
@@ 11,6 11,8 @@
use std::borrow::Cow;
use std::io::{self, Write};
+use log::*;
+
use crate::hir;
use crate::typeck::Tck;
use crate::*;
@@ 60,7 62,7 @@ fn compile_typename(t: &Type) -> Cow<'st
}
Prim(PrimType::Bool) => "bool".into(),
Named(s, types) if s == &Sym::new("Tuple") => {
- println!("Compiling tuple {:?}...", t);
+ trace!("Compiling tuple {:?}...", t);
let mut accm = String::from("(");
for typ in types {
accm += &compile_typename(&*typ);
@@ 349,7 351,7 @@ fn compile_expr(expr: &hir::ExprNode, tc
// TODO: Someday this should just be filled in by a lowering pass
let type_id = tck.get_expr_type(init);
let typ = tck.reconstruct(type_id).expect("Passed typechecking but failed to reconstruct during codegen, should never happen!");
- println!("Type for let statement is {:?}", typ);
+ trace!("Type for let statement is {:?}", typ);
let tstr = compile_typename(&typ);
let istr = compile_expr(init, tck);
if *mutable {
M src/bin/garnetc.rs +2 -0
@@ 4,6 4,7 @@ use std::io;
use std::path::{Path, PathBuf};
use argh::FromArgs;
+use pretty_env_logger;
use garnet::backend::Backend;
@@ 72,6 73,7 @@ fn compile_rust(input_file: &Path, exe_n
}
fn main() -> std::io::Result<()> {
+ pretty_env_logger::init();
let opt: Opt = argh::from_env();
let exe_name = if let Some(out) = &opt.out {
M src/hir.rs +2 -3
@@ 730,7 730,7 @@ fn lower_typedef(accm: &mut Vec<Decl>, n
// TODO: What do we do with the generics... Right now they just
// get stuffed into the constructor functions verbatim.
Type::Sum(body, generics) => {
- println!("Lowering sum type {}", name);
+ trace!("Lowering sum type {}", name);
let struct_body: Vec<_> = body
.iter()
.map(|(variant_name, variant_type)| {
@@ 778,7 778,7 @@ fn lower_typedef(accm: &mut Vec<Decl>, n
// For other types, we create a constructor function to build them.
other => {
let s = Sym::new("x");
- println!("Lowering params {:?}", params);
+ trace!("Lowering params {:?}", params);
let type_params: Vec<_> = params.iter().map(|s| Type::Generic(*s)).collect();
let signature = ast::Signature {
params: vec![(s, other.clone())],
@@ 854,7 854,6 @@ fn lower_decls(decls: &[ast::Decl]) -> I
}
let i = Ir { decls: accm };
- println!("{}", i);
i
}
M src/lib.rs +5 -1
@@ 6,6 6,8 @@ use std::collections::{BTreeMap, BTreeSe
use std::fmt;
use std::sync::Arc;
+use log::*;
+
pub mod ast;
pub mod backend;
pub mod format;
@@ 185,7 187,7 @@ impl Type {
}
let mut accm = vec![];
helper(self, &mut accm);
- println!("Found type params for {:?}: {:?}", self, accm);
+ trace!("Found type params for {:?}: {:?}", self, accm);
accm
}
@@ 465,9 467,11 @@ pub fn try_compile(
parser.parse()
};
let hir = hir::lower(&ast);
+ info!("HIR from AST lowering:\n{}", &hir);
let hir = passes::run_passes(hir);
let tck = &mut typeck::typecheck(&hir)?;
let hir = passes::run_typechecked_passes(hir, tck);
+ info!("HIR after transform passes:\n{}", &hir);
Ok(backend::output(backend, &hir, tck))
}
M src/passes.rs +4 -4
@@ 14,6 14,8 @@
//!
//! TODO also: monomorphization
+use log::*;
+
use crate::hir::{Decl as D, Expr as E, ExprNode, Ir};
use crate::*;
@@ 33,8 35,6 @@ pub fn run_typechecked_passes(ir: Ir, tc
//let passes: &[TckPass] = &[nameify, struct_to_tuple];
let passes: &[TckPass] = &[struct_to_tuple, monomorphize];
let res = passes.iter().fold(ir, |prev_ir, f| f(prev_ir, tck));
- println!();
- println!("{}", res);
res
}
@@ 694,7 694,7 @@ fn tuplize_type(typ: Type) -> Type {
///
/// TODO: Do something with the expr types?
fn tuplize_expr(expr: ExprNode, tck: &mut typeck::Tck) -> ExprNode {
- println!("Tuplizing expr {:?}", expr);
+ trace!("Tuplizing expr {:?}", expr);
let expr_typeid = tck.get_expr_type(&expr);
let struct_type = tck.reconstruct(expr_typeid).expect("Should never happen?");
let new_contents = match &*expr.e {
@@ 919,7 919,7 @@ mod tests {
#[test]
fn test_expr_map() {
fn swap_binop_args(expr: ExprNode) -> ExprNode {
- println!("Swapping {:?}", expr);
+ trace!("Swapping {:?}", expr);
expr.map(&mut |e| match e {
E::BinOp { op, lhs, rhs } => E::BinOp {
op,
M src/typeck.rs +22 -16
@@ 7,6 7,8 @@ use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::rc::Rc;
+use log::*;
+
use crate::*;
use crate::hir;
@@ 670,7 672,7 @@ impl Tck {
let mut vars_report: Vec<_> = self.vars.iter().collect();
vars_report.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));
for (k, v) in vars_report.iter() {
- print!(" ${} => {:?}\n", k.0, v);
+ trace!(" ${} => {:?}\n", k.0, v);
}
}
@@ 1191,9 1193,11 @@ fn typecheck_expr(
typecheck_expr(tck, symtbl, func_rettype, e)?;
let tuple_type = tck.get_expr_type(e);
let field_type = tck.get_tuple_field_type(symtbl, tuple_type, *elt);
- println!(
+ trace!(
"Heckin tuple ref... Type of {:?}.{} is {:?}",
- e, elt, field_type
+ e,
+ elt,
+ field_type
);
tck.set_expr_type(expr, field_type);
@@ 1204,11 1208,11 @@ fn typecheck_expr(
.iter()
.map(|(name, expr)| {
// ? in map doesn't work too well...
- println!("Checking field {} expr {:?}", name, expr);
+ trace!("Checking field {} expr {:?}", name, expr);
typecheck_expr(tck, symtbl, func_rettype, expr).and_then(|t| (Ok((*name, t))))
})
.collect();
- println!("Typechecking struct ctor: {:?}", body_types);
+ trace!("Typechecking struct ctor: {:?}", body_types);
let body_types = body_types?;
let struct_type = TypeInfo::Struct(body_types);
let typeid = tck.insert(struct_type);
@@ 1221,9 1225,11 @@ fn typecheck_expr(
// struct_type is the type of the struct... but the
// type of this structref expr is the type of the *field in the struct*.
let struct_field_type = tck.get_struct_field_type(symtbl, struct_type, *elt);
- println!(
+ trace!(
"Heckin struct ref... Type of {:?}.{} is {:?}",
- expr, elt, struct_field_type
+ expr,
+ elt,
+ struct_field_type
);
tck.set_expr_type(expr, struct_field_type);
@@ 1289,7 1295,7 @@ fn typecheck_expr(
body,
} => {
let named_type = symtbl.get_type(*name).expect("Unknown type constructor");
- println!("Got type named {}: is {:?}", name, named_type);
+ trace!("Got type named {}: is {:?}", name, named_type);
// Ok if we have declared type params we gotta instantiate them
// to match the type's generics.
//let type_param_names = named_type.get_generic_args();
@@ 1303,13 1309,13 @@ fn typecheck_expr(
type_params
);
let tid = tck.instantiate(&named_type, None);
- println!("Instantiated {:?} into {:?}", named_type, tid);
+ trace!("Instantiated {:?} into {:?}", named_type, tid);
//let tid = tck.insert_known(&named_type);
let body_type = typecheck_expr(tck, symtbl, func_rettype, body)?;
- println!("Expected type is {:?}, body type is {:?}", tid, body_type);
+ trace!("Expected type is {:?}, body type is {:?}", tid, body_type);
tck.unify(symtbl, tid, body_type)?;
- println!("Done unifying type ctor");
+ trace!("Done unifying type ctor");
// The type the expression returns
let constructed_type =
tck.insert_known(&Type::Named(name.clone(), type_params.clone()));
@@ 1354,7 1360,7 @@ fn typecheck_expr(
let inner_type = symtbl
.get_type(nm)
.ok_or(format!("Named type {} is not known!", nm))?;
- println!("Inner type is {:?}", inner_type);
+ trace!("Inner type is {:?}", inner_type);
// The inner_type is a concrete Type, not a TypeInfo that may have
// unknowns, so we instantiate it to sub out any of its
@@ 1511,7 1517,7 @@ pub fn typecheck(ast: &hir::Ir) -> Resul
} => {
let t = typecheck_func_body(Some(*name), tck, symtbl, signature, body);
t.unwrap_or_else(|e| {
- eprintln!("Error, type context is:");
+ error!("Error, type context is:");
tck.print_types();
panic!("Error while typechecking function {}:\n{}", name, e)
});
@@ 1561,15 1567,15 @@ pub fn typecheck(ast: &hir::Ir) -> Resul
}
// Print out toplevel symbols
for (name, id) in &symtbl.frames.borrow().last().unwrap().symbols {
- println!(
+ info!(
"value {} type is {:?}",
name,
tck.reconstruct(id.0).map(|t| t.get_name())
);
}
- println!("Type variables:");
+ trace!("Type variables:");
for (id, info) in &tck.vars {
- println!(" ${} = {info:?}", id.0);
+ trace!(" ${} = {info:?}", id.0);
}
Ok(t)
}