diff --git a/bfckr.c b/bfckr.c index 7815530..8107bff 100644 --- a/bfckr.c +++ b/bfckr.c @@ -66,12 +66,46 @@ void die(const char *message) /* Prints the bf source at the current location */ void print_sourceviewer(bf_code_t *bf) { - printf("\nSource viewer: \n" + printf("\nSource viewer:\n"); + printf("-------------------------------------------------------------\n"); // 62 dashes + + if(bf->ip <= 30) { // if there aren't enough instruction characters to print + for(int i = bf->ip; i >= 0; i--) { // print as much instructions as possible + printf("%c", bf->code[i]); + } + for(int i = 30-(bf->ip); i >= 0; i--) { // then fill with whitespaces + printf(" "); + } + } + + if(bf->ip > 30) { // if there are enough instruction characters + for(int i = 30; i >= 0; i--) { // print 30 instruction chars before ip + printf("%c", bf->code[i]); + } + } + + // print 30 instructions begining at ip + for(size_t i = bf->ip; i < bf->ip + 29; i++) { + if(bf->code[i] == '\n') { + printf(" "); + } else if(bf->code[i] == NULL) { + printf(" "); + } else { + printf("%c", bf->code[i]); + } + } + printf("\n"); + printf(" ^ \n"); + printf(" ip=%d \n", bf->ip); + printf("-------------------------------------------------------------\n"); + + /*printf("\nSource viewer: \n" "-----------------------------------------------------------\n" "_____________________________>+++++++++++++++[<+>>>>>>>>+++\n" " ^ \n" " ip=0 \n" "-----------------------------------------------------------\n"); // just to get the idea ... + */ } /* Prints memory information at the current memory location */ @@ -79,6 +113,8 @@ void print_memoryviewer(bf_code_t *bf) { // int pointerlocation = (p - memory)*sizeof(*memory); // find the arrayindex at which the pointer is pointing + + printf("\nMemory viewer: \n" "-----------------------------------------------------------\n" "000 000 000 000 000 000 001 001 000 000 000 000 000 000 000\n" @@ -91,7 +127,7 @@ void print_memoryviewer(bf_code_t *bf) /* Pauses the program flow and prints information */ void bfuck_debugger(bf_code_t *bf) //char *bf_source_input, int instructionpointer) { - clear(); // clear terminal + //clear(); // clear terminal printf("[s]: single step [c]: continue\n"); @@ -154,7 +190,7 @@ void bfuck_execute(bf_code_t *bf) // jump forward to the command after the next ] loop = 1; while(loop > 0) { // count nested loops and make sure to get the matching ] - bf->ip = i++; + bf->ip = i++; // save current instruction pointer and increment it if(bf->code[i] == '[') { loop++; } @@ -172,7 +208,7 @@ void bfuck_execute(bf_code_t *bf) // jump back to the command after the matching [ loop = 1; while(loop > 0) { // count nested loops and make sure to get the matching [ - bf->ip = i--; + bf->ip = i--; // save current instruction pointer and increment it if(bf->code[i] == '[') { loop--; } @@ -226,6 +262,7 @@ int main(int argc, char* argv[]) // close file after reading fclose(fp); + bfuck_debugger(&bf); // try to interpret it bfuck_execute(&bf); diff --git a/examples/pi.bf b/examples/pi.bf index 63cdf79..2e38c9d 100644 --- a/examples/pi.bf +++ b/examples/pi.bf @@ -1,4 +1,4 @@ -> +++++ +++++ +++++ (15 digits) +> +++++ +++++ +++++ [<+>>>>>>>>++++++++++<<<<<<<-]>+++++[<+++++++++>-]+>>>>>>+[<<+++[>>[-<]<[>]<-]>> [>+>]<[<]>]>[[->>>>+<<<<]>>>+++>-]<[<<<<]<<<<<<<<+[->>>>>>>>>>>>[<+[->>>>+<<<<]>