Migrates group value

This commit is contained in:
howardwu 2020-06-07 00:54:48 -07:00
parent ad0a9c5381
commit 85f38d3ed0
6 changed files with 66 additions and 62 deletions

View File

@ -12,13 +12,11 @@ use crate::{
ArrayType,
CircuitType,
DataType,
GroupType,
Identifier,
SelfType,
Visibility
},
values::{
NumberValue,
Value
}
};
@ -72,55 +70,6 @@ impl<'ast> fmt::Display for Type<'ast> {
}
}
// Values
#[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)
}
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::group_single_or_tuple))]
pub enum GroupValue<'ast> {
Single(NumberValue<'ast>),
Tuple(GroupTuple<'ast>),
}
impl<'ast> fmt::Display for GroupValue<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
GroupValue::Single(number) => write!(f, "{}", number),
GroupValue::Tuple(tuple) => write!(f, "{}", tuple),
}
}
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::value_group))]
pub struct Group<'ast> {
pub value: GroupValue<'ast>,
pub _type: GroupType,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for Group<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}
// Variables + Mutability
#[derive(Clone, Debug, FromPest, PartialEq)]

View File

@ -11,13 +11,11 @@ visibility = { visibility_public | visibility_private }
visibility_public = { "public" }
visibility_private = { "private" }
/// Unary Operations
/// Operations
// Declared in operations/not_operation.rs
operation_not = { "!" }
/// Binary Operations
// Declared in operations/binary_operation.rs
operation_and = { "&&" }
operation_or = { "||" }
@ -97,6 +95,9 @@ 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* }
@ -112,12 +113,10 @@ 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 = { value_field | value_group | value_boolean | value_integer | value_implicit }
expression_primitive = { value | identifier }
/// Variables + Mutability
@ -187,6 +186,7 @@ expression_term = {
| expression_array_initializer
}
expression_not = { operation_not ~ expression_term }
expression_primitive = { value | identifier }
expression = { expression_term ~ (operation_binary ~ expression_term)* }
expression_tuple = _{ (expression ~ ("," ~ expression)*)? }

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

@ -4,6 +4,9 @@ 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::*;

View File

@ -1,4 +1,4 @@
use crate::{ast::{Group, Rule}, values::{BooleanValue, IntegerValue, FieldValue, NumberImplicitValue}};
use crate::{ast::Rule, values::{BooleanValue, IntegerValue, FieldValue, GroupValue, NumberImplicitValue}};
use pest::Span;
use pest_ast::FromPest;
@ -9,7 +9,7 @@ use std::fmt;
pub enum Value<'ast> {
Integer(IntegerValue<'ast>),
Field(FieldValue<'ast>),
Group(Group<'ast>),
Group(GroupValue<'ast>),
Boolean(BooleanValue<'ast>),
Implicit(NumberImplicitValue<'ast>),
}

View File

@ -22,6 +22,7 @@ use leo_ast::{
values::{
BooleanValue,
FieldValue,
GroupValue,
IntegerValue,
NumberValue,
NumberImplicitValue,
@ -142,8 +143,8 @@ impl<'ast> From<FieldValue<'ast>> for types::Expression {
/// 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())
}
}