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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
// Copyright (C) 2023 Dzshy <dzshy@outlook.com>. All Rights Reserved.
// Licensed under Non-Profit Open Software License ("Non-Profit OSL") 3.0.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "crc32.h"
#include "htable.h"
#define LOGGER(s, ...) \
do { \
fprintf(stderr, s "\n", ##__VA_ARGS__); \
} while (0)
uint8_t *pbuf;
uint8_t *nbuf;
long nsz, psz;
static const int INIT_SZ = 128;
typedef struct {
long key;
long val;
} JmpTblEntry;
static bool jmptbl_eq(void *x, void *y)
{
long *a = x, *b = y;
return *a == *b;
}
static uint32_t jmptbl_hash(void *k) { return crc32(0, k, sizeof(long)); }
HTable jmptbl;
struct listnode {
struct listnode *next;
long val;
};
typedef struct {
struct listnode *head;
} Stack;
void stack_push(Stack *s, long val)
{
struct listnode *n = malloc(sizeof(struct listnode));
n->next = s->head;
n->val = val;
s->head = n;
}
long *stack_top(Stack *s)
{
if (s->head == NULL)
return NULL;
return &(s->head->val);
}
void stack_pop(Stack *s)
{
if (s->head == NULL)
return;
struct listnode *next = s->head->next;
free(s->head);
s->head = next;
}
void buildjmptable(char *buf, long len)
{
Stack s = {0};
htable_init(&jmptbl, sizeof(JmpTblEntry), -1, jmptbl_hash, jmptbl_eq);
for (long i = 0; i < len; i++) {
if (buf[i] == '[') {
stack_push(&s, i);
} else if (buf[i] == ']') {
long j = *stack_top(&s);
stack_pop(&s);
JmpTblEntry e1 = {i, j};
JmpTblEntry e2 = {j, i};
htable_insert(&jmptbl, &e1);
htable_insert(&jmptbl, &e2);
}
}
}
static long tbllookup(long pos)
{
JmpTblEntry *iter = htable_find(&jmptbl, &pos);
return iter->val;
}
void ensurespc_impl(uint8_t **buf, long *cursz, long ptr)
{
if (ptr > *cursz) {
*buf = realloc(*buf, ptr * 2);
memset((*buf) + (*cursz), 0, 2 * ptr - (*cursz));
*cursz = ptr * 2;
}
}
void ensurespc(long ptr)
{
if (ptr >= 0) {
ensurespc_impl(&pbuf, &psz, ptr);
} else {
ensurespc_impl(&nbuf, &nsz, -ptr);
}
}
uint8_t getdata(long ptr)
{
ensurespc(ptr);
if (ptr >= 0)
return pbuf[ptr];
else
return nbuf[-ptr];
}
void setdata(long ptr, uint8_t val)
{
ensurespc(ptr);
if (ptr >= 0)
pbuf[ptr] = val;
else
nbuf[-ptr] = val;
}
void interp(char *prog, long prog_sz)
{
long prog_p = 0;
long data_p = 0;
nbuf = malloc(INIT_SZ);
pbuf = malloc(INIT_SZ);
nsz = INIT_SZ;
psz = INIT_SZ;
memset(nbuf, 0, INIT_SZ);
memset(pbuf, 0, INIT_SZ);
while (prog_p < prog_sz) {
switch (prog[prog_p]) {
case '>':
data_p++;
break;
case '<':
data_p--;
break;
case '+':
setdata(data_p, getdata(data_p) + 1);
break;
case '-':
setdata(data_p, getdata(data_p) - 1);
break;
case '.':
putchar(getdata(data_p));
break;
case ',':
setdata(data_p, getchar());
break;
case '[':
if (getdata(data_p) == 0) {
prog_p = tbllookup(prog_p);
}
break;
case ']':
if (getdata(data_p) != 0) {
prog_p = tbllookup(prog_p);
}
break;
default:
break;
}
prog_p++;
}
}
int main(int argc, char **argv)
{
if (argc < 2) {
LOGGER("invalid args");
return EXIT_FAILURE;
}
int fd = open(argv[1], O_RDONLY);
if (fd < 0) {
return EXIT_FAILURE;
}
struct stat fst;
int ret = fstat(fd, &fst);
if (ret != 0) {
LOGGER("error: failed to get file stat");
return EXIT_FAILURE;
}
long filesz = fst.st_size;
char *prog = malloc(filesz);
FILE *fp = fdopen(fd, "r");
if (fp == NULL) {
LOGGER("error opening file");
exit(-1);
}
size_t read = fread(prog, 1, filesz, fp);
if (read < filesz) {
LOGGER("erro reading");
exit(-1);
}
buildjmptable(prog, filesz);
interp(prog, filesz);
return EXIT_SUCCESS;
}
|