/media/sda-magnetic/david/Dok-15-2023-11-27/fernuni-hagen/cs-i-ii/old-cs-2-03/pascal-compiler/siac4t/virtualmachine03.txt


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N_LINES             256
#define LINE_MAX_CH         128
#define N_VAR_ABC            26
#define N_VAR_X            1024
#define N_ATOM               20
#define ATOM_MAX_CH          16

char code[N_LINES][LINE_MAX_CH];
int var_abc[N_VAR_ABC];
int var_x[N_VAR_X];

char atom[N_ATOM][ATOM_MAX_CH];

int get_atom_token(char *d, char *s) {
    char *p;
    
    p = s;
    while ((*s != ' ' ) && (*s != 0) && (*s != ',') && (*s != '\n')) {
        *d = *s;
        s++;
        d++;
    }
    *d = 0;
    while ((*s == ',') || (*s == ' ') || (*s == '\n')) s++;
return (s-p);
}

int get_atoms(int line) {
    int i;
    int length;
    int length2;
    
    length = 0;
    for (i = 0;  ; i++) {
        if((length2 = get_atom_token(atom[i], code[line] + length)) == 0)
            break;
        length += length2;
    }
    

    
return 9;
}

int get_index_of_x_var(char *s) {
    int i;
    
    s++;
    i = 0;
    
    while (*s != 0) {
        i += *s - '0';
        i *= 10;
        s++;
    }
    i /= 10;
    
return i;
}

int main(int argc, char *argv[]) {
    FILE *fp;
    int line_counter;
    int i;
    int op1, op2, op3;
    
    if((fp = fopen(argv[1], "r")) == NULL) {
        perror("Can't open file");
        exit(1);
    }
    
    line_counter = 0;
    while(!feof(fp)) {
        fgets(code[line_counter], LINE_MAX_CH, fp);
        line_counter++;
    }
    
    for (i = 0;  i < N_VAR_ABC;  i++)
        var_abc[i] = 0;
    for (i = 0;  i < N_VAR_X;  i++)
        var_x[i] = 0;
    
    for (i = 0;  i < line_counter;  i++) {
        get_atoms(i);
        if(strcmp(atom[0], "add") == 0) {
            if((atom[1][0] == 'x') && ((atom[1][1] >= '0' ) && (atom[1][1] <= '9'))) {
                op1 = get_index_of_x_var(atom[1]);
                if((atom[2][0] == 'x') && ((atom[2][1] >= '0' ) && (atom[2][1] <= '9'))) {
                    op2 = get_index_of_x_var(atom[2]);
                    if((atom[3][0] == 'x') && ((atom[3][1] >= '0' ) && (atom[3][1] <= '9'))) {
                        op3 = get_index_of_x_var(atom[3]);
                        printf("x%i x%i x%i\n", op1, op2, op3);
                    }
                    else if(((atom[3][0] >= 'a') && (atom[3][0] <= 'z')) && (atom [3][1] == 0)) {
                       op3 = atom[3][0] - 'a';
                       printf("x%i x%i a%i\n", op1, op2, op3);
                    }
                }
                else if(((atom[2][0] >= 'a') && (atom[2][0] <= 'z')) && (atom [2][1] == 0)) {
                    op2 = atom[2][0] - 'a';
                    if((atom[3][0] == 'x') && ((atom[3][1] >= '0' ) && (atom[3][1] <= '9'))) {
                        op3 = get_index_of_x_var(atom[3]);
                        printf("x%i a%i x%i\n", op1, op2, op3);
                    }
                    else if(((atom[3][0] >= 'a') && (atom[3][0] <= 'z')) && (atom [3][1] == 0)) {
                        op3 = atom[3][0] - 'a';
                        printf("x%i a%i a%i\n", op1, op2, op3);
                    }
                }
            }
            else if(((atom[1][0] >= 'a') && (atom[1][0] <= 'z')) && (atom [1][1] == 0)) {
                op1 = atom[1][0] - 'a';
                if((atom[2][0] == 'x') && ((atom[2][1] >= '0' ) && (atom[2][1] <= '9'))) {
                    op2 = get_index_of_x_var(atom[2]);
                    if((atom[3][0] == 'x') && ((atom[3][1] >= '0' ) && (atom[3][1] <= '9'))) {
                        op3 = get_index_of_x_var(atom[3]);
                        printf("a%i x%i x%i\n", op1, op2, op3);
                    }
                    else if(((atom[3][0] >= 'a') && (atom[3][0] <= 'z')) && (atom [3][1] == 0)) {
                       op3 = atom[3][0] - 'a';
                       printf("a%i x%i a%i\n", op1, op2, op3);
                    }
                }
                else if(((atom[2][0] >= 'a') && (atom[2][0] <= 'z')) && (atom [2][1] == 0)) {
                    op2 = atom[2][0] - 'a';
                    if((atom[3][0] == 'x') && ((atom[3][1] >= '0' ) && (atom[3][1] <= '9'))) {
                        op3 = get_index_of_x_var(atom[3]);
                        printf("a%i a%i x%i\n", op1, op2, op3);
                    }
                    else if(((atom[3][0] >= 'a') && (atom[3][0] <= 'z')) && (atom [3][1] == 0)) {
                        op3 = atom[3][0] - 'a';
                        printf("a%i a%i a%i\n", op1, op2, op3);
                    }
                }
            }
            atom[2];
            atom[3];
            
        }
    }
    
    
    fclose(fp);
    
return 0;
}