aboutsummaryrefslogtreecommitdiff
path: root/src/compiler/source_code
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2024-09-25 19:47:29 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2024-09-25 19:47:29 +0330
commitc409b8aaf6b6f63bd68a3356e146ab80b2ec8c4b (patch)
tree65ea5801fd910fc6bcff3f2e8f06b5fd7d249c78 /src/compiler/source_code
parentf79290084948f3cf140395c270c07cf29ca58e8d (diff)
fixed multiple variable definition bug
tried to implement import
Diffstat (limited to 'src/compiler/source_code')
-rw-r--r--src/compiler/source_code/source_code.c57
-rw-r--r--src/compiler/source_code/source_code.h24
2 files changed, 81 insertions, 0 deletions
diff --git a/src/compiler/source_code/source_code.c b/src/compiler/source_code/source_code.c
new file mode 100644
index 0000000..7f1c8fa
--- /dev/null
+++ b/src/compiler/source_code/source_code.c
@@ -0,0 +1,57 @@
+#include "source_code.h"
+
+#include <stdlib.h>
+#include <utils/memory/memory.h>
+
+Code makeCode(char *filePath, char *code) {
+ const Code result = {
+ .filePath = filePath,
+ .code = code,
+ };
+ return result;
+}
+
+SourceCode makeSourceCode() {
+ const SourceCode result = {
+ .codes = NULL,
+ .size = 0,
+ };
+ return result;
+}
+
+SourceCode *newSourceCode() {
+ SourceCode *result = a404m_malloc(sizeof(*result));
+ result->codes = NULL;
+ result->size = 0;
+ return result;
+}
+
+void pushToSourceCode(SourceCode *sourceCode, Code *code) {
+ size_t size = a404m_malloc_usable_size(sourceCode->codes) / sizeof(Code *);
+ if (sourceCode->size == size) {
+ size += size / 2 + 1;
+ sourceCode->codes = a404m_realloc(sourceCode->codes, size * sizeof(Code *));
+ }
+ sourceCode->codes[sourceCode->size] = code;
+ sourceCode->size += 1;
+}
+
+void deleteSourceCodeInners(SourceCode sourceCode) {
+ if (sourceCode.size != 0) {
+ for (size_t i = 0; i < sourceCode.size; ++i) {
+ deleteCode(sourceCode.codes[i]);
+ }
+ free(sourceCode.codes);
+ }
+}
+
+void deleteSourceCode(SourceCode *sourceCode) {
+ deleteSourceCodeInners(*sourceCode);
+ free(sourceCode);
+}
+
+void deleteCode(Code *code) {
+ free(code->filePath);
+ free(code->code);
+ free(code);
+}
diff --git a/src/compiler/source_code/source_code.h b/src/compiler/source_code/source_code.h
new file mode 100644
index 0000000..7e5b42d
--- /dev/null
+++ b/src/compiler/source_code/source_code.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include <stdio.h>
+
+typedef struct Code {
+ char *filePath;
+ char *code;
+} Code;
+
+typedef struct SourceCode {
+ Code **codes;
+ size_t size;
+} SourceCode;
+
+extern Code makeCode(char *filePath, char *code);
+
+extern SourceCode makeSourceCode();
+extern SourceCode *newSourceCode();
+
+extern void pushToSourceCode(SourceCode *sourceCode,Code *code);
+
+extern void deleteSourceCodeInners(SourceCode sourceCode);
+extern void deleteSourceCode(SourceCode *sourceCode);
+extern void deleteCode(Code *code);