diff --git a/mx-rust-semantics/main/preprocessing/methods.md b/mx-rust-semantics/main/preprocessing/methods.md index bf0676e..9aff04f 100644 --- a/mx-rust-semantics/main/preprocessing/methods.md +++ b/mx-rust-semantics/main/preprocessing/methods.md @@ -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 @@ -30,24 +46,27 @@ module MX-RUST-PREPROCESSING-METHODS ) rule - mxRustPreprocessMethods(T:TypePath) - => mxRustPreprocessMethods(T, MethodNames) + mxRustPreprocessMethods(T:TypePath, TType:TraitType) + => mxRustPreprocessMethods(T, TType, MethodNames) ... T MethodNames: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 - mxRustPreprocessStorage(Trait:TypePath, Method:Identifier) + mxRustPreprocessStorage(Trait:TypePath, contract, Method:Identifier) => addStorageMethodBody (... trait: Trait, methodName: Method , storageName: getStorageName(Atts) @@ -64,12 +83,28 @@ module MX-RUST-PREPROCESSING-METHODS requires getStorageName(Atts) =/=K "" [priority(50)] - rule mxRustPreprocessStorage(_Trait:TypePath, _Method:Identifier) => .K + rule mxRustPreprocessStorage(_Trait:TypePath, contract, _Method:Identifier) => .K [priority(100)] rule - mxRustPreprocessEndpoint(Trait:TypePath, Method:Identifier) + mxRustPreprocessEndpoint(Trait:TypePath, proxy, Method:Identifier) + => .K + ... + + Trait + Method + + empty + => block(buildProxyEndpointMethod(Params, getEndpointName(Atts, Method))) + + Atts:OuterAttributes + Params:NormalizedFunctionParameterList + requires getEndpointName(Atts, Method) =/=K "" + [priority(50)] + rule + + mxRustPreprocessEndpoint(Trait:TypePath, contract, Method:Identifier) => mxRustAddEndpointMapping (... trait: Trait, methodName: Method , endpointName: getEndpointName(Atts, Method) @@ -81,7 +116,7 @@ module MX-RUST-PREPROCESSING-METHODS Atts:OuterAttributes 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 @@ -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 diff --git a/mx-rust-semantics/main/preprocessing/traits.md b/mx-rust-semantics/main/preprocessing/traits.md index e37339e..8248105 100644 --- a/mx-rust-semantics/main/preprocessing/traits.md +++ b/mx-rust-semantics/main/preprocessing/traits.md @@ -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 + + mxRustPreprocessTrait(Trait:TypePath) => mxRustPreprocessMethods(Trait, contract) + ... + + Trait + + #[ #token("multiversx_sc", "Identifier") + :: #token("contract", "Identifier") + :: .SimplePathList + ] + .NonEmptyOuterAttributes + + rule + + mxRustPreprocessTrait(Trait:TypePath) => mxRustPreprocessMethods(Trait, proxy) + ... + + Trait + + #[ #token("multiversx_sc", "Identifier") + :: #token("proxy", "Identifier") + :: .SimplePathList + ] + .NonEmptyOuterAttributes + endmodule ``` diff --git a/mx-rust-semantics/main/representation.md b/mx-rust-semantics/main/representation.md index 5de8bc8..f66d0d3 100644 --- a/mx-rust-semantics/main/representation.md +++ b/mx-rust-semantics/main/representation.md @@ -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 @@ -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 @@ -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) diff --git a/mx-rust-semantics/setup/mx.md b/mx-rust-semantics/setup/mx.md index 75f243b..514ff9e 100644 --- a/mx-rust-semantics/setup/mx.md +++ b/mx-rust-semantics/setup/mx.md @@ -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" @@ -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 @@ -75,7 +77,10 @@ module MX-RUST-SETUP-MX ~> MxRust#clearMxReturnValue ... - ... ListItem(TraitName:TypePath) + ... Traits:List + + rule TraitName:TypePath ~> MxRust#addAccountWithPreprocessedCode(Contract) + => MxRust#addAccountWithPreprocessedCode(Contract, TraitName) rule diff --git a/rust-semantics/preprocessing.md b/rust-semantics/preprocessing.md index f12e1f0..4a47388 100644 --- a/rust-semantics/preprocessing.md +++ b/rust-semantics/preprocessing.md @@ -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" @@ -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 diff --git a/rust-semantics/preprocessing/configuration.md b/rust-semantics/preprocessing/configuration.md index fc3a6fd..314415a 100644 --- a/rust-semantics/preprocessing/configuration.md +++ b/rust-semantics/preprocessing/configuration.md @@ -18,6 +18,7 @@ module RUST-PREPROCESSING-CONFIGURATION my_identifier:TypePath + `emptyOuterAttributes`(.KList):OuterAttributes .List // List of Identifier diff --git a/rust-semantics/preprocessing/crate.md b/rust-semantics/preprocessing/crate.md index 099a3e6..017e932 100644 --- a/rust-semantics/preprocessing/crate.md +++ b/rust-semantics/preprocessing/crate.md @@ -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) diff --git a/rust-semantics/preprocessing/initialization.md b/rust-semantics/preprocessing/initialization.md index 4828b65..e77c750 100644 --- a/rust-semantics/preprocessing/initialization.md +++ b/rust-semantics/preprocessing/initialization.md @@ -7,7 +7,7 @@ module INITIALIZATION imports private RUST-PREPROCESSING-PRIVATE-SYNTAX rule - traitInitializer(Name:TypePath) => .K + traitInitializer(Name:TypePath, Atts:OuterAttributes) => .K ... .List => ListItem(Name) ... @@ -16,6 +16,7 @@ module INITIALIZATION .Bag => Name + Atts ... diff --git a/rust-semantics/preprocessing/module.md b/rust-semantics/preprocessing/module.md new file mode 100644 index 0000000..536b394 --- /dev/null +++ b/rust-semantics/preprocessing/module.md @@ -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 + +``` diff --git a/rust-semantics/preprocessing/syntax.md b/rust-semantics/preprocessing/syntax.md index e848d55..7de2da7 100644 --- a/rust-semantics/preprocessing/syntax.md +++ b/rust-semantics/preprocessing/syntax.md @@ -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( @@ -28,6 +34,7 @@ module RUST-PREPROCESSING-PRIVATE-SYNTAX OuterAttributes ) + syntax TypePath ::= append(MaybeTypePath, Identifier) [function, total] endmodule ``` diff --git a/rust-semantics/preprocessing/tools.md b/rust-semantics/preprocessing/tools.md index 2896052..7fede8e 100644 --- a/rust-semantics/preprocessing/tools.md +++ b/rust-semantics/preprocessing/tools.md @@ -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 @@ -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 ``` \ No newline at end of file diff --git a/rust-semantics/preprocessing/trait-methods.md b/rust-semantics/preprocessing/trait-methods.md index 79bb0f7..b5ec681 100644 --- a/rust-semantics/preprocessing/trait-methods.md +++ b/rust-semantics/preprocessing/trait-methods.md @@ -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 diff --git a/rust-semantics/preprocessing/trait.md b/rust-semantics/preprocessing/trait.md index 0fc23aa..3f3fb51 100644 --- a/rust-semantics/preprocessing/trait.md +++ b/rust-semantics/preprocessing/trait.md @@ -3,9 +3,13 @@ module TRAIT imports private RUST-PREPROCESSING-PRIVATE-SYNTAX - rule traitParser(trait Name:Identifier { .InnerAttributes Functions:AssociatedItems }) - => traitInitializer(Name) - ~> traitMethodsParser(Functions, Name) + rule traitParser + ( trait Name:Identifier { .InnerAttributes Functions:AssociatedItems } + , Path:MaybeTypePath + , ItemAtts:OuterAttributes + ) + => traitInitializer(append(Path, Name), ItemAtts) + ~> traitMethodsParser(Functions, append(Path, Name)) endmodule ``` diff --git a/rust-semantics/rust-common-syntax.md b/rust-semantics/rust-common-syntax.md index d7636ff..26c354e 100644 --- a/rust-semantics/rust-common-syntax.md +++ b/rust-semantics/rust-common-syntax.md @@ -497,7 +497,8 @@ https://doc.rust-lang.org/reference/items/extern-crates.html ```k syntax TupleExpression ::= "(" MaybeTupleElements ")" - syntax MaybeTupleElements ::= "" | TupleElements + syntax MaybeTupleElements ::= "" [symbol(noTupleElements)] + | TupleElements syntax TupleElements ::= Expression "," | Expression "," TupleElementsNoEndComma | Expression "," TupleElementsNoEndComma ","