Skip to content

Commit

Permalink
#714Refactor Transform String Resugar Term Scott && keep function mim…
Browse files Browse the repository at this point in the history
…ics the shape of the AST for easier visualization
  • Loading branch information
Liberxue committed Sep 12, 2024
1 parent a09ada6 commit 22e7d94
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 158 deletions.
6 changes: 3 additions & 3 deletions src/fun/transform/resugar_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Term {
/// Converts scott-encoded lists ending with List/Nil to list literals.
fn resugar_lists_scott(&mut self) {
maybe_grow(|| {
// Searc h for a List/Cons pattern in the term and try to build a list from that point on.
// Searc h for a List/Cons pattern in the term and try to build a list from that point on.

Check warning on line 82 in src/fun/transform/resugar_list.rs

View workflow job for this annotation

GitHub Actions / cspell

Unknown word (Searc)
// If successful, replace the term with the list.
// If not, keep as-is.

Expand Down Expand Up @@ -124,7 +124,7 @@ impl Term {
match l {
Ok(l) => *self = Term::List { els: l.into_iter().map(|x| *x).collect() },
// Was not a list term, keep as-is.
Err(mut l) => {
Err(mut l) => {
*head = l.pop().unwrap();
assert!(l.is_empty())
}
Expand Down Expand Up @@ -241,7 +241,7 @@ fn build_list_scott(term: &mut Term, mut l: Vec<Box<Term>>) -> Result<Vec<Box<Te
}
}
}
}
}
// Cons: (List/Cons <term> <term>)
if let Term::App { tag: Tag::Static, fun, arg: tail } = term {
if let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_mut() {
Expand Down
155 changes: 0 additions & 155 deletions src/fun/transform/resugar_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{

impl Term {
/// Converts lambda-encoded strings ending with String/nil to string literals.
/// This method chooses the appropriate resugaring function based on the ADT encoding.
pub fn resugar_strings(&mut self, adt_encoding: AdtEncoding) {
match adt_encoding {
AdtEncoding::Scott => self.try_resugar_strings_with(Self::resugar_strings_scott),
Expand All @@ -14,7 +13,6 @@ impl Term {
}

/// Converts encoded strings to string literals using the provided extraction function.
/// This method recursively processes the term structure.
fn try_resugar_strings_with(&mut self, extract_fn: fn(&Term) -> Option<(char, &Term)>) {
maybe_grow(|| {
// Try to resugar nil or cons patterns. If unsuccessful, recurse into child terms.
Expand All @@ -31,38 +29,13 @@ impl Term {
matches!(self, Term::Ref { nam } if nam == builtins::SNIL).then(|| *self = Term::str("")).is_some()
}

<<<<<<< HEAD
/// Attempts to resugar a Scott-encoded cons term.
fn try_resugar_cons_scott(term: &mut Term) -> bool {
// Pattern matching to extract the necessary components Cons: @x (x CONS_TAG <num> <str>)
let Term::Lam { tag: Tag::Static, pat: outer_pat, bod } = term else { return false };
let Pattern::Var(None) = outer_pat.as_ref() else { return false };
let Term::Lam { tag: Tag::Static, pat: inner_pat, bod: inner_bod } = bod.as_mut() else { return false };
let Pattern::Var(Some(var_lam)) = inner_pat.as_ref() else { return false };
let Term::App { tag: Tag::Static, fun, arg: tail } = inner_bod.as_mut() else { return false };
let Term::App { tag: Tag::Static, fun: inner_fun, arg: head } = fun.as_mut() else { return false };
let (Term::Var { nam: var_app }, Term::Num { val: Num::U24(head_val) }) =
(inner_fun.as_mut(), head.as_mut())
else {
return false;
};

if var_lam != var_app {
return false;
};

let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER);
Self::build_string_scott(tail, head_char.to_string()).map(|str| *term = Term::str(&str)).is_some()
=======
/// Attempts to resugar a cons term to a string literal.
/// This method tries both the provided extraction function and the common extraction method.
fn try_resugar_strings_cons(&mut self, extract_fn: fn(&Term) -> Option<(char, &Term)>) -> bool {
self
.try_resugar_strings_cons_with(extract_fn)
.or_else(|| self.try_resugar_strings_cons_common())
.map(|str| *self = Term::str(&str))
.is_some()
>>>>>>> 6b0dd50b (#714Refactor Transform String Resugar Term Scott && keep function mimics the shape of the AST for easier visualization)
}

/// Attempts to resugar a cons term using the provided extraction function.
Expand All @@ -71,69 +44,6 @@ impl Term {
.and_then(|(head_char, tail)| Self::build_strings_common(tail, head_char.to_string(), extract_fn))
}

<<<<<<< HEAD
/// Attempts to resugar a common cons term.
fn try_resugar_cons_common(&mut self, build_string_fn: fn(&Term, String) -> Option<String>) -> bool {
let Term::App { tag: Tag::Static, fun, arg: tail } = self else { return false };
let Term::App { tag: Tag::Static, fun: inner_fun, arg: head } = fun.as_mut() else { return false };
let (Term::Ref { nam }, Term::Num { val: Num::U24(head_val) }) = (inner_fun.as_ref(), head.as_ref())
else {
return false;
};

if nam != builtins::SCONS {
return false;
};

let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER);
build_string_fn(tail, head_char.to_string()).map(|str| *self = Term::str(&str)).is_some()
}

/// common string building function that uses custom and default cons handlers.
/// pattern keep if tree mimics the shape of the AST visualize the structure #713
fn build_string<F>(term: &Term, mut s: String, cons_handler: F) -> Option<String>
where
F: Fn(&Term) -> Option<(char, &Term)>,
{
maybe_grow(|| {
let mut current = term;
loop {
// Nil: String/nil
if let Term::Ref { nam } = current {
if nam == builtins::SNIL {
return Some(s);
}
}

// Try custom cons handler
if let Some((head, tail)) = cons_handler(current) {
s.push(head);
current = tail;
continue;
}

// Cons: @x (x CONS_TAG <num> <str>)
if let Term::Lam { tag: Tag::Static, pat, bod } = current {
if let Pattern::Var(Some(var_lam)) = pat.as_ref() {
if let Term::App { tag: Tag::Static, fun, arg: tail } = bod.as_ref() {
if let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_ref() {
if let Term::App { tag: Tag::Static, fun, arg } = fun.as_ref() {
if let Term::Var { nam: var_app } = fun.as_ref() {
if let Term::Ref { nam } = arg.as_ref() {
if let Term::Num { val: Num::U24(head_val) } = head.as_ref() {
if var_lam == var_app && nam == builtins::SCONS_TAG_REF {
let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER);
s.push(head_char);
current = tail;
continue;
}
}
}
}
}
}
}
=======
/// Attempts to resugar a cons term using the common extraction method.
fn try_resugar_strings_cons_common(&self) -> Option<String> {
if let Term::App { tag: Tag::Static, fun, arg: tail } = self {
Expand Down Expand Up @@ -168,53 +78,12 @@ impl Term {
let (head, next) = extract_fn(current).or_else(|| Self::extract_strings_common(current))?;
s.push(head);
current = next;
>>>>>>> 6b0dd50b (#714Refactor Transform String Resugar Term Scott && keep function mimics the shape of the AST for easier visualization)
}
}

// Cons: (String/cons <num> <str>)
if let Term::App { tag: Tag::Static, fun, arg: tail } = current {
if let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_ref() {
if let Term::Ref { nam } = fun.as_ref() {
if let Term::Num { val: Num::U24(head_val) } = head.as_ref() {
if nam == builtins::SCONS {
let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER);
s.push(head_char);
current = tail;
continue;
}
}
}
}
}

// Not a string term, stop
return None;
}
})
}

<<<<<<< HEAD
/// Builds a string from a NumScott-encoded term.
fn build_string_num_scott(term: &Term, s: String) -> Option<String> {
Self::build_string(term, s, |term| {
let Term::Lam { tag: Tag::Static, pat, bod } = term else { return None };
let Pattern::Var(Some(var_lam)) = pat.as_ref() else { return None };
let Term::App { tag: Tag::Static, fun, arg: tail } = bod.as_ref() else { return None };
let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_ref() else { return None };
let Term::App { tag: Tag::Static, fun, arg } = fun.as_ref() else { return None };
let (Term::Var { nam: var_app }, Term::Ref { nam }, Term::Num { val: Num::U24(head_val) }) =
(fun.as_ref(), arg.as_ref(), head.as_ref())
else {
return None;
};

if var_lam == var_app && nam == builtins::SCONS_TAG_REF {
let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER);
Some((head_char, tail))
} else {
None
=======
/// Extracts a character and the remaining term from a Scott-encoded string term.
/// The structure of this function mimics the shape of the AST for easier visualization.
fn resugar_strings_scott(term: &Term) -> Option<(char, &Term)> {
Expand All @@ -236,33 +105,11 @@ impl Term {
}
}
}
>>>>>>> 6b0dd50b (#714Refactor Transform String Resugar Term Scott && keep function mimics the shape of the AST for easier visualization)
}
}
None
}

<<<<<<< HEAD
/// Builds a string from a Scott-encoded term.
fn build_string_scott(term: &Term, s: String) -> Option<String> {
Self::build_string(term, s, |term| {
let Term::Lam { tag: Tag::Static, pat, bod } = term else { return None };
let Pattern::Var(None) = pat.as_ref() else { return None };
let Term::Lam { tag: Tag::Static, pat, bod } = bod.as_ref() else { return None };
let Pattern::Var(Some(var_lam)) = pat.as_ref() else { return None };
let Term::App { tag: Tag::Static, fun, arg: tail } = bod.as_ref() else { return None };
let Term::App { tag: Tag::Static, fun, arg: head } = fun.as_ref() else { return None };
let (Term::Var { nam: var_app }, Term::Num { val: Num::U24(head_val) }) = (fun.as_ref(), head.as_ref())
else {
return None;
};

if var_lam == var_app {
let head_char = char::from_u32(*head_val).unwrap_or(char::REPLACEMENT_CHARACTER);
Some((head_char, tail))
} else {
None
=======
/// Extracts a character and the remaining term from a NumScott-encoded string term.
/// The structure of this function mimics the shape of the AST for easier visualization.
fn resugar_strings_num_scott(term: &Term) -> Option<(char, &Term)> {
Expand All @@ -285,7 +132,6 @@ impl Term {
}
}
}
>>>>>>> 6b0dd50b (#714Refactor Transform String Resugar Term Scott && keep function mimics the shape of the AST for easier visualization)
}
}
None
Expand All @@ -307,4 +153,3 @@ impl Term {
None
}
}

0 comments on commit 22e7d94

Please sign in to comment.