-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck.h
executable file
·102 lines (72 loc) · 1.76 KB
/
brainfuck.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef __BRAINFUCK__
#define __BRAINFUCK__
#include <stdio.h>
#include <stdlib.h>
#define MAX_CELLS 65536
#define MIN_SOURCE_SIZE 1
#define MAX_SOURCE_SIZE 524288
typedef unsigned char bf_cell;
typedef struct {
bf_cell *cell;
char *returnPoint;
} bf_stack_item;
typedef struct {
long size;
long pointer;
bf_stack_item *items;
} bf_stack;
typedef struct {
bf_stack *stack;
// Memory cells
bf_cell *firstCell;
bf_cell *lastCell; // optimization
bf_cell *currentCell;
// Code
char *firstCommand;
char *lastCommand; // optimization
char *currentCommand;
long codeSize;
} bf_state;
typedef enum {
CMD_NEXT_CELL, // >
CMD_PREV_CELL, // <
CMD_INCREMENT, // +
CMD_DECREMENT, // -
CMD_PRINT_CELL, // .
CMD_INPUT_CELL, // ,
CMD_JUMP_FORWARD, // [
CMD_JUMP_BACKWARD, // ]
CMD_CARET_RETURN, // \r
CMD_LINEFEED ,// \n
NUM_COMMANDS
} bf_command;
typedef enum {
ERR_SUCCESS,
ERR_UNKNOWN_COMMAND,
ERR_CELL_OUT_OF_RANGE,
ERR_STACK_OVERFLOW,
ERR_STACK_UNDERFLOW,
ERR_IO_ERROR,
ERR_NO_MEMORY,
ERR_INVALID_SOURCE,
NUM_ERRORS
} bf_error;
typedef bf_error (*bf_command_handler) (bf_state*);
typedef struct {
bf_command type;
char code;
bf_command_handler handler;
} bf_command_info;
extern const bf_command_info command_info[NUM_COMMANDS];
typedef struct {
bf_erro type;
const char* messageText;
} bf_error_info;
extern const bf_error_info error_info[NUM_ERRORS];
bf_error new_interpreter(char* sourcePath, bf_state** stateBuffer);
bf_error run_interpreter(bf_state* state);
void free_interpreter(bf_state* state);
void print_error(bf_state* state, bf_error error);
// Utils
long fsize(FILE *fp);
#endif // __BRAINFUCK__