added functions
This commit is contained in:
parent
92a8cabcab
commit
74afdd73db
3 changed files with 32 additions and 12 deletions
|
@ -40,7 +40,6 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
|
|||
},
|
||||
ASTPart::Function(func) => {
|
||||
println!("{}{}: Function:", prefix, func.pos);
|
||||
println!("{} Name: {}", prefix, func.name);
|
||||
println!("{} Args: {}", prefix, func.args.join(", "));
|
||||
println!("{} Body:", prefix);
|
||||
for part in &func.body {
|
||||
|
|
|
@ -67,7 +67,6 @@ pub struct AstVarUpdate {
|
|||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AstFunction {
|
||||
pub name: String,
|
||||
pub args: Vec<String>,
|
||||
pub body: Vec<ASTPart>,
|
||||
pub pos: usize
|
||||
|
@ -140,6 +139,9 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
|||
ASTPart::Null(_) => {
|
||||
output.push(part);
|
||||
},
|
||||
ASTPart::Function(_) => {
|
||||
output.push(part);
|
||||
},
|
||||
ASTPart::Operation(op) => {
|
||||
if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP {
|
||||
output.push(part);
|
||||
|
@ -198,6 +200,7 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
|
|||
}
|
||||
fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPart {
|
||||
let mut args: Vec<String> = vec![];
|
||||
let start_pos = input[*pos].pos;
|
||||
if with_args {
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "(" {
|
||||
panic!("Expected ( at {}", input[*pos].pos);
|
||||
|
@ -223,8 +226,24 @@ fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPar
|
|||
}
|
||||
*pos += 1;
|
||||
}
|
||||
println!("Args: {:?}", args);
|
||||
return ASTPart::NOOP;
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "{" {
|
||||
panic!("Expected {{ at {}", input[*pos].pos);
|
||||
}
|
||||
*pos += 1;
|
||||
let op_ends: Vec<Token> = vec![
|
||||
Token { typ: TokenType::OPEND, value: String::from("\n"), pos: 0 },
|
||||
Token { typ: TokenType::OPEND, value: String::from(";"), pos: 0 },
|
||||
Token { typ: TokenType::SEPARATOR, value: String::from("}"), pos: 0 }
|
||||
];
|
||||
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);
|
||||
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "}" {
|
||||
panic!("Unexpected end of function at {}", input[*pos].pos);
|
||||
}
|
||||
*pos += 1;
|
||||
return ASTPart::Function(AstFunction { args: args, body: body, pos: start_pos });
|
||||
}
|
||||
fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends: &Vec<Token>) -> ASTPart {
|
||||
let mut expressions: Vec<ASTPart> = vec![];
|
||||
|
@ -258,7 +277,7 @@ fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends:
|
|||
expressions.push(ASTPart::Null(AstNull { pos: token.pos }));
|
||||
} else if token.value == "lőcsve" {
|
||||
let func = read_function(input, pos, true);
|
||||
println!("Function: {:?}", func);
|
||||
expressions.push(func);
|
||||
} else {
|
||||
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
|
||||
}
|
||||
|
@ -344,18 +363,17 @@ 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>) -> 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![];
|
||||
let mut pos = 0;
|
||||
while pos < input.len() {
|
||||
let op = next_operation(&mut pos, &input, &op_ends, &parse_ends);
|
||||
while *pos < input.len() {
|
||||
let op = next_operation(pos, &input, &op_ends, &parse_ends);
|
||||
match op {
|
||||
ASTPart::NOOP => {},
|
||||
_ => {
|
||||
out.push(op);
|
||||
}
|
||||
}
|
||||
if is_end(&input[pos], &parse_ends) {
|
||||
if is_end(&input[*pos], &parse_ends) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -370,6 +388,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);
|
||||
let out = parse_internal(&input, op_ends, parse_ends, &mut 0);
|
||||
return out;
|
||||
}
|
5
test.as
5
test.as
|
@ -1,4 +1,7 @@
|
|||
gethelj a = 1
|
||||
gethelj b = (a + 2) * 3
|
||||
ugass(a,b)
|
||||
gethelj c = piszv & nem piszv
|
||||
gethelj c = piszv & nem piszv
|
||||
gethelj d = lőcsve(e,f) {
|
||||
ugass(a,b)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue