Move compile::Ctx to its own mod, compile::ctx::Ctx

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-05-20 14:51:30 -04:00
parent 9439dfda87
commit 499e09b254
6 changed files with 43 additions and 27 deletions

View File

@@ -1,7 +1,7 @@
use crate::{ use crate::{
syn::{ast::*, op::BinOp, span::*}, syn::{ast::*, op::BinOp, span::*},
compile::{ compile::{
Ctx, ctx::Ctx,
error::*, error::*,
ir, ir,
visit::*, visit::*,

21
src/compile/ctx.rs Normal file
View File

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

View File

@@ -1,31 +1,11 @@
#[macro_use] pub mod visit; #[macro_use] pub mod visit;
pub mod block; pub mod block;
pub mod ctx;
pub mod error; pub mod error;
pub mod ir; pub mod ir;
pub mod name; pub mod name;
use crate::compile::name::NameStack;
// * Desugar // * Desugar
// * Collect names as symbols // * Collect names as symbols
// * Create basic blocks // * 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
}
}

View File

@@ -1,10 +1,14 @@
use crate::{ use crate::{
compile::{visit::*, Ctx}, compile::{ctx::Ctx, visit::*},
obj::prelude::*, obj::prelude::*,
syn::{ast::prelude::*, span::*}, syn::{ast::prelude::*, span::*},
}; };
use std::collections::HashMap; use std::collections::HashMap;
////////////////////////////////////////////////////////////////////////////////
// NameStack
////////////////////////////////////////////////////////////////////////////////
pub struct NameStack { pub struct NameStack {
next_sym: usize, next_sym: usize,
stack: Vec<HashMap<String, NameId>>, stack: Vec<HashMap<String, NameId>>,
@@ -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> { pub struct CollectNames<'c, 't> {
ctx: &'c mut Ctx, ctx: &'c mut Ctx,
text: &'t str, text: &'t str,
@@ -133,9 +141,17 @@ impl Visit<BaseExpr> for CollectNames<'_, '_> {
fn visit(&mut self, expr: &BaseExpr) -> Self::Out { fn visit(&mut self, expr: &BaseExpr) -> Self::Out {
// This is a LHS standalone expr // 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(); let name = expr.text_at(self.text).to_string();
self.ctx.name_stack_mut().add(name); self.ctx.name_stack_mut().add(name);
} }
} }
} }
////////////////////////////////////////////////////////////////////////////////
// CollectSyms
////////////////////////////////////////////////////////////////////////////////

View File

@@ -43,7 +43,7 @@ fn main() -> Result<()> {
let ast = parser.next_body()?; let ast = parser.next_body()?;
//println!("{:#?}", ast); //println!("{:#?}", ast);
let mut ctx = compile::Ctx::new(); let mut ctx = compile::ctx::Ctx::new();
ctx.name_stack_mut().push_default(); ctx.name_stack_mut().push_default();
{ {
let mut collect_names = compile::name::CollectNames::new(&mut ctx, text.as_str()); let mut collect_names = compile::name::CollectNames::new(&mut ctx, text.as_str());

View File

@@ -17,7 +17,6 @@ pub mod ns {
} }
pub mod str; pub mod str;
pub mod sym; pub mod sym;
//pub mod ty;
pub mod prelude { pub mod prelude {
pub use crate::{ pub use crate::{