Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added License and updated the code.

  • Loading branch information...
commit 1e8d8744de780a233e8a5207e24687f52bec00cd 1 parent 9fe0add
@fabianm authored
View
4 AUTHORS.markdown
@@ -1,4 +0,0 @@
-# Brainfuck
-## Authors
-------------
- Fabian M. mail.fabianm@gmail.com http://www.github.com/FabianM
View
13 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.
View
33 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.
View
4 samples/hello_world.bf
@@ -1,3 +1 @@
-#! /home/fabian/workspace/Brainfuck/Debug/Brainfuck
-
-++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
+ ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
View
1  samples/output.bf
@@ -1,2 +1 @@
-#! /home/fabian/workspace/Brainfuck/Debug/Brainfuck
+[+>-]<+.
View
66 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++;
@@ -59,7 +27,6 @@ void interpret(char *filename) {
break;
case '.':
putchar(tape[pointer]);
- fflush(stdout);
break;
case ',':
tape[pointer] = (int) getchar();
@@ -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;
}
-
Please sign in to comment.
Something went wrong with that request. Please try again.