diff options
author | Rasmus Luha <rasmus.luha@gmail.com> | 2022-11-03 01:08:28 +0200 |
---|---|---|
committer | Rasmus Luha <rasmus.luha@gmail.com> | 2022-11-03 01:08:28 +0200 |
commit | 4bdad2d5c7dec0eb19c418cadb47cc3124092310 (patch) | |
tree | c80d9cceb8e10e91daf38657cf64d8ce0bbf3104 | |
parent | fcb9ff2b96de6989e9a676e96753b1b1279acf67 (diff) |
-rw-r--r-- | inc/mem.h | 9 | ||||
-rw-r--r-- | inc/stack.h | 15 | ||||
-rw-r--r-- | inc/translator.h | 18 | ||||
-rw-r--r-- | src/bfi.c | 25 | ||||
-rw-r--r-- | src/mem.c | 35 | ||||
-rw-r--r-- | src/stack.c | 99 | ||||
-rw-r--r-- | src/translator.c | 0 | ||||
-rw-r--r-- | test/stack.c | 106 | ||||
-rw-r--r-- | test/stack.h | 18 |
9 files changed, 161 insertions, 164 deletions
@@ -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 @@ -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; } @@ -1,4 +1,5 @@ #include <stdio.h> +#include <string.h> #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 <stdio.h> +#include <stdlib.h> +#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 --- /dev/null +++ b/src/translator.c 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 <stdio.h> -#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 |