mirror of
https://github.com/Alvin-Zilverstand/Tralalero_lang.git
synced 2026-03-06 11:17:06 +01:00
feat: Implement arithmetic operations
This commit is contained in:
14
README.md
14
README.md
@@ -44,4 +44,16 @@ Pinguino Arrabiato Fruti 3
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Ciao!
|
### `Chimpanzini <variable_name> <operand1> <operator> <operand2>`
|
||||||
|
|
||||||
|
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!
|
||||||
6
arithmetic.tralla
Normal file
6
arithmetic.tralla
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
Tralalero Tralala
|
||||||
|
Biscottini x 10
|
||||||
|
Biscottini y 5
|
||||||
|
Chimpanzini result x + y
|
||||||
|
Matteeeo result
|
||||||
|
Bombardiro Crocodilo
|
||||||
26
src/main.rs
26
src/main.rs
@@ -32,6 +32,16 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_value(s: &str, variables: &HashMap<String, String>) -> Option<f64> {
|
||||||
|
if let Ok(num) = s.parse::<f64>() {
|
||||||
|
Some(num)
|
||||||
|
} else if let Some(val_str) = variables.get(s) {
|
||||||
|
val_str.parse::<f64>().ok()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap<String, String>) -> usize {
|
fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap<String, String>) -> usize {
|
||||||
let line = lines[pc].trim();
|
let line = lines[pc].trim();
|
||||||
let mut words = line.split_whitespace();
|
let mut words = line.split_whitespace();
|
||||||
@@ -84,7 +94,21 @@ fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap<Strin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if keyword == "Chimpanzini" {
|
||||||
|
if let (Some(var_name), Some(op1_str), Some(op), Some(op2_str)) = (words.next(), words.next(), words.next(), words.next()) {
|
||||||
|
if let (Some(op1), Some(op2)) = (get_value(op1_str, variables), get_value(op2_str, variables)) {
|
||||||
|
let result = match op {
|
||||||
|
"+" => op1 + op2,
|
||||||
|
"-" => op1 - op2,
|
||||||
|
"*" => op1 * op2,
|
||||||
|
"/" => op1 / op2,
|
||||||
|
_ => 0.0,
|
||||||
|
};
|
||||||
|
variables.insert(var_name.to_string(), result.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pc + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pc + 1
|
pc + 1
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user