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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::tree::parser::ast::arg::ArgumentsType::{Named, Unnamed};
use crate::tree::parser::ast::call::Call;
use crate::tree::parser::ast::message::Message;
use crate::tree::parser::ast::Key;
use crate::tree::{cerr, TreeError};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, format, Formatter, Write};

/// Just a pair of name and type
/// This is a representation of a tree parameter
/// # Example
/// `sequence tree(a:string){...}`
/// and a:string is a param
#[derive(Clone, Debug, PartialEq)]
pub struct Param {
    pub name: Key,
    pub tpe: MesType,
}

impl Display for Param {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.name, self.tpe)
    }
}

impl Param {
    pub fn new(id: &str, tpe: MesType) -> Self {
        Param {
            name: id.to_string(),
            tpe,
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Params {
    pub params: Vec<Param>,
}

impl Params {
    pub fn new(params: Vec<Param>) -> Self {
        Params { params }
    }
}

/// The right part of the argument
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum ArgumentRhs {
    /// A pointer to the bb key or the parameter from the parent tree
    Id(Key),
    /// A scalar value
    Mes(Message),
    /// A call to a tree
    Call(Call),
}

impl ArgumentRhs {
    pub fn get_call(&self) -> Option<Call> {
        match self {
            ArgumentRhs::Call(call) => Some(call.clone()),
            _ => None,
        }
    }
}

impl Display for ArgumentRhs {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ArgumentRhs::Id(id) => write!(f, "&{}", id),
            ArgumentRhs::Mes(m) => write!(f, "{}", m),
            ArgumentRhs::Call(c) => match c {
                Call::Invocation(name, args) => {
                    write!(f, "{}({})", name, args)
                }
                Call::HoInvocation(name) => {
                    write!(f, "{}(..)", name)
                }
                Call::Lambda(tpe, _) => {
                    write!(f, "{}...", tpe)
                }
                Call::Decorator(tpe, args, _call) => {
                    write!(f, "{}({})...", tpe, args)
                }
            },
        }
    }
}

/// An argument is a pair of name and value or just a value
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum Argument {
    Assigned(Key, ArgumentRhs),
    Unassigned(ArgumentRhs),
}

impl Argument {
    pub fn has_name(&self, key: &Key) -> bool {
        matches!(self, Argument::Assigned(k, _) if k == key)
    }

    pub fn name(&self) -> Option<&Key> {
        match self {
            Argument::Assigned(k, _) => Some(k),
            Argument::Unassigned(_) => None,
        }
    }

    pub fn value(&self) -> &ArgumentRhs {
        match self {
            Argument::Assigned(_, v) | Argument::Unassigned(v) => v,
        }
    }
}

impl Display for Argument {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Argument::Assigned(k, rhs) => write!(f, "{}={}", k, rhs),
            Argument::Unassigned(rhs) => write!(f, "{}", rhs),
        }
    }
}

impl Argument {
    pub fn id(v: &str) -> Self {
        Argument::Unassigned(ArgumentRhs::Id(v.to_string()))
    }
    pub fn mes(v: Message) -> Self {
        Argument::Unassigned(ArgumentRhs::Mes(v))
    }
    pub fn call(v: Call) -> Self {
        Argument::Unassigned(ArgumentRhs::Call(v))
    }
    pub fn id_id(lhs: &str, rhs: &str) -> Self {
        Argument::Assigned(lhs.to_string(), ArgumentRhs::Id(rhs.to_string()))
    }
    pub fn id_mes(lhs: &str, rhs: Message) -> Self {
        Argument::Assigned(lhs.to_string(), ArgumentRhs::Mes(rhs))
    }
    pub fn id_call(lhs: &str, rhs: Call) -> Self {
        Argument::Assigned(lhs.to_string(), ArgumentRhs::Call(rhs))
    }
}

#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
pub struct Arguments {
    pub args: Vec<Argument>,
}

/// this is a plain representation of the argument type
/// to avoid several levels of nesting
pub enum ArgumentsType {
    Unnamed,
    Named,
    Empty,
}

impl Arguments {

    pub fn get_type(&self) -> Result<ArgumentsType, TreeError> {
        let mut curr = None;

        for a in &self.args {
            match (a, &curr) {
                (Argument::Assigned(_, _), None) => curr = Some(Named),
                (Argument::Unassigned(_), None) => curr = Some(Unnamed),
                (Argument::Assigned(_, _), Some(Named)) => {}
                (Argument::Unassigned(_), Some(Unnamed)) => {}
                _ => {
                    return Err(cerr(
                        format!("the arguments ({}) should be either named ot unnamed but not a mix",self)
                    ))
                }
            }
        }
        Ok(curr.unwrap_or(ArgumentsType::Empty))
    }
}

impl Display for Arguments {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let str = &self.args.iter().map(|a| format!("{}", a)).join(",");
        write!(f, "{}", str)
    }
}

impl Arguments {
    pub fn new(args: Vec<Argument>) -> Self {
        Self { args }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum MesType {
    Any,
    Num,
    Array,
    Object,
    String,
    Bool,
    Tree,
}

impl Display for MesType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            MesType::Any => write!(f, "any"),
            MesType::Num => write!(f, "num"),
            MesType::Array => write!(f, "array"),
            MesType::Object => write!(f, "object"),
            MesType::String => write!(f, "string"),
            MesType::Bool => write!(f, "bool"),
            MesType::Tree => write!(f, "tree"),
        }
    }
}