2424import re
2525from pathlib import Path
2626
27- def replace_body_in_curly_brackets (string_to_elaborate ):
28- """Function printing python version."""
29- new_string_to_elaborate = re .sub (r"(\{[^\{\}]*\})" , r";" , string_to_elaborate )
30- while new_string_to_elaborate != string_to_elaborate :
31- string_to_elaborate = new_string_to_elaborate
32- new_string_to_elaborate = re .sub (r"(\{[^\{\}]*\})" , r";" , string_to_elaborate )
33-
34- return string_to_elaborate
35-
3627def remove_assert (content ):
3728 """Function printing python version."""
38- # match .assert "macroName(x)", { macroName(1) }, { lda #1 }
39- content = re .sub (r".assert [\.\|\"\w\(\)\,\s+\{\}\%\#\$\;\[\]]+\}" , "" , content )
29+ # match .assert "macroName(x)", macroName(1), lda #0
30+ # on single line without curly braces
31+ content = re .sub (r".assert [^{}]*(?=\n|$)" , r"" , content )
4032
41- # match .assert "macroName(x)", macroName(1), 1
42- content = re .sub (r"(.assert [^\,]+\,[^\,]+\,[^\n]+)" , r"" , content )
33+ # match .assert "macroName(x)", { macroName(1) }, { lda #0 }
34+ # on single or multiple line with curly braces
35+ content = re .sub (r".assert [^\}]+\}[^\}]+\}" , r"" , content )
4336
4437 return content
4538
4639def remove_assert_error (content ):
4740 """Function printing python version."""
4841 # match .asserterror "macroName(x)", { macroName(1) }
49- content = re .sub (r"( .asserterror [^\,]+\,[^\ }]+\}) " , r"" , content )
42+ content = re .sub (r".asserterror [^\}]+\}" , r"" , content )
5043
5144 return content
5245
@@ -67,7 +60,7 @@ def remove_importonce(content):
6760def remove_import (content ):
6861 """Function printing python version."""
6962 # remove import
70- content = re .sub (r"#import[\w\"\\\/\.\s]* \n" , "" , content )
63+ content = re .sub (r"#import[^\"]+\"[^\"]+\" \n" , "" , content )
7164
7265 return content
7366
@@ -78,27 +71,56 @@ def remove_inital_dot_from_keywords(content):
7871 content = content .replace ('.macro ' , 'macro ' )
7972 content = content .replace ('.function ' , 'function ' )
8073 content = content .replace ('.label ' , 'label ' )
74+ content = content .replace ('.const ' , 'const ' )
8175 content = content .replace ('.pseudocommand ' , 'pseudocommand ' )
76+ content = content .replace ('.struct ' , 'struct ' )
8277
8378 return content
8479
85- def add_semicolor_to_label_declaration (content ):
80+ def fix_struct_definition (content ):
8681 """Function printing python version."""
87- # add semicolor at the end of label declaration
88- content = re .sub (r'(label[^\n]+)' , r'\1;' , content )
82+ # add a comma on the last field definition on struct before
83+ # closing curly brace
84+ content = re .sub (r"((.struct)[\s@\w]+\{[^\}]+)" , r"\1," , content )
85+ content = re .sub (r"((.struct)[\s@\w]+\{[^\}]+\})" , r"\1;" , content )
8986
9087 return content
9188
92- def convert_file ( filename ):
89+ def add_semicolon_to_label_declaration ( content ):
9390 """Function printing python version."""
94- print ("Processing " + filename )
95- head , tail = os .path .split (filename )
91+ # add semicolon at the end of label declaration
92+ content = re .sub (r'(label[^\n\,]+)' , r'\1;' , content )
93+
94+ return content
95+
96+ def add_semicolon_to_const_declaration (content ):
97+ """Function printing python version."""
98+ # add semicolon at the end of const declaration
99+ content = re .sub (r'(const[^\n\,]+)' , r'\1;' , content )
100+
101+ return content
96102
97- output_filename = head + '/output/' + tail
103+ def remove_some_newline (content ):
104+ """Function printing python version."""
105+ content = re .sub (r"[\n]{2,}\/\*\*" , r"\n\n/**" , content )
98106
99- print ("Reading..." )
100- content = open (filename , 'r' , encoding = 'utf8' ).read ()
107+ return content
108+
109+ def check_folder (path_to_folder ):
110+ """Function printing python version."""
111+ if os .path .isdir (path_to_folder ) is False :
112+ print ("Creating " + path_to_folder )
113+ os .makedirs (path_to_folder , exist_ok = True )
101114
115+ def read_file (filename ):
116+ """Function printing python version."""
117+ print ("Processing " + filename )
118+
119+ with open (filename , 'r' , encoding = 'utf8' ) as file_to_open :
120+ return file_to_open .read ()
121+
122+ def convert_file (content ):
123+ """Function printing python version."""
102124 print ("Editing..." )
103125
104126 content = remove_assert (content )
@@ -111,30 +133,23 @@ def convert_file(filename):
111133
112134 content = remove_import (content )
113135
114- # we need to clean macro/function body, but first check if there
115- # is a namespace (we don't have to clean namespace body)
116- namespace_index = content .find (".namespace " )
117- if namespace_index != - 1 :
118- # there is a namespace, preserve it and clean any body inside it
119- namespace_index = content .find ("{" , namespace_index ) + 1
120- content_in_namespace = content [namespace_index :]
121-
122- content_in_namespace_replaced = replace_body_in_curly_brackets (content_in_namespace )
123-
124- content = content .replace (content_in_namespace , content_in_namespace_replaced )
125- else :
126- # there is no namespace, clean all bodies
127- content = replace_body_in_curly_brackets (content )
136+ content = fix_struct_definition (content )
128137
129138 content = remove_inital_dot_from_keywords (content )
130139
131- content = add_semicolor_to_label_declaration (content )
140+ content = add_semicolon_to_label_declaration (content )
141+
142+ content = add_semicolon_to_const_declaration (content )
132143
133- # finish... save the new file
134- print ("Saving " + output_filename + "..." )
135- out_file = open (output_filename , 'w' , encoding = 'utf8' )
136- out_file .write (content )
137- out_file .close ()
144+ content = remove_some_newline (content )
145+ return content
146+
147+ def write_file (filename , content ):
148+ """Function printing python version."""
149+ print ("Saving " + filename + "..." )
150+ with open (filename , 'w' , encoding = 'utf8' ) as out_file :
151+ out_file .write (content )
152+ out_file .close ()
138153
139154def usage ():
140155 """Function printing python version."""
@@ -154,11 +169,22 @@ def usage():
154169 print (usage ())
155170else :
156171 print ("Using " + sys .argv [1 ] + " command line arguments" )
157- head_argument , tail_argument = os .path .split (sys .argv [1 ])
158- if os .path .isdir (head_argument + '/output' ) is False :
159- print ("Creating " + head_argument + '/output' )
160- os .makedirs (head_argument + '/output' , exist_ok = True )
172+ src_path = os .path .abspath (sys .argv [1 ])
173+ if src_path [- 1 ] != '/' :
174+ src_path += '/'
161175
162- for file in Path (sys .argv [1 ]).glob ("*.asm" ):
176+ out_path = src_path + "../output"
177+ check_folder (out_path )
178+
179+ print (out_path )
180+
181+ for file in Path (src_path ).rglob ("*.asm" ):
182+ relative_path = os .path .relpath (file , src_path )
183+ output_file = out_path + "/" + relative_path
163184 if os .path .isdir (file ) is False :
164- convert_file (str (file ))
185+ output_file_path = os .path .split (output_file )[0 ]
186+ check_folder (output_file_path )
187+
188+ content_to_elaborate = read_file (str (file ))
189+ content_to_elaborate = convert_file (content_to_elaborate )
190+ write_file (output_file , content_to_elaborate )
0 commit comments