-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.py
84 lines (64 loc) · 2.54 KB
/
tokens.py
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
class Token:
def __init__(self, value, type, start_index, end_index):
self.value = value
self.type = type
self.start_index = start_index
self.end_index = end_index
def __repr__(self) -> str:
# return f'{str(self.value)}'
# return f'{str(self.value)}>{str(self.type)[:3]}'
return f'"{str(self.value)}>{str(self.type)[:3]}"'
class Integer(Token):
def __init__(self, value, start_index=None, end_index=None):
super().__init__(int(value), 'integer', start_index, end_index)
class Float(Token):
def __init__(self, value, start_index=None, end_index=None):
super().__init__(float(value), 'float', start_index, end_index)
class Operation(Token):
def __init__(self, value, start_index=None, end_index=None):
super().__init__(value, 'operation', start_index, end_index)
def execute(self, num1, num2):
match self.value:
case '+':
return num1 + num2
case '-':
return num1 - num2
case '*':
return num1 * num2
case '/':
return num1 / num2
class Variable(Token):
def __init__(self, value, start_index=None, end_index=None):
super().__init__(value, 'variable', start_index, end_index)
class Declaration(Token):
def __init__(self, value, start_index=None, end_index=None):
super().__init__(value, 'declaration', start_index, end_index)
class BooleanOperator(Token):
def __init__(self, value, start_index=None, end_index=None):
super().__init__(value, 'boolean_operator', start_index, end_index)
def execute(self, comp1, comp2=None):
match self.value:
case 'و':
return int(comp1 and comp2)
case 'أو':
return int(comp1 or comp2)
case 'عكس':
return int(not comp1)
class Comparision(Token):
def __init__(self, value, start_index, end_index):
super().__init__(value, 'comparision', start_index, end_index)
def execute(self, num1, num2):
match self.value:
case '>':
return int(num1 > num2)
case '<':
return int(num1 < num2)
case '==':
return int(num1 == num2)
case '<=':
return int(num1 <= num2)
case '>=':
return int(num1 >= num2)
class Keyword(Token):
def __init__(self, value, start_index, end_index):
super().__init__(value, 'keyword', start_index, end_index)