-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbf.c
More file actions
133 lines (110 loc) · 2.72 KB
/
bf.c
File metadata and controls
133 lines (110 loc) · 2.72 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
int shell() {
/** Specification:
*/
print("Voila!!, you entered the matrix.\nImplementing shell in version 2.0!\n");
return 0;
}
int increment() {
(*current)++;
return 0;
}
int decrement() {
(*current)--;
return 0;
}
int leftshift() {
// Add the feature to check if the pointer is already at the leftmost position
// If the pointer is already at the leftmost position
// Warn the User and exit the program with an error code
current--;
return 0;
}
int rightshift() {
// Add the feature to check if the pointer is already at the rightmost position
// If it is, either increase the size, or exit the program with an error code
current++;
return 0;
}
int loopin() {
if(*current == 0) {
// look for corresponding ']'
}
else {
// Follow next instruction i.e. read and execute next symbol
}
return 0;
}
int loopout() {
if(*current == 0) {
// Follow next instruction i.e after ']'
}
else {
// Look for its corresponding '['
}
return 0;
}
int print() {
printf("%c", *current);
return 0;
}
int input() {
// Input as an integer so that the ASCII value gets stored
// In current as the address of the memory cell is contained in the pointer current
scanf("%d", current);
return 0;
}
int interpret(char command) {
/** Specification:
* Line is passed to the function so that the interpreted can parse over each of its command
* Current Pointer is passed so that the required modification can take place
*/
// char command = NULL;
// Read the characters of each line one by one
// Ignore the spaces encountered if any till the return character is encountered
// Check if the all the character is a allowed command
// Process each command symbol
switch(command) {
// Increment the value of current cell
case '+':
increment();
break;
// Decrement the value of current value
case '-':
decrement();
break;
// Shifts the pointer to left side of the current cell
case '<':
leftshift();
break;
// Shifts the pointer to the right side of the current cell
case '>':
rightshift();
break;
// Looks for its correspondent "]", refer specifications for more details.
case '[':
loopin();
break;
// Looks for its correspondent "[", refer specifications for more details.
case ']':
loopout();
break;
// Prints the value corresponding to ascii value present in current cell to stdout.
case '.':
print();
break;
// ASCII value of the input from stdin is stored in current cell/memory.
case ',':
input();
break;
// Ignore the space if present
case ' ':
// Pass to the next command in the script
fileptr++;
break;
default:
printf("Error!(Code 4) Unrecognised symbol: %c\n", command);
printf("Exitting program...\n");
exit(4);
}
return 0;
}