From 30979b6cdb18b0dfb9a346f70c635eaf05cbaa20 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 24 Nov 2023 14:41:45 -0500 Subject: [PATCH] more thought required for ast --- include/bf_tokenizer.h | 40 ++++++++++++++++++++++++++++++---------- src/bf_interpreter.cpp | 20 ++++++++++---------- src/bf_mips_codegen.cpp | 2 +- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/include/bf_tokenizer.h b/include/bf_tokenizer.h index 1d2a31d..22f2f0a 100644 --- a/include/bf_tokenizer.h +++ b/include/bf_tokenizer.h @@ -24,20 +24,20 @@ enum class bf_token CLOSE }; -struct token_t +struct bf_token_t { bf_token type; int64_t offset; std::optional name = {}; - explicit token_t(bf_token type, int64_t offset = 1): type(type), offset(offset) + explicit bf_token_t(bf_token type, int64_t offset = 1): type(type), offset(offset) {} }; class bf_tokenizer { private: - std::vector tokens; + std::vector tokens; size_t conditionalCount = 0; size_t currentIndex = 0; @@ -50,7 +50,8 @@ class bf_tokenizer { tokenize(program); bf_name(); - if (hasNext()){ + if (hasNext()) + { // skip past comments if (peek().type == bf_token::OPEN) while (hasNext() && peek().type != bf_token::CLOSE) @@ -58,30 +59,49 @@ class bf_tokenizer } } - inline bool hasNext(){ + inline bool hasNext() + { return currentIndex < tokens.size(); } - inline size_t advance(){ + inline size_t advance() + { return currentIndex++; } - inline const token_t& next() { + inline const bf_token_t& next() + { return tokens[advance()]; } - inline const token_t& peek(){ + inline const bf_token_t& peek() + { return tokens[currentIndex]; } - inline std::vector& data(){ + inline std::vector& data() + { return tokens; } void print(size_t index); - inline void print(){ + + inline void print() + { print(currentIndex); } }; +enum ast_token +{ + INC_DP, DEC_DP, INC_VAL, DEC_VAL, PRINT, READ, WHILE +}; + +struct ast_token_t +{ + ast_token type; + +}; + + #endif //BRAINFUCK_MISC_BF_TOKENIZER_H diff --git a/src/bf_interpreter.cpp b/src/bf_interpreter.cpp index 2451578..247d512 100644 --- a/src/bf_interpreter.cpp +++ b/src/bf_interpreter.cpp @@ -23,7 +23,7 @@ static inline void match(functor f, int sp, size_t& index, const std::string& pr } template -static inline void match(functor f, int sp, size_t& index, const std::vector& program) +static inline void match(functor f, int sp, size_t& index, const std::vector& program) { while (f(index) < program.size()) { @@ -79,22 +79,22 @@ void interpret_bf(const std::string& program) } } -std::function& tokens)> funcs[8] = { - [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.increment_dp(off); }, - [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.decrement_dp(off); }, - [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { +std::function& tokens)> funcs[8] = { + [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.increment_dp(off); }, + [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.decrement_dp(off); }, + [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.increment(static_cast(off)); }, - [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { + [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.decrement(static_cast(off)); }, - [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.print(std::cout); }, - [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.read(std::cin); }, - [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { + [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.print(std::cout); }, + [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { inter.read(std::cin); }, + [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { if (inter.is() == 0) match([](size_t& idx) { return ++idx; }, 1, index, tokens); }, - [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { + [](brainfuck_interpreter& inter, size_t off, size_t& index, const std::vector& tokens) { if (inter.is() != 0) match([](size_t& idx) { return --idx; }, -1, index, tokens); }, diff --git a/src/bf_mips_codegen.cpp b/src/bf_mips_codegen.cpp index 2dd8249..f08fca8 100644 --- a/src/bf_mips_codegen.cpp +++ b/src/bf_mips_codegen.cpp @@ -22,7 +22,7 @@ setup: bf: )"; -void process_print(const std::vector& tokens, size_t index); +void process_print(const std::vector& tokens, size_t index); void codegen(bf_tokenizer& tokenizer, std::ostream& out) {