-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.h
executable file
·117 lines (98 loc) · 2.99 KB
/
object.h
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef CLOX_OBJECT_H
#define CLOX_OBJECT_H
#include "common.h"
#include "chunk.h"
#include "table.h"
#include "value.h"
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
#define IS_BOUND_METHOD(value) is_obj_type(value, OBJ_BOUND_METHOD)
#define IS_CLASS(value) is_obj_type(value, OBJ_CLASS)
#define IS_CLOSURE(value) is_obj_type(value, OBJ_CLOSURE)
#define IS_FUNCTION(value) is_obj_type(value, OBJ_FUNCTION)
#define IS_INSTANCE(value) is_obj_type(value, OBJ_INSTANCE)
#define IS_NATIVE(value) is_obj_type(value, OBJ_NATIVE)
#define IS_STRING(value) is_obj_type(value, OBJ_STRING)
#define AS_BOUND_METHOD(value) ((obj_bound_method_t *)AS_OBJ(value))
#define AS_CLASS(value) ((obj_class_t *)AS_OBJ(value))
#define AS_CLOSURE(value) ((obj_closure_t *)AS_OBJ(value))
#define AS_FUNCTION(value) ((obj_function_t *)AS_OBJ(value))
#define AS_INSTANCE(value) ((obj_instance_t *)AS_OBJ(value))
#define AS_NATIVE(value) \
(((obj_native_fn_t *)AS_OBJ(value))->function)
#define AS_STRING(value) ((obj_string_t *)AS_OBJ(value))
#define AS_CSTRING(value) (((obj_string_t *)AS_OBJ(value))->chars)
typedef enum {
OBJ_BOUND_METHOD,
OBJ_CLASS,
OBJ_CLOSURE,
OBJ_FUNCTION,
OBJ_INSTANCE,
OBJ_NATIVE,
OBJ_STRING,
OBJ_UPVALUE
} obj_type_t;
struct obj {
obj_type_t type;
bool is_marked;
struct obj* next;
};
struct obj_string {
object_t obj;
int length;
char* chars;
uint32_t hash;
};
typedef struct obj_upvalue {
object_t obj;
value_t* location;
value_t closed;
struct obj_upvalue* next;
} obj_upvalue_t;
typedef struct {
object_t obj;
int arity;
int upvalue_count;
chunk_t chunk;
obj_string_t* name;
} obj_function_t;
typedef struct {
object_t obj;
obj_function_t* function;
obj_upvalue_t **upvalues;
int upvalue_count;
} obj_closure_t;
typedef struct {
object_t obj;
obj_string_t* name;
table_t methods;
} obj_class_t;
typedef struct {
object_t obj;
obj_class_t* klass;
table_t fields;
} obj_instance_t;
typedef struct {
object_t obj;
value_t receiver;
obj_closure_t* method;
} obj_bound_method_t;
typedef value_t (*native_fn_t)(int arg_count, value_t* args);
typedef struct {
object_t obj;
native_fn_t function;
} obj_native_fn_t;
obj_bound_method_t* new_bound_method(value_t receiver,
obj_closure_t* method);
obj_class_t* new_class(obj_string_t* name);
obj_closure_t* new_closure(obj_function_t* function);
obj_function_t* new_function();
obj_instance_t* new_instance(obj_class_t* klass);
obj_native_fn_t* new_native(native_fn_t function);
obj_string_t* take_string(char* chars, int length);
obj_string_t* copy_string(const char* chars, int length);
obj_upvalue_t* new_upvalue(value_t* slot);
void print_object(value_t value);
static inline bool is_obj_type(value_t value, obj_type_t type) {
return IS_OBJ(value) && AS_OBJ(value)->type == type;
}
#endif