diff --git a/compiler/src/constraints/expression.rs b/compiler/src/constraints/expression.rs index 0d86812bda..159509f9e6 100644 --- a/compiler/src/constraints/expression.rs +++ b/compiler/src/constraints/expression.rs @@ -46,7 +46,7 @@ impl> ConstrainedProgram { unresolved_identifier: Identifier, ) -> Result, ExpressionError> { // Evaluate the identifier name in the current function scope - let variable_name = new_scope(function_scope, unresolved_identifier.to_string()); + let variable_name = new_scope(function_scope.clone(), unresolved_identifier.to_string()); let identifier_name = new_scope(file_scope, unresolved_identifier.to_string()); let mut result_value = if let Some(value) = self.get(&variable_name) { @@ -701,7 +701,17 @@ impl> ConstrainedProgram { circuit_member: Identifier, span: Span, ) -> Result, ExpressionError> { - println!("access"); + // access a circuit member using the `self` keyword + if let Expression::Identifier(ref identifier) = *circuit_identifier { + if identifier.is_self() { + let self_keyword = new_scope(function_scope, SELF_KEYWORD.to_string()); + let member_value = + self.evaluate_identifier(file_scope, self_keyword, &vec![], circuit_member.clone())?; + + return Ok(member_value); + } + } + let (circuit_name, members) = match self.enforce_expression_value( cs, file_scope.clone(), @@ -731,7 +741,6 @@ impl> ConstrainedProgram { let self_keyword = new_scope(function_scope, SELF_KEYWORD.to_string()); let field = new_scope(self_keyword, stored_member.0.to_string()); - println!("storing"); self.store(field, stored_member.1.clone()); } } diff --git a/compiler/src/constraints/program.rs b/compiler/src/constraints/program.rs index 307b87ecfe..6fe4ee2ce7 100644 --- a/compiler/src/constraints/program.rs +++ b/compiler/src/constraints/program.rs @@ -22,7 +22,6 @@ impl> ConstrainedProgram { } pub(crate) fn store(&mut self, name: String, value: ConstrainedValue) { - println!("{}", name); self.identifiers.insert(name, value); } diff --git a/compiler/src/constraints/value.rs b/compiler/src/constraints/value.rs index c423d0681d..c0ee7ad1bf 100644 --- a/compiler/src/constraints/value.rs +++ b/compiler/src/constraints/value.rs @@ -40,7 +40,9 @@ pub enum ConstrainedValue> { Mutable(Box>), Static(Box>), + Import(String, Box>), + Unresolved(String), } diff --git a/compiler/tests/circuits/mod.rs b/compiler/tests/circuits/mod.rs index 8839501f27..9247b7e6e8 100644 --- a/compiler/tests/circuits/mod.rs +++ b/compiler/tests/circuits/mod.rs @@ -48,7 +48,7 @@ fn output_circuit(program: EdwardsTestCompiler) { ); } -fn fail_expected_member(program: EdwardsTestCompiler) { +fn expect_fail(program: EdwardsTestCompiler) { match get_error(program) { CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError( ExpressionError::Error(_string), @@ -57,15 +57,6 @@ fn fail_expected_member(program: EdwardsTestCompiler) { } } -fn fail_undefined_member(program: EdwardsTestCompiler) { - match get_error(program) { - CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError( - ExpressionError::Error(_), - ))) => {} - error => panic!("Expected undefined circuit member error, got {}", error), - } -} - // Expressions #[test] @@ -81,7 +72,7 @@ fn test_inline_fail() { let bytes = include_bytes!("inline_fail.leo"); let program = parse_program(bytes).unwrap(); - fail_expected_member(program) + expect_fail(program) } #[test] @@ -112,7 +103,7 @@ fn test_member_field_fail() { let bytes = include_bytes!("member_field_fail.leo"); let program = parse_program(bytes).unwrap(); - fail_undefined_member(program); + expect_fail(program); } #[test] @@ -136,7 +127,7 @@ fn test_member_function_fail() { let bytes = include_bytes!("member_function_fail.leo"); let program = parse_program(bytes).unwrap(); - fail_undefined_member(program); + expect_fail(program); } #[test] @@ -144,12 +135,7 @@ fn test_member_function_invalid() { let bytes = include_bytes!("member_function_invalid.leo"); let program = parse_program(bytes).unwrap(); - match get_error(program) { - CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError( - ExpressionError::Error(_), - ))) => {} - error => panic!("Expected invalid function error, got {}", error), - } + expect_fail(program); } #[test] @@ -165,12 +151,7 @@ fn test_member_static_function_undefined() { let bytes = include_bytes!("member_static_function_undefined.leo"); let program = parse_program(bytes).unwrap(); - match get_error(program) { - CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError( - ExpressionError::Error(_), - ))) => {} - error => panic!("Expected undefined static function error, got {}", error), - } + expect_fail(program) } #[test] @@ -178,18 +159,37 @@ fn test_member_static_function_invalid() { let bytes = include_bytes!("member_static_function_invalid.leo"); let program = parse_program(bytes).unwrap(); - match get_error(program) { - CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError( - ExpressionError::Error(_), - ))) => {} - error => panic!("Expected invalid static function error, got {}", error), - } + expect_fail(program) } // Self #[test] -fn test_self() { - let bytes = include_bytes!("self.leo"); +fn test_self_member() { + let bytes = include_bytes!("self_member.leo"); + let program = parse_program(bytes).unwrap(); + + output_one(program); +} + +#[test] +fn test_self_no_member_fail() { + let bytes = include_bytes!("self_no_member_fail.leo"); + let program = parse_program(bytes).unwrap(); + + let _err = get_error(program); +} + +#[test] +fn test_self_member_fail() { + let bytes = include_bytes!("self_member_fail.leo"); + let program = parse_program(bytes).unwrap(); + + let _err = get_error(program); +} + +#[test] +fn test_self_circuit() { + let bytes = include_bytes!("self_circuit.leo"); let program = parse_program(bytes).unwrap(); let output = get_output(program); diff --git a/compiler/tests/circuits/pedersen_mock.leo b/compiler/tests/circuits/pedersen_mock.leo index ce7b714716..f7928c94f0 100644 --- a/compiler/tests/circuits/pedersen_mock.leo +++ b/compiler/tests/circuits/pedersen_mock.leo @@ -1,12 +1,14 @@ circuit PedersenHash { parameters: u32[512] + static function new(parameters: u32[512]) -> Self { return Self { parameters: parameters } } + function hash(bits: bool[512]) -> u32 { let mut digest: u32 = 0; for i in 0..512 { - let base = if bits[i] ? parameters[i] : 0u32; + let base = if bits[i] ? self.parameters[i] : 0u32; digest += base; } return digest diff --git a/compiler/tests/circuits/self.leo b/compiler/tests/circuits/self_circuit.leo similarity index 100% rename from compiler/tests/circuits/self.leo rename to compiler/tests/circuits/self_circuit.leo diff --git a/compiler/tests/circuits/self_member.leo b/compiler/tests/circuits/self_member.leo new file mode 100644 index 0000000000..a1401e15d3 --- /dev/null +++ b/compiler/tests/circuits/self_member.leo @@ -0,0 +1,12 @@ +circuit Foo { + f: u32, + + function bar() -> u32 { + return self.f + } +} + +function main() -> u32 { + let circuit = Foo { f: 1u32 }; + return circuit.bar() +} \ No newline at end of file diff --git a/compiler/tests/circuits/self_member_fail.leo b/compiler/tests/circuits/self_member_fail.leo new file mode 100644 index 0000000000..53050fcb5b --- /dev/null +++ b/compiler/tests/circuits/self_member_fail.leo @@ -0,0 +1,12 @@ +circuit Foo { + f: u32, + + function bar() -> u32 { + return f + } +} + +function main() -> u32 { + let circuit = Foo { f: 1u32 }; + return circuit.bar() +} \ No newline at end of file diff --git a/compiler/tests/circuits/self_no_member_fail.leo b/compiler/tests/circuits/self_no_member_fail.leo new file mode 100644 index 0000000000..bfc5b57e31 --- /dev/null +++ b/compiler/tests/circuits/self_no_member_fail.leo @@ -0,0 +1,10 @@ +circuit Foo { + function bar() -> u32 { + return self.f + } +} + +function main() -> u32 { + let circuit = Foo { }; + return circuit.bar() +} \ No newline at end of file