Conversation
andreikop
left a comment
There was a problem hiding this comment.
I've added some comments.
And one more generic issue.
Initially I wrote this script like use-one-and-drop tool, but it seems like it is used by somebody and even developed further. But it can not grow without unit tests. It is not mandatory requirement, I'll merge this PR as is when minor issues fixed, but would be really cool if you can create some tests.
|
|
||
| # subst = "\\2\\4(\\5):\\n\\2" | ||
|
|
||
| # line = re.sub(regex, subst, line, 0, re.VERBOSE | re.MULTILINE) |
There was a problem hiding this comment.
This code is dead. Do we need it in the repo?
| V | ||
| d | ||
| """ | ||
| regex = r"(double|int|char|float)\s+\*\s?(\w+)\b" |
There was a problem hiding this comment.
You replace only 4 standard types, but there are much more. And user defined types might exist. Is it possible to write more generic reg exp but still avoid false positive matches?
\s is declared mandatory after type name, but int* i is very common coding style.
I think variable name can include digits and underscores. Just make sure digit is not the first letter.
| for j in range(1,c-1,1): | ||
|
|
||
| """ | ||
| regex = r"for\s?\(\s?(\s?(\w+)\b\s?=\s?(\d+|\w+)\b\s?)?;\s?((\w+)\s?(>|>=|<|<=|==|!=)?\s?((\w+|\d+)?\s?(\-|\+|\*|\/)?\s?(\w+|\d+)?))?\s?;\s?(\w+\s?(\+\+|\-\-|\+|\-)(\d+)?(\w+)?)?\s?\)" |
There was a problem hiding this comment.
Wouldn't this reg exp replace both for(j = 1; j < c - 1; j+=1) and for(j = 1; j >= c - 1; j-=1) with for j in range(1,c-1,1):?
In this case hard-to-track bug might be introduced which is not visible for human who edits generated code.
| V | ||
| i | ||
| """ | ||
| line = re.sub('(int|double|float|char) ', '', line) # |
There was a problem hiding this comment.
Couldn't we join this reg exp with reg exp which removes pointers and write something generic like
[optional const] [optional unsigned] [standard type name] [optional star, at least one space before or after star, or just space without star] [variable name which starts from letter or underscore and might contain digits] ?
| """ remove ++ | ||
|
|
||
| i++ | ||
| V | ||
| i+=1 | ||
| """ | ||
| line = re.sub('\+\+', '+=1', line) # | ||
|
|
||
| """ remove -- | ||
|
|
||
| i-- | ||
| V | ||
| i-=1 | ||
| """ | ||
| line = re.sub('--', '-=1', line) # | ||
|
|
||
| line = re.sub(';', '', line) # | ||
|
|
||
| # ALTER % IN PRINT TO STR() FUNCTION | ||
|
|
||
| line = re.sub('printf', 'print', line) |
| regex = r"print\((\"[\!\+\-\*\\a-zA-Z0-9_ ,\[\]\.\%\:]+\s?\")\,?\s?([\+\-\*\\a-zA-Z0-9_ ,\[\]\.\%\(\)]+)*\)" | ||
|
|
||
| subst = "print(\\1, %(\\2))" |
There was a problem hiding this comment.
Could you please add comments for this code with examples before -> after? I don't understand it
| #FILL ARRAY | ||
| regex = r""" | ||
| ([a-zA-Z0-9_]+)\s*\[([a-zA-Z0-9_]+)\s*((\+=)|(\-=)|(\+\+))\s*([0-9]*)\]\s*\=*\s* | ||
| """ | ||
|
|
||
| subst = "\\2\\3\\7\n\\1[\\2] = " |
| regex = r",\s*%\(\)" | ||
|
|
||
| subst = "" | ||
|
|
||
| line = re.sub(regex, subst, line, 0, re.VERBOSE | re.MULTILINE) |
No description provided.