-
Notifications
You must be signed in to change notification settings - Fork 0
/
uzbek.py
157 lines (123 loc) · 4.22 KB
/
uzbek.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#######################################
# CONSTANTS
#######################################
RAQAMLAR = '0123456789' #digits
#######################################
# ERRORS
#######################################
class Error:
def __init__(self, pos_start, pos_end, error_name, details):
self.pos_start = pos_start
self.pos_end = pos_end
self.error_name = error_name
self.details = details
def as_string(self):
result = f'{self.error_name}: {self.details}\n'
result += f'File {self.pos_start.fn}, line {self.pos_start.ln + 1}'
return result
class IllegalCharError(Error):
def __init__(self, pos_start, pos_end, details):
super().__init__(pos_start, pos_end, 'Illegal Character', details)
#######################################
# POSITION
#######################################
class Position:
def __init__(self, idx, ln, col, fn, ftxt):
self.idx = idx
self.ln = ln
self.col = col
self.fn = fn
self.ftxt = ftxt
def advance(self, current_char):
self.idx += 1
self.col += 1
if current_char == '\n':
self.ln += 1
self.col = 0
return self
def copy(self):
return Position(self.idx, self.ln, self.col, self.fn, self.ftxt)
#######################################
# TOKENS
#######################################
TT_RAQAM = 'RAQAM' # int
TT_SUZMOQ = 'SUZMOG' #float
TT_QOSHISH = 'QOSHISH' #add
TT_AJRASH= 'AJRASH' #minus
TT_KOPAYTIRMOQ = 'KOPAYTIRMOQ' #multiply
TT_BOLMOQ = 'BOLMOQ' #bolmoq
TT_OQAVS = 'OQAVS' #right parenthesis
TT_CHQAVS = 'CHQAVS' #left parenthesis
class Token:
def __init__(self, type_, value=None):
self.type = type_
self.value = value
def __repr__(self):
if self.value: return f'{self.type}:{self.value}'
return f'{self.type}'
#######################################
# LEXER
#######################################
class Lexer:
def __init__(self, fn, text):
self.fn = fn
self.text = text
self.pos = Position(-1, 0, -1, fn, text)
self.current_char = None
self.advance()
def advance(self):
self.pos.advance(self.current_char)
self.current_char = self.text[self.pos.idx] if self.pos.idx < len(self.text) else None
def make_tokens(self):
tokens = []
while self.current_char != None:
if self.current_char in ' \t':
self.advance()
elif self.current_char in RAQAMLAR:
tokens.append(self.make_number())
elif self.current_char == '+':
tokens.append(Token(TT_QOSHISH))
self.advance()
elif self.current_char == '-':
tokens.append(Token(TT_AJRASH))
self.advance()
elif self.current_char == '*':
tokens.append(Token(TT_KOPAYTIRMOQ))
self.advance()
elif self.current_char == '/':
tokens.append(Token(TT_BOLMOQ))
self.advance()
elif self.current_char == '(':
tokens.append(Token(TT_OQAVS))
self.advance()
elif self.current_char == ')':
tokens.append(Token(TT_CHQAVS))
self.advance()
else:
pos_start = self.pos.copy()
char = self.current_char
self.advance()
return [], IllegalCharError(pos_start, self.pos, "'" + char + "'")
return tokens, None
def make_number(self):
num_str = ''
dot_count = 0
while self.current_char != None and self.current_char in RAQAMLAR + '.':
if self.current_char == '.':
if dot_count == 1: break
dot_count += 1
num_str += '.'
else:
num_str += self.current_char
self.advance()
if dot_count == 0:
return Token(TT_RAQAM, int(num_str))
else:
return Token(TT_SUZMOQ, float(num_str))
#######################################
# RUN
#######################################
def run(fn, text):
lexer = Lexer(fn, text)
tokens, error = lexer.make_tokens()
return tokens, error