add address type to leo-ast and leo-types

This commit is contained in:
collin 2020-07-06 01:47:07 -07:00
parent 8fbf2b2e7e
commit f1030117ee
9 changed files with 109 additions and 37 deletions

View File

@ -113,8 +113,17 @@ type_group = { "group" }
// Declared in types/boolean_type.rs
type_boolean = { "bool" }
// Declared in types/address_type.rs
type_address = { "address" }
// Declared in types/data_type.rs
type_data = { type_field | type_group | type_boolean | type_integer }
type_data = {
type_address
| type_boolean
| type_field
| type_group
| type_integer
}
// Declared in types/self_type.rs
type_self = { "Self" }
@ -130,7 +139,14 @@ type_list = _{ (type_ ~ ("," ~ type_)*)? }
/// Values
// Declared in values/value.rs
value = { value_field | value_group | value_boolean | value_integer | value_implicit }
value = {
value_address
| value_boolean
| value_field
| value_group
| value_integer
| value_implicit // must be last as a catch all
}
// Declared in values/number_value.rs
value_number = @{ "0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* }
@ -152,6 +168,9 @@ value_group = { group_single_or_tuple ~ type_group }
group_tuple = {"(" ~ value_number ~ "," ~ value_number ~ ")"}
group_single_or_tuple = {value_number | group_tuple}
// Declared in values/address_value.rs
value_address = { type_address ~ "(" ~ value_number ~ ")" }
/// Access
// Declared in access/access.rs
@ -252,7 +271,7 @@ assert_eq = {"assert_eq!" ~ "(" ~ NEWLINE* ~ expression ~ "," ~ NEWLINE* ~ expre
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)?}
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
@ -262,7 +281,7 @@ statement_definition = { declare ~ variable ~ "=" ~ expression ~ LINE_END}
statement_expression = { expression ~ LINE_END }
// Declared in statements/for_statement.rs
statement_for = { "for " ~ identifier ~ "in " ~ expression ~ ".." ~ expression ~ "{" ~ NEWLINE* ~ statement+ ~ "}"}
statement_for = { "for " ~ identifier ~ " in " ~ expression ~ ".." ~ expression ~ "{" ~ NEWLINE* ~ statement+ ~ "}"}
// Declared in statements/multiple_assignment_statement.rs
statement_multiple_assignment = { declare ~ "(" ~ variable_tuple ~ ")" ~ "=" ~ identifier ~ "(" ~ expression_tuple ~ ")" ~ LINE_END}
@ -286,7 +305,7 @@ test_function = { "test " ~ function_definition }
/// Imports
// Declared in imports/import.rs
import = { "import" ~ package ~ LINE_END}
import = { "import " ~ package ~ LINE_END}
// Declared in imports/package.rs
package = { identifier ~ "." ~ package_access }
@ -305,7 +324,7 @@ multiple_package_access = _{ "(" ~ NEWLINE* ~ package_access ~ ("," ~ NEWLINE* ~
star = {"*"}
// Declared in imports/import_symbol.rs
import_symbol = { identifier ~ ("as" ~ identifier)? }
import_symbol = { identifier ~ (" as " ~ identifier)? }
/// Utilities

View File

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

View File

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

View File

@ -1,3 +1,6 @@
pub mod address_type;
pub use address_type::*;
pub mod array_type;
pub use array_type::*;

View File

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

View File

@ -1,3 +1,6 @@
pub mod address_value;
pub use address_value::*;
pub mod boolean_value;
pub use boolean_value::*;

View File

@ -1,6 +1,6 @@
use crate::{
ast::Rule,
values::{BooleanValue, FieldValue, GroupValue, IntegerValue, NumberImplicitValue},
values::{AddressValue, BooleanValue, FieldValue, GroupValue, IntegerValue, NumberImplicitValue},
};
use pest::Span;
@ -10,21 +10,23 @@ use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::value))]
pub enum Value<'ast> {
Integer(IntegerValue<'ast>),
Address(AddressValue<'ast>),
Boolean(BooleanValue<'ast>),
Field(FieldValue<'ast>),
Group(GroupValue<'ast>),
Boolean(BooleanValue<'ast>),
Implicit(NumberImplicitValue<'ast>),
Integer(IntegerValue<'ast>),
}
impl<'ast> Value<'ast> {
pub fn span(&self) -> &Span<'ast> {
match self {
Value::Integer(value) => &value.span,
Value::Address(value) => &value.span,
Value::Boolean(value) => &value.span,
Value::Field(value) => &value.span,
Value::Group(value) => &value.span,
Value::Boolean(value) => &value.span,
Value::Implicit(value) => &value.span,
Value::Integer(value) => &value.span,
}
}
}
@ -32,11 +34,12 @@ impl<'ast> Value<'ast> {
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::Address(ref value) => write!(f, "{}", value),
Value::Boolean(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),
Value::Integer(ref value) => write!(f, "{}", value),
}
}
}

View File

@ -16,6 +16,7 @@ use leo_ast::{
values::{BooleanValue, FieldValue, GroupValue, IntegerValue, NumberImplicitValue, Value},
};
use leo_ast::values::AddressValue;
use serde::{Deserialize, Serialize};
use std::fmt;
@ -26,11 +27,12 @@ pub enum Expression {
Identifier(Identifier),
// Values
Integer(IntegerType, String, Span),
Address(String, Span),
Boolean(String, Span),
Field(String, Span),
Group(String, Span),
Boolean(String, Span),
Implicit(String, Span),
Integer(IntegerType, String, Span),
// Number operations
Add(Box<Expression>, Box<Expression>, Span),
@ -121,11 +123,12 @@ impl<'ast> fmt::Display for Expression {
Expression::Identifier(ref variable) => write!(f, "{}", variable),
// Values
Expression::Integer(ref type_, ref integer, ref _span) => write!(f, "{}{}", integer, type_),
Expression::Address(ref address, ref _span) => write!(f, "{}", address),
Expression::Boolean(ref bool, ref _span) => write!(f, "{}", bool),
Expression::Field(ref field, ref _span) => write!(f, "{}", field),
Expression::Group(ref group, ref _span) => write!(f, "{}", group),
Expression::Boolean(ref bool, ref _span) => write!(f, "{}", bool),
Expression::Implicit(ref value, ref _span) => write!(f, "{}", value),
Expression::Integer(ref type_, ref integer, ref _span) => write!(f, "{}{}", integer, type_),
// Number operations
Expression::Add(ref left, ref right, ref _span) => write!(f, "{} + {}", left, right),
@ -405,11 +408,12 @@ impl<'ast> From<ArrayInitializerExpression<'ast>> for Expression {
impl<'ast> From<Value<'ast>> for Expression {
fn from(value: Value<'ast>) -> Self {
match value {
Value::Integer(num) => Expression::from(num),
Value::Address(address) => Expression::from(address),
Value::Boolean(boolean) => Expression::from(boolean),
Value::Field(field) => Expression::from(field),
Value::Group(group) => Expression::from(group),
Value::Boolean(bool) => Expression::from(bool),
Value::Implicit(value) => Expression::from(value),
Value::Implicit(number) => Expression::from(number),
Value::Integer(integer) => Expression::from(integer),
}
}
}
@ -423,6 +427,18 @@ impl<'ast> From<NotExpression<'ast>> for Expression {
}
}
impl<'ast> From<AddressValue<'ast>> for Expression {
fn from(address: AddressValue<'ast>) -> Self {
Expression::Address(address.number.value, Span::from(address.span))
}
}
impl<'ast> From<BooleanValue<'ast>> for Expression {
fn from(boolean: BooleanValue<'ast>) -> Self {
Expression::Boolean(boolean.value, Span::from(boolean.span))
}
}
impl<'ast> From<FieldValue<'ast>> for Expression {
fn from(field: FieldValue<'ast>) -> Self {
Expression::Field(field.number.value, Span::from(field.span))
@ -435,12 +451,6 @@ impl<'ast> From<GroupValue<'ast>> for Expression {
}
}
impl<'ast> From<BooleanValue<'ast>> for Expression {
fn from(boolean: BooleanValue<'ast>) -> Self {
Expression::Boolean(boolean.value, Span::from(boolean.span))
}
}
impl<'ast> From<NumberImplicitValue<'ast>> for Expression {
fn from(number: NumberImplicitValue<'ast>) -> Self {
Expression::Implicit(number.number.value, Span::from(number.span))

View File

@ -7,10 +7,14 @@ use std::fmt;
/// Explicit type used for defining a variable or expression type
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Type {
IntegerType(IntegerType),
// Data types
Address,
Boolean,
Field,
Group,
Boolean,
IntegerType(IntegerType),
// Data type wrappers
Array(Box<Type>, Vec<usize>),
Circuit(Identifier),
SelfType,
@ -35,12 +39,13 @@ impl Type {
/// pest ast -> Explicit Type for defining circuit members and function params
impl From<DataType> for Type {
fn from(basic_type: DataType) -> Self {
match basic_type {
DataType::Integer(_type) => Type::IntegerType(IntegerType::from(_type)),
fn from(data_type: DataType) -> Self {
match data_type {
DataType::Address(_type) => Type::Address,
DataType::Boolean(_type) => Type::Boolean,
DataType::Field(_type) => Type::Field,
DataType::Group(_type) => Type::Group,
DataType::Boolean(_type) => Type::Boolean,
DataType::Integer(_type) => Type::IntegerType(IntegerType::from(_type)),
}
}
}
@ -106,10 +111,11 @@ impl Type {
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type),
Type::Address => write!(f, "address"),
Type::Boolean => write!(f, "bool"),
Type::Field => write!(f, "field"),
Type::Group => write!(f, "group"),
Type::Boolean => write!(f, "bool"),
Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type),
Type::Circuit(ref variable) => write!(f, "circuit {}", variable),
Type::SelfType => write!(f, "SelfType"),
Type::Array(ref array, ref dimensions) => {