From 741ebe5cd173e66c9b8bdc8312ff56e15a71505a Mon Sep 17 00:00:00 2001 From: FranchuFranchu Date: Mon, 4 Mar 2024 09:31:36 -0300 Subject: [PATCH] Write utility function that calls `stacker::maybe_grow` --- src/ast.rs | 12 ++++++++---- src/host/calc_labels.rs | 3 ++- src/host/encode_def.rs | 3 ++- src/host/readback.rs | 3 ++- src/util.rs | 2 ++ src/util/stack.rs | 4 ++++ 6 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 src/util/stack.rs diff --git a/src/ast.rs b/src/ast.rs index 2768c4df..fd47f4a7 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -10,7 +10,11 @@ //! //! [interaction calculus]: https://en.wikipedia.org/wiki/Interaction_nets#Interaction_calculus -use crate::{ops::Op, run::Lab, util::deref}; +use crate::{ + ops::Op, + run::Lab, + util::{deref, maybe_grow}, +}; use std::{collections::BTreeMap, fmt, str::FromStr}; /// The top level AST node, representing a collection of named nets. @@ -113,7 +117,7 @@ struct Parser<'i> { impl<'i> Parser<'i> { /// Book = ("@" Name "=" Net)* fn parse_book(&mut self) -> Result { - stacker::maybe_grow(1024 * 32, 1024 * 1024, move || { + maybe_grow(move || { let mut book = BTreeMap::new(); while self.consume("@").is_ok() { let name = self.parse_name()?; @@ -139,7 +143,7 @@ impl<'i> Parser<'i> { } fn parse_tree(&mut self) -> Result { - stacker::maybe_grow(1024 * 32, 1024 * 1024, move || { + maybe_grow(move || { self.skip_trivia(); match self.peek_char() { // Era = "*" @@ -350,7 +354,7 @@ impl fmt::Display for Net { impl fmt::Display for Tree { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - stacker::maybe_grow(1024 * 32, 1024 * 1024, move || match self { + maybe_grow(move || match self { Tree::Era => write!(f, "*"), Tree::Ctr { lab, lft, rgt } => match lab { 0 => write!(f, "({lft} {rgt})"), diff --git a/src/host/calc_labels.rs b/src/host/calc_labels.rs index 3c203d5a..fc4395c6 100644 --- a/src/host/calc_labels.rs +++ b/src/host/calc_labels.rs @@ -1,4 +1,5 @@ use super::*; +use crate::util::maybe_grow; /// Calculates the labels used in each definition of a book. /// @@ -156,7 +157,7 @@ pub(crate) fn calculate_label_sets<'a>(book: &'a Book, host: &Host) -> impl Iter } fn visit_tree(&mut self, tree: &'a Tree, depth: Option, mut out: Option<&mut LabSet>) -> usize { - stacker::maybe_grow(1024 * 32, 1024 * 1024, move || match tree { + maybe_grow(move || match tree { Tree::Era | Tree::Var { .. } | Tree::Num { .. } => usize::MAX, Tree::Ctr { lab, lft, rgt } => { if let Some(out) = out.as_deref_mut() { diff --git a/src/host/encode_def.rs b/src/host/encode_def.rs index 2a270110..10e67bc4 100644 --- a/src/host/encode_def.rs +++ b/src/host/encode_def.rs @@ -1,4 +1,5 @@ use super::*; +use crate::util::maybe_grow; /// Converts an ast net to a list of instructions to create the net. /// @@ -54,7 +55,7 @@ pub(super) fn ast_net_to_instructions(defs: &HashMap, net: &Net) self.visit_tree(tree, trg); } fn visit_tree(&mut self, tree: &'a Tree, trg: TrgId) { - stacker::maybe_grow(1024 * 32, 1024 * 1024, move || match tree { + maybe_grow(move || match tree { Tree::Era => { self.instr.push(Instruction::LinkConst { trg, port: Port::ERA }); } diff --git a/src/host/readback.rs b/src/host/readback.rs index 544d47d4..0c16292e 100644 --- a/src/host/readback.rs +++ b/src/host/readback.rs @@ -1,4 +1,5 @@ use super::*; +use crate::util::maybe_grow; impl Host { /// Creates an ast tree from a wire in a runtime net. @@ -42,7 +43,7 @@ impl<'a> ReadbackState<'a> { /// `wire` this port was reached from must be supplied to key into the /// `vars` map. fn read_port(&mut self, port: Port, wire: Option) -> Tree { - stacker::maybe_grow(1024 * 32, 1024 * 1024, move || match port.tag() { + maybe_grow(move || match port.tag() { Tag::Var => { let key = wire.unwrap().addr().min(port.addr()); Tree::Var { diff --git a/src/util.rs b/src/util.rs index 0b87dde2..8a8b22cc 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,9 +2,11 @@ mod apply_tree; mod bi_enum; mod create_var; mod deref; +mod stack; mod stats; pub(crate) use bi_enum::*; pub(crate) use create_var::*; pub(crate) use deref::*; +pub use stack::*; pub use stats::*; diff --git a/src/util/stack.rs b/src/util/stack.rs new file mode 100644 index 00000000..528cc8cb --- /dev/null +++ b/src/util/stack.rs @@ -0,0 +1,4 @@ +/// Function that adds a guard against stack overflows in recursive functions. +pub fn maybe_grow(f: impl FnOnce() -> R) -> R { + stacker::maybe_grow(1024 * 32, 1024 * 1024, f) +}