You should check that the input is valid, and print an error message and abort.
The easiest way to write Code.comp is to write a table of comp strings and their instruction bit values. For example, in my assembler (written in Python) I have:
_compDict = {
...
'D+1': '0011111',
'A+1': '0110111',
...
'D+A': '0000010',
'A+D': '0000010',
...
};
Most high level languages have an easy way to search a table like this. If the mnemonic is not found, print an error message and abort. Otherwise, return the bit string.
You can see that I chose to support D+A and A+D, but not 1+D or 1+A. It would be simple to add them to the table if in the future I want to support them.
--Mark