Migrate assignee

This commit is contained in:
howardwu 2020-06-07 17:05:21 -07:00
parent 6d9d92dcaf
commit eb5c3864a7
6 changed files with 40 additions and 32 deletions

View File

@ -52,30 +52,6 @@ lazy_static! {
static ref PRECEDENCE_CLIMBER: PrecClimber<Rule> = precedence_climber();
}
// Access
#[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, "")
}
}
// Expressions
fn precedence_climber() -> PrecClimber<Rule> {

View File

@ -0,0 +1,27 @@
use crate::{access::AssigneeAccess, ast::Rule, common::Identifier, types::Type};
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, "")
}
}

View File

@ -1,3 +1,6 @@
pub mod assignee;
pub use assignee::*;
pub mod identifier;
pub use identifier::*;

View File

@ -1,5 +1,8 @@
/// Common
// Declared in common/assignee.rs
assignee = { identifier ~ access_assignee* }
// 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" }
@ -161,8 +164,6 @@ access_member = { "." ~ identifier }
// Declared in access/static_member_access.rs
access_static_member = { "::" ~ identifier }
assignee = { identifier ~ access_assignee* }
/// Circuits
// Declared in circuits/circuit_definition.rs

View File

@ -1,4 +1,4 @@
use crate::{ast::{Rule, LineEnd, Assignee}, expressions::Expression, operations::AssignOperation};
use crate::{ast::{Rule, LineEnd}, common::Assignee, expressions::Expression, operations::AssignOperation};
use pest::Span;
use pest_ast::FromPest;

View File

@ -15,6 +15,7 @@ use leo_ast::{
CircuitMember
},
common::{
Assignee,
Identifier,
RangeOrExpression as AstRangeOrExpression,
SpreadOrExpression as AstSpreadOrExpression,
@ -433,9 +434,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
@ -465,8 +466,8 @@ impl<'ast> From<Identifier<'ast>> for types::Assignee {
}
}
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