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
#[allow(dead_code)]
#[derive(Debug, PartialEq, Clone)]
pub enum TT {
    Let,
    WhiteSpace,
    NewLine,
    LParen,
    RParen,
    LBrace,
    RBrace,
    Comma,
    Dot,
    Plus,
    PlusAssign,
    Minus,
    MinusAssign,
    Slash,
    Star,
    Percent,
    Bang,
    BangEqual,
    Equal,
    Assign,
    Greater,
    GreaterEqual,
    Less,
    LessEqual,
    Identifier,
    And,
    Or,
    Struct,
    If,
    Else,
    Elif,
    True,
    False,
    Function,
    For,
    While,
    Print,
    EOF,
    Hash,
    Semicolon,
    Break,
    LBracket,
    RBracket,
    Letter(String),
    Num(String),
    Char(String),
    Quotation,
    Unknown,
}
#[derive(Clone)]
pub struct Coder {
    pub lex: Vec<TT>,
}
impl Coder {
    pub fn push(&mut self, tt: TT) {
        self.lex.push(tt);
    }
    pub fn new() -> Coder {
        Coder { lex: Vec::new() }
    }
    // finds the closesest TT after the current_line
    pub fn next(&self, _tt: TT, current_line: usize) -> usize {
        /*for x in current_line..self.lex.len() {
            //println!("{:#?},{}",self.lex[x],x);

            if self.lex[x] == _tt {
                /*println!("{}",x);
                println!("{:#?}",_tt);
                println!("{:#?}",self.lex[x]);*/

                return x;
            }
        }*/
        let mut _retur = 0;
        let x = self.lex[current_line..self.lex.len()]
            .iter()
            .position(|x| *x == _tt);
        let mut _pos = 0;
        match x {
            Some(x) => {
                _pos = x + current_line;
                _retur = _pos;
                //println!("test {}", pos);
            }
            None => {
                panic!("cannot find the stop for the if");
            }
        }
        if _retur != 0 {
            return _retur;
        }
        panic!("Next not found {:#?},{}", _tt, current_line)
    }
}
//this is the lexer that makes everything into a long vector of garbage that the parser then makes into a parsed vector the inter can read
pub fn lext(code: String) -> Coder {
    let code = code;
    let code: Vec<String> = code.chars().map(|x| x.to_string()).collect();
    let code: Vec<&str> = code.iter().map(|x| x.as_str()).collect();
    let mut holder = Coder::new();
    for char in code.into_iter() {
        match char {
            "[" => holder.push(TT::LBracket),
            "]" => holder.push(TT::RBracket),
            "(" => holder.push(TT::LParen),
            ")" => holder.push(TT::RParen),
            " " => holder.push(TT::WhiteSpace),
            //"=" => holder.push(TT::Equal),
            r#"""# => holder.push(TT::Quotation),

            "\n" => {}
            a => {
                let char: Vec<char> = a.chars().collect();
                match char[0] {
                    'a'..='z' => holder.push(TT::Letter(a.to_string())),
                    '0'..='9' => holder.push(TT::Num(a.to_string())),
                    a => holder.push(TT::Char(a.to_string())),
                    // _=> panic!("Thats not a letter")
                }
            } //_=> holder.push(TT::Unknown)
        }
    }
    return holder;
}