diff --git a/README.md b/README.md index b10a6c2..0a66aa1 100644 --- a/README.md +++ b/README.md @@ -56,4 +56,17 @@ Chimpanzini result x + y Matteeeo result // prints 15 ``` -## Ciao! \ No newline at end of file +### `Tung Tung Tung ` + +This command executes a block of code if a condition is true. The supported operators are `==`, `!=`, `>`, `<`, `>=`, `<=`. + +Example: +```tralalero +Biscottini x 10 +Tung Tung Tung x > 5 +{ + Matteeeo "x is greater than 5" +} +``` + +## Ciao! diff --git a/conditional.tralla b/conditional.tralla new file mode 100644 index 0000000..35847fc --- /dev/null +++ b/conditional.tralla @@ -0,0 +1,7 @@ +Tralalero Tralala +Biscottini x 10 +Tung Tung Tung x > 5 +{ + Matteeeo "x is greater than 5" +} +Bombardiro Crocodilo diff --git a/src/main.rs b/src/main.rs index 95f7037..3b07069 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,7 +108,48 @@ fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap op1 == op2, + "!=" => op1 != op2, + ">" => op1 > op2, + "<" => op1 < op2, + ">=" => op1 >= op2, + "<=" => op1 <= op2, + _ => false, + }; + + let block_start = pc + 2; + let mut block_end = block_start; + let mut brace_count = 1; + + for i in block_start..lines.len() { + if lines[i].trim() == "{" { + brace_count += 1; + } + if lines[i].trim() == "}" { + brace_count -= 1; + if brace_count == 0 { + block_end = i; + break; + } + } + } + + if condition { + let mut inner_pc = block_start; + while inner_pc < block_end { + inner_pc = parse_and_execute(inner_pc, lines, variables); + } + } + return block_end + 1; + } + } + } } } pc + 1 -} +} \ No newline at end of file