From 87db0508f4b211ab8960a885b775058bb9eba299 Mon Sep 17 00:00:00 2001 From: collin Date: Tue, 7 Jul 2020 10:11:23 -0700 Subject: [PATCH] impl return tuples and update tests --- ast/src/common/mod.rs | 6 ++++++ ast/src/common/return_.rs | 20 ++++++++++++++++++ ast/src/common/return_tuple.rs | 25 +++++++++++++++++++++++ ast/src/leo.pest | 8 +++++++- ast/src/statements/return_statement.rs | 12 +++-------- compiler/tests/function/multiple.leo | 4 ++-- compiler/tests/function/multiple_main.leo | 2 +- compiler/tests/group/point.leo | 4 +++- types/src/statements/statement.rs | 12 +++++++---- 9 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 ast/src/common/return_.rs create mode 100644 ast/src/common/return_tuple.rs diff --git a/ast/src/common/mod.rs b/ast/src/common/mod.rs index 54945c1bf4..4d8e464a5e 100644 --- a/ast/src/common/mod.rs +++ b/ast/src/common/mod.rs @@ -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::*; diff --git a/ast/src/common/return_.rs b/ast/src/common/return_.rs new file mode 100644 index 0000000000..58455b88df --- /dev/null +++ b/ast/src/common/return_.rs @@ -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), + } + } +} diff --git a/ast/src/common/return_tuple.rs b/ast/src/common/return_tuple.rs new file mode 100644 index 0000000000..80ac040956 --- /dev/null +++ b/ast/src/common/return_tuple.rs @@ -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>, + #[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, "") + } +} diff --git a/ast/src/leo.pest b/ast/src/leo.pest index d9d2ced315..54299bf038 100644 --- a/ast/src/leo.pest +++ b/ast/src/leo.pest @@ -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 diff --git a/ast/src/statements/return_statement.rs b/ast/src/statements/return_statement.rs index 8ec544f3e6..f22b070e22 100644 --- a/ast/src/statements/return_statement.rs +++ b/ast/src/statements/return_statement.rs @@ -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>, + 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_) } } diff --git a/compiler/tests/function/multiple.leo b/compiler/tests/function/multiple.leo index 9ffd2fcfc0..53439ef706 100644 --- a/compiler/tests/function/multiple.leo +++ b/compiler/tests/function/multiple.leo @@ -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) } \ No newline at end of file diff --git a/compiler/tests/function/multiple_main.leo b/compiler/tests/function/multiple_main.leo index 206fb86d96..4e00a8743c 100644 --- a/compiler/tests/function/multiple_main.leo +++ b/compiler/tests/function/multiple_main.leo @@ -1,3 +1,3 @@ function main() -> (bool, bool) { - return true, false + return (true, false) } \ No newline at end of file diff --git a/compiler/tests/group/point.leo b/compiler/tests/group/point.leo index 2fbd9f27b5..43dd2173cc 100644 --- a/compiler/tests/group/point.leo +++ b/compiler/tests/group/point.leo @@ -1,3 +1,5 @@ function main() -> group { - return (7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)group + const point = (7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)group; + + return point } \ No newline at end of file diff --git a/types/src/statements/statement.rs b/types/src/statements/statement.rs index 172682a532..bb3785e59c 100644 --- a/types/src/statements/statement.rs +++ b/types/src/statements/statement.rs @@ -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> 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> for Statement { expression }) .collect(), - span, - ) + }; + + Statement::Return(expressions, span) } }