mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-25 03:04:13 +03:00
Reorganize ast/functions folder
This commit is contained in:
parent
a73ddfb815
commit
ac4cd71800
@ -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.
|
||||
|
@ -14,16 +14,27 @@
|
||||
// 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::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)
|
||||
}
|
||||
}
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<Annotation>,
|
||||
/// The function identifier, e.g., `foo` in `function foo(...) { ... }`.
|
||||
pub identifier: Identifier,
|
||||
/// The function's parameters.
|
||||
pub input: Vec<FunctionInput>,
|
||||
/// The function's required return type.
|
||||
pub output: Type,
|
||||
/// Any mapping to the core library.
|
||||
/// Always `None` when initially parsed.
|
||||
pub core_mapping: Cell<Option<Symbol>>,
|
||||
/// 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::<Vec<_>>().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);
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
pub mod function_input;
|
||||
pub use function_input::*;
|
||||
|
||||
pub mod input_variable;
|
||||
pub use input_variable::*;
|
@ -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<Annotation>,
|
||||
/// The function identifier, e.g., `foo` in `function foo(...) { ... }`.
|
||||
pub identifier: Identifier,
|
||||
/// The function's parameters.
|
||||
pub input: Vec<FunctionInput>,
|
||||
/// The function's required return type.
|
||||
pub output: Type,
|
||||
/// Any mapping to the core library.
|
||||
/// Always `None` when initially parsed.
|
||||
pub core_mapping: Cell<Option<Symbol>>,
|
||||
/// 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::<Vec<_>>().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);
|
||||
|
Loading…
Reference in New Issue
Block a user