bfx
An interpreter, compiler, and REPL for brainfuck-like languages
Loading...
Searching...
No Matches
bfx.h File Reference
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Data Structures

struct  BFX_FileIndex
 Structure to represent an index in a file (or user input). More...
 
struct  BFX_Block
 Structure to represent a loop in the brainfuck program. This structure holds the start and end indices of a loop, allowing the interpreter to efficiently jump between matching brackets during execution. More...
 
struct  BFX
 Structure to store generic data for a brainfuck-like esolang interpreter. More...
 

Macros

#define BFX_DEFAULT_INPUT_MAX   1024
 Default maximum input size for the interpreter.
 
#define BFX_DEFAULT_TAPE_SIZE   30000
 Default tape size for the interpreter.
 
#define BFX_INITIAL_LOOP_SIZE   2048
 Default maximum loop count for the interpreter (scaled anyways if higher)
 
#define BFX_PROCEDURE_SIZE   128
 Default maximum procedure count for interpreter.
 
#define BFX_VERSION   "unknown"
 Version of the libbfx library.
 
#define BFX_DEFAULT_LANG   BFX_LANG_BRAINFUCK
 Default language for the interpreter/compiler.
 
#define BFX_DEFAULT_EOF_BEHAVIOR   BFX_EOF_BEHAVIOR_ZERO
 Default EOF behavior for the interpreter.
 
#define BFX_FLAG_DEBUG   1
 Interpreter: Debug mode.
 
#define BFX_FLAG_REPL   2
 Interpreter: REPL mode.
 
#define BFX_FLAG_DISABLE_SPECIAL_INSTRUCTIONS   4
 Interpreter: Disable special instructions.
 
#define BFX_FLAG_LANG_DATA_FLAGS   8
 lang_data contains flags for the given language
 
#define BFX_FLAG_GRAPHICS   16
 Interpreter: Enable graphics mode.
 
#define BFX_FLAG_SEPARATE_INPUT_AND_SOURCE    32
 Interpreter: Use input in source file separated by '!'.
 
#define BFX_FLAG_DEGREES   64
 Interpreter (Grin): Use degrees instead of radians.
 
#define BFX_ERROR(s)   fprintf(stderr, "libbfx: Error: %s\n", s);
 Error handling macro for the interpreter.
 
#define BFX_IN_DEBUG_MODE(b)   ((b).flags & BFX_FLAG_DEBUG)
 Check if the interpreter is in debug mode (arg is of type BFX)
 
#define BFX_IN_REPL_MODE(b)   ((b).flags & BFX_FLAG_REPL)
 Check if the interpreter is in REPL mode (arg is of type BFX)
 
#define BFX_SPECIAL_INSTRUCTIONS_ENABLED(b)   (!((b).flags & BFX_FLAG_DISABLE_SPECIAL_INSTRUCTIONS))
 Check if the interpreter has special instructions enabled (arg is of type BFX)
 

Enumerations

enum  BFX_EOFBehavior { BFX_EOF_BEHAVIOR_ZERO , BFX_EOF_BEHAVIOR_DECREMENT , BFX_EOF_BEHAVIOR_UNCHANGED }
 Behaviors for the interpreter when an EOF is encountered in input. More...
 
enum  BFX_Language {
  BFX_LANG_UNKNOWN , BFX_LANG_brainfuck , BFX_LANG_brainfork , BFX_LANG_pbrain ,
  BFX_LANG_grin , BFX_LANG_weave
}
 Language options for the interpreter/compiler. More...
 
enum  BFX_Error {
  BFX_SUCCESS = 0 , BFX_STACK_OVERFLOW_ERROR , BFX_STACK_UNDERFLOW_ERROR , BFX_SYNTAX_ERROR ,
  BFX_OUT_OF_MEMORY_ERROR , BFX_RUNTIME_ERROR
}
 

Functions

void bfx_free (BFX *)
 Frees the memory allocated for the BFX instance.
 
BFX_Error bfx_run_file (const char *, BFX *)
 Runs the program loaded from a file.
 
BFX_Error bfx_run_repl (BFX *)
 Runs the BFX interpreter in REPL (Read-Eval-Print Loop) mode.
 

Macro Definition Documentation

◆ BFX_DEFAULT_EOF_BEHAVIOR

#define BFX_DEFAULT_EOF_BEHAVIOR   BFX_EOF_BEHAVIOR_ZERO

Default EOF behavior for the interpreter.

◆ BFX_DEFAULT_INPUT_MAX

#define BFX_DEFAULT_INPUT_MAX   1024

Default maximum input size for the interpreter.

◆ BFX_DEFAULT_LANG

#define BFX_DEFAULT_LANG   BFX_LANG_BRAINFUCK

Default language for the interpreter/compiler.

◆ BFX_DEFAULT_TAPE_SIZE

