commit 9a1e385

soggy  ·  2026-11-09 20:10:56 +0000 UTC
parent f5a08c6
cot & docs - added another debugging cog, also bumped the version to 0.1.1
4 files changed,  +56, -24
+4, -2
 1@@ -1,6 +1,6 @@
 2 .Dd September 2026
 3 .Dt COT 5
 4-.Os cotton 0.1
 5+.Os cotton 0.1.1
 6 .Sh NAME
 7 .Nm cot
 8 .Nd the cot programming language
 9@@ -63,7 +63,9 @@ Prints a number
10 .It Ic .s
11 Prints a string 
12 .It Ic .stk
13-Prints the current state of the stack - does NOT modify the stack (its meant to be more of a debugging tool of sorts
14+Prints the current state of the stack - does NOT modify the stack (its meant to be more of a debugging tool of sorts)
15+.It Ic .vars
16+Prints the currently assigned variables and their values - does NOT modify any variables (just like .stk, its more of a debugging tool)
17 .It Ic nl
18 Meant to be used alongside .n and .s, makes numbers and strings print in a new line 
19 .It Ic ? if
+2, -2
 1@@ -1,6 +1,6 @@
 2 .Dd September 2026
 3 .Dt COTTON 1
 4-.Os cotton 0.1
 5+.Os cotton 0.1.1
 6 .Sh NAME
 7 .Nm cotton
 8 .Nd a tiny stack machine for the cot programming language 
 9@@ -13,7 +13,7 @@
10 
11 cotton is a small stack machine for the cot(5) programming language (inspired by Lua, Forth and Uxntal) written in C99
12 
13-cotton's internals consist of 7 files (as of cotton 0.1) which are:
14+cotton's internals consist of 7 files (as of cotton 0.1.1) which are:
15 
16 .Ss src/
17 .Bl -tag -width "main.c"
+1, -1
1@@ -1,5 +1,5 @@
2 project('cotton', 'c',
3-  version : '0.1',
4+  version : '0.1.1',
5   default_options : ['c_std=c99', 'warning_level=2'])
6 
7 src_files = [
+49, -19
  1@@ -178,10 +178,11 @@ void cog_store
  2     int num = pop(c);
  3     int slot = pop(c);
  4 
  5-    if (slot >= 0 && slot < VARS_SIZE) {
  6+    if (slot >= 0 && slot < INT_VARS_SIZE) {
  7 
  8-        c->vars[slot] = num;
  9-    }
 10+        c->int_vars[slot] = num;
 11+		c->int_vars_assigned[slot] = true;
 12+   }
 13 
 14     else {
 15 
 16@@ -194,9 +195,9 @@ void cog_fetch
 17 {
 18     int slot = pop(c);
 19 
 20-    if (slot >= 0 && slot < VARS_SIZE) {
 21+    if (slot >= 0 && slot < INT_VARS_SIZE) {
 22 
 23- 		push(c, c->vars[slot]);
 24+ 		push(c, c->int_vars[slot]);
 25     }
 26 
 27     else {
 28@@ -229,16 +230,16 @@ void cog_dotstack
 29 {
 30 	printf("Stack status:\n");
 31 
 32-	if (c->stack_p == 0) {
 33+	if (c->stack_ptr == 0) {
 34 
 35-		printf("Stack is empty");
 36+		fprintf(stderr, "cotton: stack is empty so far\n");
 37 
 38 		return;
 39 	}
 40 
 41-	for (int i = 0; i < c->stack_p; i++) {
 42+	for (int i = 0; i < c->stack_ptr; i++) {
 43 
 44-		if (i == c->stack_p - 1) {
 45+		if (i == c->stack_ptr - 1) {
 46 
 47 			printf("[%d] <- Top\n", c->stack[i]);
 48 		}
 49@@ -250,6 +251,35 @@ void cog_dotstack
 50 	}
 51 }
 52 
 53+void cog_dotvars
 54+(Cotton *c)
 55+{
 56+	bool var_finder = false;
 57+
 58+	printf("Vars status:\n");
 59+
 60+	for (int i = 0; i < INT_VARS_SIZE; i++) {
 61+	
 62+		if (c->int_vars_assigned[i] == false) {
 63+
 64+			continue;
 65+		}
 66+
 67+		else if (c->int_vars_assigned[i]) {
 68+			
 69+			printf("[%d]\n", c->int_vars[i]);
 70+
 71+			var_finder = true;
 72+		}
 73+
 74+	}
 75+
 76+	if ( var_finder == false) {
 77+
 78+		fprintf(stderr, "cotton: no variables have been assigned yet\n");
 79+	}
 80+}
 81+
 82 // im aware c goes unused here and hence this gives a warning, yes im lazy, might do smth about it another time
 83 void cog_newline
 84 (Cotton *c)
 85@@ -283,6 +313,7 @@ Cog builtins[] = {
 86 	{".n", cog_dotnum},
 87 	{".s", cog_dotstr},
 88 	{".stk", cog_dotstack},
 89+	{".vars", cog_dotvars},
 90 	{"nl", cog_newline},
 91 	{NULL, NULL}
 92 };
 93@@ -296,30 +327,30 @@ Cog builtins[] = {
 94 void cog_if
 95 (Cotton *c)
 96 {
 97-    if (c->jmp_stack_p >= JMP_STACK_SIZE) {
 98+    if (c->ctrl_stack_ptr >= CTRL_STACK_SIZE) {
 99 
100         fprintf(stderr, "cotton: there are too many nested ifs, calm down!! D:\n"); 
101         return;
102     }
103 
104     c->mem[c->mem_len++] = OP_FJMP;
105-    c->jmp_stack[c->jmp_stack_p++] = c->mem_len;
106+    c->ctrl_stack[c->ctrl_stack_ptr++] = c->mem_len;
107     c->mem[c->mem_len++] = 0;
108 }
109 
110 void cog_else
111 (Cotton *c)
112 {
113-    if (c->jmp_stack_p <= 0) {
114+    if (c->ctrl_stack_ptr <= 0) {
115 
116         fprintf(stderr, "cotton: i found an else without an if.. where'd that come from??\n"); 
117         return;
118     }
119 
120-    int if_target = c->jmp_stack[--c->jmp_stack_p];
121+    int if_target = c->ctrl_stack[--c->ctrl_stack_ptr];
122 
123     c->mem[c->mem_len++] = OP_JMP;
124-    c->jmp_stack[c->jmp_stack_p++] = c->mem_len;
125+    c->ctrl_stack[c->ctrl_stack_ptr++] = c->mem_len;
126     c->mem[c->mem_len++] = 0;
127 
128     c->mem[if_target] = c->mem_len;
129@@ -328,26 +359,26 @@ void cog_else
130 void cog_end
131 (Cotton *c)
132 {
133-    if (c->jmp_stack_p <= 0) {
134+    if (c->ctrl_stack_ptr <= 0) {
135 
136         fprintf(stderr, "cotton: i found an end without an if.. where'd that come from??\n"); 
137         return;
138     }
139 
140-    int target = c->jmp_stack[--c->jmp_stack_p];
141+    int target = c->ctrl_stack[--c->ctrl_stack_ptr];
142     c->mem[target] = c->mem_len;
143 }
144 
145 void cog_loop
146 (Cotton *c)
147 {
148-	c->jmp_stack[c->jmp_stack_p++] = c->mem_len;
149+	c->ctrl_stack[c->ctrl_stack_ptr++] = c->mem_len;
150 }
151 
152 void cog_until
153 (Cotton *c)
154 {
155-	int loop_back = c->jmp_stack[--c->jmp_stack_p];
156+	int loop_back = c->ctrl_stack[--c->ctrl_stack_ptr];
157 
158 	c->mem[c->mem_len++] = OP_FJMP;
159 	c->mem[c->mem_len++] = loop_back;
160@@ -359,5 +390,4 @@ Cog immediates [] = {
161     {"end", cog_end},
162     {"loop", cog_loop},
163     {"until", cog_until},
164-    {NULL, NULL}
165 };