From ac20ebebd7b9eef4359ca798b7c6012197ccb794 Mon Sep 17 00:00:00 2001 From: Georges SARR Date: Mon, 1 Apr 2024 13:21:27 +0200 Subject: [PATCH] Added command type in parser. --- src/main.rs | 2 +- src/parser.rs | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 48ebb0f..d99cb5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ fn main() { if row.is_empty() { continue; } else { - println!("{:?}", parser.parsed_args_mut()); + println!("{:?}", parser.parsed_args()); // let command = row.split_whitespace(); } } else { diff --git a/src/parser.rs b/src/parser.rs index 4aa736f..c94e6df 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -15,6 +15,7 @@ where Ok(io::BufReader::new(file).lines()) } +#[derive(Debug, Clone)] pub enum Command { ARITHMETIC, CALL, @@ -29,6 +30,7 @@ pub enum Command { #[derive(Debug, Clone)] pub struct Args { + command: Option, one: Option, two: Option, three: Option, @@ -37,23 +39,46 @@ pub struct Args { impl Args { pub fn new() -> Self { Self { + command: None, one: None, two: None, three: None, } } - pub fn mutate(&mut self, args: String, number: usize) { + pub fn mutate_args(&mut self, arg: String, number: usize) { assert!(1 <= number && number <= 3); if number == 1 { - self.one = Some(args); + self.one = Some(arg.clone()); + self.mutate_command(arg); } else if number == 2 { - self.two = Some(args); + self.two = Some(arg); } else { - self.three = Some(args); + self.three = Some(arg); } } + pub fn mutate_command(&mut self, arg: String) { + self.command = Some( + if arg.starts_with("add") + || arg.starts_with("and") + || arg.starts_with("or") + || arg.starts_with("neg") + || arg.starts_with("not") + || arg.starts_with("sub") + || arg.starts_with("lt") + || arg.starts_with("gt") + || arg.starts_with("eq") + { + Command::ARITHMETIC + } else if arg.starts_with("pop") { + Command::POP + } else { + Command::PUSH + }, + ); + } pub fn is_empty(&self) -> bool { return self.one.is_none() && self.two.is_none() && self.three.is_none(); + // return self.command.is_none(); } } @@ -78,7 +103,7 @@ impl> Parser

{ &self.filename } - pub fn parsed_args_mut(&self) -> &Args { + pub fn parsed_args(&self) -> &Args { &self.parsed_args } @@ -90,7 +115,7 @@ impl> Parser

{ let command_args = row.split_whitespace(); let mut args = Args::new(); for (number, arg) in command_args.enumerate() { - args.mutate(arg.to_string(), number + 1); + args.mutate_args(arg.to_string(), number + 1); } self.parsed_args = args.clone(); Ok(args)