-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
60 lines (52 loc) · 1.68 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
from Bharat.bharat_lexer import BharatLexer
from Bharat.bharat_parser import BharatParser
from Bharat.bharat_interpreter import Process
import sys
import os
def repl():
lexer = BharatLexer()
parser = BharatParser()
env = {}
program = Process((), env=env)
while True:
try:
text = input('भाऱत >>> ')
if text in ("exit", "निकले"):
print('धन्यवाद')
break
except KeyboardInterrupt:
break
except EOFError:
break
if text:
tokens = lexer.tokenize(text)
try:
tree = parser.parse(tokens)
program.tree = tree
program.run()
except TypeError as e:
if str(e).startswith("'NoneType' आब्जेक्ट iterable नही है"):
print("सिन्टैक्स इरर")
else:
print(e)
def exec_file():
lexer = BharatLexer()
parser = BharatParser()
if not os.path.exists(sys.argv[1]):
print("यह फ़ाइल मौजूद नहीं है")
sys.exit(1)
if os.path.isdir(sys.argv[1]):
print("यह एक फ़ोल्डर है, फ़ाइल नहीं")
sys.exit(1)
else:
with open(sys.argv[1]) as opened_file:
tokens = lexer.tokenize(opened_file.read())
tree = parser.parse(tokens)
program = Process(tree)
program.run()
# print(program.env)
if __name__ == "__main__":
if len(sys.argv) == 1:
repl()
else:
exec_file()