From 4bdad2d5c7dec0eb19c418cadb47cc3124092310 Mon Sep 17 00:00:00 2001 From: Rasmus Luha Date: Thu, 3 Nov 2022 01:08:28 +0200 Subject: praks IV done --- inc/mem.h | 9 +++++ inc/stack.h | 15 ++++---- inc/translator.h | 18 ++++++++++ src/bfi.c | 25 +++++++++---- src/mem.c | 35 ++++++++++++++++++ src/stack.c | 99 +++++++++++++++++++++++++++++++++++++-------------- src/translator.c | 0 test/stack.c | 106 ------------------------------------------------------- test/stack.h | 18 ---------- 9 files changed, 161 insertions(+), 164 deletions(-) create mode 100644 inc/translator.h create mode 100644 src/translator.c delete mode 100644 test/stack.c delete mode 100644 test/stack.h diff --git a/inc/mem.h b/inc/mem.h index 86bed3f..4ebdf61 100644 --- a/inc/mem.h +++ b/inc/mem.h @@ -9,12 +9,21 @@ struct mem_st { }; int mem_pos(); + int mem_inc(); int mem_dec(); + int mem_left(); int mem_right(); + int mem_get(); int mem_set(char v); + void mem_printDebug(); + +//Added +//int mem_add(int amout); +//int mem_move(int amout); + #endif diff --git a/inc/stack.h b/inc/stack.h index fcc976a..45f8daa 100644 --- a/inc/stack.h +++ b/inc/stack.h @@ -5,13 +5,16 @@ struct stack_st { int len; - int arr[STACK_SIZE]; + int size; + int* arr; }; -void stackPush(int val); -int stackPop(void); -int stackIsEmpty(void); -int stackPeek(void); -void stackPrint(void); +void stackPush(struct stack_st *s, int val); +int stackPop(struct stack_st *s); +int stackIsEmpty(struct stack_st *s); +int stackPeek(struct stack_st *s); +void stackPrint(struct stack_st *s); + +void stackFree(struct stack_st *s); #endif diff --git a/inc/translator.h b/inc/translator.h new file mode 100644 index 0000000..0007526 --- /dev/null +++ b/inc/translator.h @@ -0,0 +1,18 @@ +#ifndef _TRANSLATOR_H__ +#define _TRANSLATOR_H__ + +/* Selle struktuuriga esitame kõiki Brainfuck instruktsioone koos + lisainfoga, mis võimaldavad optimeeringuid. +*/ +struct BF_instruction_st { + + /* Instruktsiooni väljakutse funktsioon. Esimene parameeter on viit instruktsiooni + objektile teine on viit instruktsiooni indeksile, mida antud funktsioon + peab kas suurendama või muutma (näiteks tsüklites). + */ + + void (*run)(struct BF_instruction_st *instruction, int *index); +}; + + +#endif diff --git a/src/bfi.c b/src/bfi.c index cc13b11..03b32c1 100644 --- a/src/bfi.c +++ b/src/bfi.c @@ -11,6 +11,9 @@ #endif void intepret(char *program){ + + struct stack_st stack= { .len = 0, .size = 0, .arr = NULL}; + int i = 0; int length = 0; while (program[i++] != '\0') @@ -18,8 +21,6 @@ void intepret(char *program){ i = 0; while (i < length){ - //printf("i = %d\n",i); - //printf("%c\n", program[i]); switch(program[i]) { @@ -69,9 +70,14 @@ void intepret(char *program){ case BF_START_LOOP: { - stackPush(i); + stackPush(&stack, i); int memVal = mem_get(); + //printf("StartLoop! i = %d\n",i); + //printf("memVal: %d\n", mem_get()); + //printf("stackVal: %d\n", stackPeek(&stack)); + //printf("%c\n\n", program[i]); + //Skiping loop if (memVal == 0){ int helpCounter = 1; @@ -82,19 +88,24 @@ void intepret(char *program){ if (program[i] == ']') helpCounter--; } - stackPop(); + stackPop(&stack); } break; } case BF_END_LOOP: { + //printf("Endloop! i = %d\n",i); + //printf("memVal: %d\n", mem_get()); + //printf("stackVal: %d\n", stackPeek(&stack)); + //printf("%c\n\n", program[i]); + if (mem_get() != 0){ - i = stackPop(); - stackPush(i); + i = stackPop(&stack); + stackPush(&stack, i); } else - stackPop(); + stackPop(&stack); break; } diff --git a/src/mem.c b/src/mem.c index 798fa1b..9b43c9d 100644 --- a/src/mem.c +++ b/src/mem.c @@ -1,4 +1,5 @@ #include +#include #include "mem.h" struct mem_st mem = { .pos = 0, .arr={0}}; @@ -62,3 +63,37 @@ void mem_printDebug(){ } printf("\n"); } + + + +/* +// Praks 10-11 Added + +void mem_add(int amount){ + mem.arr[mem.pos] += amount; +} + +mem_int(){return mem_add(-1);}; +mem_dec(){return mem_add(1);}; + +int mem_move(int amount) { + int new_pos = mem.pos + amount; + if (amount < 0) { + //NB, siin võib olla probleem + new_pos += (MEM_SIZE -1); + } + else if (new_pos >= MEM_SIZE){ + new_pos %= MEM_SIZE; + } + mem.pos = new_pos; + return mem_pos; +} + +void mem_init() { + memset(mem.arr, 0, MEM_SIZE * sizeof(char)); + //for (int i = 0; i < MEM_SIZE; ++i) { + // mem.arr[i] = 0; + //} +} + +*/ diff --git a/src/stack.c b/src/stack.c index d29d1a7..afdf243 100644 --- a/src/stack.c +++ b/src/stack.c @@ -1,55 +1,100 @@ -#include "stack.h" #include +#include +#include "stack.h" -#define STACK_SIZE 100 +void stackPush(struct stack_st* s, int val){ -struct stack_st stack = { .len = 0, .arr = {0}}; + if (s->len == 0 || s->arr == NULL){ -// Functions + // Peame küsima mälu + s->size = 1; + //printf("yeet: %d\n", *(s->arr)); + s->arr = (int*) malloc(s->size * sizeof(int)); -void stackPush(int val){ - if (stack.len >= STACK_SIZE){ - printf("Stack overrflow\n"); - return; + // Kontrollime, et mälu allokeermisel ei tekinud probleeme + if (s->arr == NULL) { + printf("Mälu otsas\n"); + return; + } + s->arr[s->len++] = val; + + } // s->len == 0 end + + else if (s->len == s->size){ + int tmp_size = 2 * s->size; + int* tmp_arr = realloc(s->arr, tmp_size); + + // Kontrollime, kas saime mälu juurde + if (tmp_arr == NULL) { + printf("Mäluala suurendamine Erroris\n"); + return; + } + + s->size = tmp_size; + s->arr = tmp_arr; + + s->arr[s->len] = val; + s->len++; + return; + } + else { + s->arr[s->len++] = val; } - stack.arr[stack.len] = val; - stack.len++; - return; } -int stackPop(void){ - if (stackIsEmpty()){ - return 0; +int stackPop(struct stack_st *s){ + int x = s->arr[--s->len]; + int tmp_size = s->size / 2; + + // Kui vaja s-> mälu suurust kahandada + if (s->len < tmp_size){ + int* tmp_arr = realloc(s->arr, tmp_size); + + if (!tmp_arr){ + printf("No good, malu otsas"); + return 0; + } + + s->size = tmp_size; + s->arr = tmp_arr; } - int stackPopped = stack.arr[--stack.len]; - return stackPopped; + + if (s->len == 0) + stackFree(s); + + return x; +} + +void stackFree(struct stack_st *s){ + free(s->arr); + s->arr = NULL; } -int stackIsEmpty(void) { - return !(stack.len > 0); +int stackIsEmpty(struct stack_st *s) { + return !(s->len > 0); } -int stackPeek(){ - // To avoid funking up the stack.len - if (stackIsEmpty()) return 0; - else if (stack.len == 1) return stack.arr[1]; +int stackPeek(struct stack_st *s){ + // To avoid funking up the s->len + if (stackIsEmpty(s)) return 0; + else if (s->len == 1) return s->arr[1]; else{ - int x = stackPop(); - stackPush(x); + int x = stackPop(s); + stackPush(s, x); return x; } } -void stackPrint(){ +void stackPrint(struct stack_st *s){ while(1){ - if(stackIsEmpty()) + if(stackIsEmpty(s)) break; - int x = stackPop(); + int x = stackPop(s); printf("%d\n", x); } return; diff --git a/src/translator.c b/src/translator.c new file mode 100644 index 0000000..e69de29 diff --git a/test/stack.c b/test/stack.c deleted file mode 100644 index 8d227f9..0000000 --- a/test/stack.c +++ /dev/null @@ -1,106 +0,0 @@ -#include -#include "stack.h" - -struct stack_st stack = { .len = 0, //prageused el - .size = 0, // max el - .arr = NULL}; - -// Functions - -void stackPush(int val){ - if (stack.len == 0 || stack.arr == NULL){ - // Peame küsima mälu - stack.size = 1; - stack.arr = malloc(stack.size * sizeof(int)); - - if (stack.arr == NULL) { - printf("Mälu otsas\n"); - return; - } - stack.arr[stack.len++] = val; - } - - else if (stack.len == stack.size){ - int tmp_size = malloc(2 stack.size); - int* tmp_arr = realloc(stack.val, tmp_size * sizeof(int)); - - if (stack.arr == NULL) { - printf("Mälu otsas\n"); - return; - } - - stack.size = tmp_size; - stack.arr = tmp_arr; - stack.arr[stack.len] = val; - stack.len++; - return; - } - -} - - -int stackPop(void){ - // Mälu vähendamine - /* - return stack.arr[--stack.len]; - */ - // Peaks olema kogu värk. - - int x = stack.val[--stack.len] - int tmp_size = stack.size/2; - if (stack.len < tmp-size){ - int* tmp_arr = realloc(stack.arr, tmp_size *sizeof(int)); - if (!tmp_arr){ - printf("No good, malu ots"); - return 0; - } - stack.size = tmp_size; - stack.arr = tmp_arr; - - if (stack.len == 0) - stack_free(); -// End of new stuff // - - if (stackIsEmpty()){ - return 0; - } - int stackPopped = stack.arr[--stack.len]; - return stackPopped; -} - - -int stackIsEmpty(void) { - return !(stack.len > 0); -} - - -int stackPeek(){ - // To avoid funking up the stack.len - if (stackIsEmpty()) return 0; - else if (stack.len == 1) return stack.arr[1]; - else{ - int x = stackPop(); - stackPush(x); - return x; - } -} - - -void stackPrint(){ - while(1){ - if(stackIsEmpty()) - break; - - int x = stackPop(); - printf("%d\n", x); - } - return; -} - - -/* -stack_free(){ - free(stack.val); // kutsuda kõige lõpus vist - stack.arr = NULL; -} -*/ diff --git a/test/stack.h b/test/stack.h deleted file mode 100644 index 8bb1539..0000000 --- a/test/stack.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef __STACK_H__ -#define __STACK_H__ - -#define STACK_SIZE 100 - -struct stack_st { - int len; - int size; - int* arr; -}; - -void stackPush(int val); -int stackPop(void); -int stackIsEmpty(void); -int stackPeek(void); -void stackPrint(void); - -#endif -- cgit v1.2.3