diff options
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | inc/mem.h | 1 | ||||
-rw-r--r-- | inc/stack.h | 7 | ||||
-rw-r--r-- | src/bfi.c | 102 | ||||
-rw-r--r-- | src/main.c | 49 | ||||
-rw-r--r-- | src/mem.c | 16 | ||||
-rw-r--r-- | src/stack.c | 5 | ||||
-rw-r--r-- | test.txt | 1 | ||||
-rw-r--r-- | test/stack.c | 106 | ||||
-rw-r--r-- | test/stack.h | 18 |
10 files changed, 257 insertions, 54 deletions
@@ -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 @@ -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); @@ -1,6 +1,104 @@ +#include <stdio.h> +#include <string.h> +#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 +} @@ -1,45 +1,14 @@ #include <stdio.h> +#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; } @@ -1,7 +1,11 @@ #include <stdio.h> #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 <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 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 |