gabelang 3.1.0

A high level, interpretted and garbage collected programming 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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
use std::fmt::Display;
use std::iter::Peekable;

use crate::ast::{ self, join, TokenNotInfixOpErr };
use crate::lexer::{ Lexer, LexerError, Location, Token, TokenWithLocation };

#[derive(PartialEq, Debug)]
enum ParserErrorType {
    UnexpectedToken {
        expected_token: Token,
        recieved_token: Token,
    },
    UnexpectedTokenOption {
        expected_token_options: Vec<Token>,
        recieved_token: Token
    },
    TokenNotInfixOp(TokenNotInfixOpErr),
    LexerError(LexerError),
    UnexpectedEOF
}

impl From<ast::TokenNotInfixOpErr> for ParserErrorType {
    fn from(err: ast::TokenNotInfixOpErr) -> Self {
        Self::TokenNotInfixOp(err)
    }
}

impl Display for ParserErrorType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnexpectedEOF => f.write_str("Unexpected end of file"),
            Self::TokenNotInfixOp(err) => write!(f, "{}", err),
            Self::LexerError(err) => write!(f, "{}", err),
            Self::UnexpectedToken { expected_token, recieved_token } => write!(f, "Expected token {}, found {}", expected_token, recieved_token),
            Self::UnexpectedTokenOption { expected_token_options, recieved_token } => write!(f, "Expected one of the following tokens [{}], found {}", join(expected_token_options, ", "), recieved_token)
        }
    }
}

/// Error type generated when parsing fails
#[derive(PartialEq, Debug)]
pub struct ParserError {
    error_type: ParserErrorType,
    location: Location
}

impl Display for ParserError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Parser Error at line {} column {}: {}", self.location.line, self.location.position, self.error_type)
    }
}

impl From<LexerError> for ParserError {
    fn from(err: LexerError) -> Self {
        Self {
            location: err.location.clone(),
            error_type: ParserErrorType::LexerError(err)
        }
    }
}

type ParseResult<T> = Result<T, ParserError>;

/// Struct that turns a string into an AST(Abstract Syntax Tree)
pub struct Parser<'a> {
    token_iter: Peekable<Lexer<'a>>,
    location: Location
}

