From cc5af4eb5540ad38eba5fe6531826661e0377699 Mon Sep 17 00:00:00 2001 From: ACassimiro Date: Tue, 3 Sep 2024 11:35:33 -0300 Subject: [PATCH 1/5] Addressing constants with type identifiers, introducing tests and fixing path typo in rust-lite --- rust-lite/src/rust_lite/__main__.py | 6 +++--- rust-lite/src/rust_lite/manager.py | 11 ++++++++++- rust-semantics/preprocessing/constants.md | 9 +++++++++ tests/execution/constant-lookup.1.run | 5 +++++ tests/execution/constant-lookup.2.run | 5 +++++ tests/execution/constant-lookup.rs | 23 +++++++++++++++++++++++ 6 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/execution/constant-lookup.1.run create mode 100644 tests/execution/constant-lookup.2.run create mode 100644 tests/execution/constant-lookup.rs diff --git a/rust-lite/src/rust_lite/__main__.py b/rust-lite/src/rust_lite/__main__.py index 30824e1..ca11a1a 100644 --- a/rust-lite/src/rust_lite/__main__.py +++ b/rust-lite/src/rust_lite/__main__.py @@ -60,7 +60,7 @@ def exec_run(options: RunOptions) -> None: print('Performed all possible rewriting operations; Trying to fetch the content of the K cell.') module_manager.print_k_top_element() - + def trigger_exec_run(stripped_args): options = generate_options(stripped_args) executor_name = 'exec_run' @@ -76,11 +76,11 @@ def exec_erc20() -> None: trigger_exec_run(stripped_args) def exec_staking() -> None: - stripped_args = {'command': 'run', 'input_file': Path('../tests/syntax/lending.rs')} + stripped_args = {'command': 'run', 'input_file': Path('../tests/syntax/staking.rs')} trigger_exec_run(stripped_args) def exec_lending() -> None: - stripped_args = {'command': 'run', 'input_file': Path('../tests/syntax/staking.rs')} + stripped_args = {'command': 'run', 'input_file': Path('../tests/syntax/lending.rs')} trigger_exec_run(stripped_args) diff --git a/rust-lite/src/rust_lite/manager.py b/rust-lite/src/rust_lite/manager.py index ae7f67d..25bec09 100644 --- a/rust-lite/src/rust_lite/manager.py +++ b/rust-lite/src/rust_lite/manager.py @@ -32,7 +32,7 @@ def _init_cterm(self) -> None: def load_program(self, program_path: str) -> None: - returned_process = _kast(file=program_path, definition_dir=f'../.build/rust-execution-kompiled') + returned_process = _kast(file=program_path, definition_dir=f'../.build/rust-preprocessing-kompiled') program = returned_process.stdout @@ -59,3 +59,12 @@ def print_k_top_element(self) -> None: _PPRINT.pprint(top_cell) else: print('Cell is empty.') + + + def print_constants_cell(self) -> None: + cell = self.cterm.cell('CONSTANTS_CELL') + + print('--------------------------------------------------') + print('CONSTANTS CELL ELEMENT: ') + _PPRINT.pprint(cell) + \ No newline at end of file diff --git a/rust-semantics/preprocessing/constants.md b/rust-semantics/preprocessing/constants.md index 76caa73..289ac25 100644 --- a/rust-semantics/preprocessing/constants.md +++ b/rust-semantics/preprocessing/constants.md @@ -24,5 +24,14 @@ module RUST-CONSTANTS ... + + rule Name:Identifier::.PathExprSegments => V ... + Name + V:Value + + rule Name:Identifier::I:Identifier::.PathExprSegments => V ... + Name + V:Value + endmodule ``` \ No newline at end of file diff --git a/tests/execution/constant-lookup.1.run b/tests/execution/constant-lookup.1.run new file mode 100644 index 0000000..936166b --- /dev/null +++ b/tests/execution/constant-lookup.1.run @@ -0,0 +1,5 @@ +new ConstantValueLookup; +call ConstantValueLookup.lookup_constant; +return_value; +check_eq 7_000_u64 + diff --git a/tests/execution/constant-lookup.2.run b/tests/execution/constant-lookup.2.run new file mode 100644 index 0000000..61f79d1 --- /dev/null +++ b/tests/execution/constant-lookup.2.run @@ -0,0 +1,5 @@ +new ConstantValueLookup; +call ConstantValueLookup.lookup_constant_with_type; +return_value; +check_eq 7_000_u64 + diff --git a/tests/execution/constant-lookup.rs b/tests/execution/constant-lookup.rs new file mode 100644 index 0000000..6d22fea --- /dev/null +++ b/tests/execution/constant-lookup.rs @@ -0,0 +1,23 @@ +#![no_std] + +#[allow(unused_imports)] +use multiversx_sc::imports::*; + +pub const YEARLY_INTEREST: u64 = 7_000; + +#[multiversx_sc::contract] +pub trait ConstantValueLookup { + #[init] + fn init(&self) { + } + + #[upgrade] + fn upgrade(&self) {} + + fn lookup_constant(&self) -> u64 { YEARLY_INTEREST } + + fn lookup_constant_with_type(&self) -> u64 { + let x = YEARLY_INTEREST::u64; + x } + +} From bf9debf77d49e1604378015e6f99ef2f628b7570 Mon Sep 17 00:00:00 2001 From: acassimiro Date: Tue, 3 Sep 2024 11:40:00 -0300 Subject: [PATCH 2/5] Fixing warning --- rust-semantics/preprocessing/constants.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-semantics/preprocessing/constants.md b/rust-semantics/preprocessing/constants.md index 289ac25..8d1a21a 100644 --- a/rust-semantics/preprocessing/constants.md +++ b/rust-semantics/preprocessing/constants.md @@ -29,7 +29,7 @@ module RUST-CONSTANTS Name V:Value - rule Name:Identifier::I:Identifier::.PathExprSegments => V ... + rule Name:Identifier::_:Identifier::.PathExprSegments => V ... Name V:Value From cef53d4d91453a95540fd0931a8632e47a29db87 Mon Sep 17 00:00:00 2001 From: acassimiro Date: Tue, 3 Sep 2024 15:57:08 -0300 Subject: [PATCH 3/5] Addressing comments --- rust-lite/src/rust_lite/__main__.py | 2 +- rust-semantics/preprocessing/constants.md | 4 ---- tests/execution/constant-lookup.rs | 5 +++-- tests/syntax/staking.rs | 2 +- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/rust-lite/src/rust_lite/__main__.py b/rust-lite/src/rust_lite/__main__.py index ca11a1a..d6fd02b 100644 --- a/rust-lite/src/rust_lite/__main__.py +++ b/rust-lite/src/rust_lite/__main__.py @@ -60,7 +60,7 @@ def exec_run(options: RunOptions) -> None: print('Performed all possible rewriting operations; Trying to fetch the content of the K cell.') module_manager.print_k_top_element() - + def trigger_exec_run(stripped_args): options = generate_options(stripped_args) executor_name = 'exec_run' diff --git a/rust-semantics/preprocessing/constants.md b/rust-semantics/preprocessing/constants.md index 8d1a21a..95c3aa0 100644 --- a/rust-semantics/preprocessing/constants.md +++ b/rust-semantics/preprocessing/constants.md @@ -29,9 +29,5 @@ module RUST-CONSTANTS Name V:Value - rule Name:Identifier::_:Identifier::.PathExprSegments => V ... - Name - V:Value - endmodule ``` \ No newline at end of file diff --git a/tests/execution/constant-lookup.rs b/tests/execution/constant-lookup.rs index 6d22fea..5609665 100644 --- a/tests/execution/constant-lookup.rs +++ b/tests/execution/constant-lookup.rs @@ -17,7 +17,8 @@ pub trait ConstantValueLookup { fn lookup_constant(&self) -> u64 { YEARLY_INTEREST } fn lookup_constant_with_type(&self) -> u64 { - let x = YEARLY_INTEREST::u64; - x } + let x = YEARLY_INTEREST; + x + } } diff --git a/tests/syntax/staking.rs b/tests/syntax/staking.rs index ad9cb42..ded9ff7 100644 --- a/tests/syntax/staking.rs +++ b/tests/syntax/staking.rs @@ -9,7 +9,7 @@ use multiversx_sc::imports::*; pub const YEARLY_INTEREST: u64 = 7_000; pub const BPS: u64 = 100_000; pub const SECONDS_IN_DAY: u64 = 24 * 60 * 60; -pub const SECONDS_IN_YEAR: u64 = 365 * SECONDS_IN_DAY; +pub const SECONDS_IN_YEAR: u64 = 365_u64 * SECONDS_IN_DAY; #[multiversx_sc::contract] pub trait Staking { From 071c3fe3ede10310fad8e9874d206558a0efa565 Mon Sep 17 00:00:00 2001 From: acassimiro Date: Tue, 3 Sep 2024 16:21:55 -0300 Subject: [PATCH 4/5] Minor adjustments in tests --- tests/execution/constant-lookup.1.run | 3 +-- tests/execution/constant-lookup.2.run | 3 +-- tests/execution/constant-lookup.rs | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/execution/constant-lookup.1.run b/tests/execution/constant-lookup.1.run index 936166b..ee4f961 100644 --- a/tests/execution/constant-lookup.1.run +++ b/tests/execution/constant-lookup.1.run @@ -1,5 +1,4 @@ new ConstantValueLookup; call ConstantValueLookup.lookup_constant; return_value; -check_eq 7_000_u64 - +check_eq 7_000_u64 \ No newline at end of file diff --git a/tests/execution/constant-lookup.2.run b/tests/execution/constant-lookup.2.run index 61f79d1..2767362 100644 --- a/tests/execution/constant-lookup.2.run +++ b/tests/execution/constant-lookup.2.run @@ -1,5 +1,4 @@ new ConstantValueLookup; call ConstantValueLookup.lookup_constant_with_type; return_value; -check_eq 7_000_u64 - +check_eq 7100_u64 \ No newline at end of file diff --git a/tests/execution/constant-lookup.rs b/tests/execution/constant-lookup.rs index 5609665..4ac713f 100644 --- a/tests/execution/constant-lookup.rs +++ b/tests/execution/constant-lookup.rs @@ -3,7 +3,7 @@ #[allow(unused_imports)] use multiversx_sc::imports::*; -pub const YEARLY_INTEREST: u64 = 7_000; +pub const SAMPLE_CONSTANT: u64 = 7_000; #[multiversx_sc::contract] pub trait ConstantValueLookup { @@ -14,10 +14,10 @@ pub trait ConstantValueLookup { #[upgrade] fn upgrade(&self) {} - fn lookup_constant(&self) -> u64 { YEARLY_INTEREST } + fn lookup_constant(&self) -> u64 { SAMPLE_CONSTANT } fn lookup_constant_with_type(&self) -> u64 { - let x = YEARLY_INTEREST; + let x = 100_u64 + SAMPLE_CONSTANT; x } From 2b2771a09a9afaaf7d2fcf072e98c02543720bcb Mon Sep 17 00:00:00 2001 From: acassimiro Date: Wed, 4 Sep 2024 15:04:34 -0300 Subject: [PATCH 5/5] Making an exclusive module for the constant lookup rule and importing it for both preprocessing and execution --- rust-semantics/expression.md | 2 ++ rust-semantics/expression/constants.md | 14 ++++++++++++++ rust-semantics/preprocessing/constants.md | 5 ----- rust-semantics/targets/preprocessing/rust.md | 2 ++ 4 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 rust-semantics/expression/constants.md diff --git a/rust-semantics/expression.md b/rust-semantics/expression.md index 0ecb4be..644f674 100644 --- a/rust-semantics/expression.md +++ b/rust-semantics/expression.md @@ -1,4 +1,5 @@ ```k +requires "expression/constants.md" requires "expression/casts.md" requires "expression/literals.md" requires "expression/variables.md" @@ -7,5 +8,6 @@ module RUST-EXPRESSION imports private RUST-CASTS imports private RUST-EXPRESSION-LITERALS imports private RUST-EXPRESSION-VARIABLES + imports private RUST-EXPRESSION-CONSTANTS endmodule ``` diff --git a/rust-semantics/expression/constants.md b/rust-semantics/expression/constants.md new file mode 100644 index 0000000..cb66638 --- /dev/null +++ b/rust-semantics/expression/constants.md @@ -0,0 +1,14 @@ +```k + +module RUST-EXPRESSION-CONSTANTS + imports private COMMON-K-CELL + imports private RUST-SHARED-SYNTAX + imports private RUST-REPRESENTATION + imports private RUST-PREPROCESSING-CONFIGURATION + + rule Name:Identifier::.PathExprSegments => V ... + Name + V:Value + +endmodule +``` \ No newline at end of file diff --git a/rust-semantics/preprocessing/constants.md b/rust-semantics/preprocessing/constants.md index 95c3aa0..76caa73 100644 --- a/rust-semantics/preprocessing/constants.md +++ b/rust-semantics/preprocessing/constants.md @@ -24,10 +24,5 @@ module RUST-CONSTANTS ... - - rule Name:Identifier::.PathExprSegments => V ... - Name - V:Value - endmodule ``` \ No newline at end of file diff --git a/rust-semantics/targets/preprocessing/rust.md b/rust-semantics/targets/preprocessing/rust.md index 9d58471..12b9a70 100644 --- a/rust-semantics/targets/preprocessing/rust.md +++ b/rust-semantics/targets/preprocessing/rust.md @@ -4,6 +4,7 @@ requires "configuration.md" requires "../../preprocessing.md" requires "../../representation.md" requires "../../expression/casts.md" +requires "../../expression/constants.md" requires "../../expression/literals.md" requires "../../rust-common-syntax.md" @@ -13,6 +14,7 @@ endmodule module RUST imports private RUST-EXPRESSION-LITERALS + imports private RUST-EXPRESSION-CONSTANTS imports private RUST-PREPROCESSING imports private RUST-RUNNING-CONFIGURATION endmodule