Created
March 17, 2015 23:20
-
-
Save peteristhegreat/fdef92ee7914f8703fbf to your computer and use it in GitHub Desktop.
cpp to matlab converter using python and regular expressions (regex)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Handles lots of cases but only for certain kinds of c++ coding | |
# Example conversion: | |
# int count = 0; | |
# for(int i = 0; i < 5; i++) | |
# count++; | |
# | |
# changes to | |
# | |
# count = 0; | |
# for i = 0: 5 | |
# count = count + 1; | |
# end | |
# Use at your own risk! Doesn't handle function headers, includes, struct types, etc, or most things besides the basics | |
# This conversion is only supposed to speed up some of the find and replace tasks when porting, but is not a replacement | |
# for real time and effort into doing the port from c++ to matlab and testing it. | |
import re | |
files = ['temp.cpp'] | |
four_spaces = re.compile(r" {4}") | |
multi_line_comment = re.compile(r"/\*([\s\S]*?)\*/\s*", re.MULTILINE) | |
single_line_comment = re.compile(r"//") | |
square_brackets = re.compile(r"\[(.+?)\]") | |
log_statements = re.compile(r".*__LOG.*\n") | |
else_block = re.compile(r"\}\s*else\s*\{") | |
trailing_curly_brace = re.compile(r"\}") | |
leading_curly_brace = re.compile(r"\{") | |
casting_to_double = re.compile(r"\(double\)|\(float\)") | |
local_var_init_1 = re.compile(r"^(\s*)(double|int|float)\s+(\S+)\s*=\s*(\S+)\s*;", re.MULTILINE) | |
local_var_init_2 = re.compile(r"^(\s*)(double|int|float)\s+(\S+)\s*;", re.MULTILINE) | |
static_member_refs = re.compile(r"m_s_"); | |
member_refs = re.compile(r"m_"); | |
inc_for_loop = re.compile(r"for\s*\(\s*(\S*\s)?(\S+)\s*=\s*(\S+)\s*;\s*\2\s*<\s*(\S+)\s*;\s*((\+\+\2)|(\2\+\+))\s*\)") | |
generic_for_loop = re.compile(r"(\s*)for\s*\(([\s\S]*?);([\s\S]*?);([\s\S]*?)\)") | |
simple_inc_1 = re.compile(r"^(\s*)\+\+(\S*);\s*$", re.MULTILINE) | |
simple_inc_2 = re.compile(r"^(\s*)(\S*)\+\+;\s*$", re.MULTILINE) | |
simple_dec_1 = re.compile(r"^(\s*)\-\-(\S*);\s*$", re.MULTILINE) | |
simple_dec_2 = re.compile(r"^(\s*)(\S*)\-\-;\s*$", re.MULTILINE) | |
simple_arith = re.compile(r"^(\s*)(\S*)\s*([\*\+\/\-])=\s*(\S*);\s*$", re.MULTILINE) | |
# Not yet working!!! | |
if_with_no_curly_brackets = re.compile(r"^(\s*?)(if\s*\(.+?\)[^\n]*)$\s*([^\s{/%][^\n]*)$", re.MULTILINE) | |
for_with_no_curly_brackets = re.compile(r"^(\s*?)(for\s*\(.+?\)[^\n]*)$\s*([^\s{/%][^\n]*)$", re.MULTILINE) | |
inline_if_statement = re.compile(r"^(\s*?)(if\s*\(.+?\))\s*([^\s\/\)].*;)$", re.MULTILINE) | |
pointer_to_dot = re.compile(r"->") | |
for file in files: | |
data = "" | |
with open (file, "r") as cppfile: | |
data = cppfile.read() | |
data = four_spaces.sub("\t", data) | |
data = multi_line_comment.sub('', data) | |
data = inline_if_statement.sub(r'\1\2\n\1\t\3', data) | |
data = if_with_no_curly_brackets.sub(r'\1\2\n\1\t\3\n\1end\n', data) | |
data = for_with_no_curly_brackets.sub(r'\1\2\n\1\t\3\n\1end\n', data) | |
data = single_line_comment.sub("% ", data) | |
data = square_brackets.sub(r'(\1)', data) | |
data = square_brackets.sub(r'(\1)', data) # catch any double nested square_brackets | |
data = square_brackets.sub(r'(\1)', data) # catch any triple nested square_brackets | |
data = log_statements.sub('', data) | |
data = else_block.sub('else', data) | |
data = trailing_curly_brace.sub('end', data) | |
data = leading_curly_brace.sub('', data) | |
data = inc_for_loop.sub(r'for \2 = \3 : \4 - 1', data) | |
data = generic_for_loop.sub(r'\1%FIXME: for(\2;\3;\4)\1for \2:\3:\4', data) | |
data = casting_to_double.sub('', data) | |
data = static_member_refs.sub('', data) | |
data = member_refs.sub('', data) | |
data = local_var_init_1.sub(r'\1\3 = \4;', data) | |
data = local_var_init_2.sub(r'\1\3 = 0;', data) | |
data = simple_inc_1.sub(r'\1\2 = \2 + 1;', data) | |
data = simple_inc_2.sub(r'\1\2 = \2 + 1;', data) | |
data = simple_dec_1.sub(r'\1\2 = \2 - 1;', data) | |
data = simple_dec_2.sub(r'\1\2 = \2 - 1;', data) | |
data = simple_arith.sub(r'\1\2 = \2 \3 \4;', data) | |
data = pointer_to_dot.sub(r'.', data) | |
mfile = file.replace('.cpp', '.m') | |
with open (mfile, "w") as outfile: | |
outfile.write(data) | |
print(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment