1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * AquaVM Workflow Engine
 *
 * Copyright (C) 2024 Fluence DAO
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

mod canon_stream;
mod canon_stream_map;
mod lambda_result;
mod resolved_call;
mod vec_resolved_call;

pub(crate) use canon_stream::CanonStreamIterableIngredients;
pub(crate) use canon_stream_map::CanonStreamMapIterableIngredients;
pub(crate) use lambda_result::IterableLambdaResult;
pub(crate) use resolved_call::IterableResolvedCall;
pub(crate) use vec_resolved_call::IterableVecResolvedCall;

use super::ValueAggregate;
use crate::execution_step::RcSecurityTetraplet;
use crate::JValue;

use air_interpreter_data::Provenance;
use air_interpreter_data::TracePos;

/// This trait represent bidirectional iterator and
/// is used to abstract values used in fold as iterables.
pub(crate) trait Iterable<'ctx> {
    /// Represent iterable type.
    type Item;

    /// Move inner iterator to the next value and return true if it exists,
    /// does nothing and return false otherwise.
    fn next(&mut self) -> bool;

    /// Move inner iterator to the previous value and return true if it exists,
    /// does nothing and return false otherwise.
    fn prev(&mut self) -> bool;

    /// Return current iterable value if Iterable value is not empty and None otherwise.
    fn peek(&'ctx self) -> Option<Self::Item>;

    /// Returns length of the current iterator.
    fn len(&self) -> usize;
}

/// Combines all possible iterable item types.
///
/// Iterable item is a variable that `fold` sets to each element of the collection it iterates
/// through, i.e., it is the `iterable` in the `(fold collection iterable instruction)` statement.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum IterableItem<'ctx> {
    RefValue((&'ctx JValue, RcSecurityTetraplet, TracePos, Provenance)),
    RcValue((JValue, RcSecurityTetraplet, TracePos, Provenance)),
}

impl IterableItem<'_> {
    pub(crate) fn pos(&self) -> TracePos {
        use IterableItem::*;

        let pos = match self {
            RefValue((.., pos, _)) => pos,
            RcValue((.., pos, _)) => pos,
        };

        *pos
    }

    pub(crate) fn provenance(&self) -> Provenance {
        use IterableItem::*;

        match self {
            RefValue((.., ref prov)) => prov,
            RcValue((.., ref prov)) => prov,
        }
        .clone()
    }

    pub(crate) fn into_resolved_result(self) -> ValueAggregate {
        use IterableItem::*;

        let (value, tetraplet, pos, provenance) = match self {
            RefValue((value, tetraplet, pos, prov)) => (value.clone(), tetraplet, pos, prov),
            RcValue(ingredients) => ingredients,
        };

        ValueAggregate::new(value, tetraplet, pos, provenance)
    }
}

#[macro_export]
macro_rules! foldable_next {
    ($self: expr, $len:expr) => {{
        if $self.cursor + 1 < $len {
            $self.cursor += 1;
            true
        } else {
            false
        }
    }};
}

#[macro_export]
macro_rules! foldable_prev {
    ($self: expr) => {{
        if $self.cursor >= 1 {
            $self.cursor -= 1;
            true
        } else {
            false
        }
    }};
}