added else, elseif, cleaned up
This commit is contained in:
parent
efaca55138
commit
eae6c61c24
4 changed files with 66 additions and 1 deletions
|
@ -100,7 +100,7 @@ fn generate_combinations(words: Vec<&str>) -> Vec<String> {
|
|||
return result;
|
||||
}
|
||||
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![];
|
||||
for keyword in &keywords {
|
||||
let spi: Vec<&str> = keyword.split(" ").collect();
|
||||
|
|
16
src/main.rs
16
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);
|
||||
|
|
|
@ -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<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 condition: Box<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 });
|
||||
} 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);
|
||||
}
|
||||
|
|
10
test.as
10
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue