-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
189 lines (171 loc) · 4.07 KB
/
common.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdlib.h>
#include <unistd.h>
#define MAX_BUFF 256
#define MAIN_INIT "#include <stdio.h>\n" \
"#include <string.h>\n" \
"int main(){\n" \
"char mem[30000];\n" \
"char *ptr = mem;\n" \
"memset(mem, 0, 30000);\n" /* clean mem */
#define MAIN_FIN "return 0;\n" \
"}\n"
/* string.h replacements */
static void __attribute__((unused))
clean(char *s, size_t l)
{
while(l-- > 0)
*(s+l) = '\0';
}
static char * __attribute__((unused))
scpy(char *d, char *s)
{
char *p = d;
if (*s == '\0')
return NULL;
while(*s != '\0')
*p++ = *s++;
return d;
}
static char * __attribute__((unused))
scat(char *d, char *s)
{
char *p = d;
if (*s == '\0')
return NULL;
while (*++p != '\0') ;
while(*s != '\0')
*p++ = *s++;
return d;
}
static size_t __attribute__((unused))
slen(char *s)
{
size_t l = 0;
if (*s == '\0')
return l;
while(*s++ != '\0')
l++;
return l;
}
static int __attribute__((unused))
gchar()
{
int c;
read(0, (void *)&c, 1);
return c;
}
static int __attribute__((unused))
pchar(int c)
{
write(1, (void *)&c, 1);
return c;
}
static void __attribute__((unused))
_abort(const char *err, const int pos)
{
if (err != NULL) {
if (pos > -1)
fprintf(stderr, "error: %s (at pos: %d)\n", err, pos);
else
fprintf(stderr, "error: %s\n", err);
}
exit(EXIT_FAILURE);
}
/* testing
* taken from: https://esolangs.org/wiki/Brainfuck
*/
#define HELLO_WORLD "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
#define CAT ",[.,]"
/* lexer.c */
enum {
TOKEN_EOF = -1,
#define CHAR_EOF '\0'
TOKEN_UNKNOWN,
TOKEN_RIGHT, /* > */
#define CHAR_RIGHT '>'
TOKEN_LEFT, /* < */
#define CHAR_LEFT '<'
TOKEN_INC, /* + */
#define CHAR_INC '+'
TOKEN_DEC, /* - */
#define CHAR_DEC '-'
TOKEN_OUT, /* . */
#define CHAR_OUT '.'
TOKEN_IN, /* , */
#define CHAR_IN ','
TOKEN_OLOOP, /* [ */
#define CHAR_OLOOP '['
TOKEN_CLOOP /* ] */
#define CHAR_CLOOP ']'
};
struct lexer {
const char *src;
int pos;
};
extern struct lexer lexer_init(const char *s);
extern int lexer_token(struct lexer *l);
extern void lexer_print(int token);
/* emit.c */
struct emitter {
int fd;
char code[MAX_BUFF];
};
extern struct emitter emitter_init(const char *path);
extern int emitter_fin(struct emitter *e);
extern ssize_t emitter_out(struct emitter *e);
/* interpreter.c */
#define MEMSIZE 30000
struct interpreter {
char *ptr;
char *mem;
};
extern struct interpreter interpreter_init(const int memlen);
extern void interpreter_fin(struct interpreter *i);
extern void interpreter_right(struct interpreter *i);
extern void interpreter_left(struct interpreter *i);
extern void interpreter_inc(struct interpreter *i);
extern void interpreter_dec(struct interpreter *i);
extern void interpreter_in(struct interpreter *i);
extern void interpreter_out(struct interpreter *i);
/* parser.c */
struct parser {
struct lexer lexer;
struct emitter emitter;
struct interpreter interpreter;
int interpreting;
int debugging;
};
extern struct parser parser_init(const char *s, const char *o, const int vm,
const int bdg);
extern int parser_fin(struct parser *p);
extern void parser_program(struct parser *p);
/* debugger.c */
#define PRNT_NVALUES 16*2 // nitems * lines
enum {
#define CMD_QUIT 'q'
DBG_QUIT,
#define CMD_INFO 'i'
DBG_INFO,
#define CMD_PRNT 'p'
DBG_PRNT,
#define CMD_CONT 'c'
DBG_CONT,
#define CMD_STEP 's'
DBG_STEP,
#define CMD_BRKP 'b'
DBG_BRKP,
#define CMD_HELP '?'
DBG_HELP,
};
struct dbg_cmd {
int id;
char cmd;
char *help;
};
struct dbg_session {
int brkps[MAX_BUFF];
int steps;
};
extern struct dbg_cmd debugger_parser(struct parser *p, char *c,
struct dbg_session *d);
extern int debugger_isbrk(int p, int brkps[]);