about summary refs log tree commit diff
path: root/src/3cl.c
diff options
context:
space:
mode:
authorNakidai <plaza521@inbox.ru>2024-08-23 20:43:31 +0300
committerNakidai <plaza521@inbox.ru>2024-08-23 20:43:31 +0300
commit2b0e05cbc1e4d9beccd3a5867c8730880f6ecc10 (patch)
treef0e5e31e6259e0b6ea940c1d4c394b976a4bac74 /src/3cl.c
parent9e4058194742794f7742f19cb1a0bb3451ce22ea (diff)
download3cl-2b0e05cbc1e4d9beccd3a5867c8730880f6ecc10.tar.gz
3cl-2b0e05cbc1e4d9beccd3a5867c8730880f6ecc10.zip
Start to rewriting code
Since there's some UB in the master I decided to rewrite code from
scratch again. I hope that attempt will be better :D
Diffstat (limited to 'src/3cl.c')
-rw-r--r--src/3cl.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/3cl.c b/src/3cl.c
new file mode 100644
index 0000000..c8016a9
--- /dev/null
+++ b/src/3cl.c
@@ -0,0 +1,74 @@
+#include "3cl.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "instruction.h"
+
+
+int ccl_init(struct CCL *ccl, const char *code, int (*in)(), void (*out)(int))
+{
+    *ccl = (struct CCL)
+    {
+        .code = code,
+        .in = in,
+        .out = out,
+        .rootframe = (struct CCLFrame)
+        {
+            .prev = NULL, .next = NULL,
+            .type = CCL_ROOT,
+            .ep = 0,
+            .vars = (struct CCLVariable)
+            {
+                .prev = NULL,
+                .next = NULL,
+                .name = '_',
+                .value = 0
+            },
+        },
+        .stack = (struct CCLStack)
+        {
+            .length = CCL_STACKSIZE,
+            .stack = (CCLNum *)malloc(CCL_STACKSIZE)
+        },
+    };
+
+    return errno;
+}
+
+void ccl_free(struct CCL *ccl)
+{
+    free(ccl->stack.stack);
+    if (ccl->rootframe.next != NULL)
+    {
+        for (struct CCLFrame *frame = ccl->rootframe.next, *new;;)
+        {
+            if (frame->vars.next != NULL)
+            {
+                for (struct CCLVariable *var = frame->vars.next, *new;;)
+                {
+                    if (var->next == NULL)
+                        break;
+                    new = var->next;
+                    free(var);
+                    var = new;
+                }
+            }
+
+            if (frame->next == NULL)
+                break;
+            new = frame->next;
+            free(frame);
+            frame = new;
+        }
+    }
+}
+
+void ccl_exec(struct CCL *ccl)
+{
+    struct CCLFrame *curframe = &ccl->rootframe;
+
+    for (;;++curframe->ep)
+        curframe = ccl_instruction(ccl, curframe);
+}