diff --git a/src/expression_evaluator.rs b/src/expression_evaluator.rs index cba3d3b..47a2f7f 100644 --- a/src/expression_evaluator.rs +++ b/src/expression_evaluator.rs @@ -105,11 +105,11 @@ impl<'a> Evaluate for FilteredExpression<'a> { pub enum Expression<'a> { Constant(Value), - BinaryExpression(BinaryOperation, Box>, Box>), - UnaryExpression(UnaryOperation, Box>), - SubscriptExpression(SubscriptExpression<'a>), + Binary(BinaryOperation, Box>, Box>), + Unary(UnaryOperation, Box>), + Subscript(SubscriptExpression<'a>), ValueRef(ValueRefExpression), - FilteredExpression(FilteredExpression<'a>), + Filtered(FilteredExpression<'a>), Tuple(TupleExpression<'a>), Dict(DictionaryExpression<'a>), } @@ -151,12 +151,12 @@ impl<'a> Evaluate for Expression<'a> { fn evaluate(&self, values: Context<'_>) -> Result { let result = match &self { Expression::Constant(value) => value.clone(), - Expression::BinaryExpression(op, left, right) => { + Expression::Binary(op, left, right) => { let left_val = left.evaluate(values.clone())?; let right_val = right.evaluate(values)?; visitors::BinaryMathOperation::apply(op, left_val, right_val) } - Expression::UnaryExpression(op, expr) => { + Expression::Unary(op, expr) => { let expression = expr.evaluate(values)?; match op { UnaryOperation::Plus => expression, @@ -164,11 +164,11 @@ impl<'a> Evaluate for Expression<'a> { UnaryOperation::LogicalNot => !expression, } } - Expression::SubscriptExpression(sub) => sub.evaluate(values)?, + Expression::Subscript(sub) => sub.evaluate(values)?, Expression::ValueRef(identifier) => identifier.evaluate(values)?, Expression::Tuple(tuple) => tuple.evaluate(values)?, Expression::Dict(dict) => dict.evaluate(values)?, - Expression::FilteredExpression(filter) => filter.evaluate(values)?, + Expression::Filtered(filter) => filter.evaluate(values)?, }; Ok(result) } diff --git a/src/expression_parser.rs b/src/expression_parser.rs index b60af93..03ccf3f 100644 --- a/src/expression_parser.rs +++ b/src/expression_parser.rs @@ -46,7 +46,7 @@ impl ExpressionParser { if let Some(Ok(Token::LogicalOr)) = lexer.peek() { lexer.next(); let right = ExpressionParser::parse_logical_or(lexer)?; - return Ok(Expression::BinaryExpression( + return Ok(Expression::Binary( BinaryOperation::LogicalOr, Box::new(left), Box::new(right), @@ -60,7 +60,7 @@ impl ExpressionParser { if let Some(Ok(Token::LogicalAnd)) = lexer.peek() { lexer.next(); let right = ExpressionParser::parse_logical_and(lexer)?; - return Ok(Expression::BinaryExpression( + return Ok(Expression::Binary( BinaryOperation::LogicalAnd, Box::new(left), Box::new(right), @@ -87,7 +87,7 @@ impl ExpressionParser { lexer.next(); let right = ExpressionParser::parse_string_concat(lexer)?; - Ok(Expression::BinaryExpression( + Ok(Expression::Binary( binary_op, Box::new(left), Box::new(right), @@ -99,7 +99,7 @@ impl ExpressionParser { if let Some(Ok(Token::Tilde)) = lexer.peek() { lexer.next(); let right = ExpressionParser::parse_logical_and(lexer)?; - return Ok(Expression::BinaryExpression( + return Ok(Expression::Binary( BinaryOperation::StringConcat, Box::new(left), Box::new(right), @@ -113,7 +113,7 @@ impl ExpressionParser { if let Some(Ok(Token::MulMul)) = lexer.peek() { lexer.next(); let right = ExpressionParser::parse_math_pow(lexer)?; - return Ok(Expression::BinaryExpression( + return Ok(Expression::Binary( BinaryOperation::Pow, Box::new(left), Box::new(right), @@ -133,7 +133,7 @@ impl ExpressionParser { }; lexer.next(); let right = ExpressionParser::parse_math_plus_minus(lexer)?; - Ok(Expression::BinaryExpression( + Ok(Expression::Binary( binary_op, Box::new(left), Box::new(right), @@ -152,7 +152,7 @@ impl ExpressionParser { lexer.next(); let right = ExpressionParser::parse_math_mul_div(lexer)?; - Ok(Expression::BinaryExpression( + Ok(Expression::Binary( binary_op, Box::new(left), Box::new(right), @@ -175,14 +175,14 @@ impl ExpressionParser { let sub_expr = ExpressionParser::parse_value_expression(lexer)?; let result = match unary_op { - Some(op) => Expression::UnaryExpression(op, Box::new(sub_expr)), + Some(op) => Expression::Unary(op, Box::new(sub_expr)), None => sub_expr, }; if let Some(Ok(Token::Pipe)) = lexer.peek() { lexer.next(); let filter_expression = ExpressionParser::parse_filter_expression(lexer)?; - Ok(Expression::FilteredExpression(FilteredExpression::new( + Ok(Expression::Filtered(FilteredExpression::new( Box::new(result), filter_expression, ))) @@ -398,7 +398,7 @@ impl ExpressionParser { }; } - Ok(Expression::SubscriptExpression(subscript)) + Ok(Expression::Subscript(subscript)) } fn parse_tuple<'a>(lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result> { let mut tuple = TupleExpression::default(); diff --git a/src/statement/mod.rs b/src/statement/mod.rs index de95255..3bc021d 100644 --- a/src/statement/mod.rs +++ b/src/statement/mod.rs @@ -1,3 +1,6 @@ +use std::io::Write; +use std::rc::Rc; + use crate::context::Context; use crate::error::Result; use crate::expression_evaluator::Evaluate; @@ -5,12 +8,11 @@ use crate::lexer::Token; use crate::renderer::ComposedRenderer; use crate::renderer::Render; use crate::value::{Value, ValuesList, ValuesMap}; -use std::io::Write; -use std::sync::Arc; + pub mod parser; pub struct IfStatement<'a> { expression: Box, - body: Option>>, + body: Option>>, else_branches: Vec>, } impl<'a> IfStatement<'a> { @@ -21,7 +23,7 @@ impl<'a> IfStatement<'a> { else_branches: vec![], } } - fn set_main_body(&mut self, body: Arc>) { + fn set_main_body(&mut self, body: Rc>) { let if_body = body.clone(); self.body = Some(if_body); } @@ -52,7 +54,7 @@ impl<'a> Render for IfStatement<'a> { pub struct ElseStatement<'a> { expression: Option>, - body: Option>>, + body: Option>>, } impl<'a> ElseStatement<'a> { @@ -62,7 +64,7 @@ impl<'a> ElseStatement<'a> { body: None, } } - fn set_main_body(&mut self, body: Arc>) { + fn set_main_body(&mut self, body: Rc>) { let else_body = body.clone(); self.body = Some(else_body); } @@ -82,7 +84,7 @@ impl<'a> Render for ElseStatement<'a> { } pub struct WithStatement<'a> { scope_vars: Vec<(String, Box)>, - body: Option>>, + body: Option>>, } impl<'a> WithStatement<'a> { pub fn new(scope_vars: Vec<(String, Box)>) -> Self { @@ -91,7 +93,7 @@ impl<'a> WithStatement<'a> { body: None, } } - fn set_main_body(&mut self, body: Arc>) { + fn set_main_body(&mut self, body: Rc>) { let with_body = body.clone(); self.body = Some(with_body); } @@ -111,7 +113,7 @@ impl<'a> Render for WithStatement<'a> { pub struct ForStatement<'a> { vars: Vec, value: Box, - body: Option>>, + body: Option>>, } impl<'a> ForStatement<'a> { @@ -122,7 +124,7 @@ impl<'a> ForStatement<'a> { body: None, } } - fn set_main_body(&mut self, body: Arc>) { + fn set_main_body(&mut self, body: Rc>) { let for_body = body.clone(); self.body = Some(for_body); } @@ -224,7 +226,7 @@ pub enum Statement<'a> { Include(IncludeStatement<'a>), } impl<'a> Statement<'a> { - pub fn set_main_body(&mut self, body: Arc>) { + pub fn set_main_body(&mut self, body: Rc>) { match self { Statement::If(statement) => statement.set_main_body(body), Statement::Else(statement) => statement.set_main_body(body), @@ -255,8 +257,8 @@ impl<'a> Render for Statement<'a> { pub struct StatementInfo<'a> { mode: StatementInfoType, - pub current_composition: Arc>, - compositions: Vec>>, + pub current_composition: Rc>, + compositions: Vec>>, _token: Option>, renderer: Option>, } @@ -273,7 +275,7 @@ impl<'a> StatementInfo<'a> { pub fn new( mode: StatementInfoType, _token: Option>, - renderers: Arc>, + renderers: Rc>, ) -> Self { let current_composition = renderers.clone(); let compositions = vec![renderers]; diff --git a/src/statement/parser.rs b/src/statement/parser.rs index d7dee73..6bae8ec 100644 --- a/src/statement/parser.rs +++ b/src/statement/parser.rs @@ -1,16 +1,20 @@ -use super::{ - ElseStatement, ForStatement, IfStatement, IncludeStatement, Statement, StatementInfo, - StatementInfoList, StatementInfoType, WithStatement, -}; +use std::rc::Rc; + +use logos::{Lexer, Logos}; + use crate::error::{Error, ParseError, ParseErrorKind, Result}; use crate::expression_parser::ExpressionParser; use crate::lexer::{PeekableLexer, Token}; use crate::renderer::ComposedRenderer; use crate::source::SourceLocationInfo; use crate::statement::Evaluate; -use logos::{Lexer, Logos}; + +use super::{ + ElseStatement, ForStatement, IfStatement, IncludeStatement, Statement, StatementInfo, + StatementInfoList, StatementInfoType, WithStatement, +}; + pub struct StatementParser; -use std::sync::Arc; impl StatementParser { pub fn parse<'a>(text: &'a str, statementinfo_list: &mut StatementInfoList<'a>) -> Result<()> { @@ -52,7 +56,7 @@ impl StatementParser { statementinfo_list: &mut StatementInfoList<'a>, ) -> Result<()> { let value = ExpressionParser::full_expresion_parser(lexer)?; - let composed_renderer = Arc::new(ComposedRenderer::new()); + let composed_renderer = Rc::new(ComposedRenderer::new()); let renderer = Statement::If(IfStatement::new(Box::new(value))); let mut statement_info = StatementInfo::new( StatementInfoType::IfStatement, @@ -69,7 +73,7 @@ impl StatementParser { statementinfo_list: &mut StatementInfoList<'a>, ) -> Result<()> { let value = ExpressionParser::full_expresion_parser(lexer)?; - let composed_renderer = Arc::new(ComposedRenderer::new()); + let composed_renderer = Rc::new(ComposedRenderer::new()); let renderer = Statement::Else(ElseStatement::new(Some(Box::new(value)))); let mut statement_info = StatementInfo::new( StatementInfoType::ElseIfStatement, @@ -82,7 +86,7 @@ impl StatementParser { } fn parse_else(statementinfo_list: &mut StatementInfoList<'_>) { - let composed_renderer = Arc::new(ComposedRenderer::new()); + let composed_renderer = Rc::new(ComposedRenderer::new()); let renderer = Statement::Else(ElseStatement::new(None)); let mut statement_info = StatementInfo::new( StatementInfoType::ElseIfStatement, @@ -161,7 +165,7 @@ impl StatementParser { Some(SourceLocationInfo::new_with_range(range.start, range.end)), ))) } else { - let composed_renderer = Arc::new(ComposedRenderer::new()); + let composed_renderer = Rc::new(ComposedRenderer::new()); let renderer = Statement::For(ForStatement::new(vars, Box::new(expression))); let mut statement_info = StatementInfo::new( StatementInfoType::ForStatement, @@ -244,7 +248,7 @@ impl StatementParser { Some(SourceLocationInfo::new_with_range(range.start, range.end)), ))); } - let composed_renderer = Arc::new(ComposedRenderer::new()); + let composed_renderer = Rc::new(ComposedRenderer::new()); let renderer = Statement::With(WithStatement::new(vars)); let mut statement_info = StatementInfo::new( StatementInfoType::WithStatement, diff --git a/src/template_parser.rs b/src/template_parser.rs index 7d5f415..b031fa4 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -1,3 +1,6 @@ +use std::rc::Rc; +use std::sync::RwLock; + use crate::error::{Error, ParseError, ParseErrorKind, Result}; use crate::expression_parser::ExpressionParser; use crate::keyword::{RegexEnum, ROUGH_TOKENIZER}; @@ -8,7 +11,6 @@ use crate::statement::parser::StatementParser; use crate::statement::{StatementInfo, StatementInfoList, StatementInfoType}; use crate::template_env::TemplateEnv; use regex::Regex; -use std::sync::{Arc, RwLock}; pub struct TemplateParser<'a> { template_body: &'a str, @@ -70,7 +72,7 @@ impl<'a> TemplateParser<'a> { statement } } - fn fine_parsing(&self, renderer: Arc>) -> Result<()> { + fn fine_parsing(&self, renderer: Rc>) -> Result<()> { let mut statements_stack: StatementInfoList<'_> = vec![]; let root = StatementInfo::new(StatementInfoType::TemplateRoot, None, renderer); statements_stack.push(root); @@ -109,9 +111,9 @@ impl<'a> TemplateParser<'a> { pub fn parse(&mut self) -> Result> { match self.rough_parsing() { Ok(_) => { - let renderer = Arc::new(ComposedRenderer::new()); + let renderer = Rc::new(ComposedRenderer::new()); self.fine_parsing(renderer.clone())?; - Ok(Arc::try_unwrap(renderer).unwrap()) + Ok(Rc::try_unwrap(renderer).unwrap()) } Err(error) => Err(error), } diff --git a/src/value/ops.rs b/src/value/ops.rs index ac1a7d8..7f3fb04 100644 --- a/src/value/ops.rs +++ b/src/value/ops.rs @@ -139,13 +139,7 @@ impl Value { } impl PartialOrd for Value { fn partial_cmp(&self, other: &Self) -> Option { - match (self, other) { - (Value::Integer(left), Value::Integer(right)) => left.partial_cmp(right), - (Value::Integer(left), Value::Double(right)) => (*left as f64).partial_cmp(right), - (Value::Double(left), Value::Integer(right)) => left.partial_cmp(&(*right as f64)), - (Value::Double(left), Value::Double(right)) => left.partial_cmp(right), - _ => todo!(), - } + Some(self.cmp(other)) } }