diff --git a/include/rot8/rot8.h b/include/rot8/rot8.h index ea6482b..b4c2c5c 100644 --- a/include/rot8/rot8.h +++ b/include/rot8/rot8.h @@ -14,6 +14,8 @@ typedef uint8_t rot8_ins_size; // #define DATA_WIDTH (sizeof(rot8_data) * CHAR_BIT) #define UNIT_SIZE 8 // Just for test. +#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[(bc)]) + typedef enum { STP, // Step Tape Pointer,'>' BTP, // Back Tape Pointer,'<' diff --git a/include/rot8/rot8_io.h b/include/rot8/rot8_io.h index 038537f..27af6f8 100644 --- a/include/rot8/rot8_io.h +++ b/include/rot8/rot8_io.h @@ -6,4 +6,6 @@ void rot8_out_io(rot8_iter *it); void rot8_inp_io(rot8_iter *it); +void read_tape(const char *filename, char *tape); + #endif diff --git a/src/rot8_io.c b/src/rot8_io.c index 85feca2..31712c5 100644 --- a/src/rot8_io.c +++ b/src/rot8_io.c @@ -1,6 +1,9 @@ #include "rot8/rot8_io.h" #include +#include + +#include "rot8/rot8.h" void rot8_out_io(rot8_iter *it) { // putchar(out(it)); @@ -11,3 +14,16 @@ void rot8_inp_io(rot8_iter *it) { scanf("%d", &n); inp(it, n); } + +void read_tape(const char *filename, char *tape) { + char *tail = tape; + + FILE *fp = fopen(filename, "r"); + + char get; + while ((get = fgetc(fp)) != EOF) { + if (strchr("><+-[].,", get) != NULL) *tail = get; + tail++; + } + fclose(fp); +} diff --git a/tapes/erase_and_out.rot8 b/tapes/erase_and_out.rot8 new file mode 100644 index 0000000..ade6b7f --- /dev/null +++ b/tapes/erase_and_out.rot8 @@ -0,0 +1 @@ +,>-+-+-+-+-+-+-+-+<>[<[-]+.>-+]< \ No newline at end of file diff --git a/tests/vm_test.c b/tests/vm_test.c index 6bfaacb..21b7723 100644 --- a/tests/vm_test.c +++ b/tests/vm_test.c @@ -1,9 +1,14 @@ +#include +#include + #include "rot8/rot8.h" +#include "rot8/rot8_io.h" int main(int argc, char **argv) { (void)argc; - (void)argv; - char ins[] = ",>-+-+-+-+-+-+-+-+[<[-]+>-+]<"; - run(ins, sizeof(ins) / sizeof(ins[0]) - 1); + char *ins = (char *)malloc(sizeof(char) * INS_SIZE); + read_tape(argv[1], ins); + run(ins, strlen(ins) - 1); + free(ins); return (0); } \ No newline at end of file