-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins.c
72 lines (64 loc) · 1.67 KB
/
builtins.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
#include <stdio.h>
#include <glib.h>
#include "builtins.h"
BUILTIN_FUNC(builtin_print) {
Variable v = get_argument_value(c, 1);
switch(v.type) {
case Float:
printf("%f", v.f);
break;
case Integer:
printf("%d", v.i);
break;
case String:
printf("%s", v.s);
}
return null;
}
BUILTIN_FUNC(builtin_async_sleep) {
Variable *line = c->program[c->current_line];
Variable v = get_argument_value(c, 1);
if(v.type != Integer) {
fprintf(stderr, " asleep takes integer as argument ");
exit(1);
}
*c->status = 1;
g_timeout_add_once(v.i, run_context, c);
return null;
}
static void impulse() {
printf(" #> ");
}
static gboolean a_move_forward(void *p) {
Context *c = (Context *)p;
if((*(int *)c->custom_data)-- > 0) {
impulse();
*c->status = 1;
return TRUE;
//g_timeout_add_once(100, a_move_forward, c);
}
else {
free(c->custom_data);
*c->status = 1;
g_timeout_add_once(0, run_context, c);
}
return FALSE;
}
BUILTIN_FUNC(builtin_async_move_forward_start) {
Variable *args = c->program[c->current_line];
Variable v = get_argument_value(c, 1);
if(v.type != Integer) {
fprintf(stderr, " moveforward takes integer as argument ");
exit(1);
}
int steps = v.i;
c->custom_data = malloc(sizeof(int));
*((int *)c->custom_data) = steps;
*c->status = 1;
g_timeout_add(100, a_move_forward, c);
return null;
}
extern Variable evaluate(MapObject *locals, const char* expr);
BUILTIN_FUNC(builtin_evuluate) {
return evaluate(c->locals, "");
}