Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor rename: stop naming fields that can be copied automatically #54

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading