added while, break

This commit is contained in:
afonya2 2025-05-24 15:54:11 +02:00
parent 9565ca41fa
commit c3a3c9032a
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
2 changed files with 43 additions and 0 deletions

View file

@ -55,6 +55,16 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
log_ast_part(part, format!("{} ", prefix));
}
},
ASTPart::While(while_part) => {
println!("{}{}: While:", prefix, while_part.pos);
println!("{} Condition:", prefix);
log_ast_part(&while_part.condition, format!("{} ", prefix));
println!("{} Body:", prefix);
for part in &while_part.body {
log_ast_part(part, format!("{} ", prefix));
}
},
ASTPart::Break(brk) => println!("{}{}: Break", prefix, brk.pos),
ASTPart::NOOP => println!("{}NOOP", prefix)
}
}

View file

@ -15,6 +15,8 @@ pub enum ASTPart {
VarUpdate(AstVarUpdate),
Function(AstFunction),
If(AstIf),
While(AstWhile),
Break(AstBreak),
NOOP
}
#[derive(Debug, Clone, PartialEq)]
@ -78,6 +80,16 @@ pub struct AstIf {
pub body: Vec<ASTPart>,
pub pos: usize
}
#[derive(Debug, Clone, PartialEq)]
pub struct AstWhile {
pub condition: Box<ASTPart>,
pub body: Vec<ASTPart>,
pub pos: usize
}
#[derive(Debug, Clone, PartialEq)]
pub struct AstBreak {
pub pos: usize
}
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
for token in end {
@ -370,6 +382,27 @@ fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, par
_ => panic!("Expected function body at {}", token.pos)
};
return ASTPart::If(AstIf { condition: Box::new(condition), body: real_body, pos: token.pos });
} else if token.value == "amíg geny" {
if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" {
panic!("Expected ( at {}", token.pos);
}
*pos += 1;
let condition_end = vec![
Token { typ: TokenType::SEPARATOR, value: String::from(")"), pos: 0 }
];
let condition = read_exp(pos, input, &condition_end, &condition_end);
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" {
panic!("Unexpected end of condition at {}", token.pos);
}
*pos += 1;
let body = read_function(input, pos, false);
let real_body = match body {
ASTPart::Function(func) => func.body,
_ => panic!("Expected function body at {}", token.pos)
};
return ASTPart::While(AstWhile { condition: Box::new(condition), body: real_body, pos: token.pos });
} else if token.value == "kraf" {
return ASTPart::Break(AstBreak { pos: token.pos });
} else {
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
}