summaryrefslogtreecommitdiffstats
path: root/bytecode/value.c
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2023-02-11 09:41:54 -0800
committerRose Hogenson <rhogenson@posteo.net>2023-02-11 09:41:54 -0800
commit4c3b2fec322feb8afd23703866d81f1dda51b6ae (patch)
treecae4d14c66f639c13198a4bfafc9425cb8aa70bf /bytecode/value.c
parentddb69212f03a82e980144594d809a072b50e7f19 (diff)
downloadsml-4c3b2fec322feb8afd23703866d81f1dda51b6ae.tar.zst
Add a bytecode interpreter.
I wrote it in C because I actually like programming in C. Turns out setting up a Makefile is really easy too.
Diffstat (limited to 'bytecode/value.c')
-rw-r--r--bytecode/value.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/bytecode/value.c b/bytecode/value.c
new file mode 100644
index 0000000..2a15b44
--- /dev/null
+++ b/bytecode/value.c
@@ -0,0 +1,42 @@
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "panic.h"
+
+#include "value.h"
+
+bool is_int(value v)
+{
+ return v & 0x1;
+}
+
+int64_t to_int(value v)
+{
+ if (!is_int(v)) {
+ panicf("value %lx is not an int!\n", v);
+ }
+ return (int64_t) v >> 1;
+}
+
+value from_int(int64_t i)
+{
+ return i << 1 | 0x1;
+}
+
+bool is_pointer(value v)
+{
+ return v && !(v & 0x7);
+}
+
+value *to_pointer(value v)
+{
+ if (!is_pointer(v)) {
+ panicf("value %lx is not a pointer!\n", v);
+ }
+ return (value *) v;
+}
+
+value from_pointer(value *p)
+{
+ return (value) p;
+}