mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-02 12:22:08 +03:00
Merge pull request #104 from AleoHQ/feature/return-tuple
Feature/return tuple
This commit is contained in:
commit
ee6382fb8a
@ -279,7 +279,7 @@ function main() -> field {
|
||||
Functions can return tuples whose types are specified in the function signature.
|
||||
```js
|
||||
function test() -> (u32, u32[2]) {
|
||||
return 1, [2, 3]
|
||||
return (1, [2, 3])
|
||||
}
|
||||
|
||||
function main() -> u32[3] {
|
||||
|
@ -22,6 +22,12 @@ pub use range::*;
|
||||
pub mod range_or_expression;
|
||||
pub use range_or_expression::*;
|
||||
|
||||
pub mod return_;
|
||||
pub use return_::*;
|
||||
|
||||
pub mod return_tuple;
|
||||
pub use return_tuple::*;
|
||||
|
||||
pub mod spread;
|
||||
pub use spread::*;
|
||||
|
||||
|
20
ast/src/common/return_.rs
Normal file
20
ast/src/common/return_.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use crate::{ast::Rule, common::ReturnTuple, expressions::Expression};
|
||||
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::return_))]
|
||||
pub enum Return<'ast> {
|
||||
Single(Expression<'ast>),
|
||||
Tuple(ReturnTuple<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for Return<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Return::Single(ref expression) => write!(f, "{}", expression),
|
||||
Return::Tuple(ref expressions) => write!(f, "{}", expressions),
|
||||
}
|
||||
}
|
||||
}
|
25
ast/src/common/return_tuple.rs
Normal file
25
ast/src/common/return_tuple.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use crate::{ast::Rule, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::return_tuple))]
|
||||
pub struct ReturnTuple<'ast> {
|
||||
pub expressions: Vec<Expression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for ReturnTuple<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for (i, expression) in self.expressions.iter().enumerate() {
|
||||
write!(f, "{}", expression)?;
|
||||
if i < self.expressions.len() - 1 {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
}
|
||||
write!(f, "")
|
||||
}
|
||||
}
|
@ -32,6 +32,12 @@ spread = { "..." ~ expression }
|
||||
// Declared in common/spread_or_expression.rs
|
||||
spread_or_expression = { spread | expression }
|
||||
|
||||
// Declared in common/return_.rs
|
||||
return_ = { return_tuple | expression }
|
||||
|
||||
// Declared in common/return_tuple.rs
|
||||
return_tuple = {"(" ~ expression ~ ("," ~ expression)+ ~ ")"}
|
||||
|
||||
// Declared in common/static_.rs
|
||||
static_ = { "static " }
|
||||
|
||||
@ -291,7 +297,7 @@ statement_multiple_assignment = { declare ~ "(" ~ variable_tuple ~ ")" ~ "=" ~
|
||||
variable_tuple = _{ variable ~ ("," ~ variable)* }
|
||||
|
||||
// Declared in statements/return_statement.rs
|
||||
statement_return = { "return " ~ expression_tuple }
|
||||
statement_return = { "return " ~ return_}
|
||||
|
||||
/// Functions
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::Rule, expressions::Expression};
|
||||
use crate::{ast::Rule, common::Return};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
@ -7,19 +7,13 @@ use std::fmt;
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::statement_return))]
|
||||
pub struct ReturnStatement<'ast> {
|
||||
pub expressions: Vec<Expression<'ast>>,
|
||||
pub return_: Return<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for ReturnStatement<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for (i, expression) in self.expressions.iter().enumerate() {
|
||||
write!(f, "{}", expression)?;
|
||||
if i < self.expressions.len() - 1 {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
}
|
||||
write!(f, "")
|
||||
write!(f, "{}", self.return_)
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
function test() -> (bool, bool) {
|
||||
return true, false
|
||||
return (true, false)
|
||||
}
|
||||
|
||||
function main() -> (bool, bool) {
|
||||
let (a, b) = test();
|
||||
return a, b
|
||||
return (a, b)
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
function main() -> (bool, bool) {
|
||||
return true, false
|
||||
return (true, false)
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
function main() -> group {
|
||||
return (7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)group
|
||||
const point = (7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)group;
|
||||
|
||||
return point
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
use crate::{Assignee, ConditionalStatement, Declare, Expression, Identifier, Span, Variable};
|
||||
use leo_ast::{
|
||||
common::Return,
|
||||
operations::AssignOperation,
|
||||
statements::{
|
||||
AssertStatement,
|
||||
@ -32,8 +33,10 @@ pub enum Statement {
|
||||
impl<'ast> From<ReturnStatement<'ast>> for Statement {
|
||||
fn from(statement: ReturnStatement<'ast>) -> Self {
|
||||
let span = Span::from(statement.span);
|
||||
Statement::Return(
|
||||
statement
|
||||
|
||||
let expressions = match statement.return_ {
|
||||
Return::Single(expression) => vec![Expression::from(expression)],
|
||||
Return::Tuple(tuple) => tuple
|
||||
.expressions
|
||||
.into_iter()
|
||||
.map(|expression| {
|
||||
@ -43,8 +46,9 @@ impl<'ast> From<ReturnStatement<'ast>> for Statement {
|
||||
expression
|
||||
})
|
||||
.collect(),
|
||||
span,
|
||||
)
|
||||
};
|
||||
|
||||
Statement::Return(expressions, span)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user