aboutsummaryrefslogtreecommitdiff
path: root/lisp/tag.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2025-05-14 21:12:58 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2025-05-15 22:25:45 +0100
commit12de1e8db90bccd5a0eefd21075f07c7b7e3dfaa (patch)
tree0434141f2bfd24207a2864f613a1c2e3ee7181fc /lisp/tag.c
parentba5c0a4579ece5d53c009a14d00e683e70b982f4 (diff)
downloadoats-12de1e8db90bccd5a0eefd21075f07c7b7e3dfaa.tar.gz
oats-12de1e8db90bccd5a0eefd21075f07c7b7e3dfaa.tar.bz2
oats-12de1e8db90bccd5a0eefd21075f07c7b7e3dfaa.zip
Refactor for cleanliness
Move files into separate folders for ease of reading, include source directory so we can use angle bracket includes, adjust build system to make directories for objects
Diffstat (limited to 'lisp/tag.c')
-rw-r--r--lisp/tag.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/lisp/tag.c b/lisp/tag.c
new file mode 100644
index 0000000..26db6d5
--- /dev/null
+++ b/lisp/tag.c
@@ -0,0 +1,94 @@
+/* Copyright (C) 2025 Aryadev Chavali
+
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
+ * details.
+
+ * You may distribute and modify this code under the terms of the GNU General
+ * Public License Version 2, which you should have received a copy of along with
+ * this program. If not, please go to <https://www.gnu.org/licenses/>.
+
+ * Created: 2025-04-06
+ * Description:
+ */
+
+#include <lisp/tag.h>
+#include <string.h>
+
+lisp_t *tag_int(i64 i)
+{
+ return TAG((u64)i, INT);
+}
+
+lisp_t *tag_cons(void *ptr)
+{
+ return TAG((u64)ptr, CONS);
+}
+
+lisp_t *tag_sym(void *ptr)
+{
+ return TAG((u64)ptr, SYM);
+}
+
+lisp_t *tag_ssym(const char *data, size_t size)
+{
+ assert(size <= 7);
+ u8 buffer[sizeof(u64)];
+ memset(buffer, 0, sizeof(buffer));
+ // in 8 bits we have:
+ // size - 3 bits (up to 7)
+ // tag - 5 bits
+ buffer[0] = size;
+ buffer[0] <<= SHIFT_SSYM;
+ buffer[0] |= TAG_SSYM;
+ memcpy(buffer + 1, data, size);
+ u64 word = 0;
+ memcpy(&word, buffer, sizeof(u64));
+ return (lisp_t *)word;
+}
+
+lisp_t *tag_bool(bool b)
+{
+ return TAG((u64)b, BOOL);
+}
+
+lisp_t *tag_vec(void *ptr)
+{
+ return TAG((u64)ptr, VEC);
+}
+
+lisp_t *tag_str(void *ptr)
+{
+ return TAG((u64)ptr, STR);
+}
+
+lisp_t *tag_char(u32 codepoint)
+{
+ u64 w = codepoint;
+ return TAG(w, CHAR);
+}
+
+enum Tag tag_get(lisp_t *ptr)
+{
+ static_assert(NUM_TAGS == 9);
+ if (!ptr)
+ return TAG_NIL;
+ else if (IS_TAG(ptr, INT))
+ return TAG_INT;
+ else if (IS_TAG(ptr, CHAR))
+ return TAG_CHAR;
+ else if (IS_TAG(ptr, SYM))
+ return TAG_SYM;
+ else if (IS_TAG(ptr, SSYM))
+ return TAG_SSYM;
+ else if (IS_TAG(ptr, BOOL))
+ return TAG_BOOL;
+ else if (IS_TAG(ptr, VEC))
+ return TAG_VEC;
+ else if (IS_TAG(ptr, STR))
+ return TAG_STR;
+ else if (IS_TAG(ptr, CONS))
+ return TAG_CONS;
+ return 0;
+}