1// i got to eventually clean up this file a bit i feel like its very janky but i suppose i can worry about that later - future dani here, it seems you ended up just remaking the entire file bc you thought remaking cot into a concatenated language was cooler, good job dani - future future dani here, thank you dani :D
2
3#include "interpreter.h"
4#include "cottonwindow.h"
5#include "eiki.h"
6#include <SDL.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <ctype.h>
11
12#define MAX_LINE 256
13
14// da stack
15static void push
16(Cotton *c, int v)
17{
18 if (c->stack_p >= 64) {
19 fprintf(stderr, "cot: there has been a stack overflow :< (like the website badumptsss)\n");
20 return;
21 }
22
23 c->stack[c->stack_p++] = v;
24}
25
26static int pop
27(Cotton *c)
28{
29 if (c->stack_p <= 0) {
30 fprintf(stderr, "cot: there has been a stack underflow :<\n");
31 return 0;
32 }
33
34 return c->stack[--c->stack_p];
35}
36
37// da variable stuff
38static int find_var
39(Cotton *c, const char *name)
40{
41 for (int i = 0; i < c->var_count; i++) {
42 if (strcmp(c->vars[i].name, name) == 0) return i;
43 }
44
45 return -1;
46}
47
48static void add_var
49(Cotton *c, const char *name)
50{
51 if (c->var_count >= VARS) {
52 fprintf(stderr, "cot: too many variables\n");
53 return;
54 }
55
56 strncpy(c->vars[c->var_count].name, name, VAR_NAME_LEN - 1);
57 c->vars[c->var_count].name[VAR_NAME_LEN - 1] = '\0';
58 strcpy(c->vars[c->var_count].value, "0");
59 c->var_count++;
60}
61
62// da graphics
63static void draw_pixel_c
64(Cotton *c, int x, int y)
65{
66 if (x < 0 || x >= VIDEO_WIDTH || y < 0 || y >= VIDEO_HEIGHT) return;
67 c->video[y * VIDEO_WIDTH + x] = c->c_cottolette;
68}
69
70static void draw_char_c
71(Cotton *c, char ch, int x, int y)
72{
73 for (int row = 0; row < FONT_HEIGHT; row++) {
74 uint8_t bits = EIKI_FONT[(uint8_t)ch][row];
75
76 for (int col = 0; col < FONT_WIDTH; col++) {
77 if (bits & (1 << col)) {
78 draw_pixel_c(c, x + col, y + row);
79 }
80 }
81 }
82}
83
84// those who interpretate skull emoji skull emoji nahhhh get outttttttttt skull emoji door emoji
85void cotton_interpret
86(Cotton *c, CottonWindow *cw, FILE *file)
87{
88 char line[MAX_LINE];
89 if (!fgets(line, sizeof(line), file)) return;
90
91 line[strcspn(line, "\r\n")] = 0;
92 if (line[0] == '\0' || (line[0] == '/' && line[1] == '/')) return;
93
94 char *tok = strtok(line, " ");
95
96 while (tok) {
97
98 int is_a_num = 1;
99 for (int i = 0; tok[i]; i++) {
100 if (!isdigit(tok[i]) && !(i == 0 && tok[i] == '-')) {
101 is_a_num = 0;
102 break;
103 }
104 }
105
106 if (is_a_num) {
107 push(c, atoi(tok));
108 }
109
110 // those who do math..., math from the epic video gaem baldi's basics in education and learning,., you guys should play it its a good game i like baldi,. send me an email telling me your guys' PR on endless mode
111 else if (strcmp(tok, "+") == 0) {
112 int b = pop(c), a = pop(c);
113 push(c, a + b);
114 }
115
116 else if (strcmp(tok, "-") == 0) {
117 int b = pop(c), a = pop(c);
118 push(c, a - b);
119 }
120
121 else if (strcmp(tok, "*") == 0) {
122 int b = pop(c), a = pop(c);
123 push(c, a * b);
124 }
125
126 else if (strcmp(tok, "/") == 0) {
127 int b = pop(c), a = pop(c);
128 push(c, a / b);
129 }
130
131 // while forth does use ! and @, im quirky and #notlikeothergirls so im changing ! to -> bc i think it makes more sense :P
132 else if (strcmp(tok, "->") == 0) {
133
134 int slot = pop(c);
135 int num = pop(c);
136
137 if (slot >= 0 && slot < c->var_count) {
138 char buf[VAR_VAL_LEN];
139 sprintf(buf, "%d", num);
140 strncpy(c->vars[slot].value, buf, VAR_VAL_LEN - 1);
141 }
142
143 else {
144 fprintf(stderr, "cot: tried to store somewhere that don't exist.,. its in the void now.., :<\n");
145 }
146 }
147
148 else if (strcmp(tok, "@") == 0) {
149 int slot = pop(c);
150
151 if (slot >= 0 && slot < c->var_count) {
152 push(c, atoi(c->vars[slot].value));
153 }
154
155 else {
156 fprintf(stderr, "cot: tried to fetch from somewhere that don't exist,., :<\n");
157 }
158
159 }
160
161 else if (strcmp(tok, ".") == 0) {
162 printf("%d", pop(c));
163 fflush(stdout);
164 }
165
166 else if (strcmp(tok, "var") == 0) {
167 tok = strtok(NULL, " ");
168 if (tok) add_var(c, tok);
169 }
170
171 else if (strcmp(tok, "pixel") == 0) {
172 int y = pop(c);
173 int x = pop(c);
174 draw_pixel_c(c, x, y);
175 cottonwindow_update(cw, c->video, sizeof(c->video[0]) * VIDEO_WIDTH);
176 }
177
178 else if (strcmp(tok, "color") == 0) {
179 int slot = pop(c);
180 if (slot >= 0 && slot < COTTOLETTE_SIZE)
181 c->c_cottolette = COTTOLETTE[slot];
182 }
183
184 else if (strcmp(tok, "clear") == 0) {
185 for (int i = 0; i < VIDEO_WIDTH * VIDEO_HEIGHT; i++)
186 c->video[i] = 0;
187 c->cursor_x = 0;
188 c->cursor_y = 0;
189 cottonwindow_update(cw, c->video, sizeof(c->video[0]) * VIDEO_WIDTH);
190 }
191
192 // the great wall of colors
193 else if (strcmp(tok, "blabla") == 0) push(c, 0);
194 else if (strcmp(tok, "whie") == 0) push(c, 1);
195 else if (strcmp(tok, "lav") == 0) push(c, 2);
196 else if (strcmp(tok, "wink") == 0) push(c, 3);
197 else if (strcmp(tok, "sink") == 0) push(c, 4);
198 else if (strcmp(tok, "wist") == 0) push(c, 5);
199 else if (strcmp(tok, "yebow") == 0) push(c, 6);
200 else if (strcmp(tok, "geen") == 0) push(c, 7);
201
202 else if (strcmp(tok, "print") == 0) {
203 char *str = strtok(NULL, "");
204
205 if (str) {
206 while (*str == ' ') str++;
207
208 int nl = 0;
209 char *nl_at = strstr(str, " /n");
210
211 if (nl_at) {
212 *nl_at = '\0';
213 nl = 1;
214 }
215
216 if (str[0] == '"') {
217 str++;
218 char *end = strrchr(str, '"');
219 if (end) *end = '\0';
220 }
221
222 printf("%s", str);
223 if (nl) printf("\n");
224 fflush(stdout);
225
226 for (int i = 0; str[i]; i++) {
227 if (str[i] == '\n') {
228 c->cursor_x = 0;
229 c->cursor_y += FONT_HEIGHT + 4;
230 continue;
231 }
232
233 draw_char_c(c, str[i], c->cursor_x, c->cursor_y);
234 c->cursor_x += FONT_WIDTH + 2;
235
236 if (c->cursor_x + FONT_WIDTH >= VIDEO_WIDTH) {
237 c->cursor_x = 0;
238 c->cursor_y += FONT_HEIGHT + 4;
239 }
240 }
241
242 if (nl) {
243 c->cursor_x = 0;
244 c->cursor_y += FONT_HEIGHT + 4;
245 }
246
247 cottonwindow_update(cw, c->video, sizeof(c->video[0]) * VIDEO_WIDTH);
248 }
249 break;
250 }
251
252 else if (strcmp(tok, "wait") == 0) {
253 c->ticktock = SDL_GetTicks() + pop(c);
254 }
255
256 else if (strcmp(tok, "kill") == 0) {
257 exit(0);
258 }
259
260 else {
261 int slot = find_var(c, tok);
262 if (slot >= 0) {
263 push(c, slot);
264 }
265
266 else {
267 fprintf(stderr, "cot: cotton doesn't know what \"%s\" means, maybe type that out again? :<", tok);
268 }
269 }
270
271 tok = strtok(NULL, " ");
272 }
273}