-
Notifications
You must be signed in to change notification settings - Fork 1
/
pythangina.py
74 lines (57 loc) · 2.09 KB
/
pythangina.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
import os
import sys
import cmd
import subprocess
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'keywords'))
from keywords import keywords, splitters
def translate(code, file_extension):
inside_single_quote = False
inside_double_quote = False
tokens = []
current_token = ""
for c in code:
if c in ["'", '"']:
if c == "'":
inside_single_quote = not inside_single_quote
else:
inside_double_quote = not inside_double_quote
if not inside_single_quote and not inside_double_quote:
if current_token in keywords:
current_token = keywords[current_token]
current_token += c
if c in splitters:
tokens.append(current_token)
current_token = ""
tokens.append(current_token)
translated_code = "".join(tokens)
if file_extension == ".puta":
translated_code = translated_code.replace(".puta", ".py")
return translated_code
def interpret(filepath):
_, file_extension = os.path.splitext(filepath)
if file_extension != ".puta":
raise ValueError("Di-wastong extension ng file. Dapat .puta")
with open(filepath, "r") as f:
code = f.read()
translated_code = translate(code, file_extension)
# Write translated code to new .py file
new_filepath = filepath.replace(".puta", ".py")
with open(new_filepath, "w") as f:
f.write(translated_code)
# Run that one instead of just doing exec()
subprocess.run(["python", new_filepath])
class TagalogInterpreter(cmd.Cmd):
intro = "Maligayang pagdating sa Tagalog Python Interpreter! I-type ang 'help' para sa isang listahan ng mga command."
prompt = ">> "
def do_execute(self, filepath):
"Magpaandar ng python file na nakasulat sa Tagalog."
try:
interpret(filepath)
except Exception as e:
print(f"Error: {e}")
def do_exit(self):
"Mag-exit ng interpreter."
return True
if __name__ == "__main__":
interpreter = TagalogInterpreter()
interpreter.cmdloop()