diff --git a/a.out b/a.out new file mode 100644 index 0000000..e69de29 diff --git a/src/codegen.c b/src/codegen.c new file mode 100644 index 0000000..e69de29 diff --git a/src/codegen.h b/src/codegen.h new file mode 100644 index 0000000..e69de29 diff --git a/src/runner.c b/src/runner.c index 0ccb74f..5833e3e 100644 --- a/src/runner.c +++ b/src/runner.c @@ -108,6 +108,26 @@ int run(FILE *alpha) { fprintf(stderr, "INPUT FILE NOT FOUND\n"); return -1; } + + char *line; + int i = 1; + while ((line = file_read_line(alpha)) != NULL) { + CodeLine *code_line = malloc(sizeof(CodeLine)); + code_line->line_number = i; + code_line->line = malloc(strlen(line) + 1); + strcpy(code_line->line, line); + code_line->next = NULL; + code_line->is_error = false; + append_code_line(code_line); + free(line); + i++; + } + + fseek(alpha, 0, SEEK_SET); + + + // print_code_lines(); + yyin = alpha; yyparse(); @@ -225,3 +245,61 @@ int is_alpha_file(char *alpha, int file_len) { } return 0; // is alpha file } + +void insert_code_line(char *line, int line_number) { +} + +void append_code_line(CodeLine *code_line) { + if (code_line == NULL) return; + + if (code_head == NULL) { + code_head = code_line; + } else { + CodeLine *current = code_head; + while (current->next != NULL) { + current = current->next; + } + current->next = code_line; + } +} + +void print_code_lines() { + CodeLine *current = code_head; + while (current != NULL) { + printf("%d %03d: %s",current->line_number, current->line); + current = current->next; + } +} + +char *file_read_line(FILE *fp) { + if (fp == NULL) return NULL; + + size_t size = 128; + size_t len = 0; + char *str = malloc(size); + if (!str) return NULL; + + int c; + while ((c = fgetc(fp)) != EOF) { + if (len + 1 >= size) { + size *= 2; + char *new_buffer = realloc(str, size); + if (!new_buffer) { + free(str); + return NULL; + } + str = new_buffer; + } + + str[len++] = (char)c; + if (c == '\n') break; + } + + if (len == 0 && c == EOF) { + free(str); + return NULL; + } + + str[len] = '\0'; + return str; +} \ No newline at end of file diff --git a/src/runner.h b/src/runner.h index 16b505b..0573255 100644 --- a/src/runner.h +++ b/src/runner.h @@ -91,3 +91,17 @@ char *COLOR_LIGHTBLUE = "\033[1;34m"; char *COLOR_LIGHTPURPLE = "\033[1;35m"; char *COLOR_LIGHTCYAN = "\033[1;36m"; char *COLOR_WHITE = "\033[1;37m"; + +typedef struct CodeLine { + char *line; + int line_number; + bool is_error; + struct CodeLine *next; +} CodeLine; + +CodeLine *code_head; + +char *file_read_line(FILE *fp); +void insert_code_line(char *line, int line_number); +void append_code_line(CodeLine *code_line); +void print_code_lines(); \ No newline at end of file