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. 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.
## Ciao! ### `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 Tralalero Tralala
Matteeeo bambini gusini "Hello, World!" Matteeeo "Hello, World!"
Bombardiro Crocodilo Bombardiro Crocodilo

View File

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