mirror of
https://github.com/AleoHQ/leo.git
synced 2024-09-11 15:05:33 +03:00
Migrates Functions
This commit is contained in:
parent
3a554af9fe
commit
928f67c00b
@ -5,9 +5,9 @@ use crate::{
|
||||
constraints::{new_scope, ConstrainedProgram, ConstrainedValue},
|
||||
errors::{FunctionError, ImportError},
|
||||
field_from_input, group_from_input,
|
||||
types::{Function, Program},
|
||||
types::{Program},
|
||||
GroupType};
|
||||
use leo_types::{Expression, Identifier, InputValue, Integer, Type};
|
||||
use leo_types::{Expression, Function, Identifier, InputValue, Integer, Type};
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
|
@ -2,10 +2,10 @@
|
||||
|
||||
use crate::{
|
||||
errors::ValueError,
|
||||
types::{Circuit, Function},
|
||||
types::{Circuit},
|
||||
FieldType, GroupType,
|
||||
};
|
||||
use leo_types::{Identifier, Integer, IntegerType, Type};
|
||||
use leo_types::{Function, Identifier, Integer, IntegerType, Type};
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
|
@ -2,7 +2,7 @@
|
||||
//! Each defined type consists of typed statements and expressions.
|
||||
|
||||
use crate::Import;
|
||||
use leo_types::{Identifier, Statement, Type};
|
||||
use leo_types::{Identifier, Function, TestFunction, Type};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -20,35 +20,6 @@ pub struct Circuit {
|
||||
pub members: Vec<CircuitMember>,
|
||||
}
|
||||
|
||||
/// Function parameters
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct InputModel {
|
||||
pub identifier: Identifier,
|
||||
pub mutable: bool,
|
||||
pub private: bool,
|
||||
pub _type: Type,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Function {
|
||||
pub function_name: Identifier,
|
||||
pub inputs: Vec<InputModel>,
|
||||
pub returns: Vec<Type>,
|
||||
pub statements: Vec<Statement>,
|
||||
}
|
||||
|
||||
impl Function {
|
||||
pub fn get_name(&self) -> String {
|
||||
self.function_name.name.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Tests
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Test(pub Function);
|
||||
|
||||
/// A simple program with statement expressions, program arguments and program returns.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Program {
|
||||
@ -57,7 +28,7 @@ pub struct Program {
|
||||
pub imports: Vec<Import>,
|
||||
pub circuits: HashMap<Identifier, Circuit>,
|
||||
pub functions: HashMap<Identifier, Function>,
|
||||
pub tests: HashMap<Identifier, Test>,
|
||||
pub tests: HashMap<Identifier, TestFunction>,
|
||||
}
|
||||
|
||||
impl<'ast> Program {
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
use crate::{
|
||||
Circuit, CircuitMember,
|
||||
Function, InputModel,
|
||||
};
|
||||
|
||||
use std::fmt;
|
||||
@ -44,63 +43,3 @@ impl fmt::Debug for Circuit {
|
||||
self.format(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for InputModel {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// mut var: private bool
|
||||
if self.mutable {
|
||||
write!(f, "mut ")?;
|
||||
}
|
||||
write!(f, "{}: ", self.identifier)?;
|
||||
if self.private {
|
||||
write!(f, "private ")?;
|
||||
} else {
|
||||
write!(f, "public ")?;
|
||||
}
|
||||
write!(f, "{}", self._type)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Function {
|
||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "function {}", self.function_name)?;
|
||||
let parameters = self
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|x| format!("{}", x))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let returns = self
|
||||
.returns
|
||||
.iter()
|
||||
.map(|r| format!("{}", r))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let statements = self
|
||||
.statements
|
||||
.iter()
|
||||
.map(|s| format!("\t{}\n", s))
|
||||
.collect::<Vec<_>>()
|
||||
.join("");
|
||||
if self.returns.len() == 0 {
|
||||
write!(f, "({}) {{\n{}}}", parameters, statements,)
|
||||
} else if self.returns.len() == 1 {
|
||||
write!(f, "({}) -> {} {{\n{}}}", parameters, returns, statements,)
|
||||
} else {
|
||||
write!(f, "({}) -> ({}) {{\n{}}}", parameters, returns, statements,)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Function {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.format(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Function {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.format(f)
|
||||
}
|
||||
}
|
||||
|
@ -8,22 +8,13 @@ use leo_ast::{
|
||||
CircuitFunction,
|
||||
CircuitMember
|
||||
},
|
||||
common::{
|
||||
Visibility,
|
||||
Private,
|
||||
},
|
||||
files::File,
|
||||
functions::{
|
||||
Function,
|
||||
FunctionInput,
|
||||
TestFunction
|
||||
},
|
||||
imports::{
|
||||
Import as AstImport,
|
||||
ImportSymbol as AstImportSymbol,
|
||||
},
|
||||
};
|
||||
use leo_types::{Identifier, Statement, Type};
|
||||
use leo_types::{Function, Identifier, TestFunction, Type};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -42,7 +33,7 @@ impl<'ast> From<CircuitFunction<'ast>> for types::CircuitMember {
|
||||
fn from(circuit_function: CircuitFunction<'ast>) -> Self {
|
||||
types::CircuitMember::CircuitFunction(
|
||||
circuit_function._static.is_some(),
|
||||
types::Function::from(circuit_function.function),
|
||||
Function::from(circuit_function.function),
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -76,52 +67,6 @@ impl<'ast> From<Circuit<'ast>> for types::Circuit {
|
||||
}
|
||||
}
|
||||
|
||||
/// pest ast -> function types::Parameters
|
||||
|
||||
impl<'ast> From<FunctionInput<'ast>> for types::InputModel {
|
||||
fn from(parameter: FunctionInput<'ast>) -> Self {
|
||||
types::InputModel {
|
||||
identifier: Identifier::from(parameter.identifier),
|
||||
mutable: parameter.mutable.is_some(),
|
||||
// private by default
|
||||
private: parameter.visibility.map_or(true, |visibility| {
|
||||
visibility.eq(&Visibility::Private(Private {}))
|
||||
}),
|
||||
_type: Type::from(parameter._type),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// pest ast -> types::Function
|
||||
|
||||
impl<'ast> From<Function<'ast>> for types::Function {
|
||||
fn from(function_definition: Function<'ast>) -> Self {
|
||||
let function_name = Identifier::from(function_definition.function_name);
|
||||
let parameters = function_definition
|
||||
.parameters
|
||||
.into_iter()
|
||||
.map(|parameter| types::InputModel::from(parameter))
|
||||
.collect();
|
||||
let returns = function_definition
|
||||
.returns
|
||||
.into_iter()
|
||||
.map(|return_type| Type::from(return_type))
|
||||
.collect();
|
||||
let statements = function_definition
|
||||
.statements
|
||||
.into_iter()
|
||||
.map(|statement| Statement::from(statement))
|
||||
.collect();
|
||||
|
||||
types::Function {
|
||||
function_name,
|
||||
inputs: parameters,
|
||||
returns,
|
||||
statements,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// pest ast -> Import
|
||||
|
||||
impl<'ast> From<AstImportSymbol<'ast>> for ImportSymbol {
|
||||
@ -146,13 +91,6 @@ impl<'ast> From<AstImport<'ast>> for Import {
|
||||
}
|
||||
}
|
||||
|
||||
/// pest ast -> Test
|
||||
impl<'ast> From<TestFunction<'ast>> for types::Test {
|
||||
fn from(test: TestFunction) -> Self {
|
||||
types::Test(types::Function::from(test.function))
|
||||
}
|
||||
}
|
||||
|
||||
/// pest ast -> types::Program
|
||||
|
||||
impl<'ast> types::Program {
|
||||
@ -178,13 +116,13 @@ impl<'ast> types::Program {
|
||||
file.functions.into_iter().for_each(|function_def| {
|
||||
functions.insert(
|
||||
Identifier::from(function_def.function_name.clone()),
|
||||
types::Function::from(function_def),
|
||||
Function::from(function_def),
|
||||
);
|
||||
});
|
||||
file.tests.into_iter().for_each(|test_def| {
|
||||
tests.insert(
|
||||
Identifier::from(test_def.function.function_name.clone()),
|
||||
types::Test::from(test_def),
|
||||
TestFunction::from(test_def),
|
||||
);
|
||||
});
|
||||
|
||||
|
87
types/src/functions/function.rs
Normal file
87
types/src/functions/function.rs
Normal file
@ -0,0 +1,87 @@
|
||||
use crate::{Identifier, FunctionInput, Statement, Type};
|
||||
use leo_ast::functions::Function as AstFunction;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Function {
|
||||
pub function_name: Identifier,
|
||||
pub inputs: Vec<FunctionInput>,
|
||||
pub returns: Vec<Type>,
|
||||
pub statements: Vec<Statement>,
|
||||
}
|
||||
|
||||
impl<'ast> From<AstFunction<'ast>> for Function {
|
||||
fn from(function_definition: AstFunction<'ast>) -> Self {
|
||||
let function_name = Identifier::from(function_definition.function_name);
|
||||
let parameters = function_definition
|
||||
.parameters
|
||||
.into_iter()
|
||||
.map(|parameter| FunctionInput::from(parameter))
|
||||
.collect();
|
||||
let returns = function_definition
|
||||
.returns
|
||||
.into_iter()
|
||||
.map(|return_type| Type::from(return_type))
|
||||
.collect();
|
||||
let statements = function_definition
|
||||
.statements
|
||||
.into_iter()
|
||||
.map(|statement| Statement::from(statement))
|
||||
.collect();
|
||||
|
||||
Function {
|
||||
function_name,
|
||||
inputs: parameters,
|
||||
returns,
|
||||
statements,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Function {
|
||||
pub fn get_name(&self) -> String {
|
||||
self.function_name.name.clone()
|
||||
}
|
||||
|
||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "function {}", self.function_name)?;
|
||||
let parameters = self
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|x| format!("{}", x))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let returns = self
|
||||
.returns
|
||||
.iter()
|
||||
.map(|r| format!("{}", r))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let statements = self
|
||||
.statements
|
||||
.iter()
|
||||
.map(|s| format!("\t{}\n", s))
|
||||
.collect::<Vec<_>>()
|
||||
.join("");
|
||||
if self.returns.len() == 0 {
|
||||
write!(f, "({}) {{\n{}}}", parameters, statements,)
|
||||
} else if self.returns.len() == 1 {
|
||||
write!(f, "({}) -> {} {{\n{}}}", parameters, returns, statements,)
|
||||
} else {
|
||||
write!(f, "({}) -> ({}) {{\n{}}}", parameters, returns, statements,)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Function {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.format(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Function {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.format(f)
|
||||
}
|
||||
}
|
42
types/src/functions/function_input.rs
Normal file
42
types/src/functions/function_input.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use crate::{Identifier, Type};
|
||||
use leo_ast::{common::{Visibility, Private}, functions::FunctionInput as AstFunctionInput};
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct FunctionInput {
|
||||
pub identifier: Identifier,
|
||||
pub mutable: bool,
|
||||
pub private: bool,
|
||||
pub _type: Type,
|
||||
}
|
||||
|
||||
impl<'ast> From<AstFunctionInput<'ast>> for FunctionInput {
|
||||
fn from(parameter: AstFunctionInput<'ast>) -> Self {
|
||||
FunctionInput {
|
||||
identifier: Identifier::from(parameter.identifier),
|
||||
mutable: parameter.mutable.is_some(),
|
||||
// private by default
|
||||
private: parameter.visibility.map_or(true, |visibility| {
|
||||
visibility.eq(&Visibility::Private(Private {}))
|
||||
}),
|
||||
_type: Type::from(parameter._type),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FunctionInput {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// mut var: private bool
|
||||
if self.mutable {
|
||||
write!(f, "mut ")?;
|
||||
}
|
||||
write!(f, "{}: ", self.identifier)?;
|
||||
if self.private {
|
||||
write!(f, "private ")?;
|
||||
} else {
|
||||
write!(f, "public ")?;
|
||||
}
|
||||
write!(f, "{}", self._type)
|
||||
}
|
||||
}
|
8
types/src/functions/mod.rs
Normal file
8
types/src/functions/mod.rs
Normal file
@ -0,0 +1,8 @@
|
||||
pub mod function;
|
||||
pub use function::*;
|
||||
|
||||
pub mod function_input;
|
||||
pub use function_input::*;
|
||||
|
||||
pub mod test_function;
|
||||
pub use test_function::*;
|
11
types/src/functions/test_function.rs
Normal file
11
types/src/functions/test_function.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use crate::Function;
|
||||
use leo_ast::functions::TestFunction as AstTestFunction;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TestFunction(pub Function);
|
||||
|
||||
impl<'ast> From<AstTestFunction<'ast>> for TestFunction {
|
||||
fn from(test: AstTestFunction) -> Self {
|
||||
TestFunction(Function::from(test.function))
|
||||
}
|
||||
}
|
@ -13,6 +13,9 @@ pub use errors::*;
|
||||
pub mod expression;
|
||||
pub use expression::*;
|
||||
|
||||
pub mod functions;
|
||||
pub use functions::*;
|
||||
|
||||
pub mod input_value;
|
||||
pub use input_value::*;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user