From 59570002f2c499315d8bac0894f65f101c37c730 Mon Sep 17 00:00:00 2001 From: collin Date: Sat, 5 Sep 2020 22:03:02 -0700 Subject: [PATCH] mutable self wip --- ast/src/common/assignee.rs | 6 ++-- ast/src/common/keyword_or_identifier.rs | 43 +++++++++++++++++++++++ ast/src/common/mod.rs | 3 ++ ast/src/common/self_keyword.rs | 7 ++++ ast/src/expressions/postfix_expression.rs | 16 +-------- ast/src/functions/input/input_keyword.rs | 7 ++++ ast/src/leo.pest | 2 +- compiler/src/statement/assign/assign.rs | 4 +-- typed/src/common/assignee.rs | 10 ++++-- typed/src/common/identifier.rs | 4 +-- typed/src/expression.rs | 2 +- 11 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 ast/src/common/keyword_or_identifier.rs diff --git a/ast/src/common/assignee.rs b/ast/src/common/assignee.rs index 4019f9d6cd..a11b442f29 100644 --- a/ast/src/common/assignee.rs +++ b/ast/src/common/assignee.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{access::AssigneeAccess, ast::Rule, common::Identifier, SpanDef}; +use crate::{access::AssigneeAccess, ast::Rule, common::KeywordOrIdentifier, SpanDef}; use pest::Span; use pest_ast::FromPest; @@ -24,7 +24,7 @@ use std::fmt; #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[pest_ast(rule(Rule::assignee))] pub struct Assignee<'ast> { - pub identifier: Identifier<'ast>, + pub name: KeywordOrIdentifier<'ast>, pub accesses: Vec>, #[pest_ast(outer())] #[serde(with = "SpanDef")] @@ -33,7 +33,7 @@ pub struct Assignee<'ast> { impl<'ast> fmt::Display for Assignee<'ast> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.identifier)?; + write!(f, "{}", self.name)?; for (i, access) in self.accesses.iter().enumerate() { write!(f, "{}", access)?; if i < self.accesses.len() - 1 { diff --git a/ast/src/common/keyword_or_identifier.rs b/ast/src/common/keyword_or_identifier.rs new file mode 100644 index 0000000000..af013bc24b --- /dev/null +++ b/ast/src/common/keyword_or_identifier.rs @@ -0,0 +1,43 @@ +// Copyright (C) 2019-2020 Aleo Systems Inc. +// This file is part of the Leo library. + +// The Leo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Leo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Leo library. If not, see . + +use crate::{ + ast::Rule, + common::{Identifier, SelfKeyword}, + functions::InputKeyword, +}; + +use pest_ast::FromPest; +use serde::Serialize; +use std::fmt; + +#[derive(Clone, Debug, FromPest, PartialEq, Serialize)] +#[pest_ast(rule(Rule::keyword_or_identifier))] +pub enum KeywordOrIdentifier<'ast> { + SelfKeyword(SelfKeyword<'ast>), + Input(InputKeyword<'ast>), + Identifier(Identifier<'ast>), +} + +impl<'ast> fmt::Display for KeywordOrIdentifier<'ast> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + KeywordOrIdentifier::SelfKeyword(self_keyword) => write!(f, "{}", self_keyword), + KeywordOrIdentifier::Input(input_keyword) => write!(f, "{}", input_keyword), + KeywordOrIdentifier::Identifier(identifier) => write!(f, "{}", identifier), + } + } +} diff --git a/ast/src/common/mod.rs b/ast/src/common/mod.rs index 870e83a030..7029423a3e 100644 --- a/ast/src/common/mod.rs +++ b/ast/src/common/mod.rs @@ -26,6 +26,9 @@ pub use eoi::*; pub mod identifier; pub use identifier::*; +pub mod keyword_or_identifier; +pub use keyword_or_identifier::*; + pub mod line_end; pub use line_end::*; diff --git a/ast/src/common/self_keyword.rs b/ast/src/common/self_keyword.rs index 50757fe449..278f125e0c 100644 --- a/ast/src/common/self_keyword.rs +++ b/ast/src/common/self_keyword.rs @@ -22,6 +22,7 @@ use crate::{ use pest::Span; use pest_ast::FromPest; use serde::Serialize; +use std::fmt; #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[pest_ast(rule(Rule::self_keyword))] @@ -32,3 +33,9 @@ pub struct SelfKeyword<'ast> { #[serde(with = "SpanDef")] pub span: Span<'ast>, } + +impl<'ast> fmt::Display for SelfKeyword<'ast> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.keyword) + } +} diff --git a/ast/src/expressions/postfix_expression.rs b/ast/src/expressions/postfix_expression.rs index a3938ad53e..8c260bd74c 100644 --- a/ast/src/expressions/postfix_expression.rs +++ b/ast/src/expressions/postfix_expression.rs @@ -14,13 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ - access::Access, - ast::Rule, - common::{Identifier, SelfKeyword}, - functions::InputKeyword, - SpanDef, -}; +use crate::{access::Access, ast::Rule, common::KeywordOrIdentifier, SpanDef}; use pest::Span; use pest_ast::FromPest; @@ -35,11 +29,3 @@ pub struct PostfixExpression<'ast> { #[serde(with = "SpanDef")] pub span: Span<'ast>, } - -#[derive(Clone, Debug, FromPest, PartialEq, Serialize)] -#[pest_ast(rule(Rule::keyword_or_identifier))] -pub enum KeywordOrIdentifier<'ast> { - SelfKeyword(SelfKeyword<'ast>), - Input(InputKeyword<'ast>), - Identifier(Identifier<'ast>), -} diff --git a/ast/src/functions/input/input_keyword.rs b/ast/src/functions/input/input_keyword.rs index e70e43f20d..877d2346cd 100644 --- a/ast/src/functions/input/input_keyword.rs +++ b/ast/src/functions/input/input_keyword.rs @@ -22,6 +22,7 @@ use crate::{ use pest::Span; use pest_ast::FromPest; use serde::Serialize; +use std::fmt; #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[pest_ast(rule(Rule::input_keyword))] @@ -32,3 +33,9 @@ pub struct InputKeyword<'ast> { #[serde(with = "SpanDef")] pub span: Span<'ast>, } + +impl<'ast> fmt::Display for InputKeyword<'ast> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.keyword) + } +} diff --git a/ast/src/leo.pest b/ast/src/leo.pest index 4bf42da3cf..acaeb7523a 100644 --- a/ast/src/leo.pest +++ b/ast/src/leo.pest @@ -1,7 +1,7 @@ /// Common // Declared in common/assignee.rs -assignee = { identifier ~ access_assignee* } +assignee = { keyword_or_identifier ~ access_assignee* } // Declared in files/file.rs file = { SOI ~ NEWLINE* ~ definition* ~ NEWLINE* ~ EOI } diff --git a/compiler/src/statement/assign/assign.rs b/compiler/src/statement/assign/assign.rs index de63c4bd99..a38dc77aa5 100644 --- a/compiler/src/statement/assign/assign.rs +++ b/compiler/src/statement/assign/assign.rs @@ -79,8 +79,8 @@ impl> ConstrainedProgram { span, ), Assignee::Tuple(_tuple, index) => self.assign_tuple(cs, indicator, variable_name, index, new_value, span), - Assignee::CircuitField(_assignee, object_name) => { - self.mutute_circuit_variable(cs, indicator, variable_name, object_name, new_value, span) + Assignee::CircuitField(_assignee, circuit_variable) => { + self.mutute_circuit_variable(cs, indicator, variable_name, circuit_variable, new_value, span) } } } diff --git a/typed/src/common/assignee.rs b/typed/src/common/assignee.rs index 9bea935574..b5e0a54920 100644 --- a/typed/src/common/assignee.rs +++ b/typed/src/common/assignee.rs @@ -17,7 +17,7 @@ use crate::{Expression, Identifier, RangeOrExpression}; use leo_ast::{ access::AssigneeAccess as AstAssigneeAccess, - common::{Assignee as AstAssignee, Identifier as AstIdentifier}, + common::{Assignee as AstAssignee, Identifier as AstIdentifier, KeywordOrIdentifier}, }; use serde::{Deserialize, Serialize}; @@ -38,9 +38,15 @@ impl<'ast> From> for Assignee { } } +impl<'ast> From> for Assignee { + fn from(name: KeywordOrIdentifier<'ast>) -> Self { + Assignee::Identifier(Identifier::from(name)) + } +} + impl<'ast> From> for Assignee { fn from(assignee: AstAssignee<'ast>) -> Self { - let variable = Assignee::from(assignee.identifier); + let variable = Assignee::from(assignee.name); // We start with the id, and we fold the array of accesses by wrapping the current value assignee diff --git a/typed/src/common/identifier.rs b/typed/src/common/identifier.rs index b02c0cad8a..274bce1a02 100644 --- a/typed/src/common/identifier.rs +++ b/typed/src/common/identifier.rs @@ -23,8 +23,8 @@ use leo_ast::{ use leo_input::common::Identifier as InputAstIdentifier; use leo_ast::{ - common::SelfKeyword, - expressions::{CircuitName, KeywordOrIdentifier}, + common::{KeywordOrIdentifier, SelfKeyword}, + expressions::CircuitName, functions::InputKeyword, types::SelfType, }; diff --git a/typed/src/expression.rs b/typed/src/expression.rs index fb17a983b4..4371f55175 100644 --- a/typed/src/expression.rs +++ b/typed/src/expression.rs @@ -362,7 +362,7 @@ impl<'ast> From> for Expression { // Assignee -> Expression for operator assign statements impl<'ast> From> for Expression { fn from(assignee: Assignee<'ast>) -> Self { - let variable = Expression::Identifier(Identifier::from(assignee.identifier)); + let variable = Expression::Identifier(Identifier::from(assignee.name)); // we start with the id, and we fold the array of accesses by wrapping the current value assignee