Formatted all files,adjusted cmakelists *2

This commit is contained in:
starcells committed 2026-03-27 23:52:48 +08:00
1 parent b2c7e9800e
commit 68bfe71cd6
10 files changed
+13 -186

No files matched your search

+7 -21
View File
@@ -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)
add_executable(rot8_test tests/vm_test.c)
target_link_libraries(rot8_test rot8_core)
View File
File renamed without changes.
File renamed without changes.
+2 -2
View File
@@ -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
#endif
+2 -2
View File
@@ -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
#endif
-133
View File
@@ -1,133 +0,0 @@
#include "rot8.h"
// #include "rot8_error.h"
#include "rot8_io.h"
#include "rot8_mem.h"
#include <limits.h>
#include <stdbit.h>
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; }
-1
View File
@@ -1 +0,0 @@
#include "rot8_error.h"
-8
View File
@@ -1,8 +0,0 @@
#include "rot8_io.h"
#include <stdio.h>
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()); }
-17
View File
@@ -1,17 +0,0 @@
#include "rot8_mem.h"
#include "rot8.h"
#include <stdlib.h>
#include <string.h>
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); }
+2 -2
View File
@@ -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);
}