diff --git a/asg/src/expression/sizeof.rs b/asg/src/expression/lengthof.rs similarity index 67% rename from asg/src/expression/sizeof.rs rename to asg/src/expression/lengthof.rs index bc580eeae1..ee8c883ebb 100644 --- a/asg/src/expression/sizeof.rs +++ b/asg/src/expression/lengthof.rs @@ -21,19 +21,19 @@ use leo_errors::{Result, Span}; use std::cell::Cell; #[derive(Clone)] -pub struct SizeOfExpression<'a> { +pub struct LengthOfExpression<'a> { pub parent: Cell>>, pub span: Option, pub inner: Cell<&'a Expression<'a>>, } -impl<'a> Node for SizeOfExpression<'a> { +impl<'a> Node for LengthOfExpression<'a> { fn span(&self) -> Option<&Span> { 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>) { self.parent.replace(Some(parent)); } @@ -47,10 +47,7 @@ impl<'a> ExpressionNode<'a> for SizeOfExpression<'a> { } fn get_type(&self) -> Option> { - // TODO: make decision on get type method - // Should it be always u32? For indexes? - // How type casts are applied to this? - Some(Type::Integer(IntegerType::U32)) + Some(Type::Integer(IntegerType::U32)) // For now we stick to U32 value type } fn is_mut_ref(&self) -> bool { @@ -58,16 +55,6 @@ impl<'a> ExpressionNode<'a> for SizeOfExpression<'a> { } fn const_value(&self) -> Option { - 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 } @@ -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( scope: &'a Scope<'a>, - value: &leo_ast::SizeOfExpression, + value: &leo_ast::LengthOfExpression, _expected_type: Option>, - ) -> Result> { + ) -> Result> { let inner = <&Expression<'a>>::from_ast(scope, &*value.inner, None)?; - Ok(SizeOfExpression { + Ok(LengthOfExpression { parent: Cell::new(None), span: Some(value.span.clone()), inner: Cell::new(inner), @@ -92,9 +79,9 @@ impl<'a> FromAst<'a, leo_ast::SizeOfExpression> for SizeOfExpression<'a> { } } -impl<'a> Into for &SizeOfExpression<'a> { - fn into(self) -> leo_ast::SizeOfExpression { - leo_ast::SizeOfExpression { +impl<'a> Into for &LengthOfExpression<'a> { + fn into(self) -> leo_ast::LengthOfExpression { + leo_ast::LengthOfExpression { inner: Box::new(self.inner.get().into()), span: self.span.clone().unwrap_or_default(), } diff --git a/asg/src/expression/mod.rs b/asg/src/expression/mod.rs index 34c0c959bd..734b227344 100644 --- a/asg/src/expression/mod.rs +++ b/asg/src/expression/mod.rs @@ -65,8 +65,8 @@ pub use variable_ref::*; mod cast; pub use cast::*; -mod sizeof; -pub use sizeof::*; +mod lengthof; +pub use lengthof::*; use crate::{ConstValue, FromAst, Node, PartialType, Scope, Type}; use leo_errors::{Result, Span}; @@ -79,7 +79,7 @@ pub enum Expression<'a> { Unary(UnaryExpression<'a>), Ternary(TernaryExpression<'a>), Cast(CastExpression<'a>), - SizeOf(SizeOfExpression<'a>), + LengthOf(LengthOfExpression<'a>), ArrayInline(ArrayInlineExpression<'a>), ArrayInit(ArrayInitExpression<'a>), @@ -111,7 +111,7 @@ impl<'a> Node for Expression<'a> { Unary(x) => x.span(), Ternary(x) => x.span(), Cast(x) => x.span(), - SizeOf(x) => x.span(), + LengthOf(x) => x.span(), ArrayInline(x) => x.span(), ArrayInit(x) => x.span(), ArrayAccess(x) => x.span(), @@ -146,7 +146,7 @@ impl<'a> ExpressionNode<'a> for Expression<'a> { Unary(x) => x.set_parent(parent), Ternary(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), ArrayInit(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(), Ternary(x) => x.get_parent(), Cast(x) => x.get_parent(), - SizeOf(x) => x.get_parent(), + LengthOf(x) => x.get_parent(), ArrayInline(x) => x.get_parent(), ArrayInit(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), Ternary(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), ArrayInit(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(), Ternary(x) => x.get_type(), Cast(x) => x.get_type(), - SizeOf(x) => x.get_type(), + LengthOf(x) => x.get_type(), ArrayInline(x) => x.get_type(), ArrayInit(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(), Ternary(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(), ArrayInit(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(), Ternary(x) => x.const_value(), Cast(x) => x.const_value(), - SizeOf(x) => x.const_value(), + LengthOf(x) => x.const_value(), ArrayInline(x) => x.const_value(), ArrayInit(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(), Ternary(x) => x.is_consty(), Cast(x) => x.is_consty(), - SizeOf(x) => x.is_consty(), + LengthOf(x) => x.is_consty(), ArrayInline(x) => x.is_consty(), ArrayInit(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 .alloc_expression(CastExpression::from_ast(scope, cast, expected_type).map(Expression::Cast)?), - SizeOf(sizeof) => scope - .context - .alloc_expression(SizeOfExpression::from_ast(scope, sizeof, expected_type).map(Expression::SizeOf)?), + LengthOf(lengthof) => scope.context.alloc_expression( + LengthOfExpression::from_ast(scope, lengthof, expected_type).map(Expression::LengthOf)?, + ), ArrayInline(array_inline) => scope.context.alloc_expression( ArrayInlineExpression::from_ast(scope, array_inline, expected_type).map(Expression::ArrayInline)?, @@ -373,7 +373,7 @@ impl<'a> Into for &Expression<'a> { Unary(x) => leo_ast::Expression::Unary(x.into()), Ternary(x) => leo_ast::Expression::Ternary(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()), ArrayInit(x) => leo_ast::Expression::ArrayInit(x.into()), ArrayAccess(x) => leo_ast::Expression::ArrayAccess(x.into()), diff --git a/asg/src/reducer/monoidal_director.rs b/asg/src/reducer/monoidal_director.rs index 182f2aca05..8ce0e1e58b 100644 --- a/asg/src/reducer/monoidal_director.rs +++ b/asg/src/reducer/monoidal_director.rs @@ -48,7 +48,7 @@ impl<'a, T: Monoid, R: MonoidalReducerExpression<'a, T>> MonoidalDirector<'a, T, Expression::CircuitInit(e) => self.reduce_circuit_init(e), Expression::Ternary(e) => self.reduce_ternary_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::TupleAccess(e) => self.reduce_tuple_access(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) } - 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()); - self.reducer.reduce_sizeof_expression(input, inner) + self.reducer.reduce_lengthof_expression(input, inner) } pub fn reduce_constant(&mut self, input: &Constant<'a>) -> T { diff --git a/asg/src/reducer/monoidal_reducer.rs b/asg/src/reducer/monoidal_reducer.rs index d6b5cac1fc..f33b311f26 100644 --- a/asg/src/reducer/monoidal_reducer.rs +++ b/asg/src/reducer/monoidal_reducer.rs @@ -68,7 +68,7 @@ pub trait MonoidalReducerExpression<'a, T: Monoid> { 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 } diff --git a/asg/src/reducer/reconstructing_director.rs b/asg/src/reducer/reconstructing_director.rs index 708c35a0fe..92029b0c43 100644 --- a/asg/src/reducer/reconstructing_director.rs +++ b/asg/src/reducer/reconstructing_director.rs @@ -48,7 +48,7 @@ impl<'a, R: ReconstructingReducerExpression<'a>> ReconstructingDirector<'a, R> { Expression::CircuitInit(e) => self.reduce_circuit_init(e), Expression::Ternary(e) => self.reduce_ternary_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::TupleAccess(e) => self.reduce_tuple_access(e), Expression::TupleInit(e) => self.reduce_tuple_init(e), diff --git a/asg/src/reducer/visitor.rs b/asg/src/reducer/visitor.rs index d577e958bb..4f3b468ab5 100644 --- a/asg/src/reducer/visitor.rs +++ b/asg/src/reducer/visitor.rs @@ -76,7 +76,7 @@ pub trait ExpressionVisitor<'a> { Default::default() } - fn visit_sizeof_expression(&mut self, input: &SizeOfExpression<'a>) -> VisitResult { + fn visit_lengthof_expression(&mut self, input: &LengthOfExpression<'a>) -> VisitResult { Default::default() } diff --git a/asg/src/reducer/visitor_director.rs b/asg/src/reducer/visitor_director.rs index 5c56607b3e..5490f4b014 100644 --- a/asg/src/reducer/visitor_director.rs +++ b/asg/src/reducer/visitor_director.rs @@ -61,7 +61,7 @@ impl<'a, R: ExpressionVisitor<'a>> VisitorDirector<'a, R> { Expression::CircuitInit(e) => self.visit_circuit_init(e), Expression::Ternary(e) => self.visit_ternary_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::TupleAccess(e) => self.visit_tuple_access(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 { - match self.visitor.visit_sizeof_expression(input) { + pub fn visit_lengthof_expression(&mut self, input: &LengthOfExpression<'a>) -> ConcreteVisitResult { + match self.visitor.visit_lengthof_expression(input) { VisitResult::VisitChildren => { self.visit_expression(&input.inner)?; Ok(()) diff --git a/ast/src/expression/sizeof.rs b/ast/src/expression/lengthof.rs similarity index 91% rename from ast/src/expression/sizeof.rs rename to ast/src/expression/lengthof.rs index 8b41f11f2d..e5dec13a62 100644 --- a/ast/src/expression/sizeof.rs +++ b/ast/src/expression/lengthof.rs @@ -19,18 +19,18 @@ use super::*; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct SizeOfExpression { +pub struct LengthOfExpression { pub inner: Box, pub span: Span, } -impl fmt::Display for SizeOfExpression { +impl fmt::Display for LengthOfExpression { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "sizeof {}", self.inner) } } -impl Node for SizeOfExpression { +impl Node for LengthOfExpression { fn span(&self) -> &Span { &self.span } diff --git a/ast/src/expression/mod.rs b/ast/src/expression/mod.rs index 7196ee4915..d40dcf109e 100644 --- a/ast/src/expression/mod.rs +++ b/ast/src/expression/mod.rs @@ -54,8 +54,8 @@ mod call; pub use call::*; mod cast; pub use cast::*; -mod sizeof; -pub use sizeof::*; +mod lengthof; +pub use lengthof::*; /// Expression that evaluates to a value #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -66,7 +66,7 @@ pub enum Expression { Unary(UnaryExpression), Ternary(TernaryExpression), Cast(CastExpression), - SizeOf(SizeOfExpression), + LengthOf(LengthOfExpression), ArrayInline(ArrayInlineExpression), ArrayInit(ArrayInitExpression), @@ -103,7 +103,7 @@ impl Node for Expression { CircuitStaticFunctionAccess(n) => n.span(), Call(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), Call(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), Call(n) => n.fmt(f), Cast(n) => n.fmt(f), - SizeOf(n) => n.fmt(f), + LengthOf(n) => n.fmt(f), } } } diff --git a/ast/src/reducer/reconstructing_director.rs b/ast/src/reducer/reconstructing_director.rs index 384a3192ff..c74aad5dbc 100644 --- a/ast/src/reducer/reconstructing_director.rs +++ b/ast/src/reducer/reconstructing_director.rs @@ -57,7 +57,7 @@ impl ReconstructingDirector { Expression::Unary(unary) => Expression::Unary(self.reduce_unary(unary)?), Expression::Ternary(ternary) => Expression::Ternary(self.reduce_ternary(ternary)?), 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::ArrayInit(array_init) => Expression::ArrayInit(self.reduce_array_init(array_init)?), diff --git a/compiler/src/expression/expression.rs b/compiler/src/expression/expression.rs index 0f85165252..4f9eb24711 100644 --- a/compiler/src/expression/expression.rs +++ b/compiler/src/expression/expression.rs @@ -98,8 +98,8 @@ impl<'a, F: PrimeField, G: GroupType> ConstrainedProgram<'a, F, G> { // Cast Expression::Cast(_) => unimplemented!("casts not implemented"), - // SizeOf - Expression::SizeOf(sizeof) => self.enforce_sizeof(cs, sizeof, span), + // LengthOf + Expression::LengthOf(sizeof) => self.enforce_sizeof(cs, sizeof, span), // Variables Expression::VariableRef(variable_ref) => self.evaluate_ref(variable_ref), diff --git a/compiler/src/expression/operator/sizeof.rs b/compiler/src/expression/operator/lengthof.rs similarity index 83% rename from compiler/src/expression/operator/sizeof.rs rename to compiler/src/expression/operator/lengthof.rs index 2f26634b96..440d1020d0 100644 --- a/compiler/src/expression/operator/sizeof.rs +++ b/compiler/src/expression/operator/lengthof.rs @@ -21,7 +21,7 @@ use crate::{ value::{ConstrainedValue, Integer}, GroupType, }; -use leo_asg::{ConstInt, SizeOfExpression}; +use leo_asg::{ConstInt, LengthOfExpression}; use leo_errors::{Result, Span}; use snarkvm_fields::PrimeField; @@ -32,16 +32,16 @@ impl<'a, F: PrimeField, G: GroupType> ConstrainedProgram<'a, F, G> { pub fn enforce_sizeof>( &mut self, cs: &mut CS, - sizeof: &'a SizeOfExpression<'a>, - _span: &Span, + lengthof: &'a LengthOfExpression<'a>, + span: &Span, ) -> Result> { - let value = self.enforce_expression(cs, sizeof.inner.get())?; + let value = self.enforce_expression(cs, lengthof.inner.get())?; Ok(match value { ConstrainedValue::Array(array) => { 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()), }) } } diff --git a/compiler/src/expression/operator/mod.rs b/compiler/src/expression/operator/mod.rs index 773e668d58..2d0e37ccc3 100644 --- a/compiler/src/expression/operator/mod.rs +++ b/compiler/src/expression/operator/mod.rs @@ -14,5 +14,5 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -pub mod sizeof; -pub use self::sizeof::*; +pub mod lengthof; +pub use self::lengthof::*; diff --git a/errors/src/compiler/compiler_errors.rs b/errors/src/compiler/compiler_errors.rs index f46b4e9566..84272a3b45 100644 --- a/errors/src/compiler/compiler_errors.rs +++ b/errors/src/compiler/compiler_errors.rs @@ -834,4 +834,11 @@ create_errors!( msg: format!("no implementation found for `{}`", operation), 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, + } ); diff --git a/grammar/src/main.rs b/grammar/src/main.rs index 28b9f6da74..d180ef3bb4 100644 --- a/grammar/src/main.rs +++ b/grammar/src/main.rs @@ -222,3 +222,8 @@ fn main() -> Result<()> { Ok(()) } + +#[test] +fn test_true() { + assert!(true); +} diff --git a/parser/src/parser/expression.rs b/parser/src/parser/expression.rs index 233e253f3b..eb07d5f4f0 100644 --- a/parser/src/parser/expression.rs +++ b/parser/src/parser/expression.rs @@ -402,8 +402,20 @@ impl ParserContext { /// pub fn parse_postfix_expression(&mut self) -> Result { 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 { + Token::LengthOf => { + expr = Expression::LengthOf(LengthOfExpression { + span: expr.span().clone(), + inner: Box::new(expr), + }) + } Token::LeftSquare => { if self.eat(Token::DotDot).is_some() { let right = if self.peek_token().as_ref() != &Token::RightSquare { @@ -727,10 +739,10 @@ impl ParserContext { }; Expression::Identifier(ident) } - Token::SizeOf => Expression::SizeOf(SizeOfExpression { - span, - inner: Box::new(self.parse_primary_expression()?), - }), + // Token::LengthOf => Expression::LengthOf(LengthOfExpression { + // span, + // inner: Box::new(self.parse_primary_expression()?), + // }), token => { return Err(ParserError::unexpected_str(token, "expression", &span).into()); } diff --git a/parser/src/tokenizer/lexer.rs b/parser/src/tokenizer/lexer.rs index 455b125028..7a7c32ed92 100644 --- a/parser/src/tokenizer/lexer.rs +++ b/parser/src/tokenizer/lexer.rs @@ -388,6 +388,9 @@ impl Token { return (len, Some(Token::DotDotDot)); } else if let Some(len) = eat(input, "..") { 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)); } @@ -519,7 +522,6 @@ impl Token { "string" => Token::String, "true" => Token::True, "type" => Token::Type, - "sizeof" => Token::SizeOf, "u8" => Token::U8, "u16" => Token::U16, "u32" => Token::U32, diff --git a/parser/src/tokenizer/token.rs b/parser/src/tokenizer/token.rs index 6c5f0ff981..97ac2164fd 100644 --- a/parser/src/tokenizer/token.rs +++ b/parser/src/tokenizer/token.rs @@ -140,8 +140,9 @@ pub enum Token { String, Type, - // Operators - SizeOf, + // Not yet in ABNF + // arr.length() token - hacky zone + LengthOf, // Not yet in ABNF // BitAnd, @@ -199,7 +200,7 @@ pub const KEYWORD_TOKENS: &[Token] = &[ Token::String, Token::True, Token::Type, - Token::SizeOf, + Token::LengthOf, Token::U8, Token::U16, Token::U32, @@ -312,7 +313,7 @@ impl fmt::Display for Token { Static => write!(f, "static"), String => write!(f, "string"), Type => write!(f, "type"), - SizeOf => write!(f, "sizeof"), + LengthOf => write!(f, ".length()"), // FIXME Eof => write!(f, ""), // BitAnd => write!(f, "&"), // BitAndEq => write!(f, "&="), diff --git a/tests/compiler/array_without_size/input/dummy.in b/tests/compiler/array_without_size/input/dummy.in index 6e43b49ecc..9a0231bcd8 100644 --- a/tests/compiler/array_without_size/input/dummy.in +++ b/tests/compiler/array_without_size/input/dummy.in @@ -1,6 +1,7 @@ [main] y: bool = true; n: bool = false; +a: [char; 11] = "hello world"; [registers] r0: bool = false; diff --git a/tests/compiler/array_without_size/length.leo b/tests/compiler/array_without_size/length.leo new file mode 100644 index 0000000000..234815c8fc --- /dev/null +++ b/tests/compiler/array_without_size/length.leo @@ -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); +} diff --git a/tests/compiler/array_without_size/length_fail.leo b/tests/compiler/array_without_size/length_fail.leo new file mode 100644 index 0000000000..b9175ecdc5 --- /dev/null +++ b/tests/compiler/array_without_size/length_fail.leo @@ -0,0 +1,9 @@ +/* +namespace: Compile +expectation: Fail +input_file: input/dummy.in +*/ + +function main(y: bool) { + return 10.length() == 10u8; +} diff --git a/tests/compiler/array_without_size/length_function.leo b/tests/compiler/array_without_size/length_function.leo new file mode 100644 index 0000000000..7a1d371943 --- /dev/null +++ b/tests/compiler/array_without_size/length_function.leo @@ -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(); +} diff --git a/tests/expectations/compiler/compiler/array_without_size/length.leo.out b/tests/expectations/compiler/compiler/array_without_size/length.leo.out new file mode 100644 index 0000000000..5f40d5fcdd --- /dev/null +++ b/tests/expectations/compiler/compiler/array_without_size/length.leo.out @@ -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 diff --git a/tests/expectations/compiler/compiler/array_without_size/length_fail.leo.out b/tests/expectations/compiler/compiler/array_without_size/length_fail.leo.out new file mode 100644 index 0000000000..40fa5ba7a6 --- /dev/null +++ b/tests/expectations/compiler/compiler/array_without_size/length_fail.leo.out @@ -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 | ^^^^^^^^^^^^^^^^^^^" diff --git a/tests/expectations/compiler/compiler/array_without_size/length_function.leo.out b/tests/expectations/compiler/compiler/array_without_size/length_function.leo.out new file mode 100644 index 0000000000..894eff2349 --- /dev/null +++ b/tests/expectations/compiler/compiler/array_without_size/length_function.leo.out @@ -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