summary refs log tree commit diff
path: root/src/cccl.c
diff options
context:
space:
mode:
authorNakidai <plaza521@inbox.ru>2024-07-05 00:04:56 +0300
committerNakidai <plaza521@inbox.ru>2024-07-05 00:04:56 +0300
commita9c159f5f7bf3479c3236735960597b4bc36a204 (patch)
treef325e249f11a4bb8518f01151114b1239207a33b /src/cccl.c
download3cl-a9c159f5f7bf3479c3236735960597b4bc36a204.tar.gz
3cl-a9c159f5f7bf3479c3236735960597b4bc36a204.zip
Add code
Diffstat (limited to 'src/cccl.c')
-rw-r--r--src/cccl.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/cccl.c b/src/cccl.c
new file mode 100644
index 0000000..1803fcf
--- /dev/null
+++ b/src/cccl.c
@@ -0,0 +1,80 @@
+#include "cccl.h"
+#include "types.h"
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "cvector.h"
+#include "utils.h"
+
+
+static cccl pc = {0};
+
+void cccl_init(s8 *filename)
+{
+    memset(&pc, 0, sizeof(pc));
+
+    cvector_init(pc.stack, 1, NULL);
+    cvector_init(pc.variables, 1, NULL);
+    cvector_init(pc.procedures, 1, NULL);
+    cvector_init(pc.ep_stack, 1, NULL);
+    if (errno) die(1, strerror(errno));
+
+    pc.filename = filename;
+}
+
+void cccl_free()
+{
+    cvector_free(pc.stack);
+    cvector_free(pc.variables);
+    cvector_free(pc.procedures);
+    cvector_free(pc.ep_stack);
+    free(pc.code);
+}
+
+void cccl_read(void)
+{
+    s8 chr;
+    FILE *file = fopen(pc.filename, "r");
+    if (errno) die(1, strerror(errno));
+        fseek(file, 0, SEEK_END);
+        pc.size = ftell(file);
+        fseek(file, 0, SEEK_SET);
+        pc.code = calloc(pc.size + 1, sizeof(s8));
+
+        for (u32 i = 0; i < pc.size; ++i)
+        {
+            if ((chr = fgetc(file)) == EOF)
+            {
+                if (errno)
+                {
+                    fclose(file);
+                    die(1, strerror(errno));
+                }
+                else
+                {
+                    break;
+                }
+            }
+            pc.code[i] = chr;
+        }
+    fclose(file);
+}
+
+void cccl_run(void)
+{
+    bool is_comment;
+    for (pc.ep = 0; pc.ep < pc.size; ++pc.ep)
+    {
+        if (is_comment && pc.code[pc.ep] == '\n')
+            is_comment = false;
+        switch (pc.code[pc.ep])
+        {
+        case '/':
+            is_comment = true;
+            break;
+        }
+    }
+}