commit e7d8581
0uppy
·
2026-10-13 16:57:53 +0000 UTC
parent 97916f8
misc - formatted my code to keep it a bit more consistent xP
5 files changed,
+27,
-21
+4,
-6
1@@ -66,9 +66,8 @@ void cog_dotstr
2 int ptr = pop(c);
3
4 while (c->mem[ptr] != '\0') {
5-
6+
7 printf("%c", c->mem[ptr]);
8-
9 ptr++;
10 }
11 }
12@@ -210,7 +209,7 @@ void cog_if
13 {
14 if (c->jmp_stack_p >= JMP_STACK_SIZE) {
15
16- fprintf(stderr, "cotton: there are too many nested ifs, calm down!! D:\n");
17+ fprintf(stderr, "cotton: there are too many nested ifs, calm down!! D:\n");
18 return;
19 }
20
21@@ -224,7 +223,7 @@ void cog_else
22 {
23 if (c->jmp_stack_p <= 0) {
24
25- fprintf(stderr, "cotton: i found an else without an if,., where'd that come from??\n");
26+ fprintf(stderr, "cotton: i found an else without an if,., where'd that come from??\n");
27 return;
28 }
29
30@@ -242,7 +241,7 @@ void cog_end
31 {
32 if (c->jmp_stack_p <= 0) {
33
34- fprintf(stderr, "cotton: i found an end without an if,., where'd that come from??\n");
35+ fprintf(stderr, "cotton: i found an end without an if,., where'd that come from??\n");
36 return;
37 }
38
39@@ -273,4 +272,3 @@ Cog immediates [] = {
40 {"until", cog_until},
41 {NULL, NULL}
42 };
43-
+4,
-1
1@@ -10,6 +10,7 @@ void cotton_init
2 c->mem_len = 0;
3
4 for (int i = 0; i < VARS_SIZE; i++) {
5+
6 c->vars[i] = 0;
7 }
8 }
9@@ -18,6 +19,7 @@ void push
10 (Cotton *c, int v)
11 {
12 if (c->stack_p >= STACK_SIZE) {
13+
14 fprintf(stderr, "cotton: stack overflow,., like the website,.., :<\n");
15 return;
16 }
17@@ -29,7 +31,8 @@ int pop
18 (Cotton *c)
19 {
20 if (c->stack_p <= 0) {
21- fprintf(stderr, "cotton: stack underflow :<\n");
22+
23+ fprintf(stderr, "cotton: stack underflow :<\n");
24 return 0;
25 }
26
+5,
-1
1@@ -16,12 +16,15 @@
2 typedef struct Cotton Cotton;
3
4 typedef struct {
5+
6 char *name;
7 void (*code)(Cotton *c);
8+
9 } Cog;
10
11 struct Cotton {
12- int stack[STACK_SIZE];
13+
14+ int stack[STACK_SIZE];
15 int stack_p;
16
17 int jmp_stack[JMP_STACK_SIZE];
18@@ -32,6 +35,7 @@ struct Cotton {
19 int mem_len;
20
21 int vars[VARS_SIZE];
22+
23 };
24
25 void cotton_init
+9,
-9
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@@ -41,9 +41,8 @@ void cotton_compile
11 for (i = string_start; line[i] != '\0'; i++) {
12
13 if (line[i] == '"') {
14-
15+
16 line[i] = '\0';
17-
18 break;
19 }
20
21@@ -166,20 +165,20 @@ void cotton_run
22 switch (op) {
23
24 case OP_PUSH:
25+
26 push(c, c->mem[c->instruct_p++]);
27-
28 break;
29
30 case OP_CALL: {
31+
32 int id = c->mem[c->instruct_p++];
33 builtins[id].code(c);
34-
35 break;
36 }
37
38 case OP_JMP: {
39+
40 c->instruct_p = c->mem[c->instruct_p];
41-
42 break;
43 }
44
45@@ -188,9 +187,10 @@ void cotton_run
46 int target = c->mem[c->instruct_p++];
47
48 if (pop(c) == 0) {
49+
50 c->instruct_p = target;
51 }
52-
53+
54 break;
55 }
56
57@@ -199,11 +199,11 @@ void cotton_run
58 push(c, c->instruct_p);
59
60 while (c->mem[c->instruct_p] != '\0') {
61+
62 c->instruct_p++;
63 }
64
65- c->instruct_p++;
66-
67+ c->instruct_p++;
68 break;
69
70 case OP_KILL:
+5,
-4
1@@ -6,14 +6,14 @@ int main
2 (int argc, char **argv)
3 {
4 if (argc < 2) {
5- fprintf(stderr, "cotton: cotton couldn't find any .cot files to run :P\n");
6- return 1;
7+ fprintf(stderr, "cotton: cotton couldn't find any .cot files to run :P\n");
8+ return 1;
9 }
10
11 FILE *f = fopen(argv[1], "r");
12
13 if (!f) {
14- fprintf(stderr, "cotton: couldn't open \"%s\", is this even a file.,.? :<\n", argv[1]);
15+ fprintf(stderr, "cotton: couldn't open \"%s\", is this even a file.,.? :<\n", argv[1]);
16 return 1;
17 }
18
19@@ -22,11 +22,12 @@ int main
20
21 char line[256];
22 while (fgets(line, sizeof(line), f)) {
23+
24 cotton_compile(&c, line);
25 }
26
27 fclose(f);
28-
29+
30 c.mem[c.mem_len++] = OP_KILL;
31
32 cotton_run(&c);