From 28c440539d06679defca18f4bdcfadc423f19084 Mon Sep 17 00:00:00 2001 From: afonya2 Date: Sat, 17 May 2025 19:52:46 +0200 Subject: [PATCH] added parenthesis --- src/parser.rs | 60 ++++++++++++++++++++++++++++++++++++++------------- test.as | 2 +- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 7b3ce17..0ec3036 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,6 +1,8 @@ +use core::panic; + use crate::lexer::{Token, TokenType}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum ASTPart { String(AstString), Number(AstNumber), @@ -10,35 +12,35 @@ pub enum ASTPart { Call(AstCall), NOOP } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct AstString { pub value: String, pub pos: usize } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct AstNumber { pub value: i64, pub pos: usize } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct AstAssigment { pub variable: String, pub value: Box, pub pos: usize } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct AstOperation { pub operator: String, pub left: Box, pub right: Box, pub pos: usize } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct AstVarRead { pub variable: String, pub pos: usize } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct AstCall { pub function: Box, pub args: Vec, @@ -106,6 +108,10 @@ fn shunt(input: Vec) -> ASTPart { output.push(part); }, ASTPart::Operation(op) => { + if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP { + output.push(part); + break; + } while stack.len() > 0 { let top = &stack[stack.len()-1]; match top { @@ -127,24 +133,32 @@ fn shunt(input: Vec) -> ASTPart { while stack.len() > 0 { output.push(stack.pop().unwrap()); } - for i in 0..output.len() { + let mut i = 0; + while i < output.len() { match &output[i] { ASTPart::Operation(op) => { + if *op.left != ASTPart::NOOP && *op.right != ASTPart::NOOP { + i += 1; + continue; + } if i < 2 { panic!("Unexpected operation at {}", op.pos); } - let left = &output[i-2]; - let right = &output[i-1]; + let left = output[i-2].clone(); + let right = output[i-1].clone(); output[i] = ASTPart::Operation(AstOperation { operator: op.operator.clone(), - left: Box::new(left.clone()), - right: Box::new(right.clone()), - pos: op.pos + left: Box::new(left), + right: Box::new(right), + pos: op.pos, }); output.remove(i-2); output.remove(i-2); - }, - _ => {} + i -= 1; + } + _ => { + i += 1; + } } } return output[0].clone(); @@ -181,6 +195,22 @@ fn read_exp(pos: &mut usize, input: &Vec, ends: &Vec, parse_ends: } } else if token.typ == TokenType::OPERATOR { expressions.push(ASTPart::Operation(AstOperation { operator: token.value.clone(), left: Box::new(ASTPart::NOOP), right: Box::new(ASTPart::NOOP), pos: token.pos })); + } else if token.typ == TokenType::SEPARATOR { + //We check for () and then send the into read_exp again, so we recursively parse the expression + if token.value == "(" { + let ends: Vec = vec![ + Token { typ: TokenType::SEPARATOR, value: String::from(")"), pos: 0 } + ]; + let exp = read_exp(pos, input, &ends, &ends); + if input[*pos].typ == TokenType::SEPARATOR && input[*pos].value == ")" { + *pos += 1; + } else { + panic!("Unclosed parenthesis at {}", token.pos); + } + expressions.push(exp); + } else { + panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos); + } } else { panic!("Unexpected {:?}({}) at {}", token.typ, token.value, token.pos); } diff --git a/test.as b/test.as index d41d1d4..b0e1814 100644 --- a/test.as +++ b/test.as @@ -1,3 +1,3 @@ gethelj a = 1 -gethelj b = a + (2+2) +gethelj b = a + 2 gethelj c = ugass(a,b) \ No newline at end of file