From 92a8cabcab4083d584bd7a1302aa09d663248f49 Mon Sep 17 00:00:00 2001 From: afonya2 Date: Fri, 23 May 2025 21:19:17 +0200 Subject: [PATCH] more function stuff --- src/main.rs | 10 ++++++++++ src/parser.rs | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 8adf430..da467fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ fn log_ast_part(part: &ASTPart, prefix: String) { ASTPart::Number(number) => println!("{}{}: Number: {}", prefix, number.pos, number.value), ASTPart::String(str) => println!("{}{}: String: {}", prefix, str.pos, str.value), ASTPart::Boolean(bool) => println!("{}{}: Boolean: {}", prefix, bool.pos, bool.value), + ASTPart::Null(null) => println!("{}{}: Null", prefix, null.pos), ASTPart::Assigment(assigment) => { println!("{}{}: Assigment: {}", prefix, assigment.pos, assigment.variable); println!("{} Value:", prefix); @@ -37,6 +38,15 @@ fn log_ast_part(part: &ASTPart, prefix: String) { 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) } } diff --git a/src/parser.rs b/src/parser.rs index ae6557d..ee93c38 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -196,6 +196,36 @@ fn shunt(input: Vec) -> ASTPart { } return output[0].clone(); } +fn read_function(input: &Vec, pos: &mut usize, with_args: bool) -> ASTPart { + let mut args: Vec = 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, ends: &Vec, parse_ends: &Vec) -> ASTPart { let mut expressions: Vec = vec![]; while pos < &mut input.len() { @@ -227,7 +257,8 @@ fn read_exp(pos: &mut usize, input: &Vec, ends: &Vec, parse_ends: } else if token.value == "nincs hám" { expressions.push(ASTPart::Null(AstNull { pos: token.pos })); } 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 { panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos); }