From ca79377e3ea2201c076dea92ce001b65481af90e Mon Sep 17 00:00:00 2001 From: Veit Heller Date: Tue, 7 Oct 2014 13:29:01 +0200 Subject: [PATCH] Added documentation to nbfi --- nbfi/src/nbfi.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/nbfi/src/nbfi.c b/nbfi/src/nbfi.c index 9ae08c5..2e40ab7 100644 --- a/nbfi/src/nbfi.c +++ b/nbfi/src/nbfi.c @@ -1,39 +1,79 @@ #include #include +/*end instruction*/ #define END 0 +/*increment pointer instruction, equivalent to > */ #define INC_DP 1 +/*decrement pointer instruction, equivalent to < */ #define DEC_DP 2 +/*increment value instruction, equivalent to + */ #define INC_VAL 3 +/*decrement value instruction, equivalent to - */ #define DEC_VAL 4 +/*output value instruction, equivalent to . */ #define OUT 5 +/*input value instruction, equivalent to , */ #define IN 6 +/*jump forward instruction, equivalent to [ */ #define JMP_FWD 7 +/*jump back instruction, equivalent to ] */ #define JMP_BCK 8 +/*Maximum program size*/ #define PROGRAM_SIZE 1048576 +/*Maximum stack size*/ #define MAX_SIZE 5024291 +/*Maximum data size*/ #define DATA_SIZE 1638375 +/*Push macro, implements stack operation push*/ #define PUSH(A) (stack[sp++] = A) +/*Pop macro, implements stack operation pop*/ #define POP() (stack[--sp]) +/*Empty macro, implements stack operation check if empty*/ #define EMPTY() (sp == 0) +/*Full macro, implements stack operation check if full*/ #define FULL() (sp == MAX_SIZE) + +/** + * @brief die + * @param code -> the error code + * @param message -> the error message + * + * Lets the program die and emits an error message. + */ static inline void die(int code, const char* message){ fprintf(stderr, "%s", message); exit(code); } +/** + * @brief An instruction struct + * + * Consists of operator and operand, + * the easiest way of abstracting + * an instruction. + */ struct instruction{ - unsigned int operator; - unsigned int operand; + unsigned int operator; ///< Operator + unsigned int operand; ///< Operand }; +/*The program as an array of structs*/ static struct instruction program[PROGRAM_SIZE]; +/*The stack*/ static unsigned int stack[MAX_SIZE]; +/*The stack pointer*/ static unsigned long sp = 0; +/** + * @brief compile + * @param fp -> the file + * + * Compiles a file to bytecode and saves that within program. + */ void compile(FILE* fp){ unsigned int pc = 0, jmp_pc; int ip; @@ -74,6 +114,11 @@ void compile(FILE* fp){ program[pc].operator = END; } +/** + * @brief execute + * + * Executes the bytecode within program. + */ void execute(){ unsigned int data[DATA_SIZE], pc = 0; unsigned int ptr = DATA_SIZE; @@ -96,6 +141,13 @@ void execute(){ die(127, "nbfi:execute: Program used up too much memory.\n"); } +/* + * @brief main + * @param argc -> argument counter + * @param argv -> argument vector + * + * compiles and executes brainfuck code from a file. + */ int main(int argc, const char * argv[]){ FILE *fp; if(argc != 2)