feat: Implement variables and simplify syntax

This commit is contained in:
Alvin
2025-09-10 12:50:25 +02:00
parent 4cd38de88c
commit eb1a79dc88
4 changed files with 36 additions and 16 deletions

View File

@@ -24,8 +24,12 @@ This is the entry point of the program. It must be the first line of your code.
This is the exit point of the program. It must be the last line of your code.
### `Matteeeo bambini gusini "<message>"`
### `Matteeeo <value>`
This command prints a message to the console. The message must be enclosed in double quotes.
This command prints a value to the console. The value can be a string literal (enclosed in double quotes) or a variable.
### `Biscottini <variable_name> <value>`
This command declares a variable and assigns a value to it. The value can be a string literal or a number.
## Ciao!

View File

@@ -1,3 +1,3 @@
Tralalero Tralala
Matteeeo bambini gusini "Hello, World!"
Matteeeo "Hello, World!"
Bombardiro Crocodilo

View File

@@ -1,5 +1,6 @@
use std::env;
use std::fs;
use std::collections::HashMap;
fn main() {
let args: Vec<String> = env::args().collect();
@@ -23,20 +24,31 @@ fn main() {
return;
}
let mut variables: HashMap<String, String> = HashMap::new();
for line in lines.iter().skip(1).rev().skip(1).rev() {
parse_and_execute(line);
parse_and_execute(line, &mut variables);
}
}
fn parse_and_execute(line: &str) {
if line.starts_with("Matteeeo bambini gusini") {
let parts: Vec<&str> = line.splitn(2, ' ').collect();
if parts.len() > 1 {
let string_to_print = parts[1]
.trim_start_matches("bambini gusini")
.trim()
.trim_matches('"');
println!("{}", string_to_print);
fn parse_and_execute(line: &str, variables: &mut HashMap<String, String>) {
let mut words = line.split_whitespace();
if let Some(keyword) = words.next() {
if keyword == "Biscottini" {
if let Some(var_name) = words.next() {
let value = words.collect::<Vec<&str>>().join(" ");
variables.insert(var_name.to_string(), value.trim_matches('"').to_string());
}
} else if keyword == "Matteeeo" {
if let Some(expression) = words.next() {
if expression.starts_with('"') && expression.ends_with('"') {
println!("{}", expression.trim_matches('"'));
} else {
if let Some(value) = variables.get(expression) {
println!("{}", value);
}
}
}
}
}
}

4
variables.tralla Normal file
View File

@@ -0,0 +1,4 @@
Tralalero Tralala
Biscottini my_variable "Hello from a variable!"
Matteeeo my_variable
Bombardiro Crocodilo