latest soggy/cotton / src / cotton.c
 1#include "cotton.h"
 2#include <stdio.h>
 3
 4void cotton_init
 5(Cotton *c) 
 6{
 7    c->stack_p = 0;
 8    c->jmp_stack_p = 0;
 9    c->instruct_p = 0;
10    c->mem_len = 0;
11
12    for (int i = 0; i < VARS_SIZE; i++) {
13        c->vars[i] = 0;
14    }
15}
16
17void push
18(Cotton *c, int v) 
19{
20    if (c->stack_p >= STACK_SIZE) {
21        fprintf(stderr, "cotton: stack overflow,., like the website,.., :<\n");
22        return;
23    }
24
25    c->stack[c->stack_p++] = v;
26}
27
28int pop
29(Cotton *c) 
30{
31    if (c->stack_p <= 0) {
32        fprintf(stderr, "cotton: stack underflow :<\n");
33        return 0;
34    }
35
36    return c->stack[--c->stack_p];
37}