fix slice assignment for strings

This commit is contained in:
gluax 2021-06-08 12:29:49 -07:00
parent c33facbff0
commit c9b565cbfb
7 changed files with 149 additions and 32 deletions

View File

@ -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()),
}
}

View File

@ -14,7 +14,7 @@
// 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/>.
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.");

View File

@ -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"
}
}
}

View File

@ -1,3 +1,4 @@
function main() {
let s = "Hello, World!";
s[..2] = "ab";
}

View File

@ -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!";
}

View File

@ -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:

View File

@ -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"