summaryrefslogtreecommitdiffstats
path: root/src/front/token.py
blob: e1bc8d8da48f5c96ba42f32f05797be500e00d53 (plain) (blame)
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
# -*- coding: utf-8 -*-

class Tag(object):
    __slots__ = ["name"]

    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

    def __repr__(self):
        return "<Tag: %s>" % self

# token tags
Tag.NUMBER      = Tag("NUMBER")
Tag.BOOL        = Tag("BOOL")
Tag.IDENT       = Tag("IDENT")
Tag.WHILE       = Tag("WHILE")
Tag.IF          = Tag("IF")
Tag.ELSE        = Tag("ELSE")
Tag.END         = Tag("END")
Tag.LBRAK       = Tag("LBRAK")
Tag.RBRAK       = Tag("RBRAK")
Tag.LPAREN      = Tag("LPAREN")
Tag.RPAREN      = Tag("RPAREN")
Tag.NEWLINE     = Tag("NEWLINE")
Tag.COMMA       = Tag("COMMA")
Tag.FUN         = Tag("FUN")
Tag.ASSIGNMENT  = Tag("ASSIGNMENT")
Tag.RETURN      = Tag("RETURN")
Tag.OPERATOR    = Tag("OPERATOR")

class Token(object):
    __slots__ = ["tag","value"]

    def __init__(self, tag, value = None):
        self.tag = tag
        self.value = value

    def __repr__(self):
        if self.value:
            return "<Token: %s, Value: %s>" % (self.tag, str(self.value))
        return "<Token: %s>" % self.tag