impl<'a> Parser<'a> {
    /// Creates a new parser from an input string
    pub fn new(contents: &'a str) -> Parser<'a> {
        let lexer = Lexer::new(contents);
        Self {
            token_iter: lexer.peekable(),
            location: Location::default()
        }
    }

    fn next_token(&mut self) -> ParseResult<Token> {
        match self.token_iter.next() {
            Some(token) => {
                let token = token?;
                self.location = token.ref_location().clone();
                Ok(token.to_token())
            },
            None => Err(self.error(ParserErrorType::UnexpectedEOF))
        }
    }

    fn peek_next_token(&mut self) -> ParseResult<Option<&Token>> {
        match self.token_iter.peek() {
            Some(token) => match token.as_ref() {
                Ok(token) => {
                    Ok(Some(token.ref_token()))
                },
                Err(err) => Err(err.clone())?

            },
            None => Ok(None)
        }
    }

    fn peek_token_is(&mut self, token: &Token) -> ParseResult<bool> {
        Ok(self.peek_next_token()? == Some(token))
    }

    fn is_eof(&mut self) -> ParseResult<bool> {
        Ok(self.peek_next_token()?.is_none())
    }

    fn peek_token_is_arithmetic(&mut self) -> ParseResult<bool> {
        if let Some(token) = self.peek_next_token()? {
            match token {
                Token::PLUS |
                Token::MINUS |
                Token::ASTERISK |
                Token::SLASH |
                Token::EQ |
                Token::NOTEQ => Ok(true),
                _ => Ok(false)
            }
        } else {
            Err(self.error(ParserErrorType::UnexpectedEOF))
        }
    }

    fn skip_optional_token(&mut self, optional_token: &Token) -> ParseResult<bool> {
        if self.peek_token_is(optional_token)? {
            self.next_token()?;
            return Ok(true)
        }
        Ok(false)
    }

    /// Turns the input string into a vec of statements
    ///
    /// If parsing fails it returns a [ParserError]
    pub fn parse_program(&mut self) -> ParseResult<Vec<ast::Statement>> {
        let mut statements: Vec<ast::Statement> = Vec::new();
        while !self.is_eof()? {
            statements.push(self.parse_statement()?);
        }
        Ok(statements)
    }

    // Precondition: Caller must only call this if the current token is an open squig
    fn parse_block(&mut self) -> ParseResult<Vec<ast::Statement>> {
        // Expect that the current token is an lsquig token
        self.expect_token(Token::LSQUIG)?;
        let mut statements = Vec::new();
        while let Some(token) = self.peek_next_token()? {
            if token == &Token::RSQUIG {
                // If you reach the close token then the block is done being read
                self.next_token()?;
                return Ok(statements);
            } else {
                statements.push(self.parse_statement()?);
            }
        }
        Err(self.error(ParserErrorType::UnexpectedEOF))
    }

    fn parse_statement(&mut self) -> ParseResult<ast::Statement> {
        match self.peek_next_token()?.unwrap() {
            Token::LET => Ok(self.parse_let_statement()?),
            Token::IDENTIFIER(_) => {
                let assignable = self.parse_assignable()?;
                Ok(self.parse_statement_after_assignable(assignable)?)
            },
            Token::DO => Ok(self.parse_do_while_loop()?),
            Token::WHILE => Ok(self.parse_while_loop()?),
            Token::FOR => Ok(self.parse_for_loop()?),
            Token::IF => Ok(self.parse_if_statement()?),
            Token::RETURN => Ok(self.parse_return_statement()?),
            Token::FN => Ok(self.parse_function()?),
            _ => Ok(self.parse_expression_statement()?)
        }
    }

    fn parse_statement_after_assignable(&mut self, assignable: ast::Assignable) -> ParseResult<ast::Statement> {
        if let Some(token) = self.peek_next_token()? {
            match token {
                Token::EQUAL => Ok(self.parse_assign_statement(assignable)?),
                Token::LPAREN => {
                    let func_call = self.parse_function_call(assignable)?;
                    if self.is_eof()? || self.peek_token_is(&Token::SEMICOLON)? {
                        return Ok(ast::Statement::Expression(func_call))
                    }
                    let expression = self.parse_infix(func_call)?;
                    let res = Ok(ast::Statement::Expression(expression));
                    self.skip_optional_token(&Token::SEMICOLON)?;
                    res
                },
                Token::SEMICOLON => {
                    self.next_token()?;
                    Ok(ast::Statement::Expression(ast::Expression::Assignable(assignable)))
                }
                _ => {
                    let expression = self.parse_infix(ast::Expression::Assignable(assignable))?;
                    self.skip_optional_token(&Token::SEMICOLON)?;
                    Ok(ast::Statement::Expression(expression))
                }
            }
        } else {
            Ok(ast::Statement::Expression(ast::Expression::Assignable(assignable)))
        }
    }

    fn parse_expression_statement(&mut self) -> ParseResult<ast::Statement> {
        let expression = self.parse_expression(0)?;
        Ok(ast::Statement::Expression(expression))
    }

    // Precondition: caller must check that current token is a let token
    fn parse_let_statement(&mut self) -> ParseResult<ast::Statement> {
        // Expect the current token to be a let token
        self.expect_token(Token::LET)?;
        // If no token after let keyword or token after let keyword is not an identifier it is an invalid let statement
        if self.is_eof()? {
            return Err(self.error(ParserErrorType::UnexpectedEOF));
        }
        // Now that we know that parse identifier will not panic, i.e., it is followed by a token, we get the identifier token
        let ident = self.parse_identifier()?;
        // Expect assignment operator in let statement
        self.expect_token(Token::EQUAL)?;
        let expression = self.parse_expression(0)?;
        self.expect_token(Token::SEMICOLON)?;
        Ok(ast::Statement::Let {
            ident,
            expression
        })
    }

    // Precondition: caller must ensure that current token is an equal token
    fn parse_assign_statement(&mut self, assignable: ast::Assignable) -> ParseResult<ast::Statement> {
        self.expect_token(Token::EQUAL)?;
        let expression = self.parse_expression(0)?;
        self.expect_token(Token::SEMICOLON)?;
        Ok(ast::Statement::Assign {
            assignable,
            expression
        })
    }

    // Syntax do { block } while cond
    // Precondition: Caller must only call this if the current token is a do token
    fn parse_do_while_loop(&mut self) -> ParseResult<ast::Statement> {
        // Expect that current token is do and move on to next token
        self.expect_token(Token::DO)?;
        let body = self.parse_block()?;
        // Expect the keyword while after code block and preceeding condition
        self.expect_token(Token::WHILE)?;
        let cond = self.parse_expression(0)?;
        Ok(ast::Statement::DoWhile {
            body,
            cond
        })
    }

    // Precondition: Caller must only call this if the current token is a while token
    fn parse_while_loop(&mut self) -> ParseResult<ast::Statement> {
        // Expect that the current token is a while token
        self.expect_token(Token::WHILE)?;
        let cond = self.parse_expression(0)?;
        let body = self.parse_block()?;
        Ok(ast::Statement::While {
            cond,
            body
        })
    }

    // Precondition: Caller must only call this if the current token is a for token
    // Syntax is for(statement; condition; statement;) {body}
    fn parse_for_loop(&mut self) -> ParseResult<ast::Statement> {
        // Expect that current token is a for token
        self.expect_token(Token::FOR)?;
        self.expect_token(Token::LPAREN)?;
        let init = Box::from(self.parse_statement()?);
        let cond = self.parse_expression(0)?;
        self.expect_token(Token::SEMICOLON)?;
        let update = Box::from(self.parse_statement()?);
        self.expect_token(Token::RPAREN)?;
        let body = self.parse_block()?;
        Ok(ast::Statement::For {
            init,
            cond,
            update,
            body
        })
    }

    // Precondition: Caller must only call this if the current token is an if token
    fn parse_if_statement(&mut self) -> ParseResult<ast::Statement> {
        // Expect that the current token is an if token
        self.expect_token(Token::IF)?;
        let cond = self.parse_expression(0)?;
        let body = self.parse_block()?;
        let r#else = if self.skip_optional_token(&Token::ELSE)? {
            Some(self.parse_block()?)
        } else {
            None
        };
        Ok(ast::Statement::If {
            cond,
            body,
            r#else
        })
    }

