summaryrefslogtreecommitdiff
path: root/src/bfi.c
blob: 03b32c17599c6e2f54d275553df92bd43c4edd6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#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){

    struct stack_st stack= { .len = 0, .size = 0, .arr = NULL};

    int i = 0;
    int length = 0;
    while (program[i++] != '\0')
        length++;

    i = 0;
    while (i < length){
        
        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(&stack, i);
                int memVal = mem_get();

                //printf("StartLoop! i = %d\n",i);
                //printf("memVal: %d\n", mem_get());
                //printf("stackVal: %d\n", stackPeek(&stack));
                //printf("%c\n\n", program[i]);

                //Skiping loop
                if (memVal == 0){
                    int helpCounter = 1;
                    while (helpCounter != 0){
                        i++;
                        if (program[i] == '[')
                            helpCounter++;
                        if (program[i] == ']')
                            helpCounter--;
                    }
                    stackPop(&stack);
                }
                break;
            }


            case BF_END_LOOP: {
                //printf("Endloop! i = %d\n",i);
                //printf("memVal: %d\n", mem_get());
                //printf("stackVal: %d\n", stackPeek(&stack));
                //printf("%c\n\n", program[i]);

                if (mem_get() != 0){
                    i = stackPop(&stack);
                    stackPush(&stack, i);
                }
                else
                    stackPop(&stack);
                break;
            }

        } // Switch end
        i++;
    } // While loop end
}