-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminicompiler.py
More file actions
40 lines (32 loc) · 1.05 KB
/
minicompiler.py
File metadata and controls
40 lines (32 loc) · 1.05 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
from lexer import tokenize, InvalidTokenException
from parser import TokenList, parse, IncorrectTokenException, ExcedingTokensException, NoMoreTokensException
from collections import deque
import click
@click.group()
def cli():
pass
@cli.command()
@click.argument('input_string')
def scanner(input_string: str) -> None:
try:
for token in tokenize(input_string):
print(token)
except InvalidTokenException as e:
print(e)
@cli.command()
@click.argument('input_string')
def parser(input_string: str) -> None:
try:
token_list: TokenList = deque(tokenize(input_string))
parse(token_list)
print(f'{input_string} is a correct and valid expression')
except InvalidTokenException as e:
print(e)
except IncorrectTokenException as e:
print(e)
except ExcedingTokensException:
print(f'{input_string} is too long to be a valid expression')
except NoMoreTokensException:
print(f'{input_string} is to short to be a valid expression')
if __name__ == '__main__':
cli()