Merge pull request #48 from AleoHQ/refactor/ast

Split ast into access, circuits, common, expressions, imports, operations, statements, types, and values
This commit is contained in:
Howard Wu 2020-06-07 20:24:48 -07:00 committed by GitHub
commit 6bf9dc7b62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
86 changed files with 1971 additions and 1426 deletions

12
ast/src/access/access.rs Normal file
View File

@ -0,0 +1,12 @@
use crate::{access::*, ast::Rule};
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::access))]
pub enum Access<'ast> {
Array(ArrayAccess<'ast>),
Call(CallAccess<'ast>),
Object(MemberAccess<'ast>),
StaticObject(StaticMemberAccess<'ast>),
}

View File

@ -0,0 +1,12 @@
use crate::{ast::Rule, common::RangeOrExpression};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::access_array))]
pub struct ArrayAccess<'ast> {
pub expression: RangeOrExpression<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,20 @@
use crate::{access::{ArrayAccess, MemberAccess}, ast::Rule};
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::access_assignee))]
pub enum AssigneeAccess<'ast> {
Array(ArrayAccess<'ast>),
Member(MemberAccess<'ast>),
}
impl<'ast> fmt::Display for AssigneeAccess<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AssigneeAccess::Array(ref array) => write!(f, "[{}]", array.expression),
AssigneeAccess::Member(ref member) => write!(f, ".{}", member.identifier),
}
}
}

View File

@ -0,0 +1,12 @@
use crate::{ast::Rule, expressions::Expression};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::access_call))]
pub struct CallAccess<'ast> {
pub expressions: Vec<Expression<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,12 @@
use crate::{ast::Rule, common::Identifier};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::access_member))]
pub struct MemberAccess<'ast> {
pub identifier: Identifier<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

17
ast/src/access/mod.rs Normal file
View File

@ -0,0 +1,17 @@
pub mod access;
pub use access::*;
pub mod array_access;
pub use array_access::*;
pub mod assignee_access;
pub use assignee_access::*;
pub mod call_access;
pub use call_access::*;
pub mod member_access;
pub use member_access::*;
pub mod static_member_access;
pub use static_member_access::*;

View File

@ -0,0 +1,12 @@
use crate::{ast::Rule, common::Identifier};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::access_static_member))]
pub struct StaticMemberAccess<'ast> {
pub identifier: Identifier<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +0,0 @@
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement_for))]
pub struct ForStatement<'ast> {
pub index: Identifier<'ast>,
pub start: Expression<'ast>,
pub stop: Expression<'ast>,
pub statements: Vec<Statement<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,13 @@
use crate::{ast::Rule, circuits::CircuitMember, common::Identifier};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::circuit_definition))]
pub struct Circuit<'ast> {
pub identifier: Identifier<'ast>,
pub members: Vec<CircuitMember<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,13 @@
use crate::{ast::Rule, common::Identifier, expressions::Expression};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::circuit_field))]
pub struct CircuitField<'ast> {
pub identifier: Identifier<'ast>,
pub expression: Expression<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,13 @@
use crate::{ast::Rule, common::Identifier, types::Type};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::circuit_field_definition))]
pub struct CircuitFieldDefinition<'ast> {
pub identifier: Identifier<'ast>,
pub _type: Type<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,13 @@
use crate::{ast::Rule, common::Static, function::Function};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::circuit_function))]
pub struct CircuitFunction<'ast> {
pub _static: Option<Static>,
pub function: Function<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,10 @@
use crate::{ast::Rule, circuits::{CircuitFunction, CircuitFieldDefinition}};
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::circuit_member))]
pub enum CircuitMember<'ast> {
CircuitFieldDefinition(CircuitFieldDefinition<'ast>),
CircuitFunction(CircuitFunction<'ast>),
}

14
ast/src/circuits/mod.rs Normal file
View File

@ -0,0 +1,14 @@
pub mod circuit;
pub use circuit::*;
pub mod circuit_field;
pub use circuit_field::*;
pub mod circuit_field_definition;
pub use circuit_field_definition::*;
pub mod circuit_function;
pub use circuit_function::*;
pub mod circuit_member;
pub use circuit_member::*;

View File

@ -0,0 +1,27 @@
use crate::{access::AssigneeAccess, ast::Rule, common::Identifier};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::assignee))]
pub struct Assignee<'ast> {
pub identifier: Identifier<'ast>,
pub accesses: Vec<AssigneeAccess<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for Assignee<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.identifier)?;
for (i, access) in self.accesses.iter().enumerate() {
write!(f, "{}", access)?;
if i < self.accesses.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, "")
}
}

7
ast/src/common/eoi.rs Normal file
View File

@ -0,0 +1,7 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::EOI))]
pub struct EOI;

View File

@ -0,0 +1,20 @@
use crate::ast::{span_into_string, Rule};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::identifier))]
pub struct Identifier<'ast> {
#[pest_ast(outer(with(span_into_string)))]
pub value: String,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for Identifier<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}

View File

@ -0,0 +1,7 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::LINE_END))]
pub struct LineEnd;

35
ast/src/common/mod.rs Normal file
View File

@ -0,0 +1,35 @@
pub mod assignee;
pub use assignee::*;
pub mod eoi;
pub use eoi::*;
pub mod identifier;
pub use identifier::*;
pub mod line_end;
pub use line_end::*;
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_::*;
pub mod variable;
pub use variable::*;
pub mod visibility;
pub use visibility::*;

View File

@ -0,0 +1,7 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::mutable))]
pub struct Mutable {}

21
ast/src/common/range.rs Normal file
View File

@ -0,0 +1,21 @@
use crate::{ast::Rule, expressions::Expression};
use pest::Span;
use pest_ast::FromPest;
#[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>);

View File

@ -0,0 +1,33 @@
use crate::{ast::Rule, common::Range, expressions::Expression};
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
View 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)
}
}

View File

@ -0,0 +1,20 @@
use crate::{ast::Rule, common::Spread, expressions::Expression};
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),
}
}
}

View File

@ -0,0 +1,7 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::static_))]
pub struct Static {}

View File

@ -0,0 +1,31 @@
use crate::{ast::Rule, common::{Identifier, Mutable}, types::Type};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::variable))]
pub struct Variable<'ast> {
pub mutable: Option<Mutable>,
pub identifier: Identifier<'ast>,
pub _type: Option<Type<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for Variable<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref _mutable) = self.mutable {
write!(f, "mut ")?;
}
write!(f, "{}", self.identifier)?;
if let Some(ref _type) = self._type {
write!(f, ": {}", _type)?;
}
write!(f, "")
}
}

View File

@ -0,0 +1,18 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::visibility))]
pub enum Visibility {
Public(Public),
Private(Private),
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::visibility_public))]
pub struct Public {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::visibility_private))]
pub struct Private {}

View File

@ -17,7 +17,7 @@ impl From<Error<Rule>> for SyntaxError {
Rule::type_group => "`group`".to_owned(),
Rule::file => "an import, circuit, or function".to_owned(),
Rule::identifier => "a variable name".to_owned(),
Rule::_type => "a type".to_owned(),
Rule::type_ => "a type".to_owned(),
Rule::access => "`.`, `::`, `()`".to_owned(),
Rule::operation_and => "`&&`".to_owned(),

View File

@ -0,0 +1,13 @@
use crate::{ast::Rule, common::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>,
}

View File

@ -0,0 +1,12 @@
use crate::{ast::Rule, common::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>,
}

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

View File

@ -0,0 +1,13 @@
use crate::{ast::Rule, circuits::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>,
}

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

View 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::*;

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

View File

@ -0,0 +1,13 @@
use crate::{access::Access, ast::Rule, 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>,
}

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

16
ast/src/files/file.rs Normal file
View File

@ -0,0 +1,16 @@
use crate::{ast::Rule, common::EOI, functions::{Function, TestFunction}, imports::Import, circuits::Circuit};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::file))]
pub struct File<'ast> {
pub imports: Vec<Import<'ast>>,
pub circuits: Vec<Circuit<'ast>>,
pub functions: Vec<Function<'ast>>,
pub tests: Vec<TestFunction<'ast>>,
pub eoi: EOI,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

2
ast/src/files/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod file;
pub use file::*;

View File

@ -0,0 +1,15 @@
use crate::{ast::Rule, common::Identifier, functions::FunctionInput, statements::Statement, types::Type};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::function_definition))]
pub struct Function<'ast> {
pub function_name: Identifier<'ast>,
pub parameters: Vec<FunctionInput<'ast>>,
pub returns: Vec<Type<'ast>>,
pub statements: Vec<Statement<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,15 @@
use crate::{ast::Rule, common::{Identifier, Visibility, Mutable}, types::Type};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::function_input))]
pub struct FunctionInput<'ast> {
pub mutable: Option<Mutable>,
pub identifier: Identifier<'ast>,
pub visibility: Option<Visibility>,
pub _type: Type<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

8
ast/src/functions/mod.rs Normal file
View File

@ -0,0 +1,8 @@
pub mod function;
pub use function::*;
pub mod function_input;
pub use function_input::*;
pub mod test_function;
pub use test_function::*;

