use alloc::vec::Vec;
use core::fmt;
use miden_crypto::{Felt, hash::rpo::RpoDigest};
use miden_formatting::prettier::{Document, PrettyPrint, const_text, nl};
use super::MastNodeExt;
use crate::{
OPCODE_DYN, OPCODE_DYNCALL,
mast::{DecoratorId, MastForest},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DynNode {
is_dyncall: bool,
before_enter: Vec<DecoratorId>,
after_exit: Vec<DecoratorId>,
}
impl DynNode {
pub const DYN_DOMAIN: Felt = Felt::new(OPCODE_DYN as u64);
pub const DYNCALL_DOMAIN: Felt = Felt::new(OPCODE_DYNCALL as u64);
}
impl DynNode {
pub fn new_dyn() -> Self {
Self {
is_dyncall: false,
before_enter: Vec::new(),
after_exit: Vec::new(),
}
}
pub fn new_dyncall() -> Self {
Self {
is_dyncall: true,
before_enter: Vec::new(),
after_exit: Vec::new(),
}
}
pub fn is_dyncall(&self) -> bool {
self.is_dyncall
}
pub fn domain(&self) -> Felt {
if self.is_dyncall() {
Self::DYNCALL_DOMAIN
} else {
Self::DYN_DOMAIN
}
}
pub fn digest(&self) -> RpoDigest {
if self.is_dyncall {
RpoDigest::new([
Felt::new(8751004906421739448),
Felt::new(13469709002495534233),
Felt::new(12584249374630430826),
Felt::new(7624899870831503004),
])
} else {
RpoDigest::new([
Felt::new(8115106948140260551),
Felt::new(13491227816952616836),
Felt::new(15015806788322198710),
Felt::new(16575543461540527115),
])
}
}
pub fn before_enter(&self) -> &[DecoratorId] {
&self.before_enter
}
pub fn after_exit(&self) -> &[DecoratorId] {
&self.after_exit
}
}
impl DynNode {
pub fn append_before_enter(&mut self, decorator_ids: &[DecoratorId]) {
self.before_enter.extend_from_slice(decorator_ids);
}
pub fn append_after_exit(&mut self, decorator_ids: &[DecoratorId]) {
self.after_exit.extend_from_slice(decorator_ids);
}
}
impl MastNodeExt for DynNode {
fn decorators(&self) -> impl Iterator<Item = (usize, DecoratorId)> {
self.before_enter.iter().chain(&self.after_exit).copied().enumerate()
}
}
impl DynNode {
pub(super) fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> impl fmt::Display + 'a {
DynNodePrettyPrint { node: self, mast_forest }
}
pub(super) fn to_pretty_print<'a>(
&'a self,
mast_forest: &'a MastForest,
) -> impl PrettyPrint + 'a {
DynNodePrettyPrint { node: self, mast_forest }
}
}
struct DynNodePrettyPrint<'a> {
node: &'a DynNode,
mast_forest: &'a MastForest,
}
impl DynNodePrettyPrint<'_> {
fn concatenate_decorators(
&self,
decorator_ids: &[DecoratorId],
prepend: Document,
append: Document,
) -> Document {
let decorators = decorator_ids
.iter()
.map(|&decorator_id| self.mast_forest[decorator_id].render())
.reduce(|acc, doc| acc + const_text(" ") + doc)
.unwrap_or_default();
if decorators.is_empty() {
decorators
} else {
prepend + decorators + append
}
}
fn single_line_pre_decorators(&self) -> Document {
self.concatenate_decorators(self.node.before_enter(), Document::Empty, const_text(" "))
}
fn single_line_post_decorators(&self) -> Document {
self.concatenate_decorators(self.node.after_exit(), const_text(" "), Document::Empty)
}
fn multi_line_pre_decorators(&self) -> Document {
self.concatenate_decorators(self.node.before_enter(), Document::Empty, nl())
}
fn multi_line_post_decorators(&self) -> Document {
self.concatenate_decorators(self.node.after_exit(), nl(), Document::Empty)
}
}
impl crate::prettier::PrettyPrint for DynNodePrettyPrint<'_> {
fn render(&self) -> crate::prettier::Document {
let dyn_text = if self.node.is_dyncall() {
const_text("dyncall")
} else {
const_text("dyn")
};
let single_line = self.single_line_pre_decorators()
+ dyn_text.clone()
+ self.single_line_post_decorators();
let multi_line =
self.multi_line_pre_decorators() + dyn_text + self.multi_line_post_decorators();
single_line | multi_line
}
}
impl fmt::Display for DynNodePrettyPrint<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.pretty_print(f)
}
}
#[cfg(test)]
mod tests {
use miden_crypto::hash::rpo::Rpo256;
use super::*;
#[test]
pub fn test_dyn_node_digest() {
assert_eq!(
DynNode::new_dyn().digest(),
Rpo256::merge_in_domain(
&[RpoDigest::default(), RpoDigest::default()],
DynNode::DYN_DOMAIN
)
);
assert_eq!(
DynNode::new_dyncall().digest(),
Rpo256::merge_in_domain(
&[RpoDigest::default(), RpoDigest::default()],
DynNode::DYNCALL_DOMAIN
)
);
}
}