From 166b63c20d5da537b480c3d49b5e346ee70dad32 Mon Sep 17 00:00:00 2001 From: id101010 Date: Wed, 16 Nov 2016 16:06:26 +0100 Subject: [PATCH] Fixed bug in optparsing, implemented -e switch, updated Readme and Makefile --- Makefile | 2 +- README.md | 3 ++- bfckr.c | 27 +++++++++++++++++---------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index ef7dee6..20fdf96 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ HFILES=$(shell find . -name '*.h') STYLE=astyle --style=1tbs RUN=valgrind --leak-check=full DEBUG=gdb --args -ARGS="examples/pi.bf" +ARGS=-d -f "examples/pi.bf" all: build diff --git a/README.md b/README.md index 9ecff14..4b92433 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ Memory viewer: ------------------------------------------------------------ ~~~~ - +./bfckr -e "----[---->+<]>++." +A # Breakpoints diff --git a/bfckr.c b/bfckr.c index e2060d0..11b6e7f 100644 --- a/bfckr.c +++ b/bfckr.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -15,8 +16,8 @@ #include // Defines -#define MEMORY_SIZE 10000 -#define MAX_INPUT_SIZE 10000 +#define MEMORY_SIZE 1000 +#define MAX_INPUT_SIZE 1000 #define clear() printf("\033[H\033[J") // Struct which holds the brainfuck code, the bandtape and some helpervariables @@ -109,7 +110,12 @@ void print_memoryviewer(bf_code_t *bf) // print the adresses beneath the memory cells for(int i=(mp-7); i<(mp+8); i++) { - printf("%03d ", ((i<0 || i>MEMORY_SIZE) ? 0 : (mp+i))); + + if((i<0) || i>MEMORY_SIZE) { + printf("%03d ", MEMORY_SIZE+i); + } else { + printf("%03d ", i); + } } @@ -145,6 +151,11 @@ void bfuck_execute(bf_code_t *bf) int loop = 0; for(size_t i = bf->ip; bf->code[i] != 0; bf->ip=i++) { // where i is the instruction pointer + + if(bf->debug) { + bfuck_debugger(bf); + } + switch(bf->code[i]) { case '>': if(bf->mp >= MEMORY_SIZE) { // prevent overrun @@ -225,10 +236,6 @@ void bfuck_execute(bf_code_t *bf) // Do nothing break; } - - if(bf->debug) { - bfuck_debugger(bf); - } } } @@ -249,7 +256,7 @@ int main(int argc, char* argv[]) } // optparsing ahead - while((option = getopt(argc, argv, "hdef:")) >= 0) { + while((option = getopt(argc, argv, "hde:f:")) >= 0) { switch(option) { case 'h': // show help printf("Usage: %s [OPTION] [FILE]\n", argv[0]); @@ -260,8 +267,8 @@ int main(int argc, char* argv[]) exit(EXIT_SUCCESS); break; case 'e': // read input string - //printf ("Input code: \"%s\"\n", argv[2]); - sscanf(bf.code, "%s", argv[2]); + // copy optarg to the code register + memcpy(bf.code, optarg, strlen(optarg)); break; case 'f': // file input // try to open file