10 #include "rjsobject.h"
14 #include "rvmcodegen.h"
15 #include "rvmcodemap.h"
17 #include "rvmoperator.h"
22 typedef struct rastcompiler_s {
29 void r_astcompiler_loadrules(rastcompiler_t *aco);
32 static int debuginfo = 0;
33 static int parseinfo = 0;
34 static int verboseinfo = 0;
35 static int compileonly = 0;
38 rastcompiler_t *r_astcompiler_create()
42 aco = r_malloc(sizeof(*aco));
43 r_memset(aco, 0, sizeof(*aco));
44 aco->gc = r_gc_create();
49 void r_astcompiler_destroy(rastcompiler_t *aco)
52 r_object_destroy((robject_t*)aco->gc);
58 void r_astcompiler_dumptree(rastcompiler_t *aco)
64 void codegen_unmap_file(rstr_t *buf)
67 munmap(buf->str, buf->size);
73 rstr_t *codegen_map_file(const char *filename)
80 int fd = open(filename, O_RDONLY);
84 if (fstat(fd, &st) < 0) {
88 buffer = (char*)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
89 if (buffer == (void*)-1) {
93 str = (rstr_t *)r_malloc(sizeof(*str));
96 r_memset(str, 0, sizeof(*str));
98 str->size = st.st_size;
103 munmap(buffer, st.st_size);
109 inline int r_astcompiler_dumpnotification(rpa_stat_handle stat, const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason)
111 rastcompiler_t *aco = (rastcompiler_t *)userdata;
113 fprintf(stdout, "%s: ", name);
114 fprintf(stdout, "\n");
119 inline int r_astcompiler_notify(rpa_stat_handle stat, const char *name, void *userdata, const char *input, unsigned int size, unsigned int reason)
121 rastcompiler_t *aco = (rastcompiler_t *)userdata;
124 r_astcompiler_dumpnotification(stat, name, userdata, input, size, reason);
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) {
141 int main(int argc, char *argv[])
144 rstr_t *script = NULL, *unmapscript = NULL;
145 rastcompiler_t *aco = r_astcompiler_create();
146 aco->dbex = rpa_dbex_create();
149 for (i = 1; i < argc; i++) {
150 if (r_strcmp(argv[i], "-L") == 0) {
151 } else if (r_strcmp(argv[i], "-d") == 0) {
153 } else if (r_strcmp(argv[i], "-p") == 0) {
155 } else if (r_strcmp(argv[i], "-P") == 0) {
158 } else if (r_strcmp(argv[i], "-c") == 0) {
160 } else if (r_strcmp(argv[i], "-o") == 0) {
162 } else if (r_strcmp(argv[i], "-m") == 0) {
167 r_astcompiler_loadrules(aco);
169 for (i = 1; i < argc; i++) {
170 if (r_strcmp(argv[i], "-e") == 0) {
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);
181 for (i = 1; i < argc; i++) {
182 if (r_strcmp(argv[i], "-f") == 0) {
185 script = codegen_map_file(argv[i]);
187 res = rpa_dbex_parse(aco->dbex, rpa_dbex_default_pattern(aco->dbex), script->str, script->str, script->str + script->size);
188 unmapscript = script;
200 r_astcompiler_dumptree(aco);
205 codegen_unmap_file(unmapscript);
206 rpa_dbex_destroy(aco->dbex);
207 r_astcompiler_destroy(aco);
210 r_printf("Max alloc mem: %ld\n", r_debug_get_maxmem());
211 r_printf("Leaked mem: %ld\n", r_debug_get_allocmem());
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;
222 void r_astcompiler_loadrules(rastcompiler_t *aco)
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;
229 rpa_dbex_open(aco->dbex);
230 rpa_dbex_add_callback(aco->dbex, ".*", RPA_REASON_ALL, r_astcompiler_notify, aco);
232 while ((ret = rpa_dbex_load(aco->dbex, pattern, inputsize)) > 0) {
237 for (line = 1; pattern >= buffer; --pattern) {
238 if (*pattern == '\n')
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.");
246 rpa_dbex_close(aco->dbex);