-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbf.c
More file actions
136 lines (118 loc) · 2.63 KB
/
bf.c
File metadata and controls
136 lines (118 loc) · 2.63 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
134
135
136
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
extern void emulate(const uint8_t *restrict convprog, const uint32_t *restrict brackets, uint16_t *restrict memory);
#define N_STACKS (1<<16)
static uint16_t memory[N_STACKS];
//// hello world
//static const char *program =
//"++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-."
//"<.+++.------.--------.>>+.>++.";
// long-running benchmark programm
static const char *program =
">+>+>+>+>++<[>[<+++>->>>>>>+>+>+>+>++<[>[<+++>->>>>>>+>+>+>+>++<[>[<+++>->>>>"
">>+>+>+>+>++<[>[<+++>->>>>>+++[->+++++<]>[-]<<<<<<]<<]>[-]<<<<<]<<]>[-]<<<<<]"
"<<]>[-]<<<<<]<<]>.";
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
const uint32_t inputlen = strlen(program);
// count real programm length
uint32_t proglen = 0;
for(uint32_t i = 0; i != inputlen; ++i) {
switch(program[i])
{
case '>':
case '<':
case '+':
case '-':
case '.':
case ',':
case '[':
case ']':
proglen += 1;
break;
default:
break;
}
}
uint8_t *convprog = malloc(sizeof(uint8_t) * (proglen+1));
if(convprog == NULL) {
fprintf(stderr, "malloc failed\n");
exit(-1);
}
// convert program to range 0 - 7
uint32_t idx = 0;
for(uint32_t i = 0; i != inputlen; ++i) {
uint32_t inc = 1;
switch(program[i])
{
case '>':
convprog[idx] = 0;
break;
case '<':
convprog[idx] = 1;
break;
case '+':
convprog[idx] = 2;
break;
case '-':
convprog[idx] = 3;
break;
case '.':
convprog[idx] = 4;
break;
case ',':
convprog[idx] = 5;
break;
case '[':
convprog[idx] = 6;
break;
case ']':
convprog[idx] = 7;
break;
default:
inc = 0;
break;
}
idx += inc;
}
// end of program symbol
convprog[proglen] = 8;
// precompute jump table for loops
uint32_t *brackets = (uint32_t*)malloc(sizeof(uint32_t) * proglen);
uint32_t *stack = (uint32_t*)malloc(sizeof(uint32_t) * (proglen+1));
if(brackets == NULL || stack == NULL) {
fprintf(stderr, "malloc failed\n");
exit(-1);
}
uint32_t stack_idx = 1;
for(uint32_t i = 0; i != proglen; ++i) {
brackets[i] = 0xffffffff;
if(convprog[i] == 6) {
stack[stack_idx] = i;
stack_idx += 1;
}
else if(convprog[i] == 7) {
stack_idx -= 1;
if(stack_idx == 0) {
fprintf(stderr, "too many closing brackets\n");
exit(-1);
}
brackets[i] = stack[stack_idx];
brackets[stack[stack_idx]] = i;
}
}
if(stack_idx != 1) {
fprintf(stderr, "too few closing brackets\n");
exit(-1);
}
free(stack);
emulate(convprog, brackets, memory);
putchar('\n');
free(brackets);
free(convprog);
return 0;
}