From 21c9c9ef02437b826b6297ffc88dc7a49b531775 Mon Sep 17 00:00:00 2001 From: Seungmin Jeon Date: Fri, 23 Sep 2022 00:10:53 +0900 Subject: [PATCH] Add calc --- src/bin/calc.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/bin/calc.rs diff --git a/src/bin/calc.rs b/src/bin/calc.rs new file mode 100644 index 0000000..55c2602 --- /dev/null +++ b/src/bin/calc.rs @@ -0,0 +1,70 @@ +use std::fs::File; +use std::io::{self, BufRead, Read}; + +use anyhow::Result; +use clap::Parser; + +use cs220::assignments::assignment04::*; + +struct Input<'a> { + source: Box, +} + +impl<'a> Input<'a> { + fn console(stdin: &'a io::Stdin) -> Input<'a> { + Input { + source: Box::new(stdin.lock()), + } + } + + fn file(path: &str) -> io::Result> { + File::open(path).map(|file| Input { + source: Box::new(io::BufReader::new(file)), + }) + } +} + +impl<'a> Read for Input<'a> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.source.read(buf) + } +} + +impl<'a> BufRead for Input<'a> { + fn fill_buf(&mut self) -> io::Result<&[u8]> { + self.source.fill_buf() + } + + fn consume(&mut self, amt: usize) { + self.source.consume(amt); + } +} + +#[derive(Parser, Debug)] +#[clap(author, version, about, long_about = None)] +struct Args { + /// Expression filepath. + #[clap(value_parser)] + filepath: Option, +} + +fn main() -> Result<()> { + // Parses arguments. + let args = Args::parse(); + + let stdin = io::stdin(); + let input = if let Some(filepath) = args.filepath { + Input::file(&filepath)? + } else { + Input::console(&stdin) + }; + + let mut context = context::Context::new(); + for line in input.lines() { + let command = parser::parse_command(&line?)?; + let (variable, value) = context.calc_command(&command)?; + println!("{} = {}", variable, value); + } + + Ok(()) +}