    // Precondition: Caller must only call this if the current token is a return token
    fn parse_return_statement(&mut self) -> ParseResult<ast::Statement> {
        // Expect current token to be return and move to expression
        self.expect_token(Token::RETURN)?;
        let return_value = if self.peek_token_is(&Token::SEMICOLON)? {
            None
        } else {
            Some(self.parse_expression(0)?)
        };
        self.expect_token(Token::SEMICOLON)?;
        Ok(ast::Statement::Return(return_value))
    }

    // Precondition: Caller must only call this if the current token is an if token
    fn parse_function(&mut self) -> ParseResult<ast::Statement> {
        // Expect current token to be fn 
        self.expect_token(Token::FN)?;
        let ident = self.parse_identifier()?;
        // Expect an open parenthesis before parameters after function name
        self.expect_token(Token::LPAREN)?;
        let mut params = Vec::new();
        // Get identifier list
        while !self.peek_token_is(&Token::RPAREN)? {
            params.push(self.parse_identifier()?);
            if !self.skip_optional_token(&Token::COMMA)? {
                break
            };
        }
        self.expect_token(Token::RPAREN)?;
        let body = self.parse_block()?;
        Ok(ast::Statement::FuncDecl(ast::Function{
            ident,
            params,
            body
        }))
    }

