This repository has been archived by the owner on Apr 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
builtins.cc
131 lines (111 loc) · 3.53 KB
/
builtins.cc
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
#include <stdio.h>
#include <string.h>
#include "lslmini.hh"
#include "logger.hh"
char *builtins_file = NULL;
extern char *builtins_txt[];
struct _TypeMap {
char *name;
LST_TYPE type;
} types[] = {
{"void", LST_NULL},
{"integer", LST_INTEGER},
{"float", LST_FLOATINGPOINT},
{"string", LST_STRING},
{"key", LST_KEY},
{"vector", LST_VECTOR},
{"rotation",LST_QUATERNION},
{"list", LST_LIST},
{NULL, LST_ERROR}
};
LLScriptType *str_to_type(char *str) {
for (int i = 0; types[i].name != NULL; ++i) {
if ( strcmp(types[i].name, str) == 0 )
return LLScriptType::get(types[i].type);
}
fprintf(stderr, "invalid type in builtins.txt: %s\n", str);
exit(EXIT_FAILURE);
return LLScriptType::get(LST_ERROR);
}
void LLScriptScript::define_builtins() {
LLScriptFunctionDec *dec = NULL;
FILE *fp = NULL;
char buf[1024];
char original[1024];
char *ret_type = NULL;
char *name = NULL;
char *ptype = NULL, *pname = NULL;
int line = 0;
if(builtins_file != NULL) {
fp = fopen(builtins_file, "r");
if (fp==NULL) {
snprintf(buf, 1024, "couldn't open %s", builtins_file);
perror(buf);
exit(EXIT_FAILURE);
}
}
while (1) {
if (fp) {
if (fgets(buf, 1024, fp)==NULL)
break;
} else {
if (builtins_txt[line]==NULL)
break;
strncpy(buf, builtins_txt[line], 1024);
++line;
}
strcpy(original, buf);
ret_type = strtok(buf, " (),");
if ( ret_type == NULL ) {
fprintf(stderr, "error parsing %s: %s\n", builtins_file, original);
exit(EXIT_FAILURE);
return;
}
if (!strcmp(ret_type, "const")) {
ret_type = strtok(NULL, " =(),");
name = strtok(NULL, " =(),");
if ( ret_type == NULL || name == NULL ) {
fprintf(stderr, "error parsing %s: %s\n", builtins_file, original);
exit(EXIT_FAILURE);
return;
}
define_symbol( new LLScriptSymbol(
strdup(name), str_to_type(ret_type), SYM_VARIABLE, SYM_BUILTIN
));
}
else if (!strcmp(ret_type, "event")) {
name = strtok(NULL, " (),");
if ( ret_type == NULL || name == NULL ) {
fprintf(stderr, "error parsing %s: %s\n", builtins_file, original);
exit(EXIT_FAILURE);
return;
}
dec = new LLScriptFunctionDec();
while ( (ptype = strtok(NULL, " (),")) != NULL ) {
if ( (pname = strtok(NULL, " (),")) != NULL ) {
dec->push_child(new LLScriptIdentifier( str_to_type(ptype), strdup(pname)));
}
}
define_symbol( new LLScriptSymbol(
strdup(name), str_to_type("void"), SYM_EVENT, SYM_BUILTIN, dec
));
}
else {
name = strtok(NULL, " (),");
if ( ret_type == NULL || name == NULL ) {
fprintf(stderr, "error parsing %s: %s\n", builtins_file, original);
exit(EXIT_FAILURE);
return;
}
dec = new LLScriptFunctionDec();
while ( (ptype = strtok(NULL, " (),")) != NULL ) {
if ( (pname = strtok(NULL, " (),")) != NULL ) {
dec->push_child(new LLScriptIdentifier( str_to_type(ptype), strdup(pname)));
}
}
define_symbol( new LLScriptSymbol(
strdup(name), str_to_type(ret_type), SYM_FUNCTION, SYM_BUILTIN, dec
));
}
}
}