diff --git a/src/main.rs b/src/main.rs index bdeb4b0..217c2f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) } } diff --git a/src/parser.rs b/src/parser.rs index 742e410..ab84a05 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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, pub pos: usize } +#[derive(Debug, Clone, PartialEq)] +pub struct AstIf { + pub condition: Box, + pub body: Vec, + pub pos: usize +} fn is_end(input: &Token, end: &Vec) -> bool { for token in end { @@ -344,6 +351,25 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, 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); } diff --git a/test.as b/test.as index 820f28d..316b28d 100644 --- a/test.as +++ b/test.as @@ -4,4 +4,7 @@ ugass(a,b) gethelj c = piszv & nem piszv gethelj d = lőcsve(e,f) { ugass(a,b) +} +ha geny (1 == 1) { + ugass(1) } \ No newline at end of file