summaryrefslogtreecommitdiff
path: root/src/bfi.c
diff options
context:
space:
mode:
authorRasmus Luha <rasmus.luha@gmail.com>2022-10-22 02:45:34 +0300
committerRasmus Luha <rasmus.luha@gmail.com>2022-10-22 02:45:34 +0300
commitfcb9ff2b96de6989e9a676e96753b1b1279acf67 (patch)
treee9a8839939aac6eeba3dea89ca0c20ad8268150e /src/bfi.c
parenta97b362e6a35be4575f7ded3c31cdeede89ab2a8 (diff)
hw3 status
Diffstat (limited to 'src/bfi.c')
-rw-r--r--src/bfi.c102
1 files changed, 100 insertions, 2 deletions
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 <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
+}