added else, elseif, cleaned up

This commit is contained in:
afonya2 2025-05-24 16:27:53 +02:00
parent efaca55138
commit eae6c61c24
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
4 changed files with 66 additions and 1 deletions

View file

@ -100,7 +100,7 @@ fn generate_combinations(words: Vec<&str>) -> Vec<String> {
return result; return result;
} }
fn read_identifier(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>) { fn read_identifier(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>) {
let keywords = vec!["kraf","piszolj","ha nem geny akkor geny","ha nem geny","nem piszv","kopva","gethelj","ha geny","jukadban","lőcsve","nem reti","csecs","megint","reti","piszv","amíg geny","nincs hám","szard le"]; let keywords = vec!["kraf","piszolj","ha nem geny akkor geny","ha nem geny","nem piszv","kopva","gethelj","ha geny","lőcsve","csecs","reti","piszv","amíg geny","nincs hám","szard le"];
let mut raw_keywords: Vec<String> = vec![]; let mut raw_keywords: Vec<String> = vec![];
for keyword in &keywords { for keyword in &keywords {
let spi: Vec<&str> = keyword.split(" ").collect(); let spi: Vec<&str> = keyword.split(" ").collect();

View file

@ -55,6 +55,22 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
log_ast_part(part, format!("{} ", prefix)); log_ast_part(part, format!("{} ", prefix));
} }
}, },
ASTPart::ElseIf(elif) => {
println!("{}{}: Else If:", prefix, elif.pos);
println!("{} Condition:", prefix);
log_ast_part(&elif.condition, format!("{} ", prefix));
println!("{} Body:", prefix);
for part in &elif.body {
log_ast_part(part, format!("{} ", prefix));
}
},
ASTPart::Else(els) => {
println!("{}{}: Else:", prefix, els.pos);
println!("{} Body:", prefix);
for part in &els.body {
log_ast_part(part, format!("{} ", prefix));
}
},
ASTPart::While(while_part) => { ASTPart::While(while_part) => {
println!("{}{}: While:", prefix, while_part.pos); println!("{}{}: While:", prefix, while_part.pos);
println!("{} Condition:", prefix); println!("{} Condition:", prefix);

View file

@ -15,6 +15,8 @@ pub enum ASTPart {
VarUpdate(AstVarUpdate), VarUpdate(AstVarUpdate),
Function(AstFunction), Function(AstFunction),
If(AstIf), If(AstIf),
ElseIf(AstElseIf),
Else(AstElse),
While(AstWhile), While(AstWhile),
Break(AstBreak), Break(AstBreak),
For(AstFor), For(AstFor),
@ -83,6 +85,17 @@ pub struct AstIf {
pub pos: usize pub pos: usize
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct AstElseIf {
pub condition: Box<ASTPart>,
pub body: Vec<ASTPart>,
pub pos: usize
}
#[derive(Debug, Clone, PartialEq)]
pub struct AstElse {
pub body: Vec<ASTPart>,
pub pos: usize
}
#[derive(Debug, Clone, PartialEq)]
pub struct AstWhile { pub struct AstWhile {
pub condition: Box<ASTPart>, pub condition: Box<ASTPart>,
pub body: Vec<ASTPart>, pub body: Vec<ASTPart>,
@ -455,6 +468,32 @@ fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, par
return ASTPart::For(AstFor { init: Box::new(init[0].clone()), condition: Box::new(condition), update: Box::new(update[0].clone()), body: real_body, pos: token.pos }); return ASTPart::For(AstFor { init: Box::new(init[0].clone()), condition: Box::new(condition), update: Box::new(update[0].clone()), body: real_body, pos: token.pos });
} else if token.value == "szard le" { } else if token.value == "szard le" {
return ASTPart::Continue(AstContinue { pos: token.pos }); return ASTPart::Continue(AstContinue { pos: token.pos });
} else if token.value == "ha nem geny akkor 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::ElseIf(AstElseIf { condition: Box::new(condition), body: real_body, pos: token.pos });
} else if token.value == "ha nem geny" {
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::Else(AstElse { body: real_body, pos: token.pos });
} else { } else {
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos); panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
} }

10
test.as
View file

@ -7,4 +7,14 @@ gethelj d = lőcsve(e,f) {
} }
ha geny (1 == 1) { ha geny (1 == 1) {
ugass(1) ugass(1)
} ha nem geny {
ugass(2)
} ha nem geny akkor geny (1 == 2) {
ugass(3)
}
amíg geny (1 == 1) {
kraf
}
kopva (gethelj i = 0; i < 10; i = i + 1) {
ugass(i)
} }