I just finished my assembler in python using regular expressions for labels and command recognition. There's one thing that still bothers me, though. I can't find the single regex pattern to matches both C-command variants - with and without jump - and returns only the 'comp' part of the command. I have to use conditional statement to check if I found one or the other and then extract the data:
compVal = re.search('=(.+);?', line)
if not compVal:
compVal = re.search('=?(.+);', line)
Combining both patterns into:
=?(.+);?
doesn't work either.
The following form:
=?(.+);|=(.+);?
is working but it returns 2 separate groups of which one will always be empty and, once again, program has to check which group is the valid one.
Any suggestions?