more function stuff

This commit is contained in:
afonya2 2025-05-23 21:19:17 +02:00
parent 315ccc1504
commit 92a8cabcab
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
2 changed files with 42 additions and 1 deletions

View file

@ -10,6 +10,7 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
ASTPart::Number(number) => println!("{}{}: Number: {}", prefix, number.pos, number.value), ASTPart::Number(number) => println!("{}{}: Number: {}", prefix, number.pos, number.value),
ASTPart::String(str) => println!("{}{}: String: {}", prefix, str.pos, str.value), ASTPart::String(str) => println!("{}{}: String: {}", prefix, str.pos, str.value),
ASTPart::Boolean(bool) => println!("{}{}: Boolean: {}", prefix, bool.pos, bool.value), ASTPart::Boolean(bool) => println!("{}{}: Boolean: {}", prefix, bool.pos, bool.value),
ASTPart::Null(null) => println!("{}{}: Null", prefix, null.pos),
ASTPart::Assigment(assigment) => { ASTPart::Assigment(assigment) => {
println!("{}{}: Assigment: {}", prefix, assigment.pos, assigment.variable); println!("{}{}: Assigment: {}", prefix, assigment.pos, assigment.variable);
println!("{} Value:", prefix); println!("{} Value:", prefix);
@ -37,6 +38,15 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
log_ast_part(&arg, format!("{} ", prefix)); log_ast_part(&arg, format!("{} ", prefix));
} }
}, },
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 {
log_ast_part(part, format!("{} ", prefix));
}
},
ASTPart::NOOP => println!("{}NOOP", prefix) ASTPart::NOOP => println!("{}NOOP", prefix)
} }
} }

View file

@ -196,6 +196,36 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
} }
return output[0].clone(); return output[0].clone();
} }
fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPart {
let mut args: Vec<String> = vec![];
if with_args {
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "(" {
panic!("Expected ( at {}", input[*pos].pos);
}
*pos += 1;
while pos < &mut input.len() {
let token = &input[*pos];
if token.typ == TokenType::SEPARATOR && token.value == ")" {
break;
}
*pos += 1;
if token.typ == TokenType::SEPARATOR && token.value == "," {
continue;
}
if token.typ == TokenType::IDENTIFIER {
args.push(token.value.clone());
} else {
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
}
}
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" {
panic!("Unexpected end of arguments at {}", input[*pos].pos);
}
*pos += 1;
}
println!("Args: {:?}", args);
return ASTPart::NOOP;
}
fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends: &Vec<Token>) -> ASTPart { fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends: &Vec<Token>) -> ASTPart {
let mut expressions: Vec<ASTPart> = vec![]; let mut expressions: Vec<ASTPart> = vec![];
while pos < &mut input.len() { while pos < &mut input.len() {
@ -227,7 +257,8 @@ fn read_exp(pos: &mut usize, input: &Vec<Token>, ends: &Vec<Token>, parse_ends:
} else if token.value == "nincs hám" { } else if token.value == "nincs hám" {
expressions.push(ASTPart::Null(AstNull { pos: token.pos })); expressions.push(ASTPart::Null(AstNull { pos: token.pos }));
} else if token.value == "lőcsve" { } else if token.value == "lőcsve" {
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos); let func = read_function(input, pos, true);
println!("Function: {:?}", func);
} else { } else {
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos); panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos);
} }