Skip to content

Commit

Permalink
streamline deps
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi committed May 27, 2024
1 parent e57af2b commit 21e53a0
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 69 deletions.
9 changes: 0 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ edition = "2021"
path = "src/ast.rs"

[dependencies]
arrayvec = { version = "0.7.4", default-features = false }
ordered-float = { version = "4.2.0", default-features = false }
thiserror = { version = "1.0.59", optional = true }
TSPL = { version = "0.0.12", optional = true }

hvm64-util = { path = "../util" }

[features]
default = ["std", "parser"]
std = ["ordered-float/std", "dep:thiserror"]
std = []
parser = ["dep:TSPL"]

[lints]
Expand Down
84 changes: 40 additions & 44 deletions ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
//! The AST is based on the [interaction calculus].
//!
//! [interaction calculus]: https://en.wikipedia.org/wiki/Interaction_nets#Interaction_calculus
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

include!("../../prelude.rs");

Expand All @@ -21,9 +18,8 @@ mod parser;
use alloc::collections::BTreeMap;

use crate::prelude::*;
use hvm64_util::{array_vec, create_var, deref, maybe_grow, ops::TypedOp as Op, var_to_num};
use hvm64_util::{create_var, deref, maybe_grow, multi_iterator, ops::TypedOp as Op, var_to_num};

use arrayvec::ArrayVec;
use ordered_float::OrderedFloat;

pub type Lab = u16;
Expand Down Expand Up @@ -194,28 +190,26 @@ impl Net {
impl Tree {
#[inline(always)]
pub fn children(&self) -> impl ExactSizeIterator + DoubleEndedIterator<Item = &Tree> {
ArrayVec::<_, MAX_ARITY>::into_iter(match self {
Tree::Era | Tree::Int { .. } | Tree::F32 { .. } | Tree::Ref { .. } | Tree::Var { .. } => {
array_vec::from_array([])
}
Tree::Ctr { ports, .. } => array_vec::from_iter(ports),
Tree::Op { rhs, out, .. } => array_vec::from_array([rhs, out]),
Tree::Mat { zero, succ, out } => array_vec::from_array([zero, succ, out]),
Tree::Adt { fields, .. } => array_vec::from_iter(fields),
})
multi_iterator! { Iter { Nil, Two, Three, Vec } }
match self {
Tree::Era | Tree::Int { .. } | Tree::F32 { .. } | Tree::Ref { .. } | Tree::Var { .. } => Iter::Nil([]),
Tree::Ctr { ports, .. } => Iter::Vec(ports),
Tree::Op { rhs, out, .. } => Iter::Two([&**rhs, out]),
Tree::Mat { zero, succ, out } => Iter::Three([&**zero, succ, out]),
Tree::Adt { fields, .. } => Iter::Vec(fields),
}
}

#[inline(always)]
pub fn children_mut(&mut self) -> impl ExactSizeIterator + DoubleEndedIterator<Item = &mut Tree> {
ArrayVec::<_, MAX_ARITY>::into_iter(match self {
Tree::Era | Tree::Int { .. } | Tree::F32 { .. } | Tree::Ref { .. } | Tree::Var { .. } => {
array_vec::from_array([])
}
Tree::Ctr { ports, .. } => array_vec::from_iter(ports),
Tree::Op { rhs, out, .. } => array_vec::from_array([rhs, out]),
Tree::Mat { zero, succ, out } => array_vec::from_array([zero, succ, out]),
Tree::Adt { fields, .. } => array_vec::from_iter(fields),
})
multi_iterator! { Iter { Nil, Two, Three, Vec } }
match self {
Tree::Era | Tree::Int { .. } | Tree::F32 { .. } | Tree::Ref { .. } | Tree::Var { .. } => Iter::Nil([]),
Tree::Ctr { ports, .. } => Iter::Vec(ports),
Tree::Op { rhs, out, .. } => Iter::Two([&mut **rhs, out]),
Tree::Mat { zero, succ, out } => Iter::Three([&mut **zero, succ, out]),
Tree::Adt { fields, .. } => Iter::Vec(fields),
}
}

pub fn lab(&self) -> Option<Lab> {
Expand Down Expand Up @@ -373,24 +367,26 @@ impl fmt::Display for Tree {
}
}

// #[test]
// fn test_tree_drop() {
// use alloc::vec;

// drop(Tree::from_str("((* (* *)) (* *))"));

// let mut long_tree = Tree::Era;
// let mut cursor = &mut long_tree;
// for _ in 0 .. 100_000 {
// *cursor = Tree::Ctr { lab: 0, ports: vec![Tree::Era, Tree::Era] };
// let Tree::Ctr { ports, .. } = cursor else { unreachable!() };
// cursor = &mut ports[0];
// }
// drop(long_tree);

// let mut big_tree = Tree::Era;
// for _ in 0 .. 16 {
// big_tree = Tree::Ctr { lab: 0, ports: vec![big_tree.clone(), big_tree] };
// }
// drop(big_tree);
// }
#[test]
#[cfg(feature = "parser")]
fn test_tree_drop() {
use alloc::vec;
use core::str::FromStr;

drop(Tree::from_str("((* (* *)) (* *))"));

let mut long_tree = Tree::Era;
let mut cursor = &mut long_tree;
for _ in 0 .. 100_000 {
*cursor = Tree::Ctr { lab: 0, ports: vec![Tree::Era, Tree::Era] };
let Tree::Ctr { ports, .. } = cursor else { unreachable!() };
cursor = &mut ports[0];
}
drop(long_tree);

let mut big_tree = Tree::Era;
for _ in 0 .. 16 {
big_tree = Tree::Ctr { lab: 0, ports: vec![big_tree.clone(), big_tree] };
}
drop(big_tree);
}
1 change: 1 addition & 0 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ hvm64-ast = { path = "../ast", default-features = false }
hvm64-runtime = { path = "../runtime", default-features = false }

[features]
default = ["std"]
std = ["hvm64-ast/std", "hvm64-runtime/std"]

[lints]
Expand Down
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "src/runtime.rs"
hvm64-util = { path = "../util" }

[features]
default = ["std"]
std = []
trace = ["std"]

Expand Down
2 changes: 1 addition & 1 deletion src/compile/include_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ hvm64-runtime = { path = "../runtime", default-features = false }
prelude
crate util {
lib
array_vec
bi_enum
create_var
deref
maybe_grow
multi_iterator
ops {
num
word
Expand Down
6 changes: 3 additions & 3 deletions transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ path = "src/transform.rs"
ordered-float = { version = "4.2.0", default-features = false }
thiserror = "1.0.58"

hvm64-ast = { path = "../ast", default-features = false }
hvm64-util = { path = "../util" }
hvm64-runtime = { path = "../runtime", default-features = false }
hvm64-util = { path = "../util", default-features = false }
hvm64-ast = { path = "../ast", default-features = false }
hvm64-host = { path = "../host", default-features = false }

[features]
default = ["std"]
std = ["hvm64-ast/std", "hvm64-runtime/std", "hvm64-util/std", "hvm64-host/std"]
std = ["hvm64-runtime/std", "hvm64-ast/std", "hvm64-host/std"]

[lints]
workspace = true
2 changes: 1 addition & 1 deletion util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.3.0"
edition = "2021"

[dependencies]
stacker = { version = "0.1.15", default-features = false }
stacker = { version = "0.1.15" }

[lints]
workspace = true
17 changes: 9 additions & 8 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#![no_std]

include!("../../prelude.rs");

pub mod bi_enum;
pub mod create_var;
pub mod deref;
pub mod maybe_grow;
pub mod ops;
pub mod parse_abbrev_number;
pub mod pretty_num;

mod bi_enum;
mod deref;
mod multi_iterator;

mod create_var;
mod maybe_grow;
mod parse_abbrev_number;
mod pretty_num;

pub use create_var::*;
pub use maybe_grow::*;
Expand Down
40 changes: 40 additions & 0 deletions util/src/multi_iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// A macro for creating iterators that can have statically known
/// different types. Useful for iterating over tree children, where
/// each tree node variant yields a different iterator type.
#[macro_export]
macro_rules! multi_iterator {
($Iter:ident { $($Variant:ident),* $(,)? }) => {
#[derive(Debug, Clone)]
enum $Iter<$($Variant),*> {
$($Variant { iter: $Variant }),*
}

impl<$($Variant),*> $Iter<$($Variant),*> {
$(
#[allow(non_snake_case)]
fn $Variant(iter: impl IntoIterator<IntoIter = $Variant>) -> Self {
$Iter::$Variant { iter: iter.into_iter() }
}
)*
}

impl<T, $($Variant: Iterator<Item = T>),*> Iterator for $Iter<$($Variant),*> {
type Item = T;
fn next(&mut self) -> Option<T> {
match self { $($Iter::$Variant { iter } => iter.next()),* }
}

fn size_hint(&self) -> (usize, Option<usize>) {
match self { $($Iter::$Variant { iter } => iter.size_hint()),* }
}
}

impl<T, $($Variant: DoubleEndedIterator<Item = T>),*> DoubleEndedIterator for $Iter<$($Variant),*> {
fn next_back(&mut self) -> Option<T> {
match self { $($Iter::$Variant { iter } => iter.next_back()),* }
}
}

impl<T, $($Variant: ExactSizeIterator<Item = T>),*> ExactSizeIterator for $Iter<$($Variant),*> {}
};
}

0 comments on commit 21e53a0

Please sign in to comment.