avid 0.6.1

A plug-and-play scripting language
Documentation
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use std::{
    collections::{HashMap, HashSet},
    fmt,
    iter::{self, Enumerate},
    mem, sync,
};

use crate::{
    boo::Boo,
    builder::BuildTarget,
    func::Callable,
    stack::{Avid, Stack},
    Ast, Error, ErrorKind, Location,
};

#[derive(Debug)]
pub(crate) struct Lexer<'a> {
    #[allow(unused)]
    original: &'a str, // This will be used to display error messages
    current_file_name: Option<sync::Arc<String>>,
    src: iter::Peekable<iter::Enumerate<std::str::Lines<'a>>>,
    current_line: iter::Peekable<std::str::CharIndices<'a>>,
    current_line_number: usize,
    is_neg_num: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Token {
    pub data: TokenData,
    pub loc: Location,
}

impl Token {
    pub fn new(data: TokenData, loc: Location) -> Self {
        Self { data, loc }
    }
}

enum_type! {
    // all unparsed tokens
    // TODO(#2): Include location in token data
    hidden TokenData {
        Int(isize),
        String(String),
        // The name of something not provided, ie a configuration function/value
        Promise(String),
        Bool(bool),
        If,
        // The token of the "while" in a "while ... do ... end"
        StartWhile,
        // The token of the "do" in a "while ... do ... end"
        DoWhile,
        End
    }

}

impl Clone for TokenData {
    fn clone(&self) -> Self {
        match self {
            Self::Int(arg0) => Self::Int(*arg0),
            Self::String(arg0) => Self::String(arg0.clone()),
            Self::Promise(arg0) => Self::Promise(arg0.clone()),
            Self::Bool(arg0) => Self::Bool(*arg0),
            Self::If => Self::If,
            Self::StartWhile => Self::StartWhile,
            Self::DoWhile => Self::DoWhile,
            Self::End => Self::End,
        }
    }
}

#[derive(Debug, PartialEq)]
pub(crate) struct Operation {
    pub(crate) typ: OpType,
    pub(crate) loc: Location,
}

impl Operation {
    pub fn new(typ: OpType, loc: Location) -> Self {
        Self { typ, loc }
    }
}

// Parsed tokens, including code flow stuff
pub(crate) enum OpType {
    Int(isize),
    String(String),
    Bool(bool),
    // The index into the vector of provided operations.
    // This is rather hacky, but it gets around the inability to clone
    // Box<dyn T>
    Provided(usize),
    // The name of something not provided, ie a configuration function/value
    Promise(String),
    // How far to jump forward if the condition is false
    If(usize),
    // A marker that shows the start of a `while` loop
    WhileMarker,
    // A marker that shows the `do` of a `while..do` loop
    DoMarker,
    // How far to jump backward when reached, if it's there at all.
    End(Option<usize>),
}

assert_send!(Operation);

impl PartialEq for OpType {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Int(l0), Self::Int(r0)) => l0 == r0,
            (Self::Bool(b1), Self::Bool(b2)) => b1 == b2,
            (Self::String(l0), Self::String(r0)) => l0 == r0,
            (Self::Provided(i), Self::Provided(l)) => i == l,
            (Self::Promise(l0), Self::Promise(r0)) => l0 == r0,
            (Self::WhileMarker, Self::WhileMarker) => true,
            (Self::DoMarker, Self::DoMarker) => true,
            _ => false,
        }
    }
}

impl fmt::Debug for OpType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(),
            Self::String(arg0) => f.debug_tuple("String").field(arg0).finish(),
            Self::Provided(arg0) => f.debug_tuple("Provided").field(arg0).finish(),
            Self::Promise(arg0) => f.debug_tuple("Promise").field(arg0).finish(),
            Self::If(i) => f.debug_tuple("If").field(i).finish(),
            Self::End(e) => f.debug_tuple("End").field(e).finish(),
            Self::Bool(b) => f.debug_tuple("Bool").field(b).finish(),
            Self::WhileMarker => write!(f, "While"),
            Self::DoMarker => write!(f, "Do"),
        }
    }
}

impl<'a> Lexer<'a> {
    pub fn new(src: &'a str, src_name: Option<String>) -> Self {
        Self {
            original: src,
            current_file_name: src_name.map(sync::Arc::new),
            src: src.lines().enumerate().peekable(),
            current_line: "".char_indices().peekable(),
            current_line_number: 0,
            is_neg_num: false,
        }
    }

