forked from gabordemooij/citrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.c
377 lines (353 loc) · 9.65 KB
/
system.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include "citrine.h"
#include "siphash.h"
/**
* @internal
* GarbageCollector Marker
*/
void ctr_gc_mark(ctr_object* object) {
ctr_object* el;
ctr_mapitem* item;
ctr_object* o;
ctr_object* k;
long i;
if (object->info.type == CTR_OBJECT_TYPE_OTARRAY) {
for (i = 0; i < object->value.avalue->head; i++) {
el = *(object->value.avalue->elements+i);
el->info.mark = 1;
ctr_gc_mark(el);
}
}
item = object->properties->head;
while(item) {
k = item->key;
o = item->value;
o->name = k->value.svalue->value;
o->info.mark = 1;
k->info.mark = 1;
ctr_gc_mark(o);
item = item->next;
}
item = object->methods->head;
while(item) {
o = item->value;
k = item->key;
o->name = k->value.svalue->value;
o->info.mark = 1;
k->info.mark = 1;
ctr_gc_mark(o);
item = item->next;
}
}
/**
* @internal
* GarbageCollector Sweeper
*/
void ctr_gc_sweep() {
ctr_object* previousObject = NULL;
ctr_object* currentObject = ctr_first_object;
while(currentObject) {
ctr_gc_object_counter ++;
if (currentObject->info.mark==0 && currentObject->info.sticky==0){
ctr_gc_dust_counter ++;
if (previousObject) {
previousObject->gnext = currentObject->gnext;
}
free(currentObject);
currentObject = previousObject->gnext;
} else {
if (currentObject->info.mark == 1) {
currentObject->info.mark = 0;
}
previousObject = currentObject;
currentObject = currentObject->gnext;
}
}
}
/**
* Broom
*
* GarbageCollector, to invoke use:
*
* [Broom] sweep.
*/
ctr_object* ctr_gc_collect (ctr_object* myself, ctr_argument* argumentList) {
ctr_object* context;
int oldcid;
ctr_gc_dust_counter = 0;
ctr_gc_object_counter = 0;
context = ctr_contexts[ctr_context_id];
oldcid = ctr_context_id;
while(ctr_context_id > -1) {
ctr_gc_mark(context);
ctr_context_id--;
context = ctr_contexts[ctr_context_id];
}
ctr_gc_sweep();
ctr_context_id = oldcid;
return myself;
}
/**
* [Broom] dust
*
* Returns the number of objects collected.
*/
ctr_object* ctr_gc_dust(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float((ctr_number) ctr_gc_dust_counter);
}
/**
* GCCount
*
* Returns the number of objects marked.
*/
ctr_object* ctr_gc_object_count(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float((ctr_number) ctr_gc_object_counter);
}
/**
* [Shell] call: [String]
*
* Performs a Shell operation. The Shell object uses a fluid API, so you can
* mix shell code with programming logic. For instance to list the contents
* of a directory use:
*
* Shell ls
*
* This will output the contents of the current working directly, you
* can also pass keyword messages like so:
*
* Shell echo: 'Hello from the Shell!'.
*
* The example above will output the specified message to the console.
* Every message you send will be turned into a string and dispatched to
* the 'call:' message.
*/
ctr_object* ctr_shell_call(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* arg = ctr_internal_cast2string(argumentList->object);
long vlen = arg->value.svalue->vlen;
char* comString = malloc(vlen + 1);
int r;
memcpy(comString, arg->value.svalue->value, vlen);
memcpy(comString+vlen,"\0",1);
r = system(comString);
return ctr_build_number_from_float( (ctr_number) r );
}
/**
* @internal
*
* Shell Object uses a fluid API.
*/
ctr_object* ctr_shell_respond_to_with(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* commandObj;
ctr_object* prefix;
ctr_object* suffix;
ctr_argument* newArgumentList;
char* command;
int len;
prefix = ctr_internal_cast2string(argumentList->object);
suffix = ctr_internal_cast2string(argumentList->next->object);
len = prefix->value.svalue->vlen + suffix->value.svalue->vlen;
if (len == 0) return myself;
command = (char*) malloc(len); /* actually we need +1 for the space between commands, but we dont because we remove the colon : !*/
strncpy(command, prefix->value.svalue->value, prefix->value.svalue->vlen - 1); /* remove colon, gives room for space */
strncpy(command + (prefix->value.svalue->vlen - 1), " ", 1); /* space to separate commands */
strncpy(command + (prefix->value.svalue->vlen), suffix->value.svalue->value, suffix->value.svalue->vlen);
commandObj = ctr_build_string(command, len);
newArgumentList = CTR_CREATE_ARGUMENT();
newArgumentList->object = commandObj;
ctr_shell_call(myself, newArgumentList);
return myself;
}
/**
* @internal
*
* Shell Object uses a fluid API.
*/
ctr_object* ctr_shell_respond_to(ctr_object* myself, ctr_argument* argumentList) {
ctr_shell_call(myself, argumentList);
return myself;
}
/**
* [Command] argument: [Number]
*
* Obtains an argument from the CLI invocation.
*/
ctr_object* ctr_command_argument(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* numberObject = ctr_internal_cast2number(argumentList->object);
int n = (int) numberObject->value.nvalue;
if (n >= ctr_argc || n < 0) return CtrStdNil;
return ctr_build_string(ctr_argv[n], strlen(ctr_argv[n]));
}
/**
* [Command] argCount
*
* Returns the number of CLI arguments passed to the script.
*/
ctr_object* ctr_command_num_of_args(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float( (ctr_number) ctr_argc );
}
/**
* [Command] exit
*
* Exits program immediately.
*/
ctr_object* ctr_command_exit(ctr_object* myself, ctr_argument* argumentList) {
exit(0);
}
/**
* [Command] env: [String]
*
* Returns the value of an environment variable.
*
* Usage:
*
* x := Command env: 'MY_PATH_VAR'.
*/
ctr_object* ctr_command_get_env(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* envVarNameObj;
char* envVarNameStr;
char* envVal;
envVarNameObj = ctr_internal_cast2string(argumentList->object);
envVarNameStr = malloc((envVarNameObj->value.svalue->vlen+1)*sizeof(char));
strncpy(envVarNameStr, envVarNameObj->value.svalue->value, envVarNameObj->value.svalue->vlen);
*(envVarNameStr + (envVarNameObj->value.svalue->vlen)) = '\0';
envVal = getenv(envVarNameStr);
if (envVal == NULL) {
return CtrStdNil;
}
return ctr_build_string_from_cstring(envVal);
}
/**
* [Command] env: [Key] val: [Value]
*
* Sets the value of an environment variable.
*/
ctr_object* ctr_command_set_env(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* envVarNameObj;
ctr_object* envValObj;
char* envVarNameStr;
char* envValStr;
envVarNameObj = ctr_internal_cast2string(argumentList->object);
envValObj = ctr_internal_cast2string(argumentList->next->object);
envVarNameStr = malloc((envVarNameObj->value.svalue->vlen+1)*sizeof(char));
CTR_2CSTR(envVarNameStr, envVarNameObj);
CTR_2CSTR(envValStr, envValObj);
setenv(envVarNameStr, envValStr, 1);
return myself;
}
/**
* Command askQuestion
*
* Ask a question on the command-line, resumes program
* only after pressing the enter key.
* Only reads up to 100 characters.
*
* Usage:
*
* Pen write: 'What is your name ?'.
* x := Command askQuestion.
* Pen write: 'Hello ' + x + ' !', brk.
*
* The example above asks the user for his/her name and
* then displays the input received.
*/
ctr_object* ctr_command_question(ctr_object* myself, ctr_argument* argumentList) {
int c;
ctr_size bytes = 0;
char* buff;
ctr_size page = 10;
buff = malloc(page * sizeof(char));
while ((c = getchar()) != '\n') {
buff[bytes] = c;
bytes++;
if (bytes > page) {
page *= 2;
buff = (char*) realloc(buff, page * sizeof(char));
if (buff == NULL) {
CtrStdError = ctr_build_string_from_cstring("Out of memory\0");
}
}
}
return ctr_build_string(buff, bytes);
}
/**
* [Command] flush.
*
* Flushes the STDOUT output buffer.
*/
ctr_object* ctr_command_flush(ctr_object* myself, ctr_argument* ctr_argumentList) {
fflush(stdout);
return myself;
}
/**
* [Dice] rollWithSides: [Number]
*
* Rolls the dice, generates a pseudo random number.
*/
ctr_object* ctr_dice_sides(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* arg = ctr_internal_cast2number(argumentList->object);
return ctr_build_number_from_float( (ctr_number) (rand() % ((int)arg->value.nvalue)));
}
/**
* [Dice] roll
*
* Rolls a standard dice with 6 sides.
*/
ctr_object* ctr_dice_throw(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float( (ctr_number) (rand() % 6));
}
/**
* [Dice] rawRandomNumber
*
* Generates a random number, the traditional way (like rand()).
*/
ctr_object* ctr_dice_rand(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float( (ctr_number) (rand()) );
}
/**
* [Clock] wait
*
* Waits X seconds.
*/
ctr_object* ctr_clock_wait(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* arg = ctr_internal_cast2number(argumentList->object);
int n = (int) arg->value.nvalue;
sleep(n);
return myself;
}
/**
* [Clock] time
*
* Returns UNIX epoch time in seconds.
*/
ctr_object* ctr_clock_time(ctr_object* myself, ctr_argument* argumentList) {
time_t seconds = time(NULL);
return ctr_build_number_from_float((ctr_number)seconds);
}
/**
* [Pen] write: [String]
*
* Writes string to console.
*/
ctr_object* ctr_console_write(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* argument1 = argumentList->object;
ctr_object* strObject = ctr_internal_cast2string(argument1);
fwrite(strObject->value.svalue->value, sizeof(char), strObject->value.svalue->vlen, stdout);
return myself;
}
/**
* [Pen] brk
*
* Outputs a newline character.
*/
ctr_object* ctr_console_brk(ctr_object* myself, ctr_argument* argumentList) {
fwrite("\n", sizeof(char), 1, stdout);
return myself;
}