@@ -29,10 +29,17 @@ import std::getopts;
29
29
import std:: getopts:: optopt;
30
30
import std:: getopts:: optmulti;
31
31
import std:: getopts:: optflag;
32
+ import std:: getopts:: optflagopt;
32
33
import std:: getopts:: opt_present;
33
34
34
35
import back:: link:: output_type;
35
36
37
+ tag pp_mode {
38
+ ppm_normal;
39
+ ppm_typed;
40
+ ppm_identified;
41
+ }
42
+
36
43
fn default_environment ( session:: session sess,
37
44
str argv0 ,
38
45
str input ) -> eval:: env {
@@ -112,19 +119,21 @@ fn compile_input(session::session sess,
112
119
}
113
120
114
121
fn pretty_print_input ( session:: session sess, eval:: env env, str input ,
115
- bool typed ) {
122
+ pp_mode ppm ) {
116
123
auto def = tup ( ast:: local_crate, 0 ) ;
117
124
auto p = front:: parser:: new_parser ( sess, env, def, input, 0 u, 0 u) ;
118
125
auto crate = front:: parser:: parse_crate_from_source_file ( p) ;
119
126
120
127
auto mode;
121
- if ( typed) {
122
- auto def_map = resolve:: resolve_crate ( sess, crate ) ;
123
- auto ty_cx = ty:: mk_ctxt ( sess, def_map) ;
124
- typeck:: check_crate ( ty_cx, crate ) ;
125
- mode = pprust:: mo_typed ( ty_cx) ;
126
- } else {
127
- mode = pprust:: mo_untyped;
128
+ alt ( ppm) {
129
+ case ( ppm_typed) {
130
+ auto def_map = resolve:: resolve_crate ( sess, crate ) ;
131
+ auto ty_cx = ty:: mk_ctxt ( sess, def_map) ;
132
+ typeck:: check_crate ( ty_cx, crate ) ;
133
+ mode = pprust:: mo_typed ( ty_cx) ;
134
+ }
135
+ case ( ppm_normal) { mode = pprust:: mo_untyped; }
136
+ case ( ppm_identified) { mode = pprust:: mo_identified; }
128
137
}
129
138
130
139
pprust:: print_file ( sess, crate . node. module , input, std:: io:: stdout ( ) ,
@@ -150,8 +159,7 @@ options:
150
159
-o <filename> write output to <filename>
151
160
--glue generate glue.bc file
152
161
--shared compile a shared-library crate
153
- --pretty pretty-print the input instead of compiling
154
- --typed-pretty pretty-print the input with types instead of compiling
162
+ --pretty [type] pretty-print the input instead of compiling
155
163
--ls list the symbols defined by a crate file
156
164
-L <path> add a directory to the library search path
157
165
--noverify suppress LLVM verification step (slight speedup)
@@ -304,12 +312,21 @@ fn build_session(@session::options sopts) -> session::session {
304
312
ret sess;
305
313
}
306
314
315
+ fn parse_pretty( session:: session sess, & str name) -> pp_mode {
316
+ if ( str:: eq ( name, "normal" ) ) { ret ppm_normal; }
317
+ else if ( str:: eq ( name, "typed" ) ) { ret ppm_typed; }
318
+ else if ( str:: eq ( name, "identified" ) ) { ret ppm_identified; }
319
+
320
+ sess. err ( "argument to `pretty` must be one of `normal`, `typed`, or " +
321
+ "`identified`" ) ;
322
+ }
323
+
307
324
fn main ( vec[ str] args) {
308
325
309
326
auto opts = [ optflag ( "h" ) , optflag ( "help" ) ,
310
327
optflag ( "v" ) , optflag ( "version" ) ,
311
328
optflag ( "glue" ) , optflag ( "emit-llvm" ) ,
312
- optflag ( "pretty" ) , optflag ( "typed- pretty") ,
329
+ optflagopt ( " pretty") ,
313
330
optflag ( "ls" ) , optflag ( "parse-only" ) ,
314
331
optflag ( "O" ) , optopt ( "OptLevel" ) ,
315
332
optflag ( "shared" ) , optmulti ( "L" ) ,
@@ -365,42 +382,50 @@ fn main(vec[str] args) {
365
382
auto ifile = match . free . ( 0 ) ;
366
383
let str saved_out_filename = "" ;
367
384
auto env = default_environment ( sess, binary, ifile) ;
368
- auto pretty = opt_present ( match , "pretty" ) ;
369
- auto typed_pretty = opt_present ( match , "typed- pretty" ) ;
385
+ auto pretty = option :: map [ str , pp_mode ] ( bind parse_pretty ( sess , _ ) ,
386
+ getopts :: opt_default ( match , "pretty" , "normal" ) ) ;
370
387
auto ls = opt_present ( match , "ls" ) ;
371
- if ( pretty || typed_pretty) {
372
- pretty_print_input ( sess, env, ifile, typed_pretty) ;
373
- ret;
374
- } else if ( ls) {
388
+
389
+ alt ( pretty) {
390
+ case ( some[ pp_mode] ( ?ppm) ) {
391
+ pretty_print_input ( sess, env, ifile, ppm) ;
392
+ ret;
393
+ }
394
+ case ( none[ pp_mode] ) { /* continue */ }
395
+ }
396
+
397
+ if ( ls) {
375
398
front:: creader:: list_file_metadata ( ifile, std:: io:: stdout ( ) ) ;
376
399
ret;
377
- } else {
378
- alt ( output_file) {
379
- case ( none) {
380
- let vec[ str] parts = str:: split ( ifile, '.' as u8 ) ;
381
- vec:: pop[ str] ( parts) ;
382
- saved_out_filename = parts. ( 0 ) ;
383
- alt ( sopts. output_type ) {
384
- case ( link:: output_type_none) { parts += [ "pp" ] ; }
385
- case ( link:: output_type_bitcode) { parts += [ "bc" ] ; }
386
- case ( link:: output_type_assembly) { parts += [ "s" ] ; }
387
-
388
- // Object and exe output both use the '.o' extension here
389
- case ( link:: output_type_object) { parts += [ "o" ] ; }
390
- case ( link:: output_type_exe) { parts += [ "o" ] ; }
391
- }
392
- auto ofile = str:: connect ( parts, "." ) ;
393
- compile_input ( sess, env, ifile, ofile) ;
394
- }
395
- case ( some ( ?ofile) ) {
396
- saved_out_filename = ofile;
397
- compile_input ( sess, env, ifile, ofile) ;
400
+ }
401
+
402
+ alt ( output_file) {
403
+ case ( none) {
404
+ let vec[ str] parts = str:: split ( ifile, '.' as u8 ) ;
405
+ vec:: pop[ str] ( parts) ;
406
+ saved_out_filename = parts. ( 0 ) ;
407
+ alt ( sopts. output_type ) {
408
+ case ( link:: output_type_none) { parts += [ "pp" ] ; }
409
+ case ( link:: output_type_bitcode) { parts += [ "bc" ] ; }
410
+ case ( link:: output_type_assembly) { parts += [ "s" ] ; }
411
+
412
+ // Object and exe output both use the '.o' extension here
413
+ case ( link:: output_type_object) { parts += [ "o" ] ; }
414
+ case ( link:: output_type_exe) { parts += [ "o" ] ; }
398
415
}
416
+ auto ofile = str:: connect ( parts, "." ) ;
417
+ compile_input ( sess, env, ifile, ofile) ;
418
+ }
419
+ case ( some ( ?ofile) ) {
420
+ saved_out_filename = ofile;
421
+ compile_input ( sess, env, ifile, ofile) ;
399
422
}
400
423
}
401
424
402
425
// If the user wants an exe generated we need to invoke
403
426
// gcc to link the object file with some libs
427
+ //
428
+ // TODO: Factor this out of main.
404
429
if ( sopts. output_type == link:: output_type_exe) {
405
430
406
431
//FIXME: Should we make the 'stage3's variable here?
0 commit comments