main
Brett 2023-11-20 17:52:26 -05:00
parent 72a59ff8bc
commit 2bcd08df90
5 changed files with 146 additions and 1410 deletions

View File

@ -61,18 +61,15 @@ class tokenizer
currentIndex++;
}
inline optional_token next() {
if (currentIndex >= tokens.size())
return {};
inline const token_t& next() {
return tokens[currentIndex++];
}
inline optional_token peek(){
if (currentIndex >= tokens.size())
return {};
inline const token_t& peek(){
return tokens[currentIndex];
}
void print();
void print(size_t index = 0);
};

1524
mips.asm

File diff suppressed because it is too large Load Diff

View File

@ -31,17 +31,18 @@ void codegen(tokenizer& tokenizer, std::ostream& out)
{
out << preamble;
size_t index = 0;
if (!tokenizer.hasNext())
throw std::runtime_error("You failed to provide valid BF code");
// skip past comments
if (tokenizer.next().value().get().token == bf_token::OPEN)
while (tokens[index].token != bf_token::CLOSE)
index++;
if (tokenizer.next().token == bf_token::OPEN)
while (tokenizer.hasNext() && tokenizer.next().token != bf_token::CLOSE);
tokenizer.print(index);
size_t sp = 0;
while (index < tokens.size())
while (tokenizer.hasNext())
{
auto& token = tokens[index++];
auto& token = tokenizer.next();
uint64_t name = 0;
if (token.name.has_value())
name = token.name.value();

View File

@ -96,7 +96,7 @@ inline void tabulate(signed long long v)
void tokenizer::print(size_t index)
{
signed long long sp = 1;
signed long long sp = 0;
while (index < tokens.size())
{
auto& token = tokens[index++];
@ -147,3 +147,8 @@ void tokenizer::print(size_t index)
}
}
}
void tokenizer::print()
{
print(currentIndex);
}

View File

@ -5,10 +5,11 @@
#include <blt/std/loader.h>
#include <bf_mips_codegen.h>
#include <bf_interpreter.h>
int main(int argc, const char** argv)
{
std::string file{"../life.bf"};
std::string file{"../helloworld.bf"};
if (argc > 1)
file = argv[1];
auto program = blt::fs::loadBrainFuckFile(file);
@ -18,5 +19,7 @@ int main(int argc, const char** argv)
tokenizer tokenizer(program);
codegen(tokenizer, out);
//interpret_bf(program);
return 0;
}