    // Precondition: Caller must make sure that current token is an identifier and that the
    // next token is an open parenthesis
    fn parse_function_call(&mut self, func: ast::Assignable) -> ParseResult<ast::Expression> {
        // Expect current tokent to be lparen
        self.expect_token(Token::LPAREN)?;
        // Get expressions list for function call
        let mut params: Vec<ast::Expression> = Vec::new();
        while !self.peek_token_is(&Token::RPAREN)? {
            params.push(self.parse_expression(0)?);
            if !self.skip_optional_token(&Token::COMMA)? {
                break
            };
        }
        // Param list should end with a close parenthesis otherwise it is an invalid function call
        self.expect_token(Token::RPAREN)?;
        Ok(ast::Expression::FuncCall {
            func,
            params
        })
    }

    fn parse_expression(&mut self, precidence: i8) -> ParseResult<ast::Expression> {
        // invalid expression if it is blank
        if self.is_eof()? {
            return Err(self.error(ParserErrorType::UnexpectedEOF));
        }
        // Return group expression if expression starts with an open paren
        if self.peek_token_is(&Token::LPAREN)? {
            return Ok(self.parse_group_expression()?)
        }
        // Return array literal if expression starts with an open square bracket
        if self.peek_token_is(&Token::LSQR)? {
            return Ok(ast::Expression::Literal(self.parse_array_literal()?))
        }
        if self.peek_token_is(&Token::LSQUIG)? {
            return Ok(ast::Expression::Literal(self.parse_object_literal()?))
        }
        // Get "left side" of the expression
        let mut left_side: ast::Expression = self.parse_prefix()?;
        while !self.is_eof()? {
            // Recursively parse right side of equation and build on expression "left side"
            let op_prec: i8 = Self::token_type_precedence(self.peek_next_token()?.unwrap().clone());
            // If reaches a lower precidence operation than its parent call it should not simplify
            // it for the right hand side.
            // Also if it reaches an unexpected token then expression should be complete. This is
            // done bc we can assert that precidence will always be >= 0 and so an unexpected -1
            // will always break out of the loop
            if op_prec < precidence {
                break
            }
            left_side = self.parse_infix(left_side)?;
        }
        Ok(left_side)
    }

    // Parses infix expressions including literals and identifiers
    fn parse_prefix(&mut self) -> ParseResult<ast::Expression> {
        match self.peek_next_token()?.unwrap().clone() {
            Token::IDENTIFIER(_) => {
                let assignable = self.parse_assignable()?;
                if self.peek_token_is(&Token::LPAREN)? {
                    return self.parse_function_call(assignable);
                }
                return Ok(ast::Expression::Assignable(assignable))
            },
            Token::MINUS => {
                // TODO make minus work with assigned values
                self.next_token()?;
                let number = self.parse_number()?;
                if let ast::Literal::NumberLit(num) = number {
                    Ok(ast::Expression::Literal(ast::Literal::NumberLit(-num)))
                } else {
                    unreachable!()
                }
            }
            Token::INT(_) => Ok(ast::Expression::Literal(self.parse_number()?)),
            Token::STRING(_)=> Ok(ast::Expression::Literal(self.parse_string_literal()?)),
            Token::BOOL(_) => Ok(ast::Expression::Literal(self.parse_bool_literal()?)),
            token => {
                Err(self.error(ParserErrorType::UnexpectedTokenOption { 
                    expected_token_options: vec![Token::MINUS, Token::IDENTIFIER(String::new()), Token::INT(0), Token::STRING(String::new())],
                    recieved_token: token.clone()
                }))
            }
        }
    }

    // Precondition: Caller must make sure this is called on an arithmetic token and provide left side of the equation as first parameter
    fn parse_infix(&mut self, left:ast::Expression) -> ParseResult<ast::Expression> {
        let operand_token = self.next_token()?;
        let precidence: i8 = Self::token_type_precedence(operand_token.clone());
        let right = self.parse_expression(precidence)?;
        let operand_token = TokenWithLocation::new(operand_token, self.location.clone());
        let op = match operand_token.try_into() {
            Ok(op) => op,
            Err(err) => {
                let err: ast::TokenNotInfixOpErr = err;
                return Err(self.error(err.into()));
            }
        };
        Ok(ast::Expression::Infix {
            op,
            left: Box::new(left),
            right: Box::new(right)
        })
    }

