Skip to content

Commit

Permalink
Preprocess proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta committed Sep 16, 2024
1 parent 406fdef commit 9843ef4
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 31 deletions.
103 changes: 87 additions & 16 deletions mx-rust-semantics/main/preprocessing/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,26 @@ module MX-RUST-PREPROCESSING-METHODS
syntax Identifier ::= "storage_mapper" [token]
syntax MxRustInstruction ::= mxRustPreprocessMethods(trait: TypePath, methodNames: List)
| mxRustPreprocessMethod(trait: TypePath, methodName: Identifier)
| mxRustPreprocessStorage(trait: TypePath, methodName: Identifier)
| mxRustPreprocessEndpoint(trait: TypePath, methodName: Identifier)
syntax MxRustInstruction ::= mxRustPreprocessMethods
( trait: TypePath
, traitType: TraitType
, methodNames: List
)
| mxRustPreprocessMethod
( trait: TypePath
, traitType: TraitType
, methodName: Identifier
)
| mxRustPreprocessStorage
( trait: TypePath
, traitType: TraitType
, methodName: Identifier
)
| mxRustPreprocessEndpoint
( trait: TypePath
, traitType: TraitType
, methodName: Identifier
)
| addStorageMethodBody
( trait: TypePath
, methodName: Identifier
Expand All @@ -30,24 +46,27 @@ module MX-RUST-PREPROCESSING-METHODS
)
rule
<k> mxRustPreprocessMethods(T:TypePath)
=> mxRustPreprocessMethods(T, MethodNames)
<k> mxRustPreprocessMethods(T:TypePath, TType:TraitType)
=> mxRustPreprocessMethods(T, TType, MethodNames)
...
</k>
<trait-path> T </trait-path>
<method-list> MethodNames:List </method-list>
rule mxRustPreprocessMethods(_:TypePath, .List) => .K
rule mxRustPreprocessMethods(T:TypePath, ListItem(MethodName:Identifier) Names:List)
=> mxRustPreprocessMethod(T, MethodName) ~> mxRustPreprocessMethods(T, Names)
rule mxRustPreprocessMethods(_:TypePath, _:TraitType, .List) => .K
rule mxRustPreprocessMethods
(T:TypePath, TType:TraitType, ListItem(MethodName:Identifier) Names:List)
=> mxRustPreprocessMethod(T, TType, MethodName)
~> mxRustPreprocessMethods(T, TType, Names)
rule mxRustPreprocessMethod(Trait:TypePath, Method:Identifier)
=> mxRustPreprocessStorage(Trait, Method)
~> mxRustPreprocessEndpoint(Trait, Method)
rule mxRustPreprocessMethod(Trait:TypePath, TType:TraitType, Method:Identifier)
=> mxRustPreprocessStorage(Trait, TType, Method)
~> mxRustPreprocessEndpoint(Trait, TType, Method)
rule mxRustPreprocessStorage(_Trait:TypePath, proxy, _Method:Identifier) => .K
rule
<k>
mxRustPreprocessStorage(Trait:TypePath, Method:Identifier)
mxRustPreprocessStorage(Trait:TypePath, contract, Method:Identifier)
=> addStorageMethodBody
(... trait: Trait, methodName: Method
, storageName: getStorageName(Atts)
Expand All @@ -64,12 +83,28 @@ module MX-RUST-PREPROCESSING-METHODS
</method-return-type>
requires getStorageName(Atts) =/=K ""
[priority(50)]
rule mxRustPreprocessStorage(_Trait:TypePath, _Method:Identifier) => .K
rule mxRustPreprocessStorage(_Trait:TypePath, contract, _Method:Identifier) => .K
[priority(100)]
rule
<k>
mxRustPreprocessEndpoint(Trait:TypePath, Method:Identifier)
mxRustPreprocessEndpoint(Trait:TypePath, proxy, Method:Identifier)
=> .K
...
</k>
<trait-path> Trait </trait-path>
<method-name> Method </method-name>
<method-implementation>
empty
=> block(buildProxyEndpointMethod(Params, getEndpointName(Atts, Method)))
</method-implementation>
<method-outer-attributes> Atts:OuterAttributes </method-outer-attributes>
<method-params> Params:NormalizedFunctionParameterList </method-params>
requires getEndpointName(Atts, Method) =/=K ""
[priority(50)]
rule
<k>
mxRustPreprocessEndpoint(Trait:TypePath, contract, Method:Identifier)
=> mxRustAddEndpointMapping
(... trait: Trait, methodName: Method
, endpointName: getEndpointName(Atts, Method)
Expand All @@ -81,7 +116,7 @@ module MX-RUST-PREPROCESSING-METHODS
<method-outer-attributes> Atts:OuterAttributes </method-outer-attributes>
requires getEndpointName(Atts, Method) =/=K ""
[priority(50)]
rule mxRustPreprocessEndpoint(_Trait:TypePath, _Method:Identifier) => .K
rule mxRustPreprocessEndpoint(_Trait:TypePath, contract, _Method:Identifier) => .K
[priority(100)]
rule
Expand Down Expand Up @@ -150,6 +185,42 @@ module MX-RUST-PREPROCESSING-METHODS
=> getEndpointName(Atts, Default)
[owise]
syntax BlockExpression ::= buildProxyEndpointMethod
( params: NormalizedFunctionParameterList
, endpointName: String
) [function]
rule buildProxyEndpointMethod
(... params: S:SelfSort : _ , Params:NormalizedFunctionParameterList
, endpointName: Name
)
=> { .InnerAttributes
S . #token("endpoint_name", "Identifier") = Name;
S . #token("args", "Identifier") =
(paramsToMaybeTupleElements(Params)):TupleExpression;
S
}
syntax MaybeTupleElements ::= paramsToMaybeTupleElements(NormalizedFunctionParameterList) [function, total]
rule paramsToMaybeTupleElements(.NormalizedFunctionParameterList) => `noTupleElements`(.KList)
rule paramsToMaybeTupleElements(Name:Identifier : _ , .NormalizedFunctionParameterList)
=> Name ,
rule paramsToMaybeTupleElements
( Name:Identifier : _
, N:NormalizedFunctionParameter
, Ns:NormalizedFunctionParameterList
)
=> Name , paramsToTupleElementsNoEndComma(N , Ns)
rule paramsToMaybeTupleElements(P , _Ps:NormalizedFunctionParameterList)
=> error("Unexpected param in paramsToMaybeTupleElements", ListItem(P)) ,
syntax TupleElementsNoEndComma ::= paramsToTupleElementsNoEndComma(NormalizedFunctionParameterList) [function, total]
rule paramsToTupleElementsNoEndComma(.NormalizedFunctionParameterList)
=> .TupleElementsNoEndComma
rule paramsToTupleElementsNoEndComma(Name:Identifier : _ , Ps:NormalizedFunctionParameterList)
=> Name , paramsToTupleElementsNoEndComma(Ps)
rule paramsToTupleElementsNoEndComma(P , Ps:NormalizedFunctionParameterList)
=> error("Unexpected param in paramsToTupleElementsNoEndComma", ListItem(P))
, paramsToTupleElementsNoEndComma(Ps)
syntax BlockExpression ::= buildStorageMethodBody
( params: NormalizedFunctionParameterList
, storageName: String
Expand Down
27 changes: 26 additions & 1 deletion mx-rust-semantics/main/preprocessing/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,32 @@ module MX-RUST-PREPROCESSING-TRAITS
rule mxRustPreprocessTraits(ListItem(Trait:TypePath) Traits:List)
=> mxRustPreprocessTrait(Trait) ~> mxRustPreprocessTraits(Traits)
rule mxRustPreprocessTrait(Trait:TypePath) => mxRustPreprocessMethods(Trait)
rule
<k>
mxRustPreprocessTrait(Trait:TypePath) => mxRustPreprocessMethods(Trait, contract)
...
</k>
<trait-path> Trait </trait-path>
<trait-attributes>
#[ #token("multiversx_sc", "Identifier")
:: #token("contract", "Identifier")
:: .SimplePathList
]
.NonEmptyOuterAttributes
</trait-attributes>
rule
<k>
mxRustPreprocessTrait(Trait:TypePath) => mxRustPreprocessMethods(Trait, proxy)
...
</k>
<trait-path> Trait </trait-path>
<trait-attributes>
#[ #token("multiversx_sc", "Identifier")
:: #token("proxy", "Identifier")
:: .SimplePathList
]
.NonEmptyOuterAttributes
</trait-attributes>
endmodule
```
4 changes: 3 additions & 1 deletion mx-rust-semantics/main/representation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module MX-RUST-REPRESENTATION
imports RUST-SHARED-SYNTAX
syntax MxRustInstruction ::= "mxRustPreprocessTraits"
| mxRustPreprocessMethods(TypePath)
| mxRustPreprocessMethods(TypePath, TraitType)
| mxRustCreateAccount(String)
| mxRustCreateContract
( owner: String
Expand All @@ -22,6 +22,7 @@ module MX-RUST-REPRESENTATION
| mxRustLoadPtr(Int)
| mxRustGetBigIntFromStruct(Value)
syntax TraitType ::= "contract" | "proxy"
syntax MxRustType ::= "noType" | rustType(Type)
syntax MxRustTypeOrError ::= MxRustType | SemanticsError
syntax Value ::= MxRustType
Expand All @@ -30,6 +31,7 @@ module MX-RUST-REPRESENTATION
syntax Expression ::= concatString(Expression, Expression) [seqstrict]
| toString(Expression) [strict]
| error(String, KItem)
syntax MxValue ::= rustDestination(Int, MxRustType)
Expand Down
9 changes: 7 additions & 2 deletions mx-rust-semantics/setup/mx.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module MX-RUST-SETUP-MX
"," gasLimit: Int
"," args: MxValueList
")"
syntax MXRustInstruction ::= "MxRust#addAccountWithPreprocessedCode" "(" String ")"
syntax MXRustInstruction ::= "MxRust#addAccountWithPreprocessedCode" "(" String "," TypePath ")"
| "MxRust#clearMxReturnValue"
Expand Down Expand Up @@ -59,7 +60,8 @@ module MX-RUST-SETUP-MX
, gasLimit: GasLimit:Int
, args: Args:MxValueList
)
=> MxRust#addAccountWithPreprocessedCode(Contract, TraitName)
=> findContractName(Traits)
~> MxRust#addAccountWithPreprocessedCode(Contract)
~> callContract
( "#init"
, prepareIndirectContractCallInput
Expand All @@ -75,7 +77,10 @@ module MX-RUST-SETUP-MX
~> MxRust#clearMxReturnValue
...
</k>
<preprocessed> ... <trait-list> ListItem(TraitName:TypePath) </trait-list> </preprocessed>
<preprocessed> ... <trait-list> Traits:List </trait-list> </preprocessed>
rule TraitName:TypePath ~> MxRust#addAccountWithPreprocessedCode(Contract)
=> MxRust#addAccountWithPreprocessedCode(Contract, TraitName)
rule
<k>
Expand Down
2 changes: 2 additions & 0 deletions rust-semantics/preprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ requires "preprocessing/crate.md"
requires "preprocessing/configuration.md"
requires "preprocessing/helpers.md"
requires "preprocessing/initialization.md"
requires "preprocessing/module.md"
requires "preprocessing/syntax.md"
requires "preprocessing/tools.md"
requires "preprocessing/trait.md"
Expand All @@ -14,6 +15,7 @@ module RUST-PREPROCESSING
imports private CRATE
imports private INITIALIZATION
imports private RUST-CONSTANTS
imports private RUST-MODULE
imports private RUST-PREPROCESSING-TOOLS
imports private TRAIT
imports private TRAIT-METHODS
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
9 changes: 7 additions & 2 deletions rust-semantics/preprocessing/crate.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ module CRATE
( (_Atts:InnerAttributes (_A:OuterAttributes _U:UseDeclaration):Item Is:Items):Crate
=> (.InnerAttributes Is):Crate
)
rule (.K => moduleParser(M))
~> crateParser
( (_Atts:InnerAttributes (_ItemAtts:OuterAttributes _V:MaybeVisibility M:Module):Item Is:Items):Crate
=> (.InnerAttributes Is):Crate
)
rule
(.K => traitParser(T))
(.K => traitParser(T, .TypePath, 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
25 changes: 25 additions & 0 deletions rust-semantics/preprocessing/module.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
```k
module RUST-MODULE
imports private RUST-PREPROCESSING-PRIVATE-SYNTAX
rule moduleParser(mod Name:Identifier { _:InnerAttributes Contents:Items })
=> moduleItemsParser(Contents, Name)
rule moduleItemsParser(.Items, _Name) => .K
rule
(.K => traitParser(T, Name, ItemAtts))
~> moduleItemsParser
( (ItemAtts:OuterAttributes _V:MaybeVisibility T:Trait):Item Is:Items
=> Is
, Name
)
rule
moduleItemsParser
( (_OuterAttributes _:MacroItem):Item Is:Items
=> Is
, _Name
)
endmodule
```
11 changes: 9 additions & 2 deletions rust-semantics/preprocessing/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ module RUST-PREPROCESSING-PRIVATE-SYNTAX
imports RUST-REPRESENTATION
imports RUST-SHARED-SYNTAX
syntax Initializer ::= traitParser(Trait)
| traitMethodsParser(AssociatedItems, traitName:Identifier)
syntax MaybeTypePath ::= ".TypePath" | TypePath
syntax Initializer ::= traitParser(Trait, MaybeTypePath, OuterAttributes)
| traitMethodsParser(AssociatedItems, traitName:TypePath)
| traitInitializer
( traitName: TypePath
, atts: OuterAttributes
)
syntax Initializer ::= moduleParser(Module)
| moduleItemsParser(Items, TypePath)
syntax Initializer ::= addMethod(traitName : TypePath, function: Function, atts:OuterAttributes)
| #addMethod(
Expand All @@ -28,6 +34,7 @@ module RUST-PREPROCESSING-PRIVATE-SYNTAX
OuterAttributes
)
syntax TypePath ::= append(MaybeTypePath, Identifier) [function, total]
endmodule
```
11 changes: 11 additions & 0 deletions rust-semantics/preprocessing/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module RUST-PREPROCESSING-TOOLS
imports private BOOL
imports private RUST-PREPROCESSING-PRIVATE-SYNTAX
imports private RUST-REPRESENTATION
imports private RUST-SHARED-SYNTAX
imports private RUST-VALUE-SYNTAX
Expand All @@ -13,6 +14,16 @@ module RUST-PREPROCESSING-TOOLS
rule reverse(.CallParamsList, L:CallParamsList) => L
rule reverse((P , Ps:CallParamsList => Ps), (L:CallParamsList => P, L))
rule append(.TypePath, Name:Identifier) => Name
rule append(:: T:TypePathSegments, Name:Identifier) => :: appendSegments(T, Name)
rule append(T:TypePathSegments, Name:Identifier) => appendSegments(T, Name)
rule appendSegments(S:TypePathSegment, Name:Identifier) => S :: Name
rule appendSegments(S:TypePathSegment :: T:TypePathSegments, Name:Identifier)
=> S :: appendSegments(T, Name)
syntax TypePathSegments ::= appendSegments(TypePathSegments, Identifier) [function, total]
endmodule
```
4 changes: 2 additions & 2 deletions rust-semantics/preprocessing/trait-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module TRAIT-METHODS
imports private RUST-PREPROCESSING-PRIVATE-HELPERS
imports private RUST-PREPROCESSING-PRIVATE-SYNTAX
rule traitMethodsParser(.AssociatedItems, _Name:Identifier)
rule traitMethodsParser(.AssociatedItems, _Name:TypePath)
=> .K
rule (.K => addMethod(TraitName, F, A))
~> traitMethodsParser(
(A:OuterAttributes F:Function) AIs:AssociatedItems => AIs,
TraitName:Identifier
TraitName:TypePath
)
endmodule
Expand Down
Loading

0 comments on commit 9843ef4

Please sign in to comment.