From 3c1a1489af209a216e9178e7c077413f84059dd7 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Sat, 4 Sep 2021 23:15:01 +0200 Subject: parser: removed Boxes --- src/parser.rs | 82 ++++++++++++++++++++++++++--------------------------------- 1 file changed, 36 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/parser.rs b/src/parser.rs index f18d49e..76f31e4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -12,7 +12,7 @@ pub enum ExprAST { Binary(char, Box, Box), /// Call - Expression class for function calls. - Call(String, Vec>), + Call(String, Vec), } /// PrototypeAST - This class represents the "prototype" for a function, @@ -23,7 +23,7 @@ pub struct PrototypeAST(String, Vec); /// FunctionAST - This class represents a function definition itself. #[derive(Debug)] -pub struct FunctionAST(Box, Box); +pub struct FunctionAST(PrototypeAST, ExprAST); /// Parse result with String as Error type (to be compliant with tutorial). type ParseResult = Result; @@ -73,12 +73,12 @@ where /// numberexpr ::= number /// /// Implement `std::unique_ptr ParseNumberExpr();` from the tutorial. - fn parse_num_expr(&mut self) -> ParseResult> { + fn parse_num_expr(&mut self) -> ParseResult { match *self.cur_tok() { Token::Number(num) => { // Consume the number token. self.get_next_token(); - Ok(Box::new(ExprAST::Number(num))) + Ok(ExprAST::Number(num)) } _ => unreachable!(), } @@ -87,7 +87,7 @@ where /// parenexpr ::= '(' expression ')' /// /// Implement `std::unique_ptr ParseParenExpr();` from the tutorial. - fn parse_paren_expr(&mut self) -> ParseResult> { + fn parse_paren_expr(&mut self) -> ParseResult { // Eat '(' token. assert_eq!(*self.cur_tok(), Token::Char('(')); self.get_next_token(); @@ -108,7 +108,7 @@ where /// ::= identifier '(' expression* ')' /// /// Implement `std::unique_ptr ParseIdentifierExpr();` from the tutorial. - fn parse_identifier_expr(&mut self) -> ParseResult> { + fn parse_identifier_expr(&mut self) -> ParseResult { let id_name = match self.cur_tok.take() { Some(Token::Identifier(id)) => { // Consume identifier. @@ -120,14 +120,14 @@ where if *self.cur_tok() != Token::Char('(') { // Simple variable reference. - Ok(Box::new(ExprAST::Variable(id_name))) + Ok(ExprAST::Variable(id_name)) } else { // Call. // Eat '(' token. self.get_next_token(); - let mut args: Vec> = Vec::new(); + let mut args: Vec = Vec::new(); // If there are arguments collect them. if *self.cur_tok() != Token::Char(')') { @@ -149,7 +149,7 @@ where } } - Ok(Box::new(ExprAST::Call(id_name, args))) + Ok(ExprAST::Call(id_name, args)) } } @@ -159,7 +159,7 @@ where /// ::= parenexpr /// /// Implement `std::unique_ptr ParsePrimary();` from the tutorial. - fn parse_primary(&mut self) -> ParseResult> { + fn parse_primary(&mut self) -> ParseResult { match *self.cur_tok() { Token::Identifier(_) => self.parse_identifier_expr(), Token::Number(_) => self.parse_num_expr(), @@ -176,7 +176,7 @@ where /// ::= primary binoprhs /// /// Implement `std::unique_ptr ParseExpression();` from the tutorial. - fn parse_expression(&mut self) -> ParseResult> { + fn parse_expression(&mut self) -> ParseResult { let lhs = self.parse_primary()?; self.parse_bin_op_rhs(0, lhs) } @@ -185,11 +185,7 @@ where /// ::= ('+' primary)* /// /// Implement `std::unique_ptr ParseBinOpRHS(int ExprPrec, std::unique_ptr LHS);` from the tutorial. - fn parse_bin_op_rhs( - &mut self, - expr_prec: isize, - mut lhs: Box, - ) -> ParseResult> { + fn parse_bin_op_rhs(&mut self, expr_prec: isize, mut lhs: ExprAST) -> ParseResult { loop { let tok_prec = get_tok_precedence(self.cur_tok()); @@ -226,7 +222,7 @@ where rhs = self.parse_bin_op_rhs(tok_prec + 1, rhs)? } - lhs = Box::new(ExprAST::Binary(binop, lhs, rhs)); + lhs = ExprAST::Binary(binop, Box::new(lhs), Box::new(rhs)); } } @@ -238,7 +234,7 @@ where /// ::= id '(' id* ')' /// /// Implement `std::unique_ptr ParsePrototype();` from the tutorial. - fn parse_prototype(&mut self) -> ParseResult> { + fn parse_prototype(&mut self) -> ParseResult { let id_name = match self.cur_tok.take() { Some(Token::Identifier(id)) => { // Consume the identifier. @@ -276,13 +272,13 @@ where // Consume ')'. self.get_next_token(); - Ok(Box::new(PrototypeAST(id_name, args))) + Ok(PrototypeAST(id_name, args)) } /// definition ::= 'def' prototype expression /// /// Implement `std::unique_ptr ParseDefinition();` from the tutorial. - pub fn parse_definition(&mut self) -> ParseResult> { + pub fn parse_definition(&mut self) -> ParseResult { // Consume 'def' token. assert_eq!(*self.cur_tok(), Token::Def); self.get_next_token(); @@ -290,13 +286,13 @@ where let proto = self.parse_prototype()?; let expr = self.parse_expression()?; - Ok(Box::new(FunctionAST(proto, expr))) + Ok(FunctionAST(proto, expr)) } /// external ::= 'extern' prototype /// /// Implement `std::unique_ptr ParseExtern();` from the tutorial. - pub fn parse_extern(&mut self) -> ParseResult> { + pub fn parse_extern(&mut self) -> ParseResult { // Consume 'extern' token. assert_eq!(*self.cur_tok(), Token::Extern); self.get_next_token(); @@ -307,10 +303,10 @@ where /// toplevelexpr ::= expression /// /// Implement `std::unique_ptr ParseTopLevelExpr();` from the tutorial. - pub fn parse_top_level_expr(&mut self) -> ParseResult> { + pub fn parse_top_level_expr(&mut self) -> ParseResult { let e = self.parse_expression()?; - let proto = Box::new(PrototypeAST("".into(), Vec::new())); - Ok(Box::new(FunctionAST(proto, e))) + let proto = PrototypeAST("".into(), Vec::new()); + Ok(FunctionAST(proto, e)) } } @@ -346,7 +342,7 @@ mod test { fn parse_number() { let mut p = parser("13.37"); - assert_eq!(p.parse_num_expr(), Ok(Box::new(ExprAST::Number(13.37f64)))); + assert_eq!(p.parse_num_expr(), Ok(ExprAST::Number(13.37f64))); } #[test] @@ -355,7 +351,7 @@ mod test { assert_eq!( p.parse_identifier_expr(), - Ok(Box::new(ExprAST::Variable("foop".into()))) + Ok(ExprAST::Variable("foop".into())) ); } @@ -363,19 +359,13 @@ mod test { fn parse_primary() { let mut p = parser("1337 foop \n bla(123)"); - assert_eq!(p.parse_primary(), Ok(Box::new(ExprAST::Number(1337f64)))); + assert_eq!(p.parse_primary(), Ok(ExprAST::Number(1337f64))); - assert_eq!( - p.parse_primary(), - Ok(Box::new(ExprAST::Variable("foop".into()))) - ); + assert_eq!(p.parse_primary(), Ok(ExprAST::Variable("foop".into()))); assert_eq!( p.parse_primary(), - Ok(Box::new(ExprAST::Call( - "bla".into(), - vec![Box::new(ExprAST::Number(123f64))] - ))) + Ok(ExprAST::Call("bla".into(), vec![ExprAST::Number(123f64)])) ); } @@ -390,17 +380,17 @@ mod test { // a b let mut p = parser("a + b - c"); - let binexpr_ab = Box::new(ExprAST::Binary( + let binexpr_ab = ExprAST::Binary( '+', Box::new(ExprAST::Variable("a".into())), Box::new(ExprAST::Variable("b".into())), - )); + ); - let binexpr_abc = Box::new(ExprAST::Binary( + let binexpr_abc = ExprAST::Binary( '-', - binexpr_ab, + Box::new(binexpr_ab), Box::new(ExprAST::Variable("c".into())), - )); + ); assert_eq!(p.parse_expression(), Ok(binexpr_abc)); } @@ -416,17 +406,17 @@ mod test { // b c let mut p = parser("a + b * c"); - let binexpr_bc = Box::new(ExprAST::Binary( + let binexpr_bc = ExprAST::Binary( '*', Box::new(ExprAST::Variable("b".into())), Box::new(ExprAST::Variable("c".into())), - )); + ); - let binexpr_abc = Box::new(ExprAST::Binary( + let binexpr_abc = ExprAST::Binary( '+', Box::new(ExprAST::Variable("a".into())), - binexpr_bc, - )); + Box::new(binexpr_bc), + ); assert_eq!(p.parse_expression(), Ok(binexpr_abc)); } -- cgit v1.2.3