From 74afdd73db3b97fbf4801bf645a567528a51b36c Mon Sep 17 00:00:00 2001 From: afonya2 Date: Fri, 23 May 2025 23:26:16 +0200 Subject: [PATCH] added functions --- src/main.rs | 1 - src/parser.rs | 38 ++++++++++++++++++++++++++++---------- test.as | 5 ++++- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index da467fa..bdeb4b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { diff --git a/src/parser.rs b/src/parser.rs index ee93c38..742e410 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -67,7 +67,6 @@ pub struct AstVarUpdate { } #[derive(Debug, Clone, PartialEq)] pub struct AstFunction { - pub name: String, pub args: Vec, pub body: Vec, pub pos: usize @@ -140,6 +139,9 @@ fn shunt(input: Vec) -> 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 { } fn read_function(input: &Vec, pos: &mut usize, with_args: bool) -> ASTPart { let mut args: Vec = 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, 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 = 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 = 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, ends: &Vec, parse_ends: &Vec) -> ASTPart { let mut expressions: Vec = vec![]; @@ -258,7 +277,7 @@ fn read_exp(pos: &mut usize, input: &Vec, ends: &Vec, 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, op_ends: &Vec, par } } -fn parse_internal(input: Vec, op_ends: Vec, parse_ends: Vec) -> Vec { +fn parse_internal(input: &Vec, op_ends: Vec, parse_ends: Vec, pos: &mut usize) -> Vec { let mut out: Vec = 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) -> Vec { let parse_ends: Vec = 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; } \ No newline at end of file diff --git a/test.as b/test.as index 7cdb8df..820f28d 100644 --- a/test.as +++ b/test.as @@ -1,4 +1,7 @@ gethelj a = 1 gethelj b = (a + 2) * 3 ugass(a,b) -gethelj c = piszv & nem piszv \ No newline at end of file +gethelj c = piszv & nem piszv +gethelj d = lőcsve(e,f) { + ugass(a,b) +} \ No newline at end of file