From d4f008f5722bbce8b6343bac3e1f9ba222ae0529 Mon Sep 17 00:00:00 2001 From: ACassimiro Date: Sat, 31 Aug 2024 15:32:09 -0300 Subject: [PATCH] Adding arithmetic operations in the preprocessing stage (#68) * 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 --- rust-lite/Makefile | 4 ++ rust-semantics/preprocessing.md | 2 + .../preprocessing/arithmetic-operations.md | 42 +++++++++++++++++++ rust-semantics/rust-common-syntax.md | 10 ++--- tests/execution/arithmetic-expression.1.run | 4 ++ tests/execution/arithmetic-expression.2.run | 4 ++ tests/execution/arithmetic-expression.3.run | 4 ++ tests/execution/arithmetic-expression.4.run | 4 ++ tests/execution/arithmetic-expression.5.run | 4 ++ tests/execution/arithmetic-expression.rs | 25 +++++++++++ 10 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 rust-semantics/preprocessing/arithmetic-operations.md create mode 100644 tests/execution/arithmetic-expression.1.run create mode 100644 tests/execution/arithmetic-expression.2.run create mode 100644 tests/execution/arithmetic-expression.3.run create mode 100644 tests/execution/arithmetic-expression.4.run create mode 100644 tests/execution/arithmetic-expression.5.run create mode 100644 tests/execution/arithmetic-expression.rs diff --git a/rust-lite/Makefile b/rust-lite/Makefile index 54cb6c8..8f928df 100644 --- a/rust-lite/Makefile +++ b/rust-lite/Makefile @@ -19,6 +19,10 @@ build: poetry-install: $(POETRY) install +.PHONY: kompile +kompile: + @make -C .. clean -i + @make -C .. build # Tests diff --git a/rust-semantics/preprocessing.md b/rust-semantics/preprocessing.md index 879f4a7..a0ea1b5 100644 --- a/rust-semantics/preprocessing.md +++ b/rust-semantics/preprocessing.md @@ -1,5 +1,6 @@ ```k +requires "preprocessing/arithmetic-operations.md" requires "preprocessing/constants.md" requires "preprocessing/crate.md" requires "preprocessing/configuration.md" @@ -15,6 +16,7 @@ module RUST-PREPROCESSING imports private INITIALIZATION imports private TRAIT imports private TRAIT-METHODS + imports private RUST-INTEGER-ARITHMETIC-OPERATIONS endmodule ``` diff --git a/rust-semantics/preprocessing/arithmetic-operations.md b/rust-semantics/preprocessing/arithmetic-operations.md new file mode 100644 index 0000000..cd51556 --- /dev/null +++ b/rust-semantics/preprocessing/arithmetic-operations.md @@ -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 + +``` diff --git a/rust-semantics/rust-common-syntax.md b/rust-semantics/rust-common-syntax.md index bebfda0..6dfb3ca 100644 --- a/rust-semantics/rust-common-syntax.md +++ b/rust-semantics/rust-common-syntax.md @@ -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 diff --git a/tests/execution/arithmetic-expression.1.run b/tests/execution/arithmetic-expression.1.run new file mode 100644 index 0000000..f2e50b8 --- /dev/null +++ b/tests/execution/arithmetic-expression.1.run @@ -0,0 +1,4 @@ +new ArithmeticExpression; +call ArithmeticExpression.arithmetic_expression_mult_constant; +return_value; +check_eq 86400_u64 diff --git a/tests/execution/arithmetic-expression.2.run b/tests/execution/arithmetic-expression.2.run new file mode 100644 index 0000000..dd0d0dc --- /dev/null +++ b/tests/execution/arithmetic-expression.2.run @@ -0,0 +1,4 @@ +new ArithmeticExpression; +call ArithmeticExpression.arithmetic_expression_div_constant; +return_value; +check_eq 2_u64 diff --git a/tests/execution/arithmetic-expression.3.run b/tests/execution/arithmetic-expression.3.run new file mode 100644 index 0000000..92ab1ca --- /dev/null +++ b/tests/execution/arithmetic-expression.3.run @@ -0,0 +1,4 @@ +new ArithmeticExpression; +call ArithmeticExpression.arithmetic_expression_sum_constant; +return_value; +check_eq 101_u64 diff --git a/tests/execution/arithmetic-expression.4.run b/tests/execution/arithmetic-expression.4.run new file mode 100644 index 0000000..890b7eb --- /dev/null +++ b/tests/execution/arithmetic-expression.4.run @@ -0,0 +1,4 @@ +new ArithmeticExpression; +call ArithmeticExpression.arithmetic_expression_sub_constant; +return_value; +check_eq 99_u64 diff --git a/tests/execution/arithmetic-expression.5.run b/tests/execution/arithmetic-expression.5.run new file mode 100644 index 0000000..f04959e --- /dev/null +++ b/tests/execution/arithmetic-expression.5.run @@ -0,0 +1,4 @@ +new ArithmeticExpression; +call ArithmeticExpression.arithmetic_expression_mod_constant; +return_value; +check_eq 2_u64 diff --git a/tests/execution/arithmetic-expression.rs b/tests/execution/arithmetic-expression.rs new file mode 100644 index 0000000..6a42e70 --- /dev/null +++ b/tests/execution/arithmetic-expression.rs @@ -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 } + +}