1+ import sys
2+ import re
3+
4+ class NyanTranspiler :
5+ def __init__ (self ):
6+ # 냥스크립트 -> 파이썬 키워드 매핑
7+ self .map = {
8+ r'기지개' : 'if __name__ == "__main__"' ,
9+ r'식빵굽기' : 'pass' ,
10+ r'박스' : '' , # 변수 선언 삭제
11+ r'야옹' : 'print' ,
12+ r'쫑긋' : 'input' ,
13+ r'꾹꾹이' : 'while' ,
14+ r'간식주면' : 'if' ,
15+ r'아니면' : 'else' ,
16+ r'하악질' : 'raise Exception' ,
17+ r'잡았다' : 'True' ,
18+ r'놓쳤다' : 'False' ,
19+ r'그루밍' : '#' ,
20+ }
21+
22+ def transpile (self , code ):
23+ lines = code .split ('\n ' )
24+ py_lines = []
25+ indent_level = 0
26+
27+ for line in lines :
28+ # 1차 정리: 앞뒤 공백 제거
29+ line = line .strip ()
30+ if not line : continue
31+
32+ # 1. 닫는 중괄호 '}' 처리
33+ if '}' in line :
34+ indent_level = max (0 , indent_level - 1 )
35+ line = line .replace ('}' , '' )
36+
37+ # 2. 키워드 치환
38+ for nyan_key , py_key in self .map .items ():
39+ line = line .replace (nyan_key , py_key )
40+
41+ # [✨ 추가할 코드] C스타일 주석(//)을 파이썬 주석(#)으로 변환
42+ line = line .replace ('//' , '#' )
43+
44+ # [✅ 중요 수정] 치환 후 '박스 ' 처럼 키워드 뒤에 남은 공백을 다시 한 번 제거!
45+ line = line .strip ()
46+ if not line : continue
47+
48+ # 3. 여는 중괄호 '{' 처리
49+ if '{' in line :
50+ line = line .replace ('{' , ':' )
51+ increment_indent = True
52+ else :
53+ increment_indent = False
54+
55+ # 4. 들여쓰기 적용
56+ indent_str = ' ' * indent_level
57+ py_lines .append (f"{ indent_str } { line } " )
58+
59+ if increment_indent :
60+ indent_level += 1
61+
62+ return '\n ' .join (py_lines )
63+
64+ def execute (self , file_path ):
65+ try :
66+ with open (file_path , 'r' , encoding = 'utf-8' ) as f :
67+ source_code = f .read ()
68+
69+ py_code = self .transpile (source_code )
70+
71+ # 디버깅: 깔끔해진 코드 확인
72+ # print("=== 📜 변환된 파이썬 코드 ===")
73+ # print(py_code)
74+ # print("===========================\n")
75+
76+ exec (py_code , globals ())
77+
78+ except IndentationError as e :
79+ print (f"😿 들여쓰기 오류다냥! 줄을 맞춰라냥: { e } " )
80+ except Exception as e :
81+ print (f"😿 실행 중 하악질 발생: { e } " )
82+
83+ if __name__ == "__main__" :
84+ if len (sys .argv ) < 2 :
85+ print ("사용법: python nyan.py [파일이름.nyan]" )
86+ else :
87+ interpreter = NyanTranspiler ()
88+ interpreter .execute (sys .argv [1 ])
0 commit comments