-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainTerminal.c
100 lines (95 loc) · 2.48 KB
/
mainTerminal.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 255
int valuesBox[MAX] = {0};
int current_box_position = 0;
void inputTaker(int pos)
{
char temp;
scanf(" %c", &temp);
valuesBox[pos] = (int)temp;
}
void decoder(char *p)
{
int loopStartPositions[1000];
int loopTop = -1;
for (int i = 0; p[i] != '\0'; i++)
{
switch (p[i])
{
case '+':
valuesBox[current_box_position] += 1;
break;
case '-':
valuesBox[current_box_position] -= 1;
break;
case '>':
current_box_position += 1;
if (current_box_position >= MAX)
{
printf("Error: Data pointer out of bounds (too far right)\n");
return;
}
break;
case '<':
current_box_position -= 1;
if (current_box_position < 0)
{
printf("Error: Data pointer out of bounds (too far left)\n");
return;
}
break;
case '.':
printf("%c", valuesBox[current_box_position]);
break;
case ',':
inputTaker(current_box_position);
break;
case '[':
if (valuesBox[current_box_position] == 0)
{
int bracket_flag = 1;
while (bracket_flag > 0)
{
i++;
if (p[i] == '[')
bracket_flag++;
else if (p[i] == ']')
bracket_flag--;
if (p[i] == '\0')
{
printf("Error: Mismatched '['\n");
return;
}
}
}
else
{
loopTop++;
loopStartPositions[loopTop] = i;
}
break;
case ']':
if (loopTop < 0)
{
printf("Error: Mismatched ']'\n");
return;
}
if (valuesBox[current_box_position] != 0)
i = loopStartPositions[loopTop];
else
loopTop--;
break;
default:
break;
}
}
printf("\n");
}
void main()
{
char input[30000];
scanf("%[^\n]s", input);
decoder(input);
}