-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.h
More file actions
149 lines (121 loc) · 3.06 KB
/
value.h
File metadata and controls
149 lines (121 loc) · 3.06 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef VALUE_H
#define VALUE_H
#include "token.h"
typedef struct AstNode AstNode;
typedef struct Environment Environment;
typedef struct Runtime Runtime;
typedef struct Value Value;
typedef struct NativeFunction NativeFunction;
typedef struct FunctionObject FunctionObject;
typedef struct MethodEntry MethodEntry;
typedef struct ClassObject ClassObject;
typedef struct InstanceField InstanceField;
typedef struct InstanceObject InstanceObject;
typedef struct BoundMethodObject BoundMethodObject;
typedef struct ListObject ListObject;
typedef struct DictEntry DictEntry;
typedef struct DictObject DictObject;
typedef enum {
VAL_NONE,
VAL_BOOL,
VAL_NUMBER,
VAL_STRING,
VAL_NATIVE_FUNCTION,
VAL_FUNCTION,
VAL_CLASS,
VAL_INSTANCE,
VAL_BOUND_METHOD,
VAL_LIST,
VAL_DICT
} ValueType;
struct FunctionObject {
char* name;
Token* params;
int paramCount;
AstNode* body;
Environment* closure;
};
struct MethodEntry {
char* name;
FunctionObject* function;
};
struct ClassObject {
char* name;
AstNode* body;
Environment* closure;
MethodEntry* methods;
int methodCount;
int methodCapacity;
};
struct BoundMethodObject {
InstanceObject* receiver;
FunctionObject* method;
};
struct ListObject {
Value* items;
int count;
int capacity;
};
struct Value {
ValueType type;
union {
int boolean;
double number;
char* string;
NativeFunction* nativeFunction;
FunctionObject* function;
ClassObject* classObject;
InstanceObject* instance;
BoundMethodObject* boundMethod;
ListObject* list;
DictObject* dict;
} as;
};
struct DictEntry {
Value key;
Value value;
};
struct DictObject {
DictEntry* entries;
int count;
int capacity;
};
struct NativeFunction {
const char* name;
int arity;
Value (*fn)(Runtime* runtime, int argCount, Value* args);
};
struct InstanceField {
char* name;
Value value;
};
struct InstanceObject {
ClassObject* classObject;
InstanceField* fields;
int fieldCount;
int fieldCapacity;
};
Value makeNone(void);
Value makeBool(int boolean);
Value makeNumber(double number);
Value makeStringOwned(char* string);
Value makeStringCopy(const char* string);
Value makeNativeFunction(NativeFunction* nativeFunction);
Value makeFunction(FunctionObject* function);
Value makeClass(ClassObject* classObject);
Value makeInstance(InstanceObject* instance);
Value makeBoundMethod(BoundMethodObject* boundMethod);
Value makeList(ListObject* list);
ListObject* createListObject(void);
int listAppend(ListObject* list, Value value);
Value makeDict(DictObject* dict);
DictObject* createDictObject(void);
int dictSet(DictObject* dict, Value key, Value value);
int dictGet(DictObject* dict, const Value* key, Value* outValue);
void freeValue(Value* value);
Value copyValue(const Value* value);
int valueIsTruthy(const Value* value);
int valueEquals(const Value* a, const Value* b);
const char* valueTypeName(const Value* value);
void printValue(const Value* value);
#endif