-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
63 lines (50 loc) · 1.33 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LOOP "++++++++++[>+++++++>++++++++++>+++>+<<<<-]"
int cells[5] = {0,70,100,30,10};
int char_to_cell(char c, int current){
int target;
int reltarget;
if(c <'A') target = 3;
if(c>='a' && c<='z') target = 2;
if(c>='A' && c<='Z') target = 1;
if(c == '\n') target = 4;
reltarget = target - current;
c = reltarget < 0 ? '<' : '>';
for(int i = 0; i < abs(reltarget); i++) putchar(c);
return target;
}
int char_to_nr(char c, int cell_nr){
int a = c - cells[cell_nr];
for(int i = 0; i < abs(a); i++) {
(a<0) ? (putchar('-') && cells[cell_nr]--) : (putchar('+') && cells[cell_nr]++);
}
putchar('.');
return 1;
}
int char_to_bf(char c, int index){
index = char_to_cell(c, index);
char_to_nr(c,index);
return index;
}
int text_to_brainfuck(const char* text, int index){
for(int i = 0; text[i] != 0; i++){
index = char_to_bf(text[i], index);
}
return index;
}
int main(int argc, char const *argv[])
{
if(argc >= 2){
int index = 1;
printf(LOOP);
putchar('>');
for(int i = 1; i < argc; i++){
index = text_to_brainfuck(argv[i],index);
index = char_to_bf(' ', index);
}
char_to_bf('\n', index);
}
return 0;
}