Skip to content

Commit

Permalink
Added License and updated the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianishere committed Apr 17, 2012
1 parent 9fe0add commit 1e8d874
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 70 deletions.
4 changes: 0 additions & 4 deletions AUTHORS.markdown

This file was deleted.

13 changes: 13 additions & 0 deletions LICENSE
@@ -0,0 +1,13 @@
Copyright 2012 Fabian M.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
33 changes: 9 additions & 24 deletions README.markdown
@@ -1,27 +1,12 @@
# Brainfuck
Brainfuck
===========
Lightweight Brainfuck interpreter written in C.

------------
## License
See LICENSE file.

Lightweight, open-source Brainfuck interpreter written in C.
## Authors
Fabian M. mail.fabianm@gmail.com http://www.github.com/FabianM

Notice
----------------
Written on a machine running Ubuntu (Linux). Not tested on other platforms yet.

Brainfuck
---------
Urban Müller created brainfuck in 1993 with the intention of designing a language which could be implemented with the smallest possible compiler.

### Commands
The eight language commands, each consisting of a single character:

| Character | Meaning |
|:----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| > | increment the data pointer (to point to the next cell to the right). |
| < | decrement the data pointer (to point to the next cell to the left). |
| + | increment (increase by one) the byte at the data pointer. |
| - | decrement (decrease by one) the byte at the data pointer. |
| . | output a character, the ASCII value of which being the byte at the data pointer. |
| , | accept one byte of input, storing its value in the byte at the data pointer. |
| [ | if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command*.|
| ] | if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command*.|
## Notice
Written on a machine running Ubuntu (Linux). Not tested on other platforms yet.
4 changes: 1 addition & 3 deletions samples/hello_world.bf
@@ -1,3 +1 @@
#! /home/fabian/workspace/Brainfuck/Debug/Brainfuck
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
1 change: 0 additions & 1 deletion samples/output.bf
@@ -1,2 +1 @@
#! /home/fabian/workspace/Brainfuck/Debug/Brainfuck
+[+>-]<+.
66 changes: 28 additions & 38 deletions src/interpreter.c → src/brainfuck.c
@@ -1,49 +1,17 @@
// Copyright (c) 2012, Fabian M.
// Please see the AUTHORS file for other authors. All rights reserved.

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

#define VERSION "1.2"
#define MAX_CELLS 65536

/* Interpreter */
void interpret(char *filename) {
/* Variable declaration */
void brainfuck_eval(char chars[]) {
char c;

FILE *file;

int pointer = 0;
int size = 0;
int char_pointer = 0;
int loop = 0;
int tape[MAX_CELLS];

/* Get the size of the file */
file = fopen(filename, "r");
if (file == NULL)
exit(EXIT_FAILURE);

fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
char chars[size];

/* Put every character in character array */
while ((c = fgetc(file)) != EOF)
chars[pointer++] = (char) c;


/* Close file */
fclose(file);

/* Set the pointer back to 0 */
pointer = 0;

/* Loop through all character in the character array */
while (char_pointer++ < sizeof(chars))
while (char_pointer++ < strlen(chars)) {
switch (chars[char_pointer]) {
case '>':
pointer++;
Expand All @@ -59,7 +27,6 @@ void interpret(char *filename) {
break;
case '.':
putchar(tape[pointer]);
fflush(stdout);
break;
case ',':
tape[pointer] = (int) getchar();
Expand Down Expand Up @@ -91,13 +58,36 @@ void interpret(char *filename) {
while (chars[++char_pointer] != '\n');
break;
}
}
}
void brainfuck_file(char filename[]) {

/* Main */
FILE *file;
char c;
int pointer = 0;
int size = 0;

/* Get the size of the file */
file = fopen(filename, "r");
if (file == NULL) {
printf("Failed to open file.\n");
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
char chars[size];
/* Put every character in character array */
while ((c = fgetc(file)) != EOF)
chars[pointer++] = (char) c;
/* Close file */
fclose(file);
/* Run the code */
brainfuck_eval(chars);
}
int main(int argc, char *argv[]) {
if (argc < 2)
return EXIT_FAILURE;
interpret(argv[1]);
brainfuck_file(argv[1]);
return EXIT_SUCCESS;
}

0 comments on commit 1e8d874

Please sign in to comment.