-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret_unsafe_unoptimised.cpp
69 lines (68 loc) · 1.95 KB
/
interpret_unsafe_unoptimised.cpp
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
#include "interpret_unsafe_unoptimised.hpp"
void interpret_raw_unsafe(
const std::string& program,
std::vector<uint16_t>& mem_tape,
std::stack<size_t>& bracket_insn_pointers
//size_t data_pointer,
//size_t insn_pointer,
)
{
size_t data_pointer = 0;
size_t insn_pointer = 0;
size_t brackets_to_skip = 0;
for(insn_pointer = 0; insn_pointer < program.size(); insn_pointer++)
{
char insn = program[insn_pointer];
switch(insn)
{
case '+':
mem_tape[data_pointer]++;
break;
case '-':
mem_tape[data_pointer]--;
break;
case '>':
data_pointer++;
break;
case '<':
data_pointer--;
break;
case '.':
fmt::printf("%lc", mem_tape[data_pointer]);
break;
case ',':
/*
fmt::print("\x1b\7");
mem_tape[data_pointer] = getchar();
fflush(stdin);
fmt::print("\x1b\10\x1b[1C\x1b[1A");
*/
mem_tape[data_pointer] = getchar();
break;
case '[':
if(mem_tape[data_pointer] == 0)
{
brackets_to_skip = 1;
size_t j = insn_pointer;
while(brackets_to_skip)
{
j++;
if(program[j] == '[') { brackets_to_skip++; }
if(program[j] == ']') { brackets_to_skip--; }
if(brackets_to_skip == 0) { insn_pointer = j; break; }
}
} else
{
bracket_insn_pointers.push(insn_pointer-1);
}
break;
case ']':
insn_pointer = bracket_insn_pointers.top();
bracket_insn_pointers.pop();
break;
default:
break;
}
}
return;
}