RPA Toolkit
e1a9fa6c9c6601ebbf0bf6601297196a40c9521c
[rpatk.git] / tests / rast-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <sys/mman.h>
9 #include "rmem.h"
10 #include "rjsobject.h"
11 #include "rvmcpu.h"
12 #include "rpadbex.h"
13 #include "rpaerror.h"
14 #include "rvmcodegen.h"
15 #include "rvmcodemap.h"
16 #include "rvmscope.h"
17 #include "rvmoperator.h"
18 #include "rgc.h"
19 #include "rastnode.h"
20
21
22 typedef struct rastcompiler_s {
23         rpa_dbex_handle dbex;
24         rgc_t *gc;
25         rastnode_t *root;
26         rint optimized;
27 } rastcompiler_t;
28
29 void r_astcompiler_loadrules(rastcompiler_t *aco);
30
31
32 static int debuginfo = 0;
33 static int parseinfo = 0;
34 static int verboseinfo = 0;
35 static int compileonly = 0;
36
37
38 rastcompiler_t *r_astcompiler_create()
39 {
40         rastcompiler_t *aco;
41
42         aco = r_malloc(sizeof(*aco));
43         r_memset(aco, 0, sizeof(*aco));
44         aco->gc = r_gc_create();
45         return aco;
46 }
47
48
49 void r_astcompiler_destroy(rastcompiler_t *aco)
50 {
51         if (aco) {
52                 r_object_destroy((robject_t*)aco->gc);
53         }
54         r_free(aco);
55 }
56
57
58 void r_astcompiler_dumptree(rastcompiler_t *aco)
59 {
60
61 }
62
63
64 void codegen_unmap_file(rstr_t *buf)
65 {
66         if (buf) {
67                 munmap(buf->str, buf->size);
68                 r_free(buf);
69         }
70 }
71
72
73 rstr_t *codegen_map_file(const char *filename)
74 {
75         struct stat st;
76         rstr_t *str;
77         char *buffer;
78
79
80         int fd = open(filename, O_RDONLY);
81         if (fd < 0) {
82                 return (void*)0;
83         }
84         if (fstat(fd, &st) < 0) {
85                 close(fd);
86                 return (void*)0;
87         }
88         buffer = (char*)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
89         if (buffer == (void*)-1) {
90                 close(fd);
91                 return (void*)0;
92         }
93         str = (rstr_t *)r_malloc(sizeof(*str));
94         if (!str)
95                 goto error;
96         r_memset(str, 0, sizeof(*str));
97         str->str = buffer;
98         str->size = st.st_size;
99         close(fd);
100         return str;
101
102 error:
103         munmap(buffer, st.st_size);
104         close(fd);
105         return str;
106 }
107
108
109 inline int r_astcompiler_dumpnotification(rpa_stat_handle stat, const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason)
110 {
111         rastcompiler_t *aco = (rastcompiler_t *)userdata;
112
113         fprintf(stdout, "%s: ", name);
114         fprintf(stdout, "\n");
115         return size;
116 }
117
118
119 inline int r_astcompiler_notify(rpa_stat_handle stat, const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason)
120 {
121         rastcompiler_t *aco = (rastcompiler_t *)userdata;
122
123         if (parseinfo)
124                 r_astcompiler_dumpnotification(stat, name, userdata, input, size, reason);
125
126         if (reason & RPA_REASON_START) {
127                 rastnode_t *node = r_astnode_create();
128                 r_gc_add(aco->gc, (robject_t*)node);
129                 node->parent = aco->root;
130         } else if (reason & RPA_REASON_MATCHED) {
131
132
133         } else {
134
135         }
136
137         return size;
138 }
139
140
141 int main(int argc, char *argv[])
142 {
143         int res, i;
144         rstr_t *script = NULL, *unmapscript = NULL;
145         rastcompiler_t *aco = r_astcompiler_create();
146         aco->dbex = rpa_dbex_create();
147
148
149         for (i = 1; i < argc; i++) {
150                 if (r_strcmp(argv[i], "-L") == 0) {
151                 } else if (r_strcmp(argv[i], "-d") == 0) {
152                         debuginfo = 1;
153                 } else if (r_strcmp(argv[i], "-p") == 0) {
154                         parseinfo = 1;
155                 } else if (r_strcmp(argv[i], "-P") == 0) {
156                         parseinfo = 1;
157                         verboseinfo = 1;
158                 } else if (r_strcmp(argv[i], "-c") == 0) {
159                         compileonly = 1;
160                 } else if (r_strcmp(argv[i], "-o") == 0) {
161                         aco->optimized = 1;
162                 } else if (r_strcmp(argv[i], "-m") == 0) {
163
164                 }
165         }
166
167         r_astcompiler_loadrules(aco);
168
169         for (i = 1; i < argc; i++) {
170                 if (r_strcmp(argv[i], "-e") == 0) {
171                         if (++i < argc) {
172                                 rstr_t script = { argv[i], r_strlen(argv[i]) };
173                                 res = rpa_dbex_parse(aco->dbex, rpa_dbex_default_pattern(aco->dbex), script.str, script.str, script.str + script.size);
174                                 if (res <= 0)
175                                         goto end;
176                         }
177                         goto exec;
178                 }
179         }
180
181         for (i = 1; i < argc; i++) {
182                 if (r_strcmp(argv[i], "-f") == 0) {
183
184                         if (++i < argc) {
185                                 script = codegen_map_file(argv[i]);
186                                 if (script) {
187                                         res = rpa_dbex_parse(aco->dbex, rpa_dbex_default_pattern(aco->dbex), script->str, script->str, script->str + script->size);
188                                         unmapscript = script;
189                                         if (res <= 0)
190                                                 goto end;
191                                 }
192
193                         }
194                         goto exec;
195                 }
196         }
197
198
199 exec:
200         r_astcompiler_dumptree(aco);
201
202
203 end:
204         if (unmapscript)
205                 codegen_unmap_file(unmapscript);
206         rpa_dbex_destroy(aco->dbex);
207         r_astcompiler_destroy(aco);
208
209         if (debuginfo) {
210                 r_printf("Max alloc mem: %ld\n", r_debug_get_maxmem());
211                 r_printf("Leaked mem: %ld\n", r_debug_get_allocmem());
212         }
213         return 0;
214 }
215
216
217
218 extern char _binary_____________tests_ecma262_rpa_start[];
219 extern char _binary_____________tests_ecma262_rpa_end[];
220 extern unsigned long *_binary_____________tests_ecma262_rpa_size;
221
222 void r_astcompiler_loadrules(rastcompiler_t *aco)
223 {
224         int ret, line;
225         int inputsize = _binary_____________tests_ecma262_rpa_end - _binary_____________tests_ecma262_rpa_start;
226         const char *buffer = _binary_____________tests_ecma262_rpa_start;
227         const char *pattern = buffer;
228
229         rpa_dbex_open(aco->dbex);
230         rpa_dbex_add_callback(aco->dbex, ".*", RPA_REASON_ALL, r_astcompiler_notify, aco);
231
232         while ((ret = rpa_dbex_load(aco->dbex, pattern, inputsize)) > 0) {
233                 inputsize -= ret;
234                 pattern += ret;
235         }
236         if (ret < 0) {
237                 for (line = 1; pattern >= buffer; --pattern) {
238                         if (*pattern == '\n')
239                                 line += 1;
240                 }
241                 fprintf(stdout, "Line: %d, RPA LOAD ERROR: %s\n", line, (rpa_dbex_get_error(aco->dbex) == RPA_E_SYNTAX_ERROR) ? "Syntax Error." : "Pattern Loading failed.");
242                 goto error;
243         }
244
245 error:
246         rpa_dbex_close(aco->dbex);
247 }