mirror of
https://github.com/Alvin-Zilverstand/Tralalero_lang.git
synced 2026-03-06 11:17:06 +01:00
feat: Implement else statements
This commit is contained in:
14
README.md
14
README.md
@@ -60,13 +60,19 @@ Matteeeo result // prints 15
|
|||||||
|
|
||||||
This command executes a block of code if a condition is true. The supported operators are `==`, `!=`, `>`, `<`, `>=`, `<=`.
|
This command executes a block of code if a condition is true. The supported operators are `==`, `!=`, `>`, `<`, `>=`, `<=`.
|
||||||
|
|
||||||
|
This can be followed by a `Ballerina Cappuccina` block to execute code if the condition is false.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```tralalero
|
```tralalero
|
||||||
Biscottini x 10
|
Biscottini x 5
|
||||||
Tung Tung Tung x > 5
|
Tung Tung Tung x > 10
|
||||||
{
|
{
|
||||||
Matteeeo "x is greater than 5"
|
Matteeeo "x is greater than 10"
|
||||||
|
}
|
||||||
|
Ballerina Cappuccina
|
||||||
|
{
|
||||||
|
Matteeeo "x is not greater than 10"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Ciao!
|
## Ciao!
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
Tralalero Tralala
|
Tralalero Tralala
|
||||||
Biscottini x 10
|
Biscottini x 5
|
||||||
Tung Tung Tung x > 5
|
Tung Tung Tung x > 10
|
||||||
{
|
{
|
||||||
Matteeeo "x is greater than 5"
|
Matteeeo "x is greater than 10"
|
||||||
}
|
}
|
||||||
Bombardiro Crocodilo
|
Ballerina Cappuccina
|
||||||
|
{
|
||||||
|
Matteeeo "x is not greater than 10"
|
||||||
|
}
|
||||||
|
Bombardiro Crocodilo
|
||||||
49
src/main.rs
49
src/main.rs
@@ -122,34 +122,67 @@ fn parse_and_execute(pc: usize, lines: &Vec<&str>, variables: &mut HashMap<Strin
|
|||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let block_start = pc + 2;
|
let if_block_start = pc + 2;
|
||||||
let mut block_end = block_start;
|
let mut if_block_end = if_block_start;
|
||||||
let mut brace_count = 1;
|
let mut brace_count = 1;
|
||||||
|
|
||||||
for i in block_start..lines.len() {
|
for i in if_block_start..lines.len() {
|
||||||
if lines[i].trim() == "{" {
|
if lines[i].trim() == "{" {
|
||||||
brace_count += 1;
|
brace_count += 1;
|
||||||
}
|
}
|
||||||
if lines[i].trim() == "}" {
|
if lines[i].trim() == "}" {
|
||||||
brace_count -= 1;
|
brace_count -= 1;
|
||||||
if brace_count == 0 {
|
if brace_count == 0 {
|
||||||
block_end = i;
|
if_block_end = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut next_pc = if_block_end + 1;
|
||||||
|
|
||||||
if condition {
|
if condition {
|
||||||
let mut inner_pc = block_start;
|
let mut inner_pc = if_block_start;
|
||||||
while inner_pc < block_end {
|
while inner_pc < if_block_end {
|
||||||
inner_pc = parse_and_execute(inner_pc, lines, variables);
|
inner_pc = parse_and_execute(inner_pc, lines, variables);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Check for an else block
|
||||||
|
if let Some(next_line) = lines.get(if_block_end + 1) {
|
||||||
|
let mut next_words = next_line.trim().split_whitespace();
|
||||||
|
if let Some(else_keyword) = next_words.next() {
|
||||||
|
if else_keyword == "Ballerina" && next_words.next() == Some("Cappuccina") {
|
||||||
|
let else_block_start = if_block_end + 3;
|
||||||
|
let mut else_block_end = else_block_start;
|
||||||
|
let mut brace_count = 1;
|
||||||
|
|
||||||
|
for i in else_block_start..lines.len() {
|
||||||
|
if lines[i].trim() == "{" {
|
||||||
|
brace_count += 1;
|
||||||
|
}
|
||||||
|
if lines[i].trim() == "}" {
|
||||||
|
brace_count -= 1;
|
||||||
|
if brace_count == 0 {
|
||||||
|
else_block_end = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut inner_pc = else_block_start;
|
||||||
|
while inner_pc < else_block_end {
|
||||||
|
inner_pc = parse_and_execute(inner_pc, lines, variables);
|
||||||
|
}
|
||||||
|
next_pc = else_block_end + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return block_end + 1;
|
return next_pc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pc + 1
|
pc + 1
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user