diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..5c93f45 --- /dev/null +++ b/LICENCE @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..647ef21 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +CC=gcc +CFLAGS=-std=c99 -Wall -O2 -static +CLIBS=-lm +PRGNAME=bfckr +CFILES=$(shell find . -name '*.c') +HFILES=$(shell find . -name '*.h') +STYLE=astyle --style=1tbs +RUN=valgrind --leak-check=full +DEBUG=gdb --args +ARGS="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." + + +all: build run + +clean: + $(STYLE) $(CFILES) + rm -f *.o + rm -f $(PRGNAME) +build: + $(CC) $(CFLAGS) $(CLIBS) -o $(PRGNAME) -Isrc $(CFILES) +run: + ./$(PRGNAME) $(ARGS) +memtest: + $(RUN) ./$(PRGNAME) $(ARGS) +debug: + $(DEBUG) ./$(PRGNAME) diff --git a/README.md b/README.md index c6d010c..0e975e5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,14 @@ # bfckr A little brainfuck interpreter written in C. + +# How to run +* Type "make" +* Read [this](https://en.wikipedia.org/wiki/Brainf**k) +* Create your own brainfuck software +* ./bfckr "$yourstuff" + +# Licence +Copyright © 2016 Aaron aaron@duckpond.ch +This work is free. You can redistribute it and/or modify it under the +terms of the Do What The Fuck You Want To Public License, Version 2, +as published by Sam Hocevar. See the COPYING file for more details. diff --git a/bfckr.c b/bfckr.c new file mode 100644 index 0000000..161e244 --- /dev/null +++ b/bfckr.c @@ -0,0 +1,112 @@ +/* This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include +#include +#include +#include + +// Amount of memory on the band tape +#define MEMORY_SIZE 10000 + +// Memory initialized with zeros and its pointer +uint8_t memory[MEMORY_SIZE] = {0}; +uint8_t *p = memory; + +// Prototypes +void bfuck_parser(char *input); + +// Error handler +void die(const char *message) +{ + if(errno) { + perror(message); + } else { + printf("[:-(] Error: %s\n", message); + } + + exit(EXIT_FAILURE); +} + +// Parses and executes a brainfuck expression +void bfuck_parser(char *input) +{ + //printf("%s\n", input); + + char curr; + uint16_t loop; + + for(uint16_t i = 0; input[i] != 0; i++) { + curr = input[i]; + + switch(curr) { + case '>': + ++p; + break; + + case '<': + --p; + break; + + case '+': + ++*p; + break; + + case '-': + --*p; + break; + + case '.': + putchar(*p); + break; + + case ',': + *p = getchar(); + break; + + case '[': + continue; + break; + + case ']': + if(*p) { + loop = 1; + while(loop) { + curr = input[--i]; + if(curr == '[') { + loop--; + } + if(curr == ']') { + loop++; + } + } + } + break; + + case ' ': + break; + + default: + printf("%c", *p); + die("Invalid operant!"); + break; + } + } +} + +int main(int argc, char* argv[]) +{ + if(argc < 2) { + die("Need more arguments."); + } + + bfuck_parser(argv[1]); + + exit(EXIT_SUCCESS); +}