I have been trying to find ways to optimize my code, there's one problem though, the only way I can think of doing it is using a table given by our teacher. I tried manipulating but I couldn't get it to work as it should.
About the project, my group and I have to make a compiler that will transform a .s file into a .cor that will be used for a corewar. I'm in charge of detecting the commands and dealing with bad syntax. My program detects all of the different commands you can do. This is what I got for now.
int reading_file(char *pepper)
{
char *bffr;
int k;
k = 0;
while ((bffr = get_next_line(0)))
{
while (bffr[k] == '\t')
k++;
my_putstr(bffr);
my_putchar('\n');
if ((strncmp((bffr + k), "ld", my_strlen("ld"))) == 0)
my_putstr("it works! \n");
else if (strncmp((bffr + k), "zjmp", my_strlen("zjmp")) == 0)
my_putstr("it works!\n");
else if (strncmp((bffr + k), "sti", my_strlen("sti")) == 0)
my_putstr("it works! \n");
else if (strncmp((bffr + k), "live", my_strlen("live")) == 0)
my_putstr("it works! \n");
else if (strncmp((bffr + k), "st", my_strlen("st")) == 0)
my_putstr("it works!");
/* all of the other commands go after this */
k = 0;
}
}
Now, this gives no problems, but it just doesn't look like it would be very optimized, as I said before, I tried using a table that was given by our teacher, it has all the information needed for each command. I understand what each part of the table means but I have absolutely no idea how to manipulate it.
Here's the table and the structure related to it:
Structure:
struct op_s
{
char *mnemonique;
char nbr_args;
args_type_t type[MAX_ARGS_NUMBER];
char code;
int nbr_cycles;
char *comment;
};
typedef struct op_s op_t;
Table:
#include "op.h"
op_t op_tab[] =
{
{"live", 1, {T_DIR}, 1, 10, "alive"},
{"ld", 2, {T_DIR | T_IND, T_REG}, 2, 5, "load"},
{"st", 2, {T_REG, T_IND | T_REG}, 3, 5, "store"},
{"add", 3, {T_REG, T_REG, T_REG}, 4, 10, "addition"},
{"sub", 3, {T_REG, T_REG, T_REG}, 5, 10, "soustraction"},
{"and", 3, {T_REG | T_DIR | T_IND, T_REG | T_IND | T_DIR, T_REG}, 6, 6,
"et (and r1, r2, r3 r1&r2 -> r3"},
{"or", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 7, 6,
"ou (or r1, r2, r3 r1 | r2 -> r3"},
{"xor", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 8, 6,
"ou (xor r1, r2, r3 r1^r2 -> r3"},
{"zjmp", 1, {T_DIR}, 9, 20, "jump if zero"},
{"ldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG, T_REG}, 10, 25,
"load index"},
{"sti", 3, {T_REG, T_REG | T_DIR | T_IND, T_DIR | T_REG}, 11, 25,
"store index"},
{"fork", 1, {T_DIR}, 12, 800, "fork"},
{"lld", 2, {T_DIR | T_IND, T_REG}, 13, 10, "long load"},
{"lldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG, T_REG}, 14, 50,
"long load index"},
{"lfork", 1, {T_DIR}, 15, 1000, "long fork"},
{"aff", 1, {T_REG}, 16, 2, "aff"},
{0, 0, {0}, 0, 0, 0}
};
Now, I tried doing something just a while ago but it just didn't work as expected:
int reading_file(char *pepper)
{
char *bffr;
int k;
op_t woop;
k = 0;
while ((bffr = get_next_line(0)))
{
while (bffr[k] == '\t')
k++;
my_putstr(bffr);
my_putchar('\n');
if ((strncmp((bffr + k), woop.mnemonique, my_strlen(woop.mnemonique))) == 0)
my_putstr("it works!\n");
k = 0;
}
}
This code will show "it works!" all over the place. Which it isn't what I'm looking for.
In short, are there ways to do this other than doing a wall of if and else if condition? Is it possible to do it by manipulating the table that I was given?