Skip to content

Commit

Permalink
Adding arithmetic operations in the preprocessing stage (#68)
Browse files Browse the repository at this point in the history
* Adding arithmetic operations in the preprocessing stage

* Fix typo

* Changing strict for seqstrict and introducing a test for arithmetic expressions on constants

* Fixing typo in name and addint tests
  • Loading branch information
ACassimiro authored Aug 31, 2024
1 parent 0073e26 commit d4f008f
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 5 deletions.
4 changes: 4 additions & 0 deletions rust-lite/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ build:
poetry-install:
$(POETRY) install

.PHONY: kompile
kompile:
@make -C .. clean -i
@make -C .. build

# Tests

Expand Down
2 changes: 2 additions & 0 deletions rust-semantics/preprocessing.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
```k
requires "preprocessing/arithmetic-operations.md"
requires "preprocessing/constants.md"
requires "preprocessing/crate.md"
requires "preprocessing/configuration.md"
Expand All @@ -15,6 +16,7 @@ module RUST-PREPROCESSING
imports private INITIALIZATION
imports private TRAIT
imports private TRAIT-METHODS
imports private RUST-INTEGER-ARITHMETIC-OPERATIONS
endmodule
```
42 changes: 42 additions & 0 deletions rust-semantics/preprocessing/arithmetic-operations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
```k
module RUST-INTEGER-ARITHMETIC-OPERATIONS
imports RUST-SHARED-SYNTAX
imports private RUST-REPRESENTATION
// Operations are implemented only for the same types,
// as implicit type casting (coercion) is not available
// in Rust.
rule i32(A):Value * i32(B):Value => i32(A *MInt B)
rule i32(A):Value + i32(B):Value => i32(A +MInt B)
rule i32(A):Value - i32(B):Value => i32(A -MInt B)
rule i32(A):Value / i32(B):Value => i32(A /sMInt B)
rule i32(A):Value % i32(B):Value => i32(A %sMInt B)
rule u32(A):Value * u32(B):Value => u32(A *MInt B)
rule u32(A):Value + u32(B):Value => u32(A +MInt B)
rule u32(A):Value - u32(B):Value => u32(A -MInt B)
rule u32(A):Value / u32(B):Value => u32(A /uMInt B)
rule u32(A):Value % u32(B):Value => u32(A %uMInt B)
rule i64(A):Value * i64(B):Value => i64(A *MInt B)
rule i64(A):Value + i64(B):Value => i64(A +MInt B)
rule i64(A):Value - i64(B):Value => i64(A -MInt B)
rule i64(A):Value / i64(B):Value => i64(A /sMInt B)
rule i64(A):Value % i64(B):Value => i64(A %sMInt B)
rule u64(A):Value * u64(B):Value => u64(A *MInt B)
rule u64(A):Value + u64(B):Value => u64(A +MInt B)
rule u64(A):Value - u64(B):Value => u64(A -MInt B)
rule u64(A):Value / u64(B):Value => u64(A /uMInt B)
rule u64(A):Value % u64(B):Value => u64(A %uMInt B)
rule u128(A):Value * u128(B):Value => u128(A *MInt B)
rule u128(A):Value + u128(B):Value => u128(A +MInt B)
rule u128(A):Value - u128(B):Value => u128(A -MInt B)
rule u128(A):Value / u128(B):Value => u128(A /uMInt B)
rule u128(A):Value % u128(B):Value => u128(A %uMInt B)
endmodule
```
10 changes: 5 additions & 5 deletions rust-semantics/rust-common-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,13 @@ https://doc.rust-lang.org/reference/items/extern-crates.html
// TypeCastExpression
> left:
Expression "*" Expression
| Expression "/" Expression
| Expression "%" Expression
Expression "*" Expression [seqstrict, left]
| Expression "/" Expression [seqstrict, left]
| Expression "%" Expression [seqstrict, left]
> left:
Expression "+" Expression
| Expression "-" Expression
Expression "+" Expression [seqstrict, left]
| Expression "-" Expression [seqstrict, left]
> left:
Expression "<<" Expression
Expand Down
4 changes: 4 additions & 0 deletions tests/execution/arithmetic-expression.1.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new ArithmeticExpression;
call ArithmeticExpression.arithmetic_expression_mult_constant;
return_value;
check_eq 86400_u64
4 changes: 4 additions & 0 deletions tests/execution/arithmetic-expression.2.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new ArithmeticExpression;
call ArithmeticExpression.arithmetic_expression_div_constant;
return_value;
check_eq 2_u64
4 changes: 4 additions & 0 deletions tests/execution/arithmetic-expression.3.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new ArithmeticExpression;
call ArithmeticExpression.arithmetic_expression_sum_constant;
return_value;
check_eq 101_u64
4 changes: 4 additions & 0 deletions tests/execution/arithmetic-expression.4.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new ArithmeticExpression;
call ArithmeticExpression.arithmetic_expression_sub_constant;
return_value;
check_eq 99_u64
4 changes: 4 additions & 0 deletions tests/execution/arithmetic-expression.5.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new ArithmeticExpression;
call ArithmeticExpression.arithmetic_expression_mod_constant;
return_value;
check_eq 2_u64
25 changes: 25 additions & 0 deletions tests/execution/arithmetic-expression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![no_std]

#[allow(unused_imports)]
use multiversx_sc::imports::*;

#[multiversx_sc::contract]
pub trait ArithmeticExpression {
#[init]
fn init(&self) {
}

#[upgrade]
fn upgrade(&self) {}

fn arithmetic_expression_mult_constant(&self) -> u64 { 24_u64 * 60_u64 * 60_u64 }

fn arithmetic_expression_div_constant(&self) -> u64 { 10_u64 / 4_u64 }

fn arithmetic_expression_sum_constant(&self) -> u64 { 100_u64 + 1_u64 }

fn arithmetic_expression_sub_constant(&self) -> u64 { 100_u64 - 1_u64 }

fn arithmetic_expression_mod_constant(&self) -> u64 { 5_u64 % 3_u64 }

}

0 comments on commit d4f008f

Please sign in to comment.