diff options
author | Johannes Stoelp <johannes.stoelp@gmail.com> | 2022-09-25 00:04:18 +0200 |
---|---|---|
committer | Johannes Stoelp <johannes.stoelp@gmail.com> | 2022-09-25 00:04:18 +0200 |
commit | 7b76c697bd9f733aa242e12a01df4dc3a065a9e0 (patch) | |
tree | 3392f82f7a338dd0b8a447428b7a0bb00cee6a75 /src/main.rs | |
parent | da91cbbd5bb8cb925dee4a5c6ab6d66588cb2c8f (diff) | |
download | llvm-kaleidoscope-rs-7b76c697bd9f733aa242e12a01df4dc3a065a9e0.tar.gz llvm-kaleidoscope-rs-7b76c697bd9f733aa242e12a01df4dc3a065a9e0.zip |
allow to run program from file, add ch5 example programs
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 47 |
1 files changed, 36 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs index b660416..868706d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -130,17 +130,10 @@ where module.dump(); } -fn main() { - println!("Parse stdin."); - println!("ENTER to parse current input."); - println!("C-d to exit."); - - // Create lexer over stdin. - let lexer = Lexer::new(std::io::stdin().bytes().filter_map(|v| { - let v = v.ok()?; - Some(v.into()) - })); - +fn run_kaleidoscope<I>(lexer: Lexer<I>) +where + I: Iterator<Item = char>, +{ // Create parser for kaleidoscope. let mut parser = Parser::new(lexer); @@ -155,3 +148,35 @@ fn main() { // De-allocate managed static LLVM data. llvm::shutdown(); } + +fn main() { + match std::env::args().nth(1) { + Some(file) => { + println!("Parse {}.", file); + + // Create lexer over file. + let lexer = Lexer::new( + std::fs::File::open(&file) + .expect(&format!("Failed to open file {}!", file)) + .bytes() + .filter_map(|v| { + let v = v.ok()?; + Some(v.into()) + }), + ); + run_kaleidoscope(lexer); + } + None => { + println!("Parse stdin."); + println!("ENTER to parse current input."); + println!("C-d to exit."); + + // Create lexer over stdin. + let lexer = Lexer::new(std::io::stdin().bytes().filter_map(|v| { + let v = v.ok()?; + Some(v.into()) + })); + run_kaleidoscope(lexer); + } + } +} |