-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokens.py
More file actions
38 lines (33 loc) · 956 Bytes
/
Copy pathtokens.py
File metadata and controls
38 lines (33 loc) · 956 Bytes
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
from typing import Optional
TT_NUMBER = "TT_NUMBER"
TT_PLUS = "TT_PLUS"
TT_MINUS = "TT_MINUS"
TT_MULTIPLY = "TT_MULTIPLY"
TT_DIVIDE = "TT_DIVIDE"
TT_MODULO = "TT_MODULO"
TT_LPAREN = "TT_LPAREN"
TT_RPAREN = "TT_RPAREN"
def operator_to_character(token_type: str) -> str:
if token_type == TT_PLUS:
return "+"
elif token_type == TT_MINUS:
return "-"
elif token_type == TT_MULTIPLY:
return "*"
elif token_type == TT_DIVIDE:
return "/"
elif token_type == TT_MODULO:
return "%"
elif token_type == TT_LPAREN:
return "("
elif token_type == TT_RPAREN:
return ")"
class Token:
def __init__(self,type:str,value:Optional[float]=None):
self.type = type
self.value = value
def __repr__(self) -> str:
token_repr = self.type.replace("TT_","")
if self.value is not None:
token_repr += ":" + str(self.value)
return token_repr