-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
235 lines (221 loc) · 6.06 KB
/
main.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* \file main.c
* \brief Brain Fuck Interpreter.
* \author Nicolas Fleurot & Hassan El Azzouzi
* \version 0.0.6
* \date 31/08/2014
*
* A simple Brain Fuck Interpreter in C.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#define SIZE_BUFF 256
#define TOKEN_INF '<'
#define TOKEN_SUP '>'
#define TOKEN_WB '['
#define TOKEN_WE ']'
#define TOKEN_PRINT '.'
#define TOKEN_GET ','
#define TOKEN_MINUS '-'
#define TOKEN_PLUS '+'
#define VERSION_FINAL 0
#define VERSION_SUB 0
#define VERSION_WIP 6
#define STABLE 1
#define AUTHOR "Hassan El Azzouzi & Nicolas Fleurot"
void Parse(const char* a_sInput);
void IBF(void);
void getVersion(void);
int main(int argc, char** argv)
{
//TODO File input/output arguments
//flag 0 : interpreter command line
//flag 1 : get version
int flags[2]={0};
int c;
char *stringOpt = NULL;
opterr = 0;
while((c = getopt(argc, argv, "r:iv"))!=-1)
{
switch(c)
{
case 'r':
stringOpt=optarg;
break;
case 'i':
flags[0]=1;
break;
case 'v':
flags[1]=1;
break;
case '?':
if(optopt=='r')
fprintf(stderr,"Option -%c requires an argument [String].\n",optopt);
else if(isprint(optopt))
fprintf(stderr,"Unknown option %c.\n",optopt);
else
fprintf(stderr,"Unknown option character %c.\n",optopt);
return 1;
default:
exit(EXIT_FAILURE);
break;
}
}
printf ("flags[0] = %d, stringOpt = %s\n",
flags[0], stringOpt);
if(stringOpt!=NULL)
Parse(stringOpt);
else if(flags[1]==1)
getVersion();
else if(flags[0]==1)
IBF();
return EXIT_SUCCESS;
}
/**
* \fn void Parse(const char* a_sInput)
* \brief Parsing a string.
* \param a_sInput (String)
*/
void Parse(const char* a_sInput)
{
int i=0;
int t_iIndex = 0;
int t_iPosIndex = 0;
int t_iFactor = 1;
unsigned int* t_Buffer = (unsigned int*)calloc(SIZE_BUFF, sizeof(unsigned int));
if(t_Buffer==NULL)
perror("calloc");
while(a_sInput[t_iIndex] != '\0')
{
char t_Token = a_sInput[t_iIndex];
switch (t_Token)
{
case TOKEN_PLUS:
{
t_Buffer[t_iPosIndex] += 1;
break;
}
case TOKEN_MINUS:
{
t_Buffer[t_iPosIndex] -= 1;
break;
}
case TOKEN_SUP:
{
t_iPosIndex += 1;
if(t_iPosIndex > (t_iFactor * SIZE_BUFF)){
t_iFactor += 1;
unsigned int* t_NewBuffer = (unsigned int *)calloc(t_iFactor * SIZE_BUFF, sizeof(unsigned int));
if(t_NewBuffer==NULL)
perror("calloc");
i=0;
for(i=0 ; i < ((t_iFactor - 1) * SIZE_BUFF) ; i++){
t_NewBuffer[i] = t_Buffer[i];
}
free(t_Buffer);
t_Buffer = t_NewBuffer;
}
break;
}
case TOKEN_INF:
{
t_iPosIndex -= 1;
break;
}
case TOKEN_PRINT:
{
putchar(t_Buffer[t_iPosIndex]);
break;
}
case TOKEN_GET:
{
t_Buffer[t_iPosIndex] = getchar();
break;
}
case TOKEN_WB:
{
if(t_Buffer[t_iPosIndex] == 0){
int t_iLevel = 1;
while(t_iLevel != 0){
t_iIndex += 1;
if(a_sInput[t_iIndex] == '['){
t_iLevel += 1;
}else if(a_sInput[t_iIndex] == ']'){
t_iLevel -= 1;
}
}
}
break;
}
case TOKEN_WE:
{
if(t_Buffer[t_iPosIndex] != 0){
int t_iLevel = 1;
while(t_iLevel != 0){
t_iIndex -= 1;
if(a_sInput[t_iIndex] == '['){
t_iLevel -= 1;
}else if(a_sInput[t_iIndex] == ']'){
t_iLevel += 1;
}
}
}
break;
}
default:
{
break;
}
}
t_iIndex += 1;
} // While
free(t_Buffer);
}
/**
* \fn void IBF(void)
* \brief Dirty interpreter.
* \param void
*/
void IBF(void)
{
char* brainCode=(char *)calloc((size_t)SIZE_BUFF,sizeof(char));
if(brainCode==NULL)
perror("calloc");
int tmpFree=0;
do{
printf(">>");
char *posRet=NULL;
if(fgets(brainCode,(SIZE_BUFF*sizeof(unsigned char)),stdin)!=NULL)
posRet=strchr(brainCode,'\n');
if(posRet!=NULL)
*posRet='\0';
else
while(tmpFree=0,tmpFree!='\n' && tmpFree!=EOF) tmpFree=getchar();
puts("\n\n\t\t-----------------------");
Parse(brainCode);
puts("");
}while(strchr(brainCode,'e')==NULL);
getVersion();
free(brainCode);
}
/**
* \fn void getVersion(void)
* \brief Print the version.
* \param void
*/
void getVersion(void)
{
puts("\t\t ___ ________ ________ ");
puts("\t\t|\\ \\|\\ __ \\|\\ _____\\");
puts("\t\t\\ \\ \\ \\ \\|\\ /\\ \\ \\__/");
puts("\t\t\\ \\ \\ \\ __ \\ \\ __\\");
puts("\t\t\\ \\ \\ \\ \\|\\ \\ \\ \\_|");
puts("\t\t\\ \\__\\ \\_______\\ \\__\\");
puts("\t\t\\|__|\\|_______|\\|__|");
printf("IBrainFuck version %s %d.%d.%d by %s %s\n",(STABLE)? "STABLE" : "NOT STABLE",
VERSION_FINAL,VERSION_SUB,VERSION_WIP,AUTHOR,__DATE__);
}