From c9b565cbfbf8d34650271054099185b173075c73 Mon Sep 17 00:00:00 2001 From: gluax Date: Tue, 8 Jun 2021 12:29:49 -0700 Subject: [PATCH] fix slice assignment for strings --- ast/src/reducer/canonicalization.rs | 8 ++ compiler/tests/canonicalization/mod.rs | 15 +-- .../string_transformation.json | 111 ++++++++++++++++-- .../string_transformation.leo | 1 + tests/compiler/string/replace.leo | 12 ++ .../compiler/compiler/char/neq.leo.out | 10 +- .../compiler/compiler/string/replace.leo.out | 24 ++++ 7 files changed, 149 insertions(+), 32 deletions(-) create mode 100644 tests/compiler/string/replace.leo create mode 100644 tests/expectations/compiler/compiler/string/replace.leo.out diff --git a/ast/src/reducer/canonicalization.rs b/ast/src/reducer/canonicalization.rs index 218e90a740..5f38b95797 100644 --- a/ast/src/reducer/canonicalization.rs +++ b/ast/src/reducer/canonicalization.rs @@ -493,6 +493,8 @@ impl ReconstructingReducer for Canonicalizer { ))); } + // println!("crs {}, elee {:?}", string, elements); + Ok(Expression::ArrayInline(ArrayInlineExpression { elements, span: span.clone(), @@ -602,6 +604,12 @@ impl ReconstructingReducer for Canonicalizer { span: assign.span.clone(), }) } + Expression::ArrayInline(_) => Ok(AssignStatement { + operation: AssignOperation::Assign, + assignee, + value, + span: assign.span.clone(), + }), _ => Ok(assign.clone()), } } diff --git a/compiler/tests/canonicalization/mod.rs b/compiler/tests/canonicalization/mod.rs index 5a86dcf4b2..34d3fe0c24 100644 --- a/compiler/tests/canonicalization/mod.rs +++ b/compiler/tests/canonicalization/mod.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{assert_satisfied, parse_program, parse_program_with_input}; +use crate::parse_program; use leo_ast::Ast; use leo_parser::parser; @@ -35,9 +35,6 @@ pub fn parse_program_ast(file_string: &str) -> Ast { fn test_big_self_in_circuit_replacement() { // Check program is valid. let program_string = include_str!("big_self_in_circuit_replacement.leo"); - let program = parse_program(program_string).unwrap(); - assert_satisfied(program); - // Check we get expected ast. let ast = parse_program_ast(program_string); let expected_json = include_str!("big_self_in_circuit_replacement.json"); @@ -57,10 +54,6 @@ fn test_big_self_outside_circuit_fail() { #[test] fn test_array_expansion() { let program_string = include_str!("array_expansion.leo"); - let input_string = include_str!("input/array_expansion.in"); - let program = parse_program_with_input(program_string, input_string).unwrap(); - assert_satisfied(program); - let ast = parse_program_ast(program_string); let expected_json = include_str!("array_expansion.json"); let expected_ast: Ast = Ast::from_json_string(expected_json).expect("Unable to parse json."); @@ -78,9 +71,6 @@ fn test_array_size_zero_fail() { #[test] fn test_compound_assignment() { let program_string = include_str!("compound_assignment.leo"); - let program = parse_program(program_string).unwrap(); - assert_satisfied(program); - let ast = parse_program_ast(program_string); let expected_json = include_str!("compound_assignment.json"); let expected_ast: Ast = Ast::from_json_string(expected_json).expect("Unable to parse json."); @@ -99,9 +89,6 @@ fn test_illegal_array_range_fail() { #[test] fn test_string_transformation() { let program_string = include_str!("string_transformation.leo"); - let program = parse_program(program_string).unwrap(); - assert_satisfied(program); - let ast = parse_program_ast(program_string); let expected_json = include_str!("string_transformation.json"); let expected_ast: Ast = Ast::from_json_string(expected_json).expect("Unable to parse json."); diff --git a/compiler/tests/canonicalization/string_transformation.json b/compiler/tests/canonicalization/string_transformation.json index 9abfbb842f..c53001b02e 100644 --- a/compiler/tests/canonicalization/string_transformation.json +++ b/compiler/tests/canonicalization/string_transformation.json @@ -31,16 +31,7 @@ } } ], - "type_": { - "Array": [ - "Char", - [ - { - "value": "13" - } - ] - ] - }, + "type_": null, "value": { "ArrayInline": { "elements": [ @@ -285,11 +276,105 @@ "content": " let s = \"Hello, World!\";" } } + }, + { + "Assign": { + "operation": "Assign", + "assignee": { + "identifier": "{\"name\":\"s\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":5,\\\"col_stop\\\":6,\\\"path\\\":\\\"\\\",\\\"content\\\":\\\" s[..2] = \\\\\\\"ab\\\\\\\";\\\"}\"}", + "accesses": [ + { + "ArrayRange": [ + null, + { + "Value": { + "Implicit": [ + "2", + { + "line_start": 3, + "line_stop": 3, + "col_start": 9, + "col_stop": 10, + "path": "", + "content": " s[..2] = \"ab\";" + } + ] + } + } + ] + } + ], + "span": { + "line_start": 3, + "line_stop": 3, + "col_start": 5, + "col_stop": 11, + "path": "", + "content": " s[..2] = \"ab\";" + } + }, + "value": { + "ArrayInline": { + "elements": [ + { + "Expression": { + "Value": { + "Char": [ + "a", + { + "line_start": 3, + "line_stop": 3, + "col_start": 14, + "col_stop": 18, + "path": "", + "content": " s[..2] = \"ab\";" + } + ] + } + } + }, + { + "Expression": { + "Value": { + "Char": [ + "b", + { + "line_start": 3, + "line_stop": 3, + "col_start": 14, + "col_stop": 18, + "path": "", + "content": " s[..2] = \"ab\";" + } + ] + } + } + } + ], + "span": { + "line_start": 3, + "line_stop": 3, + "col_start": 14, + "col_stop": 18, + "path": "", + "content": " s[..2] = \"ab\";" + } + } + }, + "span": { + "line_start": 3, + "line_stop": 3, + "col_start": 5, + "col_stop": 18, + "path": "", + "content": " s[..2] = \"ab\";" + } + } } ], "span": { "line_start": 1, - "line_stop": 3, + "line_stop": 4, "col_start": 17, "col_stop": 2, "path": "", @@ -298,11 +383,11 @@ }, "span": { "line_start": 1, - "line_stop": 3, + "line_stop": 4, "col_start": 1, "col_stop": 2, "path": "", - "content": "function main() {\n...\n}" + "content": "function main() {\n...\n}\n" } } } diff --git a/compiler/tests/canonicalization/string_transformation.leo b/compiler/tests/canonicalization/string_transformation.leo index e3830d38ee..2f58fecf09 100644 --- a/compiler/tests/canonicalization/string_transformation.leo +++ b/compiler/tests/canonicalization/string_transformation.leo @@ -1,3 +1,4 @@ function main() { let s = "Hello, World!"; + s[..2] = "ab"; } \ No newline at end of file diff --git a/tests/compiler/string/replace.leo b/tests/compiler/string/replace.leo new file mode 100644 index 0000000000..175c9a585d --- /dev/null +++ b/tests/compiler/string/replace.leo @@ -0,0 +1,12 @@ +/* +namespace: Compile +expectation: Pass +input_file: + - inputs/string.in + - inputs/weird.in +*/ + +function main(s1: [char; 13], s2: [char; 4]) -> bool { + s1[..4] = s2; + return s1 != "Hello, World!"; +} \ No newline at end of file diff --git a/tests/expectations/compiler/compiler/char/neq.leo.out b/tests/expectations/compiler/compiler/char/neq.leo.out index 48f1729114..8c12ebf9ac 100644 --- a/tests/expectations/compiler/compiler/char/neq.leo.out +++ b/tests/expectations/compiler/compiler/char/neq.leo.out @@ -4,11 +4,11 @@ expectation: Pass outputs: - circuit: num_public_variables: 0 - num_private_variables: 6 - num_constraints: 3 - at: cc1286e0b6fa2e90fb6f0880431a7c1e2cc37a329fae3aff1c13f51036c66f12 - bt: 02c492cb6df07172e56cffd0cfd902a8443921e1256a2d907bbabd30bf6b8f6d - ct: a1f8e2b168c0f2f28f0ca3f16ce9b25ba7f7c410cfd68b0912bf19c90b53f2a2 + num_private_variables: 7 + num_constraints: 4 + at: a2dab6a003b17db5afe774f7100f9c5c73a5ff060db42422c13a4c475af7ccd9 + bt: 3f3314eb65daf16719533fc973a8934ce4a68db2aee344f5d3f0d63bb997f127 + ct: 03ccd28b26810eecb8c66d87f77485bd0d8c0d1a114a122422675e0608152c49 output: - input_file: inputs/ascii.in output: diff --git a/tests/expectations/compiler/compiler/string/replace.leo.out b/tests/expectations/compiler/compiler/string/replace.leo.out new file mode 100644 index 0000000000..d76fb693cc --- /dev/null +++ b/tests/expectations/compiler/compiler/string/replace.leo.out @@ -0,0 +1,24 @@ +--- +namespace: Compile +expectation: Pass +outputs: + - circuit: + num_public_variables: 0 + num_private_variables: 68 + num_constraints: 51 + at: 45fa182371664010950da6ec3fa6360de34dda5cb814b3c8bd119a09e86b5c77 + bt: 4da3499799890924c2910625e052c6757e7cfd857ebbd18afd3d16b5bd0e9a73 + ct: 183c15a7bf0d7c109b082a55e696874797e7ce685096881ce803f6bfa453a875 + output: + - input_file: inputs/string.in + output: + registers: + out: + type: bool + value: "true" + - input_file: inputs/weird.in + output: + registers: + out: + type: bool + value: "true"