RPA Toolkit
work on RJS compile time errors
[rpatk.git] / rjs / rjsexec.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
10 #include "rstring.h"
11 #include "rmem.h"
12 #include "rjs.h"
13 #include "rparecord.h"
14
15
16 static int debuginfo = 0;
17 static int parseinfo = 0;
18 static int compileonly = 0;
19 static int debugcompileonly = 0;
20
21
22 void rjs_unmap_file(rstr_t *buf)
23 {
24         if (buf) {
25                 munmap(buf->str, buf->size);
26                 r_free(buf);
27         }
28 }
29
30
31 rstr_t *rjs_map_file(const char *filename)
32 {
33         struct stat st;
34         rstr_t *str;
35         char *buffer;
36
37
38         int fd = open(filename, O_RDONLY);
39         if (fd < 0) {
40                 return (void*)0;
41         }
42         if (fstat(fd, &st) < 0) {
43                 close(fd);
44                 return (void*)0;
45         }
46         buffer = (char*)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
47         if (buffer == (void*)-1) {
48                 close(fd);
49                 return (void*)0;
50         }
51         str = (rstr_t *)r_malloc(sizeof(*str));
52         if (!str)
53                 goto error;
54         r_memset(str, 0, sizeof(*str));
55         str->str = buffer;
56         str->size = st.st_size;
57         close(fd);
58         return str;
59
60 error:
61         munmap(buffer, st.st_size);
62         close(fd);
63         return str;
64 }
65
66
67 int rjs_exec_script(rjs_engine_t *jse, rstr_t  *script)
68 {
69         if (!script)
70                 return -1;
71         if (parseinfo) {
72                 rjs_engine_dumpast(jse, script->str, script->size);
73         } else if (debugcompileonly) {
74                 int res = 0;
75                 jse->debugcompile = 1;
76                 res = rjs_engine_compile(jse, script->str, script->size);
77                 jse->debugcompile = 0;
78                 if (res < 0)
79                         return -1;
80         } else if (compileonly) {
81                 rjs_engine_compile(jse, script->str, script->size);
82         } else {
83                 if (rjs_engine_compile(jse, script->str, script->size) < 0)
84                         return -1;
85                 if (rjs_engine_run(jse) < 0)
86                         return -1;
87         }
88
89         return 0;
90 }
91
92
93 void rjs_display_errors(rjs_engine_t *jse, rstr_t *script)
94 {
95         rlong line = 0;
96         rlong i;
97         rjs_coerror_t *err;
98
99         for (i = 0; i < r_array_length(jse->co->errors); i++) {
100                 err = (rjs_coerror_t *)r_array_slot(jse->co->errors, i);
101                 fprintf(stdout, "Line: %ld ", (rlong)line);
102                 if (err->code == RJS_ERROR_SYNTAX) {
103                         fprintf(stdout, "Sytax Error");
104                 } else if (err->code == RJS_ERROR_UNDEFINED) {
105                         fprintf(stdout, "Undefined Identifier");
106                 } else {
107                         fprintf(stdout, "Error");
108                 }
109                 fprintf(stdout, "(%ld, %ld): ", (rlong)(err->script - script->str), (rlong)err->scriptsize);
110                 fwrite(err->script, sizeof(rchar), err->scriptsize, stdout);
111                 fprintf(stdout, "\n");
112         }
113 }
114
115
116 int main(int argc, char *argv[])
117 {
118         rint i;
119         rstr_t *script = NULL, *unmapscript = NULL;
120         rstr_t line;
121         rjs_engine_t *jse = rjs_engine_create();
122
123         for (i = 1; i < argc; i++) {
124                 if (r_strcmp(argv[i], "-L") == 0) {
125
126                 } else if (r_strcmp(argv[i], "-d") == 0) {
127                         debuginfo = 1;
128                         jse->debugexec = 1;
129                 } else if (r_strcmp(argv[i], "-p") == 0) {
130                         parseinfo = 1;
131                 } else if (r_strcmp(argv[i], "-C") == 0) {
132                         debugcompileonly = 1;
133                 } else if (r_strcmp(argv[i], "-c") == 0) {
134                         compileonly = 1;
135                 } else if (r_strcmp(argv[i], "-m") == 0) {
136
137                 }
138         }
139
140
141         for (i = 1; i < argc; i++) {
142                 if (r_strcmp(argv[i], "-e") == 0) {
143                         if (++i < argc) {
144                                 line.str = argv[i];
145                                 line.size = r_strlen(argv[i]);
146                                 script = &line;
147                         }
148                         if (rjs_exec_script(jse, script) < 0)
149                                 goto end;
150                 }
151         }
152
153         for (i = 1; i < argc; i++) {
154                 if (r_strcmp(argv[i], "-f") == 0) {
155                         if (++i < argc) {
156                                 script = rjs_map_file(argv[i]);
157                                 if (script) {
158                                         unmapscript = script;
159                                 }
160                         }
161                         if (rjs_exec_script(jse, script) < 0)
162                                 goto end;
163                 }
164         }
165
166 end:
167         if (jse->co && r_array_length(jse->co->errors))
168                 rjs_display_errors(jse, script);
169         rjs_engine_destroy(jse);
170         if (unmapscript)
171                 rjs_unmap_file(unmapscript);
172         if (debuginfo)
173                 fprintf(stdout, "\nRJS Version: %s, memory: %ld KB (leaked %ld Bytes)\n", rjs_version(), (rlong)r_debug_get_maxmem()/1000, (rlong)r_debug_get_allocmem());
174         return 0;
175 }