Remove standalone finalize statement from the AST

This commit is contained in:
d0cd 2022-10-14 21:34:56 -04:00
parent 232cbfb6bb
commit 060f57d971
6 changed files with 13 additions and 79 deletions

View File

@ -77,7 +77,6 @@ pub trait StatementConsumer {
Statement::Decrement(stmt) => self.consume_decrement(stmt),
Statement::Definition(stmt) => self.consume_definition(stmt),
Statement::Expression(stmt) => self.consume_expression_statement(stmt),
Statement::Finalize(stmt) => self.consume_finalize(stmt),
Statement::Increment(stmt) => self.consume_increment(stmt),
Statement::Iteration(stmt) => self.consume_iteration(*stmt),
Statement::Return(stmt) => self.consume_return(stmt),
@ -98,8 +97,6 @@ pub trait StatementConsumer {
fn consume_expression_statement(&mut self, input: ExpressionStatement) -> Self::Output;
fn consume_finalize(&mut self, input: FinalizeStatement) -> Self::Output;
fn consume_increment(&mut self, input: IncrementStatement) -> Self::Output;
fn consume_iteration(&mut self, input: IterationStatement) -> Self::Output;

View File

@ -171,7 +171,6 @@ pub trait StatementReconstructor: ExpressionReconstructor {
Statement::Decrement(stmt) => self.reconstruct_decrement(stmt),
Statement::Definition(stmt) => self.reconstruct_definition(stmt),
Statement::Expression(stmt) => self.reconstruct_expression_statement(stmt),
Statement::Finalize(stmt) => self.reconstruct_finalize(stmt),
Statement::Increment(stmt) => self.reconstruct_increment(stmt),
Statement::Iteration(stmt) => self.reconstruct_iteration(*stmt),
Statement::Return(stmt) => self.reconstruct_return(stmt),
@ -270,20 +269,6 @@ pub trait StatementReconstructor: ExpressionReconstructor {
)
}
fn reconstruct_finalize(&mut self, input: FinalizeStatement) -> (Statement, Self::AdditionalOutput) {
(
Statement::Finalize(FinalizeStatement {
arguments: input
.arguments
.into_iter()
.map(|arg| self.reconstruct_expression(arg).0)
.collect(),
span: input.span,
}),
Default::default(),
)
}
fn reconstruct_increment(&mut self, input: IncrementStatement) -> (Statement, Self::AdditionalOutput) {
(
Statement::Increment(IncrementStatement {
@ -317,6 +302,12 @@ pub trait StatementReconstructor: ExpressionReconstructor {
(
Statement::Return(ReturnStatement {
expression: self.reconstruct_expression(input.expression).0,
finalize_args: input.finalize_args.map(|arguments| {
arguments
.into_iter()
.map(|argument| self.reconstruct_expression(argument).0)
.collect()
}),
span: input.span,
}),
Default::default(),

View File

@ -124,7 +124,6 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> {
Statement::Decrement(stmt) => self.visit_decrement(stmt),
Statement::Definition(stmt) => self.visit_definition(stmt),
Statement::Expression(stmt) => self.visit_expression_statement(stmt),
Statement::Finalize(stmt) => self.visit_finalize(stmt),
Statement::Increment(stmt) => self.visit_increment(stmt),
Statement::Iteration(stmt) => self.visit_iteration(stmt),
Statement::Return(stmt) => self.visit_return(stmt),
@ -177,12 +176,6 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> {
self.visit_expression(&input.expression, &Default::default());
}
fn visit_finalize(&mut self, input: &'a FinalizeStatement) {
input.arguments.iter().for_each(|expr| {
self.visit_expression(expr, &Default::default());
});
}
fn visit_increment(&mut self, input: &'a IncrementStatement) {
self.visit_expression(&input.amount, &Default::default());
self.visit_expression(&input.index, &Default::default());
@ -197,6 +190,11 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> {
fn visit_return(&mut self, input: &'a ReturnStatement) {
self.visit_expression(&input.expression, &Default::default());
if let Some(arguments) = &input.finalize_args {
arguments.iter().for_each(|argument| {
self.visit_expression(argument, &Default::default());
})
}
}
}

View File

@ -1,46 +0,0 @@
// Copyright (C) 2019-2022 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{Expression, Node};
use leo_span::Span;
use core::fmt;
use serde::{Deserialize, Serialize};
/// A return statement `finalize(arg1, ..., argN);`.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct FinalizeStatement {
/// The arguments to pass to the finalize block.
pub arguments: Vec<Expression>,
/// The span of `finalize(arg1, ..., argN)` excluding the semicolon.
pub span: Span,
}
impl fmt::Display for FinalizeStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "finalize(")?;
for (i, param) in self.arguments.iter().enumerate() {
write!(f, "{param}")?;
if i < self.arguments.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, ");")
}
}
crate::simple_node_impl!(FinalizeStatement);

View File

@ -35,9 +35,6 @@ pub use definition::*;
pub mod expression;
pub use expression::*;
pub mod finalize;
pub use finalize::*;
pub mod increment;
pub use increment::*;
@ -71,8 +68,6 @@ pub enum Statement {
Definition(DefinitionStatement),
/// An expression statement
Expression(ExpressionStatement),
/// A finalize statement.
Finalize(FinalizeStatement),
/// An increment statement.
Increment(IncrementStatement),
/// A `for` statement.
@ -101,7 +96,6 @@ impl fmt::Display for Statement {
Statement::Decrement(x) => x.fmt(f),
Statement::Definition(x) => x.fmt(f),
Statement::Expression(x) => x.fmt(f),
Statement::Finalize(x) => x.fmt(f),
Statement::Increment(x) => x.fmt(f),
Statement::Iteration(x) => x.fmt(f),
Statement::Return(x) => x.fmt(f),
@ -120,7 +114,6 @@ impl Node for Statement {
Decrement(n) => n.span(),
Definition(n) => n.span(),
Expression(n) => n.span(),
Finalize(n) => n.span(),
Increment(n) => n.span(),
Iteration(n) => n.span(),
Return(n) => n.span(),
@ -137,7 +130,6 @@ impl Node for Statement {
Decrement(n) => n.set_span(span),
Definition(n) => n.set_span(span),
Expression(n) => n.set_span(span),
Finalize(n) => n.set_span(span),
Increment(n) => n.set_span(span),
Iteration(n) => n.set_span(span),
Return(n) => n.set_span(span),

View File

@ -25,6 +25,8 @@ use std::fmt;
pub struct ReturnStatement {
/// The expression to return to the function caller.
pub expression: Expression,
/// Arguments to the finalize block.
pub finalize_args: Option<Vec<Expression>>,
/// The span of `return expression` excluding the semicolon.
pub span: Span,
}