View File

@ -0,0 +1,12 @@
use crate::{ast::Rule, functions::Function};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::test_function))]
pub struct TestFunction<'ast> {
pub function: Function<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

14
ast/src/imports/import.rs Normal file
View File

@ -0,0 +1,14 @@
use crate::{ast::Rule, common::LineEnd, imports::{ImportSource, ImportSymbol}};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::import))]
pub struct Import<'ast> {
pub source: ImportSource<'ast>,
pub symbols: Vec<ImportSymbol<'ast>>,
pub line_end: LineEnd,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,13 @@
use crate::ast::{Rule, span_into_string};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::import_source))]
pub struct ImportSource<'ast> {
#[pest_ast(outer(with(span_into_string)))]
pub value: String,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,13 @@
use crate::{ast::Rule, common::Identifier};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::import_symbol))]
pub struct ImportSymbol<'ast> {
pub value: Identifier<'ast>,
pub alias: Option<Identifier<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

8
ast/src/imports/mod.rs Normal file
View File

@ -0,0 +1,8 @@
pub mod import;
pub use import::*;
pub mod import_source;
pub use import_source::*;
pub mod import_symbol;
pub use import_symbol::*;

View File

@ -1,37 +1,60 @@
/// Identifiers
/// Common
// Declared in common/assignee.rs
assignee = { identifier ~ access_assignee* }
// Declared in common/file.rs
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ circuit_definition* ~ NEWLINE* ~ function_definition* ~ NEWLINE* ~ test_function* ~ NEWLINE* ~ EOI }
// 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" }
identifier = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
// Declared in common/line_end.rs
LINE_END = { ";" ~ NEWLINE* }
// Declared in common/mutable.rs
mutable = { "mut" }
/// Visibility
// 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" }
// Declared in common/variable.rs
variable = { mutable? ~ identifier ~ (":" ~ type_)? }
// Declared in common/visibility.rs
visibility = { visibility_public | visibility_private }
visibility_public = { "public" }
visibility_private = { "private" }
visibility = { visibility_public | visibility_private }
/// Unary Operations
/// Operations
operation_pre_not = { "!" }
expression_not = { operation_pre_not ~ expression_term }
// expression_increment = { expression+ ~ "++" }
//
// expression_decrement = { expression ~ "--" }
/// Binary Operations
// Declared in operations/not_operation.rs
operation_not = { "!" }
// Declared in operations/binary_operation.rs
operation_and = { "&&" }
operation_or = { "||" }
operation_eq = { "==" }
operation_ne = { "!=" }
operation_ge = { ">=" }
operation_gt = { ">" }
operation_le = { "<=" }
operation_lt = { "<" }
operation_add = { "+" }
operation_sub = { "-" }
operation_mul = { "*" }
@ -43,12 +66,16 @@ operation_compare = _{
operation_ge | operation_gt |
operation_le | operation_lt
}
operation_binary = _{
operation_compare | operation_and | operation_or |
operation_add | operation_sub | operation_pow | operation_mul | operation_div
}
// Declared in operations/assign_operation.rs
operation_assign = {
assign | operation_add_assign | operation_sub_assign |
operation_mul_assign | operation_div_assign | operation_pow_assign
}
assign = { "=" }
operation_add_assign = { "+=" }
operation_sub_assign = { "-=" }
@ -56,18 +83,12 @@ operation_mul_assign = { "*=" }
operation_div_assign = { "/=" }
operation_pow_assign = { "**=" }
operation_assign = {
assign | operation_add_assign | operation_sub_assign |
operation_mul_assign | operation_div_assign | operation_pow_assign
}
/// Types
type_u8 = {"u8"}
type_u16 = {"u16"}
type_u32 = {"u32"}
type_u64 = {"u64"}
type_u128 = {"u128"}
// Declared in types/type_.rs
type_ = { type_self | type_array | type_data | type_circuit }
// Declared in types/integer.rs
type_integer = {
type_u8
| type_u16
@ -75,79 +96,96 @@ type_integer = {
| type_u64
| type_u128
}
type_u8 = { "u8" }
type_u16 = { "u16" }
type_u32 = { "u32" }
type_u64 = { "u64" }
type_u128 = { "u128" }
type_field = {"field"}
type_group = {"group"}
type_bool = {"bool"}
type_self = {"Self"}
type_basic = { type_field | type_group | type_bool | type_integer }
// Declared in types/field_type.rs
type_field = { "field" }
// Declared in types/group_type.rs
type_group = { "group" }
// Declared in types/boolean_type.rs
type_boolean = { "bool" }
// Declared in types/data_type.rs
type_data = { type_field | type_group | type_boolean | type_integer }
// Declared in types/self_type.rs
type_self = { "Self" }
// Declared in types/self_type.rs
type_circuit = { identifier }
type_array = {type_basic ~ ("[" ~ value ~ "]")+ }
_type = {type_self | type_array | type_basic | type_circuit}
type_list = _{(_type ~ ("," ~ _type)*)?}
// Declared in types/array_type.rs
type_array = { type_data ~ ("[" ~ value ~ "]")+ }
type_list = _{ (type_ ~ ("," ~ type_)*)? }
/// Values
// Declared in values/value.rs
value = { value_field | value_group | value_boolean | value_integer | value_implicit }
// Declared in values/number_value.rs
value_number = @{ "0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* }
// Declared in values/number_implicit_value.rs
value_implicit = { value_number }
// Declared in values/integer_value.rs
value_integer = { value_number ~ type_integer }
// Declared in values/boolean_value.rs
value_boolean = { "true" | "false" }
// Declared in values/field_value.rs
value_field = { value_number ~ type_field }
// Declared in values/group_value.rs
value_group = { group_single_or_tuple ~ type_group }
group_tuple = {"(" ~ value_number ~ "," ~ value_number ~ ")"}
group_single_or_tuple = {value_number | group_tuple}
value_group = { group_single_or_tuple ~ type_group }
value_boolean = { "true" | "false" }
value = { value_field | value_group | value_boolean | value_integer | value_implicit }
expression_primitive = { value | identifier }
/// Variables + Mutability
mutable = {"mut"}
variable = { mutable? ~ identifier ~ (":" ~ _type)? }
variable_tuple = _{ variable ~ ("," ~ variable)* }
/// Access
from_expression = { expression }
to_expression = { expression }
range = { from_expression? ~ ".." ~ to_expression }
range_or_expression = { range | expression }
access_array = { "[" ~ range_or_expression ~ "]" }
access_call = { "(" ~ expression_tuple ~ ")" }
access_member = { "." ~ identifier }
access_static_member = { "::" ~ identifier }
// Declared in access/access.rs
access = { access_array | access_call | access_member | access_static_member}
expression_postfix = { identifier ~ access+ }
// Declared in access/array_access.rs
access_array = { "[" ~ range_or_expression ~ "]" }
assignee_access = { access_array | access_member }
assignee = { identifier ~ assignee_access* }
// Declared in access/assignee_access.rs
access_assignee = { access_array | access_member }
spread = { "..." ~ expression }
spread_or_expression = { spread | expression }
// Declared in access/call_access.rs
access_call = { "(" ~ expression_tuple ~ ")" }
/// Arrays
// Declared in access/member_access.rs
access_member = { "." ~ identifier }
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 ~ "]" }
// Declared in access/static_member_access.rs
access_static_member = { "::" ~ identifier }
/// Circuits
circuit_field_definition = { identifier ~ ":" ~ _type ~ NEWLINE* }
_static = {"static"}
circuit_function = {_static? ~ function_definition }
circuit_member = { circuit_function | circuit_field_definition }
// Declared in circuits/circuit_definition.rs
circuit_definition = { "circuit" ~ identifier ~ "{" ~ NEWLINE* ~ circuit_member* ~ NEWLINE* ~ "}" ~ NEWLINE* }
// Declared in circuits/circuit_field.rs
circuit_field = { identifier ~ ":" ~ expression }
circuit_field_list = _{(circuit_field ~ ("," ~ NEWLINE* ~ circuit_field)*)? ~ ","? }
expression_circuit_inline = { identifier ~ "{" ~ NEWLINE* ~ circuit_field_list ~ NEWLINE* ~ "}" }
// Declared in circuits/circuit_field_definition.rs
circuit_field_definition = { identifier ~ ":" ~ type_ ~ NEWLINE* }
// Declared in circuits/circuit_function.rs
circuit_function = { static_? ~ function_definition }
// Declared in circuits/circuit_member.rs
circuit_member = { circuit_function | circuit_field_definition }
/// Conditionals
@ -157,41 +195,40 @@ 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 = { expression_term ~ (operation_binary ~ expression_term)* }
expression_primitive = { value | identifier }
expression_tuple = _{ (expression ~ ("," ~ expression)*)? }
/// Asserts
// Declared in expressions/expression.rs
expression = { expression_term ~ (operation_binary ~ expression_term)* }
assert_eq = {"assert_eq!" ~ "(" ~ NEWLINE* ~ expression ~ "," ~ NEWLINE* ~ expression ~ NEWLINE* ~ ")" ~ LINE_END}
// assert_true = {"assert"}
// Declared in expressions/array_initializer_expression.rs
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ value ~ "]" }
/// Conditionals
conditional_nested_or_end = { statement_conditional | "{" ~ NEWLINE* ~ statement+ ~ "}"}
// Declared in expressions/array_inline_expression.rs
expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"}
inline_array_inner = _{(spread_or_expression ~ ("," ~ NEWLINE* ~ spread_or_expression)*)?}
// Declared in expressions/circuit_inline_expression.rs
expression_circuit_inline = { identifier ~ "{" ~ NEWLINE* ~ circuit_field_list ~ NEWLINE* ~ "}" }
circuit_field_list = _{ (circuit_field ~ ("," ~ NEWLINE* ~ circuit_field)*)? ~ ","? }
// Declared in expressions/not_expression.rs
expression_not = { operation_not ~ expression_term }
// Declared in expressions/postfix_expression.rs
expression_postfix = { identifier ~ access+ }
/// Statements
statement_return = { "return" ~ expression_tuple }
statement_definition = { "let" ~ variable ~ "=" ~ expression ~ LINE_END}
statement_assign = { assignee ~ operation_assign ~ expression ~ LINE_END}
statement_multiple_assignment = { "let" ~ "(" ~ variable_tuple ~ ")" ~ "=" ~ identifier ~ "(" ~ expression_tuple ~ ")" ~ LINE_END}
statement_conditional = {"if" ~ (expression | "(" ~ expression ~ ")") ~ "{" ~ NEWLINE* ~ statement+ ~ "}" ~ ("else" ~ conditional_nested_or_end)?}
statement_for = { "for" ~ identifier ~ "in" ~ expression ~ ".." ~ expression ~ "{" ~ NEWLINE* ~ statement+ ~ "}"}
statement_assert = {
assert_eq
// | assert_true |
}
statement_expression = { expression ~ LINE_END }
// Declared in statements/statement.rs
statement = {
(statement_return
| statement_conditional
@ -205,31 +242,58 @@ statement = {
) ~ NEWLINE*
}
// Declared in statements/assert_statement.rs
statement_assert = { assert_eq }
assert_eq = {"assert_eq!" ~ "(" ~ NEWLINE* ~ expression ~ "," ~ NEWLINE* ~ expression ~ NEWLINE* ~ ")" ~ LINE_END}
// Declared in statements/assign_statement.rs
statement_assign = { assignee ~ operation_assign ~ expression ~ LINE_END }
// Declared in statements/conditional_statement.rs
statement_conditional = {"if" ~ (expression | "(" ~ expression ~ ")") ~ "{" ~ NEWLINE* ~ statement+ ~ "}" ~ ("else" ~ conditional_nested_or_end_statement)?}
conditional_nested_or_end_statement = { statement_conditional | "{" ~ NEWLINE* ~ statement+ ~ "}"}
// Declared in statements/definition_statement.rs
statement_definition = { "let" ~ variable ~ "=" ~ expression ~ LINE_END}
// Declared in statements/expression_statement.rs
statement_expression = { expression ~ LINE_END }
// Declared in statements/for_statement.rs
statement_for = { "for" ~ identifier ~ "in" ~ expression ~ ".." ~ expression ~ "{" ~ NEWLINE* ~ statement+ ~ "}"}
// Declared in statements/multiple_assignment_statement.rs
statement_multiple_assignment = { "let" ~ "(" ~ variable_tuple ~ ")" ~ "=" ~ identifier ~ "(" ~ expression_tuple ~ ")" ~ LINE_END}
variable_tuple = _{ variable ~ ("," ~ variable)* }
// Declared in statements/return_statement.rs
statement_return = { "return" ~ expression_tuple }
/// Functions
input_model = {mutable? ~ identifier ~ ":" ~ visibility? ~ _type}
input_model_list = _{(input_model ~ ("," ~ input_model)*)?}
// Declared in functions/function.rs
function_definition = { "function" ~ identifier ~ "(" ~ input_model_list ~ ")" ~ ("->" ~ (type_ | "(" ~ type_list ~ ")"))? ~ "{" ~ NEWLINE* ~ statement* ~ NEWLINE* ~ "}" ~ NEWLINE* }
function_definition = {"function" ~ identifier ~ "(" ~ input_model_list ~ ")" ~ ("->" ~ (_type | "(" ~ type_list ~ ")"))? ~ "{" ~ NEWLINE* ~ statement* ~ NEWLINE* ~ "}" ~ NEWLINE* }
// Declared in functions/function_input.rs
function_input = { mutable? ~ identifier ~ ":" ~ visibility? ~ type_ }
input_model_list = _{ (function_input ~ ("," ~ function_input)*)? }
// Declared in functions/test_function.rs
test_function = { "test" ~ function_definition }
/// Imports
// Declared in imports/import.rs
import = { "from" ~ "\"" ~ import_source ~ "\"" ~ "import" ~ ("*" | ("{" ~ NEWLINE* ~ import_symbol_tuple ~ NEWLINE* ~ "}") | import_symbol) ~ LINE_END}
// Declared in imports/import_source.rs
import_source = @{ (!"\"" ~ ANY)* }
// Declared in imports/import_symbol.rs
import_symbol = { identifier ~ ("as" ~ identifier)? }
import_symbol_tuple = _{ import_symbol ~ ("," ~ NEWLINE* ~ import_symbol)* }
/// Utilities
COMMENT = _{ ("/*" ~ (!"*/" ~ ANY)* ~ "*/") | ("//" ~ (!NEWLINE ~ ANY)*) }
WHITESPACE = _{ " " | "\t" ~ (NEWLINE)* }
LINE_END = {";" ~ NEWLINE*}
/// Imports
import_source = @{(!"\"" ~ ANY)*}
import_symbol = { identifier ~ ("as" ~ identifier)? }
import_symbol_tuple = _{ import_symbol ~ ("," ~ NEWLINE* ~ import_symbol)* }
import = { "from" ~ "\"" ~ import_source ~ "\"" ~ "import" ~ ("*" | ("{" ~ NEWLINE* ~ import_symbol_tuple ~ NEWLINE* ~ "}") | import_symbol) ~ LINE_END}
/// Tests
test = {"test" ~ function_definition}
/// Program File
file = { SOI ~ NEWLINE* ~ import* ~ NEWLINE* ~ circuit_definition* ~ NEWLINE* ~ function_definition* ~ NEWLINE* ~ test* ~ NEWLINE* ~ EOI }

View File

@ -5,8 +5,41 @@ extern crate pest_derive;
#[macro_use]
extern crate thiserror;
pub mod access;
pub use access::*;
pub mod ast;
pub use ast::*;
pub mod circuits;
pub use circuits::*;
pub mod common;
pub use common::*;
pub mod errors;
pub use errors::*;
pub mod expressions;
pub use expressions::*;
pub mod files;
pub use files::*;
pub mod functions;
pub use functions::*;
pub mod imports;
pub use imports::*;
pub mod operations;
pub use operations::*;
pub mod statements;
pub use statements::*;
pub mod values;
pub use values::*;
pub mod types;
pub use types::*;

View File

@ -0,0 +1,38 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::operation_assign))]
pub enum AssignOperation {
Assign(Assign),
AddAssign(AddAssign),
SubAssign(SubAssign),
MulAssign(MulAssign),
DivAssign(DivAssign),
PowAssign(PowAssign),
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::assign))]
pub struct Assign {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::operation_add_assign))]
pub struct AddAssign {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::operation_sub_assign))]
pub struct SubAssign {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::operation_mul_assign))]
pub struct MulAssign {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::operation_div_assign))]
pub struct DivAssign {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::operation_pow_assign))]
pub struct PowAssign {}

