From 68bfe71cd6994bd99641416d7499e1ed512026f8 Mon Sep 17 00:00:00 2001 From: starcells Date: Fri, 27 Mar 2026 23:52:48 +0800 Subject: [PATCH] Formatted all files,adjusted cmakelists *2 --- CMakeLists.txt | 28 ++--- rot8.h => include/rot8/rot8.h | 0 rot8_error.h => include/rot8/rot8_error.h | 0 rot8_io.h => include/rot8/rot8_io.h | 4 +- rot8_mem.h => include/rot8/rot8_mem.h | 4 +- rot8.c | 133 ---------------------- rot8_error.c | 1 - rot8_io.c | 8 -- rot8_mem.c | 17 --- test.c => tests/vm_test.c | 4 +- 10 files changed, 13 insertions(+), 186 deletions(-) rename rot8.h => include/rot8/rot8.h (100%) rename rot8_error.h => include/rot8/rot8_error.h (100%) rename rot8_io.h => include/rot8/rot8_io.h (77%) rename rot8_mem.h => include/rot8/rot8_mem.h (86%) delete mode 100644 rot8.c delete mode 100644 rot8_error.c delete mode 100644 rot8_io.c delete mode 100644 rot8_mem.c rename test.c => tests/vm_test.c (59%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4276615..3c77f50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,27 +1,13 @@ cmake_minimum_required(VERSION 3.10) -project(rot8_project) +project(Rot8 C) -set(CMAKE_C_STANDARD 99) -set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_STANDARD 11) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic") +include_directories(include) -add_library(rot8 STATIC - rot8.c - rot8_io.c - rot8_error.c - rot8_mem.c -) +file(GLOB_RECURSE SOURCES "src/*.c") -target_include_directories(rot8 PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR} -) +add_library(rot8_core STATIC ${SOURCES}) -add_executable(test_rot8 - test.c -) - -target_link_libraries(test_rot8 rot8) - -enable_testing() -add_test(NAME test_rot8 COMMAND test_rot8) \ No newline at end of file +add_executable(rot8_test tests/vm_test.c) +target_link_libraries(rot8_test rot8_core) \ No newline at end of file diff --git a/rot8.h b/include/rot8/rot8.h similarity index 100% rename from rot8.h rename to include/rot8/rot8.h diff --git a/rot8_error.h b/include/rot8/rot8_error.h similarity index 100% rename from rot8_error.h rename to include/rot8/rot8_error.h diff --git a/rot8_io.h b/include/rot8/rot8_io.h similarity index 77% rename from rot8_io.h rename to include/rot8/rot8_io.h index 788a2d6..038537f 100644 --- a/rot8_io.h +++ b/include/rot8/rot8_io.h @@ -1,9 +1,9 @@ #ifndef ROT8_IO_H #define ROT8_IO_H -#include "rot8.h" +#include "rot8/rot8.h" void rot8_out_io(rot8_iter *it); void rot8_inp_io(rot8_iter *it); -#endif \ No newline at end of file +#endif diff --git a/rot8_mem.h b/include/rot8/rot8_mem.h similarity index 86% rename from rot8_mem.h rename to include/rot8/rot8_mem.h index 07ee485..d9abc83 100644 --- a/rot8_mem.h +++ b/include/rot8/rot8_mem.h @@ -1,6 +1,6 @@ #ifndef ROT8_MEM_H #define ROT8_MEM_H -#include "rot8.h" +#include "rot8/rot8.h" void rot8_mem_init(rot8_data **mem); void rot8_mem_free(rot8_data *mem); @@ -8,4 +8,4 @@ void rot8_mem_free(rot8_data *mem); void rot8_ins_init(rot8_bytecode **ins); void rot8_ins_free(rot8_bytecode *ins); -#endif \ No newline at end of file +#endif diff --git a/rot8.c b/rot8.c deleted file mode 100644 index 98f5333..0000000 --- a/rot8.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "rot8.h" -// #include "rot8_error.h" -#include "rot8_io.h" -#include "rot8_mem.h" -#include -#include - -struct rot8_iter { - rot8_data *mem_now; - rot8_bytecode *ins_now; - rot8_loop_depth loop_depth; -}; - -void step_ins(rot8_iter *iter) { - switch (*(iter->ins_now)) { - case STP: - stp(iter); - break; - case BTP: - btp(iter); - break; - case ROR: - ror(iter); - break; - case FLB: - flb(iter); - break; - case BNZ: - bnz(iter); - break; - case RET: - ret(iter); - break; - case OUT: - rot8_out_io(iter); - break; - case INP: - rot8_inp_io(iter); - break; - default:; - } - iter->ins_now++; -} - -rot8_bytecode rot8_translate(char c) { - switch (c) { - case '>': - return (STP); - case '<': - return (BTP); - case '+': - return (ROR); - case '-': - return (FLB); - case '[': - return (BNZ); - case ']': - return (RET); - case '.': - return (OUT); - case ',': - return (INP); - default:; - } - return (OUT); -} - -inline void run(char raw_ins[], int step) { - rot8_bytecode *ins; - rot8_data *mem; - rot8_iter it; - rot8_mem_init(&mem); - rot8_ins_init(&ins); - rot8_iter_init(&it, mem, ins); - for (int i = 0; i < step; i++) { - (it.ins_now)[i] = rot8_translate(raw_ins[i]); - } - while (it.ins_now != (ins + step)) { - step_ins(&it); - } - rot8_mem_free(mem); - rot8_ins_free(ins); -} - -void rot8_iter_init(rot8_iter *iter, rot8_data *mem, rot8_bytecode *ins) { - iter->mem_now = mem; - iter->ins_now = ins; -} - -void stp(rot8_iter *iter) { iter->mem_now++; } - -void btp(rot8_iter *iter) { iter->mem_now--; } - -void ror(rot8_iter *iter) { - *(iter->mem_now) = ((*(iter->mem_now) & 1) << (DATA_WIDTH - 1) | - (*(iter->mem_now)) >> 1); // Rotate right pointed data -} - -void flb(rot8_iter *iter) { *(iter->mem_now) ^= 1; } - -// TODO -void bnz(rot8_iter *iter) { - if ((*(iter->mem_now) & 1) == 0) { - iter->loop_depth = 0; - iter->ins_now++; - while (iter->loop_depth != 0 || *(iter->ins_now) != RET) { - if (*(iter->ins_now) == BNZ) - iter->loop_depth++; - else if (*(iter->ins_now) == RET) - iter->loop_depth--; - iter->ins_now++; - } - } -} - -// TODO -void ret(rot8_iter *iter) { - iter->loop_depth = 0; - iter->ins_now--; - while (iter->loop_depth != 0 || *(iter->ins_now) != BNZ) { - if (*(iter->ins_now) == BNZ) - iter->loop_depth--; - else if (*(iter->ins_now) == RET) { - iter->loop_depth++; - } - iter->ins_now--; - } - iter->ins_now--; -} - -rot8_data out(rot8_iter *iter) { return (*(iter->mem_now)); } - -void inp(rot8_iter *iter, rot8_data input) { *(iter->mem_now) = input; } \ No newline at end of file diff --git a/rot8_error.c b/rot8_error.c deleted file mode 100644 index 3f1012e..0000000 --- a/rot8_error.c +++ /dev/null @@ -1 +0,0 @@ -#include "rot8_error.h" \ No newline at end of file diff --git a/rot8_io.c b/rot8_io.c deleted file mode 100644 index 5919734..0000000 --- a/rot8_io.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "rot8_io.h" -#include - -void rot8_out_io(rot8_iter *it) { - // putchar(out(it)); - printf("%d\n", out(it)); -} -void rot8_inp_io(rot8_iter *it) { inp(it, (rot8_data)getchar()); } \ No newline at end of file diff --git a/rot8_mem.c b/rot8_mem.c deleted file mode 100644 index 6398309..0000000 --- a/rot8_mem.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "rot8_mem.h" -#include "rot8.h" -#include -#include - -void rot8_mem_init(rot8_data **mem) { - *mem = malloc(sizeof(**mem) * MEM_SIZE); - memset(*mem, 0, sizeof(**mem) * MEM_SIZE); -} - -void rot8_mem_free(rot8_data *mem) { free(mem); } - -void rot8_ins_init(rot8_bytecode **ins) { - *ins = malloc(sizeof(**ins) * INS_SIZE); -} - -void rot8_ins_free(rot8_bytecode *ins) { free(ins); } \ No newline at end of file diff --git a/test.c b/tests/vm_test.c similarity index 59% rename from test.c rename to tests/vm_test.c index b842c07..9bc650b 100644 --- a/test.c +++ b/tests/vm_test.c @@ -1,9 +1,9 @@ -#include "rot8.h" +#include "rot8/rot8.h" int main(int argc, char **argv) { (void)argc; (void)argv; - char ins[] = "-+-+-+-+-+-+-+-+-+>-+-+++++++<[>[.-+.]<-+]"; + char ins[] = "-+-+-+-+-+-+-+-+-+>-+-+-+-+++++<[>[.-+.]<-+]"; run(ins, sizeof(ins) / sizeof(ins[0]) - 1); return (0); } \ No newline at end of file