add updated values to typed

This commit is contained in:
collin 2020-08-05 16:26:25 -07:00
parent 648acb7fe3
commit 06a25a2d67
6 changed files with 114 additions and 62 deletions

View File

@ -62,7 +62,7 @@ impl InputParserError {
Self::new_from_span(message, span)
}
pub fn array_inline_length(number: NumberValue, array: ArrayInlineExpression) -> Self {
pub fn array_inline_length(number: PositiveNumber, array: ArrayInlineExpression) -> Self {
let message = format!(
"expected an array with a fixed size of {} elements, found one with {} elements",
number.to_string(),

View File

@ -1,5 +1,5 @@
use crate::{Error, Expression, Span};
use leo_ast::{common::RangeOrExpression as AstRangeOrExpression, values::NumberValue};
use crate::Expression;
use leo_ast::common::RangeOrExpression as AstRangeOrExpression;
use serde::{Deserialize, Serialize};
use std::fmt;
@ -11,15 +11,6 @@ pub enum RangeOrExpression {
Expression(Expression),
}
pub fn unwrap_bound(bound: Option<NumberValue>) -> Option<usize> {
bound.map(|number| {
let message = format!("Range bounds should be integers");
let error = Error::new_from_span(message, Span::from(number.span.clone()));
number.value.parse::<usize>().expect(&error.to_string())
})
}
impl<'ast> From<AstRangeOrExpression<'ast>> for RangeOrExpression {
fn from(range_or_expression: AstRangeOrExpression<'ast>) -> Self {
match range_or_expression {

View File

@ -19,12 +19,12 @@ use leo_ast::{
FieldValue,
GroupValue,
IntegerValue,
NumberImplicitValue,
PositiveNumber as LeoPositiveNumber,
NumberValue as AstNumber,
PositiveNumber as AstPositiveNumber,
Value,
},
};
use leo_input::values::NumberValue;
use leo_input::values::PositiveNumber as InputAstPositiveNumber;
use serde::{Deserialize, Serialize};
use std::fmt;
@ -112,11 +112,14 @@ impl Expression {
}
impl<'ast> Expression {
pub(crate) fn get_count_from_number(number: NumberValue<'ast>) -> usize {
number.value.parse::<usize>().expect("Unable to read array size")
pub(crate) fn get_count_from_input_ast(number: InputAstPositiveNumber<'ast>) -> usize {
number
.value
.parse::<usize>()
.expect("Array size should be a positive number")
}
pub(crate) fn get_count_from_positive_number(number: LeoPositiveNumber<'ast>) -> usize {
pub(crate) fn get_count_from_ast(number: AstPositiveNumber<'ast>) -> usize {
number
.value
.parse::<usize>()
@ -406,7 +409,7 @@ impl<'ast> From<ArrayInlineExpression<'ast>> for Expression {
impl<'ast> From<ArrayInitializerExpression<'ast>> for Expression {
fn from(array: ArrayInitializerExpression<'ast>) -> Self {
let count = Expression::get_count_from_positive_number(array.count);
let count = Expression::get_count_from_ast(array.count);
let expression = Box::new(SpreadOrExpression::from(*array.expression));
Expression::Array(vec![expression; count], Span::from(array.span))
@ -449,7 +452,7 @@ impl<'ast> From<BooleanValue<'ast>> for Expression {
impl<'ast> From<FieldValue<'ast>> for Expression {
fn from(field: FieldValue<'ast>) -> Self {
Expression::Field(field.number.value, Span::from(field.span))
Expression::Field(field.number.to_string(), Span::from(field.span))
}
}
@ -459,19 +462,39 @@ impl<'ast> From<GroupValue<'ast>> for Expression {
}
}
impl<'ast> From<NumberImplicitValue<'ast>> for Expression {
fn from(number: NumberImplicitValue<'ast>) -> Self {
Expression::Implicit(number.number.value, Span::from(number.span))
impl<'ast> From<AstNumber<'ast>> for Expression {
fn from(number: AstNumber<'ast>) -> Self {
let (value, span) = match number {
AstNumber::Positive(number) => (number.value, number.span),
AstNumber::Negative(number) => (number.value, number.span),
};
Expression::Implicit(value, Span::from(span))
}
}
impl<'ast> From<IntegerValue<'ast>> for Expression {
fn from(integer: IntegerValue<'ast>) -> Self {
Expression::Integer(
IntegerType::from(integer._type),
integer.number.value,
Span::from(integer.span),
)
let span = Span::from(integer.span().clone());
let (type_, value) = match integer {
IntegerValue::Signed(integer) => {
let type_ = IntegerType::from(integer.type_);
let number = match integer.number {
AstNumber::Negative(number) => number.value,
AstNumber::Positive(number) => number.value,
};
(type_, number)
}
IntegerValue::Unsigned(integer) => {
let type_ = IntegerType::from(integer.type_);
let number = integer.number.value;
(type_, number)
}
};
Expression::Integer(type_, value, span)
}
}

View File

@ -2,7 +2,7 @@ use leo_input::{
errors::InputParserError,
expressions::{ArrayInitializerExpression, ArrayInlineExpression, Expression},
types::{ArrayType, DataType, IntegerType, Type},
values::{BooleanValue, FieldValue, GroupValue, NumberImplicitValue, NumberValue, Value},
values::{BooleanValue, FieldValue, GroupValue, NumberValue, Value},
};
use leo_input::values::Address;
@ -28,8 +28,8 @@ impl InputValue {
Ok(InputValue::Boolean(boolean))
}
fn from_number(integer_type: IntegerType, number: NumberValue) -> Result<Self, InputParserError> {
Ok(InputValue::Integer(integer_type, number.value))
fn from_number(integer_type: IntegerType, number: String) -> Result<Self, InputParserError> {
Ok(InputValue::Integer(integer_type, number))
}
fn from_group(group: GroupValue) -> Self {
@ -37,16 +37,16 @@ impl InputValue {
}
fn from_field(field: FieldValue) -> Self {
InputValue::Field(field.number.value)
InputValue::Field(field.number.to_string())
}
fn from_implicit(data_type: DataType, implicit: NumberImplicitValue) -> Result<Self, InputParserError> {
fn from_implicit(data_type: DataType, implicit: NumberValue) -> Result<Self, InputParserError> {
match data_type {
DataType::Address(_) => Err(InputParserError::implicit_type(data_type, implicit)),
DataType::Boolean(_) => Err(InputParserError::implicit_type(data_type, implicit)),
DataType::Integer(integer_type) => InputValue::from_number(integer_type, implicit.number),
DataType::Group(_) => Ok(InputValue::Group(implicit.number.value)),
DataType::Field(_) => Ok(InputValue::Field(implicit.number.value)),
DataType::Integer(integer_type) => InputValue::from_number(integer_type, implicit.to_string()),
DataType::Group(_) => Ok(InputValue::Group(implicit.to_string())),
DataType::Field(_) => Ok(InputValue::Field(implicit.to_string())),
}
}
@ -55,7 +55,7 @@ impl InputValue {
(DataType::Address(_), Value::Address(address)) => Ok(InputValue::from_address(address.address)),
(DataType::Boolean(_), Value::Boolean(boolean)) => InputValue::from_boolean(boolean),
(DataType::Integer(integer_type), Value::Integer(integer)) => {
InputValue::from_number(integer_type, integer.number)
InputValue::from_number(integer_type, integer.to_string())
}
(DataType::Group(_), Value::Group(group)) => Ok(InputValue::from_group(group)),
(DataType::Field(_), Value::Field(field)) => Ok(InputValue::from_field(field)),
@ -113,7 +113,7 @@ impl InputValue {
mut array_type: ArrayType,
initializer: ArrayInitializerExpression,
) -> Result<Self, InputParserError> {
let initializer_count = initializer.count.value.parse::<usize>()?;
let initializer_count = initializer.count.to_string().parse::<usize>()?;
if let Some(number) = array_type.next_dimension() {
let outer_dimension = number.value.parse::<usize>()?;

View File

@ -1,5 +1,13 @@
use leo_ast::types::IntegerType as AstIntegerType;
use leo_input::types::IntegerType as InputAstIntegerType;
use leo_ast::types::{
IntegerType as AstIntegerType,
SignedIntegerType as AstSignedIntegerType,
UnsignedIntegerType as AstUnsignedIntegerType,
};
use leo_input::types::{
IntegerType as InputAstIntegerType,
SignedIntegerType as InputAstSignedIntegerType,
UnsignedIntegerType as InputAstUnsignedIntegerType,
};
use serde::{Deserialize, Serialize};
use std::fmt;
@ -23,17 +31,32 @@ pub enum IntegerType {
impl From<AstIntegerType> for IntegerType {
fn from(integer_type: AstIntegerType) -> Self {
match integer_type {
AstIntegerType::U8Type(_type) => IntegerType::U8,
AstIntegerType::U16Type(_type) => IntegerType::U16,
AstIntegerType::U32Type(_type) => IntegerType::U32,
AstIntegerType::U64Type(_type) => IntegerType::U64,
AstIntegerType::U128Type(_type) => IntegerType::U128,
AstIntegerType::Signed(signed) => Self::from(signed),
AstIntegerType::Unsigned(unsigned) => Self::from(unsigned),
}
}
}
AstIntegerType::I8Type(_type) => IntegerType::I8,
AstIntegerType::I16Type(_type) => IntegerType::I16,
AstIntegerType::I32Type(_type) => IntegerType::I32,
AstIntegerType::I64Type(_type) => IntegerType::I64,
AstIntegerType::I128Type(_type) => IntegerType::I128,
impl From<AstUnsignedIntegerType> for IntegerType {
fn from(integer_type: AstUnsignedIntegerType) -> Self {
match integer_type {
AstUnsignedIntegerType::U8Type(_type) => IntegerType::U8,
AstUnsignedIntegerType::U16Type(_type) => IntegerType::U16,
AstUnsignedIntegerType::U32Type(_type) => IntegerType::U32,
AstUnsignedIntegerType::U64Type(_type) => IntegerType::U64,
AstUnsignedIntegerType::U128Type(_type) => IntegerType::U128,
}
}
}
impl From<AstSignedIntegerType> for IntegerType {
fn from(integer_type: AstSignedIntegerType) -> Self {
match integer_type {
AstSignedIntegerType::I8Type(_type) => IntegerType::I8,
AstSignedIntegerType::I16Type(_type) => IntegerType::I16,
AstSignedIntegerType::I32Type(_type) => IntegerType::I32,
AstSignedIntegerType::I64Type(_type) => IntegerType::I64,
AstSignedIntegerType::I128Type(_type) => IntegerType::I128,
}
}
}
@ -41,17 +64,32 @@ impl From<AstIntegerType> for IntegerType {
impl From<InputAstIntegerType> for IntegerType {
fn from(integer_type: InputAstIntegerType) -> Self {
match integer_type {
InputAstIntegerType::U8Type(_type) => IntegerType::U8,
InputAstIntegerType::U16Type(_type) => IntegerType::U16,
InputAstIntegerType::U32Type(_type) => IntegerType::U32,
InputAstIntegerType::U64Type(_type) => IntegerType::U64,
InputAstIntegerType::U128Type(_type) => IntegerType::U128,
InputAstIntegerType::Signed(signed) => Self::from(signed),
InputAstIntegerType::Unsigned(unsigned) => Self::from(unsigned),
}
}
}
InputAstIntegerType::I8Type(_type) => IntegerType::I8,
InputAstIntegerType::I16Type(_type) => IntegerType::I16,
InputAstIntegerType::I32Type(_type) => IntegerType::I32,
InputAstIntegerType::I64Type(_type) => IntegerType::I64,
InputAstIntegerType::I128Type(_type) => IntegerType::I128,
impl From<InputAstUnsignedIntegerType> for IntegerType {
fn from(integer_type: InputAstUnsignedIntegerType) -> Self {
match integer_type {
InputAstUnsignedIntegerType::U8Type(_type) => IntegerType::U8,
InputAstUnsignedIntegerType::U16Type(_type) => IntegerType::U16,
InputAstUnsignedIntegerType::U32Type(_type) => IntegerType::U32,
InputAstUnsignedIntegerType::U64Type(_type) => IntegerType::U64,
InputAstUnsignedIntegerType::U128Type(_type) => IntegerType::U128,
}
}
}
impl From<InputAstSignedIntegerType> for IntegerType {
fn from(integer_type: InputAstSignedIntegerType) -> Self {
match integer_type {
InputAstSignedIntegerType::I8Type(_type) => IntegerType::I8,
InputAstSignedIntegerType::I16Type(_type) => IntegerType::I16,
InputAstSignedIntegerType::I32Type(_type) => IntegerType::I32,
InputAstSignedIntegerType::I64Type(_type) => IntegerType::I64,
InputAstSignedIntegerType::I128Type(_type) => IntegerType::I128,
}
}
}

View File

@ -57,7 +57,7 @@ impl<'ast> From<InputArrayType<'ast>> for Type {
let dimensions = array_type
.dimensions
.into_iter()
.map(|row| Expression::get_count_from_number(row))
.map(|row| Expression::get_count_from_input_ast(row))
.collect();
Type::Array(element_type, dimensions)
@ -101,7 +101,7 @@ impl<'ast> From<ArrayType<'ast>> for Type {
let dimensions = array_type
.dimensions
.into_iter()
.map(|row| Expression::get_count_from_positive_number(row))
.map(|row| Expression::get_count_from_ast(row))
.collect();
Type::Array(element_type, dimensions)