-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
127 lines (94 loc) · 2.78 KB
/
main.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
import argparse
import logging
from pprint import pprint
from kitty.lexer import Lexer
from kitty.parser import Parser
from kitty.validator import Validator
arg_parser = argparse.ArgumentParser()
group = arg_parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"-l", "--lex", action="store_true", help="lex input file and return token list"
)
group.add_argument(
"-c", "--compile", action="store_true", help="compile the input file"
)
group.add_argument(
"-p", "--parse", action="store_true", help="parse the input file and result AST"
)
group.add_argument(
"-p+v",
"--parse+validate",
action="store_true",
help="parse the input file, validate and solve expressions if possible and result AST",
)
group.add_argument(
"-s",
"--simulate",
action="store_true",
help="run kitty interpreter with file as input",
)
arg_parser.add_argument(
"-d", "--debug", action="store_true", help="enable compiler debug mode"
)
arg_parser.add_argument(
"-p-indent", type=int, help="indent count while print AST", default=2
)
arg_parser.add_argument(
"file", type=str, help="run kitty interpreter with file as input"
)
def lex(filename: str):
with open(filename, "r", encoding="utf-8") as f:
text = f.read()
lexer = Lexer(args.file, text)
return lexer.tokenize()
def parse(tokens):
parser = Parser(tokens)
return parser.parse()
def validate(ast):
validator = Validator(ast)
return validator.validate()
if __name__ == "__main__":
args = arg_parser.parse_args()
if args.debug:
logging.basicConfig(
level=logging.DEBUG, format="%(filename)s : %(lineno)d : %(message)s"
)
if args.compile:
print("Compiling not implemented yet!")
elif args.simulate:
print("Simulating not implemented yet!")
elif args.lex:
tokens, error = lex(args.file)
if error:
print(error)
exit(-1)
pprint(tokens)
elif args.parse:
tokens, error = lex(args.file)
if error:
print(error)
exit(-1)
res = parse(tokens)
if res.error:
print(res.error)
elif res.node:
print(res.node.pretty_repr(indent=" " * args.p_indent))
else:
print("Empty file!")
exit(0)
elif args.__dict__["parse+validate"]:
tokens, error = lex(args.file)
if error:
print(error)
exit(-1)
res = parse(tokens)
if res.error:
print(res.error)
elif not res.node:
print("Empty file!")
exit(0)
res = validate(res.node)
if res.error:
print(res.error)
else:
print(res.ast.pretty_repr(indent=" " * args.p_indent))