Fixed mistake

This commit is contained in:
id101010
2016-11-10 00:43:38 +01:00
parent 4e62be46f7
commit 619a559f06
2 changed files with 20 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
CC=gcc CC=gcc
CFLAGS=-std=c99 -Wall -O2 -static -g CFLAGS=-std=c99 -Wall -O0 -static
CLIBS=-lm CLIBS=-lm
PRGNAME=bfckr PRGNAME=bfckr
CFILES=$(shell find . -name '*.c') CFILES=$(shell find . -name '*.c')
@@ -9,17 +9,24 @@ RUN=valgrind --leak-check=full
DEBUG=gdb --args DEBUG=gdb --args
ARGS="examples/rot13.bf" ARGS="examples/rot13.bf"
all: build run all: build
clean: clean:
$(STYLE) $(CFILES) $(STYLE) $(CFILES)
rm -f *.o rm -f *.o
rm -f $(PRGNAME) rm -f $(PRGNAME)
build: build:
$(CC) $(CFLAGS) $(CLIBS) -o $(PRGNAME) -Isrc $(CFILES) $(CC) $(CFLAGS) $(CLIBS) -o $(PRGNAME) -Isrc $(CFILES)
debug:
$(CC) $(CFLAGS) -g $(CLIBS) -o $(PRGNAME) -Isrc $(CFILES)
run_debug:
$(DEBUG) ./$(PRGNAME) $(ARGS)
run: run:
./$(PRGNAME) $(ARGS) ./$(PRGNAME) $(ARGS)
memtest: memtest:
$(RUN) ./$(PRGNAME) $(ARGS) $(RUN) ./$(PRGNAME) $(ARGS)
debug:
$(DEBUG) ./$(PRGNAME) $(ARGS)

17
bfckr.c
View File

@@ -30,7 +30,7 @@ void die(const char *message)
if(errno) { if(errno) {
perror(message); perror(message);
} else { } else {
printf("[:-(] Error: %s\n", message); printf("[:(] Error: %s\n", message);
} }
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -71,8 +71,7 @@ void bfuck_parser(char *input)
if(*p == 0) { // if the byte at the data pointer is zero if(*p == 0) { // if the byte at the data pointer is zero
// jump forward to the command after the next ] // jump forward to the command after the next ]
loop = 1; loop = 1;
// jump back to the command after the matching [ while(loop > 0) { // count nested loops and make sure to get the matching ]
while(loop > 0) {
i++; i++;
if(input[i] == '[') { if(input[i] == '[') {
loop++; loop++;
@@ -88,9 +87,9 @@ void bfuck_parser(char *input)
case ']': case ']':
if(*p != 0) { // if the byte at the data pointer is nonzero if(*p != 0) { // if the byte at the data pointer is nonzero
loop = 1;
// jump back to the command after the matching [ // jump back to the command after the matching [
while(loop > 0) { loop = 1;
while(loop > 0) { // count nested loops and make sure to get the matching [
i--; i--;
if(input[i] == '[') { if(input[i] == '[') {
loop--; loop--;
@@ -129,15 +128,17 @@ int main(int argc, char* argv[])
die("Couldn't open file."); die("Couldn't open file.");
} }
// read the whole file and store it in the input buffer // read the file and store it in the input buffer
while((c = getc(fp)) != EOF) { while((c = getc(fp)) != EOF) {
input[i++] = c; input[i++] = c;
} }
// close file after reading
fclose(fp);
// try to interpret it // try to interpret it
bfuck_parser(input); bfuck_parser(input);
// close and exit // exit
fclose(fp);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }