diff --git a/src/compiler.rs b/src/compiler.rs index 9e95a59..5578a9b 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -7,8 +7,8 @@ use crate::{ #[derive(Debug, Clone)] pub struct RefMeta { - now: u16, - total: Rc>, + pub now: u16, + pub total: Rc>, } struct Scope<'a> { @@ -38,10 +38,9 @@ impl<'a> Scope<'a> { } } -pub fn compile(mut e: Expr) { +pub fn compile(e: &mut Expr) { let mut scope = Scope::with_parent(None); - analyze(&mut scope, &mut e); - println!("{e:?}"); + analyze(&mut scope, e); } fn analyze(scope: &mut Scope, e: &mut Expr) { @@ -63,12 +62,10 @@ fn analyze(scope: &mut Scope, e: &mut Expr) { // increment # of uses count.update(|c| c + 1); // set ref meta - let now = count.get(); *ref_meta = Some(RefMeta { now: count.get(), total: count, }); - println!("ref {id} #{now}"); } // ignore Expr::Literal(_) => {} diff --git a/src/lexer.rs b/src/lexer.rs index 258229b..a498fba 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -30,7 +30,14 @@ impl fmt::Display for Literal { Literal::Integer(n) => write!(f, "{n}"), Literal::Float(n) => write!(f, "{n}"), Literal::Boolean(b) => write!(f, "{b}"), - Literal::Ident(id, _) => write!(f, "{id}"), + Literal::Ident(id, ref_meta) => write!( + f, + "{id}{}", + ref_meta + .as_ref() + .map(|rm| format!("@{}/{}", rm.now, rm.total.get())) + .unwrap_or_default() + ), Literal::Nil => write!(f, "nil"), } } diff --git a/src/main.rs b/src/main.rs index a1bb305..f41d60d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,11 +16,12 @@ fn main() { let block = parser.parse().unwrap(); println!("Parse took {:?}", start.elapsed()); - let e = parser::Expr::Block(block); + let mut e = parser::Expr::Block(block); parser::util::display(&e); let start = Instant::now(); - compiler::compile(e); + compiler::compile(&mut e); println!("Analysis took {:?}", start.elapsed()); + parser::util::display(&e); }