0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Koonetz
  • 53
  • 6
  • Is it not irrelevant whether the **compiler** of the warrior-program is optimized? – Bernd Elkemann Mar 21 '14 at 12:52
  • to avoid code-duplication: instead of writing an `else if` for every entry in the op-table you could make an inner loop that goes through the entries in the op-table and compares them to the current pointer-pos. – Bernd Elkemann Mar 21 '14 at 12:55
  • There are certain things that will substract points from the final grade, it happens very often that students get heavily penalized for having "ugly, unoptimized code". EDIT: I see what you mean, I'll try that out. – Koonetz Mar 21 '14 at 12:56
  • 1
    woop is never initialized, if woop.mnemonique[0] == '\0', your strncmp will always return true. – user26347 Mar 21 '14 at 12:58
  • I guess it's not relevent but the `op_t` you use isn't the one filled with data you show. Epitech's corewar, the memories... – Yabada Mar 21 '14 at 12:59
  • hmm, what do you mean by it isn't filled with the data I show? Can't really see it. And yea, boy am I struggling with the project. – Koonetz Mar 21 '14 at 13:03
  • Look where you are declaring `woop` -- you don't assign it any value. The `op_t` part only tells it what it is, not what value it has. Even if you change its type to *pointer* and set its value to one of the op table items, you are only testing **one** command. Ditch the Woop and simply loop over the table items directly. – Jongware Mar 21 '14 at 13:49

1 Answers1

0

IMHO what is touchy there is that C does not consider strings as a "real" type, this is why you're ending up with waterfall else if with strcmp.

I'm not sure it is optimized but one trick comes to my mind: you could (carefully) transform your strings into integers (double-check duplicates and side-effects like special chars \n, \t, \0, ...): by doing so, you will be able to replace your multiples if/else by a beautiful switch case. See this post

Community
  • 1
  • 1
n0p
  • 3,399
  • 2
  • 29
  • 50
  • This doesn't look like much of an answer to me – david.pfx Mar 21 '14 at 13:14
  • Well it is an alternative to the `if/else` solution, probably not the best one but that was the question wasn't it ? – n0p Mar 21 '14 at 13:42
  • Not really. The underlying question was: what the hell should I do with this table the teacher gave me? Is there a way to use to to make really nice code? Your trick works and gives OP a reason not to use the table. Still, if you get a tick I guess that's all that matters. – david.pfx Mar 21 '14 at 23:52