Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/trickle.py
Last active December 27, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/7339042 to your computer and use it in GitHub Desktop.
Save zeffii/7339042 to your computer and use it in GitHub Desktop.
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