From 21e53a008360413b42a76206804e533e1df6e5f0 Mon Sep 17 00:00:00 2001 From: tjjfvi Date: Mon, 27 May 2024 14:33:40 -0400 Subject: [PATCH] streamline deps --- Cargo.lock | 9 ---- ast/Cargo.toml | 4 +- ast/src/ast.rs | 84 +++++++++++++++++------------------- host/Cargo.toml | 1 + runtime/Cargo.toml | 1 + src/compile/include_files.rs | 2 +- transform/Cargo.toml | 6 +-- util/Cargo.toml | 2 +- util/src/lib.rs | 17 ++++---- util/src/multi_iterator.rs | 40 +++++++++++++++++ 10 files changed, 97 insertions(+), 69 deletions(-) create mode 100644 util/src/multi_iterator.rs diff --git a/Cargo.lock b/Cargo.lock index 0b88971e..f2335691 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,12 +69,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "arrayvec" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" - [[package]] name = "autocfg" version = "1.2.0" @@ -297,10 +291,8 @@ name = "hvm64-ast" version = "0.3.0" dependencies = [ "TSPL", - "arrayvec", "hvm64-util", "ordered-float", - "thiserror", ] [[package]] @@ -335,7 +327,6 @@ dependencies = [ name = "hvm64-util" version = "0.3.0" dependencies = [ - "arrayvec", "stacker", ] diff --git a/ast/Cargo.toml b/ast/Cargo.toml index 23ec53ab..b2894996 100644 --- a/ast/Cargo.toml +++ b/ast/Cargo.toml @@ -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] diff --git a/ast/src/ast.rs b/ast/src/ast.rs index 52bef8cd..81e9491a 100644 --- a/ast/src/ast.rs +++ b/ast/src/ast.rs @@ -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"); @@ -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; @@ -194,28 +190,26 @@ impl Net { impl Tree { #[inline(always)] pub fn children(&self) -> impl ExactSizeIterator + DoubleEndedIterator { - 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 { - 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 { @@ -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); +} diff --git a/host/Cargo.toml b/host/Cargo.toml index a043de2b..3a5b2901 100644 --- a/host/Cargo.toml +++ b/host/Cargo.toml @@ -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] diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index d111f892..d05a9e7d 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -10,6 +10,7 @@ path = "src/runtime.rs" hvm64-util = { path = "../util" } [features] +default = ["std"] std = [] trace = ["std"] diff --git a/src/compile/include_files.rs b/src/compile/include_files.rs index d4d83f64..43942a50 100644 --- a/src/compile/include_files.rs +++ b/src/compile/include_files.rs @@ -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 diff --git a/transform/Cargo.toml b/transform/Cargo.toml index 14bc7a50..3ccccf36 100644 --- a/transform/Cargo.toml +++ b/transform/Cargo.toml @@ -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 diff --git a/util/Cargo.toml b/util/Cargo.toml index 4c17b160..304adf21 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -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 diff --git a/util/src/lib.rs b/util/src/lib.rs index cd78d26d..be7b857e 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -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::*; diff --git a/util/src/multi_iterator.rs b/util/src/multi_iterator.rs new file mode 100644 index 00000000..1a660b51 --- /dev/null +++ b/util/src/multi_iterator.rs @@ -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) -> Self { + $Iter::$Variant { iter: iter.into_iter() } + } + )* + } + + impl),*> Iterator for $Iter<$($Variant),*> { + type Item = T; + fn next(&mut self) -> Option { + match self { $($Iter::$Variant { iter } => iter.next()),* } + } + + fn size_hint(&self) -> (usize, Option) { + match self { $($Iter::$Variant { iter } => iter.size_hint()),* } + } + } + + impl),*> DoubleEndedIterator for $Iter<$($Variant),*> { + fn next_back(&mut self) -> Option { + match self { $($Iter::$Variant { iter } => iter.next_back()),* } + } + } + + impl),*> ExactSizeIterator for $Iter<$($Variant),*> {} + }; +}