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 2 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
6 changes: 3 additions & 3 deletions rust-lite/src/rust_lite/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Copy link
Member

Choose a reason for hiding this comment

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

Empty space change, it may be worth removing.

def trigger_exec_run(stripped_args):
options = generate_options(stripped_args)
executor_name = 'exec_run'
Expand All @@ -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)

9 changes: 9 additions & 0 deletions rust-semantics/preprocessing/constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,14 @@ 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>

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

endmodule
```
5 changes: 5 additions & 0 deletions tests/execution/constant-lookup.1.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
new ConstantValueLookup;
call ConstantValueLookup.lookup_constant;
return_value;
check_eq 7_000_u64

5 changes: 5 additions & 0 deletions tests/execution/constant-lookup.2.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
new ConstantValueLookup;
call ConstantValueLookup.lookup_constant_with_type;
return_value;
check_eq 7_000_u64

23 changes: 23 additions & 0 deletions tests/execution/constant-lookup.rs
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Member

Choose a reason for hiding this comment

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

Does this actually work if you try to compile it with rustc? Shouldn't this be let x = YEARLY_INTEREST;?

x }
Copy link
Member

Choose a reason for hiding this comment

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

New line after x?


}
Loading