Add tokeniser.c to make parsing easier
This commit is contained in:
parent
6ddf18be96
commit
38e5cd06fa
3
src/add_imm_sh.s
Normal file
3
src/add_imm_sh.s
Normal file
@ -0,0 +1,3 @@
|
||||
add x0, x0, #1, lsl #12
|
||||
|
||||
and x0, x0, x0
|
||||
103
src/tokeniser.c
Normal file
103
src/tokeniser.c
Normal file
@ -0,0 +1,103 @@
|
||||
// Tokeniser.c
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MAX_TOKEN_COUNT 5
|
||||
#define MAX_OPERAND_COUNT 4
|
||||
#define OPERAND_DELIMITER ", "
|
||||
|
||||
char **tokenise(char *line, int *numTokens) {
|
||||
char **tokens = malloc(MAX_TOKEN_COUNT * sizeof(char *));\
|
||||
if (!tokens) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
*numTokens = 0;
|
||||
char *token = strtok(line, " ");
|
||||
assert(token != NULL);
|
||||
|
||||
tokens[(*numTokens)++] = token;
|
||||
|
||||
char *operandStart = strtok(NULL, "");
|
||||
assert(operandStart != NULL);
|
||||
|
||||
bool inBracket = false;
|
||||
char *currentToken = operandStart;
|
||||
|
||||
for (char *c = operandStart; *c != '\0'; ++c) {
|
||||
if (*c == '[' || *c == '{') {
|
||||
inBracket = true;
|
||||
} else if (*c == ']' || *c == '}') {
|
||||
inBracket = false;
|
||||
}
|
||||
|
||||
|
||||
if (*c == ',' && !inBracket) {
|
||||
*c = '\0';
|
||||
tokens[(*numTokens)++] = currentToken;
|
||||
currentToken = c + 2; // Skip ", "
|
||||
}
|
||||
}
|
||||
|
||||
if (*currentToken != '\0') {
|
||||
tokens[*numTokens] = currentToken;
|
||||
|
||||
if (tokens[*numTokens][strlen(tokens[*numTokens]) - 1] == '\n') {
|
||||
tokens[*numTokens][strlen(tokens[*numTokens]) - 1] = '\0';
|
||||
}
|
||||
|
||||
(*numTokens)++;
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
char **tokeniseOperands(char *line, int *numTokens) {
|
||||
char **tokens = malloc(MAX_OPERAND_COUNT * sizeof(char *));
|
||||
if (!tokens) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (*line == '[') {
|
||||
line++; // skip '['
|
||||
line[strlen(line) - 1] = '\0'; // remove ']'
|
||||
} else if (*line == '{') {
|
||||
line++; // skip '{'
|
||||
line[strlen(line) - 1] = '\0'; // remove '}'
|
||||
}
|
||||
|
||||
*numTokens = 0;
|
||||
bool inBracket = false;
|
||||
char *currentToken = line;
|
||||
|
||||
for (char *c = line; *c != '\0'; ++c) {
|
||||
if (*c == '[' || *c == '{') {
|
||||
inBracket = true;
|
||||
} else if (*c == ']' || *c == '}') {
|
||||
inBracket = false;
|
||||
}
|
||||
|
||||
if (*c == ',' && !inBracket) {
|
||||
*c = '\0';
|
||||
tokens[(*numTokens)++] = currentToken;
|
||||
currentToken = c + 2; // Skip ", "
|
||||
}
|
||||
}
|
||||
|
||||
if (*currentToken != '\0') {
|
||||
tokens[*numTokens] = currentToken;
|
||||
|
||||
if (tokens[*numTokens][strlen(tokens[*numTokens]) - 1] == '\n') {
|
||||
tokens[*numTokens][strlen(tokens[*numTokens]) - 1] = '\0';
|
||||
}
|
||||
|
||||
(*numTokens)++;
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user