-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
160 lines (127 loc) · 4.25 KB
/
basic.py
File metadata and controls
160 lines (127 loc) · 4.25 KB
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
158
159
160
##################
# Constants
##################
DIGITS = '0123456789'
##################
# Errors
##################
class Error:
def __init__(self, error_name, pos_start, pos_end, 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.file_name}, line {self.pos_start.line + 1}'
return result
class IllegalCharError(Error):
def __init__(self,pos_start, pos_end, details):
super().__init__('Illegal Character',pos_start, pos_end, details)
##################
# Position
##################
class Position:
def __init__(self, index, line, column, file_name, file_text):
self.index = index
self.line = line
self.column = column
self.file_name = file_name
self.file_text = file_text
def advance(self, current_char):
self.index += 1
self.column += 1
if current_char == '\n':
self.line += 1
self.column = 0
return self
def copy(self):
return Position(self.index, self.line, self.column, self.file_name, self.file_text)
##################
# Tokens
##################
# TT means Token Type
TT_INT = 'INT'
TT_FLOAT = 'FLOAT'
TT_PLUS = 'PLUS'
TT_MINUS = 'MINUS'
TT_MUL = 'MUL'
TT_DIV = 'DIV'
TT_LPAREN = 'LPAREN'
TT_RPAREN = 'RPAREN'
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, file_name, text):
"""text: the text we will be processing
pos: keep track of current position
current_char: keep track of current character"""
self.file_name = file_name
self.text = text
self.pos = Position(-1, 0, -1, file_name, file_text=text)
self.current_char = None
self.advance()
def advance(self):
self.pos.advance(self.current_char)
self.current_char = self.text[self.pos.index] if self.pos.index < 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 DIGITS:
tokens.append(self.make_number())
elif self.current_char == '+':
tokens.append(Token(TT_PLUS))
self.advance()
elif self.current_char == '-':
tokens.append(Token(TT_MINUS))
self.advance()
elif self.current_char == '*':
tokens.append(Token(TT_MUL))
self.advance()
elif self.current_char == '/':
tokens.append(Token(TT_DIV))
self.advance()
elif self.current_char == '(':
tokens.append(Token(TT_LPAREN))
self.advance()
elif self.current_char == ')':
tokens.append(Token(TT_RPAREN))
self.advance()
else:
pos_start = self.pos.copy()
char = self.current_char
self.advance()
return [], IllegalCharError(pos_start, self.pos, f"'{char}'")
return tokens, None
def make_number(self):
'''creats number one digit at a time'''
'''[dot_count] to detect float or int'''
num_str = ''
dot_count = 0
while self.current_char != None and self.current_char in DIGITS + '.':
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_INT, int(num_str))
else:
return Token(TT_FLOAT, float(num_str))
def run(file_name, text):
lexer = Lexer(file_name, text)
tokens, error = lexer.make_tokens()
return tokens, error