diff --git a/src/compile/block.rs b/src/compile/block.rs index a7b93a4..926b49f 100644 --- a/src/compile/block.rs +++ b/src/compile/block.rs @@ -1,7 +1,7 @@ use crate::{ syn::{ast::*, op::BinOp, span::*}, compile::{ - Ctx, + ctx::Ctx, error::*, ir, visit::*, diff --git a/src/compile/ctx.rs b/src/compile/ctx.rs new file mode 100644 index 0000000..105009a --- /dev/null +++ b/src/compile/ctx.rs @@ -0,0 +1,21 @@ +use crate::compile::name::NameStack; + +pub struct Ctx { + name_stack: NameStack, +} + +impl Ctx { + pub fn new() -> Self { + Ctx { + name_stack: NameStack::new(), + } + } + + pub fn name_stack(&self) -> &NameStack { + &self.name_stack + } + + pub fn name_stack_mut(&mut self) -> &mut NameStack { + &mut self.name_stack + } +} diff --git a/src/compile/mod.rs b/src/compile/mod.rs index f2ceb75..a1e95fc 100644 --- a/src/compile/mod.rs +++ b/src/compile/mod.rs @@ -1,31 +1,11 @@ #[macro_use] pub mod visit; pub mod block; +pub mod ctx; pub mod error; pub mod ir; pub mod name; -use crate::compile::name::NameStack; - // * Desugar // * Collect names as symbols // * Create basic blocks -pub struct Ctx { - name_stack: NameStack, -} - -impl Ctx { - pub fn new() -> Self { - Ctx { - name_stack: NameStack::new(), - } - } - - pub fn name_stack(&self) -> &NameStack { - &self.name_stack - } - - pub fn name_stack_mut(&mut self) -> &mut NameStack { - &mut self.name_stack - } -} diff --git a/src/compile/name.rs b/src/compile/name.rs index 438a392..6d15c67 100644 --- a/src/compile/name.rs +++ b/src/compile/name.rs @@ -1,10 +1,14 @@ use crate::{ - compile::{visit::*, Ctx}, + compile::{ctx::Ctx, visit::*}, obj::prelude::*, syn::{ast::prelude::*, span::*}, }; use std::collections::HashMap; +//////////////////////////////////////////////////////////////////////////////// +// NameStack +//////////////////////////////////////////////////////////////////////////////// + pub struct NameStack { next_sym: usize, stack: Vec>, @@ -71,7 +75,11 @@ impl NameStack { } } -/// Collect local names and push them to the top layer of the name stack. +//////////////////////////////////////////////////////////////////////////////// +// CollectNames +//////////////////////////////////////////////////////////////////////////////// + +/// Collect local stack names and push them to the top layer of the name stack. pub struct CollectNames<'c, 't> { ctx: &'c mut Ctx, text: &'t str, @@ -133,9 +141,17 @@ impl Visit for CollectNames<'_, '_> { fn visit(&mut self, expr: &BaseExpr) -> Self::Out { // This is a LHS standalone expr - if let BaseExpr { kind: BaseExprKind::Ident, .. } = expr { + if let BaseExpr { + kind: BaseExprKind::Ident, + .. + } = expr + { let name = expr.text_at(self.text).to_string(); self.ctx.name_stack_mut().add(name); } } } + +//////////////////////////////////////////////////////////////////////////////// +// CollectSyms +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/main.rs b/src/main.rs index 28e8551..b7ab833 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,7 @@ fn main() -> Result<()> { let ast = parser.next_body()?; //println!("{:#?}", ast); - let mut ctx = compile::Ctx::new(); + let mut ctx = compile::ctx::Ctx::new(); ctx.name_stack_mut().push_default(); { let mut collect_names = compile::name::CollectNames::new(&mut ctx, text.as_str()); diff --git a/src/obj/mod.rs b/src/obj/mod.rs index de2959e..bcc4d1c 100644 --- a/src/obj/mod.rs +++ b/src/obj/mod.rs @@ -17,7 +17,6 @@ pub mod ns { } pub mod str; pub mod sym; -//pub mod ty; pub mod prelude { pub use crate::{