Skip to content

Commit

Permalink
Refactor contract representation (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta authored Sep 23, 2024
1 parent 246f9f8 commit 067c58f
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 30 deletions.
4 changes: 0 additions & 4 deletions mx-rust-semantics/main/calls/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ module MX-RUST-CALLS-CONFIGURATION
configuration
<mx-rust-calls>
<rust-execution-state-stack> .List </rust-execution-state-stack>
<mx-rust-endpoint-to-function> .Map </mx-rust-endpoint-to-function> // String to Identifier
// Valid only while a contract call is being prepared
<mx-rust-last-trait-name> (#token("no#path", "Identifier"):Identifier):TypePath </mx-rust-last-trait-name>
</mx-rust-calls>
endmodule
Expand Down
13 changes: 5 additions & 8 deletions mx-rust-semantics/main/calls/implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module MX-RUST-CALLS-IMPLEMENTATION
imports private MX-CALL-CONFIGURATION
imports private MX-COMMON-SYNTAX
imports private MX-RUST-CALLS-CONFIGURATION
imports private MX-RUST-PREPROCESSED-ENDPOINTS-CONFIGURATION
imports private MX-RUST-PREPROCESSED-CONFIGURATION
imports private MX-RUST-REPRESENTATION
imports private RUST-EXECUTION-CONFIGURATION
imports private RUST-PREPROCESSING-CONFIGURATION
Expand Down Expand Up @@ -45,16 +47,14 @@ module MX-RUST-CALLS-IMPLEMENTATION
<k>
host.newEnvironment
( rustCode
( EndpointToFunction:Map
, TraitName:TypePath
( MxRustPreprocessed:MxRustPreprocessedCell
, Preprocessed:PreprocessedCell
)
)
=> .K
...
</k>
<mx-rust-last-trait-name> _ => TraitName </mx-rust-last-trait-name>
<mx-rust-endpoint-to-function> _ => EndpointToFunction </mx-rust-endpoint-to-function>
(_:MxRustPreprocessedCell => MxRustPreprocessed)
(_:PreprocessedCell => Preprocessed)
rule
Expand All @@ -68,10 +68,7 @@ module MX-RUST-CALLS-IMPLEMENTATION
<mx-rust-endpoint-to-function>
FunctionName |-> Endpoint:Identifier ...
</mx-rust-endpoint-to-function>
<mx-rust-last-trait-name>
TraitName:TypePath
=> (#token("no#path", "Identifier"):Identifier):TypePath
</mx-rust-last-trait-name>
<mx-rust-contract-trait> TraitName:TypePath </mx-rust-contract-trait>
<mx-call-args> Args:MxValueList </mx-call-args>
<trait-path> TraitName </trait-path>
<method-name> Endpoint </method-name>
Expand Down
5 changes: 4 additions & 1 deletion mx-rust-semantics/main/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
requires "calls/configuration.md"
requires "mx-semantics/main/configuration.md"
requires "preprocessing/configuration.md"
requires "rust-semantics/config.md"
module MX-RUST-COMMON-CONFIGURATION
imports MX-COMMON-CONFIGURATION
imports MX-RUST-CALLS-CONFIGURATION
imports MX-RUST-PREPROCESSED-CONFIGURATION
imports RUST-CONFIGURATION
configuration
<mx-rust>
<mx-rust-calls/>
<mx-rust-preprocessed/>
<mx-common/>
<rust/>
<mx-rust-calls/>
</mx-rust>
endmodule
Expand Down
27 changes: 27 additions & 0 deletions mx-rust-semantics/main/preprocessing/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
```k
module MX-RUST-PREPROCESSED-CONFIGURATION
imports MX-RUST-PREPROCESSED-CONTRACT-TRAIT-CONFIGURATION
imports MX-RUST-PREPROCESSED-ENDPOINTS-CONFIGURATION
configuration
<mx-rust-preprocessed>
<mx-rust-contract-trait/>
<mx-rust-endpoint-to-function/>
</mx-rust-preprocessed>
endmodule
module MX-RUST-PREPROCESSED-CONTRACT-TRAIT-CONFIGURATION
imports RUST-SHARED-SYNTAX
configuration
<mx-rust-contract-trait> (#token("no#path", "Identifier"):Identifier):TypePath </mx-rust-contract-trait> // String to Identifier
endmodule
module MX-RUST-PREPROCESSED-ENDPOINTS-CONFIGURATION
imports MAP
configuration
<mx-rust-endpoint-to-function> .Map </mx-rust-endpoint-to-function> // String to Identifier
endmodule
```
1 change: 1 addition & 0 deletions mx-rust-semantics/main/preprocessing/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module MX-RUST-PREPROCESSING-METHODS
imports private K-EQUAL-SYNTAX
imports private LIST
imports private MX-RUST-CALLS-CONFIGURATION
imports private MX-RUST-PREPROCESSED-ENDPOINTS-CONFIGURATION
imports private MX-RUST-REPRESENTATION
imports private RUST-PREPROCESSING-CONFIGURATION
imports private RUST-REPRESENTATION
Expand Down
27 changes: 25 additions & 2 deletions mx-rust-semantics/main/preprocessing/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,43 @@
module MX-RUST-PREPROCESSING-TRAITS
imports private COMMON-K-CELL
imports private LIST
imports private MX-RUST-PREPROCESSED-CONTRACT-TRAIT-CONFIGURATION
imports private MX-RUST-REPRESENTATION
imports private RUST-PREPROCESSING-CONFIGURATION
imports private RUST-SHARED-SYNTAX
syntax MxRustInstruction ::= mxRustPreprocessTraits(List)
syntax MxRustInstruction ::= mxRustPreprocessAddTraits(List)
| mxRustPreprocessAddTrait(TypePath)
| mxRustPreprocessTraits(List)
| mxRustPreprocessTrait(TypePath)
rule
<k>
mxRustPreprocessTraits => mxRustPreprocessTraits(Traits)
mxRustPreprocessTraits
=> mxRustPreprocessAddTraits(Traits) ~> mxRustPreprocessTraits(Traits)
...
</k>
<trait-list> Traits:List </trait-list>
rule mxRustPreprocessAddTraits(.List) => .K
rule mxRustPreprocessAddTraits(ListItem(Trait:TypePath) Traits:List)
=> mxRustPreprocessAddTrait(Trait) ~> mxRustPreprocessAddTraits(Traits)
rule
<k>
mxRustPreprocessAddTrait(Trait:TypePath) => .K
...
</k>
<trait-path> Trait </trait-path>
<trait-attributes>
#[ #token("multiversx_sc", "Identifier")
:: #token("contract", "Identifier")
:: .SimplePathList
]
.NonEmptyOuterAttributes
</trait-attributes>
<mx-rust-contract-trait> _ => Trait </mx-rust-contract-trait>
rule mxRustPreprocessTraits(.List) => .K
rule mxRustPreprocessTraits(ListItem(Trait:TypePath) Traits:List)
=> mxRustPreprocessTrait(Trait) ~> mxRustPreprocessTraits(Traits)
Expand Down
3 changes: 2 additions & 1 deletion mx-rust-semantics/main/representation.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ module MX-RUST-REPRESENTATION
syntax MxValue ::= rustDestination(Int, MxRustType)
syntax MxRustPreprocessedCell
syntax PreprocessedCell
syntax ContractCode ::= rustCode(endpointToFunction: Map, type: TypePath, preprocessed: PreprocessedCell)
syntax ContractCode ::= rustCode(MxRustPreprocessedCell, PreprocessedCell)
endmodule
```
20 changes: 12 additions & 8 deletions mx-rust-semantics/setup/mx.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module MX-RUST-SETUP-MX
imports private COMMON-K-CELL
imports private MX-COMMON-SYNTAX
imports private MX-RUST-CALLS-CONFIGURATION
imports private MX-RUST-PREPROCESSED-CONFIGURATION
imports private MX-RUST-REPRESENTATION
imports private MX-SETUP-SYNTAX
imports private RUST-PREPROCESSING-CONFIGURATION
Expand All @@ -16,7 +16,7 @@ module MX-RUST-SETUP-MX
"," gasLimit: Int
"," args: MxValueList
")"
syntax MXRustInstruction ::= "MxRust#addAccountWithPreprocessedCode" "(" String "," TypePath ")"
syntax MXRustInstruction ::= "MxRust#addAccountWithPreprocessedCode" "(" String ")"
| "MxRust#clearMxReturnValue"
rule mxRustCreateAccount(Address:String) => MXSetup#add_account(Address)
Expand Down Expand Up @@ -59,7 +59,7 @@ module MX-RUST-SETUP-MX
, gasLimit: GasLimit:Int
, args: Args:MxValueList
)
=> MxRust#addAccountWithPreprocessedCode(Contract, TraitName)
=> MxRust#addAccountWithPreprocessedCode(Contract)
~> callContract
( "#init"
, prepareIndirectContractCallInput
Expand All @@ -75,20 +75,19 @@ module MX-RUST-SETUP-MX
~> MxRust#clearMxReturnValue
...
</k>
<preprocessed> ... <trait-list> ListItem(TraitName:TypePath) </trait-list> </preprocessed>
rule
<k>
MxRust#addAccountWithPreprocessedCode(Contract, TraitName)
MxRust#addAccountWithPreprocessedCode(Contract)
=> MxRust#clearPreprocessed
~> MXSetup#add_account_with_code
( Contract
, rustCode(EndpointToFunction, TraitName, Preprocessed)
, rustCode(MxRustPreprocessed, Preprocessed)
)
...
</k>
Preprocessed:PreprocessedCell
<mx-rust-endpoint-to-function> EndpointToFunction:Map </mx-rust-endpoint-to-function>
MxRustPreprocessed:MxRustPreprocessedCell
syntax MXRustInstruction ::= "MxRust#clearPreprocessed"
rule
Expand All @@ -99,7 +98,12 @@ module MX-RUST-SETUP-MX
(_:PreprocessedCell
=> <preprocessed> ... <trait-list> .List </trait-list> </preprocessed>
)
<mx-rust-endpoint-to-function> _ => .Map </mx-rust-endpoint-to-function>
(_:MxRustPreprocessedCell
=> <mx-rust-preprocessed>
<mx-rust-endpoint-to-function> .Map </mx-rust-endpoint-to-function>
...
</mx-rust-preprocessed>
)
rule _V:MxValue ~> MxRust#clearMxReturnValue => .K
Expand Down
1 change: 1 addition & 0 deletions rust-semantics/preprocessing/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module RUST-PREPROCESSING-CONFIGURATION
<traits>
<trait multiplicity="*" type="Map">
<trait-path> my_identifier:TypePath </trait-path>
<trait-attributes> `emptyOuterAttributes`(.KList):OuterAttributes </trait-attributes>
<method-list> .List </method-list> // List of Identifier
<methods>
<method multiplicity="*" type="Map">
Expand Down
4 changes: 2 additions & 2 deletions rust-semantics/preprocessing/crate.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module CRATE
=> (.InnerAttributes Is):Crate
)
rule
(.K => traitParser(T))
(.K => traitParser(T, ItemAtts))
~> crateParser
( (_Atts:InnerAttributes (_ItemAtts:OuterAttributes _V:MaybeVisibility T:Trait):Item Is:Items):Crate
( (_Atts:InnerAttributes (ItemAtts:OuterAttributes _V:MaybeVisibility T:Trait):Item Is:Items):Crate
=> (.InnerAttributes Is):Crate
)
rule (.K => CI:ConstantItem:KItem)
Expand Down
3 changes: 2 additions & 1 deletion rust-semantics/preprocessing/initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module INITIALIZATION
imports private RUST-PREPROCESSING-PRIVATE-SYNTAX
rule
<k> traitInitializer(Name:TypePath) => .K
<k> traitInitializer(Name:TypePath, Atts:OuterAttributes) => .K
...
</k>
<trait-list> .List => ListItem(Name) ...</trait-list>
Expand All @@ -16,6 +16,7 @@ module INITIALIZATION
.Bag
=> <trait>
<trait-path> Name </trait-path>
<trait-attributes> Atts </trait-attributes>
...
</trait>
</traits>
Expand Down
3 changes: 2 additions & 1 deletion rust-semantics/preprocessing/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ module RUST-PREPROCESSING-PRIVATE-SYNTAX
imports RUST-REPRESENTATION
imports RUST-SHARED-SYNTAX
syntax Initializer ::= traitParser(Trait)
syntax Initializer ::= traitParser(Trait, OuterAttributes)
| traitMethodsParser(AssociatedItems, traitName:Identifier)
| traitInitializer
( traitName: TypePath
, atts: OuterAttributes
)
syntax Initializer ::= addMethod(traitName : TypePath, function: Function, atts:OuterAttributes)
Expand Down
7 changes: 5 additions & 2 deletions rust-semantics/preprocessing/trait.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
module TRAIT
imports private RUST-PREPROCESSING-PRIVATE-SYNTAX
rule traitParser(trait Name:Identifier { .InnerAttributes Functions:AssociatedItems })
=> traitInitializer(Name)
rule traitParser
( trait Name:Identifier { .InnerAttributes Functions:AssociatedItems }
, ItemAtts:OuterAttributes
)
=> traitInitializer(Name, ItemAtts)
~> traitMethodsParser(Functions, Name)
endmodule
Expand Down

0 comments on commit 067c58f

Please sign in to comment.