added functions

This commit is contained in:
afonya2 2025-05-23 23:26:16 +02:00
parent 92a8cabcab
commit 74afdd73db
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
3 changed files with 32 additions and 12 deletions

View file

@ -40,7 +40,6 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
}, },
ASTPart::Function(func) => { ASTPart::Function(func) => {
println!("{}{}: Function:", prefix, func.pos); println!("{}{}: Function:", prefix, func.pos);
println!("{} Name: {}", prefix, func.name);
println!("{} Args: {}", prefix, func.args.join(", ")); println!("{} Args: {}", prefix, func.args.join(", "));
println!("{} Body:", prefix); println!("{} Body:", prefix);
for part in &func.body { for part in &func.body {

View file

@ -67,7 +67,6 @@ pub struct AstVarUpdate {
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct AstFunction { pub struct AstFunction {
pub name: String,
pub args: Vec<String>, pub args: Vec<String>,
pub body: Vec<ASTPart>, pub body: Vec<ASTPart>,
pub pos: usize pub pos: usize
@ -140,6 +139,9 @@ fn shunt(input: Vec<ASTPart>) -> ASTPart {
ASTPart::Null(_) => { ASTPart::Null(_) => {
output.push(part); output.push(part);
}, },
ASTPart::Function(_) => {
output.push(part);
},
ASTPart::Operation(op) => { ASTPart::Operation(op) => {
if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP { if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP {
output.push(part); 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 { fn read_function(input: &Vec<Token>, pos: &mut usize, with_args: bool) -> ASTPart {
let mut args: Vec<String> = vec![]; let mut args: Vec<String> = vec![];
let start_pos = input[*pos].pos;
if with_args { if with_args {
if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "(" { if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "(" {
panic!("Expected ( at {}", input[*pos].pos); 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; *pos += 1;
} }
println!("Args: {:?}", args); if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "{" {
return ASTPart::NOOP; 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 { 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![];
@ -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 })); expressions.push(ASTPart::Null(AstNull { pos: token.pos }));
} else if token.value == "lőcsve" { } else if token.value == "lőcsve" {
let func = read_function(input, pos, true); let func = read_function(input, pos, true);
println!("Function: {:?}", func); expressions.push(func);
} else { } else {
panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos); 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 out: Vec<ASTPart> = vec![];
let mut pos = 0; while *pos < input.len() {
while pos < input.len() { let op = next_operation(pos, &input, &op_ends, &parse_ends);
let op = next_operation(&mut pos, &input, &op_ends, &parse_ends);
match op { match op {
ASTPart::NOOP => {}, ASTPart::NOOP => {},
_ => { _ => {
out.push(op); out.push(op);
} }
} }
if is_end(&input[pos], &parse_ends) { if is_end(&input[*pos], &parse_ends) {
break; break;
} }
} }
@ -370,6 +388,6 @@ pub fn parse(input: Vec<Token>) -> Vec<ASTPart> {
let parse_ends: Vec<Token> = vec![ let parse_ends: Vec<Token> = vec![
Token { typ: TokenType::OPEND, value: String::from("EOF"), pos: 0 } 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; return out;
} }

View file

@ -1,4 +1,7 @@
gethelj a = 1 gethelj a = 1
gethelj b = (a + 2) * 3 gethelj b = (a + 2) * 3
ugass(a,b) ugass(a,b)
gethelj c = piszv & nem piszv gethelj c = piszv & nem piszv
gethelj d = lőcsve(e,f) {
ugass(a,b)
}