From 74307d5bbdef83eb1ddda7b90bda42853b46873d Mon Sep 17 00:00:00 2001 From: Alvin <524715@vistacollege.nl> Date: Wed, 10 Sep 2025 12:52:20 +0200 Subject: [PATCH] feat: Implement loops --- README.md | 14 +++++++++++++- loop.tralla | 6 ++++++ src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 loop.tralla diff --git a/README.md b/README.md index 0ae3dfa..9b9038f 100644 --- a/README.md +++ b/README.md @@ -32,4 +32,16 @@ This command prints a value to the console. The value can be a string literal (e This command declares a variable and assigns a value to it. The value can be a string literal or a number. -## Ciao! +### `Pinguino Arrabiato Fruti ` + +This command creates a loop that repeats a block of code a specified number of times. The block of code must be enclosed in `{` and `}` on separate lines. + +Example: +```trallalelo +Pinguino Arrabiato Fruti 3 +{ + Matteeeo "Hello" +} +``` + +## Ciao! \ No newline at end of file diff --git a/loop.tralla b/loop.tralla new file mode 100644 index 0000000..75b30df --- /dev/null +++ b/loop.tralla @@ -0,0 +1,6 @@ +Tralalero Tralala +Pinguino Arrabiato Fruti 3 +{ + Matteeeo "This is inside the loop" +} +Bombardiro Crocodilo \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 5cf95e8..b6cd3c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,30 +25,66 @@ fn main() { } let mut variables: HashMap = HashMap::new(); + let mut pc = 1; // Program counter starts after "Tralalero Tralala" - for line in lines.iter().skip(1).rev().skip(1).rev() { - parse_and_execute(line, &mut variables); + while pc < lines.len() - 1 { + pc = parse_and_execute(pc, &lines, &mut variables); } } -fn parse_and_execute(line: &str, variables: &mut HashMap) { +fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap) -> usize { + let line = lines[pc].trim(); 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()); } + return pc + 1; } 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); + let expression = words.collect::>().join(" "); + if expression.starts_with('"') && expression.ends_with('"') { + println!("{}", expression.trim_matches('"')); + } else { + if let Some(value) = variables.get(&expression) { + println!("{}", value); + } + } + return pc + 1; + } else if keyword == "Pinguino" { + if let (Some(word2), Some(word3), Some(times_str)) = (words.next(), words.next(), words.next()) { + if word2 == "Arrabiato" && word3 == "Fruti" { + if let Ok(times) = times_str.parse::() { + let loop_start = pc + 2; // After "Pinguino..." and "{" + let mut loop_end = loop_start; + let mut brace_count = 1; + + for i in loop_start..lines.len() { + if lines[i].trim() == "{" { + brace_count += 1; + } + if lines[i].trim() == "}" { + brace_count -= 1; + if brace_count == 0 { + loop_end = i; + break; + } + } + } + + for _ in 0..times { + let mut inner_pc = loop_start; + while inner_pc < loop_end { + inner_pc = parse_and_execute(inner_pc, lines, variables); + } + } + return loop_end + 1; } } } } } + pc + 1 } \ No newline at end of file