Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewriting constants #73

Merged
merged 6 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

5 changes: 5 additions & 0 deletions rust-semantics/preprocessing/constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ module RUST-CONSTANTS
</constant>
...
</constants>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule should actually be in a new file, called rust-semantics/expression/constants.mx. This file (and its module) should be required/imported in the same places as rust-semantics/expression/literals.md, so that it will be used both for preprocessing and for execution.

rule <k> Name:Identifier::.PathExprSegments => V ... </k>
<constant-name> Name </constant-name>
<constant-value> V:Value </constant-value>

endmodule
```
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
Loading