diff --git a/src/lexer.rs b/src/lexer.rs index 5979a42..1289c5d 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -100,7 +100,7 @@ fn generate_combinations(words: Vec<&str>) -> Vec { return result; } fn read_identifier(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec) { - 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 = vec![]; for keyword in &keywords { let spi: Vec<&str> = keyword.split(" ").collect(); diff --git a/src/main.rs b/src/main.rs index 9dc6ffd..a3d45aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,6 +55,22 @@ fn log_ast_part(part: &ASTPart, prefix: String) { 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) => { println!("{}{}: While:", prefix, while_part.pos); println!("{} Condition:", prefix); diff --git a/src/parser.rs b/src/parser.rs index 5cb76e4..390b546 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -15,6 +15,8 @@ pub enum ASTPart { VarUpdate(AstVarUpdate), Function(AstFunction), If(AstIf), + ElseIf(AstElseIf), + Else(AstElse), While(AstWhile), Break(AstBreak), For(AstFor), @@ -83,6 +85,17 @@ pub struct AstIf { pub pos: usize } #[derive(Debug, Clone, PartialEq)] +pub struct AstElseIf { + pub condition: Box, + pub body: Vec, + pub pos: usize +} +#[derive(Debug, Clone, PartialEq)] +pub struct AstElse { + pub body: Vec, + pub pos: usize +} +#[derive(Debug, Clone, PartialEq)] pub struct AstWhile { pub condition: Box, pub body: Vec, @@ -455,6 +468,32 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, 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 }); } else if token.value == "szard le" { 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 { panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos); } diff --git a/test.as b/test.as index 316b28d..5793560 100644 --- a/test.as +++ b/test.as @@ -7,4 +7,14 @@ gethelj d = lőcsve(e,f) { } ha geny (1 == 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) } \ No newline at end of file