#define BFX_DEFAULT_TAPE_SIZE   30000

Default tape size for the interpreter.

◆ BFX_ERROR

#define BFX_ERROR (   s)    fprintf(stderr, "libbfx: Error: %s\n", s);

Error handling macro for the interpreter.

◆ BFX_FLAG_DEBUG

#define BFX_FLAG_DEBUG   1

Interpreter: Debug mode.

◆ BFX_FLAG_DEGREES

#define BFX_FLAG_DEGREES   64

Interpreter (Grin): Use degrees instead of radians.

◆ BFX_FLAG_DISABLE_SPECIAL_INSTRUCTIONS

#define BFX_FLAG_DISABLE_SPECIAL_INSTRUCTIONS   4

Interpreter: Disable special instructions.

◆ BFX_FLAG_GRAPHICS

#define BFX_FLAG_GRAPHICS   16

Interpreter: Enable graphics mode.

◆ BFX_FLAG_LANG_DATA_FLAGS

#define BFX_FLAG_LANG_DATA_FLAGS   8

lang_data contains flags for the given language

◆ BFX_FLAG_REPL

#define BFX_FLAG_REPL   2

Interpreter: REPL mode.

◆ BFX_FLAG_SEPARATE_INPUT_AND_SOURCE

#define BFX_FLAG_SEPARATE_INPUT_AND_SOURCE    32

Interpreter: Use input in source file separated by '!'.

◆ BFX_IN_DEBUG_MODE

#define BFX_IN_DEBUG_MODE (   b)    ((b).flags & BFX_FLAG_DEBUG)

Check if the interpreter is in debug mode (arg is of type BFX)

◆ BFX_IN_REPL_MODE

#define BFX_IN_REPL_MODE (   b)    ((b).flags & BFX_FLAG_REPL)

Check if the interpreter is in REPL mode (arg is of type BFX)

◆ BFX_INITIAL_LOOP_SIZE

#define BFX_INITIAL_LOOP_SIZE   2048

Default maximum loop count for the interpreter (scaled anyways if higher)

◆ BFX_PROCEDURE_SIZE

#define BFX_PROCEDURE_SIZE   128

Default maximum procedure count for interpreter.

◆ BFX_SPECIAL_INSTRUCTIONS_ENABLED

#define BFX_SPECIAL_INSTRUCTIONS_ENABLED (   b)    (!((b).flags & BFX_FLAG_DISABLE_SPECIAL_INSTRUCTIONS))

Check if the interpreter has special instructions enabled (arg is of type BFX)

◆ BFX_VERSION

#define BFX_VERSION   "unknown"

Version of the libbfx library.

Enumeration Type Documentation

◆ BFX_EOFBehavior

Behaviors for the interpreter when an EOF is encountered in input.

Enumerator
BFX_EOF_BEHAVIOR_ZERO 

Set the current cell to zero.

BFX_EOF_BEHAVIOR_DECREMENT 

Decrement the current cell by 1.

BFX_EOF_BEHAVIOR_UNCHANGED 

Leave the current cell unchanged.

◆ BFX_Error

enum BFX_Error
Enumerator
BFX_SUCCESS 

No error.

BFX_STACK_OVERFLOW_ERROR 

Stack overflow error.

BFX_STACK_UNDERFLOW_ERROR 

Stack underflow error.

BFX_SYNTAX_ERROR 

Syntax error.

BFX_OUT_OF_MEMORY_ERROR 

Out of memory error.

BFX_RUNTIME_ERROR 

Runtime error.

◆ BFX_Language

Language options for the interpreter/compiler.

Enumerator
BFX_LANG_UNKNOWN 

Unknown language.

BFX_LANG_brainfuck 

brainfuck language

BFX_LANG_brainfork 

brainfork language

BFX_LANG_pbrain 

pbrain language

BFX_LANG_grin 

Grin language.

BFX_LANG_weave 

Weave language.

Function Documentation

◆ bfx_free()

void bfx_free ( BFX bfx)

Frees the memory allocated for the BFX instance.

This function frees the memory allocated for the BFX instance, including the program buffer, tape buffer, and loop buffer. It does not free the BFX instance itself.

Parameters
bfxPointer to the BFX instance.

◆ bfx_run_file()

BFX_Error bfx_run_file ( const char *  path,
BFX bfx 
)

Runs the program loaded from a file.

This function builds the loop structure for the BFX instance, then iterates through the program instructions, interpreting each one until the end of the program is reached.

Returns
BFX_Error The error code indicating the result of the operation.

◆ bfx_run_repl()

BFX_Error bfx_run_repl ( BFX bfx)

Runs the BFX interpreter in REPL (Read-Eval-Print Loop) mode.

This function continuously reads input from the user, appends it to the program, and interprets the instructions until the user terminates the program.