Working interpreter.

This commit is contained in:
id101010
2016-11-08 02:05:59 +01:00
parent d05f86963b
commit 38b6dd16d6
4 changed files with 163 additions and 0 deletions

13
LICENCE Normal file
View File

@@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
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.

26
Makefile Normal file
View File

@@ -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)

View File

@@ -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.

112
bfckr.c Normal file
View File

@@ -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<stdio.h>
#include<stdint.h>
#include<stdlib.h>
#include<errno.h>
#include<assert.h>
#include<ctype.h>
// 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);
}