renaming, now .length()

This commit is contained in:
damirka 2021-09-10 16:11:55 +03:00
parent 2942bf85fe
commit 093517109b
25 changed files with 176 additions and 77 deletions

View File

@ -21,19 +21,19 @@ use leo_errors::{Result, Span};
use std::cell::Cell; use std::cell::Cell;
#[derive(Clone)] #[derive(Clone)]
pub struct SizeOfExpression<'a> { pub struct LengthOfExpression<'a> {
pub parent: Cell<Option<&'a Expression<'a>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub inner: Cell<&'a Expression<'a>>, pub inner: Cell<&'a Expression<'a>>,
} }
impl<'a> Node for SizeOfExpression<'a> { impl<'a> Node for LengthOfExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl<'a> ExpressionNode<'a> for SizeOfExpression<'a> { impl<'a> ExpressionNode<'a> for LengthOfExpression<'a> {
fn set_parent(&self, parent: &'a Expression<'a>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
@ -47,10 +47,7 @@ impl<'a> ExpressionNode<'a> for SizeOfExpression<'a> {
} }
fn get_type(&self) -> Option<Type<'a>> { fn get_type(&self) -> Option<Type<'a>> {
// TODO: make decision on get type method Some(Type::Integer(IntegerType::U32)) // For now we stick to U32 value type
// Should it be always u32? For indexes?
// How type casts are applied to this?
Some(Type::Integer(IntegerType::U32))
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
@ -58,16 +55,6 @@ impl<'a> ExpressionNode<'a> for SizeOfExpression<'a> {
} }
fn const_value(&self) -> Option<ConstValue> { fn const_value(&self) -> Option<ConstValue> {
let _value = self.inner.get().const_value()?;
// match value {
// ConstValue::Int(int) => match &self.target_type {
// Type::Integer(target) => Some(ConstValue::Int(int.cast_to(target))),
// _ => None,
// },
// _ => None,
// }
// TODO: IMPLEMENT CONST VALUE
None None
} }
@ -76,15 +63,15 @@ impl<'a> ExpressionNode<'a> for SizeOfExpression<'a> {
} }
} }
impl<'a> FromAst<'a, leo_ast::SizeOfExpression> for SizeOfExpression<'a> { impl<'a> FromAst<'a, leo_ast::LengthOfExpression> for LengthOfExpression<'a> {
fn from_ast( fn from_ast(
scope: &'a Scope<'a>, scope: &'a Scope<'a>,
value: &leo_ast::SizeOfExpression, value: &leo_ast::LengthOfExpression,
_expected_type: Option<PartialType<'a>>, _expected_type: Option<PartialType<'a>>,
) -> Result<SizeOfExpression<'a>> { ) -> Result<LengthOfExpression<'a>> {
let inner = <&Expression<'a>>::from_ast(scope, &*value.inner, None)?; let inner = <&Expression<'a>>::from_ast(scope, &*value.inner, None)?;
Ok(SizeOfExpression { Ok(LengthOfExpression {
parent: Cell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
inner: Cell::new(inner), inner: Cell::new(inner),
@ -92,9 +79,9 @@ impl<'a> FromAst<'a, leo_ast::SizeOfExpression> for SizeOfExpression<'a> {
} }
} }
impl<'a> Into<leo_ast::SizeOfExpression> for &SizeOfExpression<'a> { impl<'a> Into<leo_ast::LengthOfExpression> for &LengthOfExpression<'a> {
fn into(self) -> leo_ast::SizeOfExpression { fn into(self) -> leo_ast::LengthOfExpression {
leo_ast::SizeOfExpression { leo_ast::LengthOfExpression {
inner: Box::new(self.inner.get().into()), inner: Box::new(self.inner.get().into()),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }

View File

@ -65,8 +65,8 @@ pub use variable_ref::*;
mod cast; mod cast;
pub use cast::*; pub use cast::*;
mod sizeof; mod lengthof;
pub use sizeof::*; pub use lengthof::*;
use crate::{ConstValue, FromAst, Node, PartialType, Scope, Type}; use crate::{ConstValue, FromAst, Node, PartialType, Scope, Type};
use leo_errors::{Result, Span}; use leo_errors::{Result, Span};
@ -79,7 +79,7 @@ pub enum Expression<'a> {
Unary(UnaryExpression<'a>), Unary(UnaryExpression<'a>),
Ternary(TernaryExpression<'a>), Ternary(TernaryExpression<'a>),
Cast(CastExpression<'a>), Cast(CastExpression<'a>),
SizeOf(SizeOfExpression<'a>), LengthOf(LengthOfExpression<'a>),
ArrayInline(ArrayInlineExpression<'a>), ArrayInline(ArrayInlineExpression<'a>),
ArrayInit(ArrayInitExpression<'a>), ArrayInit(ArrayInitExpression<'a>),
@ -111,7 +111,7 @@ impl<'a> Node for Expression<'a> {
Unary(x) => x.span(), Unary(x) => x.span(),
Ternary(x) => x.span(), Ternary(x) => x.span(),
Cast(x) => x.span(), Cast(x) => x.span(),
SizeOf(x) => x.span(), LengthOf(x) => x.span(),
ArrayInline(x) => x.span(), ArrayInline(x) => x.span(),
ArrayInit(x) => x.span(), ArrayInit(x) => x.span(),
ArrayAccess(x) => x.span(), ArrayAccess(x) => x.span(),
@ -146,7 +146,7 @@ impl<'a> ExpressionNode<'a> for Expression<'a> {
Unary(x) => x.set_parent(parent), Unary(x) => x.set_parent(parent),
Ternary(x) => x.set_parent(parent), Ternary(x) => x.set_parent(parent),
Cast(x) => x.set_parent(parent), Cast(x) => x.set_parent(parent),
SizeOf(x) => x.set_parent(parent), LengthOf(x) => x.set_parent(parent),
ArrayInline(x) => x.set_parent(parent), ArrayInline(x) => x.set_parent(parent),
ArrayInit(x) => x.set_parent(parent), ArrayInit(x) => x.set_parent(parent),
ArrayAccess(x) => x.set_parent(parent), ArrayAccess(x) => x.set_parent(parent),
@ -168,7 +168,7 @@ impl<'a> ExpressionNode<'a> for Expression<'a> {
Unary(x) => x.get_parent(), Unary(x) => x.get_parent(),
Ternary(x) => x.get_parent(), Ternary(x) => x.get_parent(),
Cast(x) => x.get_parent(), Cast(x) => x.get_parent(),
SizeOf(x) => x.get_parent(), LengthOf(x) => x.get_parent(),
ArrayInline(x) => x.get_parent(), ArrayInline(x) => x.get_parent(),
ArrayInit(x) => x.get_parent(), ArrayInit(x) => x.get_parent(),
ArrayAccess(x) => x.get_parent(), ArrayAccess(x) => x.get_parent(),
@ -190,7 +190,7 @@ impl<'a> ExpressionNode<'a> for Expression<'a> {
Unary(x) => x.enforce_parents(expr), Unary(x) => x.enforce_parents(expr),
Ternary(x) => x.enforce_parents(expr), Ternary(x) => x.enforce_parents(expr),
Cast(x) => x.enforce_parents(expr), Cast(x) => x.enforce_parents(expr),
SizeOf(x) => x.enforce_parents(expr), LengthOf(x) => x.enforce_parents(expr),
ArrayInline(x) => x.enforce_parents(expr), ArrayInline(x) => x.enforce_parents(expr),
ArrayInit(x) => x.enforce_parents(expr), ArrayInit(x) => x.enforce_parents(expr),
ArrayAccess(x) => x.enforce_parents(expr), ArrayAccess(x) => x.enforce_parents(expr),
@ -212,7 +212,7 @@ impl<'a> ExpressionNode<'a> for Expression<'a> {
Unary(x) => x.get_type(), Unary(x) => x.get_type(),
Ternary(x) => x.get_type(), Ternary(x) => x.get_type(),
Cast(x) => x.get_type(), Cast(x) => x.get_type(),
SizeOf(x) => x.get_type(), LengthOf(x) => x.get_type(),
ArrayInline(x) => x.get_type(), ArrayInline(x) => x.get_type(),
ArrayInit(x) => x.get_type(), ArrayInit(x) => x.get_type(),
ArrayAccess(x) => x.get_type(), ArrayAccess(x) => x.get_type(),
@ -234,7 +234,7 @@ impl<'a> ExpressionNode<'a> for Expression<'a> {
Unary(x) => x.is_mut_ref(), Unary(x) => x.is_mut_ref(),
Ternary(x) => x.is_mut_ref(), Ternary(x) => x.is_mut_ref(),
Cast(x) => x.is_mut_ref(), Cast(x) => x.is_mut_ref(),
SizeOf(x) => x.is_mut_ref(), LengthOf(x) => x.is_mut_ref(),
ArrayInline(x) => x.is_mut_ref(), ArrayInline(x) => x.is_mut_ref(),
ArrayInit(x) => x.is_mut_ref(), ArrayInit(x) => x.is_mut_ref(),
ArrayAccess(x) => x.is_mut_ref(), ArrayAccess(x) => x.is_mut_ref(),
@ -256,7 +256,7 @@ impl<'a> ExpressionNode<'a> for Expression<'a> {
Unary(x) => x.const_value(), Unary(x) => x.const_value(),
Ternary(x) => x.const_value(), Ternary(x) => x.const_value(),
Cast(x) => x.const_value(), Cast(x) => x.const_value(),
SizeOf(x) => x.const_value(), LengthOf(x) => x.const_value(),
ArrayInline(x) => x.const_value(), ArrayInline(x) => x.const_value(),
ArrayInit(x) => x.const_value(), ArrayInit(x) => x.const_value(),
ArrayAccess(x) => x.const_value(), ArrayAccess(x) => x.const_value(),
@ -278,7 +278,7 @@ impl<'a> ExpressionNode<'a> for Expression<'a> {
Unary(x) => x.is_consty(), Unary(x) => x.is_consty(),
Ternary(x) => x.is_consty(), Ternary(x) => x.is_consty(),
Cast(x) => x.is_consty(), Cast(x) => x.is_consty(),
SizeOf(x) => x.is_consty(), LengthOf(x) => x.is_consty(),
ArrayInline(x) => x.is_consty(), ArrayInline(x) => x.is_consty(),
ArrayInit(x) => x.is_consty(), ArrayInit(x) => x.is_consty(),
ArrayAccess(x) => x.is_consty(), ArrayAccess(x) => x.is_consty(),
@ -317,9 +317,9 @@ impl<'a> FromAst<'a, leo_ast::Expression> for &'a Expression<'a> {
.context .context
.alloc_expression(CastExpression::from_ast(scope, cast, expected_type).map(Expression::Cast)?), .alloc_expression(CastExpression::from_ast(scope, cast, expected_type).map(Expression::Cast)?),
SizeOf(sizeof) => scope LengthOf(lengthof) => scope.context.alloc_expression(
.context LengthOfExpression::from_ast(scope, lengthof, expected_type).map(Expression::LengthOf)?,
.alloc_expression(SizeOfExpression::from_ast(scope, sizeof, expected_type).map(Expression::SizeOf)?), ),
ArrayInline(array_inline) => scope.context.alloc_expression( ArrayInline(array_inline) => scope.context.alloc_expression(
ArrayInlineExpression::from_ast(scope, array_inline, expected_type).map(Expression::ArrayInline)?, ArrayInlineExpression::from_ast(scope, array_inline, expected_type).map(Expression::ArrayInline)?,
@ -373,7 +373,7 @@ impl<'a> Into<leo_ast::Expression> for &Expression<'a> {
Unary(x) => leo_ast::Expression::Unary(x.into()), Unary(x) => leo_ast::Expression::Unary(x.into()),
Ternary(x) => leo_ast::Expression::Ternary(x.into()), Ternary(x) => leo_ast::Expression::Ternary(x.into()),
Cast(x) => leo_ast::Expression::Cast(x.into()), Cast(x) => leo_ast::Expression::Cast(x.into()),
SizeOf(x) => leo_ast::Expression::SizeOf(x.into()), LengthOf(x) => leo_ast::Expression::LengthOf(x.into()),
ArrayInline(x) => leo_ast::Expression::ArrayInline(x.into()), ArrayInline(x) => leo_ast::Expression::ArrayInline(x.into()),
ArrayInit(x) => leo_ast::Expression::ArrayInit(x.into()), ArrayInit(x) => leo_ast::Expression::ArrayInit(x.into()),
ArrayAccess(x) => leo_ast::Expression::ArrayAccess(x.into()), ArrayAccess(x) => leo_ast::Expression::ArrayAccess(x.into()),

View File

@ -48,7 +48,7 @@ impl<'a, T: Monoid, R: MonoidalReducerExpression<'a, T>> MonoidalDirector<'a, T,
Expression::CircuitInit(e) => self.reduce_circuit_init(e), Expression::CircuitInit(e) => self.reduce_circuit_init(e),
Expression::Ternary(e) => self.reduce_ternary_expression(e), Expression::Ternary(e) => self.reduce_ternary_expression(e),
Expression::Cast(e) => self.reduce_cast_expression(e), Expression::Cast(e) => self.reduce_cast_expression(e),
Expression::SizeOf(e) => self.reduce_sizeof_expression(e), Expression::LengthOf(e) => self.reduce_lengthof_expression(e),
Expression::Constant(e) => self.reduce_constant(e), Expression::Constant(e) => self.reduce_constant(e),
Expression::TupleAccess(e) => self.reduce_tuple_access(e), Expression::TupleAccess(e) => self.reduce_tuple_access(e),
Expression::TupleInit(e) => self.reduce_tuple_init(e), Expression::TupleInit(e) => self.reduce_tuple_init(e),
@ -139,10 +139,10 @@ impl<'a, T: Monoid, R: MonoidalReducerExpression<'a, T>> MonoidalDirector<'a, T,
self.reducer.reduce_cast_expression(input, inner) self.reducer.reduce_cast_expression(input, inner)
} }
pub fn reduce_sizeof_expression(&mut self, input: &SizeOfExpression<'a>) -> T { pub fn reduce_lengthof_expression(&mut self, input: &LengthOfExpression<'a>) -> T {
let inner = self.reduce_expression(input.inner.get()); let inner = self.reduce_expression(input.inner.get());
self.reducer.reduce_sizeof_expression(input, inner) self.reducer.reduce_lengthof_expression(input, inner)
} }
pub fn reduce_constant(&mut self, input: &Constant<'a>) -> T { pub fn reduce_constant(&mut self, input: &Constant<'a>) -> T {

View File

@ -68,7 +68,7 @@ pub trait MonoidalReducerExpression<'a, T: Monoid> {
inner inner
} }
fn reduce_sizeof_expression(&mut self, input: &SizeOfExpression<'a>, inner: T) -> T { fn reduce_lengthof_expression(&mut self, input: &LengthOfExpression<'a>, inner: T) -> T {
inner inner
} }

View File

@ -48,7 +48,7 @@ impl<'a, R: ReconstructingReducerExpression<'a>> ReconstructingDirector<'a, R> {
Expression::CircuitInit(e) => self.reduce_circuit_init(e), Expression::CircuitInit(e) => self.reduce_circuit_init(e),
Expression::Ternary(e) => self.reduce_ternary_expression(e), Expression::Ternary(e) => self.reduce_ternary_expression(e),
Expression::Cast(e) => self.reduce_cast_expression(e), Expression::Cast(e) => self.reduce_cast_expression(e),
Expression::SizeOf(e) => Expression::SizeOf(e), //self.reduce_sizeof_expression(e), // TODO: implement REDUCER Expression::LengthOf(e) => Expression::LengthOf(e), // TODO: implement REDUCER
Expression::Constant(e) => self.reduce_constant(e), Expression::Constant(e) => self.reduce_constant(e),
Expression::TupleAccess(e) => self.reduce_tuple_access(e), Expression::TupleAccess(e) => self.reduce_tuple_access(e),
Expression::TupleInit(e) => self.reduce_tuple_init(e), Expression::TupleInit(e) => self.reduce_tuple_init(e),

View File

@ -76,7 +76,7 @@ pub trait ExpressionVisitor<'a> {
Default::default() Default::default()
} }
fn visit_sizeof_expression(&mut self, input: &SizeOfExpression<'a>) -> VisitResult { fn visit_lengthof_expression(&mut self, input: &LengthOfExpression<'a>) -> VisitResult {
Default::default() Default::default()
} }

View File

@ -61,7 +61,7 @@ impl<'a, R: ExpressionVisitor<'a>> VisitorDirector<'a, R> {
Expression::CircuitInit(e) => self.visit_circuit_init(e), Expression::CircuitInit(e) => self.visit_circuit_init(e),
Expression::Ternary(e) => self.visit_ternary_expression(e), Expression::Ternary(e) => self.visit_ternary_expression(e),
Expression::Cast(e) => self.visit_cast_expression(e), Expression::Cast(e) => self.visit_cast_expression(e),
Expression::SizeOf(e) => self.visit_sizeof_expression(e), Expression::LengthOf(e) => self.visit_lengthof_expression(e),
Expression::Constant(e) => self.visit_constant(e), Expression::Constant(e) => self.visit_constant(e),
Expression::TupleAccess(e) => self.visit_tuple_access(e), Expression::TupleAccess(e) => self.visit_tuple_access(e),
Expression::TupleInit(e) => self.visit_tuple_init(e), Expression::TupleInit(e) => self.visit_tuple_init(e),
@ -196,8 +196,8 @@ impl<'a, R: ExpressionVisitor<'a>> VisitorDirector<'a, R> {
} }
} }
pub fn visit_sizeof_expression(&mut self, input: &SizeOfExpression<'a>) -> ConcreteVisitResult { pub fn visit_lengthof_expression(&mut self, input: &LengthOfExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_sizeof_expression(input) { match self.visitor.visit_lengthof_expression(input) {
VisitResult::VisitChildren => { VisitResult::VisitChildren => {
self.visit_expression(&input.inner)?; self.visit_expression(&input.inner)?;
Ok(()) Ok(())

View File

@ -19,18 +19,18 @@
use super::*; use super::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SizeOfExpression { pub struct LengthOfExpression {
pub inner: Box<Expression>, pub inner: Box<Expression>,
pub span: Span, pub span: Span,
} }
impl fmt::Display for SizeOfExpression { impl fmt::Display for LengthOfExpression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "sizeof {}", self.inner) write!(f, "sizeof {}", self.inner)
} }
} }
impl Node for SizeOfExpression { impl Node for LengthOfExpression {
fn span(&self) -> &Span { fn span(&self) -> &Span {
&self.span &self.span
} }

View File

@ -54,8 +54,8 @@ mod call;
pub use call::*; pub use call::*;
mod cast; mod cast;
pub use cast::*; pub use cast::*;
mod sizeof; mod lengthof;
pub use sizeof::*; pub use lengthof::*;
/// Expression that evaluates to a value /// Expression that evaluates to a value
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
@ -66,7 +66,7 @@ pub enum Expression {
Unary(UnaryExpression), Unary(UnaryExpression),
Ternary(TernaryExpression), Ternary(TernaryExpression),
Cast(CastExpression), Cast(CastExpression),
SizeOf(SizeOfExpression), LengthOf(LengthOfExpression),
ArrayInline(ArrayInlineExpression), ArrayInline(ArrayInlineExpression),
ArrayInit(ArrayInitExpression), ArrayInit(ArrayInitExpression),
@ -103,7 +103,7 @@ impl Node for Expression {
CircuitStaticFunctionAccess(n) => n.span(), CircuitStaticFunctionAccess(n) => n.span(),
Call(n) => n.span(), Call(n) => n.span(),
Cast(n) => n.span(), Cast(n) => n.span(),
SizeOf(n) => n.span(), LengthOf(n) => n.span(),
} }
} }
@ -126,7 +126,7 @@ impl Node for Expression {
CircuitStaticFunctionAccess(n) => n.set_span(span), CircuitStaticFunctionAccess(n) => n.set_span(span),
Call(n) => n.set_span(span), Call(n) => n.set_span(span),
Cast(n) => n.set_span(span), Cast(n) => n.set_span(span),
SizeOf(n) => n.set_span(span), LengthOf(n) => n.set_span(span),
} }
} }
} }
@ -151,7 +151,7 @@ impl fmt::Display for Expression {
CircuitStaticFunctionAccess(n) => n.fmt(f), CircuitStaticFunctionAccess(n) => n.fmt(f),
Call(n) => n.fmt(f), Call(n) => n.fmt(f),
Cast(n) => n.fmt(f), Cast(n) => n.fmt(f),
SizeOf(n) => n.fmt(f), LengthOf(n) => n.fmt(f),
} }
} }
} }

