diff --git a/.clang-format b/.clang-format index 67c0c8b..67d549f 100644 --- a/.clang-format +++ b/.clang-format @@ -1,3 +1,4 @@ -BasedOnStyle: LLVM +BasedOnStyle: Google IndentWidth: 4 -UseTab: Always \ No newline at end of file +UseTab: Always +PointerAlignment: Right \ No newline at end of file diff --git a/include/rot8/rot8.h b/include/rot8/rot8.h index fcd676b..07ed6e9 100644 --- a/include/rot8/rot8.h +++ b/include/rot8/rot8.h @@ -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; diff --git a/src/rot8.c b/src/rot8.c index ee6d17d..634f753 100644 --- a/src/rot8.c +++ b/src/rot8.c @@ -1,10 +1,11 @@ #include "rot8/rot8.h" // #include "rot8_error.h" -#include "rot8/rot8_io.h" -#include "rot8/rot8_mem.h" #include #include +#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; diff --git a/src/rot8_io.c b/src/rot8_io.c index 1882f51..85feca2 100644 --- a/src/rot8_io.c +++ b/src/rot8_io.c @@ -1,8 +1,13 @@ #include "rot8/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()); } +void rot8_inp_io(rot8_iter *it) { + int n; + scanf("%d", &n); + inp(it, n); +} diff --git a/src/rot8_mem.c b/src/rot8_mem.c index 0842005..3189f62 100644 --- a/src/rot8_mem.c +++ b/src/rot8_mem.c @@ -1,8 +1,10 @@ #include "rot8/rot8_mem.h" -#include "rot8/rot8.h" + #include #include +#include "rot8/rot8.h" + void rot8_mem_init(rot8_data **mem) { *mem = malloc(sizeof(**mem) * MEM_SIZE); memset(*mem, 0, sizeof(**mem) * MEM_SIZE); diff --git a/tests/vm_test.c b/tests/vm_test.c index 956ad9c..4f50241 100644 --- a/tests/vm_test.c +++ b/tests/vm_test.c @@ -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); } \ No newline at end of file