const_value for array_init, and canonicalization fixes

This commit is contained in:
gluax 2021-06-26 00:00:09 -07:00
parent d3aa50057a
commit 5ebea328fb
9 changed files with 1146 additions and 651 deletions

View File

@ -54,8 +54,8 @@ impl<'a> ExpressionNode<'a> for ArrayInitExpression<'a> {
}
fn const_value(&self) -> Option<ConstValue> {
// not implemented due to performance concerns
None
let element = self.element.get().const_value()?;
Some(ConstValue::Array(vec![element; self.len]))
}
fn is_consty(&self) -> bool {

View File

@ -593,19 +593,13 @@ impl ReconstructingReducer for Canonicalizer {
value: Expression,
) -> Result<AssignStatement, ReducerError> {
match value {
Expression::Binary(binary_expr) if assign.operation == AssignOperation::Assign => Ok(AssignStatement {
operation: AssignOperation::Assign,
assignee,
value: Expression::Binary(binary_expr),
span: assign.span.clone(),
}),
Expression::Binary(binary_expr) if assign.operation != AssignOperation::Assign => {
value if assign.operation != AssignOperation::Assign => {
let left = self.canonicalize_accesses(
Expression::Identifier(assignee.identifier.clone()),
&assignee.accesses,
&assign.span,
)?;
let right = Box::new(Expression::Binary(binary_expr));
let right = Box::new(value);
let op = self.compound_operation_converstion(&assign.operation)?;
let new_value = Expression::Binary(BinaryExpression {
@ -622,36 +616,12 @@ impl ReconstructingReducer for Canonicalizer {
span: assign.span.clone(),
})
}
Expression::Value(value_expr) if assign.operation != AssignOperation::Assign => {
let left = self.canonicalize_accesses(
Expression::Identifier(assignee.identifier.clone()),
&assignee.accesses,
&assign.span,
)?;
let right = Box::new(Expression::Value(value_expr));
let op = self.compound_operation_converstion(&assign.operation)?;
let new_value = Expression::Binary(BinaryExpression {
left,
right,
op,
span: assign.span.clone(),
});
Ok(AssignStatement {
operation: AssignOperation::Assign,
assignee,
value: new_value,
span: assign.span.clone(),
})
}
Expression::ArrayInline(_) => Ok(AssignStatement {
value => Ok(AssignStatement {
operation: AssignOperation::Assign,
assignee,
value,
span: assign.span.clone(),
}),
_ => Ok(assign.clone()),
}
}

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,10 @@ function main() {
x += 20;
console.assert(x == 30u32);
let w = 3u32;
w += x;
console.assert(w == 33u32);
let y = [1u8, 2u8];
y[0] += 3u8;
console.assert(y[0] == 4u8);

View File

@ -718,11 +718,450 @@
"content": " x = \"test1\" == \"test2\";"
}
}
},
{
"Definition": {
"declaration_type": "Let",
"variable_names": [
{
"mutable": true,
"identifier": "{\"name\":\"z\",\"span\":\"{\\\"line_start\\\":6,\\\"line_stop\\\":6,\\\"col_start\\\":9,\\\"col_stop\\\":10,\\\"path\\\":\\\"\\\",\\\"content\\\":\\\" let z = [1u8, 2u8, 3u8, 4u8];\\\"}\"}",
"span": {
"line_start": 6,
"line_stop": 6,
"col_start": 9,
"col_stop": 10,
"path": "",
"content": " let z = [1u8, 2u8, 3u8, 4u8];"
}
}
],
"type_": null,
"value": {
"ArrayInline": {
"elements": [
{
"Expression": {
"Value": {
"Integer": [
"U8",
"1",
{
"line_start": 6,
"line_stop": 6,
"col_start": 14,
"col_stop": 17,
"path": "",
"content": " let z = [1u8, 2u8, 3u8, 4u8];"
}
]
}
}
},
{
"Expression": {
"Value": {
"Integer": [
"U8",
"2",
{
"line_start": 6,
"line_stop": 6,
"col_start": 19,
"col_stop": 22,
"path": "",
"content": " let z = [1u8, 2u8, 3u8, 4u8];"
}
]
}
}
},
{
"Expression": {
"Value": {
"Integer": [
"U8",
"3",
{
"line_start": 6,
"line_stop": 6,
"col_start": 24,
"col_stop": 27,
"path": "",
"content": " let z = [1u8, 2u8, 3u8, 4u8];"
}
]
}
}
},
{
"Expression": {
"Value": {
"Integer": [
"U8",
"4",
{
"line_start": 6,
"line_stop": 6,
"col_start": 29,
"col_stop": 32,
"path": "",
"content": " let z = [1u8, 2u8, 3u8, 4u8];"
}
]
}
}
}
],
"span": {
"line_start": 6,
"line_stop": 6,
"col_start": 13,
"col_stop": 33,
"path": "",
"content": " let z = [1u8, 2u8, 3u8, 4u8];"
}
}
},
"span": {
"line_start": 6,
"line_stop": 6,
"col_start": 5,
"col_stop": 33,
"path": "",
"content": " let z = [1u8, 2u8, 3u8, 4u8];"
}
}
},
{
"Assign": {
"operation": "Assign",
"assignee": {
"identifier": "{\"name\":\"z\",\"span\":\"{\\\"line_start\\\":7,\\\"line_stop\\\":7,\\\"col_start\\\":5,\\\"col_stop\\\":6,\\\"path\\\":\\\"\\\",\\\"content\\\":\\\" z[0..\\\\\\\"test\\\\\\\" == \\\\\\\"test\\\\\\\"? 2 : 2] = x[0..2];\\\"}\"}",
"accesses": [
{
"ArrayRange": [
{
"Value": {
"Implicit": [
"0",
{
"line_start": 7,
"line_stop": 7,
"col_start": 7,
"col_stop": 8,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
]
}
},
{
"Ternary": {
"condition": {
"Binary": {
"left": {
"ArrayInline": {
"elements": [
{
"Expression": {
"Value": {
"Char": {
"character": {
"Scalar": 116
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 11,
"col_stop": 12,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
}
},
{
"Expression": {
"Value": {
"Char": {
"character": {
"Scalar": 101
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 12,
"col_stop": 13,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
}
},
{
"Expression": {
"Value": {
"Char": {
"character": {
"Scalar": 115
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 13,
"col_stop": 14,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
}
},
{
"Expression": {
"Value": {
"Char": {
"character": {
"Scalar": 116
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 14,
"col_stop": 15,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
}
}
],
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 10,
"col_stop": 16,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
},
"right": {
"ArrayInline": {
"elements": [
{
"Expression": {
"Value": {
"Char": {
"character": {
"Scalar": 116
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 21,
"col_stop": 22,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
}
},
{
"Expression": {
"Value": {
"Char": {
"character": {
"Scalar": 101
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 22,
"col_stop": 23,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
}
},
{
"Expression": {
"Value": {
"Char": {
"character": {
"Scalar": 115
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 23,
"col_stop": 24,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
}
},
{
"Expression": {
"Value": {
"Char": {
"character": {
"Scalar": 116
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 24,
"col_stop": 25,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
}
}
],
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 20,
"col_stop": 26,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
},
"op": "Eq",
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 10,
"col_stop": 26,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
},
"if_true": {
"Value": {
"Implicit": [
"2",
{
"line_start": 7,
"line_stop": 7,
"col_start": 28,
"col_stop": 29,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
]
}
},
"if_false": {
"Value": {
"Implicit": [
"2",
{
"line_start": 7,
"line_stop": 7,
"col_start": 32,
"col_stop": 33,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
]
}
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 10,
"col_stop": 33,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
]
}
],
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 5,
"col_stop": 34,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
},
"value": {
"ArrayRangeAccess": {
"array": {
"Identifier": "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":7,\\\"line_stop\\\":7,\\\"col_start\\\":37,\\\"col_stop\\\":38,\\\"path\\\":\\\"\\\",\\\"content\\\":\\\" z[0..\\\\\\\"test\\\\\\\" == \\\\\\\"test\\\\\\\"? 2 : 2] = x[0..2];\\\"}\"}"
},
"left": {
"Value": {
"Implicit": [
"0",
{
"line_start": 7,
"line_stop": 7,
"col_start": 39,
"col_stop": 40,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
]
}
},
"right": {
"Value": {
"Implicit": [
"2",
{
"line_start": 7,
"line_stop": 7,
"col_start": 42,
"col_stop": 43,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
]
}
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 37,
"col_stop": 44,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
},
"span": {
"line_start": 7,
"line_stop": 7,
"col_start": 5,
"col_stop": 44,
"path": "",
"content": " z[0..\"test\" == \"test\"? 2 : 2] = x[0..2];"
}
}
}
],
"span": {
"line_start": 1,
"line_stop": 6,
"line_stop": 8,
"col_start": 17,
"col_stop": 2,
"path": "",
@ -731,11 +1170,11 @@
},
"span": {
"line_start": 1,
"line_stop": 6,
"line_stop": 8,
"col_start": 1,
"col_stop": 2,
"path": "",
"content": "function main() {\n...\n}\n\n\n"
"content": "function main() {\n...\n}\n\n\n\n\n"
}
}
}

View File

@ -3,4 +3,6 @@ function main() {
s[..2] = "he";
let x = false;
x = "test1" == "test2";
let z = [1u8, 2u8, 3u8, 4u8];
z[0.."test" == "test"? 2 : 2] = x[0..2];
}

View File

@ -0,0 +1,10 @@
/*
namespace: Compile
expectation: Pass
input_file: input/three_ones.in
*/
function main (a: [u8; 3]) -> bool {
let y = a[0..[0u8; 2] == [0u8; 2]? 2u8 : 2u8];
return y == [1u8, 1];
}

View File

@ -0,0 +1,18 @@
---
namespace: Compile
expectation: Pass
outputs:
- circuit:
num_public_variables: 0
num_private_variables: 39
num_constraints: 39
at: 6f3ffff33f4e513211e7a55cc9edcab3bc2d2a146c2b280981308bb69165f86f
bt: adde6ad1b603a50c986ec99b6edcc972138bb6239e58a1b88e931bc0165b2e8e
ct: 867e3f6ee1f26af954e7868633a595d319267d91afc5d7e2074fe641fabde1d6
output:
- input_file: input/three_ones.in
output:
registers:
x:
type: bool
value: "true"

View File

@ -4,11 +4,11 @@ expectation: Pass
outputs:
- circuit:
num_public_variables: 0
num_private_variables: 1087
num_constraints: 1350
at: aae29cb6b4a71a5cf49d3de006b415b9a7818a2e23191819692e8d2ee69d8be2
bt: 11262f31fcaa7950be43eded328287305e7cbcb73c19455808f08f38116cfdd6
ct: 01dfba9e0754ad9890117694c9b03f4646642140aa7b6c393b2e264e701c323e
num_private_variables: 1277
num_constraints: 1604
at: c5d2d5bbb85d0f7ba48170fdb652aff057db6d1babf378d8a548af874e0734ff
bt: 726f3c39d11da5bc8487b8b7228a55cb7819a47a9b674d41f9c756c6927fb55a
ct: 7c4917152b59cb9caa30bad7d5119a3e36c40f7a783fdb8ff602dd5447573cc7
output:
- input_file: input/index1.in
output: