Much-Assembly-Required icon indicating copy to clipboard operation
Much-Assembly-Required copied to clipboard

Refactor ParseInstruction

Open AstronautEVA opened this issue 6 years ago • 2 comments
trafficstars

I feel like it's still a little complex but I'm not sure what else to modify. Let me know of any ideas!

AstronautEVA avatar Oct 24 '19 13:10 AstronautEVA

This helps the readability a lot. I think maintenance can be improved by using arrays with the mnemonics and joining them when creating the regex. Personally i prefer caching the regex object instead of creating a new instance everytime but that maybe a bit overkill as performance does not seem a problem. Just a preference in style.

instead of

RegExp('\\b(?:mov|add|sub|and|or|test|cmp|shl|shr|xor|rol|ror|sal|sar|rcl|xchg|rcr)\\b').test(mnemonic.toLowerCase());

Do something like this:

const mnemonics = ['mov', 'add', 'sub', ...];
const regex = new RegExp(`\\b(?:${mnemonics.join('|')})\\b`);
function someCheckWithRegex(code) {
   return regex.test(code.toLowerCase())
}

Ofcourse depending on which of my suggestions you would want to implement.

kevinramharak avatar Oct 25 '19 09:10 kevinramharak

I completely agree! I had planned on doing something about that but then got side-tracked. I'll push some updates later.

AstronautEVA avatar Oct 28 '19 12:10 AstronautEVA