commit e0a7826
soggy
·
2026-11-09 20:14:05 +0000 UTC
parent 9a1e385
cotton - renamed some of cotton's internals and fixed the variable initialization
3 files changed,
+55,
-48
+10,
-9
1@@ -4,37 +4,38 @@
2 void cotton_init
3 (Cotton *c)
4 {
5- c->stack_p = 0;
6- c->jmp_stack_p = 0;
7- c->instruct_p = 0;
8+ c->stack_ptr = 0;
9+ c->ctrl_stack_ptr = 0;
10+ c->instruction_ptr = 0;
11 c->mem_len = 0;
12
13- for (int i = 0; i < VARS_SIZE; i++) {
14+ for (int i = 0; i < INT_VARS_SIZE; i++) {
15
16- c->vars[i] = 0;
17+ c->int_vars[i] = 0;
18+ c->int_vars_assigned[i] = false;
19 }
20 }
21
22 void push
23 (Cotton *c, int v)
24 {
25- if (c->stack_p >= STACK_SIZE) {
26+ if (c->stack_ptr >= STACK_SIZE) {
27
28 fprintf(stderr, "cotton: stack overflow,., like the website,.., :<\n");
29 return;
30 }
31
32- c->stack[c->stack_p++] = v;
33+ c->stack[c->stack_ptr++] = v;
34 }
35
36 int pop
37 (Cotton *c)
38 {
39- if (c->stack_p <= 0) {
40+ if (c->stack_ptr <= 0) {
41
42 fprintf(stderr, "cotton: stack underflow :<\n");
43 return 0;
44 }
45
46- return c->stack[--c->stack_p];
47+ return c->stack[--c->stack_ptr];
48 }
+22,
-16
1@@ -1,17 +1,20 @@
2 #ifndef COTTON_H
3 #define COTTON_H
4
5-#define OP_PUSH 0
6-#define OP_CALL 1
7-#define OP_KILL 2
8-#define OP_JMP 3
9-#define OP_FJMP 4
10-#define OP_PUSH_STR 5
11-
12-#define STACK_SIZE 256
13-#define JMP_STACK_SIZE 64
14-#define MEM_SIZE 4096
15-#define VARS_SIZE 256
16+#include <stdbool.h>
17+
18+#define OP_PUSH_INT 0
19+#define OP_PUSH_STR 1
20+#define OP_CALL 2
21+#define OP_KILL 3
22+#define OP_JMP 4
23+#define OP_FJMP 5
24+
25+#define STACK_SIZE 256
26+#define CTRL_STACK_SIZE 64
27+#define MEM_SIZE 4096
28+#define INT_VARS_SIZE 256
29+#define STR_VARS_SIZE 256
30
31 typedef struct Cotton Cotton;
32
33@@ -25,17 +28,20 @@ typedef struct {
34 struct Cotton {
35
36 int stack[STACK_SIZE];
37- int stack_p;
38+ int stack_ptr;
39
40- int jmp_stack[JMP_STACK_SIZE];
41- int jmp_stack_p;
42+ int ctrl_stack[CTRL_STACK_SIZE];
43+ int ctrl_stack_ptr;
44
45 int mem[MEM_SIZE];
46- int instruct_p;
47+ int instruction_ptr;
48 int mem_len;
49
50- int vars[VARS_SIZE];
51+ int int_vars[INT_VARS_SIZE];
52+ bool int_vars_assigned[INT_VARS_SIZE];
53
54+ int str_vars[STR_VARS_SIZE];
55+ bool str_vars_assigned[STR_VARS_SIZE];
56 };
57
58 void cotton_init
+23,
-23
1@@ -48,7 +48,7 @@ void cotton_compile
2
3 else if (line[i] == '\0') {
4
5- fprintf(stderr, "cotton: you forgot to close a string.. i cant compile the file until you fix it :P");
6+ fprintf(stderr, "cotton: you forgot to close a string.. i cant compile the file until you fix it :P\n");
7 return;
8 }
9
10@@ -120,7 +120,7 @@ void cotton_compile
11
12 if (is_num) {
13
14- c->mem[c->mem_len++] = OP_PUSH;
15+ c->mem[c->mem_len++] = OP_PUSH_INT;
16 c->mem[c->mem_len++] = atoi(tok);
17 }
18
19@@ -156,56 +156,56 @@ void cotton_compile
20 void cotton_run
21 (Cotton *c)
22 {
23- c->instruct_p = 0;
24+ c->instruction_ptr = 0;
25
26- while (c->instruct_p < c->mem_len) {
27+ while (c->instruction_ptr < c->mem_len) {
28
29- int op = c->mem[c->instruct_p++];
30+ int op = c->mem[c->instruction_ptr++];
31
32 switch (op) {
33
34- case OP_PUSH:
35+ case OP_PUSH_INT:
36
37- push(c, c->mem[c->instruct_p++]);
38+ push(c, c->mem[c->instruction_ptr++]);
39 break;
40
41+ case OP_PUSH_STR:
42+
43+ push(c, c->instruction_ptr);
44+
45+ while (c->mem[c->instruction_ptr] != '\0') {
46+
47+ c->instruction_ptr++;
48+ }
49+
50+ c->instruction_ptr++;
51+ break;
52+
53 case OP_CALL: {
54
55- int id = c->mem[c->instruct_p++];
56+ int id = c->mem[c->instruction_ptr++];
57 builtins[id].code(c);
58 break;
59 }
60
61 case OP_JMP: {
62
63- c->instruct_p = c->mem[c->instruct_p];
64+ c->instruction_ptr = c->mem[c->instruction_ptr];
65 break;
66 }
67
68 case OP_FJMP: {
69
70- int target = c->mem[c->instruct_p++];
71+ int target = c->mem[c->instruction_ptr++];
72
73 if (pop(c) == 0) {
74
75- c->instruct_p = target;
76+ c->instruction_ptr = target;
77 }
78
79 break;
80 }
81
82- case OP_PUSH_STR:
83-
84- push(c, c->instruct_p);
85-
86- while (c->mem[c->instruct_p] != '\0') {
87-
88- c->instruct_p++;
89- }
90-
91- c->instruct_p++;
92- break;
93-
94 case OP_KILL:
95
96 return;