Skip to content

Commit 3dbd6ab

Browse files
committed
Rust codegen: update code comments
1 parent 6f60a48 commit 3dbd6ab

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Rust/Print/LamVal.hs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ printOtherCase iTyDefs otherCase = do
163163
bodyDoc <- printValueE iTyDefs $ otherCase arg
164164
return $ group $ argDoc <+> "=>" <+> bodyDoc
165165

166-
--- | `printCaseIntE i [(1, x), (2,y)] (\other -> z)` translates into `LambdaBuffers.Runtime.Plutus.LamValcaseIntE i [(1,x), (2,y)] (\other -> z)`
166+
--- | Call `case_int` function `lbr_prelude`
167+
--
168+
-- `printCaseIntE i [(1, x), (2,y)] (\other -> z)` translates into `lbr_prelude::prelude::case_int(i, vec![(1, x), (2,y)]), Box::new(move |other| z)`
167169
printCaseIntE :: MonadPrint m => PC.TyDefs -> LV.ValueE -> [(LV.ValueE, LV.ValueE)] -> (LV.ValueE -> LV.ValueE) -> m (Doc ann)
168170
printCaseIntE iTyDefs caseIntVal cases otherCase = do
169171
caseIntERefDoc <- R.printRsQValName <$> LV.importValue RR.caseIntERef
@@ -180,11 +182,21 @@ printCaseIntE iTyDefs caseIntVal cases otherCase = do
180182
otherDoc <- printLamE iTyDefs otherCase
181183
return $ group $ caseIntERefDoc <> encloseSep lparen rparen comma [caseValDoc, align (R.printRsQValName RR.vecMacro <> encloseSep lbracket rbracket comma caseDocs), otherDoc]
182184

185+
{- | Print a list of values
186+
```
187+
[<A>, <B>]
188+
```
189+
-}
183190
printListE :: MonadPrint m => PC.TyDefs -> [LV.ValueE] -> m (Doc ann)
184191
printListE iTyDefs vals = do
185192
valDocs <- printValueE iTyDefs `traverse` vals
186193
return $ brackets (align (encloseSep mempty mempty (comma <> space) valDocs))
187194

195+
{- | Print a list (Vector in Rust) construction using vec! macro
196+
```
197+
std::vec![<A>, <B>]
198+
```
199+
-}
188200
printNewListE :: MonadPrint m => PC.TyDefs -> [LV.ValueE] -> m (Doc ann)
189201
printNewListE iTyDefs vals = do
190202
lst <- printListE iTyDefs vals
@@ -306,6 +318,19 @@ printCaseTextE iTyDefs txtVal cases otherCase = do
306318
otherDoc <- printOtherCase iTyDefs otherCase
307319
return $ "ma" <> align ("tch" <+> caseValDoc <+> braces (line <> vsep (punctuate comma (caseDocs <> [otherDoc]))))
308320

321+
{- | Print a reference
322+
323+
To help Rust type inference, we inject type information for a few references.
324+
In case of a `toPlutusData`, `fromPlutusData`, `toJson`, `fromJson` we call the reference as a trait
325+
method on the target type:
326+
```rs
327+
<TargetType>::to_plutus_data(...)
328+
```
329+
In case of `jsonConstructor`, we print the target type in a turbofish syntax:
330+
```rs
331+
lbr_prelude::json::json_constructr::<TargetType>::toPlutusData(...)
332+
```
333+
-}
309334
printRefE :: MonadPrint m => LV.Ref -> m (Doc ann)
310335
printRefE ref = do
311336
qvn <- LV.resolveRef ref

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Rust/Print/TyDef.hs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ opaque Maybe a
4040
4141
translates to
4242
43-
data Foo a b = Foo'MkFoo a | Foo'MkBar b
44-
type Maybe a = Prelude.Maybe a
43+
enum Foo<A, B> {
44+
MkFoo<A>,
45+
MkBar<B>
46+
}
47+
48+
type Maybe<A> = lbf_prelude::prelude::maybe<a>
4549
-}
4650
printTyDef :: MonadPrint m => PC.TyDef -> m (Doc ann)
4751
printTyDef (PC.TyDef tyN tyabs _) = do
@@ -55,7 +59,6 @@ printTyDef (PC.TyDef tyN tyabs _) = do
5559
<> absDoc
5660
else return $ group $ printTyDefKw kw <+> printTyName tyN <> generics <+> equals <+> absDoc
5761

58-
-- TODO(szg251): should we have a Pub wrapper type?
5962
printTyDefKw :: TyDefKw -> Doc ann
6063
printTyDefKw StructTyDef = "pub struct"
6164
printTyDefKw EnumTyDef = "pub enum"
@@ -75,8 +78,12 @@ printDeriveDebug =
7578
7679
For the above examples it prints
7780
78-
a b = Foo'MkFoo a | Foo'MkBar b
79-
a = Prelude.Maybe a
81+
<A, B> {
82+
MkFoo<A>,
83+
MkBar<B>
84+
}
85+
86+
<A> = lbf_prelude::prelude::maybe<a>
8087
-}
8188
printTyAbs :: MonadPrint m => PC.TyName -> PC.TyAbs -> m (TyDefKw, Doc ann, Doc ann)
8289
printTyAbs tyN (PC.TyAbs args body _) = do
@@ -88,8 +95,12 @@ printTyAbs tyN (PC.TyAbs args body _) = do
8895
8996
For the above examples it prints
9097
91-
Foo'MkFoo a | Foo'MkBar b
92-
Prelude.Maybe a
98+
{
99+
MkFoo<A>,
100+
MkBar<B>
101+
}
102+
103+
lbf_prelude::prelude::maybe<a>
93104
-}
94105
printTyBody :: MonadPrint m => PC.TyName -> [PC.TyArg] -> PC.TyBody -> m (TyDefKw, Doc ann)
95106
printTyBody parentTyN tyArgs (PC.SumI s) = (EnumTyDef,) <$> printSum parentTyN tyArgs s
@@ -189,6 +200,14 @@ phantomFieldIdent tyArg =
189200
"phantom_" <> R.printTyArg tyArg
190201

191202
{- | Prints an enum constructor with PhantomData fields
203+
for an LB type
204+
205+
```
206+
prod Something a b = Integer
207+
```
208+
209+
it prints
210+
192211
```rs
193212
PhantomDataCtor(PhantomData<A>, PhantomData<B>)
194213
```

0 commit comments

Comments
 (0)