View File

@ -0,0 +1,21 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::operation_binary))]
pub enum BinaryOperation {
Or,
And,
Eq,
Ne,
Ge,
Gt,
Le,
Lt,
Add,
Sub,
Mul,
Div,
Pow,
}

View File

@ -0,0 +1,8 @@
pub mod assign_operation;
pub use assign_operation::*;
pub mod binary_operation;
pub use binary_operation::*;
pub mod not_operation;
pub use not_operation::*;

View File

@ -0,0 +1,11 @@
use crate::ast::Rule;
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::operation_not))]
pub struct NotOperation<'ast> {
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,31 @@
use crate::{ast::Rule, common::LineEnd, expressions::Expression};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement_assert))]
pub enum AssertStatement<'ast> {
AssertEq(AssertEq<'ast>),
}
impl<'ast> fmt::Display for AssertStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AssertStatement::AssertEq(ref assert) => {
write!(f, "assert_eq({}, {});", assert.left, assert.right)
}
}
}
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::assert_eq))]
pub struct AssertEq<'ast> {
pub left: Expression<'ast>,
pub right: Expression<'ast>,
pub line_end: LineEnd,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,22 @@
use crate::{ast::Rule, common::{Assignee, LineEnd}, expressions::Expression, operations::AssignOperation};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement_assign))]
pub struct AssignStatement<'ast> {
pub assignee: Assignee<'ast>,
pub assign: AssignOperation,
pub expression: Expression<'ast>,
pub line_end: LineEnd,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for AssignStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} = {};", self.assignee, self.expression)
}
}

