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
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Kind {
    // Operators
    Assign,
    Plus,
    Minus,
    Product,
    Divide,
    Mod,
    Bang,

    // Compare
    LT,
    LT_OR_EQ,
    GT,
    GT_OR_EQ,
    EQ,
    NOT_EQ,

    // Boolean And, Or
    And,
    Or,
    // Bit And, Or
    Bit_And,
    Bit_Or,

    Comma,
    Semicolon,

    LPAREN,   // (
    RPAREN,   // )
    LBRACE,   // {
    RBRACE,   // }
    LBRACKET, // [
    RBRACKET, // ]
    // keywords
    Illegal,
    EOF,
    Ident,
    Function,
    Let,
    True,
    False,
    If,
    Else,
    Return,

    Int,
    String,
}

impl Kind {
    pub fn to_str(&self) -> &str {
        match self {
            Kind::Assign => "=",
            Kind::Plus => "+",
            Kind::Minus => "-",
            Kind::Product => "*",
            Kind::Divide => "/",
            Kind::Mod => "%",
            Kind::Bang => "!",

            // Compare
            Kind::LT => "<",
            Kind::LT_OR_EQ => "<=",
            Kind::GT => ">",
            Kind::GT_OR_EQ => ">=",
            Kind::EQ => "==",
            Kind::NOT_EQ => "!=",

            // Boolean And, Or
            Kind::And => "&&",
            Kind::Or => "||",
            // Bit And, Or
            Kind::Bit_And => "&",
            Kind::Bit_Or => "|",

            Kind::Comma => ",",
            Kind::Semicolon => ";",

            Kind::LPAREN => "(",
            Kind::RPAREN => ")",
            Kind::LBRACE => "{",
            Kind::RBRACE => "}",
            Kind::LBRACKET => "[",
            Kind::RBRACKET => "]",
            // keywords
            Kind::Illegal => "Illegal",
            Kind::EOF => "EOF",
            Kind::Function => "fn",
            Kind::Ident => "Ident",
            Kind::Let => "let",
            Kind::True => "true",
            Kind::False => "false",
            Kind::If => "if",
            Kind::Else => "else",
            Kind::Return => "return",
            Kind::Int => "Int",
            Kind::String => "String",
        }
    }
}

#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq)]
pub struct Token {
    pub kind: Kind,
    pub literal: String,
}

impl Token {
    pub fn new(kind: Kind) -> Self {
        Token {
            kind,
            literal: kind.to_str().to_string(),
        }
    }
    pub fn with(kind: Kind, literal: &str) -> Self {
        Token {
            kind,
            literal: literal.to_string(),
        }
    }
}

pub fn get_token_kind(word: &str) -> Kind {
    match word {
        "fn" => Kind::Function,
        "let" => Kind::Let,
        "true" => Kind::True,
        "false" => Kind::False,
        "if" => Kind::If,
        "else" => Kind::Else,
        "return" => Kind::Return,
        &_ => Kind::Ident,
    }
}