aboutsummaryrefslogtreecommitdiff
path: root/include/object.h
blob: 55ac7419dba3999b0bb197ef3dc791f608ae41d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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