Skip to content

Commit

Permalink
Write utility function that calls stacker::maybe_grow
Browse files Browse the repository at this point in the history
  • Loading branch information
FranchuFranchu committed Mar 4, 2024
1 parent d69ae53 commit 741ebe5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -113,7 +117,7 @@ struct Parser<'i> {
impl<'i> Parser<'i> {
/// Book = ("@" Name "=" Net)*
fn parse_book(&mut self) -> Result<Book, String> {
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()?;
Expand All @@ -139,7 +143,7 @@ impl<'i> Parser<'i> {
}

fn parse_tree(&mut self) -> Result<Tree, String> {
stacker::maybe_grow(1024 * 32, 1024 * 1024, move || {
maybe_grow(move || {
self.skip_trivia();
match self.peek_char() {
// Era = "*"
Expand Down Expand Up @@ -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})"),
Expand Down
3 changes: 2 additions & 1 deletion src/host/calc_labels.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::util::maybe_grow;

/// Calculates the labels used in each definition of a book.
///
Expand Down Expand Up @@ -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<usize>, 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() {
Expand Down
3 changes: 2 additions & 1 deletion src/host/encode_def.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::util::maybe_grow;

/// Converts an ast net to a list of instructions to create the net.
///
Expand Down Expand Up @@ -54,7 +55,7 @@ pub(super) fn ast_net_to_instructions(defs: &HashMap<String, DefRef>, 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 });
}
Expand Down
3 changes: 2 additions & 1 deletion src/host/readback.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::util::maybe_grow;

impl Host {
/// Creates an ast tree from a wire in a runtime net.
Expand Down Expand Up @@ -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<Wire>) -> 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 {
Expand Down
2 changes: 2 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
4 changes: 4 additions & 0 deletions src/util/stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Function that adds a guard against stack overflows in recursive functions.
pub fn maybe_grow<R>(f: impl FnOnce() -> R) -> R {
stacker::maybe_grow(1024 * 32, 1024 * 1024, f)
}

0 comments on commit 741ebe5

Please sign in to comment.