Skip to content

Commit

Permalink
Merge pull request #7 from aliclubb/master
Browse files Browse the repository at this point in the history
I forgot this one.
  • Loading branch information
fabianishere committed Sep 22, 2012
2 parents 1344248 + 38a0bb3 commit 3e9161b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 70 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG
@@ -0,0 +1,10 @@
Version 0.2
===========
Supports (in theory) many brainfuck programs already out there (But not LostKingdom.bf)
Added interactive console mode (Thanks to aliclubb)
Added Makefile and UNIX man pages
Optimised code slightly

Version 0.1
===========
First release
24 changes: 7 additions & 17 deletions Makefile
Expand Up @@ -2,38 +2,28 @@ CC = gcc
OS := $(shell uname)

all:
mkdir bin
mkdir -p bin
ifeq ($(OS), Darwin)
$(CC) -o bin/brainfuck src/*
$(CC) -O3 -Wall -o bin/brainfuck src/*
endif
ifeq ($(OS), Linux)
$(CC) -o bin/brainfuck src/*
$(CC) -O3 -Wall -o bin/brainfuck src/*
endif
ifeq ($(OS), CYGWIN_NT-5.1)
$(CC) -c src/* -o bin/brainfuck
$(CC) -O3 -Wall -c src/* -o bin/brainfuck
endif
no_interactive:
ifeq ($(OS), Darwin)
$(CC) -o src/brainfuck src/brainfuck.c
endif
ifeq ($(OS), Linux)
$(CC) -o src/brainfuck src/brainfuck.c
endif
ifeq ($(OS), CYGWIN_NT-5.1)
$(CC) -c src/brainfuck.c -o src/brainfuck
endif
install:
cp bin/brainfuck /usr/local/bin/brainfuck
ifeq ($(OS), Linux)
cp man/brainfuck_linux.1 /usr/local/man/man1/brainfuck.1
cp man/brainfuck_linux.1 /usr/local/share/man/man1/brainfuck.1
endif
ifeq ($(OS), Darwin)
cp man/brainfuck_darwin.1 /usr/local/man/man1/brainfuck.1
cp man/brainfuck_darwin.1 /usr/local/share/man/man1/brainfuck.1
endif
clean:
rm -f src/*.o
rm -r bin/

uninstall:
rm -f /usr/local/bin/brainfuck
rm -f /usr/local/man/man1/brainfuck.1
rm -f /usr/local/share/man/man1/brainfuck.1
10 changes: 7 additions & 3 deletions README.markdown
Expand Up @@ -3,7 +3,7 @@ Brainfuck
A lightweight, embeddable Brainfuck interpreter written in C.

## Usage
brainfuck [-if] <filename>
brainfuck [-fi] <filename>

## License
See LICENSE file.
Expand All @@ -15,8 +15,12 @@ See LICENSE file.
## Notice
This Brainfuck interpreter is tested on:

* Ubuntu 11.10 x86_32
* Ubuntu 11.10
* Kubuntu 11.10
* Xubuntu 11.10
* Mac OS X 10.6.8 x86_64
* Mac OS X 10.5.8 PowerPC
* Mac OS X 10.4.11 PowerPC
* Mac OS X 10.3.9 PowerPC
* Mac OS X 10.3.9 PowerPC
* Windows XP (using tcc)
* Windows 7 (using tcc)
14 changes: 7 additions & 7 deletions man/brainfuck_darwin.1
Expand Up @@ -14,7 +14,7 @@
.Nd Brainfuck interpreter
.Sh SYNOPSIS \" Section Header - required - don't modify
.Nm
.Op Fl if \" [-if]
.Op Fl fi \" [-fi]
.Op Fl f Ar filename \" [-f path]
.Sh DESCRIPTION \" Section Header - required - don't modify
This is a brainfuck interpreter written in C.
Expand Down Expand Up @@ -42,18 +42,18 @@ If the byte at the data pointer is nonzero, then instead of moving the instructi
.El
.Sh FLAGS
.Bl -tag -width -indent \" Begins a tagged list
.It -i \" Each item preceded by .It macro
Interactive mode
.It -f
.It -f \" Each item preceded by .It macro
File mode
.It -i
Interactive mode
.El \" Ends the list
.Pp
.Sh OPTIONS
.Bl -tag -width -indent \" Differs from above in tag removed
.It Fl i \"-a flag as a list item
Enters the brainfuck interactive console. ^C to exit.
.It Fl f
.It Fl f \"-a flag as a list item
Specify a file containing brainfuck code to execute.
.It Fl i
Enters the brainfuck interactive console. Press ^C to exit.
.El \" Ends the list
.Pp
.\" .Sh ENVIRONMENT \" May not be needed
Expand Down
14 changes: 7 additions & 7 deletions man/brainfuck_linux.1
Expand Up @@ -14,7 +14,7 @@
.Nd Brainfuck interpreter
.Sh SYNOPSIS \" Section Header - required - don't modify
.Nm
.Op Fl if \" [-if]
.Op Fl fi \" [-fi]
.Op Fl f Ar filename \" [-f path]
.Sh DESCRIPTION \" Section Header - required - don't modify
This is a brainfuck interpreter written in C.
Expand Down Expand Up @@ -42,18 +42,18 @@ If the byte at the data pointer is nonzero, then instead of moving the instructi
.El
.Sh FLAGS
.Bl -tag -width -indent \" Begins a tagged list
.It -i \" Each item preceded by .It macro
Interactive mode
.It -f
.It -f \" Each item preceded by .It macro
File mode
.It -i
Interactive mode
.El \" Ends the list
.Pp
.Sh OPTIONS
.Bl -tag -width -indent \" Differs from above in tag removed
.It Fl i \"-a flag as a list item
Enters the brainfuck interactive console. ^C to exit.
.It Fl f
.It Fl f \"-a flag as a list item
Specify a file containing brainfuck code to execute.
.It Fl i
Enters the brainfuck interactive console. Press ^C to exit.
.El \" Ends the list
.Pp
.\" .Sh ENVIRONMENT \" May not be needed
Expand Down
2 changes: 1 addition & 1 deletion samples/hello_world.bf
@@ -1 +1 @@
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
3 changes: 2 additions & 1 deletion src/brainfuck.c
@@ -1,5 +1,5 @@
/* This is the brainfuck 'backend'. It does all the interpretation :) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef BRAINFUCK_H
#include "brainfuck.h"
Expand Down Expand Up @@ -58,4 +58,5 @@ void brainfuck_eval(char chars[]) {
break;
}
}
putchar(10);
}
51 changes: 17 additions & 34 deletions src/brainfuck_shell.c
@@ -1,19 +1,12 @@
#include "brainfuck.h"
#include <stdio.h>
#include <stdarg.h>

#if defined __WIN32__ || defined _WIN32_ || defined _WIN32
#include <time.h>
#include <windows.h>
#endif

#if defined __APPLE__ || defined __unix__ || defined unix || defined _unix
#include <unistd.h>
#endif

#include <stdlib.h>
#include <string.h>

#ifdef __APPLE__ || defined __unix__ || defined unix || defined _unix || defined __unix
#include <unistd.h>
#endif
/* Read the file and pass it to the brainfuck_eval() function */
void brainfuck_file(char filename[]) {
FILE *file;
char c;
Expand All @@ -29,41 +22,31 @@ void brainfuck_file(char filename[]) {
size = ftell(file);
fseek(file, 0, SEEK_SET);
char chars[size];
/* Strange fix required in order to run most brainfuck programs... */
chars[pointer++] = ' ';
/* Place each character from the file into the array */
while ((c = fgetc(file)) != EOF)
chars[pointer++] = (char) c;
fclose(file);
/* Run the code */
brainfuck_eval(chars);
}

int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: brainfuck [-if] <filename>\n");
printf("Usage: brainfuck [-fi] <filename>\n");
return EXIT_FAILURE;
} else if(strcmp(argv[1], "-i") == 0) {
printf("Entering interactive mode...\n");
#if defined __APPLE__ || defined __unix__ || defined unix || defined _unix
fflush(stdout);
#endif
#if !defined __WIN32__ || !defined _WIN32_ || !defined _WIN32
sleep(1);
#endif
#if !defined __WIN32__ || !defined _WIN32_ || !defined _WIN32
sleep(1);
#endif
printf("Welcome to the Brainfuck Interpreter!");
#if defined __APPLE__ || defined __unix__ || defined unix || defined _unix
fflush(stdout);
#endif
printf("Welcome to the Brainfuck Interpreter v0.2!\n");
char c;
int pointer = 0;
for(;;) {
printf("\nbrainfuck> ");
char c;
int pointer = 0;
/* Put every character in character array */
int size = 1048576;
printf("brainfuck> ");
int size = 36000;
char chars[size];
c = ' ';
chars[pointer++] = (char) c;
while ((c = getchar()) != '\n')
chars[pointer++] = ' ';
while ((c = getchar()) != 10)
chars[pointer++] = (char) c;
/* Run the code */
brainfuck_eval(chars);
Expand All @@ -78,7 +61,7 @@ int main(int argc, char *argv[]) {
brainfuck_file(argv[2]);
}
} else {
printf("Error: Invalid command line option!\nUsage: brainfuck [-if] <filename>\n\t-i Interactive Mode\n\t-f <filename>\n");
printf("Error: Invalid command line option!\nUsage: brainfuck [-fi] <filename>\n\t-f <filename>\n\t-i Interactive Mode\n");
}
return EXIT_SUCCESS;
}
Expand Down

0 comments on commit 3e9161b

Please sign in to comment.