impl return tuples and update tests

This commit is contained in:
collin 2020-07-07 10:11:23 -07:00
parent a20d4ab22d
commit 87db0508f4
9 changed files with 75 additions and 18 deletions

View File

@ -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
View 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),
}
}
}

View 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, "")
}
}

View File

@ -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

View File

@ -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_)
}
}

View File

@ -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)
}

View File

@ -1,3 +1,3 @@
function main() -> (bool, bool) {
return true, false
return (true, false)
}

View File

@ -1,3 +1,5 @@
function main() -> group {
return (7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)group
const point = (7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)group;
return point
}

View File

@ -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)
}
}