blt is broken

main
Brett 2023-11-22 17:26:19 -05:00
parent 1c6a6965e6
commit 117e0210c8
5 changed files with 76 additions and 8 deletions

View File

@ -7,9 +7,9 @@ option(ENABLE_TSAN "Enable the thread data race sanitizer" OFF)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
option(BUILD_PROFILING OFF) option(BUILD_PROFILING ON)
option(BUILD_NBT OFF) option(BUILD_NBT ON)
option(BUILD_PARSE OFF) option(BUILD_PARSE ON)
add_subdirectory(libraries/BLT) add_subdirectory(libraries/BLT)
include_directories(include/) include_directories(include/)

View File

@ -50,6 +50,12 @@ class bf_tokenizer
{ {
tokenize(program); tokenize(program);
bf_name(); bf_name();
if (hasNext()){
// skip past comments
if (peek().type == bf_token::OPEN)
while (hasNext() && peek().type != bf_token::CLOSE)
advance();
}
} }
inline bool hasNext(){ inline bool hasNext(){
@ -68,6 +74,10 @@ class bf_tokenizer
return tokens[currentIndex]; return tokens[currentIndex];
} }
inline std::vector<token_t>& data(){
return tokens;
}
void print(size_t index); void print(size_t index);
inline void print(){ inline void print(){
print(currentIndex); print(currentIndex);

View File

@ -20,6 +20,20 @@ static inline void match(functor f, int sp, size_t& index, const std::string& pr
} }
} }
template<typename functor>
static inline void match(functor f, int sp, size_t& index, const std::vector<token_t>& program)
{
while (f(index) < program.size())
{
if (program[index].type == bf_token::OPEN)
sp++;
if (program[index].type == bf_token::CLOSE)
sp--;
if (sp == 0)
break;
}
}
void interpret_bf(const std::string& program) void interpret_bf(const std::string& program)
{ {
brainfuck_interpreter fuck; brainfuck_interpreter fuck;
@ -65,7 +79,44 @@ void interpret_bf(const std::string& program)
void interpret_bf(bf_tokenizer& tokenizer) void interpret_bf(bf_tokenizer& tokenizer)
{ {
brainfuck_interpreter fuck;
auto& tokens = tokenizer.data();
size_t index = 0;
while (index < tokens.size())
{
auto c = tokens[index];
switch (c.type)
{
case bf_token::INC_DP:
fuck.increment_dp(c.offset);
break;
case bf_token::DEC_DP:
fuck.decrement_dp(c.offset);
break;
case bf_token::INC_DV:
fuck.increment(static_cast<int8_t>(c.offset));
break;
case bf_token::DEC_DV:
fuck.decrement(static_cast<int8_t>(c.offset));
break;
case bf_token::PRINT:
fuck.print(std::cout);
break;
case bf_token::READ:
fuck.read(std::cin);
break;
case bf_token::OPEN:
if (fuck.is() == 0)
match([](size_t& idx) { return ++idx; }, 1, index, tokens);
break;
case bf_token::CLOSE:
if (fuck.is() != 0)
match([](size_t& idx) { return --idx; }, -1, index, tokens);
break;
}
index++;
}
} }
void brainfuck_interpreter::check_size() void brainfuck_interpreter::check_size()

View File

@ -29,10 +29,6 @@ void codegen(bf_tokenizer& tokenizer, std::ostream& out)
out << preamble; out << preamble;
if (!tokenizer.hasNext()) if (!tokenizer.hasNext())
throw std::runtime_error("You failed to provide valid BF code"); throw std::runtime_error("You failed to provide valid BF code");
// skip past comments
if (tokenizer.peek().type == bf_token::OPEN)
while (tokenizer.hasNext() && tokenizer.peek().type != bf_token::CLOSE)
tokenizer.advance();
tokenizer.print(); tokenizer.print();

View File

@ -6,6 +6,7 @@
#include <bf_mips_codegen.h> #include <bf_mips_codegen.h>
#include <bf_interpreter.h> #include <bf_interpreter.h>
#include <blt/profiling/profiler_v2.h>
int main(int argc, const char** argv) int main(int argc, const char** argv)
{ {
@ -19,6 +20,16 @@ int main(int argc, const char** argv)
bf_tokenizer tokenizer(program); bf_tokenizer tokenizer(program);
codegen(tokenizer, out); codegen(tokenizer, out);
BLT_START_INTERVAL("Interpreters", "Basic");
interpret_bf(program);
BLT_END_INTERVAL("Interpreters", "Basic");
BLT_START_INTERVAL("Interpreters", "Tokens");
interpret_bf(tokenizer);
BLT_END_INTERVAL("Interpreters", "Tokens");
BLT_PRINT_PROFILE("Interpreters");
//interpret_bf(program); //interpret_bf(program);
return 0; return 0;