Skip to content

Commit

Permalink
If expressions (#82)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
ACassimiro and virgil-serbanuta authored Sep 12, 2024
1 parent 4eac1c1 commit ff36b94
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 3 deletions.
4 changes: 4 additions & 0 deletions rust-semantics/expression.md
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions rust-semantics/expression/blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```k
module RUST-BLOCK-EXPRESSIONS
imports private RUST-SHARED-SYNTAX
imports private RUST-VALUE-SYNTAX
rule S:ExpressionWithBlock ; => S
endmodule
```
15 changes: 15 additions & 0 deletions rust-semantics/expression/conditionals.md
Original file line number Diff line number Diff line change
@@ -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
```
9 changes: 6 additions & 3 deletions rust-semantics/rust-common-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/execution/if-expressions.1.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new IfExpressions;
call IfExpressions.if_expression;
return_value;
check_eq 1_u64
4 changes: 4 additions & 0 deletions tests/execution/if-expressions.2.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new IfExpressions;
call IfExpressions.if_else_expression;
return_value;
check_eq 2_u64
4 changes: 4 additions & 0 deletions tests/execution/if-expressions.3.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new IfExpressions;
call IfExpressions.if_else_if_expression;
return_value;
check_eq 3_u64
4 changes: 4 additions & 0 deletions tests/execution/if-expressions.4.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new IfExpressions;
call IfExpressions.test_if_instruction;
return_value;
check_eq 11_u64
2 changes: 2 additions & 0 deletions tests/execution/if-expressions.5.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
new IfExpressions;
call IfExpressions.test_if_no_return
64 changes: 64 additions & 0 deletions tests/execution/if-expressions.rs
Original file line number Diff line number Diff line change
@@ -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;
};
}

}

0 comments on commit ff36b94

Please sign in to comment.