#ifndef CMONKEY_OBJECT_H #define CMONKEY_OBJECT_H #include "ast.h" #include "hmap.h" #include #include 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