From ac4cd71800d8b7183891da56363c89cab931c302 Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu Date: Thu, 4 Aug 2022 16:34:53 -0700 Subject: [PATCH] Reorganize ast/functions folder --- compiler/ast/README.md | 4 +- compiler/ast/src/functions/annotation.rs | 17 +++- compiler/ast/src/functions/function.rs | 87 ------------------- .../functions/{input => }/function_input.rs | 0 compiler/ast/src/functions/input/mod.rs | 21 ----- .../functions/{input => }/input_variable.rs | 0 compiler/ast/src/functions/mod.rs | 80 ++++++++++++++++- 7 files changed, 92 insertions(+), 117 deletions(-) delete mode 100644 compiler/ast/src/functions/function.rs rename compiler/ast/src/functions/{input => }/function_input.rs (100%) delete mode 100644 compiler/ast/src/functions/input/mod.rs rename compiler/ast/src/functions/{input => }/input_variable.rs (100%) diff --git a/compiler/ast/README.md b/compiler/ast/README.md index 1b391cdb07..0cb2f0f03d 100644 --- a/compiler/ast/README.md +++ b/compiler/ast/README.md @@ -33,13 +33,13 @@ Contains the Circuit's name, as well as its members. The members are a function, or a variable, or a constant. For all of them the Circuit preserves their names. -#### [Decorators](./src/annotation.rs) +#### [Decorators](./src/functions/annotation.rs) An annotation node is a decorator that can be applied to a function. Stored on the function themselves despite being a top-level node. The node stores the name of the annotation, as well as any args passed to it. -#### [Functions](./src/functions/function.rs) +#### [Functions](./src/functions/mod.rs) A function node represents a defined function in a Leo Program. An order-preserving map of these are stored on the Program. diff --git a/compiler/ast/src/functions/annotation.rs b/compiler/ast/src/functions/annotation.rs index fc0fd12d8d..acba817384 100644 --- a/compiler/ast/src/functions/annotation.rs +++ b/compiler/ast/src/functions/annotation.rs @@ -14,16 +14,27 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::Identifier; +use crate::{simple_node_impl, Identifier, Node}; use leo_span::Span; + use serde::{Deserialize, Serialize}; +use std::fmt; /// An annotation, e.g. @program. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct Annotation { + // TODO: Consider using a symbol instead of an identifier. /// The name of the annotation. - pub name: Identifier, - /// The span associated with the annotation. + pub identifier: Identifier, + /// A span locating where the annotation occurred in the source. pub span: Span, } + +simple_node_impl!(Annotation); + +impl fmt::Display for Annotation { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "@{}", self.identifier) + } +} diff --git a/compiler/ast/src/functions/function.rs b/compiler/ast/src/functions/function.rs deleted file mode 100644 index 5216a27c69..0000000000 --- a/compiler/ast/src/functions/function.rs +++ /dev/null @@ -1,87 +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 . - -use crate::{Annotation, Block, FunctionInput, Identifier, Node, Type}; -use leo_span::{sym, Span, Symbol}; - -use serde::{Deserialize, Serialize}; -use std::cell::Cell; -use std::fmt; - -/// A function definition. -#[derive(Clone, Serialize, Deserialize)] -pub struct Function { - /// Annotations on the function. - pub annotations: Vec, - /// The function identifier, e.g., `foo` in `function foo(...) { ... }`. - pub identifier: Identifier, - /// The function's parameters. - pub input: Vec, - /// The function's required return type. - pub output: Type, - /// Any mapping to the core library. - /// Always `None` when initially parsed. - pub core_mapping: Cell>, - /// The body of the function. - pub block: Block, - /// The entire span of the function definition. - pub span: Span, -} - -impl PartialEq for Function { - fn eq(&self, other: &Self) -> bool { - self.identifier == other.identifier - } -} - -impl Eq for Function {} - -impl Function { - /// Returns function name. - pub fn name(&self) -> Symbol { - self.identifier.name - } - - /// Returns `true` if the function name is `main`. - pub fn is_main(&self) -> bool { - self.name() == sym::main - } - - /// - /// Private formatting method used for optimizing [fmt::Debug] and [fmt::Display] implementations. - /// - fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "function {}", self.identifier)?; - - let parameters = self.input.iter().map(|x| x.to_string()).collect::>().join(","); - let returns = self.output.to_string(); - write!(f, "({}) -> {} {}", parameters, returns, self.block) - } -} - -impl fmt::Debug for Function { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.format(f) - } -} - -impl fmt::Display for Function { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.format(f) - } -} - -crate::simple_node_impl!(Function); diff --git a/compiler/ast/src/functions/input/function_input.rs b/compiler/ast/src/functions/function_input.rs similarity index 100% rename from compiler/ast/src/functions/input/function_input.rs rename to compiler/ast/src/functions/function_input.rs diff --git a/compiler/ast/src/functions/input/mod.rs b/compiler/ast/src/functions/input/mod.rs deleted file mode 100644 index 9c9eb5f979..0000000000 --- a/compiler/ast/src/functions/input/mod.rs +++ /dev/null @@ -1,21 +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 . - -pub mod function_input; -pub use function_input::*; - -pub mod input_variable; -pub use input_variable::*; diff --git a/compiler/ast/src/functions/input/input_variable.rs b/compiler/ast/src/functions/input_variable.rs similarity index 100% rename from compiler/ast/src/functions/input/input_variable.rs rename to compiler/ast/src/functions/input_variable.rs diff --git a/compiler/ast/src/functions/mod.rs b/compiler/ast/src/functions/mod.rs index bd05254ea4..f1a91bb176 100644 --- a/compiler/ast/src/functions/mod.rs +++ b/compiler/ast/src/functions/mod.rs @@ -17,8 +17,80 @@ pub mod annotation; pub use annotation::*; -pub mod function; -pub use function::*; +pub mod function_input; +pub use function_input::*; -pub mod input; -pub use input::*; +pub mod input_variable; +pub use input_variable::*; + +use crate::{Block, Identifier, Node, Type}; +use leo_span::{sym, Span, Symbol}; + +use serde::{Deserialize, Serialize}; +use std::cell::Cell; +use std::fmt; + +/// A function definition. +#[derive(Clone, Serialize, Deserialize)] +pub struct Function { + /// Annotations on the function. + pub annotations: Vec, + /// The function identifier, e.g., `foo` in `function foo(...) { ... }`. + pub identifier: Identifier, + /// The function's parameters. + pub input: Vec, + /// The function's required return type. + pub output: Type, + /// Any mapping to the core library. + /// Always `None` when initially parsed. + pub core_mapping: Cell>, + /// The body of the function. + pub block: Block, + /// The entire span of the function definition. + pub span: Span, +} + +impl PartialEq for Function { + fn eq(&self, other: &Self) -> bool { + self.identifier == other.identifier + } +} + +impl Eq for Function {} + +impl Function { + /// Returns function name. + pub fn name(&self) -> Symbol { + self.identifier.name + } + + /// Returns `true` if the function name is `main`. + pub fn is_main(&self) -> bool { + self.name() == sym::main + } + + /// + /// Private formatting method used for optimizing [fmt::Debug] and [fmt::Display] implementations. + /// + fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "function {}", self.identifier)?; + + let parameters = self.input.iter().map(|x| x.to_string()).collect::>().join(","); + let returns = self.output.to_string(); + write!(f, "({}) -> {} {}", parameters, returns, self.block) + } +} + +impl fmt::Debug for Function { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.format(f) + } +} + +impl fmt::Display for Function { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.format(f) + } +} + +crate::simple_node_impl!(Function);