-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.py
More file actions
58 lines (55 loc) · 1.89 KB
/
lexer.py
File metadata and controls
58 lines (55 loc) · 1.89 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
from tokens import *
def _determine_indentation(line):
return len(line) - len(line.lstrip())
def _is_digit(c):
return c in '0123456789'
def tokenize(fn):
#current_indentation = 0
i_list = [0]
token_list = []
with open(fn, 'r') as f:
lines = f.readlines()
for l in range(len(lines)):
line = lines[l]
indentation = _determine_indentation(line)
if indentation > i_list[0]:
token_list.append(INDENT(l, 0))
i_list.insert(0, indentation)
elif indentation < i_list[0]:
token_list.append(DEDENT(l, 0))
i_list.insert(0, indentation)
current_indentation = indentation
line = line.strip()
pos = -1
while pos < len(line)-1:
pos += 1
if line[pos] == ':':
token_list.append(COLON(l, pos))
elif line[pos] == '+':
token_list.append(PLUS(l, pos))
elif line[pos] == 'f':
if line[pos:pos+4] == 'func':
token_list.append(FUNC(l, pos))
pos += 3
elif line[pos] == 'm':
if line[pos:pos+4] == 'main':
token_list.append(MAIN(l, pos))
pos += 3
elif line[pos] == 'p':
if line[pos:pos+5] == 'print':
token_list.append(PRINT(l, pos))
pos += 4
elif _is_digit(line[pos]):
end_pos = pos+1
while end_pos < len(line) and _is_digit(line[end_pos]):
end_pos += 1
token_list.append(INTLIT(line[pos:end_pos], l, pos))
pos = end_pos-1
elif line[pos] == ' ':
continue
else:
token_list.append(BAD(l, pos))
i_list.pop()
for i in i_list:
token_list.append(DEDENT())
return token_list