From ff36b94bf15f696ede9555d795598026d6c9a984 Mon Sep 17 00:00:00 2001 From: ACassimiro Date: Thu, 12 Sep 2024 16:45:34 -0300 Subject: [PATCH] If expressions (#82) * Adding initial tests * Removing usage of mut var * Mx-rust framework and storage implementation (#80) (#81) * Arrange the cells for easier debugging * Create a list of traits to help with mx preprocessing * Static method calls * Integer - value conversions * Ignore generic args when casting structs * Framework for executing Mx+Rust * Framework for testing Mx+Rust * Implement storage functions * Implement storage accessors * Implement BigUint::from * Default storage values * Storage set_if_empty * Storage tests --------- Co-authored-by: Virgil <25692529+virgil-serbanuta@users.noreply.github.com> * Implementing rules for evaluating conditionals and adding tests * handling cases where the conditional is not the return * Removing dead code * Addressing PR comments and adding tests * Adding test for function without return --------- Co-authored-by: Virgil <25692529+virgil-serbanuta@users.noreply.github.com> --- rust-semantics/expression.md | 4 ++ rust-semantics/expression/blocks.md | 11 ++++ rust-semantics/expression/conditionals.md | 15 ++++++ rust-semantics/rust-common-syntax.md | 9 ++-- tests/execution/if-expressions.1.run | 4 ++ tests/execution/if-expressions.2.run | 4 ++ tests/execution/if-expressions.3.run | 4 ++ tests/execution/if-expressions.4.run | 4 ++ tests/execution/if-expressions.5.run | 2 + tests/execution/if-expressions.rs | 64 +++++++++++++++++++++++ 10 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 rust-semantics/expression/blocks.md create mode 100644 rust-semantics/expression/conditionals.md create mode 100644 tests/execution/if-expressions.1.run create mode 100644 tests/execution/if-expressions.2.run create mode 100644 tests/execution/if-expressions.3.run create mode 100644 tests/execution/if-expressions.4.run create mode 100644 tests/execution/if-expressions.5.run create mode 100644 tests/execution/if-expressions.rs diff --git a/rust-semantics/expression.md b/rust-semantics/expression.md index 7502c3f..40c0ef7 100644 --- a/rust-semantics/expression.md +++ b/rust-semantics/expression.md @@ -1,9 +1,11 @@ ```k requires "expression/calls.md" requires "expression/integer-operations.md" +requires "expression/blocks.md" requires "expression/bool-operations.md" requires "expression/constants.md" requires "expression/casts.md" +requires "expression/conditionals.md" requires "expression/literals.md" requires "expression/references.md" requires "expression/tools.md" @@ -13,6 +15,8 @@ module RUST-EXPRESSION imports private RUST-CASTS imports private RUST-EXPRESSION-CALLS imports private RUST-EXPRESSION-CONSTANTS + imports private RUST-BLOCK-EXPRESSIONS + imports private RUST-CONDITIONAL-EXPRESSIONS imports private RUST-EXPRESSION-LITERALS imports private RUST-EXPRESSION-REFERENCES imports private RUST-EXPRESSION-TOOLS diff --git a/rust-semantics/expression/blocks.md b/rust-semantics/expression/blocks.md new file mode 100644 index 0000000..c064fb3 --- /dev/null +++ b/rust-semantics/expression/blocks.md @@ -0,0 +1,11 @@ +```k + +module RUST-BLOCK-EXPRESSIONS + imports private RUST-SHARED-SYNTAX + imports private RUST-VALUE-SYNTAX + + rule S:ExpressionWithBlock ; => S + +endmodule + +``` \ No newline at end of file diff --git a/rust-semantics/expression/conditionals.md b/rust-semantics/expression/conditionals.md new file mode 100644 index 0000000..6111e2a --- /dev/null +++ b/rust-semantics/expression/conditionals.md @@ -0,0 +1,15 @@ +```k + +module RUST-CONDITIONAL-EXPRESSIONS + imports RUST-SHARED-SYNTAX + imports RUST-VALUE-SYNTAX + + rule (if ptrValue(_, true) S:BlockExpression):ExpressionWithBlock => S [owise] + rule (if ptrValue(_, false) _:BlockExpression):ExpressionWithBlock => .K [owise] + + rule (if ptrValue(_, true) A:BlockExpression else _:IfElseExpression):ExpressionWithBlock => A + rule (if ptrValue(_, false) _:BlockExpression else B:IfElseExpression):ExpressionWithBlock => B + +endmodule + +``` \ No newline at end of file diff --git a/rust-semantics/rust-common-syntax.md b/rust-semantics/rust-common-syntax.md index c3747ab..b46cab6 100644 --- a/rust-semantics/rust-common-syntax.md +++ b/rust-semantics/rust-common-syntax.md @@ -684,9 +684,12 @@ https://doc.rust-lang.org/reference/items/extern-crates.html ```k - syntax IfExpression ::= "if" ExpressionExceptStructExpression BlockExpression MaybeIfElseExpression - syntax MaybeIfElseExpression ::= "" | "else" IfElseExpression - syntax IfElseExpression ::= BlockExpression | IfExpression | IfLetExpression + syntax IfExpression ::= "if" ExpressionExceptStructExpression BlockExpression MaybeIfElseExpression [strict(1)] + syntax MaybeIfElseExpression ::= "" + | "else" IfElseExpression + syntax IfElseExpression ::= BlockExpression + | IfExpression + | IfLetExpression // TODO: Not implemented properly syntax ExpressionExceptStructExpression ::= Expression diff --git a/tests/execution/if-expressions.1.run b/tests/execution/if-expressions.1.run new file mode 100644 index 0000000..c017506 --- /dev/null +++ b/tests/execution/if-expressions.1.run @@ -0,0 +1,4 @@ +new IfExpressions; +call IfExpressions.if_expression; +return_value; +check_eq 1_u64 diff --git a/tests/execution/if-expressions.2.run b/tests/execution/if-expressions.2.run new file mode 100644 index 0000000..b57c1cd --- /dev/null +++ b/tests/execution/if-expressions.2.run @@ -0,0 +1,4 @@ +new IfExpressions; +call IfExpressions.if_else_expression; +return_value; +check_eq 2_u64 diff --git a/tests/execution/if-expressions.3.run b/tests/execution/if-expressions.3.run new file mode 100644 index 0000000..26f1e1b --- /dev/null +++ b/tests/execution/if-expressions.3.run @@ -0,0 +1,4 @@ +new IfExpressions; +call IfExpressions.if_else_if_expression; +return_value; +check_eq 3_u64 diff --git a/tests/execution/if-expressions.4.run b/tests/execution/if-expressions.4.run new file mode 100644 index 0000000..ff6f2e2 --- /dev/null +++ b/tests/execution/if-expressions.4.run @@ -0,0 +1,4 @@ +new IfExpressions; +call IfExpressions.test_if_instruction; +return_value; +check_eq 11_u64 diff --git a/tests/execution/if-expressions.5.run b/tests/execution/if-expressions.5.run new file mode 100644 index 0000000..268584a --- /dev/null +++ b/tests/execution/if-expressions.5.run @@ -0,0 +1,2 @@ +new IfExpressions; +call IfExpressions.test_if_no_return diff --git a/tests/execution/if-expressions.rs b/tests/execution/if-expressions.rs new file mode 100644 index 0000000..ffc4fea --- /dev/null +++ b/tests/execution/if-expressions.rs @@ -0,0 +1,64 @@ +#![no_std] + +#[allow(unused_imports)] +use multiversx_sc::imports::*; + +#[multiversx_sc::contract] +pub trait IfExpressions { + #[init] + fn init(&self) { + } + + #[upgrade] + fn upgrade(&self) {} + + fn if_expression(&self) -> u64 { + + if 80_u64 == 80_u64 { + 1_u64 + } + + + + } + + fn if_else_expression(&self) -> u64 { + + if 80_u64 != 80_u64 { + 1_u64 + } else { + 2_u64 + } + + } + + fn if_else_if_expression(&self) -> u64 { + + if 80_u64 != 80_u64 { + 1_u64 + } else if false { + 2_u64 + } else { + 3_u64 + } + + } + + fn test_if_instruction(&self) -> u64 { + if true { + let x = 10_u64; + } else { + let y = 11_u64; + }; + 11_u64 + } + + fn test_if_no_return(&self) { + if true { + let x = 10_u64; + } else { + let y = 11_u64; + }; + } + +}