From fcb9ff2b96de6989e9a676e96753b1b1279acf67 Mon Sep 17 00:00:00 2001 From: Rasmus Luha Date: Sat, 22 Oct 2022 02:45:34 +0300 Subject: hw3 status --- Makefile | 6 +++- inc/mem.h | 1 + inc/stack.h | 7 ++++ src/bfi.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/main.c | 49 +++++---------------------- src/mem.c | 16 +++++---- src/stack.c | 5 --- test.txt | 1 + test/stack.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/stack.h | 18 ++++++++++ 10 files changed, 257 insertions(+), 54 deletions(-) create mode 100644 test.txt create mode 100644 test/stack.c create mode 100644 test/stack.h diff --git a/Makefile b/Makefile index 67ded2b..c3e3002 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS := -Iinc -g -Og -Wall +CFLAGS := -Iinc -g -Og -Wall #-DDEBUG # Lähtekood SRC := $(wildcard src/*.c) @@ -19,6 +19,10 @@ lib: obj: mkdir obj +.PHONY: run +run: lib/interpret + ./lib/interpret + .PHONY: clean clean: rm -r obj lib diff --git a/inc/mem.h b/inc/mem.h index 50a0a17..86bed3f 100644 --- a/inc/mem.h +++ b/inc/mem.h @@ -8,6 +8,7 @@ struct mem_st { int pos; }; +int mem_pos(); int mem_inc(); int mem_dec(); int mem_left(); diff --git a/inc/stack.h b/inc/stack.h index 60d41b8..fcc976a 100644 --- a/inc/stack.h +++ b/inc/stack.h @@ -1,6 +1,13 @@ #ifndef __STACK_H__ #define __STACK_H__ +#define STACK_SIZE 100 + +struct stack_st { + int len; + int arr[STACK_SIZE]; +}; + void stackPush(int val); int stackPop(void); int stackIsEmpty(void); diff --git a/src/bfi.c b/src/bfi.c index 0171ede..cc13b11 100644 --- a/src/bfi.c +++ b/src/bfi.c @@ -1,6 +1,104 @@ +#include +#include +#include "mem.h" +#include "stack.h" #include "bfi.h" +#ifdef DEBUG + #define PRINT_PARAMS "'%c' (kood = %d)\n", c, c +#else + #define PRINT_PARAMS "%c", c +#endif + void intepret(char *program){ - -} + int i = 0; + int length = 0; + while (program[i++] != '\0') + length++; + + i = 0; + while (i < length){ + //printf("i = %d\n",i); + //printf("%c\n", program[i]); + + switch(program[i]) { + + case BF_READ: { + /* Loeme märgi standardsisendist (kasutaja sisestab konsooli). */ + int c = getc(stdin); + if (EOF == c) { + /* Sisendi lõpu korral lõpetame interpretaatori töö. */ + printf("Sisendi lõpp!\n"); + return; + } + /* Lisame mällu loetud väärtuse. */ + mem_set((char) c); + break; + } + + + case BF_RIGHT: { + mem_right(); + break; + } + + + case BF_LEFT: { + mem_left(); + break; + } + + + case BF_INCREASE: { + mem_inc(); + break; + } + + case BF_DECREASE: { + mem_dec(); + break; + } + + + case BF_PRINT: { + char c = mem_get(); + printf(PRINT_PARAMS); + break; + } + + + case BF_START_LOOP: { + stackPush(i); + int memVal = mem_get(); + + //Skiping loop + if (memVal == 0){ + int helpCounter = 1; + while (helpCounter != 0){ + i++; + if (program[i] == '[') + helpCounter++; + if (program[i] == ']') + helpCounter--; + } + stackPop(); + } + break; + } + + + case BF_END_LOOP: { + if (mem_get() != 0){ + i = stackPop(); + stackPush(i); + } + else + stackPop(); + break; + } + + } // Switch end + i++; + } // While loop end +} diff --git a/src/main.c b/src/main.c index ec1de31..8f421f6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,45 +1,14 @@ #include +#include "bfi.h" +#include "mem.h" #include "stack.h" -int main() { - int x, y, z; +int main(int argc, char *argv[]) { + if (argc != 2){ + printf("Gotta Give one Argument\n"); + return 1; + } + intepret(argv[1]); - while (1) { - scanf("%d", &x); - - // 1. Kui x==0 siis print stack. - if (x == 0){ - printf("\nPrindin stacki\n"); - stackPrint(); - break; - } - - // 2. Kui empty Push. - else if (stackIsEmpty()) - stackPush(x); - - // 3. Kui x pos. ja peeked pos: push(x). - // 4. kui x pos. aga peeked neg., y=pop() ja push(x+y). - else if (x >= 0){ - y = stackPeek(); - if (y >= 0) - stackPush(x); - - else{ - z = stackPop(); - if (x+z != 0) - stackPush(x+z); - } - } - - // 5. Kui x on neg, siis pop() - else if (x < 0) { - z = stackPop(); - if (x+z != 0) { - stackPush(x+z); - } - } - - } // While loop End - return 0; + return 0; } diff --git a/src/mem.c b/src/mem.c index 3c605a5..798fa1b 100644 --- a/src/mem.c +++ b/src/mem.c @@ -1,7 +1,11 @@ #include #include "mem.h" -struct mem_st mem = { .pos = 0}; +struct mem_st mem = { .pos = 0, .arr={0}}; + +int mem_pos(){ + return mem.pos; +} int mem_inc(){ mem.arr[mem.pos]++; @@ -45,16 +49,16 @@ int mem_set(char v){ void mem_printDebug(){ if (mem.pos+9 >= MEM_SIZE) - printf("index: %d [%d .. %d]: ", mem.pos, mem.pos, mem.pos-MEM_SIZE+9); - printf("index: %d [%d .. %d]: ", mem.pos, mem.pos, mem.pos+9); + printf("index: %d mem [%d .. %d]:", mem.pos, mem.pos, mem.pos-MEM_SIZE+9); + printf("index: %d mem [%d .. %d]:", mem.pos, mem.pos, mem.pos+9); for (int i = 0; i < 10; ++i) { if (mem.pos+i >= MEM_SIZE) { - printf("%d ", mem.arr[mem.pos-MEM_SIZE+i]); + printf("% d", mem.arr[mem.pos-MEM_SIZE+i]); } else { - printf("%d ", mem.arr[mem.pos+i]); + printf("% d", mem.arr[mem.pos+i]); } } - + printf("\n"); } diff --git a/src/stack.c b/src/stack.c index 2fe3488..d29d1a7 100644 --- a/src/stack.c +++ b/src/stack.c @@ -3,11 +3,6 @@ #define STACK_SIZE 100 -struct stack_st { - int len; - int arr[STACK_SIZE]; -}; - struct stack_st stack = { .len = 0, .arr = {0}}; // Functions diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +Hello World! diff --git a/test/stack.c b/test/stack.c new file mode 100644 index 0000000..8d227f9 --- /dev/null +++ b/test/stack.c @@ -0,0 +1,106 @@ +#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 new file mode 100644 index 0000000..8bb1539 --- /dev/null +++ b/test/stack.h @@ -0,0 +1,18 @@ +#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