    fn parse_assignable(&mut self) -> ParseResult<ast::Assignable> {
        let mut assignable = ast::Assignable::Var(self.parse_identifier()?);
        while let Some(token) = self.peek_next_token()? {
            match token {
                Token::LSQR => {
                    // Skip past left square bracker
                    self.next_token()?;
                    // Parse the index into the array
                    let index = Box::from(self.parse_expression(0)?);
                    // Array index is invalid if it is not closed
                    self.expect_token(Token::RSQR)?;
                    assignable = ast::Assignable::PropIndex {
                        obj: Box::from(assignable), 
                        index
                    }
                },
                Token::DOT => {
                    // Skip past the dot
                    self.next_token()?;
                    // Get the property
                    let prop = self.parse_identifier()?;
                    assignable = ast::Assignable::ObjectProp { 
                        obj: Box::from(assignable),
                        prop
                    }
                },
                _ => break
            };
        }
        Ok(assignable)
    }

    // Precondition: Caller must check that current_token is an identifier
    fn parse_identifier(&mut self) -> ParseResult<String> {
        match self.next_token()? {
            Token::IDENTIFIER(ident) => Ok(ident),
            other => Err(self.error(ParserErrorType::UnexpectedToken {
                expected_token: Token::IDENTIFIER(String::new()),
                recieved_token: other
            }))
        }
    }

    // Precondition: Caller must check that current_token is an LSQR
    fn parse_array_literal(&mut self) -> ParseResult<ast::Literal> {
        // Expect the current token to be LSQR
        self.expect_token(Token::LSQR)?;
        let mut values = Vec::new();
        while !self.peek_token_is(&Token::RSQR)? {
            values.push(self.parse_expression(0)?);
            if !self.skip_optional_token(&Token::COMMA)? {
                break
            }
        }
        self.expect_token(Token::RSQR)?;
        Ok(ast::Literal::ArrayLit(values))
    }

    // Precondition: Caller must make sure that current_token is an left squigly
    fn parse_object_literal(&mut self) -> ParseResult<ast::Literal> {
        // Expect current token to be LSQUIG
        self.expect_token(Token::LSQUIG)?;
        let mut fields = Vec::new();
        while !self.peek_token_is(&Token::RSQUIG)? {
            let name = self.parse_identifier()?;
            self.expect_token(Token::COLON)?;
            let value = self.parse_expression(0)?;
            fields.push((name, value));
            // Each property should be delimited by a comma
            if !self.skip_optional_token(&Token::COMMA)? {
                break
            }
        }
        self.expect_token(Token::RSQUIG)?;
        Ok(ast::Literal::ObjectLit(fields))
    }

    // Precondition: Caller must make sure that current_token is a string token
    fn parse_string_literal(&mut self) -> ParseResult<ast::Literal> {
        let token = self.next_token()?;
        if let Token::STRING(string) = token {
            Ok(ast::Literal::StringLit(string))
        } else {
            Err(self.error(ParserErrorType::UnexpectedToken { expected_token: Token::STRING(String::new()), recieved_token: token }))
        }
    }

    fn parse_bool_literal(&mut self) -> ParseResult<ast::Literal> {
        let token = self.next_token()?;
        if let Token::BOOL(bool) = token {
            Ok(ast::Literal::Bool(bool))
        } else {
            Err(self.error(ParserErrorType::UnexpectedToken { expected_token: Token::STRING(String::new()), recieved_token: token }))
        }
    }

