-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Chaos.c
404 lines (338 loc) · 11.8 KB
/
Chaos.c
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Description: Chaos C extension interface
*
* Copyright (c) 2019-2021 Chaos Language Development Authority <info@chaos-lang.org>
*
* License: GNU General Public License v3.0
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*
* Authors: M. Mert Yildiran <me@mertyildiran.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "interpreter/function.h"
#ifdef CHAOS_COMPILER
# include "../Chaos.h"
#else
# include "Chaos.h"
#endif
int defineFunction(
char *name,
enum Type type,
enum Type secondary_type,
char *params_name[],
unsigned params_type[],
unsigned params_secondary_type[],
unsigned short params_length,
KaosValue optional_params[],
unsigned short optional_params_length
) {
startFunctionParameters();
unsigned short optional_params_starts_from = params_length - optional_params_length;
unsigned short j = optional_params_length - 1;
if (params_length > 0) {
for (int i = (int) (params_length - 1); i >= optional_params_starts_from; i--) {
enum Type optional_param_type = params_type[i];
struct KaosValue optional_param_value = optional_params[j];
switch (optional_param_type) {
case K_BOOL:
addFunctionOptionalParameterBool(params_name[i], optional_param_value.b);
break;
case K_NUMBER:
addFunctionOptionalParameterFloat(params_name[i], optional_param_value.f);
break;
case K_STRING:
addFunctionOptionalParameterString(params_name[i], optional_param_value.s);
break;
default:
throw_error(E_ILLEGAL_VARIABLE_TYPE_FOR_VARIABLE, name);
break;
}
j--;
}
for (int i = (int) optional_params_starts_from - 1; i >= 0; i--) {
addFunctionParameter(params_name[i], params_type[i], params_secondary_type[i]);
}
}
#ifdef CHAOS_COMPILER
startFunction(name, type, secondary_type, "", "", "", true);
#else
startFunction(name, type, secondary_type);
#endif
startFunctionScope(function_mode);
endFunction();
return 0;
}
bool getVariableBool(char *name) {
return getSymbolValueBool(name);
}
long long getVariableInt(char *name) {
return getSymbolValueInt(name);
}
double getVariableFloat(char *name) {
return getSymbolValueFloat(name);
}
char* getVariableString(char *name) {
return getSymbolValueString(name);
}
bool getVariableBoolByTypeCasting(char *name) {
Symbol* symbol = getSymbol(name);
return symbolValueByTypeCastingToBool(symbol);
}
long long getVariableIntByTypeCasting(char *name) {
Symbol* symbol = getSymbol(name);
return symbolValueByTypeCastingToInt(symbol);
}
double getVariableFloatByTypeCasting(char *name) {
Symbol* symbol = getSymbol(name);
return symbolValueByTypeCastingToFloat(symbol);
}
char* getVariableStringByTypeCasting(char *name) {
Symbol* symbol = getSymbol(name);
return symbolValueByTypeCastingToString(symbol);
}
unsigned long getListLength(char *name) {
Symbol* symbol = getSymbol(name);
return symbol->children_count;
}
bool getListElementBool(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
if (symbol->value_type != V_BOOL) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return _getSymbolValueBool(symbol);
}
long long getListElementInt(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
if (symbol->value_type != V_INT && symbol->value_type != V_FLOAT) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return _getSymbolValueInt(symbol);
}
double getListElementFloat(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
if (symbol->value_type != V_INT && symbol->value_type != V_FLOAT) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return _getSymbolValueFloat(symbol);
}
char* getListElementString(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
if (symbol->value_type != V_STRING) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return _getSymbolValueString(symbol);
}
bool getListElementBoolByTypeCasting(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
return symbolValueByTypeCastingToBool(symbol);
}
long long getListElementIntByTypeCasting(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
return symbolValueByTypeCastingToInt(symbol);
}
double getListElementFloatByTypeCasting(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
return symbolValueByTypeCastingToFloat(symbol);
}
char* getListElementStringByTypeCasting(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
return symbolValueByTypeCastingToString(symbol);
}
void copyListElement(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
createCloneFromSymbol(
NULL,
symbol->type,
symbol,
symbol->secondary_type
);
}
enum Type getListElementType(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
return symbol->type;
}
enum ValueType getListElementValueType(char *name, long long i) {
Symbol* symbol = getListElement(getSymbol(name), i);
return symbol->value_type;
}
unsigned long getDictLength(char *name) {
Symbol* symbol = getSymbol(name);
return symbol->children_count;
}
char* getDictKeyByIndex(char *name, long long i) {
Symbol* symbol = getSymbol(name);
Symbol* child = symbol->children[i];
char* key;
key = malloc(1 + strlen(child->key));
strcpy(key, child->key);
return key;
}
bool getDictElementBool(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
if (symbol->value_type != V_BOOL) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return _getSymbolValueBool(symbol);
}
long long getDictElementInt(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
if (symbol->value_type != V_INT && symbol->value_type != V_FLOAT) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return _getSymbolValueInt(symbol);
}
double getDictElementFloat(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
if (symbol->value_type != V_INT && symbol->value_type != V_FLOAT) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return _getSymbolValueFloat(symbol);
}
char* getDictElementString(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
if (symbol->value_type != V_STRING) {
throw_error(E_UNEXPECTED_VALUE_TYPE, getValueTypeName(symbol->value_type), symbol->name);
}
return _getSymbolValueString(symbol);
}
bool getDictElementBoolByTypeCasting(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
return symbolValueByTypeCastingToBool(symbol);
}
long long getDictElementIntByTypeCasting(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
return symbolValueByTypeCastingToInt(symbol);
}
double getDictElementFloatByTypeCasting(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
return symbolValueByTypeCastingToFloat(symbol);
}
char* getDictElementStringByTypeCasting(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
return symbolValueByTypeCastingToString(symbol);
}
void copyDictElement(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
createCloneFromSymbol(
key,
symbol->type,
symbol,
symbol->secondary_type
);
}
enum Type getDictElementType(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
return symbol->type;
}
enum ValueType getDictElementValueType(char *name, char *key) {
Symbol* symbol = getDictElement(getSymbol(name), key);
return symbol->value_type;
}
char* dumpVariableToString(char *name, bool pretty, bool escaped, bool double_quotes) {
Symbol* symbol = getSymbol(name);
bool is_complex = false;
if (symbol->type == K_LIST || symbol->type == K_DICT)
is_complex = true;
char *encoded = NULL;
encoded = encodeSymbolValueToString(symbol, is_complex, pretty, escaped, 0, encoded, double_quotes);
return encoded;
}
void returnVariableBool(bool b) {
Symbol* symbol = addSymbolBool(NULL, b);
returnVariable(symbol);
}
void returnVariableInt(long long i) {
Symbol* symbol = addSymbolInt(NULL, i);
returnVariable(symbol);
}
void returnVariableFloat(double f) {
Symbol* symbol = addSymbolFloat(NULL, f);
returnVariable(symbol);
}
void returnVariableString(char *s) {
Symbol* symbol = addSymbolString(NULL, s);
returnVariable(symbol);
}
void createVariableBool(char *name, bool b) {
addSymbolBool(name, b);
}
void createVariableInt(char *name, long long i) {
addSymbolInt(name, i);
}
void createVariableFloat(char *name, double f) {
addSymbolFloat(name, f);
}
void createVariableString(char *name, char *s) {
addSymbolString(name, s);
}
void startBuildingList() {
addSymbolList(NULL);
}
void returnList(enum Type type) {
returnComplex(type);
}
void startBuildingDict() {
addSymbolDict(NULL);
}
void returnDict(enum Type type) {
returnComplex(type);
}
void returnComplex(enum Type type) {
complex_mode_stack.arr[complex_mode_stack.size - 1]->children_count = complex_mode_stack.child_counter[complex_mode_stack.size - 1];
complex_mode_stack.arr[complex_mode_stack.size - 1]->secondary_type = type;
Symbol* symbol = complex_mode_stack.arr[complex_mode_stack.size - 1];
popComplexModeStack();
returnVariable(symbol);
}
void finishList(enum Type type) {
finishComplex(type);
}
void finishDict(enum Type type) {
finishComplex(type);
}
void finishComplex(enum Type type) {
complex_mode_stack.arr[complex_mode_stack.size - 1]->children_count = complex_mode_stack.child_counter[complex_mode_stack.size - 1];
complex_mode_stack.arr[complex_mode_stack.size - 1]->secondary_type = type;
popComplexModeStack();
}
enum Type getListType(char *name) {
Symbol* symbol = getSymbol(name);
if (symbol->type != K_LIST)
throw_error(E_NOT_A_LIST, name);
return symbol->secondary_type;
}
enum Type getDictType(char *name) {
Symbol* symbol = getSymbol(name);
if (symbol->type != K_DICT)
throw_error(E_NOT_A_DICT, name);
return symbol->secondary_type;
}
enum ValueType getValueType(char *name) {
Symbol* symbol = getSymbol(name);
return symbol->value_type;
}
enum Role getRole(char *name) {
Symbol* symbol = getSymbol(name);
return symbol->role;
}
void raiseError(char *msg) {
throw_error(E_RAISED_FROM_AN_EXTENSION, msg, NULL, 0, 0);
}
void parseJson(char *json) {
injectCode(json, INIT_JSON_PARSE);
phase = PROGRAM;
}