commit 33f734d
0uppy
·
2026-07-23 00:46:20 +0000 UTC
parent 141a733
cotton v0.2 - cot can now do math yay (i also noticed some more stuff that clang-format fucked up,, or maybe it was me idk, but its fixed now :D
1 files changed,
+96,
-25
+96,
-25
1@@ -89,8 +89,8 @@ static void draw_char
2
3 // da commands
4
5-static void cmd_print
6-(Cotton *cotton, CottonWindow *cw, char *arg)
7+static void
8+cmd_print(Cotton *cotton, CottonWindow *cw, char *arg)
9 {
10 if (!arg)
11 return;
12@@ -126,8 +126,8 @@ static void cmd_print
13 cottonwindow_update(cw, cotton->video, sizeof(cotton->video[0]) * VIDEO_WIDTH);
14 }
15
16-static void cmd_var
17-(Cotton *cotton, char *arg)
18+static void
19+cmd_var(Cotton *cotton, char *arg)
20 {
21 if (!arg)
22 return;
23@@ -145,25 +145,8 @@ static void cmd_var
24 cotton_store_var(cotton, name, value);
25 }
26
27-static void
28-cmd_wait(Cotton *cotton, char *arg)
29-{
30- if (!arg)
31- return;
32-
33- int seconds = atoi(arg);
34-
35- cotton->ticktock = SDL_GetTicks() + (seconds * 1000);
36-}
37-
38 static void
39-cmd_kill(void)
40-{
41- printf("[cotton stopped]\n");
42- exit(0);
43-}
44-
45-static void cmd_clear(Cotton *cotton, CottonWindow *cw)
46+cmd_clear(Cotton *cotton, CottonWindow *cw)
47 {
48 for (int i = 0; i < VIDEO_WIDTH * VIDEO_HEIGHT; i++) {
49 cotton->video[i] = 0;
50@@ -210,6 +193,17 @@ cmd_color(Cotton *cotton, char *arg)
51 arg);
52 }
53
54+static void
55+cmd_wait(Cotton *cotton, char *arg)
56+{
57+ if (!arg)
58+ return;
59+
60+ int seconds = atoi(arg);
61+
62+ cotton->ticktock = SDL_GetTicks() + (seconds * 1000);
63+}
64+
65 static void
66 cmd_input(Cotton *cotton, char *arg)
67 {
68@@ -221,9 +215,7 @@ cmd_input(Cotton *cotton, char *arg)
69
70 char buffer[VAR_VAL_LEN];
71
72- // kind of a quick and dirty way of implementing input, i know, but, i cant
73- // be arsed to get myself into a potential rabbit hole with SDL so for now i
74- // will leave this to be this way,, sorry,,
75+ // kind of a quick and dirty way of implementing input, i know, but, i cant be arsed to get myself into a potential rabbit hole with SDL so for now i will leave this to be this way,, sorry,,
76 if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
77 buffer[strcspn(buffer, "\r\n")] = '\0';
78
79@@ -231,6 +223,69 @@ cmd_input(Cotton *cotton, char *arg)
80 }
81 }
82
83+static void
84+cmd_kill(void)
85+{
86+ printf("[cotton stopped]\n");
87+ exit(0);
88+}
89+
90+static void
91+cmd_math(Cotton *cotton, char *arg, char op)
92+{
93+ char var_name[64];
94+ char value[64];
95+
96+ if (!arg || sscanf(arg, "%63s %63s", var_name, value) != 2) {
97+ fprintf(stderr, "cot syntax error: usage is '<add, sub, mult, div> <variable> <number OR another variable>'\n");
98+ return;
99+ }
100+
101+ int current = 0;
102+ int amount = 0;
103+
104+ // thank you clang for coming in clutch and telling me to make this const i forgor (skull emoji)
105+ const char *var = get_var_value(cotton, var_name);
106+ if (var) {
107+ current = atoi(var);
108+ }
109+
110+ // and this too, i love clang, clang is the best compiler evah !!!
111+ const char *other = get_var_value(cotton, value);
112+ if (other) {
113+ amount = atoi(other);
114+ } else {
115+ amount = atoi(value);
116+ }
117+
118+ switch (op) {
119+ case '+':
120+ current += amount;
121+ break;
122+
123+ case '-':
124+ current -= amount;
125+ break;
126+
127+ case '*':
128+ current *= amount;
129+ break;
130+
131+ case '/':
132+ current /= amount;
133+ break;
134+
135+ default:
136+ fprintf(stderr, "cot syntax error: cot doesn't know what operator this is :( \n");
137+ return;
138+ }
139+
140+ char buffer[32];
141+ sprintf(buffer, "%d", current);
142+
143+ cotton_store_var(cotton, var_name, buffer);
144+}
145+
146 static void
147 cmd_unknown(const char *cmd)
148 {
149@@ -300,6 +355,22 @@ cotton_interpret(Cotton *cotton, CottonWindow *cw, FILE *file)
150 cmd_kill();
151 }
152
153+ else if (strcmp(cmd, "add") == 0) {
154+ cmd_math(cotton, arg, '+');
155+ }
156+
157+ else if (strcmp(cmd, "sub") == 0) {
158+ cmd_math(cotton, arg, '-');
159+ }
160+
161+ else if (strcmp(cmd, "mult") == 0) {
162+ cmd_math(cotton, arg, '*');
163+ }
164+
165+ else if (strcmp(cmd, "div") == 0) {
166+ cmd_math(cotton, arg, '/');
167+ }
168+
169 else {
170 cmd_unknown(cmd);
171 }