    pub(crate) fn parse_str(&mut self) -> crate::Result<Token> {
        assert_eq!(self.current_line.next().map(|x| x.1), Some('"'));

        let mut s = String::new();

        let mut last_idx = 0;

        let mut start_idx = None;

        while let Some((_, col, ch)) = self.next_ch() {
            if start_idx == None {
                start_idx = Some(col);
            }
            last_idx = col;
            match ch {
                '"' => {
                    return Ok(Token::new(
                        TokenData::String(s),
                        self.current_loc_from_col(start_idx.unwrap()),
                    ))
                }
                '\\' => {
                    if let Some((_, col, ch)) = self.next_ch() {
                        last_idx = col;
                        let to_push = match ch {
                            'n' => '\n',
                            '\n' => continue,
                            't' => '\t',
                            '\\' => '\\',
                            '"' => '"',
                            '\'' => '\'',
                            'r' => '\r',
                            _ => {
                                return Err(Error::new(
                                    ErrorKind::UnknownEscape,
                                    self.current_loc_from_col(col),
                                ))
                            }
                        };
                        s.push(to_push);
                    } else {
                        return Err(Error::new(
                            ErrorKind::UnclosedString,
                            self.current_loc_from_col(last_idx),
                        ));
                    }
                }
                _ => s.push(ch),
            }
        }

        Err(Error::new(
            ErrorKind::UnclosedString,
            self.current_loc_from_col(last_idx),
        ))
    }

    fn parse_identifier(&mut self, to_prepend: Option<char>) -> crate::Result<Token> {
        let col_num = self.peek_ch().map_or(0, |s| s.1);

        let mut ident = self
            .current_line
            .by_ref()
            .map(|x| x.1)
            .take_while(|ch| *ch != ' ')
            .collect::<String>();

        if let Some(ch) = to_prepend {
            ident.insert(0, ch);
        }

        let tok = match ident.as_str() {
            "if" => TokenData::If,
            "end" => TokenData::End,
            "false" => TokenData::Bool(false),
            "true" => TokenData::Bool(true),
            "while" => TokenData::StartWhile,
            "do" => TokenData::DoWhile,
            _ => TokenData::Promise(ident),
        };

        Ok(Token::new(tok, self.current_loc_from_col(col_num)))
    }

    fn current_loc_from_col(&self, col: usize) -> Location {
        Location {
            // the +1's are to correct for 0-based indexing
            col: col + 1,
            line: self.current_line_number + 1,
            file_name: self.current_file_name.as_ref().map(sync::Arc::clone),
        }
    }

    fn parse_number(&mut self, base: u32) -> crate::Result<Token> {
        let sign = if self.is_neg_num { -1 } else { 1 };
        self.is_neg_num = false;
        let col = self.peek_ch().map_or(0, |s| s.1);

        let s = self
            .current_line
            .by_ref()
            .map(|x| x.1)
            .take_while(|x| *x != ' ')
            .collect::<String>();

        match isize::from_str_radix(s.as_str(), base) {
            Err(_) => Err(Error::new(
                ErrorKind::IncorrectNumber,
                self.current_loc_from_col(col),
            )),
            Ok(n) => Ok(Token::new(
                TokenData::Int(n * sign),
                self.current_loc_from_col(col),
            )),
        }
    }

    fn peek_ch(&mut self) -> Option<(usize, usize, char)> {
        match self.current_line.peek() {
            Some((col, ch)) => Some((self.current_line_number, *col, *ch)),
            None => {
                let (num, line) = self.src.peek()?;
                let mut peekable = line.char_indices().peekable();
                let ch = peekable.peek()?;
                Some((*num, ch.0, ch.1))
            }
        }
    }

    fn next_ch(&mut self) -> Option<(usize, usize, char)> {
        match self.current_line.next() {
            Some((col, ch)) => Some((self.current_line_number, col, ch)),
            None => {
                match self.src.next() {
                    Some((num, line)) => {
                        let prev_num = self.current_line_number;
                        self.current_line_number = num;
                        self.current_line = line.char_indices().peekable();
                        // TODO(#17): Give the correct line number in Parser::next_ch() when hitting a newline
                        Some((prev_num, 0, '\n'))
                    }
                    None => None,
                }
            }
        }
    }

    fn skip_comment(&mut self) {
        let mut last_star = false;
        while let Some((_, _, ch)) = self.next_ch() {
            if ch == '*' {
                last_star = true;
            } else if ch == '/' && last_star {
                return;
            } else {
                last_star = false;
            }
        }
    }
}

impl<'a> iter::Iterator for Lexer<'a> {
    type Item = crate::Result<Token>;

