1#include "weave.h"
2#include "cotton.h"
3#include <stdio.h>
4
5void cog_add
6(Cotton *c)
7{
8 int b = pop(c);
9 int a = pop(c);
10
11 push(c, a + b);
12}
13
14void cog_sub
15(Cotton *c)
16{
17 int b = pop(c);
18 int a = pop(c);
19
20 push(c, a - b);
21}
22
23void cog_mul
24(Cotton *c)
25{
26 int b = pop(c);
27 int a = pop(c);
28
29 push(c, a * b);
30}
31
32void cog_div
33(Cotton *c)
34{
35 int b = pop(c);
36 int a = pop(c);
37
38 // Now, i doubt anyone will really be dividing stuff by zero but you never know.., talk about le safety amirite.., heh
39 if (b == 0) {
40
41 fprintf(stderr, "cotton: you cant divide by zero, you dummy :P\n");
42 return;
43 }
44
45 push(c, a / b);
46}
47
48void cog_mod
49(Cotton *c)
50{
51 int b = pop(c);
52 int a = pop(c);
53
54 push(c, a % b);
55}
56
57void cog_dotnum
58(Cotton *c)
59{
60 printf("%d\n", pop(c));
61}
62
63void cog_dotstr
64(Cotton *c)
65{
66 int ptr = pop(c);
67
68 while (c->mem[ptr] != '\0') {
69
70 printf("%c", c->mem[ptr]);
71
72 ptr++;
73 }
74}
75
76// im aware c goes unused here and hence this gives a warning, yes im lazy, might do smth about it another time
77void cog_newline
78(Cotton *c)
79{
80 printf("\n");
81}
82
83// while forth does use ! and @, im quirky and #notlikeothergirls so im changing ! to -> bc i think it makes more sense :P
84void cog_store
85(Cotton *c)
86{
87 int slot = pop(c);
88 int num = pop(c);
89
90 if (slot >= 0 && slot < VARS_SIZE) {
91
92 c->vars[slot] = num;
93 }
94
95 else {
96
97 fprintf(stderr, "cotton: i tried to store somewhere that don't exist,, its in the void now.., :(\n");
98 }
99}
100
101void cog_fetch
102(Cotton *c)
103{
104 int slot = pop(c);
105
106 if (slot >= 0 && slot < VARS_SIZE) {
107
108 push(c, c->vars[slot]);
109 }
110
111 else {
112
113 fprintf(stderr, "cotton: i tried to fetch from somewhere that seemingly don't exist,, erm.,.\n");
114 }
115}
116
117void cog_dup
118(Cotton *c)
119{
120 int a = pop(c);
121
122 push(c, a);
123 push(c, a);
124}
125
126void cog_drop
127(Cotton *c)
128{
129 pop(c);
130}
131
132void cog_swap
133(Cotton *c)
134{
135 int b = pop(c);
136 int a = pop(c);
137
138 push(c, b);
139 push(c, a);
140}
141
142void cog_rotate
143(Cotton *c)
144{
145 int a = pop(c);
146 int b = pop(c);
147 int c_ = pop(c);
148
149 push(c, b);
150 push(c, c_);
151 push(c, a);
152}
153
154void cog_equal
155(Cotton *c)
156{
157 int b = pop(c);
158 int a = pop(c);
159
160 push(c, a == b ? 1 : 0);
161}
162
163void cog_lssr
164(Cotton *c)
165{
166 int b = pop(c);
167 int a = pop(c);
168
169 push(c, a < b ? 1 : 0);
170}
171
172void cog_grtr
173(Cotton *c)
174{
175 int b = pop(c);
176 int a = pop(c);
177
178 push(c, a > b ? 1 : 0);
179}
180
181Cog builtins[] = {
182 {"+", cog_add},
183 {"-", cog_sub},
184 {"*", cog_mul},
185 {"/", cog_div},
186 {"%", cog_mod},
187 {".n", cog_dotnum},
188 {".s", cog_dotstr},
189 {"nl", cog_newline},
190 {"->", cog_store},
191 {"@", cog_fetch},
192 {"dup", cog_dup},
193 {"drop", cog_drop},
194 {"swap", cog_swap},
195 {"rot", cog_rotate},
196 {"=", cog_equal},
197 {"<", cog_lssr},
198 {">", cog_grtr},
199 {NULL, NULL}
200};
201
202/*
203
204 separator comment to split the builtins from immediates xP
205
206*/
207
208void cog_if
209(Cotton *c)
210{
211 if (c->jmp_stack_p >= JMP_STACK_SIZE) {
212
213 fprintf(stderr, "cotton: there are too many nested ifs, calm down!! D:\n");
214 return;
215 }
216
217 c->mem[c->mem_len++] = OP_FJMP;
218 c->jmp_stack[c->jmp_stack_p++] = c->mem_len;
219 c->mem[c->mem_len++] = 0;
220}
221
222void cog_else
223(Cotton *c)
224{
225 if (c->jmp_stack_p <= 0) {
226
227 fprintf(stderr, "cotton: i found an else without an if,., where'd that come from??\n");
228 return;
229 }
230
231 int if_target = c->jmp_stack[--c->jmp_stack_p];
232
233 c->mem[c->mem_len++] = OP_JMP;
234 c->jmp_stack[c->jmp_stack_p++] = c->mem_len;
235 c->mem[c->mem_len++] = 0;
236
237 c->mem[if_target] = c->mem_len;
238}
239
240void cog_end
241(Cotton *c)
242{
243 if (c->jmp_stack_p <= 0) {
244
245 fprintf(stderr, "cotton: i found an end without an if,., where'd that come from??\n");
246 return;
247 }
248
249 int target = c->jmp_stack[--c->jmp_stack_p];
250 c->mem[target] = c->mem_len;
251}
252
253void cog_loop
254(Cotton *c)
255{
256 c->jmp_stack[c->jmp_stack_p++] = c->mem_len;
257}
258
259void cog_until
260(Cotton *c)
261{
262 int loop_back = c->jmp_stack[--c->jmp_stack_p];
263
264 c->mem[c->mem_len++] = OP_FJMP;
265 c->mem[c->mem_len++] = loop_back;
266}
267
268Cog immediates [] = {
269 {"if", cog_if},
270 {"else", cog_else},
271 {"end", cog_end},
272 {"loop", cog_loop},
273 {"until", cog_until},
274 {NULL, NULL}
275};
276