View File

@ -57,7 +57,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
Expression::Unary(unary) => Expression::Unary(self.reduce_unary(unary)?), Expression::Unary(unary) => Expression::Unary(self.reduce_unary(unary)?),
Expression::Ternary(ternary) => Expression::Ternary(self.reduce_ternary(ternary)?), Expression::Ternary(ternary) => Expression::Ternary(self.reduce_ternary(ternary)?),
Expression::Cast(cast) => Expression::Cast(self.reduce_cast(cast)?), Expression::Cast(cast) => Expression::Cast(self.reduce_cast(cast)?),
Expression::SizeOf(sizeof) => Expression::SizeOf(sizeof.clone()), // Expression::SizeOf(self.reduce_sizeof(sizeof)?), // TODO: add reducer Expression::LengthOf(sizeof) => Expression::LengthOf(sizeof.clone()), // Expression::LengthOf(self.reduce_sizeof(sizeof)?), // TODO: add reducer
Expression::ArrayInline(array_inline) => Expression::ArrayInline(self.reduce_array_inline(array_inline)?), Expression::ArrayInline(array_inline) => Expression::ArrayInline(self.reduce_array_inline(array_inline)?),
Expression::ArrayInit(array_init) => Expression::ArrayInit(self.reduce_array_init(array_init)?), Expression::ArrayInit(array_init) => Expression::ArrayInit(self.reduce_array_init(array_init)?),

