Skip to content

Commit

Permalink
Refactor rename: stop naming fields that can be copied automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Nov 2, 2023
1 parent 15206a4 commit 9b36b74
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions src/compiler/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,8 @@ fn make_binding_unique(b: &Binding) -> InnerRenameList {
InnerRenameList {
bindings: single_name_map,
from_wing: Binding {
loc: b.loc.clone(),
nl: b.nl.clone(),
pattern: BindingPattern::Name(new_name),
body: b.body.clone(),
.. b.clone()
},
}
}
Expand All @@ -147,10 +145,8 @@ fn make_binding_unique(b: &Binding) -> InnerRenameList {
InnerRenameList {
bindings: new_names,
from_wing: Binding {
loc: b.loc.clone(),
nl: b.nl.clone(),
pattern: BindingPattern::Complex(renamed_pattern),
body: b.body.clone(),
.. b.clone()
},
}
}
Expand Down Expand Up @@ -200,11 +196,10 @@ fn rename_in_bodyform(
BodyForm::Let(kind, letdata) => {
let new_bindings = map_m(
&|b: &Rc<Binding>| -> Result<Rc<Binding>, CompileErr> {
let b_borrowed: &Binding = b.borrow();
Ok(Rc::new(Binding {
loc: b.loc(),
nl: b.nl.clone(),
pattern: b.pattern.clone(),
body: Rc::new(rename_in_bodyform(namemap, b.body.clone())?),
.. b_borrowed.clone()
}))
},
&letdata.bindings,
Expand Down Expand Up @@ -411,12 +406,8 @@ fn rename_in_helperform(
) -> Result<HelperForm, CompileErr> {
match h {
HelperForm::Defconstant(defc) => Ok(HelperForm::Defconstant(DefconstData {
loc: defc.loc.clone(),
kind: defc.kind.clone(),
name: defc.name.to_vec(),
nl: defc.nl.clone(),
kw: defc.kw.clone(),
body: Rc::new(rename_in_bodyform(namemap, defc.body.clone())?),
..defc.clone()
})),
HelperForm::Defmacro(mac) => Ok(HelperForm::Defmacro(DefmacData {
program: Rc::new(rename_in_compileform(namemap, mac.program.clone())?),
Expand All @@ -435,12 +426,8 @@ fn rename_in_helperform(
pub fn rename_args_helperform(h: &HelperForm) -> Result<HelperForm, CompileErr> {
match h {
HelperForm::Defconstant(defc) => Ok(HelperForm::Defconstant(DefconstData {
loc: defc.loc.clone(),
kind: defc.kind.clone(),
nl: defc.nl.clone(),
kw: defc.kw.clone(),
name: defc.name.clone(),
body: Rc::new(rename_args_bodyform(defc.body.borrow())?),
..defc.clone()
})),
HelperForm::Defmacro(mac) => {
let mut new_names: HashMap<Vec<u8>, Vec<u8>> = HashMap::new();
Expand Down Expand Up @@ -489,25 +476,23 @@ fn rename_in_compileform(
namemap: &HashMap<Vec<u8>, Vec<u8>>,
c: Rc<CompileForm>,
) -> Result<CompileForm, CompileErr> {
let c_ref: &CompileForm = c.borrow();
Ok(CompileForm {
loc: c.loc.clone(),
args: c.args.clone(),
include_forms: c.include_forms.clone(),
helpers: map_m(|x| rename_in_helperform(namemap, x), &c.helpers)?,
exp: Rc::new(rename_in_bodyform(namemap, c.exp.clone())?),
.. c_ref.clone()
})
}

/// For all the HelperForms in a CompileForm, do renaming in them so that all
/// unique variable bindings in the program have unique names.
pub fn rename_children_compileform(c: &CompileForm) -> Result<CompileForm, CompileErr> {
let c_ref: &CompileForm = c;
let local_renamed_helpers = map_m(&rename_args_helperform, &c.helpers)?;
let local_renamed_body = rename_args_bodyform(c.exp.borrow())?;
Ok(CompileForm {
helpers: local_renamed_helpers,
exp: Rc::new(local_renamed_body),
..c_ref.clone()
..c.clone()
})
}

Expand Down

0 comments on commit 9b36b74

Please sign in to comment.