Working on sourceviewer
This commit is contained in:
45
bfckr.c
45
bfckr.c
@@ -66,12 +66,46 @@ void die(const char *message)
|
|||||||
/* Prints the bf source at the current location */
|
/* Prints the bf source at the current location */
|
||||||
void print_sourceviewer(bf_code_t *bf)
|
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"
|
"_____________________________>+++++++++++++++[<+>>>>>>>>+++\n"
|
||||||
" ^ \n"
|
" ^ \n"
|
||||||
" ip=0 \n"
|
" ip=0 \n"
|
||||||
"-----------------------------------------------------------\n"); // just to get the idea ...
|
"-----------------------------------------------------------\n"); // just to get the idea ...
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prints memory information at the current memory location */
|
/* 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
|
// int pointerlocation = (p - memory)*sizeof(*memory); // find the arrayindex at which the pointer is pointing
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
printf("\nMemory viewer: \n"
|
printf("\nMemory viewer: \n"
|
||||||
"-----------------------------------------------------------\n"
|
"-----------------------------------------------------------\n"
|
||||||
"000 000 000 000 000 000 001 001 000 000 000 000 000 000 000\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 */
|
/* Pauses the program flow and prints information */
|
||||||
void bfuck_debugger(bf_code_t *bf) //char *bf_source_input, int instructionpointer)
|
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");
|
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 ]
|
// jump forward to the command after the next ]
|
||||||
loop = 1;
|
loop = 1;
|
||||||
while(loop > 0) { // count nested loops and make sure to get the matching ]
|
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] == '[') {
|
if(bf->code[i] == '[') {
|
||||||
loop++;
|
loop++;
|
||||||
}
|
}
|
||||||
@@ -172,7 +208,7 @@ void bfuck_execute(bf_code_t *bf)
|
|||||||
// jump back to the command after the matching [
|
// jump back to the command after the matching [
|
||||||
loop = 1;
|
loop = 1;
|
||||||
while(loop > 0) { // count nested loops and make sure to get the matching [
|
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] == '[') {
|
if(bf->code[i] == '[') {
|
||||||
loop--;
|
loop--;
|
||||||
}
|
}
|
||||||
@@ -226,6 +262,7 @@ int main(int argc, char* argv[])
|
|||||||
// close file after reading
|
// close file after reading
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
|
bfuck_debugger(&bf);
|
||||||
// try to interpret it
|
// try to interpret it
|
||||||
bfuck_execute(&bf);
|
bfuck_execute(&bf);
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
> +++++ +++++ +++++ (15 digits)
|
> +++++ +++++ +++++
|
||||||
|
|
||||||
[<+>>>>>>>>++++++++++<<<<<<<-]>+++++[<+++++++++>-]+>>>>>>+[<<+++[>>[-<]<[>]<-]>>
|
[<+>>>>>>>>++++++++++<<<<<<<-]>+++++[<+++++++++>-]+>>>>>>+[<<+++[>>[-<]<[>]<-]>>
|
||||||
[>+>]<[<]>]>[[->>>>+<<<<]>>>+++>-]<[<<<<]<<<<<<<<+[->>>>>>>>>>>>[<+[->>>>+<<<<]>
|
[>+>]<[<]>]>[[->>>>+<<<<]>>>+++>-]<[<<<<]<<<<<<<<+[->>>>>>>>>>>>[<+[->>>>+<<<<]>
|
||||||
|
|||||||
Reference in New Issue
Block a user