summaryrefslogtreecommitdiffstats
path: root/bytecode/encoding.h
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/encoding.h
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/encoding.h')
-rw-r--r--bytecode/encoding.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/bytecode/encoding.h b/bytecode/encoding.h
new file mode 100644
index 0000000..49357f6
--- /dev/null
+++ b/bytecode/encoding.h
@@ -0,0 +1,61 @@
+#ifndef _ENCODING_H_
+#define _ENCODING_H_
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "slice.h"
+#include "value.h"
+
+typedef uint8_t local;
+
+// An arg can be a value or a constant.
+struct arg {
+ local local;
+ value constant;
+};
+
+enum opcode {
+ OALLOC = 1,
+ OCALL = 2,
+ OPOKE = 3,
+ OPEEK = 4,
+ OSHUF = 5,
+ OEXIT = 6,
+};
+
+struct op {
+ enum opcode code;
+ union {
+ struct {
+ local out;
+ struct arg size;
+ } alloc;
+ struct {
+ uint8_t offset;
+ local pointer;
+ struct arg value;
+ } poke;
+ struct {
+ local out;
+ uint8_t offset;
+ struct arg value;
+ } peek;
+ struct {
+ local out;
+ struct arg value;
+ } shuf;
+ struct {
+ struct arg value;
+ } exit;
+ };
+};
+
+struct result {
+ struct op op;
+ int n;
+};
+
+struct result parse(struct slice);
+
+#endif