View File

@ -98,8 +98,8 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
// Cast // Cast
Expression::Cast(_) => unimplemented!("casts not implemented"), Expression::Cast(_) => unimplemented!("casts not implemented"),
// SizeOf // LengthOf
Expression::SizeOf(sizeof) => self.enforce_sizeof(cs, sizeof, span), Expression::LengthOf(sizeof) => self.enforce_sizeof(cs, sizeof, span),
// Variables // Variables
Expression::VariableRef(variable_ref) => self.evaluate_ref(variable_ref), Expression::VariableRef(variable_ref) => self.evaluate_ref(variable_ref),

View File

@ -21,7 +21,7 @@ use crate::{
value::{ConstrainedValue, Integer}, value::{ConstrainedValue, Integer},
GroupType, GroupType,
}; };
use leo_asg::{ConstInt, SizeOfExpression}; use leo_asg::{ConstInt, LengthOfExpression};
use leo_errors::{Result, Span}; use leo_errors::{Result, Span};
use snarkvm_fields::PrimeField; use snarkvm_fields::PrimeField;
@ -32,16 +32,16 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
pub fn enforce_sizeof<CS: ConstraintSystem<F>>( pub fn enforce_sizeof<CS: ConstraintSystem<F>>(
&mut self, &mut self,
cs: &mut CS, cs: &mut CS,
sizeof: &'a SizeOfExpression<'a>, lengthof: &'a LengthOfExpression<'a>,
_span: &Span, span: &Span,
) -> Result<ConstrainedValue<'a, F, G>> { ) -> Result<ConstrainedValue<'a, F, G>> {
let value = self.enforce_expression(cs, sizeof.inner.get())?; let value = self.enforce_expression(cs, lengthof.inner.get())?;
Ok(match value { Ok(match value {
ConstrainedValue::Array(array) => { ConstrainedValue::Array(array) => {
ConstrainedValue::Integer(Integer::new(&ConstInt::U32(array.len() as u32))) ConstrainedValue::Integer(Integer::new(&ConstInt::U32(array.len() as u32)))
} }
_ => unimplemented!("sizeof can only be used for arrays"), _ => return Err(leo_errors::CompilerError::lengthof_can_only_be_used_on_arrays(span).into()),
}) })
} }
} }

