From e52176f90c4707cfbebf4921b0518ebef354dea6 Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 13 Aug 2024 08:04:01 -0500 Subject: [PATCH] feat: store tokens in ast for basic nodes --- .gitignore | 1 + src/semantic/types/expression.rs | 10 +++++----- src/syntax/ast/functions.rs | 9 +++++++++ src/syntax/ast/mod.rs | 14 ++++++++++---- src/syntax/parsers/expression/comparison.rs | 14 +++++++------- src/syntax/parsers/expression/equality.rs | 14 +++++++------- src/syntax/parsers/expression/factor.rs | 16 ++++++++-------- src/syntax/parsers/expression/term.rs | 16 ++++++++-------- src/syntax/parsers/expression/unary.rs | 6 +++--- 9 files changed, 58 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index fbe6850..c377752 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target examples tarpaulin-report.html +run.sh diff --git a/src/semantic/types/expression.rs b/src/semantic/types/expression.rs index f9652c6..4daecc0 100644 --- a/src/semantic/types/expression.rs +++ b/src/semantic/types/expression.rs @@ -74,7 +74,7 @@ impl Typed for Expression<'_> { }; // Only supported unary operator: - & ! - if *op == "-" { + if op.value == "-" { if !expr_type.is_value("Int") && !expr_type.is_value("Float") { return Err(MistiError::Semantic(SemanticError { error_start: 0, @@ -87,7 +87,7 @@ impl Typed for Expression<'_> { } else { return Ok(Type::Value("Int".into())); } - } else if *op == "!" { + } else if op.value == "!" { if !expr_type.is_value("Bool") { return Err(MistiError::Semantic(SemanticError { error_start: 0, @@ -99,16 +99,16 @@ impl Typed for Expression<'_> { } } - unreachable!("Illegal state: Found an unexpected unary operator during semantic analysis: {}", *op); + unreachable!("Illegal state: Found an unexpected unary operator during semantic analysis: {}", op.value); } Expression::BinaryOperator(exp1, exp2, operator) => { let t1 = exp1.get_type(scope)?; let t2 = exp2.get_type(scope)?; // TODO: There's definitely a better way to do this - if *operator == "+" && t1.is_value("Int") && t2.is_value("Int") { + if operator.value == "+" && t1.is_value("Int") && t2.is_value("Int") { return Ok(Type::Value("Int".into())); - } else if *operator == "-" && t1.is_value("Int") && t2.is_value("Int") { + } else if operator.value == "-" && t1.is_value("Int") && t2.is_value("Int") { return Ok(Type::Value("Int".into())); } diff --git a/src/syntax/ast/functions.rs b/src/syntax/ast/functions.rs index bfd9c66..5000e85 100644 --- a/src/syntax/ast/functions.rs +++ b/src/syntax/ast/functions.rs @@ -6,6 +6,15 @@ pub struct FunctionCall<'a> { pub arguments: Box>, } +impl Positionable for FunctionCall<'_> { + fn get_position(&self) -> (usize, usize) { + let (start, _) = self.function.get_position(); + let (_, end) = self.arguments.get_position(); + (start, end) + } +} + + #[derive(Debug)] pub struct ArgumentsList<'a> { pub arguments: Vec>, diff --git a/src/syntax/ast/mod.rs b/src/syntax/ast/mod.rs index 40dc241..cd6d921 100644 --- a/src/syntax/ast/mod.rs +++ b/src/syntax/ast/mod.rs @@ -72,8 +72,10 @@ pub enum Expression<'a> { Boolean(&'a Token), Identifier(&'a Token), FunctionCall(FunctionCall<'a>), - UnaryOperator(&'a String, Box>), - BinaryOperator(Box>, Box>, &'a String), + /// operator, right expression + UnaryOperator(&'a Token, Box>), + /// left expression, right expression, operator + BinaryOperator(Box>, Box>, &'a Token), } impl Positionable for Expression<'_> { @@ -86,8 +88,12 @@ impl Positionable for Expression<'_> { Expression::Float(id) => (id.position, id.get_end_position()), Expression::String(id) => (id.position, id.get_end_position()), Expression::Boolean(id) => (id.position, id.get_end_position()), - Expression::FunctionCall(_) => (0, 1), - Expression::UnaryOperator(_, _) => (0, 1), + Expression::FunctionCall(f) => f.get_position(), + Expression::UnaryOperator(operator, exp) => { + let start = operator.position; + let (_, end) = exp.get_position(); + (start, end) + } Expression::BinaryOperator(_, _, _) => (0, 1), } } diff --git a/src/syntax/parsers/expression/comparison.rs b/src/syntax/parsers/expression/comparison.rs index c59c224..315a4b3 100644 --- a/src/syntax/parsers/expression/comparison.rs +++ b/src/syntax/parsers/expression/comparison.rs @@ -37,7 +37,7 @@ fn parse_many<'a>( ) { Ok((expr, next_pos)) => { let expr = - Expression::BinaryOperator(Box::new(prev_expr), Box::new(expr), &token.value); + Expression::BinaryOperator(Box::new(prev_expr), Box::new(expr), &token); parse_many(tokens, next_pos, expr, indentation_level + indent_count) } @@ -67,7 +67,7 @@ mod tests { } _ => panic!("Expected 2 identifiers"), } - assert_eq!(">=", op) + assert_eq!(">=", op.value) } _ => panic!("Expected a binary expression with 2 identifiers"), }, @@ -98,7 +98,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, ">=") + assert_eq!(op.value, ">=") } _ => panic!("Expected a binary operator"), } @@ -112,7 +112,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "<=") + assert_eq!(op.value, "<=") } _ => panic!("Expected a binary operator"), } @@ -128,7 +128,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "<=") + assert_eq!(op.value, "<=") } _ => panic!("Expected a binary operator"), } @@ -143,7 +143,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "<=") + assert_eq!(op.value, "<=") } _ => panic!("Expected a binary operator"), } @@ -158,7 +158,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, ">=") + assert_eq!(op.value, ">=") } _ => panic!("Expected a binary operator"), } diff --git a/src/syntax/parsers/expression/equality.rs b/src/syntax/parsers/expression/equality.rs index 3497d03..f59d93a 100644 --- a/src/syntax/parsers/expression/equality.rs +++ b/src/syntax/parsers/expression/equality.rs @@ -37,7 +37,7 @@ fn parse_many<'a>( ) { Ok((expr, next_pos)) => { let expr = - Expression::BinaryOperator(Box::new(prev_expr), Box::new(expr), &token.value); + Expression::BinaryOperator(Box::new(prev_expr), Box::new(expr), &token); parse_many(tokens, next_pos, expr, indentation_level + indent_count) } @@ -66,7 +66,7 @@ mod tests { } _ => panic!("Expected 2 identifiers"), } - assert_eq!("==", op) + assert_eq!("==", op.value) } _ => panic!("Expected a binary expression with 2 identifiers"), }, @@ -97,7 +97,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "==") + assert_eq!(op.value, "==") } _ => panic!("Expected a binary operator"), } @@ -114,7 +114,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "==") + assert_eq!(op.value, "==") } _ => panic!("Expected a binary operator"), } @@ -130,7 +130,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "==") + assert_eq!(op.value, "==") } _ => panic!("Expected a binary operator"), } @@ -145,7 +145,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "==") + assert_eq!(op.value, "==") } _ => panic!("Expected a binary operator"), } @@ -160,7 +160,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "==") + assert_eq!(op.value, "==") } _ => panic!("Expected a binary operator"), } diff --git a/src/syntax/parsers/expression/factor.rs b/src/syntax/parsers/expression/factor.rs index 589d9b4..5ef6c81 100644 --- a/src/syntax/parsers/expression/factor.rs +++ b/src/syntax/parsers/expression/factor.rs @@ -39,7 +39,7 @@ fn parse_many<'a>( let expr = Expression::BinaryOperator( Box::new(prev_expr), Box::new(expr), - &token.value, + &token, ); parse_many(tokens, next_pos, expr, indentation_level + indent_count) @@ -70,7 +70,7 @@ mod tests { } _ => panic!("Expected 2 identifiers"), } - assert_eq!("*", op) + assert_eq!("*", op.value) } _ => panic!("Expected a binary expression with 2 identifiers"), }, @@ -101,7 +101,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "*") + assert_eq!(op.value, "*") } _ => panic!("Expected a binary operator"), } @@ -118,7 +118,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "*") + assert_eq!(op.value, "*") } _ => panic!("Expected a binary operator"), } @@ -134,7 +134,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "*") + assert_eq!(op.value, "*") } _ => panic!("Expected a binary operator"), } @@ -149,7 +149,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "*") + assert_eq!(op.value, "*") } _ => panic!("Expected a binary operator"), } @@ -164,7 +164,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "/") + assert_eq!(op.value, "/") } _ => panic!("Expected a binary operator"), } @@ -179,7 +179,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "/") + assert_eq!(op.value, "/") } _ => panic!("Expected a binary operator"), } diff --git a/src/syntax/parsers/expression/term.rs b/src/syntax/parsers/expression/term.rs index e91a216..29c5fcc 100644 --- a/src/syntax/parsers/expression/term.rs +++ b/src/syntax/parsers/expression/term.rs @@ -39,7 +39,7 @@ fn parse_many<'a>( let expr = Expression::BinaryOperator( Box::new(prev_expr), Box::new(expr), - &token.value, + &token, ); parse_many(tokens, next_pos, expr, indentation_level + indent_count) @@ -71,7 +71,7 @@ mod tests { } _ => panic!("Expected 2 identifiers"), } - assert_eq!("+", op) + assert_eq!("+", op.value) } _ => panic!("Expected a binary expression with 2 identifiers"), }, @@ -102,7 +102,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "+") + assert_eq!(op.value, "+") } _ => panic!("Expected a binary operator"), } @@ -116,7 +116,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "+") + assert_eq!(op.value, "+") } _ => panic!("Expected a binary operator"), } @@ -132,7 +132,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "+") + assert_eq!(op.value, "+") } _ => panic!("Expected a binary operator"), } @@ -147,7 +147,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "+") + assert_eq!(op.value, "+") } _ => panic!("Expected a binary operator"), } @@ -162,7 +162,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "+") + assert_eq!(op.value, "+") } _ => panic!("Expected a binary operator"), } @@ -177,7 +177,7 @@ mod tests { match result { Expression::BinaryOperator(_, _, op) => { - assert_eq!(op, "+") + assert_eq!(op.value, "+") } _ => panic!("Expected a binary operator"), } diff --git a/src/syntax/parsers/expression/unary.rs b/src/syntax/parsers/expression/unary.rs index 827d681..f48d7c1 100644 --- a/src/syntax/parsers/expression/unary.rs +++ b/src/syntax/parsers/expression/unary.rs @@ -16,7 +16,7 @@ pub fn try_parse(tokens: &Vec, pos: usize) -> ParsingResult { Some(token) if token.value == "!" || token.value == "-" => { match Expression::try_parse(tokens, pos + 1) { Ok((expression, next_pos)) => Ok(( - Expression::UnaryOperator(&token.value, Box::new(expression)), + Expression::UnaryOperator(&token, Box::new(expression)), next_pos, )), _ => Err(ParsingError::Unmatched), @@ -53,7 +53,7 @@ mod tests { Ok((Expression::UnaryOperator(operator, expression), _)) => { match (operator, *expression) { (op, Expression::Int(value)) => { - assert_eq!(*op, "-"); + assert_eq!(op.value, "-"); assert_eq!(value.value, "10"); } _ => panic!("unexpected values"), @@ -70,7 +70,7 @@ mod tests { match expression { Ok((Expression::UnaryOperator(operator, expression), _)) => { - assert_eq!(*operator, "-"); + assert_eq!(operator.value, "-"); match *expression { Expression::BinaryOperator(_, _, _) => { // :D