diff options
Diffstat (limited to 'src')
-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); + } + } +} |