    fn next(&mut self) -> Option<crate::Result<Token>> {
        if let Some((start_col, ch)) = self.current_line.peek() {
            let start_col = *start_col;
            match ch {
                '"' => Some(self.parse_str()),

                ' ' => {
                    self.current_line.next(); // Get rid of the space
                    self.next()
                }
                '0' => {
                    self.current_line.next(); //get rid of the 0
                    if let Some((col, ch)) = self.current_line.peek() {
                        let col = *col;
                        let base = match ch {
                            'x' => 16,
                            'b' => 2,
                            'o' => 8,
                            '0'..='9' => 10,
                            ' ' | '\n' | '\t' => {
                                return Some(Ok(Token::new(
                                    TokenData::Int(0),
                                    self.current_loc_from_col(col),
                                )))
                            }
                            _ => {
                                return Some(Err(Error::new(
                                    ErrorKind::IncorrectNumber,
                                    self.current_loc_from_col(col),
                                )))
                            }
                        };
                        self.current_line.next();
                        Some(self.parse_number(base))
                    } else {
                        Some(Ok(Token::new(
                            TokenData::Int(0),
                            self.current_loc_from_col(start_col),
                        )))
                    }
                }

                '1'..='9' => Some(self.parse_number(10)),

                '-' => {
                    self.current_line.next(); // Get rid of dash

                    if let Some((_, ch)) = self.current_line.peek() {
                        if ch.is_numeric() {
                            self.is_neg_num = true;
                            self.next()
                        } else {
                            Some(self.parse_identifier(Some('-')))
                        }
                    } else {
                        Some(Ok(Token::new(
                            TokenData::Promise("-".to_string()),
                            self.current_loc_from_col(start_col),
                        )))
                    }
                }
                '/' => {
                    self.current_line.next(); // get rid of slash
                    if let Some((_, ch)) = self.current_line.peek() {
                        if *ch == '*' {
                            self.skip_comment();
                            self.next()
                        } else {
                            Some(self.parse_identifier(Some('/')))
                        }
                    } else {
                        Some(Ok(Token::new(
                            TokenData::Promise("/".to_string()),
                            self.current_loc_from_col(start_col),
                        )))
                    }
                }
                _ => Some(self.parse_identifier(None)),
            }
        } else {
            let (line_num, line) = self.src.next()?;

            self.current_line_number = line_num;
            self.current_line = line.char_indices().peekable();

            self.next()
        }
    }
}

pub(crate) struct Parser<'a, 'f, T: BuildTarget<'f, 'f>> {
    toks: Option<Enumerate<Lexer<'a>>>,
    promised: HashSet<String>,
    // The usize represents the index into the `provided_vec`.
    // If the function has already been inserted into the vec, it is
    // None.
    provided: HashMap<String, (usize, Option<T::Function>)>,
    provided_vec: Vec<Option<T::Function>>,
    cf_ops: Vec<CFOp>,
    // A reference to the already-parsed list of operations.
    operations: Vec<Operation>,
}

#[derive(Debug)]
struct CFOp {
    loc: Location,
    idx: usize,
    typ: CFOpType,
}

impl From<(Location, usize, CFOpType)> for CFOp {
    fn from(a: (Location, usize, CFOpType)) -> Self {
        Self {
            loc: a.0,
            idx: a.1,
            typ: a.2,
        }
    }
}

// An enum to represent the types of control flow operators
#[derive(Debug)]
enum CFOpType {
    If,
    StartWhile,
    DoWhile,
}

impl<'a, 'f> Parser<'a, 'f, Avid<'f, 'f>> {
    pub fn parse(mut self) -> crate::Result<Avid<'f, 'f>> {
        let ops = self.all_ops()?.into_iter().map(Boo::from).collect();
        let provided = self
            .provided_vec
            .into_iter()
            .map(|x| x.map(Boo::<'f, Callable<'f, 'f>>::from))
            .collect();
        Ok(Avid {
            stack: Stack::new(),
            ops,
            current_op: 0,
            provided,
        })
    }
}

impl<'a, 'f> Parser<'a, 'f, Ast<'f>> {
    pub fn parse(mut self) -> crate::Result<Ast<'f>> {
        let ops = self.all_ops()?;

        Ok(Ast {
            ops,
            provided: self.provided_vec.into_iter().collect(),
        })
    }
}

impl<'a, 'f, T: BuildTarget<'f, 'f>> Parser<'a, 'f, T> {
    pub fn all_ops(&mut self) -> crate::Result<Vec<Operation>> {
        while let Some(res) = self.next() {
            match res {
                Ok(o) => {
                    self.operations.push(o);
                }
                Err(e) => return Err(e),
            }
        }

        if let Some(CFOp {
            loc: location,
            idx: _,
            typ: op,
        }) = self.cf_ops.pop()
        {
            let err = match op {
                CFOpType::If => ErrorKind::UnclosedIf,
                CFOpType::StartWhile | CFOpType::DoWhile => ErrorKind::UnclosedWhile,
            };
            return Err(Error::new(err, location));
        }

        let mut buffer = Vec::new();

        mem::swap(&mut buffer, &mut self.operations);

        Ok(buffer)
    }

