From 1b633de5fbc896ee51b43cc0479aafba3b8ec254 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 18 Mar 2016 18:09:03 +0800 Subject: [PATCH 1/5] handle conversion of class instance method: generate class definition and move its method into its definition --- cpp2python.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/cpp2python.py b/cpp2python.py index 5888b27..dc1c04b 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -37,6 +37,9 @@ import os.path import re +class_lines = {'_function_':[]} +class_name = None + def is_source(filename): suffixes = ('.cpp', '.c', '.cxx', '.c++', '.cc', '.h', '.hpp', '.hxx', '.h++') for s in suffixes: @@ -45,6 +48,7 @@ def is_source(filename): return False def process_line(line): + global class_name """ remove semicolons @@ -187,6 +191,13 @@ def process_line(line): -bool pMonkeyStudio::isSameFile( const QString& left, const QString& right ) +pMonkeyStudio::isSameFile( const QString& left, const QString& right ): """ + + matches = re.findall('^[\w:&<>\*]+\s+((\w+)::)?(\w+)\(([^\)]*\))$', line) + if len(matches) > 0: + class_name = matches[0][1] + if not class_lines.has_key(class_name): + class_lines[class_name] = [] + print class_name, matches line = re.sub('^[\w:&<>\*]+\s+([\w:]+)\(([^\)]*\))$', 'def \\1(self, \\2:', line) """ after previous replacement fix "(self, )" to "(self)" @@ -279,7 +290,12 @@ def process_line(line): 'S[A-Z]+\s*\(\s*([\w\d]+)[^\)]+\)\s*\)\s*\)', '\\1.\\2.connect(\\3.\\4)', line) - return line + if class_name is not None: + # because in above code there are many rules which will produce empty lines, and they will produce incorrect identation in generated code + if len(line)>0: + class_lines[class_name].append(line) + else: + class_lines['_function_'].append(line) def process_file(in_filename, out_filename): """ @@ -289,8 +305,12 @@ def process_file(in_filename, out_filename): lines = file.readlines() # probably would die on sources more than 100 000 lines :D with open(out_filename, 'w+') as file: for line in lines: - file.write(process_line(line)) - + process_line(line) + for c in class_lines.keys(): + file.write('class %s:\r\n' % c) + for line in class_lines[c]: + file.write(' '+line) + print class_lines def main(): if '--help' in sys.argv or \ From 94e3ca98b1240a714ed2521ea4db839a53bc2f92 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 18 Mar 2016 18:21:11 +0800 Subject: [PATCH 2/5] remove & operator (reference operator or address-of operator) --- cpp2python.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpp2python.py b/cpp2python.py index dc1c04b..f7cd82e 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -235,6 +235,9 @@ def process_line(line): """ line = re.sub('::', '.', line) + # remove & (reference opeartor or address-of opeartor) + line = re.sub('&', '', line) + """ replace 'else if' with 'elif' else if (blabla) From c2b650413ec13abb25ef51a0c49de6be3acf7eea Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 18 Mar 2016 18:22:06 +0800 Subject: [PATCH 3/5] fixed Python3-style print not working for Python2 --- cpp2python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp2python.py b/cpp2python.py index f7cd82e..1baf002 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -323,7 +323,7 @@ def main(): print(help) sys.exit(0) if len (sys.argv) != 2: - print('Invalid parameters count. Must be 1', file=sys.stderr) + print >> sys.stderr, 'Invalid parameters count. Must be 1' print(help) sys.exit(-1) if os.path.isdir(sys.argv[1]): @@ -336,7 +336,7 @@ def main(): elif os.path.isfile(sys.argv[1]): process_file(sys.argv[1], sys.argv[1] + '.py') else: - print('Not a file or directory', sys.argv[1], file=sys.stderr) + print >> sys.stderr, 'Not a file or directory', sys.argv[1] sys.exit(-1) if __name__ == '__main__': From f8ecf6ab2581b3ef8149d4e6ae3496570311a5be Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 18 Mar 2016 19:50:16 +0800 Subject: [PATCH 4/5] replace "from" with "_from" (from is a keyword in python but not in c++) --- cpp2python.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp2python.py b/cpp2python.py index 1baf002..389626b 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -50,6 +50,8 @@ def is_source(filename): def process_line(line): global class_name + line = re.sub('([^\w]+)(from)([^\w]+)', '\\1_from\\3', line) + """ remove semicolons codecode(param, param); From 1b26ec958e4c7d662a826803212c06db7f60a254 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 19 Mar 2016 15:22:08 +0800 Subject: [PATCH 5/5] convert for loop to while loop (need improvement: indentation, the third part cannot include another parentheses) --- cpp2python.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpp2python.py b/cpp2python.py index 389626b..22bb5d1 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -273,6 +273,9 @@ def process_line(line): """ line = re.sub('([^\w])this([^\w])', '\\1self\\2', line) + # convert for loop to while loop (need improvement: indentation, the third part cannot include another parentheses) + line = re.sub('(\s*)for\s*\(([^;]*?);([^;]*?);\s*([^\)]*?)\)', '\\1# generated while loop\n\\1 \\2\n\\1 while(\\3):\n\\1 \\4', line) + """ Replace Qt foreach macro with Python for -foreach ( QMdiSubWindow* window, a.subWindowList() ) +foreach ( QMdiSubWindow* window, a.subWindowList() )