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
use crate::runtime::RuntimeError;
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, Formatter};

#[derive(Clone, Debug, PartialEq)]
pub struct Param {
    pub name: Key,
    pub tpe: MesType,
}

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 }
    }
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum ArgumentRhs {
    Id(Key),
    Mes(Message),
    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) => f.write_str(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)
                }
            },
        }
    }
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum Argument {
    Assigned(Key, ArgumentRhs),
    Unassigned(ArgumentRhs),
}

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

    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>,
}

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"
                    )))
                }
            }
        }
        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<'a> Arguments {
    pub fn new(args: Vec<Argument>) -> Self {
        Self { args }
    }
}

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