202604082015

This commit is contained in:
starcells committed 2026-04-18 20:15:58 +08:00
1 parent dc701fdd70
commit 954ed0828d
6 files changed
+68 -62

No files matched your search

+3 -2
View File
@@ -1,3 +1,4 @@
BasedOnStyle: LLVM
BasedOnStyle: Google
IndentWidth: 4
UseTab: Always
UseTab: Always
PointerAlignment: Right
+8 -9
View File
@@ -6,7 +6,6 @@
#define MEM_SIZE 256
#define INS_SIZE 16384
#define JUMP_TABLE_SIZE INS_SIZE
typedef uint8_t rot8_data;
typedef uint8_t rot8_loop_depth;
@@ -14,14 +13,14 @@ typedef uint8_t rot8_loop_depth;
#define DATA_WIDTH (sizeof(rot8_data) * CHAR_BIT)
typedef enum {
STP, // Step Tape Pointer,'>'
BTP, // Back Tape Pointer,'<'
ROR, // ROtate Right,'-'
FLB, // Flip Last Bit,'+'
BIZ, // Branch If Zero,'['
RNZ, // Return Not Zero,']'
OUT, // OUTput byte,'.'
INP, // INPut byte,','
STP, // Step Tape Pointer,'>'
BTP, // Back Tape Pointer,'<'
ROR, // ROtate Right,'-'
FLB, // Flip Last Bit,'+'
BIZ, // Branch If Zero,'['
RNZ, // Return Not Zero,']'
OUT, // OUTput byte,'.'
INP, // INPut byte,','
} rot8_bytecode;
typedef struct rot8_iter rot8_iter;
+47 -48
View File
@@ -1,10 +1,11 @@
#include "rot8/rot8.h"
// #include "rot8_error.h"
#include "rot8/rot8_io.h"
#include "rot8/rot8_mem.h"
#include <limits.h>
#include <stdbit.h>
#include "rot8/rot8_io.h"
#include "rot8/rot8_mem.h"
struct rot8_iter {
rot8_data *mem_now;
rot8_bytecode *ins_now;
@@ -13,59 +14,59 @@ struct rot8_iter {
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 BIZ:
biz(iter);
break;
case RNZ:
rnz(iter);
break;
case OUT:
rot8_out_io(iter);
break;
case INP:
rot8_inp_io(iter);
break;
default:;
case STP:
stp(iter);
break;
case BTP:
btp(iter);
break;
case ROR:
ror(iter);
break;
case FLB:
flb(iter);
break;
case BIZ:
biz(iter);
break;
case RNZ:
rnz(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 (BIZ);
case ']':
return (RNZ);
case '.':
return (OUT);
case ',':
return (INP);
default:;
case '>':
return (STP);
case '<':
return (BTP);
case '+':
return (ROR);
case '-':
return (FLB);
case '[':
return (BIZ);
case ']':
return (RNZ);
case '.':
return (OUT);
case ',':
return (INP);
default:;
}
return (OUT);
}
inline void run(char raw_ins[], int step) {
void run(char raw_ins[], int step) {
rot8_bytecode *ins;
rot8_data *mem;
rot8_iter it;
@@ -93,12 +94,11 @@ 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
(*(iter->mem_now)) >> 1); // Rotate right pointed data
}
void flb(rot8_iter *iter) { *(iter->mem_now) ^= 1; }
// TODO
void biz(rot8_iter *iter) {
if ((*(iter->mem_now) & 1) == 0) {
iter->loop_depth = 0;
@@ -113,7 +113,6 @@ void biz(rot8_iter *iter) {
}
}
// TODO
void rnz(rot8_iter *iter) {
if ((*(iter->mem_now) & 1) == 1) {
iter->loop_depth = 0;
+6 -1
View File
@@ -1,8 +1,13 @@
#include "rot8/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()); }
void rot8_inp_io(rot8_iter *it) {
int n;
scanf("%d", &n);
inp(it, n);
}
+3 -1
View File
@@ -1,8 +1,10 @@
#include "rot8/rot8_mem.h"
#include "rot8/rot8.h"
#include <stdlib.h>
#include <string.h>
#include "rot8/rot8.h"
void rot8_mem_init(rot8_data **mem) {
*mem = malloc(sizeof(**mem) * MEM_SIZE);
memset(*mem, 0, sizeof(**mem) * MEM_SIZE);
+1 -1
View File
@@ -3,7 +3,7 @@
int main(int argc, char **argv) {
(void)argc;
(void)argv;
char ins[] = "-+-+-+-+-+-+-+-+>-+-+-+-+++++<[>[.-+.]<-+]";
char ins[] = "-+-+-+-+-+-+-+-+>,<[>[.-+.]<-+]";
run(ins, sizeof(ins) / sizeof(ins[0]) - 1);
return (0);
}