mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
Migrate spread and range
This commit is contained in:
parent
720dfeefca
commit
6d9d92dcaf
@ -1,4 +1,4 @@
|
||||
use crate::ast::{RangeOrExpression, Rule};
|
||||
use crate::{ast::Rule, common::RangeOrExpression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -54,52 +54,6 @@ lazy_static! {
|
||||
|
||||
// Access
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::from_expression))]
|
||||
pub struct FromExpression<'ast>(pub Expression<'ast>);
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::to_expression))]
|
||||
pub struct ToExpression<'ast>(pub Expression<'ast>);
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::range))]
|
||||
pub struct Range<'ast> {
|
||||
pub from: Option<FromExpression<'ast>>,
|
||||
pub to: Option<ToExpression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::range_or_expression))]
|
||||
pub enum RangeOrExpression<'ast> {
|
||||
Range(Range<'ast>),
|
||||
Expression(Expression<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for RangeOrExpression<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
RangeOrExpression::Expression(ref expression) => write!(f, "{}", expression),
|
||||
RangeOrExpression::Range(ref range) => write!(
|
||||
f,
|
||||
"{}..{}",
|
||||
range
|
||||
.from
|
||||
.as_ref()
|
||||
.map(|e| e.0.to_string())
|
||||
.unwrap_or("".to_string()),
|
||||
range
|
||||
.to
|
||||
.as_ref()
|
||||
.map(|e| e.0.to_string())
|
||||
.unwrap_or("".to_string())
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::assignee))]
|
||||
pub struct Assignee<'ast> {
|
||||
@ -122,36 +76,6 @@ impl<'ast> fmt::Display for Assignee<'ast> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::spread))]
|
||||
pub struct Spread<'ast> {
|
||||
pub expression: Expression<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for Spread<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "...{}", self.expression)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::spread_or_expression))]
|
||||
pub enum SpreadOrExpression<'ast> {
|
||||
Spread(Spread<'ast>),
|
||||
Expression(Expression<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for SpreadOrExpression<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
SpreadOrExpression::Spread(ref spread) => write!(f, "{}", spread),
|
||||
SpreadOrExpression::Expression(ref expression) => write!(f, "{}", expression),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Expressions
|
||||
|
||||
fn precedence_climber() -> PrecClimber<Rule> {
|
||||
|
@ -4,6 +4,18 @@ pub use identifier::*;
|
||||
pub mod mutable;
|
||||
pub use mutable::*;
|
||||
|
||||
pub mod range;
|
||||
pub use range::*;
|
||||
|
||||
pub mod range_or_expression;
|
||||
pub use range_or_expression::*;
|
||||
|
||||
pub mod spread;
|
||||
pub use spread::*;
|
||||
|
||||
pub mod spread_or_expression;
|
||||
pub use spread_or_expression::*;
|
||||
|
||||
pub mod static_;
|
||||
pub use static_::*;
|
||||
|
||||
|
22
ast/src/common/range.rs
Normal file
22
ast/src/common/range.rs
Normal file
@ -0,0 +1,22 @@
|
||||
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::range))]
|
||||
pub struct Range<'ast> {
|
||||
pub from: Option<FromExpression<'ast>>,
|
||||
pub to: Option<ToExpression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::from_expression))]
|
||||
pub struct FromExpression<'ast>(pub Expression<'ast>);
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::to_expression))]
|
||||
pub struct ToExpression<'ast>(pub Expression<'ast>);
|
34
ast/src/common/range_or_expression.rs
Normal file
34
ast/src/common/range_or_expression.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use crate::{ast::Rule, common::Range, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::range_or_expression))]
|
||||
pub enum RangeOrExpression<'ast> {
|
||||
Range(Range<'ast>),
|
||||
Expression(Expression<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for RangeOrExpression<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
RangeOrExpression::Expression(ref expression) => write!(f, "{}", expression),
|
||||
RangeOrExpression::Range(ref range) => write!(
|
||||
f,
|
||||
"{}..{}",
|
||||
range
|
||||
.from
|
||||
.as_ref()
|
||||
.map(|e| e.0.to_string())
|
||||
.unwrap_or("".to_string()),
|
||||
range
|
||||
.to
|
||||
.as_ref()
|
||||
.map(|e| e.0.to_string())
|
||||
.unwrap_or("".to_string())
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
19
ast/src/common/spread.rs
Normal file
19
ast/src/common/spread.rs
Normal file
@ -0,0 +1,19 @@
|
||||
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::spread))]
|
||||
pub struct Spread<'ast> {
|
||||
pub expression: Expression<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for Spread<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "...{}", self.expression)
|
||||
}
|
||||
}
|
21
ast/src/common/spread_or_expression.rs
Normal file
21
ast/src/common/spread_or_expression.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use crate::{ast::Rule, common::Spread, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::spread_or_expression))]
|
||||
pub enum SpreadOrExpression<'ast> {
|
||||
Spread(Spread<'ast>),
|
||||
Expression(Expression<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for SpreadOrExpression<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
SpreadOrExpression::Spread(ref spread) => write!(f, "{}", spread),
|
||||
SpreadOrExpression::Expression(ref expression) => write!(f, "{}", expression),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Rule, SpreadOrExpression}, values::Value};
|
||||
use crate::{ast::Rule, common::SpreadOrExpression, values::Value};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Rule, SpreadOrExpression}};
|
||||
use crate::{ast::Rule, common::SpreadOrExpression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -7,6 +7,20 @@ protected_name = { visibility | "let" | "for"| "if" | "else" | "as" | "return" }
|
||||
// Declared in common/mutable.rs
|
||||
mutable = { "mut" }
|
||||
|
||||
// Declared in common/range.rs
|
||||
range = { from_expression? ~ ".." ~ to_expression }
|
||||
from_expression = { expression }
|
||||
to_expression = { expression }
|
||||
|
||||
// Declared in common/range_or_expression.rs
|
||||
range_or_expression = { range | expression }
|
||||
|
||||
// Declared in common/spread.rs
|
||||
spread = { "..." ~ expression }
|
||||
|
||||
// Declared in common/spread_or_expression.rs
|
||||
spread_or_expression = { spread | expression }
|
||||
|
||||
// Declared in common/static_.rs
|
||||
static_ = { "static" }
|
||||
|
||||
@ -147,16 +161,8 @@ access_member = { "." ~ identifier }
|
||||
// Declared in access/static_member_access.rs
|
||||
access_static_member = { "::" ~ identifier }
|
||||
|
||||
range_or_expression = { range | expression }
|
||||
range = { from_expression? ~ ".." ~ to_expression }
|
||||
from_expression = { expression }
|
||||
to_expression = { expression }
|
||||
|
||||
assignee = { identifier ~ access_assignee* }
|
||||
|
||||
spread = { "..." ~ expression }
|
||||
spread_or_expression = { spread | expression }
|
||||
|
||||
/// Circuits
|
||||
|
||||
// Declared in circuits/circuit_definition.rs
|
||||
|
@ -16,6 +16,8 @@ use leo_ast::{
|
||||
},
|
||||
common::{
|
||||
Identifier,
|
||||
RangeOrExpression as AstRangeOrExpression,
|
||||
SpreadOrExpression as AstSpreadOrExpression,
|
||||
Variable as AstVariable,
|
||||
Visibility,
|
||||
Private,
|
||||
@ -139,10 +141,10 @@ impl<'ast> From<IntegerValue<'ast>> for types::Expression {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<ast::RangeOrExpression<'ast>> for types::RangeOrExpression {
|
||||
fn from(range_or_expression: ast::RangeOrExpression<'ast>) -> Self {
|
||||
impl<'ast> From<AstRangeOrExpression<'ast>> for types::RangeOrExpression {
|
||||
fn from(range_or_expression: AstRangeOrExpression<'ast>) -> Self {
|
||||
match range_or_expression {
|
||||
ast::RangeOrExpression::Range(range) => {
|
||||
AstRangeOrExpression::Range(range) => {
|
||||
let from = range
|
||||
.from
|
||||
.map(|from| match types::Expression::from(from.0) {
|
||||
@ -164,7 +166,7 @@ impl<'ast> From<ast::RangeOrExpression<'ast>> for types::RangeOrExpression {
|
||||
|
||||
types::RangeOrExpression::Range(from, to)
|
||||
}
|
||||
ast::RangeOrExpression::Expression(expression) => {
|
||||
AstRangeOrExpression::Expression(expression) => {
|
||||
types::RangeOrExpression::Expression(types::Expression::from(expression))
|
||||
}
|
||||
}
|
||||
@ -228,13 +230,13 @@ impl<'ast> From<NotExpression<'ast>> for types::Expression {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<ast::SpreadOrExpression<'ast>> for types::SpreadOrExpression {
|
||||
fn from(s_or_e: ast::SpreadOrExpression<'ast>) -> Self {
|
||||
impl<'ast> From<AstSpreadOrExpression<'ast>> for types::SpreadOrExpression {
|
||||
fn from(s_or_e: AstSpreadOrExpression<'ast>) -> Self {
|
||||
match s_or_e {
|
||||
ast::SpreadOrExpression::Spread(spread) => {
|
||||
AstSpreadOrExpression::Spread(spread) => {
|
||||
types::SpreadOrExpression::Spread(types::Expression::from(spread.expression))
|
||||
}
|
||||
ast::SpreadOrExpression::Expression(expression) => {
|
||||
AstSpreadOrExpression::Expression(expression) => {
|
||||
types::SpreadOrExpression::Expression(types::Expression::from(expression))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user