-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepísaňje
63 lines (53 loc) · 2.4 KB
/
prepísaňje
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
#!/usr/bin/env python3
# Toto je prostredie vykonávania pre slovenský programovací jazyk Užovka.
# Počiatošné uskutočnenie v jazyku Pytón
import tokenize
from io import BytesIO
import sys
import csv
def load_substitutions_from_csv(filename):
"""Načítaj páry nahradenia z CSV súboru."""
substitutions = {}
with open(filename, mode='r', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
narecie, python_keyword = row
substitutions[python_keyword] = narecie # Swap positions for reverse translation (Python -> Užovka)
return substitutions
def ensure_space_around(token_str, substitutions):
"""Add space around translated token if it's an operator, without removing existing spaces."""
if token_str in substitutions:
translated = substitutions[token_str]
# Ensure spaces before and after the token, without worrying about existing spaces
return f' {translated} '
return token_str
def substitute_code(code, substitutions):
"""Vikonaj preklad z Pitónu do Užovki."""
result = []
tokens = list(tokenize.tokenize(BytesIO(code.encode('utf-8')).readline))
# Tokenize and substitute both names and operators/symbols
for token in tokens:
# Check for names and operators (OP)
if token.type in {tokenize.NAME, tokenize.OP} and token.string in substitutions:
# Apply the substitution and add spaces generously
translated_token = ensure_space_around(token.string, substitutions)
result.append((token.type, translated_token))
else:
result.append((token.type, token.string))
return tokenize.untokenize(result).decode('utf-8')
def translate_code_to_uzovka(filename, substitutions):
"""Prečítaj spis, prelož kód do Užovki a vypíš ho."""
with open(filename, 'r', encoding='utf-8') as f:
code = f.read()
translated_code = substitute_code(code, substitutions)
# Vypíš preložený kód
print(translated_code)
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Použiťje: prepísaňje <meno_spisu>")
sys.exit(1)
# Načítaj páry nahradenia z ČOH súboru
substitutions = load_substitutions_from_csv('nárečja.čoh')
# Získaj názov súboru zadaný ako argument
script_filename = sys.argv[1]
translate_code_to_uzovka(script_filename, substitutions)