-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·75 lines (51 loc) · 1.25 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# """
# PPPP: Pure Python Python Parser
# """
import sys
assert sys.version[0] >= '3'
def cmd_gen():
from pppp import parsergen
parsergen.main()
def cmd_tokenize():
from pppp import tokenizer
toks = tokenizer.tokenize_file(sys.argv[1])
for t in toks:
print(t)
def cmd_parse():
from pppp import tokenizer, parser, parserbase
toks = tokenizer.tokenize_file(sys.argv[2])
if True:
for t in toks:
print(t)
p = parser.parser(toks)
T = p.parse_file_input()
if T is not None:
parserbase.print_tree(T)
else:
print(T)
def cmd_compile():
from pppp import tokenizer, parser, parserbase, compiler
toks = tokenizer.tokenize_file(sys.argv[2])
if not toks:
print("Tokenizer error")
return
p = parser.parser(toks)
T = p.parse_file_input()
if not T:
print("Parser error")
return
compiler.compile(T)
def main():
if sys.argv[1] == 'gen':
cmd_gen()
elif sys.argv[1] == 'tokenize':
cmd_tokenize()
elif sys.argv[1] == 'parse':
cmd_parse()
elif sys.argv[1] == 'compile':
cmd_compile()
if __name__ == '__main__':
main()