Skip to content

Commit

Permalink
Rewriting constants (#73)
Browse files Browse the repository at this point in the history
* Addressing constants with type identifiers, introducing tests and fixing path typo in rust-lite

* Fixing warning

* Addressing comments

* Minor adjustments in tests

---------

Co-authored-by: Virgil <[email protected]>
  • Loading branch information
ACassimiro and virgil-serbanuta authored Sep 4, 2024
1 parent 8dd30e7 commit 4e187e5
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 4 deletions.
4 changes: 2 additions & 2 deletions rust-lite/src/rust_lite/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
11 changes: 10 additions & 1 deletion rust-lite/src/rust_lite/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

2 changes: 2 additions & 0 deletions rust-semantics/expression.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
```k
requires "expression/constants.md"
requires "expression/casts.md"
requires "expression/literals.md"
requires "expression/variables.md"
Expand All @@ -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
```
14 changes: 14 additions & 0 deletions rust-semantics/expression/constants.md
Original file line number Diff line number Diff line change
@@ -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 <k> Name:Identifier::.PathExprSegments => V ... </k>
<constant-name> Name </constant-name>
<constant-value> V:Value </constant-value>
endmodule
```
2 changes: 2 additions & 0 deletions rust-semantics/targets/preprocessing/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/execution/constant-lookup.1.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new ConstantValueLookup;
call ConstantValueLookup.lookup_constant;
return_value;
check_eq 7_000_u64
4 changes: 4 additions & 0 deletions tests/execution/constant-lookup.2.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new ConstantValueLookup;
call ConstantValueLookup.lookup_constant_with_type;
return_value;
check_eq 7100_u64
24 changes: 24 additions & 0 deletions tests/execution/constant-lookup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![no_std]

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

pub const SAMPLE_CONSTANT: u64 = 7_000;

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

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

fn lookup_constant(&self) -> u64 { SAMPLE_CONSTANT }

fn lookup_constant_with_type(&self) -> u64 {
let x = 100_u64 + SAMPLE_CONSTANT;
x
}

}
2 changes: 1 addition & 1 deletion tests/syntax/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4e187e5

Please sign in to comment.