    pub fn new(
        src: &'a str,
        src_name: Option<String>,
        promised: HashSet<String>,
        provided: HashMap<String, T::Function>,
    ) -> Self {
        let (keys, vals): (Vec<_>, Vec<_>) = provided.into_iter().unzip();

        let provided: HashMap<_, _> = keys
            .into_iter()
            .zip(vals.into_iter().map(Option::Some).enumerate())
            .collect();
        let provided_len = provided.len();

        let mut provided_vec = Vec::with_capacity(provided_len);
        for _ in 0..provided_len {
            provided_vec.push(None);
        }

        Self {
            toks: Some(Lexer::new(src, src_name).enumerate()),
            operations: Vec::new(),
            cf_ops: Vec::new(),
            promised,
            provided,
            provided_vec,
        }
    }

    fn next_tok(&mut self) -> Option<(usize, crate::Result<Token>)> {
        self.toks.as_mut().map(|x| x.next()).unwrap()
    }

    // Process the token "end", not the token representing an EOF
    fn process_end_tok(&mut self, idx: usize, tok: &Token) -> crate::Result<OpType> {
        if let Some(CFOp {
            loc: location,
            idx: cf_idx,
            typ: op,
        }) = self.cf_ops.pop()
        {
            match op {
                CFOpType::If => {
                    self.operations
                        .insert(cf_idx, Operation::new(OpType::If(idx - cf_idx), location));
                    Ok(OpType::End(None))
                }
                CFOpType::DoWhile => match self.cf_ops.pop() {
                    Some(CFOp {
                        loc: _start_loc,
                        idx: start_idx,
                        typ: CFOpType::StartWhile,
                    }) => {
                        assert_eq!(
                            self.operations[start_idx].typ,
                            OpType::WhileMarker,
                            "{start_idx}"
                        );
                        assert_eq!(self.operations[cf_idx].typ, OpType::DoMarker, "{start_idx}");
                        self.operations[cf_idx].typ = OpType::If(idx - cf_idx);
                        let offset = idx - start_idx;
                        Ok(OpType::End(Some(offset)))
                    }
                    Some(_) | None => {
                        Err(Error::new(ErrorKind::DoWithoutWhile, location))
                    }
                },
                CFOpType::StartWhile => {
                    Err(Error::new(ErrorKind::WhileWithoutDo, location))
                }
            }
        } else {
            Err(Error::new(ErrorKind::UnpairedEnd, tok.loc.clone()))
        }
    }
}

impl<'a, 'f, T: BuildTarget<'f, 'f>> Iterator for Parser<'a, 'f, T> {
    type Item = crate::Result<Operation>;

    fn next(&mut self) -> Option<Self::Item> {
        let (idx, tok) = self.next_tok()?;

        if let Err(e) = tok {
            return Some(Err(e));
        }

        let tok = tok.unwrap();

        let op = match tok.data {
            TokenData::Int(i) => OpType::Int(i),
            TokenData::String(s) => OpType::String(s),
            TokenData::Bool(b) => OpType::Bool(b),
            TokenData::If => {
                self.cf_ops.push((tok.loc.clone(), idx, CFOpType::If).into());
                return self.next();
            }
            TokenData::StartWhile => {
                self.cf_ops
                    .push((tok.loc.clone(), idx, CFOpType::StartWhile).into());
                return Some(Ok(Operation::new(OpType::WhileMarker, tok.loc.clone())));
            }
            TokenData::DoWhile => {
                self.cf_ops
                    .push((tok.loc.clone(), idx, CFOpType::DoWhile).into());
                return Some(Ok(Operation::new(OpType::DoMarker, tok.loc.clone())));
            }
            TokenData::End => match self.process_end_tok(idx, &tok) {
                Ok(o) => o,
                Err(e) => return Some(Err(e)),
            },
            TokenData::Promise(p) => {
                if let Some((idx, obj)) = self.provided.get_mut(&p) {
                    if let Some(obj) = obj.take() {
                        self.provided_vec[*idx] = Some(obj);
                    }
                    OpType::Provided(*idx)
                } else if self.promised.contains(&p) {
                    OpType::Promise(p)
                } else {
                    return Some(Err(Error::new(ErrorKind::UnknownVar { name: p }, tok.loc)));
                }
            }
        };

        Some(Ok(Operation::new(op, tok.loc)))
    }
}