Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define LENGTH 100
- #define MAXCHAR 1000
- #define YES_NULL 2355
- struct CodeLine
- {
- char* label;
- char* operation;
- char* operand1;
- char* operand2;
- char* comment;
- } * mCodeLine;
- struct CodeLine* initCodeLines(struct CodeLine* cl)
- {
- cl = (struct CodeLine*)calloc(LENGTH, sizeof(struct CodeLine));
- int i = 0;
- for (; i < LENGTH; i++)
- {
- cl[i].label = (char*)calloc(LENGTH, sizeof(char));
- cl[i].operation = (char*)calloc(LENGTH, sizeof(char));
- cl[i].operand1 = (char*)calloc(LENGTH, sizeof(char));
- cl[i].operand2 = (char*)calloc(LENGTH, sizeof(char));
- cl[i].comment = (char*)calloc(LENGTH, sizeof(char));
- }
- return cl;
- }
- struct CodeLine parsing(struct CodeLine cl, int comment_start, int label_end, char* string)
- {
- int step = 0;
- int left = label_end;
- int rigth = comment_start;
- int cursor = 0;
- for (; left < rigth; left++)
- {
- int firtsOperand = 0;
- int secondOperand = 0;
- if (string[left] != ' ' && step == 0)
- {
- cursor = left;
- // mCodeLine[i].operation =
- // (char *) calloc (LENGTH, sizeof (char));
- while (string[cursor] != ' ' && cursor < rigth)
- {
- cl.operation[firtsOperand++] = string[cursor++];
- }
- left = cursor;
- step++;
- }
- if (string[left] != ' ' && step == 1)
- {
- cursor = left;
- firtsOperand = 0;
- secondOperand = 0;
- // mCodeLine[i].operand1 =
- // (char *) calloc (LENGTH, sizeof (char));
- // mCodeLine[i].operand2 =
- // (char *) calloc (LENGTH, sizeof (char));
- while (string[cursor] != ' ' && cursor < rigth)
- {
- if (string[cursor] == ',')
- {
- int isEmptyBeforeOperand = 0;
- while (string[cursor] != ' ' && cursor < rigth)
- {
- cursor++;
- while (string[cursor] == ' ' && isEmptyBeforeOperand == 0)
- {
- cursor++;
- }
- cl.operand2[secondOperand++] = string[cursor];
- isEmptyBeforeOperand++;
- }
- }
- cl.operand1[firtsOperand++] = string[cursor++];
- }
- step++;
- left = cursor;
- }
- if (string[left] != ' ' && step == 2)
- {
- cursor = left;
- secondOperand = 0;
- // mCodeLine[i].operand2 =
- // (char *) calloc (LENGTH, sizeof (char));
- while (string[cursor] != ' ' && cursor < rigth)
- {
- if (string[cursor] != ',')
- cl.operand2[secondOperand++] = string[cursor++];
- else
- cursor++;
- }
- left = cursor;
- }
- }
- return cl;
- }
- int main()
- {
- FILE* file = fopen("code.txt", "r");
- if (file == NULL)
- {
- printf(" error");
- }
- else
- {
- char* str = (char*)calloc(LENGTH, sizeof(char));
- char symb;
- int line = 0;
- int end = 0;
- mCodeLine = initCodeLines(mCodeLine);
- while (symb = fgetc(file))
- {
- if (symb != '\n' && symb != EOF)
- {
- str[end++] = symb;
- }
- else
- {
- int comment_start = YES_NULL;
- int label_end = YES_NULL;
- int current;
- for (current = 0; current < end; current++)
- {
- if (str[current] == ';')
- {
- comment_start = current;
- // mCodeLine[i].comment =
- // (char *) calloc (LENGTH, sizeof (char));
- int cursor;
- int firstComment = 0;
- for (cursor = ++current; cursor < end; cursor++, firstComment++)
- {
- mCodeLine[line].comment[firstComment] = str[cursor];
- }
- }
- if (str[current] == ':')
- {
- label_end = current;
- // mCodeLine[i].label =
- // (char *) calloc (LENGTH, sizeof (char));
- int cursor = 0;
- int firstLabel = 0;
- for (; cursor < current; cursor++)
- {
- if (str[cursor] != ' ')
- {
- mCodeLine[line].label[firstLabel++] = str[cursor];
- }
- }
- }
- }
- if (comment_start == YES_NULL && label_end == YES_NULL)
- {
- mCodeLine[line] = parsing(mCodeLine[line], end, 0, str);
- }
- if (comment_start == YES_NULL && label_end != YES_NULL)
- {
- mCodeLine[line] = parsing(mCodeLine[line], end, ++label_end, str);
- }
- //
- if (comment_start != YES_NULL && label_end != YES_NULL)
- {
- mCodeLine[line] = parsing(mCodeLine[line], comment_start, ++label_end, str);
- }
- if (comment_start != YES_NULL && label_end == YES_NULL)
- {
- mCodeLine[line] = parsing(mCodeLine[line], comment_start, 0, str);
- }
- str = (char*)calloc(LENGTH, sizeof(char));
- end = 0;
- line++;
- }
- if (symb == EOF)
- {
- break;
- }
- }
- int one_line;
- for (one_line = 0; one_line < line; one_line++)
- {
- printf("label: %-10s ", mCodeLine[one_line].label);
- printf("operation: %-10s ", mCodeLine[one_line].operation);
- printf("operand1: %-10s ", mCodeLine[one_line].operand1);
- printf("operand2: %-10s ", mCodeLine[one_line].operand2);
- printf("comment: %-20s ", mCodeLine[one_line].comment);
- printf("\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement