added ifs

This commit is contained in:
afonya2 2025-05-23 23:47:02 +02:00
parent 74afdd73db
commit 9565ca41fa
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
3 changed files with 38 additions and 0 deletions

View file

@ -46,6 +46,15 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
log_ast_part(part, format!("{} ", prefix));
}
},
ASTPart::If(if_part) => {
println!("{}{}: If:", prefix, if_part.pos);
println!("{} Condition:", prefix);
log_ast_part(&if_part.condition, format!("{} ", prefix));
println!("{} Body:", prefix);
for part in &if_part.body {
log_ast_part(part, format!("{} ", prefix));
}
},
ASTPart::NOOP => println!("{}NOOP", prefix)
}
}

View file

@ -14,6 +14,7 @@ pub enum ASTPart {
Call(AstCall),
VarUpdate(AstVarUpdate),
Function(AstFunction),
If(AstIf),
NOOP
}
#[derive(Debug, Clone, PartialEq)]
@ -71,6 +72,12 @@ pub struct AstFunction {
pub body: Vec<ASTPart>,
pub pos: usize
}
#[derive(Debug, Clone, PartialEq)]
pub struct AstIf {
pub condition: Box<ASTPart>,
pub body: Vec<ASTPart>,
pub pos: usize
}
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
for token in end {
@ -344,6 +351,25 @@ fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, par
}
let value = read_exp(pos, input, op_ends, parse_ends);
return ASTPart::Assigment(AstAssigment { variable: variable.value.clone(), value: Box::new(value), pos: token.pos });
} else if token.value == "ha 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::If(AstIf { condition: Box::new(condition), body: real_body, pos: token.pos });
} else {
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
}

View file

@ -5,3 +5,6 @@ gethelj c = piszv & nem piszv
gethelj d = lőcsve(e,f) {
ugass(a,b)
}
ha geny (1 == 1) {
ugass(1)
}