commit f5a08c6
soggy
·
2026-11-09 15:47:16 +0000 UTC
parent e7db5c2
misc - sorted the cogs.c file to be in the same order as stuff is in cot.5 in the docs bc it was pmo
1 files changed,
+117,
-112
+117,
-112
1@@ -58,89 +58,80 @@ void cog_mod
2 push(c, a % b);
3 }
4
5-void cog_dotnum
6+void cog_equal
7 (Cotton *c)
8 {
9- printf("%d\n", pop(c));
10+ int b = pop(c);
11+ int a = pop(c);
12+
13+ push(c, a == b ? 1 : 0);
14 }
15
16-void cog_dotstr
17+void cog_unequal
18 (Cotton *c)
19-{
20- int ptr = pop(c);
21+{
22+ int b = pop(c);
23+ int a = pop(c);
24
25- while (c->mem[ptr] != '\0') {
26-
27- printf("%c", c->mem[ptr]);
28- ptr++;
29- }
30+ push(c, a != b ? 1 : 0);
31 }
32
33-void cog_stack
34+void cog_lssr
35 (Cotton *c)
36 {
37- printf("Stack status:\n");
38-
39- if (c->stack_p == 0) {
40-
41- printf("Stack is empty");
42-
43- return;
44- }
45-
46- for (int i = 0; i < c->stack_p; i++) {
47-
48- if (i == c->stack_p - 1) {
49-
50- printf("[%d] <- Top\n", c->stack[i]);
51- }
52-
53- else {
54+ int b = pop(c);
55+ int a = pop(c);
56
57- printf("[%d]\n", c->stack[i]);
58- }
59- }
60+ push(c, a < b ? 1 : 0);
61 }
62
63-// im aware c goes unused here and hence this gives a warning, yes im lazy, might do smth about it another time
64-void cog_newline
65-(Cotton *c)
66+void cog_grtr
67+(Cotton *c)
68 {
69- printf("\n");
70+ int b = pop(c);
71+ int a = pop(c);
72+
73+ push(c, a > b ? 1 : 0);
74 }
75
76-// while forth does use ! and @, im quirky and #notlikeothergirls so im changing ! to -> bc i think it makes more sense :P
77-void cog_store
78+
79+void cog_not
80 (Cotton *c)
81 {
82- int num = pop(c);
83- int slot = pop(c);
84+ int a = pop(c);
85
86- if (slot >= 0 && slot < VARS_SIZE) {
87+ push(c, !a);
88+}
89
90- c->vars[slot] = num;
91- }
92
93- else {
94+void cog_and
95+(Cotton *c)
96+{
97+ int b = pop(c);
98+ int a = pop(c);
99
100- fprintf(stderr, "cotton: tried to store somewhere that don't exist,, its in the void now :(\n");
101- }
102+ push(c, (a != 0 && b != 0) ? 1 : 0);
103 }
104
105-void cog_fetch
106+void cog_or
107 (Cotton *c)
108 {
109- int slot = pop(c);
110-
111- if (slot >= 0 && slot < VARS_SIZE) {
112+ int b = pop(c);
113+ int a = pop(c);
114
115- push(c, c->vars[slot]);
116- }
117+ push(c, (a != 0 || b != 0) ? 1 : 0);
118+}
119
120- else {
121+void cog_true
122+(Cotton *c)
123+{
124+ push(c, 1);
125+}
126
127- fprintf(stderr, "cotton: i tried to fetch from somewhere that seemingly don't exist,, erm...\n");
128- }
129+void cog_false
130+(Cotton *c)
131+{
132+ push(c, 0);
133 }
134
135 void cog_dup
136@@ -152,12 +143,6 @@ void cog_dup
137 push(c, a);
138 }
139
140-void cog_drop
141-(Cotton *c)
142-{
143- pop(c);
144-}
145-
146 void cog_swap
147 (Cotton *c)
148 {
149@@ -168,6 +153,12 @@ void cog_swap
150 push(c, a);
151 }
152
153+void cog_drop
154+(Cotton *c)
155+{
156+ pop(c);
157+}
158+
159 void cog_rotate
160 (Cotton *c)
161 {
162@@ -180,105 +171,119 @@ void cog_rotate
163 push(c, a);
164 }
165
166-void cog_not
167+// while forth does use ! and @, im quirky and #notlikeothergirls so im changing ! to -> bc i think it makes more sense :P
168+void cog_store
169 (Cotton *c)
170 {
171- int a = pop(c);
172+ int num = pop(c);
173+ int slot = pop(c);
174
175- push(c, !a);
176-}
177+ if (slot >= 0 && slot < VARS_SIZE) {
178
179-void cog_equal
180-(Cotton *c)
181-{
182- int b = pop(c);
183- int a = pop(c);
184+ c->vars[slot] = num;
185+ }
186
187- push(c, a == b ? 1 : 0);
188+ else {
189+
190+ fprintf(stderr, "cotton: tried to store somewhere that don't exist,, its in the void now :(\n");
191+ }
192 }
193
194-void cog_unequal
195+void cog_fetch
196 (Cotton *c)
197 {
198- int b = pop(c);
199- int a = pop(c);
200+ int slot = pop(c);
201
202- push(c, a != b ? 1 : 0);
203-}
204+ if (slot >= 0 && slot < VARS_SIZE) {
205
206-void cog_lssr
207-(Cotton *c)
208-{
209- int b = pop(c);
210- int a = pop(c);
211+ push(c, c->vars[slot]);
212+ }
213
214- push(c, a < b ? 1 : 0);
215+ else {
216+
217+ fprintf(stderr, "cotton: i tried to fetch from somewhere that seemingly don't exist,, erm...\n");
218+ }
219 }
220
221-void cog_grtr
222+void cog_dotnum
223 (Cotton *c)
224 {
225- int b = pop(c);
226- int a = pop(c);
227-
228- push(c, a > b ? 1 : 0);
229+ printf("%d\n", pop(c));
230 }
231
232-void cog_and
233+void cog_dotstr
234 (Cotton *c)
235 {
236- int b = pop(c);
237- int a = pop(c);
238+ int ptr = pop(c);
239
240- push(c, (a != 0 && b != 0) ? 1 : 0);
241+ while (c->mem[ptr] != '\0') {
242+
243+ printf("%c", c->mem[ptr]);
244+ ptr++;
245+ }
246 }
247
248-void cog_or
249+
250+void cog_dotstack
251 (Cotton *c)
252 {
253- int b = pop(c);
254- int a = pop(c);
255+ printf("Stack status:\n");
256
257- push(c, (a != 0 || b != 0) ? 1 : 0);
258-}
259+ if (c->stack_p == 0) {
260
261-void cog_true
262-(Cotton *c)
263-{
264- push(c, 1);
265+ printf("Stack is empty");
266+
267+ return;
268+ }
269+
270+ for (int i = 0; i < c->stack_p; i++) {
271+
272+ if (i == c->stack_p - 1) {
273+
274+ printf("[%d] <- Top\n", c->stack[i]);
275+ }
276+
277+ else {
278+
279+ printf("[%d]\n", c->stack[i]);
280+ }
281+ }
282 }
283
284-void cog_false
285+// im aware c goes unused here and hence this gives a warning, yes im lazy, might do smth about it another time
286+void cog_newline
287 (Cotton *c)
288 {
289- push(c, 0);
290+ printf("\n");
291 }
292
293+
294+
295 Cog builtins[] = {
296 {"+", cog_add},
297 {"-", cog_sub},
298 {"*", cog_mul},
299 {"/", cog_div},
300 {"%", cog_mod},
301- {".n", cog_dotnum},
302- {".s", cog_dotstr},
303- {".stk", cog_stack},
304- {"nl", cog_newline},
305- {"->", cog_store},
306- {"@", cog_fetch},
307- {"dup", cog_dup},
308- {"drop", cog_drop},
309- {"swap", cog_swap},
310- {"rot", cog_rotate},
311- {"!", cog_not},
312 {"=", cog_equal},
313 {"!=", cog_unequal},
314 {"<", cog_lssr},
315 {">", cog_grtr},
316+ {"!", cog_not},
317 {"&&", cog_and},
318 {"||", cog_or},
319 {"T", cog_true},
320 {"F", cog_false},
321+ {"dup", cog_dup},
322+ {"swap", cog_swap},
323+ {"drop", cog_drop},
324+ {"rot", cog_rotate},
325+ {"->", cog_store},
326+ {"@", cog_fetch},
327+ {".n", cog_dotnum},
328+ {".s", cog_dotstr},
329+ {".stk", cog_dotstack},
330+ {"nl", cog_newline},
331 {NULL, NULL}
332 };
333