Skip to content

Commit

Permalink
Small tweaks, fix mistake when making apply_tree crate-public.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranchuFranchu committed Feb 27, 2024
1 parent 36ef219 commit 6f31a39
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
10 changes: 4 additions & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn create_var(mut id: usize) -> String {
///
/// Returns None when the provided string is not an output of
/// `create_var`.
pub fn var_to_num(s: &str) -> Option<usize> {
pub(crate) fn var_to_num(s: &str) -> Option<usize> {
let mut n = 0usize;
for i in s.chars() {
let i = (i as u32).checked_sub('a' as u32)? as usize;
Expand Down Expand Up @@ -53,6 +53,7 @@ fn test_create_var() {
assert_eq!(create_var(1352), "aza");
assert_eq!(create_var(1378), "baa");
}

#[test]
fn test_var_to_num() {
for i in [0, 1, 2, 3, 10, 26, 27, 30, 50, 70] {
Expand Down Expand Up @@ -178,9 +179,7 @@ impl Tree {
Tree::Op1 { rgt, .. } => {
rgt.ensure_no_conflicts(fresh);
}
Tree::Era => (),
Tree::Num { .. } => (),
Tree::Ref { .. } => (),
Tree::Era | Tree::Num { .. } | Tree::Ref { .. } => {},
}
}
}
Expand All @@ -198,8 +197,7 @@ impl Net {
/// The result is equivalent a λ-calculus application. Thus,
/// if the net is a λ-calculus term, then this function will
/// apply an argument to it.
#[allow(dead_code)] // used in tests
pub(crate) fn with_argument(&mut self, arg: Tree) {
pub fn apply_tree(&mut self, arg: Tree) {
let mut fresh = 0usize;
self.ensure_no_conflicts(&mut fresh);
arg.ensure_no_conflicts(&mut fresh);
Expand Down
4 changes: 2 additions & 2 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use hvmc::{
use insta::assert_display_snapshot;

#[test]
fn test_with_argument() {
fn test_apply_tree() {
use hvmc::run;
fn eval_with_args(fun: &str, args: &[&str]) -> Net {
let area = run::Net::<run::Strict>::init_heap(1 << 10);

let mut fun: Net = fun.parse().unwrap();
for arg in args {
let arg: Tree = arg.parse().unwrap();
fun.with_argument(arg)
fun.apply_tree(arg)
}
// TODO: When feature/sc-472/argument-passing, use encode_net instead.
let mut book = Book::default();
Expand Down
10 changes: 5 additions & 5 deletions tests/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ fn list_got(index: u32) -> Book {
let mut book = parse_core(&code);
println!("{:#?}", book.keys().collect::<Vec<_>>());
let def = book.get_mut("GenGotIndex").unwrap();
def.with_argument(hvmc::ast::Tree::Ref { nam: format!("S{index}") });
def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("S{index}") });
let def = book.get_mut("main").unwrap();
def.with_argument(hvmc::ast::Tree::Ref { nam: format!("GenGotIndex") });
def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("GenGotIndex") });
book
}

fn list_put(index: u32, value: u32) -> Book {
let code = load_file("list_put_got.hvmc");
let mut book = parse_core(&code);
let def = book.get_mut("GenPutIndexValue").unwrap();
def.with_argument(hvmc::ast::Tree::Ref { nam: format!("S{index}") });
def.with_argument(hvmc::ast::Tree::Ref { nam: format!("S{value}") });
def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("S{index}") });
def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("S{value}") });
println!("{:?}", def);
let def = book.get_mut("main").unwrap();
def.with_argument(hvmc::ast::Tree::Ref { nam: format!("GenPutIndexValue") });
def.apply_tree(hvmc::ast::Tree::Ref { nam: format!("GenPutIndexValue") });
book
}

Expand Down

0 comments on commit 6f31a39

Please sign in to comment.