@@ -29,10 +29,16 @@ 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
+ }
41
+
36
42
fn default_environment ( session:: session sess,
37
43
str argv0 ,
38
44
str input ) -> eval:: env {
@@ -112,19 +118,20 @@ fn compile_input(session::session sess,
112
118
}
113
119
114
120
fn pretty_print_input ( session:: session sess, eval:: env env, str input ,
115
- bool typed ) {
121
+ pp_mode ppm ) {
116
122
auto def = tup ( ast:: local_crate, 0 ) ;
117
123
auto p = front:: parser:: new_parser ( sess, env, def, input, 0 u, 0 u) ;
118
124
auto crate = front:: parser:: parse_crate_from_source_file ( p) ;
119
125
120
126
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;
127
+ alt ( ppm) {
128
+ case ( ppm_typed) {
129
+ auto def_map = resolve:: resolve_crate ( sess, crate ) ;
130
+ auto ty_cx = ty:: mk_ctxt ( sess, def_map) ;
131
+ typeck:: check_crate ( ty_cx, crate ) ;
132
+ mode = pprust:: mo_typed ( ty_cx) ;
133
+ }
134
+ case ( ppm_normal) { mode = pprust:: mo_untyped; }
128
135
}
129
136
130
137
pprust:: print_file ( sess, crate . node. module , input, std:: io:: stdout ( ) ,
@@ -150,8 +157,7 @@ options:
150
157
-o <filename> write output to <filename>
151
158
--glue generate glue.bc file
152
159
--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
160
+ --pretty [type] pretty-print the input instead of compiling
155
161
--ls list the symbols defined by a crate file
156
162
-L <path> add a directory to the library search path
157
163
--noverify suppress LLVM verification step (slight speedup)
@@ -304,12 +310,20 @@ fn build_session(@session::options sopts) -> session::session {
304
310
ret sess;
305
311
}
306
312
313
+ fn parse_pretty( session:: session sess, & str name) -> pp_mode {
314
+ if ( str:: eq ( name, "normal" ) ) { ret ppm_normal; }
315
+ else if ( str:: eq ( name, "typed" ) ) { ret ppm_typed; }
316
+ else {
317
+ sess. err ( "argument to `pretty` must be either `normal` or `typed`" ) ;
318
+ }
319
+ }
320
+
307
321
fn main( vec[ str] args ) {
308
322
309
323
auto opts = [ optflag ( "h" ) , optflag ( "help" ) ,
310
324
optflag ( "v" ) , optflag ( "version" ) ,
311
325
optflag ( "glue" ) , optflag ( "emit-llvm" ) ,
312
- optflag ( "pretty" ) , optflag ( "typed- pretty") ,
326
+ optflagopt ( " pretty") ,
313
327
optflag ( "ls" ) , optflag ( "parse-only" ) ,
314
328
optflag ( "O" ) , optopt ( "OptLevel" ) ,
315
329
optflag ( "shared" ) , optmulti ( "L" ) ,
@@ -365,13 +379,19 @@ fn main(vec[str] args) {
365
379
auto ifile = match . free . ( 0 ) ;
366
380
let str saved_out_filename = "" ;
367
381
auto env = default_environment ( sess, binary, ifile) ;
368
- auto pretty = opt_present ( match , "pretty" ) ;
369
- auto typed_pretty = opt_present ( match , "typed- pretty" ) ;
382
+ auto pretty = option :: map [ str , pp_mode ] ( bind parse_pretty ( sess , _ ) ,
383
+ getopts :: opt_default ( match , "pretty" , "normal" ) ) ;
370
384
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) {
385
+
386
+ alt ( pretty) {
387
+ case ( some[ pp_mode] ( ?ppm) ) {
388
+ pretty_print_input ( sess, env, ifile, ppm) ;
389
+ ret;
390
+ }
391
+ case ( none[ pp_mode] ) { /* continue */ }
392
+ }
393
+
394
+ if ( ls) {
375
395
front:: creader:: list_file_metadata ( ifile, std:: io:: stdout ( ) ) ;
376
396
ret;
377
397
} else {
@@ -401,6 +421,8 @@ fn main(vec[str] args) {
401
421
402
422
// If the user wants an exe generated we need to invoke
403
423
// gcc to link the object file with some libs
424
+ //
425
+ // TODO: Factor this out of main.
404
426
if ( sopts. output_type == link:: output_type_exe) {
405
427
406
428
//FIXME: Should we make the 'stage3's variable here?
0 commit comments