]>
Witch of Git - ivy/blob - src/main.rs
4 trans
::{self, translate
},
10 io
::{self, Read
, Write
},
11 path
::{Path
, PathBuf
},
14 use structopt
::{clap
, StructOpt
};
19 /// If no mode is specified, this will fully compile the program.
20 #[derive(Debug, StructOpt)]
21 #[structopt(setting = clap::AppSettings::ArgRequiredElseHelp)]
24 #[structopt(short = "S", long = "assembly", conflicts_with = "pretty")]
27 /// Pretty-print the source code.
28 #[structopt(short = "p", long = "pretty")]
31 /// Input path. Use "-" for stdin.
34 /// Output path. Defaults to stdout or a.out, depending on the mode.
35 #[structopt(short = "o", long = "output")]
36 output
: Option
<PathBuf
>,
39 fn main() -> io
::Result
<()> {
40 let opt
= Opt
::from_args();
41 let text
= match &opt
.inp
ut
{
42 path
if path
== Path
::new("-") => {
43 let mut text
= String
::new();
44 std
::io
::stdin().read_to_string(&mut text
)?
;
47 path
=> fs
::read_to_string(path
)?
,
49 let sexp
= match parse(&text
) {
52 println
!("{:?}", err
);
53 std
::process
::exit(1);
56 let (ast
, mut builtins
) = match Ast
::parse(BUILTINS
, &sexp
) {
60 std
::process
::exit(1);
66 let mut file
= fs
::File
::create(path
)?
;
67 writeln
!(file
, "{}", ast
.to_doc().pretty(80))?
;
69 None
=> println
!("{}", ast
.to_doc().pretty(80)),
73 let code
= match translate(&ast
) {
77 std
::process
::exit(1);
80 builtins
.retain(|_
, v
| code
.globals
.contains(&v
.global_number().unwrap
()));
84 let mut file
= fs
::File
::create(path
)?
;
85 trans
::x64
::write_compile(&builtins
, &mut file
, &code
)?
;
88 let stdout
= std
::io
::stdout();
89 trans
::x64
::write_compile(&builtins
, &mut stdout
.lock(), &code
)?
;
94 let mut file
= tempfile
::Builder
::new().suffix(".s").tempfile()?
;
95 trans
::x64
::write_compile(&builtins
, &mut file
, &code
)?
;
100 "-Lrt/target/release",
106 .arg(opt
.output
.unwrap
_or
_else
(|| "a.out".into
()))