View File

@ -0,0 +1,22 @@
use crate::{ast::Rule, statements::{ConditionalStatement, Statement}};
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::conditional_nested_or_end_statement))]
pub enum ConditionalNestedOrEndStatement<'ast> {
Nested(Box<ConditionalStatement<'ast>>),
End(Vec<Statement<'ast>>),
}
impl<'ast> fmt::Display for ConditionalNestedOrEndStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ConditionalNestedOrEndStatement::Nested(ref nested) => write!(f, "else {}", nested),
ConditionalNestedOrEndStatement::End(ref statements) => {
write!(f, "else {{\n \t{:#?}\n }}", statements)
}
}
}
}

View File

@ -0,0 +1,26 @@
use crate::{ast::Rule, expressions::Expression, statements::{ConditionalNestedOrEndStatement, Statement}};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement_conditional))]
pub struct ConditionalStatement<'ast> {
pub condition: Expression<'ast>,
pub statements: Vec<Statement<'ast>>,
pub next: Option<ConditionalNestedOrEndStatement<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for ConditionalStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "if ({}) {{\n", self.condition)?;
write!(f, "\t{:#?}\n", self.statements)?;
self.next
.as_ref()
.map(|n_or_e| write!(f, "}} {}", n_or_e))
.unwrap_or(write!(f, "}}"))
}
}

View File

@ -0,0 +1,21 @@
use crate::{ast::Rule, common::{LineEnd, Variable}, expressions::Expression};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement_definition))]
pub struct DefinitionStatement<'ast> {
pub variable: Variable<'ast>,
pub expression: Expression<'ast>,
pub line_end: LineEnd,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for DefinitionStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "let {} = {};", self.variable, self.expression)
}
}

View File

@ -0,0 +1,13 @@
use crate::{ast::Rule, common::LineEnd, expressions::Expression};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement_expression))]
pub struct ExpressionStatement<'ast> {
pub expression: Expression<'ast>,
pub line_end: LineEnd,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,26 @@
use crate::{ast::Rule, expressions::{Expression}, statements::Statement, common::Identifier};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement_for))]
pub struct ForStatement<'ast> {
pub index: Identifier<'ast>,
pub start: Expression<'ast>,
pub stop: Expression<'ast>,
pub statements: Vec<Statement<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for ForStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"for {} in {}..{} {{ {:#?} }}",
self.index, self.start, self.stop, self.statements
)
}
}

29
ast/src/statements/mod.rs Normal file
View File

@ -0,0 +1,29 @@
pub mod assert_statement;
pub use assert_statement::*;
pub mod assign_statement;
pub use assign_statement::*;
pub mod conditional_statement;
pub use conditional_statement::*;
pub mod conditional_nested_or_end_statement;
pub use conditional_nested_or_end_statement::*;
pub mod definition_statement;
pub use definition_statement::*;
pub mod expression_statement;
pub use expression_statement::*;
pub mod for_statement;
pub use for_statement::*;
pub mod multiple_assignment_statement;
pub use multiple_assignment_statement::*;
pub mod return_statement;
pub use return_statement::*;
pub mod statement;
pub use statement::*;

View File

@ -0,0 +1,28 @@
use crate::{ast::Rule, common::{Identifier, LineEnd, Variable}, expressions::{Expression}};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement_multiple_assignment))]
pub struct MultipleAssignmentStatement<'ast> {
pub variables: Vec<Variable<'ast>>,
pub function_name: Identifier<'ast>,
pub arguments: Vec<Expression<'ast>>,
pub line_end: LineEnd,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for MultipleAssignmentStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, id) in self.variables.iter().enumerate() {
write!(f, "{}", id)?;
if i < self.variables.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, " = {}", self.function_name)
}
}

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::statement_return))]
pub struct ReturnStatement<'ast> {
pub expressions: Vec<Expression<'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, "")
}
}

View File

@ -0,0 +1,32 @@
use crate::{ast::Rule, statements::*};
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement))]
pub enum Statement<'ast> {
Return(ReturnStatement<'ast>),
Definition(DefinitionStatement<'ast>),
Assign(AssignStatement<'ast>),
MultipleAssignment(MultipleAssignmentStatement<'ast>),
Conditional(ConditionalStatement<'ast>),
Iteration(ForStatement<'ast>),
Assert(AssertStatement<'ast>),
Expression(ExpressionStatement<'ast>),
}
impl<'ast> fmt::Display for Statement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Statement::Return(ref statement) => write!(f, "{}", statement),
Statement::Definition(ref statement) => write!(f, "{}", statement),
Statement::Assign(ref statement) => write!(f, "{}", statement),
Statement::MultipleAssignment(ref statement) => write!(f, "{}", statement),
Statement::Conditional(ref statement) => write!(f, "{}", statement),
Statement::Iteration(ref statement) => write!(f, "{}", statement),
Statement::Assert(ref statement) => write!(f, "{}", statement),
Statement::Expression(ref statement) => write!(f, "{}", statement.expression),
}
}
}

View File

@ -0,0 +1,13 @@
use crate::{ast::Rule, types::DataType, values::Value};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_array))]
pub struct ArrayType<'ast> {
pub _type: DataType,
pub dimensions: Vec<Value<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,7 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_boolean))]
pub struct BooleanType {}

View File

@ -0,0 +1,12 @@
use crate::{ast::Rule, common::Identifier};
use pest::Span;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_circuit))]
pub struct CircuitType<'ast> {
pub identifier: Identifier<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -0,0 +1,12 @@
use crate::{ast::Rule, types::{IntegerType, FieldType, GroupType, BooleanType}};
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_data))]
pub enum DataType {
Integer(IntegerType),
Field(FieldType),
Group(GroupType),
Boolean(BooleanType),
}

View File

@ -0,0 +1,7 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_field))]
pub struct FieldType {}

View File

@ -0,0 +1,7 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_group))]
pub struct GroupType {}

View File

@ -0,0 +1,33 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_integer))]
pub enum IntegerType {
U8Type(U8Type),
U16Type(U16Type),
U32Type(U32Type),
U64Type(U64Type),
U128Type(U128Type),
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_u8))]
pub struct U8Type {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_u16))]
pub struct U16Type {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_u32))]
pub struct U32Type {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_u64))]
pub struct U64Type {}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_u128))]
pub struct U128Type {}

26
ast/src/types/mod.rs Normal file
View File

@ -0,0 +1,26 @@
pub mod array_type;
pub use array_type::*;
pub mod boolean_type;
pub use boolean_type::*;
pub mod circuit_type;
pub use circuit_type::*;
pub mod data_type;
pub use data_type::*;
pub mod field_type;
pub use field_type::*;
pub mod group_type;
pub use group_type::*;
pub mod integer_type;
pub use integer_type::*;
pub mod self_type;
pub use self_type::*;
pub mod type_;
pub use type_::*;

View File

@ -0,0 +1,7 @@
use crate::ast::Rule;
use pest_ast::FromPest;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_self))]
pub struct SelfType {}

24
ast/src/types/type_.rs Normal file
View File

@ -0,0 +1,24 @@
use crate::{ast::Rule, types::*};
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::type_))]
pub enum Type<'ast> {
Basic(DataType),
Array(ArrayType<'ast>),
Circuit(CircuitType<'ast>),
SelfType(SelfType),
}
impl<'ast> fmt::Display for Type<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Type::Basic(ref _type) => write!(f, "basic"),
Type::Array(ref _type) => write!(f, "array"),
Type::Circuit(ref _type) => write!(f, "struct"),
Type::SelfType(ref _type) => write!(f, "Self"),
}
}
}

View File

@ -0,0 +1,20 @@
use crate::ast::{Rule, span_into_string};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::value_boolean))]
pub struct BooleanValue<'ast> {
#[pest_ast(outer(with(span_into_string)))]
pub value: String,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for BooleanValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}

View File

@ -0,0 +1,20 @@
use crate::{ast::Rule, types::FieldType, values::NumberValue,};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::value_field))]
pub struct FieldValue<'ast> {
pub number: NumberValue<'ast>,
pub _type: FieldType,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for FieldValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.number)
}
}

View File

@ -0,0 +1,51 @@
use crate::{ast::Rule, types::GroupType, values::NumberValue,};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::value_group))]
pub struct GroupValue<'ast> {
pub value: GroupRepresentation<'ast>,
pub _type: GroupType,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for GroupValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::group_single_or_tuple))]
pub enum GroupRepresentation<'ast> {
Single(NumberValue<'ast>),
Tuple(GroupTuple<'ast>),
}
impl<'ast> fmt::Display for GroupRepresentation<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
GroupRepresentation::Single(number) => write!(f, "{}", number),
GroupRepresentation::Tuple(tuple) => write!(f, "{}", tuple),
}
}
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::group_tuple))]
pub struct GroupTuple<'ast> {
pub x: NumberValue<'ast>,
pub y: NumberValue<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for GroupTuple<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}

