mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
Migrate expressions
This commit is contained in:
parent
ed010a3209
commit
34a64412b5
228
ast/src/ast.rs
228
ast/src/ast.rs
@ -1,5 +1,18 @@
|
||||
//! Abstract syntax tree (ast) representation from leo.pest.
|
||||
use crate::{
|
||||
common::{
|
||||
Identifier,
|
||||
Visibility
|
||||
},
|
||||
expressions::{
|
||||
ArrayInlineExpression,
|
||||
ArrayInitializerExpression,
|
||||
CircuitInlineExpression,
|
||||
Expression,
|
||||
TernaryExpression,
|
||||
NotExpression,
|
||||
PostfixExpression
|
||||
},
|
||||
imports::Import,
|
||||
operations::{
|
||||
BinaryOperation,
|
||||
@ -12,9 +25,7 @@ use crate::{
|
||||
ArrayType,
|
||||
CircuitType,
|
||||
DataType,
|
||||
Identifier,
|
||||
SelfType,
|
||||
Visibility
|
||||
},
|
||||
values::{
|
||||
Value
|
||||
@ -191,14 +202,6 @@ pub enum Access<'ast> {
|
||||
StaticObject(StaticMemberAccess<'ast>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_postfix))]
|
||||
pub struct PostfixExpression<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
pub accesses: Vec<Access<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::assignee_access))]
|
||||
@ -268,25 +271,6 @@ impl<'ast> fmt::Display for SpreadOrExpression<'ast> {
|
||||
}
|
||||
}
|
||||
|
||||
// Arrays
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_array_inline))]
|
||||
pub struct ArrayInlineExpression<'ast> {
|
||||
pub expressions: Vec<SpreadOrExpression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_array_initializer))]
|
||||
pub struct ArrayInitializerExpression<'ast> {
|
||||
pub expression: Box<SpreadOrExpression<'ast>>,
|
||||
pub count: Value<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
// Circuits
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
@ -336,161 +320,8 @@ pub struct CircuitField<'ast> {
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_circuit_inline))]
|
||||
pub struct CircuitInlineExpression<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
pub members: Vec<CircuitField<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
// Expressions
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_not))]
|
||||
pub struct NotExpression<'ast> {
|
||||
pub operation: NotOperation<'ast>,
|
||||
pub expression: Box<Expression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
// #[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
// #[pest_ast(rule(Rule::expression_increment))]
|
||||
// pub struct IncrementExpression<'ast> {
|
||||
// pub expression: Box<Expression<'ast>>,
|
||||
// #[pest_ast(outer())]
|
||||
// pub span: Span<'ast>,
|
||||
// }
|
||||
//
|
||||
// #[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
// #[pest_ast(rule(Rule::expression_decrement))]
|
||||
// pub struct DecrementExpression<'ast> {
|
||||
// pub expression: Box<Expression<'ast>>,
|
||||
// #[pest_ast(outer())]
|
||||
// pub span: Span<'ast>,
|
||||
// }
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct BinaryExpression<'ast> {
|
||||
pub operation: BinaryOperation,
|
||||
pub left: Box<Expression<'ast>>,
|
||||
pub right: Box<Expression<'ast>>,
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_conditional))]
|
||||
pub struct TernaryExpression<'ast> {
|
||||
pub first: Box<Expression<'ast>>,
|
||||
pub second: Box<Expression<'ast>>,
|
||||
pub third: Box<Expression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Expression<'ast> {
|
||||
Value(Value<'ast>),
|
||||
Identifier(Identifier<'ast>),
|
||||
Not(NotExpression<'ast>),
|
||||
// Increment(IncrementExpression<'ast>),
|
||||
// Decrement(DecrementExpression<'ast>),
|
||||
Binary(BinaryExpression<'ast>),
|
||||
Ternary(TernaryExpression<'ast>),
|
||||
ArrayInline(ArrayInlineExpression<'ast>),
|
||||
ArrayInitializer(ArrayInitializerExpression<'ast>),
|
||||
CircuitInline(CircuitInlineExpression<'ast>),
|
||||
Postfix(PostfixExpression<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> Expression<'ast> {
|
||||
pub fn binary(
|
||||
operation: BinaryOperation,
|
||||
left: Box<Expression<'ast>>,
|
||||
right: Box<Expression<'ast>>,
|
||||
span: Span<'ast>,
|
||||
) -> Self {
|
||||
Expression::Binary(BinaryExpression {
|
||||
operation,
|
||||
left,
|
||||
right,
|
||||
span,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn ternary(
|
||||
first: Box<Expression<'ast>>,
|
||||
second: Box<Expression<'ast>>,
|
||||
third: Box<Expression<'ast>>,
|
||||
span: Span<'ast>,
|
||||
) -> Self {
|
||||
Expression::Ternary(TernaryExpression {
|
||||
first,
|
||||
second,
|
||||
third,
|
||||
span,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn span(&self) -> &Span<'ast> {
|
||||
match self {
|
||||
Expression::Value(expression) => &expression.span(),
|
||||
Expression::Identifier(expression) => &expression.span,
|
||||
Expression::Not(expression) => &expression.span,
|
||||
// Expression::Increment(expression) => &expression.span,
|
||||
// Expression::Decrement(expression) => &expression.span,
|
||||
Expression::Binary(expression) => &expression.span,
|
||||
Expression::Ternary(expression) => &expression.span,
|
||||
Expression::ArrayInline(expression) => &expression.span,
|
||||
Expression::ArrayInitializer(expression) => &expression.span,
|
||||
Expression::CircuitInline(expression) => &expression.span,
|
||||
Expression::Postfix(expression) => &expression.span,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for Expression<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Expression::Value(ref expression) => write!(f, "{}", expression),
|
||||
Expression::Identifier(ref expression) => write!(f, "{}", expression),
|
||||
Expression::Not(ref expression) => write!(f, "!{}", expression.expression),
|
||||
// Expression::Increment(ref expression) => write!(f, "{}++", expression.expression),
|
||||
// Expression::Decrement(ref expression) => write!(f, "{}--", expression.expression),
|
||||
Expression::Binary(ref expression) => {
|
||||
write!(f, "{} == {}", expression.left, expression.right)
|
||||
}
|
||||
Expression::Ternary(ref expression) => write!(
|
||||
f,
|
||||
"if {} ? {} : {}",
|
||||
expression.first, expression.second, expression.third
|
||||
),
|
||||
Expression::ArrayInline(ref expression) => {
|
||||
for (i, spread_or_expression) in expression.expressions.iter().enumerate() {
|
||||
write!(f, "{}", spread_or_expression)?;
|
||||
if i < expression.expressions.len() - 1 {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
}
|
||||
write!(f, "")
|
||||
}
|
||||
Expression::ArrayInitializer(ref expression) => {
|
||||
write!(f, "[{} ; {}]", expression.expression, expression.count)
|
||||
}
|
||||
Expression::CircuitInline(ref expression) => write!(
|
||||
f,
|
||||
"inline circuit display not impl {}",
|
||||
expression.identifier
|
||||
),
|
||||
Expression::Postfix(ref expression) => {
|
||||
write!(f, "Postfix display not impl {}", expression.identifier)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn precedence_climber() -> PrecClimber<Rule> {
|
||||
PrecClimber::new(vec![
|
||||
Operator::new(Rule::operation_or, Assoc::Left),
|
||||
@ -516,11 +347,6 @@ fn parse_term(pair: Pair<Rule>) -> Box<Expression> {
|
||||
let next = clone.into_inner().next().unwrap();
|
||||
match next.as_rule() {
|
||||
Rule::expression => Expression::from_pest(&mut pair.into_inner()).unwrap(), // Parenthesis case
|
||||
Rule::expression_circuit_inline => {
|
||||
Expression::CircuitInline(
|
||||
CircuitInlineExpression::from_pest(&mut pair.into_inner()).unwrap(),
|
||||
)
|
||||
},
|
||||
Rule::expression_array_inline => {
|
||||
Expression::ArrayInline(
|
||||
ArrayInlineExpression::from_pest(&mut pair.into_inner()).unwrap()
|
||||
@ -531,6 +357,11 @@ fn parse_term(pair: Pair<Rule>) -> Box<Expression> {
|
||||
ArrayInitializerExpression::from_pest(&mut pair.into_inner()).unwrap()
|
||||
)
|
||||
},
|
||||
Rule::expression_circuit_inline => {
|
||||
Expression::CircuitInline(
|
||||
CircuitInlineExpression::from_pest(&mut pair.into_inner()).unwrap(),
|
||||
)
|
||||
},
|
||||
Rule::expression_conditional => {
|
||||
Expression::Ternary(
|
||||
TernaryExpression::from_pest(&mut pair.into_inner()).unwrap(),
|
||||
@ -546,28 +377,6 @@ fn parse_term(pair: Pair<Rule>) -> Box<Expression> {
|
||||
let expression = parse_term(inner.next().unwrap());
|
||||
Expression::Not(NotExpression { operation, expression, span })
|
||||
},
|
||||
// Rule::expression_increment => {
|
||||
// println!("expression increment");
|
||||
// let span = next.as_span();
|
||||
// let mut inner = next.into_inner();
|
||||
// let expression = parse_term(inner.next().unwrap());
|
||||
// // let operation = match inner.next().unwrap().as_rule() {
|
||||
// // Rule::operation_post_increment => Increment::from_pest(&mut pair.into_inner().next().unwrap().into_inner()).unwrap(),
|
||||
// // rule => unreachable!("`expression_increment` should yield `operation_post_increment`, found {:#?}", rule)
|
||||
// // };
|
||||
// Expression::Increment(IncrementExpression { expression, span })
|
||||
// },
|
||||
// Rule::expression_decrement => {
|
||||
// println!("expression decrement");
|
||||
// let span = next.as_span();
|
||||
// let mut inner = next.into_inner();
|
||||
// let expression = parse_term(inner.next().unwrap());
|
||||
// // let operation = match inner.next().unwrap().as_rule() {
|
||||
// // Rule::operation_post_decrement => Decrement::from_pest(&mut pair.into_inner().next().unwrap().into_inner()).unwrap(),
|
||||
// // rule => unreachable!("`expression_decrement` should yield `operation_post_decrement`, found {:#?}", rule)
|
||||
// // };
|
||||
// Expression::Decrement(DecrementExpression { expression, span })
|
||||
// },
|
||||
Rule::expression_postfix => {
|
||||
Expression::Postfix(
|
||||
PostfixExpression::from_pest(&mut pair.into_inner()).unwrap(),
|
||||
@ -587,7 +396,6 @@ fn parse_term(pair: Pair<Rule>) -> Box<Expression> {
|
||||
rule => unreachable!("`expression_primitive` should contain one of [`value`, `identifier`], found {:#?}", rule)
|
||||
}
|
||||
},
|
||||
|
||||
rule => unreachable!("`term` should contain one of ['value', 'identifier', 'expression', 'expression_not', 'expression_increment', 'expression_decrement'], found {:#?}", rule)
|
||||
}
|
||||
}
|
||||
|
5
ast/src/common/mod.rs
Normal file
5
ast/src/common/mod.rs
Normal file
@ -0,0 +1,5 @@
|
||||
pub mod identifier;
|
||||
pub use identifier::*;
|
||||
|
||||
pub mod visibility;
|
||||
pub use visibility::*;
|
13
ast/src/expressions/array_initializer_expression.rs
Normal file
13
ast/src/expressions/array_initializer_expression.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use crate::{ast::{Rule, SpreadOrExpression}, values::Value};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_array_initializer))]
|
||||
pub struct ArrayInitializerExpression<'ast> {
|
||||
pub expression: Box<SpreadOrExpression<'ast>>,
|
||||
pub count: Value<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
12
ast/src/expressions/array_inline_expression.rs
Normal file
12
ast/src/expressions/array_inline_expression.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use crate::{ast::{Rule, SpreadOrExpression}};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_array_inline))]
|
||||
pub struct ArrayInlineExpression<'ast> {
|
||||
pub expressions: Vec<SpreadOrExpression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
11
ast/src/expressions/binary_expression.rs
Normal file
11
ast/src/expressions/binary_expression.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use crate::{expressions::Expression, operations::BinaryOperation};
|
||||
|
||||
use pest::Span;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct BinaryExpression<'ast> {
|
||||
pub operation: BinaryOperation,
|
||||
pub left: Box<Expression<'ast>>,
|
||||
pub right: Box<Expression<'ast>>,
|
||||
pub span: Span<'ast>,
|
||||
}
|
13
ast/src/expressions/circuit_inline_expression.rs
Normal file
13
ast/src/expressions/circuit_inline_expression.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use crate::{ast::{Rule, CircuitField}, common::Identifier,};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_circuit_inline))]
|
||||
pub struct CircuitInlineExpression<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
pub members: Vec<CircuitField<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
99
ast/src/expressions/expression.rs
Normal file
99
ast/src/expressions/expression.rs
Normal file
@ -0,0 +1,99 @@
|
||||
use crate::{common::Identifier, expressions::*, operations::BinaryOperation, values::Value,};
|
||||
|
||||
use pest::Span;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Expression<'ast> {
|
||||
Value(Value<'ast>),
|
||||
Identifier(Identifier<'ast>),
|
||||
Not(NotExpression<'ast>),
|
||||
Binary(BinaryExpression<'ast>),
|
||||
Ternary(TernaryExpression<'ast>),
|
||||
ArrayInline(ArrayInlineExpression<'ast>),
|
||||
ArrayInitializer(ArrayInitializerExpression<'ast>),
|
||||
CircuitInline(CircuitInlineExpression<'ast>),
|
||||
Postfix(PostfixExpression<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> Expression<'ast> {
|
||||
pub fn binary(
|
||||
operation: BinaryOperation,
|
||||
left: Box<Expression<'ast>>,
|
||||
right: Box<Expression<'ast>>,
|
||||
span: Span<'ast>,
|
||||
) -> Self {
|
||||
Expression::Binary(BinaryExpression {
|
||||
operation,
|
||||
left,
|
||||
right,
|
||||
span,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn ternary(
|
||||
first: Box<Expression<'ast>>,
|
||||
second: Box<Expression<'ast>>,
|
||||
third: Box<Expression<'ast>>,
|
||||
span: Span<'ast>,
|
||||
) -> Self {
|
||||
Expression::Ternary(TernaryExpression {
|
||||
first,
|
||||
second,
|
||||
third,
|
||||
span,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn span(&self) -> &Span<'ast> {
|
||||
match self {
|
||||
Expression::Value(expression) => &expression.span(),
|
||||
Expression::Identifier(expression) => &expression.span,
|
||||
Expression::Not(expression) => &expression.span,
|
||||
Expression::Binary(expression) => &expression.span,
|
||||
Expression::Ternary(expression) => &expression.span,
|
||||
Expression::ArrayInline(expression) => &expression.span,
|
||||
Expression::ArrayInitializer(expression) => &expression.span,
|
||||
Expression::CircuitInline(expression) => &expression.span,
|
||||
Expression::Postfix(expression) => &expression.span,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for Expression<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Expression::Value(ref expression) => write!(f, "{}", expression),
|
||||
Expression::Identifier(ref expression) => write!(f, "{}", expression),
|
||||
Expression::Not(ref expression) => write!(f, "!{}", expression.expression),
|
||||
Expression::Binary(ref expression) => {
|
||||
write!(f, "{} == {}", expression.left, expression.right)
|
||||
}
|
||||
Expression::Ternary(ref expression) => write!(
|
||||
f,
|
||||
"if {} ? {} : {}",
|
||||
expression.first, expression.second, expression.third
|
||||
),
|
||||
Expression::ArrayInline(ref expression) => {
|
||||
for (i, spread_or_expression) in expression.expressions.iter().enumerate() {
|
||||
write!(f, "{}", spread_or_expression)?;
|
||||
if i < expression.expressions.len() - 1 {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
}
|
||||
write!(f, "")
|
||||
}
|
||||
Expression::ArrayInitializer(ref expression) => {
|
||||
write!(f, "[{} ; {}]", expression.expression, expression.count)
|
||||
}
|
||||
Expression::CircuitInline(ref expression) => write!(
|
||||
f,
|
||||
"inline circuit display not impl {}",
|
||||
expression.identifier
|
||||
),
|
||||
Expression::Postfix(ref expression) => {
|
||||
write!(f, "Postfix display not impl {}", expression.identifier)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
ast/src/expressions/mod.rs
Normal file
23
ast/src/expressions/mod.rs
Normal file
@ -0,0 +1,23 @@
|
||||
pub mod array_initializer_expression;
|
||||
pub use array_initializer_expression::*;
|
||||
|
||||
pub mod array_inline_expression;
|
||||
pub use array_inline_expression::*;
|
||||
|
||||
pub mod binary_expression;
|
||||
pub use binary_expression::*;
|
||||
|
||||
pub mod circuit_inline_expression;
|
||||
pub use circuit_inline_expression::*;
|
||||
|
||||
pub mod expression;
|
||||
pub use expression::*;
|
||||
|
||||
pub mod not_expression;
|
||||
pub use not_expression::*;
|
||||
|
||||
pub mod postfix_expression;
|
||||
pub use postfix_expression::*;
|
||||
|
||||
pub mod ternary_expression;
|
||||
pub use ternary_expression::*;
|
13
ast/src/expressions/not_expression.rs
Normal file
13
ast/src/expressions/not_expression.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use crate::{ast::Rule, expressions::Expression, operations::NotOperation};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_not))]
|
||||
pub struct NotExpression<'ast> {
|
||||
pub operation: NotOperation<'ast>,
|
||||
pub expression: Box<Expression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
13
ast/src/expressions/postfix_expression.rs
Normal file
13
ast/src/expressions/postfix_expression.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use crate::{ast::{Rule, Access}, common::Identifier};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_postfix))]
|
||||
pub struct PostfixExpression<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
pub accesses: Vec<Access<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
14
ast/src/expressions/ternary_expression.rs
Normal file
14
ast/src/expressions/ternary_expression.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use crate::{ast::Rule, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::expression_conditional))]
|
||||
pub struct TernaryExpression<'ast> {
|
||||
pub first: Box<Expression<'ast>>,
|
||||
pub second: Box<Expression<'ast>>,
|
||||
pub third: Box<Expression<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
pub span: Span<'ast>,
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::Rule, types::Identifier};
|
||||
use crate::{ast::Rule, common::Identifier};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,12 +1,12 @@
|
||||
/// Identifiers
|
||||
|
||||
// Declared in types/identifier.rs
|
||||
// Declared in common/identifier.rs
|
||||
identifier = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||
protected_name = { visibility | "let" | "for"| "if" | "else" | "as" | "return" }
|
||||
|
||||
/// Visibility
|
||||
|
||||
// Declared in types/visibility.rs
|
||||
// Declared in common/visibility.rs
|
||||
visibility = { visibility_public | visibility_private }
|
||||
visibility_public = { "public" }
|
||||
visibility_private = { "private" }
|
||||
@ -120,7 +120,7 @@ group_single_or_tuple = {value_number | group_tuple}
|
||||
|
||||
/// Variables + Mutability
|
||||
|
||||
mutable = {"mut"}
|
||||
mutable = { "mut" }
|
||||
variable = { mutable? ~ identifier ~ (":" ~ _type)? }
|
||||
variable_tuple = _{ variable ~ ("," ~ variable)* }
|
||||
|
||||
@ -138,7 +138,6 @@ access_member = { "." ~ identifier }
|
||||
access_static_member = { "::" ~ identifier }
|
||||
access = { access_array | access_call | access_member | access_static_member}
|
||||
|
||||
expression_postfix = { identifier ~ access+ }
|
||||
|
||||
assignee_access = { access_array | access_member }
|
||||
assignee = { identifier ~ assignee_access* }
|
||||
@ -149,8 +148,6 @@ spread_or_expression = { spread | expression }
|
||||
/// Arrays
|
||||
|
||||
inline_array_inner = _{(spread_or_expression ~ ("," ~ NEWLINE* ~ spread_or_expression)*)?}
|
||||
expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"}
|
||||
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ value ~ "]" }
|
||||
|
||||
/// Circuits
|
||||
|
||||
@ -165,7 +162,6 @@ circuit_definition = { "circuit" ~ identifier ~ "{" ~ NEWLINE* ~ circuit_member*
|
||||
|
||||
circuit_field = { identifier ~ ":" ~ expression }
|
||||
circuit_field_list = _{(circuit_field ~ ("," ~ NEWLINE* ~ circuit_field)*)? ~ ","? }
|
||||
expression_circuit_inline = { identifier ~ "{" ~ NEWLINE* ~ circuit_field_list ~ NEWLINE* ~ "}" }
|
||||
|
||||
/// Conditionals
|
||||
|
||||
@ -175,22 +171,35 @@ expression_conditional = { "if" ~ expression ~ "?" ~ expression ~ ":" ~ expressi
|
||||
|
||||
expression_term = {
|
||||
("(" ~ expression ~ ")")
|
||||
| expression_array_initializer
|
||||
| expression_array_inline
|
||||
| expression_circuit_inline
|
||||
| expression_conditional
|
||||
| expression_not
|
||||
| expression_postfix
|
||||
| expression_primitive
|
||||
| expression_not
|
||||
// | expression_increment
|
||||
// | expression_decrement
|
||||
| expression_array_inline
|
||||
| expression_array_initializer
|
||||
}
|
||||
expression_not = { operation_not ~ expression_term }
|
||||
expression_primitive = { value | identifier }
|
||||
|
||||
expression = { expression_term ~ (operation_binary ~ expression_term)* }
|
||||
expression_tuple = _{ (expression ~ ("," ~ expression)*)? }
|
||||
|
||||
// Declared in expressions/expression.rs
|
||||
expression = { expression_term ~ (operation_binary ~ expression_term)* }
|
||||
|
||||
// Declared in expressions/array_initializer_expression.rs
|
||||
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ value ~ "]" }
|
||||
|
||||
// Declared in expressions/array_inline_expression.rs
|
||||
expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"}
|
||||
|
||||
// Declared in expressions/circuit_inline_expression.rs
|
||||
expression_circuit_inline = { identifier ~ "{" ~ NEWLINE* ~ circuit_field_list ~ NEWLINE* ~ "}" }
|
||||
|
||||
// Declared in expressions/not_expression.rs
|
||||
expression_not = { operation_not ~ expression_term }
|
||||
|
||||
// Declared in expressions/postfix_expression.rs
|
||||
expression_postfix = { identifier ~ access+ }
|
||||
|
||||
/// Statements
|
||||
|
||||
// Declared in statements/statement.rs
|
||||
|
@ -8,9 +8,15 @@ extern crate thiserror;
|
||||
pub mod ast;
|
||||
pub use ast::*;
|
||||
|
||||
pub mod common;
|
||||
pub use common::*;
|
||||
|
||||
pub mod errors;
|
||||
pub use errors::*;
|
||||
|
||||
pub mod expressions;
|
||||
pub use expressions::*;
|
||||
|
||||
pub mod imports;
|
||||
pub use imports::*;
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::ast::{Expression, Rule, LineEnd};
|
||||
use crate::{ast::{Rule, LineEnd}, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Expression, Rule, LineEnd, Assignee}, operations::AssignOperation};
|
||||
use crate::{ast::{Rule, LineEnd, Assignee}, expressions::Expression, operations::AssignOperation};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Expression, Rule}, statements::{ConditionalNestedOrEndStatement, Statement}};
|
||||
use crate::{ast::Rule, expressions::Expression, statements::{ConditionalNestedOrEndStatement, Statement}};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::ast::{Expression, Rule, LineEnd, Variable};
|
||||
use crate::{ast::{Rule, LineEnd, Variable}, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::ast::{Expression, Rule, LineEnd};
|
||||
use crate::{ast::{Rule, LineEnd}, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Expression, Rule}, statements::Statement, types::Identifier};
|
||||
use crate::{ast::Rule, expressions::{Expression}, statements::Statement, common::Identifier};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::{Expression, Rule, LineEnd, Variable}, types::Identifier};
|
||||
use crate::{ast::{Rule, LineEnd, Variable}, common::Identifier, expressions::{Expression}};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::ast::{Expression, Rule};
|
||||
use crate::{ast::Rule, expressions::Expression};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::Rule, types::Identifier};
|
||||
use crate::{ast::Rule, common::Identifier};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -16,14 +16,8 @@ pub use field_type::*;
|
||||
pub mod group_type;
|
||||
pub use group_type::*;
|
||||
|
||||
pub mod identifier;
|
||||
pub use identifier::*;
|
||||
|
||||
pub mod integer_type;
|
||||
pub use integer_type::*;
|
||||
|
||||
pub mod self_type;
|
||||
pub use self_type::*;
|
||||
|
||||
pub mod visibility;
|
||||
pub use visibility::*;
|
||||
|
@ -3,6 +3,21 @@
|
||||
use crate::{types, Import, ImportSymbol};
|
||||
use leo_ast::{
|
||||
ast,
|
||||
common::{
|
||||
Identifier,
|
||||
Visibility,
|
||||
Private,
|
||||
},
|
||||
expressions::{
|
||||
ArrayInitializerExpression,
|
||||
ArrayInlineExpression,
|
||||
BinaryExpression,
|
||||
CircuitInlineExpression,
|
||||
Expression,
|
||||
NotExpression,
|
||||
PostfixExpression,
|
||||
TernaryExpression
|
||||
},
|
||||
imports::{
|
||||
Import as AstImport,
|
||||
ImportSymbol as AstImportSymbol,
|
||||
@ -27,10 +42,7 @@ use leo_ast::{
|
||||
ArrayType,
|
||||
CircuitType,
|
||||
DataType,
|
||||
Identifier,
|
||||
IntegerType,
|
||||
Visibility,
|
||||
Private,
|
||||
},
|
||||
values::{
|
||||
BooleanValue,
|
||||
@ -197,8 +209,8 @@ impl<'ast> From<Value<'ast>> for types::Expression {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<ast::NotExpression<'ast>> for types::Expression {
|
||||
fn from(expression: ast::NotExpression<'ast>) -> Self {
|
||||
impl<'ast> From<NotExpression<'ast>> for types::Expression {
|
||||
fn from(expression: NotExpression<'ast>) -> Self {
|
||||
types::Expression::Not(Box::new(types::Expression::from(*expression.expression)))
|
||||
}
|
||||
}
|
||||
@ -216,8 +228,8 @@ impl<'ast> From<ast::SpreadOrExpression<'ast>> for types::SpreadOrExpression {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<ast::BinaryExpression<'ast>> for types::Expression {
|
||||
fn from(expression: ast::BinaryExpression<'ast>) -> Self {
|
||||
impl<'ast> From<BinaryExpression<'ast>> for types::Expression {
|
||||
fn from(expression: BinaryExpression<'ast>) -> Self {
|
||||
match expression.operation {
|
||||
// Boolean operations
|
||||
BinaryOperation::Or => types::Expression::Or(
|
||||
@ -276,8 +288,8 @@ impl<'ast> From<ast::BinaryExpression<'ast>> for types::Expression {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<ast::TernaryExpression<'ast>> for types::Expression {
|
||||
fn from(expression: ast::TernaryExpression<'ast>) -> Self {
|
||||
impl<'ast> From<TernaryExpression<'ast>> for types::Expression {
|
||||
fn from(expression: TernaryExpression<'ast>) -> Self {
|
||||
types::Expression::IfElse(
|
||||
Box::new(types::Expression::from(*expression.first)),
|
||||
Box::new(types::Expression::from(*expression.second)),
|
||||
@ -286,8 +298,8 @@ impl<'ast> From<ast::TernaryExpression<'ast>> for types::Expression {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<ast::ArrayInlineExpression<'ast>> for types::Expression {
|
||||
fn from(array: ast::ArrayInlineExpression<'ast>) -> Self {
|
||||
impl<'ast> From<ArrayInlineExpression<'ast>> for types::Expression {
|
||||
fn from(array: ArrayInlineExpression<'ast>) -> Self {
|
||||
types::Expression::Array(
|
||||
array
|
||||
.expressions
|
||||
@ -297,8 +309,8 @@ impl<'ast> From<ast::ArrayInlineExpression<'ast>> for types::Expression {
|
||||
)
|
||||
}
|
||||
}
|
||||
impl<'ast> From<ast::ArrayInitializerExpression<'ast>> for types::Expression {
|
||||
fn from(array: ast::ArrayInitializerExpression<'ast>) -> Self {
|
||||
impl<'ast> From<ArrayInitializerExpression<'ast>> for types::Expression {
|
||||
fn from(array: ArrayInitializerExpression<'ast>) -> Self {
|
||||
let count = types::Expression::get_count(array.count);
|
||||
let expression = Box::new(types::SpreadOrExpression::from(*array.expression));
|
||||
|
||||
@ -315,8 +327,8 @@ impl<'ast> From<ast::CircuitField<'ast>> for types::CircuitFieldDefinition {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<ast::CircuitInlineExpression<'ast>> for types::Expression {
|
||||
fn from(expression: ast::CircuitInlineExpression<'ast>) -> Self {
|
||||
impl<'ast> From<CircuitInlineExpression<'ast>> for types::Expression {
|
||||
fn from(expression: CircuitInlineExpression<'ast>) -> Self {
|
||||
let variable = types::Identifier::from(expression.identifier);
|
||||
let members = expression
|
||||
.members
|
||||
@ -328,8 +340,8 @@ impl<'ast> From<ast::CircuitInlineExpression<'ast>> for types::Expression {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<ast::PostfixExpression<'ast>> for types::Expression {
|
||||
fn from(expression: ast::PostfixExpression<'ast>) -> Self {
|
||||
impl<'ast> From<PostfixExpression<'ast>> for types::Expression {
|
||||
fn from(expression: PostfixExpression<'ast>) -> Self {
|
||||
let variable =
|
||||
types::Expression::Identifier(types::Identifier::from(expression.identifier));
|
||||
|
||||
@ -372,18 +384,18 @@ impl<'ast> From<ast::PostfixExpression<'ast>> for types::Expression {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<ast::Expression<'ast>> for types::Expression {
|
||||
fn from(expression: ast::Expression<'ast>) -> Self {
|
||||
impl<'ast> From<Expression<'ast>> for types::Expression {
|
||||
fn from(expression: Expression<'ast>) -> Self {
|
||||
match expression {
|
||||
ast::Expression::Value(value) => types::Expression::from(value),
|
||||
ast::Expression::Identifier(variable) => types::Expression::from(variable),
|
||||
ast::Expression::Not(expression) => types::Expression::from(expression),
|
||||
ast::Expression::Binary(expression) => types::Expression::from(expression),
|
||||
ast::Expression::Ternary(expression) => types::Expression::from(expression),
|
||||
ast::Expression::ArrayInline(expression) => types::Expression::from(expression),
|
||||
ast::Expression::ArrayInitializer(expression) => types::Expression::from(expression),
|
||||
ast::Expression::CircuitInline(expression) => types::Expression::from(expression),
|
||||
ast::Expression::Postfix(expression) => types::Expression::from(expression),
|
||||
Expression::Value(value) => types::Expression::from(value),
|
||||
Expression::Identifier(variable) => types::Expression::from(variable),
|
||||
Expression::Not(expression) => types::Expression::from(expression),
|
||||
Expression::Binary(expression) => types::Expression::from(expression),
|
||||
Expression::Ternary(expression) => types::Expression::from(expression),
|
||||
Expression::ArrayInline(expression) => types::Expression::from(expression),
|
||||
Expression::ArrayInitializer(expression) => types::Expression::from(expression),
|
||||
Expression::CircuitInline(expression) => types::Expression::from(expression),
|
||||
Expression::Postfix(expression) => types::Expression::from(expression),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user