brainfuck
0.1
A full fledged brainfuck compiler
|
Header for extern functions. More...
#include <stdio.h>
Go to the source code of this file.
Macros | |
#define | COMPILE (1 << 0) |
Macros for determining state of the program. | |
#define | ASSEMBLE (1 << 1) |
#define | LINK (1 << 2) |
#define | PIPE_OUT (1 << 3) |
#define | PIPE_IN (1 << 4) |
#define | VERBOSE (1 << 5) |
Functions | |
int | is_uint (const char *str) |
Checks if the given string can represent a positive integer. More... | |
void | print_help (void) |
Prints the help message when "-h" or "--help" are command line arguments. | |
int | my_getopt (int argc, char **argv, int *stack_size, int *array_size, char **output_file, char **input_file, int *mode) |
Custom getopt function. More... | |
int | compile (FILE *in, FILE *out, int stack_size, int array_size) |
compiler function More... | |
char * | remove_extenstion (const char *str) |
Removes the file extension from a string, if there is one at all. More... | |
char * | get_as (int mode, const char *name, const char *original, char **ret) |
Gets the name of the assembly code file. More... | |
char * | get_obj (int mode, const char *name, const char *original, char **ret) |
Gets the name of the assembled file. More... | |
int | get_as_file (int mode, FILE **out, const char *name, const char *input_name) |
Gets the file pointer to the assembly code file. More... | |
int | close_as_file (int mode, FILE *f) |
Closes the file pointer to the assembly source file. More... | |
int | assemble (int mode, const char *as_file, const char *obj_file) |
Wrapper around assembling the assembly code. More... | |
int | get_in (int mode, const char *input_file, FILE **in) |
Gets the source code file pointer and deals with IO errors. More... | |
int assemble | ( | int | mode, |
const char * | as_file, | ||
const char * | obj_file | ||
) |
Wrapper around assembling the assembly code.
This also removes the assembly code file if we aren't linking the object code afterwards.
mode | The state of the program |
as_file | The name of the assembly source code file |
obj_file | The name of the object code file that will be generated |
Definition at line 289 of file common.c.
Referenced by main().
int close_as_file | ( | int | mode, |
FILE * | f | ||
) |
Closes the file pointer to the assembly source file.
Its magic is in the fact that if this was a process the compier was writing to, it waits for the process to end using pclose(3), but otherwise using fclose(3) to close the file.
mode | The state of the program |
f | The file pointer to close |
Definition at line 267 of file common.c.
Referenced by main().
int compile | ( | FILE * | in, |
FILE * | out, | ||
int | stack_size, | ||
int | array_size | ||
) |
compiler function
in | Where to get the source code from |
out | Where to print the assembly code |
stack_size | The size of the loop stack |
array_size | The size of the brainfuck internal array |
Definition at line 28 of file compiler.c.
References BUF_SIZE.
Referenced by main().
char* get_as | ( | int | mode, |
const char * | name, | ||
const char * | original, | ||
char ** | ret | ||
) |
Gets the name of the assembly code file.
mode | The state of the program |
name | The name of the file set as output by the -o option |
original | The name of the original file that is being compiled A pointer to the malloc'd name of the assembly code file, or NULL if the compiler is piping out the assembly code |
Definition at line 153 of file common.c.
References remove_extenstion().
Referenced by get_as_file(), and main().
int get_as_file | ( | int | mode, |
FILE ** | out, | ||
const char * | name, | ||
const char * | input_name | ||
) |
Gets the file pointer to the assembly code file.
If -pipe is specified and if the assembly will be assembled, the file pointer will be pointing to the piped process assembling the code concurrently.
mode | The state of the program |
out | The pointer to the file pointer to return |
name | The name of the file set as output by the -o option |
input_name | The name of the original file that is being compiled |
Definition at line 217 of file common.c.
References get_as(), and get_obj().
Referenced by main().
int get_in | ( | int | mode, |
const char * | input_file, | ||
FILE ** | in | ||
) |
char* get_obj | ( | int | mode, |
const char * | name, | ||
const char * | original, | ||
char ** | ret | ||
) |
Gets the name of the assembled file.
mode | The state of the program |
name | The name of the file set as output by the -o option |
original | The name of the original file that is being compiled |
ret | A pointer to the malloc'd name of the assembled file if no errors occur The malloc'd name of the assembled file if no errors occur |
Definition at line 184 of file common.c.
References remove_extenstion().
Referenced by get_as_file(), and main().
int is_uint | ( | const char * | str | ) |
Checks if the given string can represent a positive integer.
str | The string to check |
Definition at line 23 of file common.c.
Referenced by my_getopt().
int my_getopt | ( | int | argc, |
char ** | argv, | ||
int * | stack_size, | ||
int * | array_size, | ||
char ** | output_file, | ||
char ** | input_file, | ||
int * | mode | ||
) |
Custom getopt function.
Since I don't want to break compatibility with all systems by including unistd.h, this function exists. It sets variables based on command line arguments and flags.
argc | The amount of command line args |
argv | The command line args |
stack_size | A pointer to set a variable which determines the loop stack size |
array_size | A pointer to set a variable which determines the internal array size for the compiled program |
output_file | A pointer to set the argument of the -o option |
input_file | A pointer to set the file to compile |
mode | A pointer to the overall state of the program determined by the flags |
Definition at line 71 of file common.c.
References is_uint(), and print_help().
Referenced by main().
char* remove_extenstion | ( | const char * | str | ) |