View File

@ -0,0 +1,20 @@
use crate::{ast::Rule, types::IntegerType, values::NumberValue};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::value_integer))]
pub struct IntegerValue<'ast> {
pub number: NumberValue<'ast>,
pub _type: IntegerType,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for IntegerValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.number)
}
}

20
ast/src/values/mod.rs Normal file
View File

@ -0,0 +1,20 @@
pub mod boolean_value;
pub use boolean_value::*;
pub mod field_value;
pub use field_value::*;
pub mod group_value;
pub use group_value::*;
pub mod integer_value;
pub use integer_value::*;
pub mod number_implicit_value;
pub use number_implicit_value::*;
pub mod number_value;
pub use number_value::*;
pub mod value;
pub use value::*;

View File

@ -0,0 +1,19 @@
use crate::{ast::Rule, values::NumberValue};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::value_implicit))]
pub struct NumberImplicitValue<'ast> {
pub number: NumberValue<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for NumberImplicitValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.number)
}
}

View File

@ -0,0 +1,20 @@
use crate::ast::{Rule, span_into_string};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::value_number))]
pub struct NumberValue<'ast> {
#[pest_ast(outer(with(span_into_string)))]
pub value: String,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for NumberValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}

39
ast/src/values/value.rs Normal file
View File

@ -0,0 +1,39 @@
use crate::{ast::Rule, values::{BooleanValue, IntegerValue, FieldValue, GroupValue, NumberImplicitValue}};
use pest::Span;
use pest_ast::FromPest;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::value))]
pub enum Value<'ast> {
Integer(IntegerValue<'ast>),
Field(FieldValue<'ast>),
Group(GroupValue<'ast>),
Boolean(BooleanValue<'ast>),
Implicit(NumberImplicitValue<'ast>),
}
impl<'ast> Value<'ast> {
pub fn span(&self) -> &Span<'ast> {
match self {
Value::Integer(value) => &value.span,
Value::Field(value) => &value.span,
Value::Group(value) => &value.span,
Value::Boolean(value) => &value.span,
Value::Implicit(value) => &value.span,
}
}
}
impl<'ast> fmt::Display for Value<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Value::Integer(ref value) => write!(f, "{}", value),
Value::Field(ref value) => write!(f, "{}", value),
Value::Group(ref value) => write!(f, "{}", value),
Value::Boolean(ref value) => write!(f, "{}", value),
Value::Implicit(ref value) => write!(f, "{}", value),
}
}
}

View File

@ -5,7 +5,7 @@ use crate::{
errors::CompilerError,
GroupType, InputValue, Program,
};
use leo_ast::ast;
use leo_ast::{ast, files::File};
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
@ -75,7 +75,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
generate_test_constraints::<F, G>(cs, self.program)
}
// pub fn compile(&self) -> Result<ast::File, CompilerError> {
// pub fn compile(&self) -> Result<File, CompilerError> {
// // Read in the main file as string
// let unparsed_file = fs::read_to_string(&self.main_file_path).map_err(|_| CompilerError::FileReadError(self.main_file_path.clone()))?;
//
@ -83,7 +83,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
// let mut file = ast::parse(&unparsed_file).map_err(|_| CompilerError::FileParsingError)?;
//
// // Build the abstract syntax tree
// let syntax_tree = ast::File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
// let syntax_tree = File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
// log::debug!("{:#?}", syntax_tree);
//
// Ok(syntax_tree)
@ -101,7 +101,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
// Build the abstract syntax tree
let syntax_tree =
ast::File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
log::debug!("{:#?}", syntax_tree);
// Build program from abstract syntax tree

View File

