-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.cpp
More file actions
132 lines (115 loc) · 3.47 KB
/
Interpreter.cpp
File metadata and controls
132 lines (115 loc) · 3.47 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
#include "Interpreter.h"
#include <iostream>
Execution Interpreter::execute(const std::string& program) {
std::map<unsigned int, int> memory;
return execute(program, memory);
}
Execution Interpreter::execute(const std::string& program, std::map<unsigned int, int> &memory) {
Execution execution(program, memory);
create_bracket_table(execution);
while(execution.program_ctr >= 0 && execution.program_ctr < program.length()) {
progress(execution);
}
return execution;
}
/**
* This method was inspired by a previous implementation of the
* BF interpreter made by 'Nikolaj Banke Jensen' for the PLD coursework in Scala.
*
* Many prominent changes have been mad; for details see Interpreter.h.
*/
int Interpreter::jump_right(const std::string& program, int program_ctr, int level) {
char c = program[program_ctr];
if(c == ']') {
level--;
} else if (c == '[') {
level++;
}
program_ctr++;
if(!((c == ']' && level == -1) || (program_ctr == program.length()))) {
return jump_right(program, program_ctr, level);
}
return program_ctr;
}
/**
* This method was inspired by a previous implementation of the
* BF interpreter made by 'Nikolaj Banke Jensen' for the PLD coursework in Scala.
*
* Many prominent changes have been mad; for details see Interpreter.h.
*/
int Interpreter::jump_left(const std::string& program, int program_ctr, int level) {
char c = program[program_ctr];
if(c == '[') {
level--;
} else if (c == ']') {
level++;
}
if(c == '[' && level == -1) {
program_ctr++;
} else if(program_ctr == 0) {
program_ctr = -1;
} else {
program_ctr--;
return jump_left(program, program_ctr, level);
}
return program_ctr;
}
void Interpreter::create_bracket_table(Execution &execution) {
execution.bracket_table.clear();
for(std::size_t i = 0; i < execution.program.length(); ++i) {
char c = execution.program[i];
if(c == '[') {
execution.bracket_table[i] = jump_right(execution.program, i + 1);
} else if (c == ']') {
execution.bracket_table[i] = jump_left(execution.program, i - 1);
}
}
}
/**
* This method was inspired by a previous implementation of the
* BF interpreter made by 'Nikolaj Banke Jensen' for the PLD coursework in Scala.
*
* Many prominent changes have been mad; for details see Interpreter.h.
*/
void Interpreter::progress(Execution &execution) {
char c = execution.program[execution.program_ctr];
switch (c) {
case '>':
execution.program_ctr++;
execution.memory_ptr++;
break;
case '<':
execution.program_ctr++;
execution.memory_ptr--;
break;
case '+':
execution.program_ctr++;
execution.setMemory(execution.memory_ptr, execution.getMemory(execution.memory_ptr) + 1);
break;
case '-':
execution.program_ctr++;
execution.setMemory(execution.memory_ptr, execution.getMemory(execution.memory_ptr) - 1);
break;
case '.':
execution.program_ctr++;
std::cout << static_cast<char>(execution.getMemory(execution.memory_ptr)) << std::flush;
break;
case '[':
if(execution.getMemory(execution.memory_ptr) == 0) {
execution.program_ctr = execution.bracket_table[execution.program_ctr];
} else {
execution.program_ctr++;
}
break;
case ']':
if(execution.getMemory(execution.memory_ptr) != 0) {
execution.program_ctr = execution.bracket_table[execution.program_ctr];
} else {
execution.program_ctr++;
}
break;
default:
execution.program_ctr++;
break;
}
}