Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. #define LENGTH 100
  6. #define MAXCHAR 1000
  7. #define YES_NULL 2355
  8.  
  9. struct CodeLine
  10. {
  11.     char* label;
  12.     char* operation;
  13.     char* operand1;
  14.     char* operand2;
  15.     char* comment;
  16.  
  17. } * mCodeLine;
  18. struct CodeLine* initCodeLines(struct CodeLine* cl)
  19. {
  20.     cl = (struct CodeLine*)calloc(LENGTH, sizeof(struct CodeLine));
  21.     int i = 0;
  22.     for (; i < LENGTH; i++)
  23.     {
  24.         cl[i].label = (char*)calloc(LENGTH, sizeof(char));
  25.         cl[i].operation = (char*)calloc(LENGTH, sizeof(char));
  26.         cl[i].operand1 = (char*)calloc(LENGTH, sizeof(char));
  27.         cl[i].operand2 = (char*)calloc(LENGTH, sizeof(char));
  28.         cl[i].comment = (char*)calloc(LENGTH, sizeof(char));
  29.     }
  30.     return cl;
  31. }
  32. struct CodeLine parsing(struct CodeLine cl, int comment_start, int label_end, char* string)
  33. {
  34.     int step = 0;
  35.     int left = label_end;
  36.     int rigth = comment_start;
  37.     int cursor = 0;
  38.     for (; left < rigth; left++)
  39.     {
  40.         int firtsOperand = 0;
  41.         int secondOperand = 0;
  42.  
  43.         if (string[left] != ' ' && step == 0)
  44.         {
  45.             cursor = left;
  46.             //                        mCodeLine[i].operation =
  47.             //                          (char *) calloc (LENGTH, sizeof (char));
  48.             while (string[cursor] != ' ' && cursor < rigth)
  49.             {
  50.                 cl.operation[firtsOperand++] = string[cursor++];
  51.             }
  52.             left = cursor;
  53.             step++;
  54.         }
  55.         if (string[left] != ' ' && step == 1)
  56.         {
  57.             cursor = left;
  58.             firtsOperand = 0;
  59.             secondOperand = 0;
  60.             //                        mCodeLine[i].operand1 =
  61.             //                          (char *) calloc (LENGTH, sizeof (char));
  62.             //                        mCodeLine[i].operand2 =
  63.             //                          (char *) calloc (LENGTH, sizeof (char));
  64.             while (string[cursor] != ' ' && cursor < rigth)
  65.             {
  66.                 if (string[cursor] == ',')
  67.                 {
  68.                     int isEmptyBeforeOperand = 0;
  69.                     while (string[cursor] != ' ' && cursor < rigth)
  70.                     {
  71.                         cursor++;
  72.                         while (string[cursor] == ' ' && isEmptyBeforeOperand == 0)
  73.                         {
  74.                             cursor++;
  75.                         }
  76.                         cl.operand2[secondOperand++] = string[cursor];
  77.                         isEmptyBeforeOperand++;
  78.                     }
  79.                 }
  80.                 cl.operand1[firtsOperand++] = string[cursor++];
  81.             }
  82.             step++;
  83.             left = cursor;
  84.         }
  85.  
  86.         if (string[left] != ' ' && step == 2)
  87.         {
  88.             cursor = left;
  89.             secondOperand = 0;
  90.             //                        mCodeLine[i].operand2 =
  91.             //                          (char *) calloc (LENGTH, sizeof (char));
  92.             while (string[cursor] != ' ' && cursor < rigth)
  93.             {
  94.                 if (string[cursor] != ',')
  95.                     cl.operand2[secondOperand++] = string[cursor++];
  96.                 else
  97.                     cursor++;
  98.             }
  99.             left = cursor;
  100.         }
  101.     }
  102.     return cl;
  103. }
  104. int main()
  105. {
  106.     FILE* file = fopen("code.txt", "r");
  107.  
  108.     if (file == NULL)
  109.     {
  110.         printf(" error");
  111.     }
  112.     else
  113.     {
  114.         char* str = (char*)calloc(LENGTH, sizeof(char));
  115.         char symb;
  116.         int line = 0;
  117.         int end = 0;
  118.         mCodeLine = initCodeLines(mCodeLine);
  119.         while (symb = fgetc(file))
  120.         {
  121.             if (symb != '\n' && symb != EOF)
  122.             {
  123.                 str[end++] = symb;
  124.             }
  125.             else
  126.             {
  127.                 int comment_start = YES_NULL;
  128.                 int label_end = YES_NULL;
  129.  
  130.                 int current;
  131.                 for (current = 0; current < end; current++)
  132.                 {
  133.                     if (str[current] == ';')
  134.                     {
  135.                         comment_start = current;
  136.                         //                    mCodeLine[i].comment =
  137.                         //                      (char *) calloc (LENGTH, sizeof (char));
  138.                         int cursor;
  139.                         int firstComment = 0;
  140.                         for (cursor = ++current; cursor < end; cursor++, firstComment++)
  141.                         {
  142.                             mCodeLine[line].comment[firstComment] = str[cursor];
  143.                         }
  144.                     }
  145.                     if (str[current] == ':')
  146.                     {
  147.                         label_end = current;
  148.                         //                    mCodeLine[i].label =
  149.                         //                      (char *) calloc (LENGTH, sizeof (char));
  150.                         int cursor = 0;
  151.                         int firstLabel = 0;
  152.                         for (; cursor < current; cursor++)
  153.                         {
  154.                             if (str[cursor] != ' ')
  155.                             {
  156.                                 mCodeLine[line].label[firstLabel++] = str[cursor];
  157.                             }
  158.                         }
  159.                     }
  160.                 }
  161.  
  162.                 if (comment_start == YES_NULL && label_end == YES_NULL)
  163.                 {
  164.                     mCodeLine[line] = parsing(mCodeLine[line], end, 0, str);
  165.                 }
  166.  
  167.                 if (comment_start == YES_NULL && label_end != YES_NULL)
  168.                 {
  169.                     mCodeLine[line] = parsing(mCodeLine[line], end, ++label_end, str);
  170.                 }
  171.                 //
  172.  
  173.                 if (comment_start != YES_NULL && label_end != YES_NULL)
  174.                 {
  175.                     mCodeLine[line] = parsing(mCodeLine[line], comment_start, ++label_end, str);
  176.                 }
  177.  
  178.  
  179.                 if (comment_start != YES_NULL && label_end == YES_NULL)
  180.                 {
  181.                     mCodeLine[line] = parsing(mCodeLine[line], comment_start, 0, str);
  182.                 }
  183.                 str = (char*)calloc(LENGTH, sizeof(char));
  184.                 end = 0;
  185.                 line++;
  186.             }
  187.  
  188.             if (symb == EOF)
  189.             {
  190.                 break;
  191.             }
  192.         }
  193.         int one_line;
  194.         for (one_line = 0; one_line < line; one_line++)
  195.         {
  196.             printf("label: %-10s ", mCodeLine[one_line].label);
  197.             printf("operation: %-10s ", mCodeLine[one_line].operation);
  198.             printf("operand1: %-10s ", mCodeLine[one_line].operand1);
  199.             printf("operand2: %-10s ", mCodeLine[one_line].operand2);
  200.             printf("comment: %-20s ", mCodeLine[one_line].comment);
  201.             printf("\n");
  202.         }
  203.     }
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement