~evan/bitart

ref: 6269c4d47ad98829dc716583a834406e2394ee73 bitart/main.c -rw-r--r-- 5.1 KiB
6269c4d4Evan Johnston add defaults to switch statements to silence warnings 5 months ago
                                                                                
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
213
214
215
216
#include <stdbool.h>
#include <unistd.h>
#include <ctype.h>

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

enum ltok {
	END,
	XOR, OR, AND, NOT,
	ADD, DIV, SUB, MUL, MOD, EXP,
	EQ, LT, GT, COUNT,
	DUP, SWAP,
	LEFT, RIGHT,
	LNOT,
	HEIGHT, WIDTH,
	X, Y, LIT, ERROR,
};

struct token {
	enum ltok tag;
	unsigned int data;
};

/* i just realized i should have gone with a design where i lex into an array
 * so i don't have to lex the program once per pixel but i don't want to
 * rewrite it now */
struct token
ntok(char **source) {
	if (!**source) return (struct token){0};
	enum ltok tag = LIT;
	unsigned int data = 0;
	while (**source == ' ') (*source)++;
	switch (*(*source)++) {
	case '^': tag = XOR; break;
	case '|': tag = OR; break;
	case '&': tag = AND; break;
	case '~': tag = NOT; break;
	case '!': tag = LNOT; break;
	case '+': tag = ADD; break;
	case '/': tag = DIV; break;
	case '-': tag = SUB; break;
	case '*': tag = MUL; break;
	case '%': tag = MOD; break;
	case 'e': tag = EXP; break;
	case '=': tag = EQ; break;
	case '<': tag = LT; break;
	case '>': tag = GT; break;
	case '#': tag = COUNT; break;
	case 'x': tag = X; break;
	case 'y': tag = Y; break;
	case 'd': tag = DUP; break;
	case 's': tag = SWAP; break;
	case 'l': tag = LEFT; break;
	case 'r': tag = RIGHT; break;
	case 'h': tag = HEIGHT; break;
	case 'w': tag = WIDTH; break;
	default:
		 (*source)--;
		 if (!isdigit(**source)) {
			 (*source)++;
			 return (struct token){ERROR, 0};
		 } data = strtoul(*source, source, 10);
	};
	return (struct token){tag, data};
};

unsigned width = 255;
unsigned height = 255;

unsigned int *
eval(char *program, unsigned int x, unsigned int y, unsigned int *stack)
{
	char *errortext = "stack underflow";
	char *p = program;
	unsigned int *sp = stack;
#define push(n) (*(sp++) = n)
	struct token t = { 0 };
	for (;;) {
		t = ntok(&p);
		if (!t.tag) break;
		switch (t.tag) {
		case ERROR:
			errortext = "unrecognised token";
			goto error;
		case X: push(x); continue;
		case Y: push(y); continue;
		case HEIGHT: push(height); continue;
		case WIDTH: push(width); continue;
		case LIT: push(t.data); continue;
		default: break;
		};
		if (--sp < stack) goto error;
		unsigned int b = *sp;
		switch (t.tag) {
		case NOT: push(~b); continue;
		case LNOT: push(!b); continue;
		/* shhhhhhhh it's fine portability is overrated */
		case COUNT: push(__builtin_popcount(b)); continue;
		case DUP: push(b); push(b); continue;
		default: break;
		};
		if (--sp < stack) goto error;
		unsigned int a = *sp;

		if (!b && (t.tag == MOD || t.tag == DIV)) {
			errortext = "division by zero";
			goto error;
		}
		switch (t.tag) {
		case XOR: push(a ^ b); continue;
		case OR: push(a | b); continue;
		case AND: push(a & b); continue;
		case ADD: push(a + b); continue;
		case DIV: push(a / b); continue;
		case SUB: push(a - b); continue;
		case MUL: push(a * b); continue;
		case MOD: push(a % b); continue;
		case EXP:;
			  unsigned int res = 1;
			  for (unsigned int i = 0; i < b; i++) res *= a;
			  push(res);
			  continue;
		case EQ: push(a == b); continue;
		case LT: push(a < b); continue;
		case GT: push(a > b); continue;
		case SWAP: push(b); push(a); continue;
		case LEFT: push(a << b); continue;
		case RIGHT: push(b >> b); continue;
		default: break;
		}
	}
	return sp;
error:
	fputs(program, stderr);
	fputc('\n', stderr);
	for (char *a = p - 1; a > program; a--) fputc(' ', stderr);
	fprintf(stderr, "\033[31;1m^--- %s\033[0m\n", errortext);
	exit(1);
}

void
usage(char *name)
{
	fprintf(stderr, "usage: %s [-g] [-c] [-i] [-w width] [-h height] [-s size] [-o out.png] program\n", name);
	exit(1);
}

void
write_image_stdout(void *context, void *data, int size)
{
	write(STDOUT_FILENO, data, size);
}

int
main(int ac, char **av)
{
	int opt;
	bool gradient = false;
	bool invert = false;
	bool color = false;
	char *filename = NULL;
	while ((opt = getopt(ac, av, "o:w:h:s:gci")) != -1) {
		switch (opt) {
		case 'w':
			width = atoi(optarg);
			break;
		case 'h':
			height = atoi(optarg);
			break;
		case 's':
			height = atoi(optarg);
			width = height;
			break;
		case 'o':
			filename = optarg;
			break;
		case 'g':
			gradient = true;
			break;
		case 'c':
			color = true;
			break;
		case 'i':
			invert = true;
			break;
		default:
			usage(av[0]);
		}
	}
	int channels = color ? 3 : 1;
	if (optind >= ac) usage(av[0]);
	char *program = av[optind];
	char *data = malloc(width * height * channels);
	/* stack cannot ever be larger than the program length, since there are
	* no instructions which grow the stack by more than one */
	unsigned int *stack = calloc(strlen(program), sizeof(*stack));
	for (unsigned y = 1; y < height; y++)
	for (unsigned x = 1; x < width; x++) {
		unsigned int *sp = eval(program, x, y, stack);
		if (sp - stack < channels) {
			fprintf(stderr, "insufficient values left on the stack\n");
			exit(1);
		}
		for (int i = 0; i < channels; i++) {
			data[(x + y * width) * channels + i] =
				gradient ? stack[i] : ((!!stack[i]) != invert) ? -1 : 0;
		}
	}
	free(stack);
	if (filename == NULL)
		stbi_write_png_to_func(&write_image_stdout, NULL, width, height, channels, data, 0);
	else stbi_write_png(filename, width, height, channels, data, 0);
	free(data);
	return 0;
}