diff --git a/lust-syntax/src/expand/env.rs b/lust-syntax/src/expand/env.rs deleted file mode 100644 index e979f6d..0000000 --- a/lust-syntax/src/expand/env.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::read::sexpr::Sexpr; -use lust_utils::intern::InternedString; -use std::{cell::RefCell, collections::HashMap, rc::Rc}; - -#[derive(Debug)] -pub struct Env { - bindings: HashMap, - parent: Option>>, -} - -impl Env { - pub fn new() -> Self { - Self { - bindings: HashMap::new(), - parent: None, - } - } - - pub fn with_parent(parent: Env) -> Self { - Self { - bindings: HashMap::new(), - parent: Some(Rc::new(RefCell::new(parent))), - } - } - - pub fn get(&self, name: &InternedString) -> Option { - self.bindings.get(name).cloned().or(self - .parent - .as_ref() - .and_then(|parent| parent.borrow().get(name).clone())) - } - - pub fn insert(&mut self, name: InternedString, value: Sexpr) { - self.bindings.insert(name, value); - } -} diff --git a/lust-syntax/src/expand/macro.rs b/lust-syntax/src/expand/macro.rs deleted file mode 100644 index 44ebb3e..0000000 --- a/lust-syntax/src/expand/macro.rs +++ /dev/null @@ -1,82 +0,0 @@ -use crate::read::sexpr::Sexpr; -use itertools::Itertools; -use lust_utils::{intern::InternedString, span::Span}; -use std::fmt::Display; - -#[derive(Debug, Clone, PartialEq)] -pub struct Macro { - name: InternedString, - params: Vec, - body: Sexpr, - span: Span, -} - -impl Macro { - pub fn new(name: InternedString, params: Vec, body: Sexpr, span: Span) -> Self { - Self { - name, - params, - body, - span, - } - } - - pub fn name(&self) -> &InternedString { - &self.name - } - - pub fn params(&self) -> &[InternedString] { - &self.params - } - - pub fn body(&self) -> &Sexpr { - &self.body - } - - pub fn span(&self) -> &Span { - &self.span - } -} - -impl Display for Macro { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "(macro ({} {}) {})", - self.name, - self.params.join(" "), - self.body - ) - } -} - -#[derive(Debug, Clone, PartialEq)] -pub struct MacroCall { - name: InternedString, - args: Vec, - span: Span, -} - -impl MacroCall { - pub fn new(name: InternedString, args: Vec, span: Span) -> Self { - Self { name, args, span } - } - - pub fn name(&self) -> &InternedString { - &self.name - } - - pub fn args(&self) -> &[Sexpr] { - &self.args - } - - pub fn span(&self) -> &Span { - &self.span - } -} - -impl Display for MacroCall { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "({} {})", self.name, self.args.iter().join(" ")) - } -} diff --git a/lust-syntax/src/expand/mod.rs b/lust-syntax/src/expand/mod.rs deleted file mode 100644 index 82a9b45..0000000 --- a/lust-syntax/src/expand/mod.rs +++ /dev/null @@ -1,144 +0,0 @@ -use self::{ - r#macro::{Macro, MacroCall}, - store::MacroStore, -}; -use crate::read::sexpr::{Atom, AtomKind, Root, Sexpr, SexprKind}; -use lust_utils::{intern::InternedString, list::List, span::Span}; -use std::{cell::RefCell, collections::HashMap, rc::Rc}; - -pub mod env; -pub mod r#macro; -pub mod store; - -// fn extract_macro(list: &List, span: Span) -> Macro { -// let mut iter = list.iter(); -// let (name, params) = match iter.next() { -// Some(s) => match s.kind() { -// SexprKind::SynList(param_list) => { -// let mut iter = param_list.list().iter(); -// let name = match iter.next() { -// Some(s) => match s.kind() { -// SexprKind::Atom(a) => match a.kind() { -// AtomKind::Sym(s) => s.clone(), -// _ => panic!("macro name must be a symbol"), -// }, -// _ => panic!("macro name must be a symbol"), -// }, -// None => panic!("macro name must be a symbol"), -// }; -// let mut params = vec![]; -// while let Some(s) = iter.next() { -// match s.kind() { -// SexprKind::Atom(a) => match a.kind() { -// AtomKind::Sym(s) => params.push(s.clone()), -// _ => panic!("macro parameter must be a symbol"), -// }, -// _ => panic!("macro parameter must be a symbol"), -// } -// } -// (name, params) -// } -// _ => panic!("macro name must be a symbol"), -// }, -// None => panic!("macro must have param list"), -// }; -// let body = match iter.next() { -// Some(s) => s.clone(), -// None => panic!("macro body must be a sexpr"), -// }; -// Macro::new(name, params, body, span) -// } - -// fn collect_macros(mut store: MacroStore, root: &Root) { -// for sexpr in root.sexprs() { -// let kind = match sexpr.kind() { -// SexprKind::SynList(list) => list, -// _ => continue, -// }; - -// let head = match kind.head() { -// Some(h) => h, -// None => continue, -// }; - -// let atom_kind = match head.kind() { -// SexprKind::Atom(a) => a, -// _ => continue, -// }; - -// let sym = match atom_kind.kind() { -// AtomKind::Sym(s) => s, -// _ => continue, -// }; - -// if &**sym != "macro" { -// continue; -// } - -// let tail = match kind.tail() { -// Some(t) => t, -// None => panic!("macro must have a body"), -// }; - -// let m = extract_macro(tail, kind.span()); -// store.insert(m); -// } -// } - -// pub fn expand_macros(store: MacroStore, root: &Root) -> Root { -// collect_macros(store.clone(), root); -// let mut sexprs = vec![]; -// for sexpr in root.sexprs() { -// let kind = match sexpr.kind() { -// SexprKind::SynList(list) => list, -// _ => { -// sexprs.push(sexpr.clone()); -// continue; -// } -// }; - -// let head = match kind.head() { -// Some(h) => h, -// None => { -// sexprs.push(sexpr.clone()); -// continue; -// } -// }; - -// let atom_kind = match head.kind() { -// SexprKind::Atom(a) => a, -// _ => { -// sexprs.push(sexpr.clone()); -// continue; -// } -// }; - -// let sym = match atom_kind.kind() { -// AtomKind::Sym(s) => s, -// _ => { -// sexprs.push(sexpr.clone()); -// continue; -// } -// }; - -// if let Some(m) = store.get(sym) { -// let tail = match kind.tail() { -// Some(t) => t, -// None => panic!("macro must have a body"), -// }; - -// let mut args = vec![]; -// for arg in tail.iter() { -// args.push(arg.clone()); -// } -// let mut body = m.body().clone(); -// for (i, param) in m.params().iter().enumerate() { -// body.replace_sym(param.clone(), args[i].clone()); -// } -// sexprs.push(body); -// } else { -// sexprs.push(sexpr.clone()); -// } -// } -// Root::new(sexprs, *root.span()) -// } diff --git a/lust-syntax/src/expand/store.rs b/lust-syntax/src/expand/store.rs deleted file mode 100644 index 369d87f..0000000 --- a/lust-syntax/src/expand/store.rs +++ /dev/null @@ -1,26 +0,0 @@ -use super::r#macro::Macro; -use lust_utils::intern::InternedString; -use std::{cell::RefCell, collections::HashMap, rc::Rc}; - -#[derive(Debug, Clone)] -pub struct MacroStore { - macros: Rc>>, -} - -impl MacroStore { - pub fn new() -> Self { - Self { - macros: Rc::new(RefCell::new(HashMap::new())), - } - } - - pub fn insert(&mut self, macro_: Macro) { - self.macros - .borrow_mut() - .insert(macro_.name().clone(), macro_); - } - - pub fn get(&self, name: &InternedString) -> Option { - self.macros.borrow().get(name).cloned() - } -}