diff --git a/README.md b/README.md index 1e249ef..0ae3dfa 100644 --- a/README.md +++ b/README.md @@ -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 ""` +### `Matteeeo ` -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! \ No newline at end of file +### `Biscottini ` + +This command declares a variable and assigns a value to it. The value can be a string literal or a number. + +## Ciao! diff --git a/hello_world.tralla b/hello_world.tralla index 19eebb6..6cd4fb2 100644 --- a/hello_world.tralla +++ b/hello_world.tralla @@ -1,3 +1,3 @@ Tralalero Tralala -Matteeeo bambini gusini "Hello, World!" -Bombardiro Crocodilo +Matteeeo "Hello, World!" +Bombardiro Crocodilo \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c793cf2..5cf95e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::env; use std::fs; +use std::collections::HashMap; fn main() { let args: Vec = env::args().collect(); @@ -23,20 +24,31 @@ fn main() { return; } + let mut variables: HashMap = 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) { + 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::>().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); + } + } + } } } -} +} \ No newline at end of file diff --git a/variables.tralla b/variables.tralla new file mode 100644 index 0000000..59de35d --- /dev/null +++ b/variables.tralla @@ -0,0 +1,4 @@ +Tralalero Tralala +Biscottini my_variable "Hello from a variable!" +Matteeeo my_variable +Bombardiro Crocodilo \ No newline at end of file