commit e7db5c2
soggy
·
2026-11-08 21:17:59 +0000 UTC
parent b36d00a
cot & docs - added debugging, logical and comparison cogs (and updated the docs)
6 files changed,
+127,
-26
+0,
-2
1@@ -4,8 +4,6 @@ cotton is a small stack machine for the cot programming language (a concatenativ
2
3 cotton is a passion hobby project being made solely by me, so, dont expect anything grandiose or of high quality - even then, suggestions and ideas are always welcome!! :D
4
5-*( little disclaimer : as of writting this (05/11/2026) the project is in a bit of a hiatus as i am a bit busy / stuck with how i want to continue the project, however, i do not want this project to die and i DO want to continue it when i feel like the time and place is proper for it!! (not that anyone but me really uses this or cares alot about it, but, even then, thought i'd write this here xP) )*
6-
7 ## building cotton
8
9 ```
+13,
-1
1@@ -34,6 +34,16 @@ Cogs that are processed at compile time to handle the control flow and the progr
2 .Bl -tag -width "-> (slot)"
3 .It Ic + , - , * , /, %
4 Pops two values, does arithmetic, and pushes the result
5+.It Ic =, !=, <, >
6+Compares two values, then, pushes 1 if the comparision, otherwise, returns 0
7+.It Ic !
8+Logical not, pops a value and pushes 1 if it was 0, otherwise, pushes 0
9+.It Ic &&
10+Logical and, pops two values and pushes 1 if both are not zero, otherwise, pushes 0
11+.It Ic ||
12+Logical or, pops two values and pushes 1 if either value is not zero, otherwise, pushes 0
13+.It Ic T, F
14+T stands for true (1), F stands for false (0), pretty straightforward
15 .It Ic --
16 Comments out a line, currently there isn't any multiline support,, sowwe,, >.<
17 .It Ic dup
18@@ -43,7 +53,7 @@ Swaps the top two values on the stack
19 .It Ic drop
20 Discards/drops the top value on the stack
21 .It Ic rot
22-Pops three values, rotates them around, so, the top-most value goes to the bottom and the latter two go up
23+Pops the top three values, moves the top most value to the bottom
24 .It Ic ->
25 Pops a value and a slot index, then stores the value in that same variable slot
26 .It Ic @
27@@ -52,6 +62,8 @@ Pops a slot index and pushes the stored value back onto the stack
28 Prints a number
29 .It Ic .s
30 Prints a string
31+.It Ic .stk
32+Prints the current state of the stack - does NOT modify the stack (its meant to be more of a debugging tool of sorts
33 .It Ic nl
34 Meant to be used alongside .n and .s, makes numbers and strings print in a new line
35 .It Ic ? if
+7,
-0
1@@ -0,0 +1,7 @@
2+-- stk is a neat little debugging intended tool !! check this out !! :D
3+
4+21 61 67 69 420
5+
6+.stk
7+
8+-- i ought to make a .vars for a similar purpose, safe for this time you could check the variable storage :3c
+0,
-0
+105,
-21
1@@ -1,4 +1,3 @@
2-#include "interpreter.h"
3 #include "cotton.h"
4 #include <stdio.h>
5
6@@ -35,7 +34,6 @@ void cog_div
7 int b = pop(c);
8 int a = pop(c);
9
10- // Now, i doubt anyone will really be dividing stuff by zero but you never know.., talk about le safety amirite.., heh
11 if (b == 0) {
12
13 fprintf(stderr, "cotton: you cant divide by zero, you dummy :P\n");
14@@ -51,6 +49,12 @@ void cog_mod
15 int b = pop(c);
16 int a = pop(c);
17
18+ if (b == 0) {
19+
20+ fprintf(stderr, "cotton: you cant modulo a number by zero, you dummy :P\n");
21+ return;
22+ }
23+
24 push(c, a % b);
25 }
26
27@@ -72,6 +76,32 @@ void cog_dotstr
28 }
29 }
30
31+void cog_stack
32+(Cotton *c)
33+{
34+ printf("Stack status:\n");
35+
36+ if (c->stack_p == 0) {
37+
38+ printf("Stack is empty");
39+
40+ return;
41+ }
42+
43+ for (int i = 0; i < c->stack_p; i++) {
44+
45+ if (i == c->stack_p - 1) {
46+
47+ printf("[%d] <- Top\n", c->stack[i]);
48+ }
49+
50+ else {
51+
52+ printf("[%d]\n", c->stack[i]);
53+ }
54+ }
55+}
56+
57 // im aware c goes unused here and hence this gives a warning, yes im lazy, might do smth about it another time
58 void cog_newline
59 (Cotton *c)
60@@ -83,8 +113,8 @@ void cog_newline
61 void cog_store
62 (Cotton *c)
63 {
64- int slot = pop(c);
65 int num = pop(c);
66+ int slot = pop(c);
67
68 if (slot >= 0 && slot < VARS_SIZE) {
69
70@@ -93,7 +123,7 @@ void cog_store
71
72 else {
73
74- fprintf(stderr, "cotton: i tried to store somewhere that don't exist,, its in the void now.., :(\n");
75+ fprintf(stderr, "cotton: tried to store somewhere that don't exist,, its in the void now :(\n");
76 }
77 }
78
79@@ -109,7 +139,7 @@ void cog_fetch
80
81 else {
82
83- fprintf(stderr, "cotton: i tried to fetch from somewhere that seemingly don't exist,, erm.,.\n");
84+ fprintf(stderr, "cotton: i tried to fetch from somewhere that seemingly don't exist,, erm...\n");
85 }
86 }
87
88@@ -150,6 +180,14 @@ void cog_rotate
89 push(c, a);
90 }
91
92+void cog_not
93+(Cotton *c)
94+{
95+ int a = pop(c);
96+
97+ push(c, !a);
98+}
99+
100 void cog_equal
101 (Cotton *c)
102 {
103@@ -159,6 +197,15 @@ void cog_equal
104 push(c, a == b ? 1 : 0);
105 }
106
107+void cog_unequal
108+(Cotton *c)
109+{
110+ int b = pop(c);
111+ int a = pop(c);
112+
113+ push(c, a != b ? 1 : 0);
114+}
115+
116 void cog_lssr
117 (Cotton *c)
118 {
119@@ -177,25 +224,62 @@ void cog_grtr
120 push(c, a > b ? 1 : 0);
121 }
122
123+void cog_and
124+(Cotton *c)
125+{
126+ int b = pop(c);
127+ int a = pop(c);
128+
129+ push(c, (a != 0 && b != 0) ? 1 : 0);
130+}
131+
132+void cog_or
133+(Cotton *c)
134+{
135+ int b = pop(c);
136+ int a = pop(c);
137+
138+ push(c, (a != 0 || b != 0) ? 1 : 0);
139+}
140+
141+void cog_true
142+(Cotton *c)
143+{
144+ push(c, 1);
145+}
146+
147+void cog_false
148+(Cotton *c)
149+{
150+ push(c, 0);
151+}
152+
153 Cog builtins[] = {
154- {"+", cog_add},
155- {"-", cog_sub},
156- {"*", cog_mul},
157- {"/", cog_div},
158+ {"+", cog_add},
159+ {"-", cog_sub},
160+ {"*", cog_mul},
161+ {"/", cog_div},
162 {"%", cog_mod},
163 {".n", cog_dotnum},
164 {".s", cog_dotstr},
165+ {".stk", cog_stack},
166 {"nl", cog_newline},
167- {"->", cog_store},
168- {"@", cog_fetch},
169- {"dup", cog_dup},
170- {"drop", cog_drop},
171- {"swap", cog_swap},
172- {"rot", cog_rotate},
173- {"=", cog_equal},
174- {"<", cog_lssr},
175- {">", cog_grtr},
176- {NULL, NULL}
177+ {"->", cog_store},
178+ {"@", cog_fetch},
179+ {"dup", cog_dup},
180+ {"drop", cog_drop},
181+ {"swap", cog_swap},
182+ {"rot", cog_rotate},
183+ {"!", cog_not},
184+ {"=", cog_equal},
185+ {"!=", cog_unequal},
186+ {"<", cog_lssr},
187+ {">", cog_grtr},
188+ {"&&", cog_and},
189+ {"||", cog_or},
190+ {"T", cog_true},
191+ {"F", cog_false},
192+ {NULL, NULL}
193 };
194
195 /*
196@@ -223,7 +307,7 @@ void cog_else
197 {
198 if (c->jmp_stack_p <= 0) {
199
200- fprintf(stderr, "cotton: i found an else without an if,., where'd that come from??\n");
201+ fprintf(stderr, "cotton: i found an else without an if.. where'd that come from??\n");
202 return;
203 }
204
205@@ -241,7 +325,7 @@ void cog_end
206 {
207 if (c->jmp_stack_p <= 0) {
208
209- fprintf(stderr, "cotton: i found an end without an if,., where'd that come from??\n");
210+ fprintf(stderr, "cotton: i found an end without an if.. where'd that come from??\n");
211 return;
212 }
213
+2,
-2
1@@ -25,7 +25,7 @@ void cotton_compile
2
3 if (c->mem_len >= MEM_SIZE - 2) {
4
5- fprintf(stderr, "cotton: i have ran out of memory,., can't compile anymore :<\n");
6+ fprintf(stderr, "cotton: i have ran out of memory.. can't compile anymore :<\n");
7 return;
8 }
9
10@@ -48,7 +48,7 @@ void cotton_compile
11
12 else if (line[i] == '\0') {
13
14- fprintf(stderr, "cotton: you forgot to close a string.,. i cant compile the file until you fix it :P");
15+ fprintf(stderr, "cotton: you forgot to close a string.. i cant compile the file until you fix it :P");
16 return;
17 }
18