commit 01b98b6
0uppy
·
2026-07-22 22:16:49 +0000 UTC
parent 6213c59
cotton v0.2 - cot now has variables!! + i tweaked the interpreter's error messages a wee bit :P
3 files changed,
+88,
-4
+29,
-0
1@@ -2,6 +2,7 @@
2 #include <string.h>
3 #include <time.h>
4 #include <stdlib.h>
5+#include <stdio.h>
6
7 void cotton_init(Cotton *c)
8 {
9@@ -12,3 +13,31 @@ void cotton_init(Cotton *c)
10 c->c_cottolette = COTTOLETTE[1];
11
12 }
13+
14+void cotton_store_var(Cotton *cotton, const char *name, const char *value)
15+{
16+ for (int i = 0; i < cotton->var_count; i++)
17+ {
18+ if (strcmp(cotton->vars[i].name, name) == 0)
19+ {
20+ strncpy(cotton->vars[i].value, value, VAR_VAL_LEN - 1);
21+ cotton->vars[i].value[VAR_VAL_LEN - 1] = '\0';
22+ return;
23+ }
24+ }
25+
26+ if (cotton->var_count < VARS)
27+ {
28+ strncpy(cotton->vars[cotton->var_count].name, name, VAR_NAME_LEN - 1);
29+ cotton->vars[cotton->var_count].name[VAR_NAME_LEN - 1] = '\0';
30+
31+ strncpy(cotton->vars[cotton->var_count].value, value, VAR_VAL_LEN - 1);
32+ cotton->vars[cotton->var_count].value[VAR_VAL_LEN - 1] = '\0';
33+
34+ cotton->var_count++;
35+ }
36+ else
37+ {
38+ fprintf(stderr, "cotton ran out of variable slots :( - max ammount is %d\n", VARS);
39+ }
40+}
+14,
-0
1@@ -9,6 +9,10 @@
2
3 #define COTTOLETTE_SIZE 7 // would be 6 but due to high demand we invited green into the mix because green is cool <3
4
5+#define VARS 32 // i think this is enough,, not like cot programs can be that complex rn anyway :P
6+#define VAR_NAME_LEN 16
7+#define VAR_VAL_LEN 32
8+
9 // i might work on better names another time xP
10 static const uint32_t COTTOLETTE[COTTOLETTE_SIZE] =
11 {
12@@ -21,6 +25,12 @@ static const uint32_t COTTOLETTE[COTTOLETTE_SIZE] =
13 0xFFD0F0C0, // 6 - the dude nobody invited (geen)
14 };
15
16+typedef struct
17+{
18+ char name[VAR_NAME_LEN];
19+ char value[VAR_VAL_LEN];
20+} CotVar;
21+
22 typedef struct
23 {
24 uint8_t memory[MEMORY_SIZE];
25@@ -33,8 +43,12 @@ typedef struct
26 uint32_t ticktock;
27 uint32_t c_cottolette; //id write "current_cottolete" but that would be alot to type and im lazyyyyyyy bwaaaaaa
28
29+ CotVar vars[VARS];
30+ int var_count;
31+
32 } Cotton;
33
34 void cotton_init(Cotton *c);
35+void cotton_store_var(Cotton *cotton, const char *name, const char *value);
36
37 #endif
+45,
-4
1@@ -13,7 +13,7 @@
2 #define MAX_LINE 256
3
4
5-// da string stuff
6+// da string AND variable stuff
7
8 static void strip_newline(char *s)
9 {
10@@ -43,7 +43,7 @@ static char *strip_quotes(char *s)
11
12 if (end == NULL)
13 {
14- fprintf(stderr, "looks like you forgot a quote at the end xP\n");
15+ fprintf(stderr, "cot syntax error : looks like you forgot a quote at the end xP\n");
16 return s;
17 }
18
19@@ -54,6 +54,17 @@ static char *strip_quotes(char *s)
20 return s;
21 }
22
23+static const char *get_var_value(Cotton *cotton, const char *name)
24+{
25+ for (int i = 0; i < cotton->var_count; i++)
26+ {
27+ if (strcmp(cotton->vars[i].name, name) == 0)
28+ {
29+ return cotton->vars[i].value;
30+ }
31+ }
32+ return NULL;
33+}
34
35 // da graphics stuff
36
37@@ -90,6 +101,12 @@ static void cmd_print(Cotton *cotton, CottonWindow *cw, char *arg)
38
39 char *text = strip_quotes(arg);
40
41+ const char *value = get_var_value(cotton, text);
42+ if (value != NULL)
43+ {
44+ text = (char *)value;
45+ }
46+
47 printf("%s\n", text);
48
49 for (int i = 0; text[i] != '\0'; i++)
50@@ -118,6 +135,25 @@ static void cmd_print(Cotton *cotton, CottonWindow *cw, char *arg)
51
52 }
53
54+static void cmd_var(Cotton *cotton, char *arg)
55+{
56+ if (!arg)
57+ return;
58+
59+ char *space = strchr(arg, ' ');
60+ if (!space)
61+ {
62+ fprintf(stderr, "cot syntax error: you forgor to put a value for your variable xP\n");
63+ return;
64+ }
65+
66+ *space = '\0';
67+ char *name = arg;
68+ char *value = space + 1;
69+
70+ cotton_store_var(cotton, name, value);
71+}
72+
73 static void cmd_wait(Cotton *cotton, char *arg)
74 {
75 if (!arg)
76@@ -173,7 +209,7 @@ static void cmd_color(Cotton *cotton, char *arg)
77 cotton->c_cottolette = COTTOLETTE[6];
78
79 else
80- fprintf(stderr, "cotton doesn't know the color \"%s\" sorry,, :(\n", arg);
81+ fprintf(stderr, "cot syntax error: cotton doesn't know the color \"%s\" sorry,, :(\n", arg);
82 }
83
84 static void cmd_input(CottonWindow *cw, char *arg)
85@@ -186,7 +222,7 @@ static void cmd_input(CottonWindow *cw, char *arg)
86
87 static void cmd_unknown(const char *cmd)
88 {
89- fprintf(stderr, "cotton doesn't know what \"%s\" means, care to type that out again? :<\n", cmd);
90+ fprintf(stderr, "cot syntax error: cotton doesn't know what \"%s\" means, care to type that out again? :<\n", cmd);
91 }
92
93
94@@ -229,6 +265,11 @@ void cotton_interpret(Cotton *cotton, CottonWindow *cw, FILE *file)
95 cmd_print(cotton, cw, arg);
96 }
97
98+ else if (strcmp(cmd, "var") == 0)
99+ {
100+ cmd_var(cotton, arg);
101+ }
102+
103 else if (strcmp(cmd, "clear") == 0)
104 {
105 cmd_clear(cotton, cw);