-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.h
208 lines (124 loc) · 3.87 KB
/
value.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#ifndef PIELANG_VALUE_H
#define PIELANG_VALUE_H
#include "bool.h"
#include "lexer.h"
#include "ast.h"
#include "hashtable.h"
#define VALUE_TYPE_COUNT 10
typedef enum {
ValueTypeNullValue = 0,
ValueTypeBoolValue,
ValueTypeIntegerValue,
ValueTypeFloatValue,
ValueTypeStringValue,
ValueTypeFunctionValue,
ValueTypeSystemFunctionValue,
ValueTypeTupleValue,
ValueTypeListValue,
ValueTypeGeneratorValue,
} ValueType;
typedef enum {
GeneratorValueTypeNumber = 1,
GeneratorValueTypeArray,
} GeneratorValueType;
struct Value {
ValueType value_type;
size_t linked_variable_count;
struct Value *context_value;
};
struct BoolValue {
struct Value value;
bool bool_value;
};
struct IntegerValue {
struct Value value;
long long int integer_value;
};
struct FloatValue {
struct Value value;
long double float_value;
};
struct StringValue {
struct Value value;
char *string_value;
size_t length;
};
struct TupleValue {
struct Value value;
struct Value **items;
size_t length;
bool has_finished;
};
struct ListValue {
struct Value value;
struct Value **items;
size_t length;
bool has_finished;
};
struct FunctionValue {
struct Value value;
Block *block;
char **arguments;
size_t argument_count;
};
typedef struct Value *(SystemFunctionCallback)(struct Value *context_value, struct TupleValue *);
struct SystemFunctionValue {
struct Value value;
ValueType context_value_type;
SystemFunctionCallback *callback;
};
struct GeneratorValue {
struct Value value;
GeneratorValueType generator_value_type;
struct Value **target_values;
long long int start_value;
long long int end_value;
long long int index;
};
struct Variable {
char *variable_name;
struct Value *value;
bool is_readonly;
};
typedef struct Value Value;
typedef struct BoolValue BoolValue;
typedef struct IntegerValue IntegerValue;
typedef struct FloatValue FloatValue;
typedef struct StringValue StringValue;
typedef struct TupleValue TupleValue;
typedef struct ListValue ListValue;
typedef struct FunctionValue FunctionValue;
typedef struct SystemFunctionValue SystemFunctionValue;
typedef struct GeneratorValue GeneratorValue;
typedef struct Variable Variable;
char *convert_to_string(Value *value);
bool convert_to_bool(Value *value);
long long int convert_to_integer(Value *value);
Value *new_null_value();
Value *new_bool_value(bool val);
Value *new_bool_value_from_literal(BoolLiteral *literal);
Value *new_integer_value(long long int val);
Value *new_integer_value_from_literal(IntegerLiteral *literal);
Value *new_float_value(long double val);
Value *new_float_value_from_literal(FloatLiteral *literal);
Value *new_string_value(char *val, size_t length);
Value *new_string_value_from_literal(StringLiteral *literal);
Value *new_function_value(Block *block, char **arguments, size_t argument_count);
Value *new_system_function_value(ValueType context_value_type, SystemFunctionCallback *callback);
Value *new_tuple_value(Value **items, size_t length, bool has_finished);
Value *new_list_value(Value **items, size_t length, bool has_finished);
Value *new_generator_value(GeneratorValueType generator_value_type, Value *first_value, Value *second_value);
Value *fetch_value_from_generator_value(GeneratorValue *generator_value);
Value *convert_to_generator_value(Value *value);
Value *convert_to_string_value(Value *value);
Value *convert_to_bool_value(Value *value);
Value *convert_to_integer_value(Value *value);
Value *copy_value(Value *value);
Variable *new_variable(char *variable_name, Value *value);
HashTable *new_variable_map(size_t size);
void variable_map_set(HashTable *variable_map, Variable *variable);
Variable *variable_map_get(HashTable *variable_map, char *variable_name);
void free_variable_map(HashTable *variable_map);
void free_value(Value *value);
void free_variable(Variable *variable);
#endif //PIELANG_VALUE_H