From ea9eead3ba33327839b33a0cf8000c7951d3e3c2 Mon Sep 17 00:00:00 2001 From: Alvin <524715@vistacollege.nl> Date: Wed, 10 Sep 2025 12:53:48 +0200 Subject: [PATCH] feat: Implement arithmetic operations --- README.md | 14 +++++++++++++- arithmetic.tralla | 6 ++++++ src/main.rs | 26 +++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 arithmetic.tralla diff --git a/README.md b/README.md index 37b8f63..b10a6c2 100644 --- a/README.md +++ b/README.md @@ -44,4 +44,16 @@ Pinguino Arrabiato Fruti 3 } ``` -## Ciao! +### `Chimpanzini ` + +This command performs an arithmetic operation and stores the result in a variable. The supported operators are `+`, `-`, `*`, `/`. + +Example: +```tralalero +Biscottini x 10 +Biscottini y 5 +Chimpanzini result x + y +Matteeeo result // prints 15 +``` + +## Ciao! \ No newline at end of file diff --git a/arithmetic.tralla b/arithmetic.tralla new file mode 100644 index 0000000..5a23086 --- /dev/null +++ b/arithmetic.tralla @@ -0,0 +1,6 @@ +Tralalero Tralala +Biscottini x 10 +Biscottini y 5 +Chimpanzini result x + y +Matteeeo result +Bombardiro Crocodilo diff --git a/src/main.rs b/src/main.rs index b6cd3c8..95f7037 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,16 @@ fn main() { } } +fn get_value(s: &str, variables: &HashMap) -> Option { + if let Ok(num) = s.parse::() { + Some(num) + } else if let Some(val_str) = variables.get(s) { + val_str.parse::().ok() + } else { + None + } +} + fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap) -> usize { let line = lines[pc].trim(); let mut words = line.split_whitespace(); @@ -84,7 +94,21 @@ fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap op1 + op2, + "-" => op1 - op2, + "*" => op1 * op2, + "/" => op1 / op2, + _ => 0.0, + }; + variables.insert(var_name.to_string(), result.to_string()); + } + } + return pc + 1; } } pc + 1 -} \ No newline at end of file +}