From 4c3b2fec322feb8afd23703866d81f1dda51b6ae Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 11 Feb 2023 09:41:54 -0800 Subject: 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. --- bytecode/value.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 bytecode/value.c (limited to 'bytecode/value.c') 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 +#include + +#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; +} -- cgit v1.3.1