added for, continue
This commit is contained in:
parent
c3a3c9032a
commit
efaca55138
3 changed files with 71 additions and 5 deletions
|
@ -17,6 +17,8 @@ pub enum ASTPart {
|
|||
If(AstIf),
|
||||
While(AstWhile),
|
||||
Break(AstBreak),
|
||||
For(AstFor),
|
||||
Continue(AstContinue),
|
||||
NOOP
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
@ -90,6 +92,18 @@ pub struct AstWhile {
|
|||
pub struct AstBreak {
|
||||
pub pos: usize
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AstFor {
|
||||
pub init: Box<ASTPart>,
|
||||
pub condition: Box<ASTPart>,
|
||||
pub update: Box<ASTPart>,
|
||||
pub body: Vec<ASTPart>,
|
||||
pub pos: usize
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AstContinue {
|
||||
pub pos: usize
|
||||
}
|
||||
|
||||
fn is_end(input: &Token, end: &Vec<Token>) -> bool {
|
||||
for token in end {
|
||||
|
@ -257,7 +271,7 @@ fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPar
|
|||
let parse_ends: Vec<Token> = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from("}"), pos: 0 }
|
||||
];
|
||||
let body = parse_internal(input, op_ends, parse_ends, pos);
|
||||
let body = parse_internal(input, &op_ends, &parse_ends, pos);
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "}" {
|
||||
panic!("Unexpected end of function at {}", input[*pos].pos);
|
||||
}
|
||||
|
@ -403,6 +417,44 @@ fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, par
|
|||
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 if token.value == "kopva" {
|
||||
if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" {
|
||||
panic!("Expected ( at {}", token.pos);
|
||||
}
|
||||
*pos += 1;
|
||||
let ends = vec![
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from(")"), pos: 0 },
|
||||
Token { typ: TokenType::OPEND, value: String::from(";"), pos: 0 }
|
||||
];
|
||||
let init = parse_internal(input, &ends, &ends, pos);
|
||||
if init.len() != 1 {
|
||||
panic!("Only one expression is expected for init at {}", token.pos);
|
||||
}
|
||||
if input[*pos].typ != TokenType::OPEND || input[*pos].value != ";" {
|
||||
panic!("Unexpected end of init at {}", token.pos);
|
||||
}
|
||||
*pos += 1;
|
||||
let condition = read_exp(pos, input, &ends, &ends);
|
||||
if input[*pos].typ != TokenType::OPEND || input[*pos].value != ";" {
|
||||
panic!("Unexpected end of condition at {}", token.pos);
|
||||
}
|
||||
*pos += 1;
|
||||
let update = parse_internal(input, &ends, &ends, pos);
|
||||
if update.len() != 1 {
|
||||
panic!("Only one expression is expected for update at {}", token.pos);
|
||||
}
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" {
|
||||
panic!("Unexpected end of update 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::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 {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
}
|
||||
|
@ -422,7 +474,7 @@ fn next_operation(pos: &mut usize, input: &Vec<Token>, op_ends: &Vec<Token>, par
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_internal(input: &Vec<Token>, op_ends: Vec<Token>, parse_ends: Vec<Token>, pos: &mut usize) -> Vec<ASTPart> {
|
||||
fn parse_internal(input: &Vec<Token>, op_ends: &Vec<Token>, parse_ends: &Vec<Token>, pos: &mut usize) -> Vec<ASTPart> {
|
||||
let mut out: Vec<ASTPart> = vec![];
|
||||
while *pos < input.len() {
|
||||
let op = next_operation(pos, &input, &op_ends, &parse_ends);
|
||||
|
@ -447,6 +499,6 @@ pub fn parse(input: Vec<Token>) -> Vec<ASTPart> {
|
|||
let parse_ends: Vec<Token> = vec![
|
||||
Token { typ: TokenType::OPEND, value: String::from("EOF"), pos: 0 }
|
||||
];
|
||||
let out = parse_internal(&input, op_ends, parse_ends, &mut 0);
|
||||
let out = parse_internal(&input, &op_ends, &parse_ends, &mut 0);
|
||||
return out;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue