mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 18:46:43 +00:00
Create helper functions for clarity
This commit is contained in:
parent
989295578f
commit
9efb79118f
46
Server/src/main/resources/static/js/editor.js
vendored
46
Server/src/main/resources/static/js/editor.js
vendored
@ -269,6 +269,25 @@ function produceError(result, currentLine, errorString) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isValidMnemonic(mnemonic) {
|
||||||
|
return isDoubleOpMnemonic(mnemonic) || isSingleOpMnemonic(mnemonic) || isZeroOpMnemonic(mnemonic);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDoubleOpMnemonic(mnemonic) {
|
||||||
|
return new RegExp('\\b(?:mov|add|sub|and|or|test|cmp|shl|shr|xor|rol|ror|sal|sar|rcl|xchg|rcr)\\b').test(mnemonic.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSingleOpMnemonic(mnemonic) {
|
||||||
|
return new RegExp('\\b(?:push|mul|pop|div|neg|call|jnz|jg|jl|jge|jle|hwi|hwq|jz|js|jns|ret|jmp|not|' +
|
||||||
|
'jc|jnc|jo|jno|inc|dec|ja|jna|seta|setnbe|setae|setnb|setnc|setbe|setna|setb|setc|' +
|
||||||
|
'setnae|sete|setz|setne|setnz|setg|setnle|setge|setnl|setle|setng|setl|setnge|seto|' +
|
||||||
|
'setno|sets|setns)\\b').test(mnemonic.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
function isZeroOpMnemonic(mnemonic) {
|
||||||
|
return new RegExp('\\b(?:ret|brk|nop|pushf|popf)\\b').test(mnemonic.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
function parseInstruction(line, result, currentLine) {
|
function parseInstruction(line, result, currentLine) {
|
||||||
line = removeComment(line);
|
line = removeComment(line);
|
||||||
line = removeLabel(line);
|
line = removeLabel(line);
|
||||||
@ -283,23 +302,19 @@ function parseInstruction(line, result, currentLine) {
|
|||||||
|
|
||||||
if (!parseDWInstruction(line, result, currentLine)) {
|
if (!parseDWInstruction(line, result, currentLine)) {
|
||||||
|
|
||||||
if (new RegExp('\\b(?:mov|add|sub|and|or|test|cmp|shl|shr|mul|push|pop|div|xor|hwi|hwq|nop|neg|' +
|
if (isValidMnemonic(mnemonic)) {
|
||||||
'seta|setnbe|setae|setnb|setnc|setbe|setna|setb|setc|setnae|sete|setz|setne|setnz|setg|setnle|setge|setnl|setle|setng|setl|setnge|seto|setno|sets|setns|' +
|
|
||||||
'call|ret|jmp|jnz|jg|jl|jge|jle|int|jz|js|jns|brk|not|jc|jnc|ror|rol|sal|sar|jo|jno|inc|dec|rcl|xchg|rcr|pushf|popf|ja|jna)\\b').test(mnemonic.toLowerCase())) {
|
|
||||||
|
|
||||||
|
|
||||||
if (line.indexOf(",") !== -1) {
|
if (line.indexOf(",") !== -1) {
|
||||||
//2 Operands
|
//2 Operands
|
||||||
var strO1 = line.substring(line.indexOf(mnemonic) + mnemonic.length, line.indexOf(','));
|
if (!isDoubleOpMnemonic(mnemonic)) {
|
||||||
var strO2 = line.substring(line.indexOf(',') + 1).trim();
|
|
||||||
|
|
||||||
|
|
||||||
//Validate operand number
|
|
||||||
if (!new RegExp('\\b(?:mov|add|sub|and|or|test|cmp|shl|shr|xor|rol|ror|sal|sar|rcl|xchg|rcr)\\b').test(mnemonic.toLowerCase())) {
|
|
||||||
produceError(result, currentLine, mnemonic + " instruction with 2 operands is illegal");
|
produceError(result, currentLine, mnemonic + " instruction with 2 operands is illegal");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var strO1 = line.substring(line.indexOf(mnemonic) + mnemonic.length, line.indexOf(','));
|
||||||
|
var strO2 = line.substring(line.indexOf(',') + 1).trim();
|
||||||
|
|
||||||
//Validate operand type
|
//Validate operand type
|
||||||
var o1Type = getOperandType(strO1, result);
|
var o1Type = getOperandType(strO1, result);
|
||||||
var o2Type = getOperandType(strO2, result);
|
var o2Type = getOperandType(strO2, result);
|
||||||
@ -320,14 +335,13 @@ function parseInstruction(line, result, currentLine) {
|
|||||||
|
|
||||||
} else if (tokens.length > 1) {
|
} else if (tokens.length > 1) {
|
||||||
//1 Operand
|
//1 Operand
|
||||||
strO1 = line.substring(line.indexOf(mnemonic) + mnemonic.length).trim();
|
if (!isSingleOpMnemonic(mnemonic)) {
|
||||||
|
|
||||||
//Validate operand number
|
|
||||||
if (!new RegExp('\\b(?:push|mul|pop|div|neg|call|jnz|jg|jl|jge|jle|hwi|hwq|jz|js|jns|ret|jmp|not|jc|jnc|jo|jno|inc|dec|ja|jna|seta|setnbe|setae|setnb|setnc|setbe|setna|setb|setc|setnae|sete|setz|setne|setnz|setg|setnle|setge|setnl|setle|setng|setl|setnge|seto|setno|sets|setns)\\b').test(mnemonic.toLowerCase())) {
|
|
||||||
produceError(result, currentLine, mnemonic + " instruction with 1 operand is illegal");
|
produceError(result, currentLine, mnemonic + " instruction with 1 operand is illegal");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
strO1 = line.substring(line.indexOf(mnemonic) + mnemonic.length).trim();
|
||||||
|
|
||||||
//Validate operand type
|
//Validate operand type
|
||||||
if (getOperandType(strO1, result) === OPERAND_INVALID) {
|
if (getOperandType(strO1, result) === OPERAND_INVALID) {
|
||||||
produceError(result, currentLine, "Invalid operand: " + strO1);
|
produceError(result, currentLine, "Invalid operand: " + strO1);
|
||||||
@ -340,10 +354,8 @@ function parseInstruction(line, result, currentLine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//No operand
|
//No Operand
|
||||||
if (!new RegExp('\\b(?:ret|brk|nop|pushf|popf)\\b').test(mnemonic.toLowerCase())) {
|
if (!isZeroOpMnemonic(mnemonic)) {
|
||||||
|
|
||||||
//Validate operand number
|
|
||||||
produceError(result, currentLine, mnemonic + " instruction with no operand is illegal");
|
produceError(result, currentLine, mnemonic + " instruction with no operand is illegal");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user