diff --git a/src/main.rs b/src/main.rs index 217c2f2..1703445 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) } } diff --git a/src/parser.rs b/src/parser.rs index ab84a05..052563f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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, pub pos: usize } +#[derive(Debug, Clone, PartialEq)] +pub struct AstWhile { + pub condition: Box, + pub body: Vec, + pub pos: usize +} +#[derive(Debug, Clone, PartialEq)] +pub struct AstBreak { + pub pos: usize +} fn is_end(input: &Token, end: &Vec) -> bool { for token in end { @@ -370,6 +382,27 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, 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); }