aboutsummaryrefslogtreecommitdiff
path: root/include/object.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/object.h')
-rw-r--r--include/object.h81
1 files changed, 81 insertions, 0 deletions
diff --git a/include/object.h b/include/object.h
new file mode 100644
index 0000000..55ac741
--- /dev/null
+++ b/include/object.h
@@ -0,0 +1,81 @@
+#ifndef CMONKEY_OBJECT_H
+#define CMONKEY_OBJECT_H
+
+#include "ast.h"
+#include "hmap.h"
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+enum object_type {
+ OBJECT_ERROR,
+ OBJECT_NULL,
+ OBJECT_INT,
+ OBJECT_BOOL,
+ OBJECT_RETURN,
+ OBJECT_FUNC,
+};
+
+struct error_object {
+ char *msg;
+};
+
+struct return_object {
+ struct object *value;
+};
+
+struct func_object {
+ struct vector *params; // identifier_expressions
+ struct statement *body;
+};
+
+struct object {
+ enum object_type type;
+ size_t refcount;
+ union {
+ bool boolean;
+ int64_t integer;
+ struct return_object retrn;
+ struct error_object error;
+ struct func_object func;
+ };
+};
+
+struct environment {
+ struct hmap *store;
+ struct environment *outer;
+};
+
+char *object_sprint(struct object *, char *str);
+
+inline const char *object_type_print(enum object_type);
+
+struct object *object_new_int(int64_t val);
+struct object *object_new_error(char *msg);
+struct object *object_new_return(struct object *val);
+struct object *object_new_func(struct expression *);
+
+#define object_new(v) _Generic((v), \
+ int: object_new_int, \
+ int64_t: object_new_int, \
+ char *: object_new_error, \
+ struct object *: object_new_return, \
+ struct expression *: object_new_func \
+ )(v)
+
+void object_ref(struct object *);
+
+void object_unref(struct object *);
+
+struct environment *environment_new_enclosed(struct environment *outer);
+
+#define environment_new() environment_new_enclosed(NULL)
+
+struct object *environment_set(struct environment *,
+ struct slice key, struct object *val);
+
+struct object *environment_get(struct environment *, const struct slice *key);
+
+void environment_destroy(struct environment *);
+
+#endif