Merge pull request #2344 from AleoHQ/fix/issue-2343

[Fix] Fix parsing for negative `scalar`, `field`, and `group` literals.
This commit is contained in:
d0cd 2023-04-12 08:47:49 -07:00 committed by GitHub
commit d039543c47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 87 additions and 68 deletions

View File

@ -248,24 +248,52 @@ impl ParserContext<'_> {
ops.push((operation, self.prev_token.span));
}
// This is needed to ensure that only the token sequence `-`, `Token::Integer(..)` is parsed as a negative integer literal.
let inner_is_integer = matches!(self.token.token, Token::Integer(..));
let mut inner = self.parse_postfix_expression()?;
for (op, op_span) in ops.into_iter().rev() {
inner = match inner {
// If the unary operation is a negate, and the inner expression is a signed integer literal,
// then produce a negative integer literal.
// This helps handle a special case where -128i8, treated as a unary expression, overflows, but -128i8, treated as an integer literal doesn't.
Expression::Literal(Literal::Integer(integer_type, string, span))
if op == UnaryOperation::Negate && inner_is_integer =>
{
Expression::Literal(Literal::Integer(integer_type, format!("-{string}"), op_span + span))
// If the last operation is a negation and the inner expression is a literal, then construct a negative literal.
if let Some((UnaryOperation::Negate, _)) = ops.last() {
match inner {
Expression::Literal(Literal::Integer(integer_type, string, span)) => {
// Remove the negation from the operations.
// Note that this unwrap is safe because there is at least one operation in `ops`.
let (_, op_span) = ops.pop().unwrap();
// Construct a negative integer literal.
inner = Expression::Literal(Literal::Integer(integer_type, format!("-{string}"), op_span + span));
}
// Otherwise, produce a unary expression.
_ => Expression::Unary(UnaryExpression { span: op_span + inner.span(), op, receiver: Box::new(inner) }),
};
Expression::Literal(Literal::Field(string, span)) => {
// Remove the negation from the operations.
// Note that
let (_, op_span) = ops.pop().unwrap();
// Construct a negative field literal.
inner = Expression::Literal(Literal::Field(format!("-{string}"), op_span + span));
}
Expression::Literal(Literal::Group(group_literal)) => {
// Remove the negation from the operations.
let (_, op_span) = ops.pop().unwrap();
// Construct a negative group literal.
// Note that we only handle the case where the group literal is a single integral value.
inner = Expression::Literal(Literal::Group(Box::new(match *group_literal {
GroupLiteral::Single(string, span) => {
GroupLiteral::Single(format!("-{string}"), op_span + span)
}
GroupLiteral::Tuple(tuple) => GroupLiteral::Tuple(tuple),
})));
}
Expression::Literal(Literal::Scalar(string, span)) => {
// Remove the negation from the operations.
let (_, op_span) = ops.pop().unwrap();
// Construct a negative scalar literal.
inner = Expression::Literal(Literal::Scalar(format!("-{string}"), op_span + span));
}
_ => (), // Do nothing.
}
}
// Apply the operations in reverse order, constructing a unary expression.
for (op, op_span) in ops.into_iter().rev() {
inner = Expression::Unary(UnaryExpression { span: op_span + inner.span(), op, receiver: Box::new(inner) });
}
Ok(inner)
}

View File

@ -2,10 +2,10 @@
namespace: Compile
expectation: Pass
outputs:
- initial_ast: c4a71c6a0f72897bfd540fc6bbf6d440d1f827746cecabd5094a9eba1ba249c0
unrolled_ast: c4a71c6a0f72897bfd540fc6bbf6d440d1f827746cecabd5094a9eba1ba249c0
ssa_ast: 7118572de7aef8ba01b0211cfa1e40d92c5d856256f91e5ffddd5621b935e2b8
flattened_ast: de6afb7703d3e89ab7a00c33683a690bdc401e58278bd27164b623088835570d
inlined_ast: de6afb7703d3e89ab7a00c33683a690bdc401e58278bd27164b623088835570d
dce_ast: 5a3c26e31d0810f8bccf1efe46988141b50f5d8c37e15b585accb74c3514bdb8
- initial_ast: ddc90f881ec1f45741301812d26aabd1901d0d471d6448e96737b5c9e3851ab3
unrolled_ast: ddc90f881ec1f45741301812d26aabd1901d0d471d6448e96737b5c9e3851ab3
ssa_ast: 7dccc1dc94e6b05557cdf294945513116856f6dd7c6e0c8b46b1089d453188bd
flattened_ast: ff3db8d357defae10ab07f35aa1df3b84dddad1453da60463c97cf1f61492147
inlined_ast: ff3db8d357defae10ab07f35aa1df3b84dddad1453da60463c97cf1f61492147
dce_ast: f86eaa7fc6bf849868a5f9f0279889c7429c98413ff38d2f8667f74a55d9ebb9
bytecode: 12b55db95d5f6e760a8ebb0604264fb1b09b771d247d093eaed4dec8c28a579b

View File

@ -2,10 +2,10 @@
namespace: Compile
expectation: Pass
outputs:
- initial_ast: 39173e607485c0706438ee8f9d8ffeac0e21c3ed08df932d9edaba8b15334ca9
unrolled_ast: 39173e607485c0706438ee8f9d8ffeac0e21c3ed08df932d9edaba8b15334ca9
ssa_ast: c6231cc87d5244b147045a8958342a3f9aae2a7108215b265c517fccab0ae697
flattened_ast: eb4b8640cee5f68e7a358638a4f2dd80fa9f5e12df485cb41af7f041042c4495
inlined_ast: eb4b8640cee5f68e7a358638a4f2dd80fa9f5e12df485cb41af7f041042c4495
dce_ast: eb4b8640cee5f68e7a358638a4f2dd80fa9f5e12df485cb41af7f041042c4495
bytecode: eeb44a4faf22686de577f93db551bd83246583158dcecb35d2dc454e0693e419
- initial_ast: a0a3f4d71f6a8515eec27eee97abf5cbaeadbddb1a3b45e80e544ccadb1acaf0
unrolled_ast: a0a3f4d71f6a8515eec27eee97abf5cbaeadbddb1a3b45e80e544ccadb1acaf0
ssa_ast: bb33a4fa86326a22060f8413b1d1a2c8f5531669e120091aa6cfd52189dcc8d9
flattened_ast: 81d3e2a9873213fa638e27f44b3b038c69bbe349c35b5356a772a7f68a485216
inlined_ast: 81d3e2a9873213fa638e27f44b3b038c69bbe349c35b5356a772a7f68a485216
dce_ast: da90f747cda7a8d74b4abbf06fb2220a0fb3420409befa188d8c812bffacee64
bytecode: bf4397cd5bc3498d05f1d6582c3b4a868579aaa6b8f6f484f1c2ce9dd03a13a2

View File

@ -2,10 +2,10 @@
namespace: Compile
expectation: Pass
outputs:
- initial_ast: 999b1553693e9c919a3a12b6fe2d9aefa0f7414eb294e9277585b8fa9d771c36
unrolled_ast: 999b1553693e9c919a3a12b6fe2d9aefa0f7414eb294e9277585b8fa9d771c36
ssa_ast: e2884225fe46a4d894ee2561635254b1079c889b26dc382128a590a40f3fe5d6
flattened_ast: e36bb3f053c14fea5b6be293c43d32da60b06324b6cfe29a84ea2c1ce0d10015
inlined_ast: e36bb3f053c14fea5b6be293c43d32da60b06324b6cfe29a84ea2c1ce0d10015
dce_ast: e36bb3f053c14fea5b6be293c43d32da60b06324b6cfe29a84ea2c1ce0d10015
bytecode: 7540a269502febfe91bebfc15030891bde7667f921d5d8d9d22efbcf16410543
- initial_ast: 46a92aaca66affbf50b74324dbce4736483379633f17f61ea0cf37e605c7532c
unrolled_ast: 46a92aaca66affbf50b74324dbce4736483379633f17f61ea0cf37e605c7532c
ssa_ast: 0b8ade69614164174dc9ec432d4b817ff866f645e288e2bdab24c6ce4941d9f9
flattened_ast: 29589d9c50c3654f9002ba09ee2354a4e073c7e6f7eb788df42ff77ea5f36a07
inlined_ast: 29589d9c50c3654f9002ba09ee2354a4e073c7e6f7eb788df42ff77ea5f36a07
dce_ast: 29589d9c50c3654f9002ba09ee2354a4e073c7e6f7eb788df42ff77ea5f36a07
bytecode: b6438347d546b70daea75dda80a96df95c782793c3e40c92f1931903604c41bf

View File

@ -2,10 +2,10 @@
namespace: Compile
expectation: Pass
outputs:
- initial_ast: 1f2eb7c4e5443c57cf49ce470cfa8229fb67e162c041c6fb7fe9ecd46b5a3546
unrolled_ast: 1f2eb7c4e5443c57cf49ce470cfa8229fb67e162c041c6fb7fe9ecd46b5a3546
ssa_ast: 41032ad876b2160b388f01d716655ddd073b8cd7d185faa01002eeac19840597
flattened_ast: 98d83fec655c25d2889cb6405068c46de336976f1468efb7e9bc30d434a5cb56
inlined_ast: 98d83fec655c25d2889cb6405068c46de336976f1468efb7e9bc30d434a5cb56
dce_ast: 98d83fec655c25d2889cb6405068c46de336976f1468efb7e9bc30d434a5cb56
bytecode: 96c9838c6cd113e26c1cb3abcb9aebb52e622fec38cab2a13ebaad1683a1c15d
- initial_ast: c1b1b7c0ce432d5ba810075cc7162357ecce82e6e5c9903a8d1f7464dc6b1018
unrolled_ast: c1b1b7c0ce432d5ba810075cc7162357ecce82e6e5c9903a8d1f7464dc6b1018
ssa_ast: 809813c98bef149a0da8d00cfb186c4579d9318a8c030278bc66f3feeb2a95cf
flattened_ast: e78c54476d0f2744d56e8e84d153b15c5ef7ec3edea3e0c8998474fbec6c78c7
inlined_ast: e78c54476d0f2744d56e8e84d153b15c5ef7ec3edea3e0c8998474fbec6c78c7
dce_ast: e78c54476d0f2744d56e8e84d153b15c5ef7ec3edea3e0c8998474fbec6c78c7
bytecode: cca4e6a4f6a893c6424f798ff45aa916ec68bc87a03ee43e8f12c8f4bec0b86d

View File

@ -427,16 +427,10 @@ outputs:
- span:
lo: 0
hi: 6
- Unary:
receiver:
Literal:
Group:
Single:
- "1"
- span:
lo: 1
hi: 7
op: Negate
span:
lo: 0
hi: 7
- Literal:
Group:
Single:
- "-1"
- span:
lo: 0
hi: 7

View File

@ -63,19 +63,13 @@ outputs:
- span:
lo: 0
hi: 6
- Unary:
receiver:
Literal:
Integer:
- I8
- "128"
- span:
lo: 2
hi: 7
op: Negate
span:
lo: 0
hi: 7
- Literal:
Integer:
- I8
- "-128"
- span:
lo: 0
hi: 7
- Unary:
receiver:
Identifier: "{\"name\":\"x\",\"span\":\"{\\\"lo\\\":0,\\\"hi\\\":1}\"}"

View File

@ -46,6 +46,7 @@ program test.aleo {
let o: field = BHP512::commit(scalar_value, 1scalar);
// let p: field = BHP512::commit(string_value, 1scalar);
let q: field = BHP512::commit(Foo { a: 1u128, b: 2u128 }, 1scalar);
let r: field = BHP512::commit(Foo { a: 1u128, b: 2u128 }, -1scalar);
return a + o;
}}

View File

@ -6,5 +6,6 @@ expectation: Pass
program test.aleo {
transition main(a: field) -> bool {
let negOneField: field = -1field;
let negnegnegOneField: field = 0field - - - 1field;
return negOneField + a == 0field;
}}

View File

@ -6,6 +6,7 @@ expectation: Pass
program test.aleo {
transition main(a: group, b: group) -> bool {
assert(-a == b);
assert(a != -2group);
return -a == b;
}}