@ -5,7 +5,7 @@ use crate::{
types::Program,
GroupType, Import,
};
use leo_ast::ast;
use leo_ast::{ast, files::File};
use from_pest::FromPest;
use snarkos_models::{
@ -42,7 +42,7 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
// generate ast from file
let syntax_tree =
ast::File::from_pest(&mut file).map_err(|_| ImportError::SyntaxTreeError)?;
File::from_pest(&mut file).map_err(|_| ImportError::SyntaxTreeError)?;
// generate aleo program from file
let mut program = Program::from(syntax_tree, import.path_string.clone());

View File

@ -1,7 +1,80 @@
//! Logic to convert from an abstract syntax tree (ast) representation to a Leo program.
use crate::{types, Import, ImportSymbol};
use leo_ast::ast;
use leo_ast::{
File,
access::{
Access,
AssigneeAccess,
},
circuits::{
Circuit,
CircuitField,
CircuitFieldDefinition,
CircuitFunction,
CircuitMember
},
common::{
Assignee,
Identifier,
RangeOrExpression as AstRangeOrExpression,
SpreadOrExpression as AstSpreadOrExpression,
Variable as AstVariable,
Visibility,
Private,
},
expressions::{
ArrayInitializerExpression,
ArrayInlineExpression,
BinaryExpression,
CircuitInlineExpression,
Expression,
NotExpression,
PostfixExpression,
TernaryExpression
},
functions::{
Function,
FunctionInput,
TestFunction
},
imports::{
Import as AstImport,
ImportSymbol as AstImportSymbol,
},
operations::{
AssignOperation,
BinaryOperation,
},
statements::{
AssertStatement,
AssignStatement,
ConditionalStatement,
ConditionalNestedOrEndStatement,
DefinitionStatement,
ExpressionStatement,
ForStatement,
MultipleAssignmentStatement,
ReturnStatement,
Statement,
},
types::{
ArrayType,
CircuitType,
DataType,
IntegerType,
Type as AstType
},
values::{
BooleanValue,
FieldValue,
GroupValue,
IntegerValue,
NumberValue,
NumberImplicitValue,
Value
}
};
use snarkos_models::gadgets::utilities::{
boolean::Boolean,
@ -11,22 +84,22 @@ use std::collections::HashMap;
/// pest ast -> types::Identifier
impl<'ast> From<ast::Identifier<'ast>> for types::Identifier {
fn from(identifier: ast::Identifier<'ast>) -> Self {
impl<'ast> From<Identifier<'ast>> for types::Identifier {
fn from(identifier: Identifier<'ast>) -> Self {
types::Identifier::new(identifier.value)
}
}
impl<'ast> From<ast::Identifier<'ast>> for types::Expression {
fn from(identifier: ast::Identifier<'ast>) -> Self {
impl<'ast> From<Identifier<'ast>> for types::Expression {
fn from(identifier: Identifier<'ast>) -> Self {
types::Expression::Identifier(types::Identifier::from(identifier))
}
}
/// pest ast -> types::Variable
impl<'ast> From<ast::Variable<'ast>> for types::Variable {
fn from(variable: ast::Variable<'ast>) -> Self {
impl<'ast> From<AstVariable<'ast>> for types::Variable {
fn from(variable: AstVariable<'ast>) -> Self {
types::Variable {
identifier: types::Identifier::from(variable.identifier),
mutable: variable.mutable.is_some(),
@ -38,24 +111,24 @@ impl<'ast> From<ast::Variable<'ast>> for types::Variable {
/// pest ast - types::Integer
impl<'ast> types::Integer {
pub(crate) fn from(number: ast::Number<'ast>, _type: ast::IntegerType) -> Self {
pub(crate) fn from(number: NumberValue<'ast>, _type: IntegerType) -> Self {
match _type {
ast::IntegerType::U8Type(_u8) => types::Integer::U8(UInt8::constant(
IntegerType::U8Type(_u8) => types::Integer::U8(UInt8::constant(
number.value.parse::<u8>().expect("unable to parse u8"),
)),
ast::IntegerType::U16Type(_u16) => types::Integer::U16(UInt16::constant(
IntegerType::U16Type(_u16) => types::Integer::U16(UInt16::constant(
number.value.parse::<u16>().expect("unable to parse u16"),
)),
ast::IntegerType::U32Type(_u32) => types::Integer::U32(UInt32::constant(
IntegerType::U32Type(_u32) => types::Integer::U32(UInt32::constant(
number
.value
.parse::<u32>()
.expect("unable to parse integers.u32"),
)),
ast::IntegerType::U64Type(_u64) => types::Integer::U64(UInt64::constant(
IntegerType::U64Type(_u64) => types::Integer::U64(UInt64::constant(
number.value.parse::<u64>().expect("unable to parse u64"),
)),
ast::IntegerType::U128Type(_u128) => types::Integer::U128(UInt128::constant(
IntegerType::U128Type(_u128) => types::Integer::U128(UInt128::constant(
number.value.parse::<u128>().expect("unable to parse u128"),
)),
}
@ -68,16 +141,16 @@ impl<'ast> types::Integer {
}
}
impl<'ast> From<ast::Integer<'ast>> for types::Expression {
fn from(field: ast::Integer<'ast>) -> Self {
impl<'ast> From<IntegerValue<'ast>> for types::Expression {
fn from(field: IntegerValue<'ast>) -> Self {
types::Expression::Integer(types::Integer::from(field.number, field._type))
}
}
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) {
@ -99,7 +172,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))
}
}
@ -108,24 +181,24 @@ impl<'ast> From<ast::RangeOrExpression<'ast>> for types::RangeOrExpression {
/// pest ast -> types::Field
impl<'ast> From<ast::Field<'ast>> for types::Expression {
fn from(field: ast::Field<'ast>) -> Self {
impl<'ast> From<FieldValue<'ast>> for types::Expression {
fn from(field: FieldValue<'ast>) -> Self {
types::Expression::Field(field.number.value)
}
}
/// pest ast -> types::Group
impl<'ast> From<ast::Group<'ast>> for types::Expression {
fn from(group: ast::Group<'ast>) -> Self {
impl<'ast> From<GroupValue<'ast>> for types::Expression {
fn from(group: GroupValue<'ast>) -> Self {
types::Expression::Group(group.to_string())
}
}
/// pest ast -> types::Boolean
impl<'ast> From<ast::Boolean<'ast>> for types::Expression {
fn from(boolean: ast::Boolean<'ast>) -> Self {
impl<'ast> From<BooleanValue<'ast>> for types::Expression {
fn from(boolean: BooleanValue<'ast>) -> Self {
types::Expression::Boolean(Boolean::Constant(
boolean
.value
@ -137,98 +210,98 @@ impl<'ast> From<ast::Boolean<'ast>> for types::Expression {
/// pest ast -> types::NumberImplicit
impl<'ast> From<ast::NumberImplicit<'ast>> for types::Expression {
fn from(number: ast::NumberImplicit<'ast>) -> Self {
impl<'ast> From<NumberImplicitValue<'ast>> for types::Expression {
fn from(number: NumberImplicitValue<'ast>) -> Self {
types::Expression::Implicit(number.number.value)
}
}
/// pest ast -> types::Expression
impl<'ast> From<ast::Value<'ast>> for types::Expression {
fn from(value: ast::Value<'ast>) -> Self {
impl<'ast> From<Value<'ast>> for types::Expression {
fn from(value: Value<'ast>) -> Self {
match value {
ast::Value::Integer(num) => types::Expression::from(num),
ast::Value::Field(field) => types::Expression::from(field),
ast::Value::Group(group) => types::Expression::from(group),
ast::Value::Boolean(bool) => types::Expression::from(bool),
ast::Value::Implicit(value) => types::Expression::from(value),
Value::Integer(num) => types::Expression::from(num),
Value::Field(field) => types::Expression::from(field),
Value::Group(group) => types::Expression::from(group),
Value::Boolean(bool) => types::Expression::from(bool),
Value::Implicit(value) => types::Expression::from(value),
}
}
}
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)))
}
}
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))
}
}
}
}
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
ast::BinaryOperator::Or => types::Expression::Or(
BinaryOperation::Or => types::Expression::Or(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::And => types::Expression::And(
BinaryOperation::And => types::Expression::And(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::Eq => types::Expression::Eq(
BinaryOperation::Eq => types::Expression::Eq(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::Ne => {
BinaryOperation::Ne => {
types::Expression::Not(Box::new(types::Expression::from(expression)))
}
ast::BinaryOperator::Ge => types::Expression::Ge(
BinaryOperation::Ge => types::Expression::Ge(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::Gt => types::Expression::Gt(
BinaryOperation::Gt => types::Expression::Gt(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::Le => types::Expression::Le(
BinaryOperation::Le => types::Expression::Le(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::Lt => types::Expression::Lt(
BinaryOperation::Lt => types::Expression::Lt(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
// Number operations
ast::BinaryOperator::Add => types::Expression::Add(
BinaryOperation::Add => types::Expression::Add(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::Sub => types::Expression::Sub(
BinaryOperation::Sub => types::Expression::Sub(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::Mul => types::Expression::Mul(
BinaryOperation::Mul => types::Expression::Mul(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::Div => types::Expression::Div(
BinaryOperation::Div => types::Expression::Div(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
ast::BinaryOperator::Pow => types::Expression::Pow(
BinaryOperation::Pow => types::Expression::Pow(
Box::new(types::Expression::from(*expression.left)),
Box::new(types::Expression::from(*expression.right)),
),
@ -236,8 +309,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)),
@ -246,8 +319,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
@ -257,8 +330,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));
@ -266,8 +339,8 @@ impl<'ast> From<ast::ArrayInitializerExpression<'ast>> for types::Expression {
}
}
impl<'ast> From<ast::CircuitField<'ast>> for types::CircuitFieldDefinition {
fn from(member: ast::CircuitField<'ast>) -> Self {
impl<'ast> From<CircuitField<'ast>> for types::CircuitFieldDefinition {
fn from(member: CircuitField<'ast>) -> Self {
types::CircuitFieldDefinition {
identifier: types::Identifier::from(member.identifier),
expression: types::Expression::from(member.expression),
@ -275,8 +348,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
@ -288,8 +361,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));
@ -302,13 +375,13 @@ impl<'ast> From<ast::PostfixExpression<'ast>> for types::Expression {
.into_iter()
.fold(variable, |acc, access| match access {
// Handle array accesses
ast::Access::Array(array) => types::Expression::ArrayAccess(
Access::Array(array) => types::Expression::ArrayAccess(
Box::new(acc),
Box::new(types::RangeOrExpression::from(array.expression)),
),
// Handle function calls
ast::Access::Call(function) => types::Expression::FunctionCall(
Access::Call(function) => types::Expression::FunctionCall(
Box::new(acc),
function
.expressions
@ -318,11 +391,11 @@ impl<'ast> From<ast::PostfixExpression<'ast>> for types::Expression {
),
// Handle circuit member accesses
ast::Access::Object(circuit_object) => types::Expression::CircuitMemberAccess(
Access::Object(circuit_object) => types::Expression::CircuitMemberAccess(
Box::new(acc),
types::Identifier::from(circuit_object.identifier),
),
ast::Access::StaticObject(circuit_object) => {
Access::StaticObject(circuit_object) => {
types::Expression::CircuitStaticFunctionAccess(
Box::new(acc),
types::Identifier::from(circuit_object.identifier),
@ -332,31 +405,31 @@ 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),
}
}
}
impl<'ast> types::Expression {
fn get_count(count: ast::Value<'ast>) -> usize {
fn get_count(count: Value<'ast>) -> usize {
match count {
ast::Value::Integer(integer) => integer
Value::Integer(integer) => integer
.number
.value
.parse::<usize>()
.expect("Unable to read array size"),
ast::Value::Implicit(number) => number
Value::Implicit(number) => number
.number
.value
.parse::<usize>()
@ -366,9 +439,9 @@ impl<'ast> types::Expression {
}
}
// ast::Assignee -> types::Expression for operator assign statements
impl<'ast> From<ast::Assignee<'ast>> for types::Expression {
fn from(assignee: ast::Assignee<'ast>) -> Self {
// Assignee -> types::Expression for operator assign statements
impl<'ast> From<Assignee<'ast>> for types::Expression {
fn from(assignee: Assignee<'ast>) -> Self {
let variable = types::Expression::Identifier(types::Identifier::from(assignee.identifier));
// we start with the id, and we fold the array of accesses by wrapping the current value
@ -376,13 +449,13 @@ impl<'ast> From<ast::Assignee<'ast>> for types::Expression {
.accesses
.into_iter()
.fold(variable, |acc, access| match access {
ast::AssigneeAccess::Member(circuit_member) => {
AssigneeAccess::Member(circuit_member) => {
types::Expression::CircuitMemberAccess(
Box::new(acc),
types::Identifier::from(circuit_member.identifier),
)
}
ast::AssigneeAccess::Array(array) => types::Expression::ArrayAccess(
AssigneeAccess::Array(array) => types::Expression::ArrayAccess(
Box::new(acc),
Box::new(types::RangeOrExpression::from(array.expression)),
),
@ -392,14 +465,14 @@ impl<'ast> From<ast::Assignee<'ast>> for types::Expression {
/// pest ast -> types::Assignee
impl<'ast> From<ast::Identifier<'ast>> for types::Assignee {
fn from(variable: ast::Identifier<'ast>) -> Self {
impl<'ast> From<Identifier<'ast>> for types::Assignee {
fn from(variable: Identifier<'ast>) -> Self {
types::Assignee::Identifier(types::Identifier::from(variable))
}
}
impl<'ast> From<ast::Assignee<'ast>> for types::Assignee {
fn from(assignee: ast::Assignee<'ast>) -> Self {
impl<'ast> From<Assignee<'ast>> for types::Assignee {
fn from(assignee: Assignee<'ast>) -> Self {
let variable = types::Assignee::from(assignee.identifier);
// we start with the id, and we fold the array of accesses by wrapping the current value
@ -407,11 +480,11 @@ impl<'ast> From<ast::Assignee<'ast>> for types::Assignee {
.accesses
.into_iter()
.fold(variable, |acc, access| match access {
ast::AssigneeAccess::Array(array) => types::Assignee::Array(
AssigneeAccess::Array(array) => types::Assignee::Array(
Box::new(acc),
types::RangeOrExpression::from(array.expression),
),
ast::AssigneeAccess::Member(circuit_field) => types::Assignee::CircuitField(
AssigneeAccess::Member(circuit_field) => types::Assignee::CircuitField(
Box::new(acc),
types::Identifier::from(circuit_field.identifier),
),
@ -421,8 +494,8 @@ impl<'ast> From<ast::Assignee<'ast>> for types::Assignee {
/// pest ast -> types::Statement
impl<'ast> From<ast::ReturnStatement<'ast>> for types::Statement {
fn from(statement: ast::ReturnStatement<'ast>) -> Self {
impl<'ast> From<ReturnStatement<'ast>> for types::Statement {
fn from(statement: ReturnStatement<'ast>) -> Self {
types::Statement::Return(
statement
.expressions
@ -433,8 +506,8 @@ impl<'ast> From<ast::ReturnStatement<'ast>> for types::Statement {
}
}
impl<'ast> From<ast::DefinitionStatement<'ast>> for types::Statement {
fn from(statement: ast::DefinitionStatement<'ast>) -> Self {
impl<'ast> From<DefinitionStatement<'ast>> for types::Statement {
fn from(statement: DefinitionStatement<'ast>) -> Self {
types::Statement::Definition(
types::Variable::from(statement.variable),
types::Expression::from(statement.expression),
@ -442,10 +515,10 @@ impl<'ast> From<ast::DefinitionStatement<'ast>> for types::Statement {
}
}
impl<'ast> From<ast::AssignStatement<'ast>> for types::Statement {
fn from(statement: ast::AssignStatement<'ast>) -> Self {
impl<'ast> From<AssignStatement<'ast>> for types::Statement {
fn from(statement: AssignStatement<'ast>) -> Self {
match statement.assign {
ast::OperationAssign::Assign(ref _assign) => types::Statement::Assign(
AssignOperation::Assign(ref _assign) => types::Statement::Assign(
types::Assignee::from(statement.assignee),
types::Expression::from(statement.expression),
),
@ -454,42 +527,42 @@ impl<'ast> From<ast::AssignStatement<'ast>> for types::Statement {
let converted = types::Expression::from(statement.assignee.clone());
match operation_assign {
ast::OperationAssign::AddAssign(ref _assign) => types::Statement::Assign(
AssignOperation::AddAssign(ref _assign) => types::Statement::Assign(
types::Assignee::from(statement.assignee),
types::Expression::Add(
Box::new(converted),
Box::new(types::Expression::from(statement.expression)),
),
),
ast::OperationAssign::SubAssign(ref _assign) => types::Statement::Assign(
AssignOperation::SubAssign(ref _assign) => types::Statement::Assign(
types::Assignee::from(statement.assignee),
types::Expression::Sub(
Box::new(converted),
Box::new(types::Expression::from(statement.expression)),
),
),
ast::OperationAssign::MulAssign(ref _assign) => types::Statement::Assign(
AssignOperation::MulAssign(ref _assign) => types::Statement::Assign(
types::Assignee::from(statement.assignee),
types::Expression::Mul(
Box::new(converted),
Box::new(types::Expression::from(statement.expression)),
),
),
ast::OperationAssign::DivAssign(ref _assign) => types::Statement::Assign(
AssignOperation::DivAssign(ref _assign) => types::Statement::Assign(
types::Assignee::from(statement.assignee),
types::Expression::Div(
Box::new(converted),
Box::new(types::Expression::from(statement.expression)),
),
),
ast::OperationAssign::PowAssign(ref _assign) => types::Statement::Assign(
AssignOperation::PowAssign(ref _assign) => types::Statement::Assign(
types::Assignee::from(statement.assignee),
types::Expression::Pow(
Box::new(converted),
Box::new(types::Expression::from(statement.expression)),
),
),
ast::OperationAssign::Assign(ref _assign) => {
AssignOperation::Assign(ref _assign) => {
unimplemented!("cannot assign twice to assign statement")
}
}
@ -498,8 +571,8 @@ impl<'ast> From<ast::AssignStatement<'ast>> for types::Statement {
}
}
impl<'ast> From<ast::MultipleAssignmentStatement<'ast>> for types::Statement {
fn from(statement: ast::MultipleAssignmentStatement<'ast>) -> Self {
impl<'ast> From<MultipleAssignmentStatement<'ast>> for types::Statement {
fn from(statement: MultipleAssignmentStatement<'ast>) -> Self {
let variables = statement
.variables
.into_iter()
@ -520,13 +593,13 @@ impl<'ast> From<ast::MultipleAssignmentStatement<'ast>> for types::Statement {
}
}
impl<'ast> From<ast::ConditionalNestedOrEnd<'ast>> for types::ConditionalNestedOrEnd {
fn from(statement: ast::ConditionalNestedOrEnd<'ast>) -> Self {
impl<'ast> From<ConditionalNestedOrEndStatement<'ast>> for types::ConditionalNestedOrEnd {
fn from(statement: ConditionalNestedOrEndStatement<'ast>) -> Self {
match statement {
ast::ConditionalNestedOrEnd::Nested(nested) => types::ConditionalNestedOrEnd::Nested(
ConditionalNestedOrEndStatement::Nested(nested) => types::ConditionalNestedOrEnd::Nested(
Box::new(types::ConditionalStatement::from(*nested)),
),
ast::ConditionalNestedOrEnd::End(statements) => types::ConditionalNestedOrEnd::End(
ConditionalNestedOrEndStatement::End(statements) => types::ConditionalNestedOrEnd::End(
statements
.into_iter()
.map(|statement| types::Statement::from(statement))
@ -536,8 +609,8 @@ impl<'ast> From<ast::ConditionalNestedOrEnd<'ast>> for types::ConditionalNestedO
}
}
impl<'ast> From<ast::ConditionalStatement<'ast>> for types::ConditionalStatement {
fn from(statement: ast::ConditionalStatement<'ast>) -> Self {
impl<'ast> From<ConditionalStatement<'ast>> for types::ConditionalStatement {
fn from(statement: ConditionalStatement<'ast>) -> Self {
types::ConditionalStatement {
condition: types::Expression::from(statement.condition),
statements: statement
@ -553,8 +626,8 @@ impl<'ast> From<ast::ConditionalStatement<'ast>> for types::ConditionalStatement
}
}
impl<'ast> From<ast::ForStatement<'ast>> for types::Statement {
fn from(statement: ast::ForStatement<'ast>) -> Self {
impl<'ast> From<ForStatement<'ast>> for types::Statement {
fn from(statement: ForStatement<'ast>) -> Self {
let from = match types::Expression::from(statement.start) {
types::Expression::Integer(number) => number,
types::Expression::Implicit(string) => types::Integer::from_implicit(string),
@ -579,10 +652,10 @@ impl<'ast> From<ast::ForStatement<'ast>> for types::Statement {
}
}
impl<'ast> From<ast::AssertStatement<'ast>> for types::Statement {
fn from(statement: ast::AssertStatement<'ast>) -> Self {
impl<'ast> From<AssertStatement<'ast>> for types::Statement {
fn from(statement: AssertStatement<'ast>) -> Self {
match statement {
ast::AssertStatement::AssertEq(assert_eq) => types::Statement::AssertEq(
AssertStatement::AssertEq(assert_eq) => types::Statement::AssertEq(
types::Expression::from(assert_eq.left),
types::Expression::from(assert_eq.right),
),
@ -590,58 +663,58 @@ impl<'ast> From<ast::AssertStatement<'ast>> for types::Statement {
}
}
impl<'ast> From<ast::ExpressionStatement<'ast>> for types::Statement {
fn from(statement: ast::ExpressionStatement<'ast>) -> Self {
impl<'ast> From<ExpressionStatement<'ast>> for types::Statement {
fn from(statement: ExpressionStatement<'ast>) -> Self {
types::Statement::Expression(types::Expression::from(statement.expression))
}
}
impl<'ast> From<ast::Statement<'ast>> for types::Statement {
fn from(statement: ast::Statement<'ast>) -> Self {
impl<'ast> From<Statement<'ast>> for types::Statement {
fn from(statement: Statement<'ast>) -> Self {
match statement {
ast::Statement::Return(statement) => types::Statement::from(statement),
ast::Statement::Definition(statement) => types::Statement::from(statement),
ast::Statement::Assign(statement) => types::Statement::from(statement),
ast::Statement::MultipleAssignment(statement) => types::Statement::from(statement),
ast::Statement::Conditional(statement) => {
Statement::Return(statement) => types::Statement::from(statement),
Statement::Definition(statement) => types::Statement::from(statement),
Statement::Assign(statement) => types::Statement::from(statement),
Statement::MultipleAssignment(statement) => types::Statement::from(statement),
Statement::Conditional(statement) => {
types::Statement::Conditional(types::ConditionalStatement::from(statement))
}
ast::Statement::Iteration(statement) => types::Statement::from(statement),
ast::Statement::Assert(statement) => types::Statement::from(statement),
ast::Statement::Expression(statement) => types::Statement::from(statement),
Statement::Iteration(statement) => types::Statement::from(statement),
Statement::Assert(statement) => types::Statement::from(statement),
Statement::Expression(statement) => types::Statement::from(statement),
}
}
}
/// pest ast -> Explicit types::Type for defining circuit members and function params
impl From<ast::IntegerType> for types::IntegerType {
fn from(integer_type: ast::IntegerType) -> Self {
impl From<IntegerType> for types::IntegerType {
fn from(integer_type: IntegerType) -> Self {
match integer_type {
ast::IntegerType::U8Type(_type) => types::IntegerType::U8,
ast::IntegerType::U16Type(_type) => types::IntegerType::U16,
ast::IntegerType::U32Type(_type) => types::IntegerType::U32,
ast::IntegerType::U64Type(_type) => types::IntegerType::U64,
ast::IntegerType::U128Type(_type) => types::IntegerType::U128,
IntegerType::U8Type(_type) => types::IntegerType::U8,
IntegerType::U16Type(_type) => types::IntegerType::U16,
IntegerType::U32Type(_type) => types::IntegerType::U32,
IntegerType::U64Type(_type) => types::IntegerType::U64,
IntegerType::U128Type(_type) => types::IntegerType::U128,
}
}
}
impl From<ast::BasicType> for types::Type {
fn from(basic_type: ast::BasicType) -> Self {
impl From<DataType> for types::Type {
fn from(basic_type: DataType) -> Self {
match basic_type {
ast::BasicType::Integer(_type) => {
DataType::Integer(_type) => {
types::Type::IntegerType(types::IntegerType::from(_type))
}
ast::BasicType::Field(_type) => types::Type::Field,
ast::BasicType::Group(_type) => types::Type::Group,
ast::BasicType::Boolean(_type) => types::Type::Boolean,
DataType::Field(_type) => types::Type::Field,
DataType::Group(_type) => types::Type::Group,
DataType::Boolean(_type) => types::Type::Boolean,
}
}
}
impl<'ast> From<ast::ArrayType<'ast>> for types::Type {
fn from(array_type: ast::ArrayType<'ast>) -> Self {
impl<'ast> From<ArrayType<'ast>> for types::Type {
fn from(array_type: ArrayType<'ast>) -> Self {
let element_type = Box::new(types::Type::from(array_type._type));
let dimensions = array_type
.dimensions
@ -653,27 +726,27 @@ impl<'ast> From<ast::ArrayType<'ast>> for types::Type {
}
}
impl<'ast> From<ast::CircuitType<'ast>> for types::Type {
fn from(circuit_type: ast::CircuitType<'ast>) -> Self {
impl<'ast> From<CircuitType<'ast>> for types::Type {
fn from(circuit_type: CircuitType<'ast>) -> Self {
types::Type::Circuit(types::Identifier::from(circuit_type.identifier))
}
}
impl<'ast> From<ast::Type<'ast>> for types::Type {
fn from(_type: ast::Type<'ast>) -> Self {
impl<'ast> From<AstType<'ast>> for types::Type {
fn from(_type: AstType<'ast>) -> Self {
match _type {
ast::Type::Basic(_type) => types::Type::from(_type),
ast::Type::Array(_type) => types::Type::from(_type),
ast::Type::Circuit(_type) => types::Type::from(_type),
ast::Type::SelfType(_type) => types::Type::SelfType,
AstType::Basic(_type) => types::Type::from(_type),
AstType::Array(_type) => types::Type::from(_type),
AstType::Circuit(_type) => types::Type::from(_type),
AstType::SelfType(_type) => types::Type::SelfType,
}
}
}
/// pest ast -> types::Circuit
impl<'ast> From<ast::CircuitFieldDefinition<'ast>> for types::CircuitMember {
fn from(circuit_value: ast::CircuitFieldDefinition<'ast>) -> Self {
impl<'ast> From<CircuitFieldDefinition<'ast>> for types::CircuitMember {
fn from(circuit_value: CircuitFieldDefinition<'ast>) -> Self {
types::CircuitMember::CircuitField(
types::Identifier::from(circuit_value.identifier),
types::Type::from(circuit_value._type),
@ -681,8 +754,8 @@ impl<'ast> From<ast::CircuitFieldDefinition<'ast>> for types::CircuitMember {
}
}
impl<'ast> From<ast::CircuitFunction<'ast>> for types::CircuitMember {
fn from(circuit_function: ast::CircuitFunction<'ast>) -> Self {
impl<'ast> From<CircuitFunction<'ast>> for types::CircuitMember {
fn from(circuit_function: CircuitFunction<'ast>) -> Self {
types::CircuitMember::CircuitFunction(
circuit_function._static.is_some(),
types::Function::from(circuit_function.function),
@ -690,21 +763,21 @@ impl<'ast> From<ast::CircuitFunction<'ast>> for types::CircuitMember {
}
}
impl<'ast> From<ast::CircuitMember<'ast>> for types::CircuitMember {
fn from(object: ast::CircuitMember<'ast>) -> Self {
impl<'ast> From<CircuitMember<'ast>> for types::CircuitMember {
fn from(object: CircuitMember<'ast>) -> Self {
match object {
ast::CircuitMember::CircuitFieldDefinition(circuit_value) => {
CircuitMember::CircuitFieldDefinition(circuit_value) => {
types::CircuitMember::from(circuit_value)
}
ast::CircuitMember::CircuitFunction(circuit_function) => {
CircuitMember::CircuitFunction(circuit_function) => {
types::CircuitMember::from(circuit_function)
}
}
}
}
impl<'ast> From<ast::Circuit<'ast>> for types::Circuit {
fn from(circuit: ast::Circuit<'ast>) -> Self {
impl<'ast> From<Circuit<'ast>> for types::Circuit {
fn from(circuit: Circuit<'ast>) -> Self {
let variable = types::Identifier::from(circuit.identifier);
let members = circuit
.members
@ -721,14 +794,14 @@ impl<'ast> From<ast::Circuit<'ast>> for types::Circuit {
/// pest ast -> function types::Parameters
impl<'ast> From<ast::InputModel<'ast>> for types::InputModel {
fn from(parameter: ast::InputModel<'ast>) -> Self {
impl<'ast> From<FunctionInput<'ast>> for types::InputModel {
fn from(parameter: FunctionInput<'ast>) -> Self {
types::InputModel {
identifier: types::Identifier::from(parameter.identifier),
mutable: parameter.mutable.is_some(),
// private by default
private: parameter.visibility.map_or(true, |visibility| {
visibility.eq(&ast::Visibility::Private(ast::Private {}))
visibility.eq(&Visibility::Private(Private {}))
}),
_type: types::Type::from(parameter._type),
}
@ -737,8 +810,8 @@ impl<'ast> From<ast::InputModel<'ast>> for types::InputModel {
/// pest ast -> types::Function
impl<'ast> From<ast::Function<'ast>> for types::Function {
fn from(function_definition: ast::Function<'ast>) -> Self {
impl<'ast> From<Function<'ast>> for types::Function {
fn from(function_definition: Function<'ast>) -> Self {
let function_name = types::Identifier::from(function_definition.function_name);
let parameters = function_definition
.parameters
@ -767,8 +840,8 @@ impl<'ast> From<ast::Function<'ast>> for types::Function {
/// pest ast -> Import
impl<'ast> From<ast::ImportSymbol<'ast>> for ImportSymbol {
fn from(symbol: ast::ImportSymbol<'ast>) -> Self {
impl<'ast> From<AstImportSymbol<'ast>> for ImportSymbol {
fn from(symbol: AstImportSymbol<'ast>) -> Self {
ImportSymbol {
symbol: types::Identifier::from(symbol.value),
alias: symbol.alias.map(|alias| types::Identifier::from(alias)),
@ -776,8 +849,8 @@ impl<'ast> From<ast::ImportSymbol<'ast>> for ImportSymbol {
}
}
impl<'ast> From<ast::Import<'ast>> for Import {
fn from(import: ast::Import<'ast>) -> Self {
impl<'ast> From<AstImport<'ast>> for Import {
fn from(import: AstImport<'ast>) -> Self {
Import {
path_string: import.source.value,
symbols: import
@ -790,8 +863,8 @@ impl<'ast> From<ast::Import<'ast>> for Import {
}
/// pest ast -> Test
impl<'ast> From<ast::Test<'ast>> for types::Test {
fn from(test: ast::Test) -> Self {
impl<'ast> From<TestFunction<'ast>> for types::Test {
fn from(test: TestFunction) -> Self {
types::Test(types::Function::from(test.function))
}
}
@ -799,7 +872,7 @@ impl<'ast> From<ast::Test<'ast>> for types::Test {
/// pest ast -> types::Program
impl<'ast> types::Program {
pub fn from(file: ast::File<'ast>, name: String) -> Self {
pub fn from(file: File<'ast>, name: String) -> Self {
// Compiled ast -> aleo program representation
let imports = file
.imports