Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 211 lines (181 sloc) 4.454 kB
e0d6430 @mimaki add mruby sources
mimaki authored
1 #include "mruby.h"
2 #include "mruby/proc.h"
8b36709 @matz move header files {irep,dump,cdump,ritehash}.h to /include/mruby
matz authored
3 #include "mruby/dump.h"
4 #include "mruby/cdump.h"
2191621 @matz move compile.h to mruby/compile.h
matz authored
5 #include "mruby/compile.h"
8b36709 @matz move header files {irep,dump,cdump,ritehash}.h to /include/mruby
matz authored
6 #include <stdio.h>
98953cd @matz include string.h instead of memory.h; close #108
matz authored
7 #include <string.h>
8b36709 @matz move header files {irep,dump,cdump,ritehash}.h to /include/mruby
matz authored
8 #include <stdlib.h>
e0d6430 @mimaki add mruby sources
mimaki authored
9
10 #define RITEBIN_EXT ".mrb"
11 #define C_EXT ".c"
f4bacab @matz rename ruby_show_version to mrb_show_version
matz authored
12 void mrb_show_version(mrb_state *);
13 void mrb_show_copyright(mrb_state *);
e0d6430 @mimaki add mruby sources
mimaki authored
14 void parser_dump(mrb_state*, struct mrb_ast_node*, int);
15 void codedump_all(mrb_state*, int);
16
17 struct _args {
18 FILE *rfp;
19 FILE *wfp;
20 char *initname;
21 char *ext;
22 int check_syntax : 1;
23 int dump_type : 2;
24 int verbose : 1;
25 };
26
27 static void
28 usage(const char *name)
29 {
30 static const char *const usage_msg[] = {
31 "switches:",
32 "-c check syntax only",
33 "-o<outfile> place the output into <outfile>",
34 "-v print version number, then trun on verbose mode",
35 "-B<symbol> binary <symbol> output in C language format",
36 "-C<func> function <func> output in C language format",
37 "--verbose run at verbose mode",
38 "--version print the version",
39 "--copyright print the copyright",
40 NULL
41 };
42 const char *const *p = usage_msg;
43
44 printf("Usage: %s [switches] programfile\n", name);
45 while(*p)
46 printf(" %s\n", *p++);
47 }
48
49 static char *
50 get_outfilename(char *infile, char *ext)
51 {
52 char *outfile;
53 char *p;
54
55 outfile = (char*)malloc(strlen(infile) + strlen(ext) + 1);
56 strcpy(outfile, infile);
57 if (*ext) {
58 if ((p = strrchr(outfile, '.')) == NULL)
59 p = &outfile[strlen(outfile)];
60 strcpy(p, ext);
61 }
62
63 return outfile;
64 }
65
66 static int
67 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
68 {
69 char *infile = NULL;
70 char *outfile = NULL;
71 char **origargv = argv;
72
73 memset(args, 0, sizeof(*args));
74 args->ext = RITEBIN_EXT;
75
76 for (argc--,argv++; argc > 0; argc--,argv++) {
77 if (**argv == '-') {
78 if (strlen(*argv) <= 1)
79 return -1;
80
81 switch ((*argv)[1]) {
82 case 'o':
83 outfile = get_outfilename((*argv) + 2, "");
84 break;
85 case 'B':
86 case 'C':
87 args->ext = C_EXT;
88 args->initname = (*argv) + 2;
89 if (*args->initname == '\0') {
90 printf("%s: Function name is not specified.\n", *origargv);
91 return -2;
92 }
93 args->dump_type = ((*argv)[1] == 'B') ? DUMP_TYPE_BIN : DUMP_TYPE_CODE;
94 break;
95 case 'c':
96 args->check_syntax = 1;
97 break;
98 case 'v':
f4bacab @matz rename ruby_show_version to mrb_show_version
matz authored
99 mrb_show_version(mrb);
e0d6430 @mimaki add mruby sources
mimaki authored
100 args->verbose = 1;
101 break;
102 case '-':
103 if (strcmp((*argv) + 2, "version") == 0) {
fb60a21 @matz revise version description
matz authored
104 mrb_show_version(mrb);
e0d6430 @mimaki add mruby sources
mimaki authored
105 }
106 else if (strcmp((*argv) + 2, "verbose") == 0) {
107 args->verbose = 1;
108 break;
109 }
110 else if (strcmp((*argv) + 2, "copyright") == 0) {
fb60a21 @matz revise version description
matz authored
111 mrb_show_copyright(mrb);
e0d6430 @mimaki add mruby sources
mimaki authored
112 }
113 else return -3;
114 return 0;
115 }
116 }
117 else if (args->rfp == NULL) {
118 infile = *argv;
119 if ((args->rfp = fopen(infile, "r")) == NULL) {
120 printf("%s: Cannot open program file. (%s)\n", *origargv, infile);
121 return 0;
122 }
123 }
124 }
125
126 if (infile == NULL)
127 return -4;
128 if (args->check_syntax)
129 return 0;
130
131 if (outfile == NULL)
132 outfile = get_outfilename(infile, args->ext);
133
134 if ((args->wfp = fopen(outfile, "wb")) == NULL) {
135 printf("%s: Cannot open output file. (%s)\n", *origargv, outfile);
136 return 0;
137 }
138
139 return 0;
140 }
141
142 static void
143 cleanup(struct _args *args)
144 {
145 if (args->rfp)
146 fclose(args->rfp);
147 if (args->wfp)
148 fclose(args->wfp);
149 }
150
151 int
152 main(int argc, char **argv)
153 {
154 mrb_state *mrb = mrb_open();
155 int n = -1;
156 struct _args args;
157 struct mrb_parser_state *p;
158
159 n = parse_args(mrb, argc, argv, &args);
160
161 if (n < 0 || args.rfp == NULL) {
162 cleanup(&args);
163 usage(argv[0]);
164 return n;
165 }
166
a18fb01 @matz new API for parser
matz authored
167 p = mrb_parse_file(mrb, args.rfp);
e0d6430 @mimaki add mruby sources
mimaki authored
168 if (!p || !p->tree || p->nerr) {
169 cleanup(&args);
170 return -1;
171 }
172
173 if (args.verbose)
174 parser_dump(mrb, p->tree, 0);
175
176 n = mrb_generate_code(mrb, p->tree);
177 mrb_pool_close(p->pool);
178
179 if (args.verbose)
180 codedump_all(mrb, n);
181
182 if (n < 0 || args.check_syntax) {
183 cleanup(&args);
184 return n;
185 }
186 if (args.initname) {
187 if (args.dump_type == DUMP_TYPE_BIN)
188 n = mrb_bdump_irep(mrb, n, args.wfp, args.initname);
189 else
190 n = mrb_cdump_irep(mrb, n, args.wfp, args.initname);
191 }
192 else {
193 n = mrb_dump_irep(mrb, n, args.wfp);
194 }
195
196 cleanup(&args);
197
198 return n;
199 }
200
201 void
202 mrb_init_ext(mrb_state *mrb)
203 {
204 }
205
206 void
207 mrb_init_mrblib(mrb_state *mrb)
208 {
209 }
210
Something went wrong with that request. Please try again.