diff options
author | Rasmus Luha <rasmus.luha@gmail.com> | 2022-10-22 02:45:34 +0300 |
---|---|---|
committer | Rasmus Luha <rasmus.luha@gmail.com> | 2022-10-22 02:45:34 +0300 |
commit | fcb9ff2b96de6989e9a676e96753b1b1279acf67 (patch) | |
tree | e9a8839939aac6eeba3dea89ca0c20ad8268150e /src | |
parent | a97b362e6a35be4575f7ded3c31cdeede89ab2a8 (diff) |
hw3 status
Diffstat (limited to 'src')
-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 |
4 files changed, 119 insertions, 53 deletions
@@ -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 |