    // Precondition: Caller must check that current_token is a number
    fn parse_number(&mut self) -> ParseResult<ast::Literal> {
        let token = self.next_token()?;
        if let Token::INT(val) = token.clone() {
            Ok(ast::Literal::NumberLit(val as i64))
        } else {
            Err(self.error(ParserErrorType::UnexpectedToken {
                expected_token: Token::INT(0),
                recieved_token: token
            }))
        }
    }

    // Precondition: Caller must only call this if the current token is an open parenthesis
    fn parse_group_expression(&mut self) -> ParseResult<ast::Expression> {
        // Expect that the current token is LPAREN
        self.expect_token(Token::LPAREN)?;
        let expression = self.parse_expression(0)?;
        // Check that the opened group is closed
        self.expect_token(Token::RPAREN)?;
        // Contruct Group expression
        let mut expression: ast::Expression = ast::Expression::Group(Box::from(expression));
        if self.is_eof()? {
            return Err(self.error(ParserErrorType::UnexpectedEOF));
        }
        self.skip_optional_token(&Token::SEMICOLON)?;
        // If next token is arithmatic then pass in the group expression as the lhs
        if !self.is_eof()? && self.peek_token_is_arithmetic()? {
            expression = self.parse_infix(expression)?;
        }
        Ok(expression)
    }

    fn error(&self, error_type: ParserErrorType) -> ParserError {
        ParserError { 
            error_type,
            location: self.location.clone()
        }
    }

    // throws an unexpected token error if next token does not match expected token, moves the
    // token iterator
    fn expect_token(&mut self, expected_token: Token) -> ParseResult<Token> {
        let token = self.next_token()?;
        if token != expected_token {
            return Err(ParserError { 
                error_type: ParserErrorType::UnexpectedToken { 
                    expected_token,
                    recieved_token: token
                },
                location: self.location.clone()
            })
        }
        Ok(token)
    }

