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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use std::collections::{HashMap, HashSet};

use nom::{branch::alt, bytes::complete::tag, character::complete::satisfy, combinator::{eof, map, map_opt, peek, value}, multi::{many0, many_till}, sequence::{delimited, preceded, tuple}};
use serde::{Deserialize, Serialize};

use crate::{annotations::Annotation, context::RynaContext, parser::{empty0, identifier_parser, Location, PResult, Span}, patterns::Pattern};

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RynaMacroType {
    Function, Expression, Block, Rdl
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RdlMacro {
    Text(String),
    Var(String),
    IndexedVar(String, String),
    Loop(String, String, Box<RdlMacro>),
    Seq(Vec<RdlMacro>),
    Code(Box<RdlMacro>)
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RynaMacro {
    pub location: Location,
    pub annotations: Vec<Annotation>,
    pub name: String,
    pub m_type: RynaMacroType,
    pub pattern: Pattern,
    pub generator: RdlMacro
}

pub fn text_pattern_char(input: Span<'_>) -> PResult<char> {
    alt((
        preceded(
            tag("\\"), 
            map_opt(
                satisfy(|_| true),
                |c| match c {
                    '\\' => Some('\\'),
                    'n' => Some('\n'),
                    't' => Some('\t'),
                    '{' => Some('{'),
                    '}' => Some('}'),
                    '$' => Some('$'),
                    '@' => Some('@'),
                    _ => None
                }
            )
        ),
        satisfy(|_| true),
    ))(input)
}

pub fn text_pattern_end(input: Span<'_>) -> PResult<()> {
    alt((
        value((), tuple((tag("$"), identifier_parser))),
        value((), tuple((tag("@"), identifier_parser))),
        value((), tag("{|")),
        value((), tag("|}")),
        value((), tag("}")),
        value((), eof),
    ))(input)
}

pub fn parse_text(input: Span<'_>) -> PResult<RdlMacro> {
    map_opt(
        many_till(text_pattern_char, peek(text_pattern_end)),
        |(i, _)| if !i.is_empty() { Some(RdlMacro::Text(i.iter().collect())) } else { None }
    )(input)
}

pub fn parse_var(input: Span<'_>) -> PResult<RdlMacro> {
    map(
        preceded(
            tag("$"),
            identifier_parser
        ),
        RdlMacro::Var
    )(input)
}

pub fn parse_index(input: Span<'_>) -> PResult<RdlMacro> {
    map(
        tuple((
            tag("$"),
            identifier_parser,
            tag("."),
            identifier_parser
        )),
        |(_, c, _, i)| RdlMacro::IndexedVar(c, i)
    )(input)
}

pub fn parse_loop_header(input: Span<'_>) -> PResult<(String, String)> {
    map(
        tuple((
            tag("@"),
            identifier_parser,
            tag("."),
            identifier_parser
        )),
        |(_, c, _, i)| (c, i)
    )(input)
}

pub fn parse_loop(input: Span<'_>) -> PResult<RdlMacro> {
    map(
        tuple((
            parse_loop_header,
            empty0,
            delimited(
                tag("{"),
                parse_ryna_macro,
                tag("}")                
            )
        )),
        |(h, _, b)| RdlMacro::Loop(h.0, h.1, Box::new(b))
    )(input)
}

pub fn parse_code(input: Span<'_>) -> PResult<RdlMacro> {
    map(
        delimited(
            tag("{|"),
            parse_ryna_macro,
            tag("|}")
        ),
        |i| RdlMacro::Code(Box::new(i))
    )(input)
}

pub fn parse_ryna_macro_line(input: Span<'_>) -> PResult<RdlMacro> {
    alt((
        parse_index,
        parse_var,
        parse_loop,
        parse_code,
        parse_text
    ))(input)
}

pub fn parse_ryna_macro(input: Span<'_>) -> PResult<RdlMacro> {
    map(
        many0(parse_ryna_macro_line),
        RdlMacro::Seq
    )(input)
}

impl RdlMacro {
    pub fn get_markers(&self) -> HashSet<(bool, String)> {
        return match self {
            RdlMacro::Loop(v, i, b) => {
                let mut res = b.get_markers().into_iter().chain(vec!((false, v.clone()), (true, i.clone()))).collect::<HashSet<_>>();
                let repeated = res.iter().filter(|(it, _)| *it).cloned().collect::<Vec<_>>();

                // Delete referenced iterator variables
                for (_, s) in repeated {
                    res.remove(&(false, s.clone()));
                }

                res
            },
            
            RdlMacro::Seq(b) => b.iter().flat_map(RdlMacro::get_markers).collect(),
            RdlMacro::Var(v) => vec!((false, v.clone())).into_iter().collect(),
            RdlMacro::IndexedVar(v, i) => vec!((false, v.clone()), (true, i.clone())).into_iter().collect(),

            RdlMacro::Code(c) => c.get_markers(),

            _ => HashSet::new(),
        };
    }

    pub fn expand(&self, args: &HashMap<String, Vec<String>>, ctx: &RynaContext) -> Result<String, String> {
        macro_rules! extract_var {
            ($n: expr) => {
                match args.get($n) {
                    Some(inner) => Ok(inner),
                    _ => Err(format!("Did not extract variable with name '{}'", $n))
                }
            };
        }

        macro_rules! assert_single {
            ($args: expr, $n: expr) => {
                if $args.len() != 1 {
                    return Err(format!("Extracted {} arguments with name {} instead of 1", $args.len(), $n))
                }
            };
        }

        match self {
            RdlMacro::Text(s) => Ok(s.clone()),
            
            RdlMacro::Var(v) => {
                let var = extract_var!(v)?;
                assert_single!(var, v);

                Ok(var[0].clone())
            },
            
            RdlMacro::IndexedVar(c, i) => {
                let cs = extract_var!(c)?;
                let idx = extract_var!(i)?;
                assert_single!(idx, i);

                match idx[0].parse::<usize>() {
                    Ok(idx_usize) => Ok(cs[idx_usize].clone()),
                    Err(_) => Err(format!("Unable to parse '{}' as an index", idx[0])),
                }
            },

            RdlMacro::Loop(c, i, b) => {
                let cont = extract_var!(c)?;

                let mut args_cpy = args.clone();

                let iters = (0..cont.len()).map(|iv| {
                    *args_cpy.entry(i.clone()).or_default() = vec!(iv.to_string());

                    b.expand(&args_cpy, ctx)

                }).collect::<Result<Vec<_>, _>>()?;

                Ok(iters.join(""))
            },
            
            RdlMacro::Seq(b) => {
                Ok(b.iter().map(|i| i.expand(args, ctx)).collect::<Result<Vec<_>, _>>()?.join(""))
            },

            RdlMacro::Code(p) => {
                let sub_code = p.expand(args, ctx)?;

                let ex = RynaContext::parse_and_execute_ryna_project_inner::<false>(
                    ctx.module_path.clone(), 
                    Some(sub_code), 
                    true, 
                    ctx.optimize,
                    false,
                    &[]
                ).unwrap();

                Ok(ex.captured_output)
            }
        }
    }
}

mod tests {
    #[allow(unused)] 
    use std::collections::HashMap;
    #[allow(unused)] 
    use crate::context::standard_ctx;
    #[allow(unused)] 
    use crate::macros::RdlMacro;
    #[allow(unused)] 
    use super::parse_ryna_macro;

    #[test]
    fn macro_parsing() {
        let example_1 = "let test = arr<$type>();";
        let example_2 = "let \\$var = arr<$type>();";
        let example_3 = "if $cond { $expr \\}";
        let example_4 = "@list.i { let i = $list.i; }";
        let example_5 = "let res = {|$expr|};";

        let example_1_macro = parse_ryna_macro(example_1.into()).unwrap().1;
        let example_2_macro = parse_ryna_macro(example_2.into()).unwrap().1;
        let example_3_macro = parse_ryna_macro(example_3.into()).unwrap().1;
        let example_4_macro = parse_ryna_macro(example_4.into()).unwrap().1;
        let example_5_macro = parse_ryna_macro(example_5.into()).unwrap().1;

        assert_eq!(example_1_macro, RdlMacro::Seq(vec!(
            RdlMacro::Text("let test = arr<".into()),
            RdlMacro::Var("type".into()),
            RdlMacro::Text(">();".into())
        )));
        
        assert_eq!(example_2_macro, RdlMacro::Seq(vec!(
            RdlMacro::Text("let $var = arr<".into()),
            RdlMacro::Var("type".into()),
            RdlMacro::Text(">();".into())
        )));
        
        assert_eq!(example_3_macro, RdlMacro::Seq(vec!(
            RdlMacro::Text("if ".into()),
            RdlMacro::Var("cond".into()),
            RdlMacro::Text(" { ".into()),
            RdlMacro::Var("expr".into()),
            RdlMacro::Text(" }".into()),
        )));
        
        assert_eq!(example_4_macro, RdlMacro::Seq(vec!(
            RdlMacro::Loop("list".into(), "i".into(), Box::new(RdlMacro::Seq(vec!(
                RdlMacro::Text(" let i = ".into()),
                RdlMacro::IndexedVar("list".into(), "i".into()),
                RdlMacro::Text("; ".into())
            ))))
        )));
        
        assert_eq!(example_5_macro, RdlMacro::Seq(vec!(
            RdlMacro::Text("let res = ".into()),
            RdlMacro::Code(Box::new(RdlMacro::Seq(vec!(RdlMacro::Var("expr".into()))))),
            RdlMacro::Text(";".into()),
        )));
    }

    #[test]
    fn macro_expansion() {
        let ctx = standard_ctx();
        
        // Array syntax
        let macro_ex_str = "let res = arr<$type>(); @elems.i {res.push($elems.i);} return move(res);";

        let macro_ex = parse_ryna_macro(macro_ex_str.into()).unwrap().1;

        let expanded_code_ex = macro_ex.expand(&[
            ("type".into(), vec!("Int".into())),
            ("elems".into(), vec!("1".into(), "7".into(), "10".into()))
        ].iter().cloned().collect(), &ctx).unwrap();

        assert_eq!(
            expanded_code_ex, 
            "let res = arr<Int>(); res.push(1);res.push(7);res.push(10); return move(res);"
        );

        // Hashmap syntax
        let macro_ex_str = "let res = hashmap<$ktype, $vtype>(); @keys.i {res.add($keys.i, $values.i);} return move(res);";

        let macro_ex = parse_ryna_macro(macro_ex_str.into()).unwrap().1;

        let expanded_code_ex = macro_ex.expand(&[
            ("ktype".into(), vec!("Int".into())),
            ("vtype".into(), vec!("String".into())),
            ("keys".into(), vec!("1".into(), "7".into(), "10".into())),
            ("values".into(), vec!("\"Test 1\"".into(), "\"Test 2\"".into(), "\"Test 3\"".into()))
        ].iter().cloned().collect(), &ctx).unwrap();

        assert_eq!(
            expanded_code_ex, 
            "let res = hashmap<Int, String>(); res.add(1, \"Test 1\");res.add(7, \"Test 2\");res.add(10, \"Test 3\"); return move(res);"
        );
    }
}