Skip to content
Snippets Groups Projects
Commit a6ef581f authored by Vishnu Menon's avatar Vishnu Menon :bulb:
Browse files

basic json parsing + error handling

parent 3a82cb92
Branches
No related tags found
No related merge requests found
......@@ -6,36 +6,37 @@
// my everlasting commitment to ridding code of magic numbers :)
#define MEMORY_SIZE 65535
#define BUFFER_SIZE 32767
#define MAX_TOKEN_LENGTH 32767
int memory[MEMORY_SIZE];
// no idea what these are yet, when i figure that out i'll rename them something better
int sp, fp = MEMORY_SIZE;
int img, but, lab, tab = 0; // renaming these too
int sp, fp = MEMORY_SIZE; /* no idea what these are yet, when i figure that out i'll rename them something better */
int img, but, lab, tab = 0; /* renaming these too */
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Error: Did not specify file.");
return 1;
return EXIT_FAILURE;
} else if (argc > 2) {
printf("Error: Too many arguments!");
return 1;
return EXIT_FAILURE;
}
FILE *executable = fopen(argv[1], "r");
if (executable == NULL) {
printf("Error: Could not locate %s", argv[1]);
return 1;
return EXIT_FAILURE;
}
char buffer[BUFFER_SIZE];
while(fgets(buffer, BUFFER_SIZE-1, executable) != NULL) {
char instruction_set[BUFFER_SIZE];
jsmn_parser parser;
jsmntok_t tokens[MAX_TOKEN_LENGTH];
// cleanup for easier jsmm parsing
......@@ -54,8 +55,20 @@ int main(int argc, char* argv[]) {
}
}
jsmn_init(&parser);
int result = jsmn_parse(&parser, instruction_set, strlen(instruction_set), tokens, sizeof(tokens) / sizeof(tokens[0]));
// debug line
printf("%s\n", instruction_set);
if (result < 1 || tokens[0].type != JSMN_ARRAY) {
printf("Error: Bad JSON, is root token not an array?");
return EXIT_FAILURE;
}
}
return 0;
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment