-
-
Save zeffii/7339042 to your computer and use it in GitHub Desktop.
This file contains 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
istr = """for i in some_array:""" | |
ostr = """\ | |
{0}for(0 => int {1}; {1}<{2}.cap(); {1}++){{ | |
{0} {2}[{1}];\n{0}}}""" | |
def check_is_loopform(istr): | |
# returns non None only if everything seems ok. | |
# does not support tabs, but could. I don't use tabs so meh. | |
# some preprocessing, count spaces to the left, store this | |
restr = istr.lstrip() | |
spaces = len(istr) - len(restr) | |
restr = restr.strip() | |
if (not restr.startswith("for ")): | |
print("must start with keyword for") | |
return | |
elements = restr.split() | |
if (not len(elements) == 4): | |
print("must take form: for i in some_array:") | |
print("got: {} instead".format(restr)) | |
return | |
if (not elements[2] == "in"): | |
print("missing the in keyword") | |
return | |
if (not restr.endswith(":")): | |
print("must end with colon") | |
return | |
return [spaces, elements] | |
def perform_replacement(istr): | |
content = check_is_loopform(istr) | |
if not content: | |
return | |
amount_space = " "*content[0] | |
print('{} spaces, do: {}'.format(*content)) | |
print(ostr.format(amount_space, content[1][1], content[1][3][:-1])) | |
perform_replacement(istr) | |
"""outputs | |
for(0 => int i; i<some_array.cap(); i++){ | |
some_array[i]; | |
} | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment