mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-26 19:51:52 +03:00
Merge pull request #154 from AleoHQ/feature/array-size
Adds positive number rule to array size
This commit is contained in:
commit
5635736130
@ -1,4 +1,4 @@
|
||||
use crate::{ast::Rule, common::SpreadOrExpression, values::Value, SpanDef};
|
||||
use crate::{ast::Rule, common::SpreadOrExpression, values::PositiveNumber, SpanDef};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
@ -8,7 +8,7 @@ use serde::Serialize;
|
||||
#[pest_ast(rule(Rule::expression_array_initializer))]
|
||||
pub struct ArrayInitializerExpression<'ast> {
|
||||
pub expression: Box<SpreadOrExpression<'ast>>,
|
||||
pub count: Value<'ast>,
|
||||
pub count: PositiveNumber<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
#[serde(with = "SpanDef")]
|
||||
pub span: Span<'ast>,
|
||||
|
@ -169,7 +169,7 @@ type_self = { "Self" }
|
||||
type_circuit = { identifier }
|
||||
|
||||
// Declared in types/array_type.rs
|
||||
type_array = { type_data ~ ("[" ~ value ~ "]")+ }
|
||||
type_array = { type_data ~ ("[" ~ positive_number ~ "]")+ }
|
||||
|
||||
type_list = _{ (type_ ~ ("," ~ type_)*)? }
|
||||
|
||||
@ -188,6 +188,9 @@ value = {
|
||||
// Declared in values/number_value.rs
|
||||
value_number = @{ (("-" ~ ASCII_NONZERO_DIGIT) | "0" | ASCII_NONZERO_DIGIT) ~ ASCII_DIGIT* }
|
||||
|
||||
// Declared in values/number_positive.rs
|
||||
positive_number = @{ ("0" | ASCII_NONZERO_DIGIT) ~ ASCII_DIGIT*}
|
||||
|
||||
// Declared in values/number_implicit_value.rs
|
||||
value_implicit = { value_number }
|
||||
|
||||
@ -271,7 +274,7 @@ expression_tuple = _{ (expression ~ ("," ~ expression)*)? }
|
||||
expression = { expression_term ~ (operation_binary ~ expression_term)* }
|
||||
|
||||
// Declared in expressions/array_initializer_expression.rs
|
||||
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ value ~ "]" }
|
||||
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ positive_number ~ "]" }
|
||||
|
||||
// Declared in expressions/array_inline_expression.rs
|
||||
expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::Rule, types::DataType, values::Value, SpanDef};
|
||||
use crate::{ast::Rule, types::DataType, values::PositiveNumber, SpanDef};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
@ -8,7 +8,7 @@ use serde::Serialize;
|
||||
#[pest_ast(rule(Rule::type_array))]
|
||||
pub struct ArrayType<'ast> {
|
||||
pub _type: DataType,
|
||||
pub dimensions: Vec<Value<'ast>>,
|
||||
pub dimensions: Vec<PositiveNumber<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
#[serde(with = "SpanDef")]
|
||||
pub span: Span<'ast>,
|
||||
|
@ -22,5 +22,8 @@ pub use number_implicit_value::*;
|
||||
pub mod number_value;
|
||||
pub use number_value::*;
|
||||
|
||||
pub mod positive_number;
|
||||
pub use positive_number::*;
|
||||
|
||||
pub mod value;
|
||||
pub use value::*;
|
||||
|
25
ast/src/values/positive_number.rs
Normal file
25
ast/src/values/positive_number.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use crate::{
|
||||
ast::{span_into_string, Rule},
|
||||
span::SpanDef,
|
||||
};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
use serde::Serialize;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
|
||||
#[pest_ast(rule(Rule::positive_number))]
|
||||
pub struct PositiveNumber<'ast> {
|
||||
#[pest_ast(outer(with(span_into_string)))]
|
||||
pub value: String,
|
||||
#[pest_ast(outer())]
|
||||
#[serde(with = "SpanDef")]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for PositiveNumber<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.value)
|
||||
}
|
||||
}
|
3
compiler/tests/array/initializer_fail.leo
Normal file
3
compiler/tests/array/initializer_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: u8[3]) {
|
||||
assert_eq!(a, [1u8; -3]);
|
||||
}
|
@ -42,6 +42,14 @@ fn test_registers() {
|
||||
|
||||
// Expressions
|
||||
|
||||
#[test]
|
||||
fn test_type_fail() {
|
||||
let program_bytes = include_bytes!("type_fail.leo");
|
||||
let syntax_error = parse_program(program_bytes).is_err();
|
||||
|
||||
assert!(syntax_error);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_inline() {
|
||||
let program_bytes = include_bytes!("inline.leo");
|
||||
@ -68,6 +76,15 @@ fn test_initializer() {
|
||||
assert_satisfied(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_initializer_fail() {
|
||||
let program_bytes = include_bytes!("initializer_fail.leo");
|
||||
let input_bytes = include_bytes!("input/three_ones.in");
|
||||
let syntax_error = parse_program_with_input(program_bytes, input_bytes).is_err();
|
||||
|
||||
assert!(syntax_error);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spread() {
|
||||
let program_bytes = include_bytes!("spread.leo");
|
||||
|
3
compiler/tests/array/type_fail.leo
Normal file
3
compiler/tests/array/type_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
let main() {
|
||||
let a: u32[-2] = [0u32; 2];
|
||||
}
|
@ -13,11 +13,19 @@ use leo_ast::{
|
||||
TernaryExpression,
|
||||
},
|
||||
operations::BinaryOperation,
|
||||
values::{BooleanValue, FieldValue, GroupValue, IntegerValue, NumberImplicitValue, Value},
|
||||
values::{
|
||||
AddressValue,
|
||||
BooleanValue,
|
||||
FieldValue,
|
||||
GroupValue,
|
||||
IntegerValue,
|
||||
NumberImplicitValue,
|
||||
PositiveNumber as LeoPositiveNumber,
|
||||
Value,
|
||||
},
|
||||
};
|
||||
|
||||
use leo_ast::values::AddressValue;
|
||||
use leo_input::values::NumberValue;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
@ -104,21 +112,16 @@ impl Expression {
|
||||
}
|
||||
|
||||
impl<'ast> Expression {
|
||||
pub(crate) fn get_count_from_value(count: Value<'ast>) -> usize {
|
||||
match count {
|
||||
Value::Integer(integer) => integer
|
||||
.number
|
||||
.value
|
||||
.parse::<usize>()
|
||||
.expect("Unable to read array size"),
|
||||
Value::Implicit(number) => number.number.value.parse::<usize>().expect("Unable to read array size"),
|
||||
size => unimplemented!("Array size should be an integer {}", size),
|
||||
}
|
||||
}
|
||||
|
||||
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_positive_number(number: LeoPositiveNumber<'ast>) -> usize {
|
||||
number
|
||||
.value
|
||||
.parse::<usize>()
|
||||
.expect("Array size should be a positive number")
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for Expression {
|
||||
@ -403,7 +406,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_value(array.count);
|
||||
let count = Expression::get_count_from_positive_number(array.count);
|
||||
let expression = Box::new(SpreadOrExpression::from(*array.expression));
|
||||
|
||||
Expression::Array(vec![expression; count], Span::from(array.span))
|
||||
|
@ -101,7 +101,7 @@ impl<'ast> From<ArrayType<'ast>> for Type {
|
||||
let dimensions = array_type
|
||||
.dimensions
|
||||
.into_iter()
|
||||
.map(|row| Expression::get_count_from_value(row))
|
||||
.map(|row| Expression::get_count_from_positive_number(row))
|
||||
.collect();
|
||||
|
||||
Type::Array(element_type, dimensions)
|
||||
|
Loading…
Reference in New Issue
Block a user