View File

@ -14,5 +14,5 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod sizeof; pub mod lengthof;
pub use self::sizeof::*; pub use self::lengthof::*;

View File

@ -834,4 +834,11 @@ create_errors!(
msg: format!("no implementation found for `{}`", operation), msg: format!("no implementation found for `{}`", operation),
help: None, help: None,
} }
@formatted
lengthof_can_only_be_used_on_arrays {
args: (),
msg: "length() can only be called on an array value".to_string(),
help: None,
}
); );

View File

@ -222,3 +222,8 @@ fn main() -> Result<()> {
Ok(()) Ok(())
} }
#[test]
fn test_true() {
assert!(true);
}

View File

@ -402,8 +402,20 @@ impl ParserContext {
/// ///
pub fn parse_postfix_expression(&mut self) -> Result<Expression> { pub fn parse_postfix_expression(&mut self) -> Result<Expression> {
let mut expr = self.parse_primary_expression()?; let mut expr = self.parse_primary_expression()?;
while let Some(token) = self.eat_any(&[Token::LeftSquare, Token::Dot, Token::LeftParen, Token::DoubleColon]) { while let Some(token) = self.eat_any(&[
Token::LeftSquare,
Token::Dot,
Token::LeftParen,
Token::DoubleColon,
Token::LengthOf,
]) {
match token.token { match token.token {
Token::LengthOf => {
expr = Expression::LengthOf(LengthOfExpression {
span: expr.span().clone(),
inner: Box::new(expr),
})
}
Token::LeftSquare => { Token::LeftSquare => {
if self.eat(Token::DotDot).is_some() { if self.eat(Token::DotDot).is_some() {
let right = if self.peek_token().as_ref() != &Token::RightSquare { let right = if self.peek_token().as_ref() != &Token::RightSquare {
@ -727,10 +739,10 @@ impl ParserContext {
}; };
Expression::Identifier(ident) Expression::Identifier(ident)
} }
Token::SizeOf => Expression::SizeOf(SizeOfExpression { // Token::LengthOf => Expression::LengthOf(LengthOfExpression {
span, // span,
inner: Box::new(self.parse_primary_expression()?), // inner: Box::new(self.parse_primary_expression()?),
}), // }),
token => { token => {
return Err(ParserError::unexpected_str(token, "expression", &span).into()); return Err(ParserError::unexpected_str(token, "expression", &span).into());
} }

View File

@ -388,6 +388,9 @@ impl Token {
return (len, Some(Token::DotDotDot)); return (len, Some(Token::DotDotDot));
} else if let Some(len) = eat(input, "..") { } else if let Some(len) = eat(input, "..") {
return (len, Some(Token::DotDot)); return (len, Some(Token::DotDot));
} else if let Some(len) = eat(input, ".length()") {
// FIXME: remove this code once we allow method calls
return (len, Some(Token::LengthOf));
} }
return (1, Some(Token::Dot)); return (1, Some(Token::Dot));
} }
@ -519,7 +522,6 @@ impl Token {
"string" => Token::String, "string" => Token::String,
"true" => Token::True, "true" => Token::True,
"type" => Token::Type, "type" => Token::Type,
"sizeof" => Token::SizeOf,
"u8" => Token::U8, "u8" => Token::U8,
"u16" => Token::U16, "u16" => Token::U16,
"u32" => Token::U32, "u32" => Token::U32,

View File

@ -140,8 +140,9 @@ pub enum Token {
String, String,
Type, Type,
// Operators // Not yet in ABNF
SizeOf, // arr.length() token - hacky zone
LengthOf,
// Not yet in ABNF // Not yet in ABNF
// BitAnd, // BitAnd,
@ -199,7 +200,7 @@ pub const KEYWORD_TOKENS: &[Token] = &[
Token::String, Token::String,
Token::True, Token::True,
Token::Type, Token::Type,
Token::SizeOf, Token::LengthOf,
Token::U8, Token::U8,
Token::U16, Token::U16,
Token::U32, Token::U32,
@ -312,7 +313,7 @@ impl fmt::Display for Token {
Static => write!(f, "static"), Static => write!(f, "static"),
String => write!(f, "string"), String => write!(f, "string"),
Type => write!(f, "type"), Type => write!(f, "type"),
SizeOf => write!(f, "sizeof"), LengthOf => write!(f, ".length()"), // FIXME
Eof => write!(f, ""), Eof => write!(f, ""),
// BitAnd => write!(f, "&"), // BitAnd => write!(f, "&"),
// BitAndEq => write!(f, "&="), // BitAndEq => write!(f, "&="),

View File

@ -1,6 +1,7 @@
[main] [main]
y: bool = true; y: bool = true;
n: bool = false; n: bool = false;
a: [char; 11] = "hello world";
[registers] [registers]
r0: bool = false; r0: bool = false;

View File

@ -0,0 +1,9 @@
/*
namespace: Compile
expectation: Pass
input_file: input/dummy.in
*/
function main(a: [char; 11], y: bool) -> bool {
return y == (a.length() == 11);
}

View File

@ -0,0 +1,9 @@
/*
namespace: Compile
expectation: Fail
input_file: input/dummy.in
*/
function main(y: bool) {
return 10.length() == 10u8;
}

View File

@ -0,0 +1,17 @@
/*
namespace: Compile
expectation: Pass
input_file: input/dummy.in
*/
function main(y: bool) -> bool {
let x = 0u8;
for i in 0..strlen("I swear to god I had something for this") {
x += 1;
}
return (x == 39) == y;
}
function strlen(str: [char; _]) -> u32 {
return str.length();
}

View File

@ -0,0 +1,22 @@
---
namespace: Compile
expectation: Pass
outputs:
- circuit:
num_public_variables: 0
num_private_variables: 12
num_constraints: 1
at: 336f487fe39f24aef980deaaf7d6dddcc0dbfa8f121c3470b05546c1ac13f87e
bt: ae35381db5558456a49acb22132b4930efd53b90eb2668df06c5d9c1a6b0ab9f
ct: cf1cbb66a638b4860a516671fb74850e6ccf787fe6c4c8d29e9c04efe880bd05
output:
- input_file: input/dummy.in
output:
registers:
r0:
type: bool
value: "true"
initial_ast: f8b64e1675dcb9624011ccf7a6ee9cd4fd7767f9fffbea3c87cbb21cbe734d67
imports_resolved_ast: f8b64e1675dcb9624011ccf7a6ee9cd4fd7767f9fffbea3c87cbb21cbe734d67
canonicalized_ast: f8b64e1675dcb9624011ccf7a6ee9cd4fd7767f9fffbea3c87cbb21cbe734d67
type_inferenced_ast: 4934eaa23f00ce1dfd8e1062e7a6b3bdbc6c5cd4e5f53641a930e910fde34206

View File

@ -0,0 +1,5 @@
---
namespace: Compile
expectation: Fail
outputs:
- "Error [EASG0373025]: unexpected type, expected: '()', received: 'bool'\n --> compiler-test:4:12\n |\n 4 | return 10.length() == 10u8;\n | ^^^^^^^^^^^^^^^^^^^"

View File

@ -0,0 +1,22 @@
---
namespace: Compile
expectation: Pass
outputs:
- circuit:
num_public_variables: 0
num_private_variables: 1
num_constraints: 1
at: 042610d0fd1fe6d6ac112138f8755752f44c7d2a00f1b5960574d6da5cda393f
bt: e97756698880ab7555a959a5fb5c6b4e15bd64612aa677adbfe2d0bd91f0a83c
ct: cf1cbb66a638b4860a516671fb74850e6ccf787fe6c4c8d29e9c04efe880bd05
output:
- input_file: input/dummy.in
output:
registers:
r0:
type: bool
value: "true"
initial_ast: 3cd71a0e1c6b7aea15b7822c7ad19f400886e82ded67c55d24046d179d202ab0
imports_resolved_ast: 3cd71a0e1c6b7aea15b7822c7ad19f400886e82ded67c55d24046d179d202ab0
canonicalized_ast: 2f670f34d1dffb2dd07782b566d6da942ea1cbb0ed5c3da1fc7e78e39fa096f7
type_inferenced_ast: 4c616d4451798fff86c86c586729b643ad8194248c22956880f168b40a8eddba