diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bfi.c | 6 | ||||
-rw-r--r-- | src/main.c | 45 | ||||
-rw-r--r-- | src/mem.c | 60 | ||||
-rw-r--r-- | src/stack.c | 61 |
4 files changed, 172 insertions, 0 deletions
diff --git a/src/bfi.c b/src/bfi.c new file mode 100644 index 0000000..0171ede --- /dev/null +++ b/src/bfi.c @@ -0,0 +1,6 @@ +#include "bfi.h" + +void intepret(char *program){ + +} + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..ec1de31 --- /dev/null +++ b/src/main.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include "stack.h" + +int main() { + int x, y, z; + + 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; +} diff --git a/src/mem.c b/src/mem.c new file mode 100644 index 0000000..3c605a5 --- /dev/null +++ b/src/mem.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include "mem.h" + +struct mem_st mem = { .pos = 0}; + +int mem_inc(){ + mem.arr[mem.pos]++; + return mem.arr[mem.pos]; +} + +int mem_dec(){ + mem.arr[mem.pos]--; + return mem.arr[mem.pos]; +} + +int mem_left(){ + if (mem.pos <= 0) { + mem.pos = MEM_SIZE-1; + } + else { + mem.pos--; + } + return mem.pos; +} + +int mem_right(){ + if (mem.pos >= MEM_SIZE-1) { + mem.pos = 0; + } + else { + mem.pos++; + } + return mem.pos; + +} + +int mem_get(){ + return mem.arr[mem.pos]; +} + +int mem_set(char v){ + mem.arr[mem.pos] = v; + return 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); + + for (int i = 0; i < 10; ++i) { + if (mem.pos+i >= MEM_SIZE) { + printf("%d ", mem.arr[mem.pos-MEM_SIZE+i]); + } + else { + printf("%d ", mem.arr[mem.pos+i]); + } + } + +} diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..2fe3488 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,61 @@ +#include "stack.h" +#include <stdio.h> + +#define STACK_SIZE 100 + +struct stack_st { + int len; + int arr[STACK_SIZE]; +}; + +struct stack_st stack = { .len = 0, .arr = {0}}; + +// Functions + +void stackPush(int val){ + if (stack.len >= STACK_SIZE){ + printf("Stack overrflow\n"); + return; + } + stack.arr[stack.len] = val; + stack.len++; + return; +} + + +int stackPop(void){ + 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; +} |