Skip to content

Commit

Permalink
cleanup upon review
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrk24 committed Oct 1, 2024
1 parent c3a53a1 commit 749401e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 48 deletions.
9 changes: 1 addition & 8 deletions src/assembly_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ NONNULL_PTR(const std::vector<NONNULL_PTR(std::vector<instruction>)>) assembly_s
} else if (argument[0] == '0' && argument[1] == 'x') {
char* last = nullptr;
unsigned long ul = strtoul(argument.c_str(), &last, 16);
if (*last != '\0') {
if (*last != '\0' || ul > 0x1f'ffffUL) {
invalid_literal(argument);
}
arg_value = static_cast<int24_t>(ul);
Expand Down Expand Up @@ -212,10 +212,6 @@ NONNULL_PTR(const std::vector<NONNULL_PTR(std::vector<instruction>)>) assembly_s
}
}

// for (const auto& el : m_label_locations) {
// std::cout << "{ " << el.second.first << ", " << el.second.second << " }: " << el.first << std::endl;
// }

// Second pass
for (auto* fragment : *m_fragments) {
for (auto& instr : *fragment) {
Expand Down Expand Up @@ -248,9 +244,6 @@ void assembly_scanner::advance(IP& ip, std::function<bool()> go_left) {
ip.first++;
ip.second = 0;
}
if (ip.first >= m_fragments->size()) {
ip.first = 0;
}
}

assembly_scanner::IP assembly_scanner::get_current_location() const {
Expand Down
44 changes: 44 additions & 0 deletions src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,47 @@
#ifndef __EMSCRIPTEN__
extern "C" void send_thread_count(size_t) {}
#endif

template<>
void interpreter<program_walker>::split_thread(
std::vector<thread<program_walker>>& new_threads,
const thread<program_walker>& old_thread
) const {
switch (old_thread.m_ip.dir) {
case direction::east:
new_threads.emplace_back(old_thread, direction::northeast);
new_threads.emplace_back(old_thread, direction::southeast);
break;
case direction::west:
new_threads.emplace_back(old_thread, direction::northwest);
new_threads.emplace_back(old_thread, direction::southwest);
break;
default:
unreachable("Only west- and east-moving threads can split");
}
}

template<>
thread<program_walker> interpreter<program_walker>::join_threads(size_t first_index, size_t second_index) {
thread<program_walker>& first_thread = m_threads[first_index];
thread<program_walker>& second_thread = m_threads[second_index];

direction new_dir;
// They should either be facing both NW/SW or both NE/SE, never one going east and one going west
assert((static_cast<char>(first_thread.m_ip.dir) & 0b011) == (static_cast<char>(second_thread.m_ip.dir) & 0b011));
switch (first_thread.m_ip.dir) {
case direction::northeast:
case direction::southeast:
new_dir = direction::east;
break;
case direction::northwest:
case direction::southwest:
new_dir = direction::west;
break;
default:
unreachable("East- and west-going IPs cannot merge");
}

std::vector<int24_t> new_stack = join_threads_work(first_thread, second_thread);
return thread<program_walker>(first_thread, new_dir, std::move(new_stack));
}
41 changes: 3 additions & 38 deletions src/interpreter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -168,45 +168,10 @@ private:
};

template<>
inline void interpreter<program_walker>::split_thread(
void interpreter<program_walker>::split_thread(
std::vector<thread<program_walker>>& new_threads,
const thread<program_walker>& old_thread
) const {
switch (old_thread.m_ip.dir) {
case direction::east:
new_threads.emplace_back(old_thread, direction::northeast);
new_threads.emplace_back(old_thread, direction::southeast);
break;
case direction::west:
new_threads.emplace_back(old_thread, direction::northwest);
new_threads.emplace_back(old_thread, direction::southwest);
break;
default:
unreachable("Only west- and east-moving threads can split");
}
}
) const;

template<>
inline thread<program_walker> interpreter<program_walker>::join_threads(size_t first_index, size_t second_index) {
thread<program_walker>& first_thread = m_threads[first_index];
thread<program_walker>& second_thread = m_threads[second_index];

direction new_dir;
// They should either be facing both NW/SW or both NE/SE, never one going east and one going west
assert((static_cast<char>(first_thread.m_ip.dir) & 0b011) == (static_cast<char>(second_thread.m_ip.dir) & 0b011));
switch (first_thread.m_ip.dir) {
case direction::northeast:
case direction::southeast:
new_dir = direction::east;
break;
case direction::northwest:
case direction::southwest:
new_dir = direction::west;
break;
default:
unreachable("East- and west-going IPs cannot merge");
}

std::vector<int24_t> new_stack = join_threads_work(first_thread, second_thread);
return thread<program_walker>(first_thread, new_dir, std::move(new_stack));
}
thread<program_walker> interpreter<program_walker>::join_threads(size_t first_index, size_t second_index);
2 changes: 1 addition & 1 deletion src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern "C" void send_debug_info(
size_t stack_depth,
size_t y,
size_t x,
const char* instruction
CONST_C_STR instruction
) {
cout << "Thread " << thread_number << '\n';
if (stack != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion src/thread.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern "C" void send_debug_info(
size_t stack_depth,
size_t y,
size_t x,
const char* instruction
CONST_C_STR instruction
);

template<class ProgramHolder>
Expand Down

0 comments on commit 749401e

Please sign in to comment.