    fn token_type_precedence(token: Token) -> i8 {
        match token {
            Token::EQ |
            Token::NOTEQ => 1,
            Token::LT |
            Token::GT => 2,
            Token::PLUS |
            Token::MINUS => 3,
            Token::ASTERISK |
            Token::SLASH => 4,
            _ => -1
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn let_statement() {
        let input = String::from("let i = 12; let greeting = \"hello\";");
        assert_eq!(
            Parser::new(&input).parse_program(),
            Ok(vec![
                ast::Statement::Let {
                    ident: "i".to_string(),
                    expression: ast::Expression::Literal(
                        ast::Literal::NumberLit(12)
                    )
                },
                ast::Statement::Let {
                    ident: "greeting".to_string(),
                    expression: ast::Expression::Literal(
                        ast::Literal::StringLit("hello".to_string())
                    )
                }
            ])
        );
    }

    #[test]
    fn assign_statment() {
        let input = String::from("i = 12;\nvar = [1, 2, 3];\nvar_two = { a: 1, b: 2 };");
        assert_eq!(
            Parser::new(&input).parse_program(),
            Ok(vec![
                ast::Statement::Assign {
                    assignable: ast::Assignable::Var(String::from("i")),
                    expression: ast::Expression::Literal(
                        ast::Literal::NumberLit(12)
                    )
                },
                ast::Statement::Assign {
                    assignable: ast::Assignable::Var(String::from("var")),
                    expression: ast::Expression::Literal(
                        ast::Literal::ArrayLit(vec![
                            ast::Expression::Literal(ast::Literal::NumberLit(1)),
                            ast::Expression::Literal(ast::Literal::NumberLit(2)),
                            ast::Expression::Literal(ast::Literal::NumberLit(3)),
                        ])
                    )
                },
                ast::Statement::Assign {
                    assignable: ast::Assignable::Var(String::from("var_two")),
                    expression: ast::Expression::Literal(
                        ast::Literal::ObjectLit(vec![
                            (String::from("a"), ast::Expression::Literal(ast::Literal::NumberLit(1))),
                            (String::from("b"), ast::Expression::Literal(ast::Literal::NumberLit(2)))
                        ])
                    )
                }
            ])
        );
    }

    #[test]
    fn return_statement() {
        let input = String::from("return i + 1;");
        assert_eq!(
            Parser::new(&input).parse_program(),
            Ok(vec![ast::Statement::Return(Some(ast::Expression::Infix {
                op: ast::InfixOp::Add,
                left: Box::from(ast::Expression::Assignable(ast::Assignable::Var("i".to_string()))),
                right: Box::from(ast::Expression::Literal(ast::Literal::NumberLit(1)))
            }))])
        );
    }

    #[test]
    fn while_statement() {
        let input = "while i {i = i - 1;}".to_string();
        assert_eq!(
            Parser::new(&input).parse_program(),
            Ok(vec![ast::Statement::While { 
                cond: ast::Expression::Assignable(
                    ast::Assignable::Var("i".to_string())
                ),
                body: vec![
                    ast::Statement::Assign { 
                        assignable: ast::Assignable::Var("i".to_string()),
                        expression: ast::Expression::Infix { 
                            op: ast::InfixOp::Sub,
                            left: Box::from(ast::Expression::Assignable(ast::Assignable::Var("i".to_string()))),
                            right: Box::from(ast::Expression::Literal(ast::Literal::NumberLit(1)))
                        }
                    }
                ]
            }])
        )
    }

    #[test]
    fn if_statement() {
        let input = "if i > 2 * 2 {\n return i + 2;\n} else { return i; }".to_string();
        assert_eq!(
            Parser::new(&input).parse_program(),
            Ok(vec![ast::Statement::If {
                cond: ast::Expression::Infix {
                    left: Box::from(ast::Expression::Assignable(ast::Assignable::Var(String::from("i")))),
                    right: Box::from(ast::Expression::Infix {
                        left: Box::from(ast::Expression::Literal(ast::Literal::NumberLit(2))),
                        right: Box::from(ast::Expression::Literal(ast::Literal::NumberLit(2))),
                        op: ast::InfixOp::Mult
                    }),
                    op: ast::InfixOp::Gt
                },
                body: vec![ast::Statement::Return(Some(ast::Expression::Infix {
                    left: Box::from(ast::Expression::Assignable(ast::Assignable::Var(String::from("i")))),
                    right: Box::from(ast::Expression::Literal(ast::Literal::NumberLit(2))),
                    op: ast::InfixOp::Add
                }))],
                r#else: Some(vec![ast::Statement::Return(Some(ast::Expression::Assignable(
                    ast::Assignable::Var(String::from("i"))
                )))])
            }])
        )
    }

    #[test]
    fn func_decl() {
        let input = "fn times_two(num) {\n\treturn num * 2;\n}".to_string();
        assert_eq!(
            Parser::new(&input).parse_program(),
            Ok(vec![
                ast::Statement::FuncDecl(ast::Function {
                    ident: String::from("times_two"),
                    params: vec![String::from("num")],
                    body: vec![ast::Statement::Return(Some(
                        ast::Expression::Infix {
                            left: Box::from(ast::Expression::Assignable(ast::Assignable::Var(String::from("num")))),
                            right: Box::from(ast::Expression::Literal(ast::Literal::NumberLit(2))),
                            op: ast::InfixOp::Mult
                        }
                    ))]
                })
            ])
        )
    }

    #[test]
    fn expression_statement() {
        let input = "2 * times_two(num[2])".to_string();
        assert_eq!(
            Parser::new(&input).parse_program(),
            Ok(vec![ast::Statement::Expression(ast::Expression::Infix {
                left: Box::from(ast::Expression::Literal(ast::Literal::NumberLit(2))),
                right:Box::from(ast::Expression::FuncCall {
                    func: ast::Assignable::Var(String::from("times_two")),
                    params: vec![ast::Expression::Assignable(ast::Assignable::PropIndex {
                        obj: Box::from(ast::Assignable::Var(String::from("num"))),
                        index: Box::from(ast::Expression::Literal(ast::Literal::NumberLit(2)))
                    })]
                }),
                op: ast::InfixOp::Mult
            })])
        )
    }
}