diff --git a/src/fileaccess.c b/src/fileaccess.c index c425ee7..14bf70d 100644 --- a/src/fileaccess.c +++ b/src/fileaccess.c @@ -1,8 +1,6 @@ #include #include -#define MAX_ASM_LINE_LENGTH 100 - //validates inputted charlist as valid filename against expected extension int isValidFileFormat(char filename[], char expectedExtension[]){ int *pointLoc = strrchr(filename, '.'); @@ -45,7 +43,7 @@ int readAssemblyFile(char inputFile[]) { } FILE *fp; - char savedLine[MAX_ASM_LINE_LENGTH]; + char savedLine[sizeof(a64inst_instruction)]; fp = fopen(inputFile, "r"); diff --git a/src/parser.c b/src/parser.c index 1c56c8e..1d7c2cd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -14,6 +14,43 @@ // - count operands and match type/values // - generate final a64inst and return -a64inst_instruction parser(char asmLine[]){ +char *splitOperands(char* str, int operandCount, char *operands[]){ + char *operandsDupe = strdup(str); + int operandCount = 0; + char *operand = strtok(operandsDupe, OPERAND_DELIMITER); + operands[0] = operand; + + while (operand != NULL){ + operandCount++; + operand = strtok(NULL, OPERAND_DELIMITER); + operands[operandCount] = operand; + } + return(operands); + +} + +a64inst_instruction *tokeniser(char asmLine[]){ + a64inst_instruction *instr = malloc(sizeof(a64inst_instruction)); + if (instr == NULL){ + exit(EXIT_FAILURE); + } + + //"opcode operand1, {operand2}, ..." + char *stringptr = strdup(asmLine); + + char *opcode = strtok(stringptr, " "); + char *operands = strtok(NULL, ""); + + if(opcode[0]=="."){ + //type is directive + } else if(opcode[strlen(opcode)-1]==":") { + //type is label + } else { + //type is instruction + int operandCount = 0; + const char *operandList[4]; + splitOperands(operands, &operandCount, operandList); + } + } \ No newline at end of file diff --git a/src/parser.h b/src/parser.h index e69de29..5542aca 100644 --- a/src/parser.h +++ b/src/parser.h @@ -0,0 +1 @@ +#define OPERAND_DELIMITER ", " \ No newline at end of file