Skip to content

Commit bc3a819

Browse files
author
jared
committed
Properly propagate pkgMap through the TS code generator
1 parent 5733250 commit bc3a819

File tree

6 files changed

+129
-119
lines changed

6 files changed

+129
-119
lines changed

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Typescript/Print.hs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import Proto.Codegen qualified as P
3737
printModule :: MonadPrint m => Ts.PkgMap -> m (Doc ann, Set Text)
3838
printModule pkgMap = do
3939
ctx <- ask
40-
tyDefDocs <- printTyDefs (ctx ^. Print.ctxModule)
41-
instDocs <- printInstances
40+
tyDefDocs <- printTyDefs pkgMap (ctx ^. Print.ctxModule)
41+
instDocs <- printInstances pkgMap
4242
st <- get
4343

4444
let modDoc =
@@ -84,10 +84,11 @@ printModule pkgMap = do
8484
(st ^. Print.stValueImports)
8585
return (modDoc, pkgDeps)
8686

87-
printTyDefs :: MonadPrint m => PC.Module -> m [Doc ann]
88-
printTyDefs m = for (toList $ m ^. #typeDefs) printTyDef
87+
printTyDefs :: MonadPrint m => Ts.PkgMap -> PC.Module -> m [Doc ann]
88+
printTyDefs pkgMap m = for (toList $ m ^. #typeDefs) $ printTyDef pkgMap
8989

9090
tsClassImplPrinters ::
91+
Ts.PkgMap ->
9192
Map
9293
Ts.QClassName
9394
( PC.ModuleName ->
@@ -96,37 +97,37 @@ tsClassImplPrinters ::
9697
PC.Ty ->
9798
Either P.InternalError (Doc ann, Set Ts.QValName)
9899
)
99-
tsClassImplPrinters =
100+
tsClassImplPrinters pkgMap =
100101
Map.fromList
101102
[
102103
( tsEqClass
103-
, printDeriveEq
104+
, printDeriveEq pkgMap
104105
)
105106
,
106107
( tsIsPlutusDataClass
107-
, printDeriveIsPlutusData
108+
, printDeriveIsPlutusData pkgMap
108109
)
109110
,
110111
( tsJsonClass
111-
, printDeriveJson
112+
, printDeriveJson pkgMap
112113
)
113114
]
114115

115-
printInstances :: MonadPrint m => m [Doc ann]
116-
printInstances = do
116+
printInstances :: MonadPrint m => Ts.PkgMap -> m [Doc ann]
117+
printInstances pkgMap = do
117118
ci <- asks (view Print.ctxCompilerInput)
118119
m <- asks (view Print.ctxModule)
119120
let iTyDefs = PC.indexTyDefs ci
120121
foldrM
121122
( \d instDocs -> do
122-
instDocs' <- printDerive iTyDefs d
123+
instDocs' <- printDerive pkgMap iTyDefs d
123124
return $ instDocs' <> instDocs
124125
)
125126
mempty
126127
(toList $ m ^. #derives)
127128

128-
printDerive :: MonadPrint m => PC.TyDefs -> PC.Derive -> m [Doc ann]
129-
printDerive iTyDefs d = do
129+
printDerive :: MonadPrint m => Ts.PkgMap -> PC.TyDefs -> PC.Derive -> m [Doc ann]
130+
printDerive pkgMap iTyDefs d = do
130131
mn <- asks (view $ Print.ctxModule . #moduleName)
131132
let qcn = PC.qualifyClassRef mn (d ^. #constraint . #classRef)
132133
classes <- asks (view $ Print.ctxConfig . C.cfgClasses)
@@ -137,16 +138,16 @@ printDerive iTyDefs d = do
137138
pqcns
138139
( \pqcn -> do
139140
Print.importClass pqcn
140-
printTsQClassImpl mn iTyDefs pqcn d
141+
printTsQClassImpl pkgMap mn iTyDefs pqcn d
141142
)
142143

143-
printTsQClassImpl :: MonadPrint m => PC.ModuleName -> PC.TyDefs -> Ts.QClassName -> PC.Derive -> m (Doc ann)
144-
printTsQClassImpl mn iTyDefs hqcn d =
145-
case Map.lookup hqcn tsClassImplPrinters of
144+
printTsQClassImpl :: MonadPrint m => Ts.PkgMap -> PC.ModuleName -> PC.TyDefs -> Ts.QClassName -> PC.Derive -> m (Doc ann)
145+
printTsQClassImpl pkgMap mn iTyDefs hqcn d =
146+
case Map.lookup hqcn (tsClassImplPrinters pkgMap) of
146147
Nothing -> throwInternalError (d ^. #constraint . #sourceInfo) ("Missing capability to print the Typescript type class " <> show hqcn)
147148
Just implPrinter -> do
148149
let ty = d ^. #constraint . #argument
149-
mkInstanceDoc <- printExportInstanceDecl hqcn ty
150+
mkInstanceDoc <- printExportInstanceDecl pkgMap hqcn ty
150151
case implPrinter mn iTyDefs mkInstanceDoc ty of
151152
Left err -> throwInternalError (d ^. #constraint . #sourceInfo) ("Failed printing the implementation for " <> (show hqcn <> "\nGot error: " <> show err))
152153
Right (instanceDefsDoc, valImps) -> do
@@ -241,13 +242,13 @@ printImports selfModName pkgMap lbTyImports tsTyImports classImps ruleImps valIm
241242
importQualified :: (Doc ann, Doc ann) -> Doc ann
242243
importQualified (pkg, mn) = "import" <+> "*" <+> "as" <+> mn <+> "from" <+> squotes pkg
243244

244-
{- | `collectPackageDeps pkgMap lbTyImports hsTyImports classImps ruleImps valImps` collects all the package dependencies.
245+
{- | `collectPackageDeps pkgMap lbTyImports tsTyImports classImps ruleImps valImps` collects all the package dependencies.
245246
Note that LB `lbTyImports` and `ruleImps` are wired by the user (as the user decides on the package name for their schemas).
246247
-}
247248
collectPackageDeps :: Ts.PkgMap -> Set PC.QTyName -> Set Ts.QTyName -> Set Ts.QClassName -> Set (PC.InfoLess PC.ModuleName) -> Set Ts.QValName -> Set Text
248-
collectPackageDeps pkgMap lbTyImports hsTyImports classImps ruleImps valImps =
249+
collectPackageDeps pkgMap lbTyImports tsTyImports classImps ruleImps valImps =
249250
let deps =
250-
Set.fromList [Ts.pkgNameToText pkgName | (Just pkgName, _, _) <- toList hsTyImports]
251+
Set.fromList [Ts.pkgNameToText pkgName | (Just pkgName, _, _) <- toList tsTyImports]
251252
`Set.union` Set.fromList [Ts.pkgNameToText pkgName | (pkgName, _, _) <- toList classImps]
252253
`Set.union` Set.fromList [Ts.pkgNameToText pkgName | (Just (pkgName, _), _) <- toList valImps]
253254
`Set.union` Set.fromList

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Typescript/Print/Derive.hs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ lamTy2PCTy = \case
4343
-- Note(jaredponn): hopefully this never happens...
4444
Nothing
4545

46-
instanceDictIdent :: Ts.QClassName -> PC.Ty -> Text
47-
instanceDictIdent className ty =
46+
instanceDictIdent :: Ts.PkgMap -> Ts.QClassName -> PC.Ty -> Text
47+
instanceDictIdent pkgMap className ty =
4848
Lens.view (dict . Lens.to (PrettyPrinter.Text.renderStrict . layoutPretty defaultLayoutOptions)) $
49-
printInstanceDict className ty
49+
printInstanceDict pkgMap className ty
5050

51-
lvEqBuiltins :: LV.PrintRead Builtin
52-
lvEqBuiltins = LV.MkPrintRead $ \(tys, refName) ->
51+
lvEqBuiltins :: Ts.PkgMap -> LV.PrintRead Builtin
52+
lvEqBuiltins pkgMap = LV.MkPrintRead $ \(tys, refName) ->
5353
case (refName, tys) of
5454
("eq", [ty]) -> do
5555
ty' <- lamTy2PCTy ty
5656
return $
5757
OverloadedBuiltin
58-
(Ts.primValName $ instanceDictIdent tsEqClass ty')
58+
(Ts.primValName $ instanceDictIdent pkgMap tsEqClass ty')
5959
0 -- index in the list of substitutions for the type we're overloading on
6060
".eq"
6161
("true", _) -> Just $ Builtin $ Ts.primValName "true"
@@ -73,12 +73,12 @@ neqClassMethodName = Ts.MkValueName "neq"
7373
tsEqClass :: Ts.QClassName
7474
tsEqClass = (Ts.MkPackageName "lbr-prelude", Ts.MkModuleName "LbrPrelude", Ts.MkClassName "Eq")
7575

76-
printDeriveEq :: PC.ModuleName -> PC.TyDefs -> (Doc ann -> Doc ann) -> PC.Ty -> Either P.InternalError (Doc ann, Set Ts.QValName)
77-
printDeriveEq mn iTyDefs mkExportInstanceDeclDoc ty = do
76+
printDeriveEq :: Ts.PkgMap -> PC.ModuleName -> PC.TyDefs -> (Doc ann -> Doc ann) -> PC.Ty -> Either P.InternalError (Doc ann, Set Ts.QValName)
77+
printDeriveEq pkgMap mn iTyDefs mkExportInstanceDeclDoc ty = do
7878
eqValE <- deriveEqImpl mn iTyDefs ty
7979
neqValE <- deriveNeqImpl mn iTyDefs ty
80-
(eqImplDoc, eqImports) <- LV.runPrint lvEqBuiltins (printValueE eqValE)
81-
(neqImplDoc, neqImports) <- LV.runPrint lvEqBuiltins (printValueE neqValE)
80+
(eqImplDoc, eqImports) <- LV.runPrint (lvEqBuiltins pkgMap) (printValueE eqValE)
81+
(neqImplDoc, neqImports) <- LV.runPrint (lvEqBuiltins pkgMap) (printValueE neqValE)
8282
let eqValueDefDoc =
8383
align $
8484
vsep
@@ -90,21 +90,21 @@ printDeriveEq mn iTyDefs mkExportInstanceDeclDoc ty = do
9090
, Set.map (Lens.view qualifiedValName) $ eqImports <> neqImports
9191
)
9292

93-
lvPlutusDataBuiltins :: LV.PrintRead Builtin
94-
lvPlutusDataBuiltins = LV.MkPrintRead $ \(tys, refName) ->
93+
lvPlutusDataBuiltins :: Ts.PkgMap -> LV.PrintRead Builtin
94+
lvPlutusDataBuiltins pkgMap = LV.MkPrintRead $ \(tys, refName) ->
9595
case (refName, tys) of
9696
("toPlutusData", [ty]) -> do
9797
ty' <- lamTy2PCTy ty
9898
return $
9999
OverloadedBuiltin
100-
(Ts.primValName $ instanceDictIdent tsIsPlutusDataClass ty')
100+
(Ts.primValName $ instanceDictIdent pkgMap tsIsPlutusDataClass ty')
101101
0 -- index in the list of substitutions for the type we're overloading on
102102
".toData"
103103
("fromPlutusData", [ty]) -> do
104104
ty' <- lamTy2PCTy ty
105105
return $
106106
OverloadedBuiltin
107-
(Ts.primValName $ instanceDictIdent tsIsPlutusDataClass ty')
107+
(Ts.primValName $ instanceDictIdent pkgMap tsIsPlutusDataClass ty')
108108
0 -- index in the list of substitutions for the type we're overloading on
109109
".fromData"
110110
("casePlutusData", _) -> Just $ Builtin $ Ts.normalValName "lbr-plutus/Runtime.js" "LbrPlutusRuntime" "casePlutusData"
@@ -125,13 +125,13 @@ fromPlutusDataClassMethodName = Ts.MkValueName "fromData"
125125
tsIsPlutusDataClass :: Ts.QClassName
126126
tsIsPlutusDataClass = (Ts.MkPackageName "lbr-plutus/PlutusData.js", Ts.MkModuleName "LbrPlutusPD", Ts.MkClassName "IsPlutusData")
127127

128-
printDeriveIsPlutusData :: PC.ModuleName -> PC.TyDefs -> (Doc ann -> Doc ann) -> PC.Ty -> Either P.InternalError (Doc ann, Set Ts.QValName)
129-
printDeriveIsPlutusData mn iTyDefs mkExportInstanceDeclDoc ty = do
128+
printDeriveIsPlutusData :: Ts.PkgMap -> PC.ModuleName -> PC.TyDefs -> (Doc ann -> Doc ann) -> PC.Ty -> Either P.InternalError (Doc ann, Set Ts.QValName)
129+
printDeriveIsPlutusData pkgMap mn iTyDefs mkExportInstanceDeclDoc ty = do
130130
toPlutusDataValE <- deriveToPlutusDataImpl mn iTyDefs ty
131131
fromPlutusDataValE <- deriveFromPlutusDataImpl mn iTyDefs ty
132132

133-
(toDataImplDoc, toDataImports) <- LV.runPrint lvPlutusDataBuiltins (printValueE toPlutusDataValE)
134-
(fromDataImplDoc, fromDataImports) <- LV.runPrint lvPlutusDataBuiltins (printValueE fromPlutusDataValE)
133+
(toDataImplDoc, toDataImports) <- LV.runPrint (lvPlutusDataBuiltins pkgMap) (printValueE toPlutusDataValE)
134+
(fromDataImplDoc, fromDataImports) <- LV.runPrint (lvPlutusDataBuiltins pkgMap) (printValueE fromPlutusDataValE)
135135

136136
let valueDefDoc =
137137
align $
@@ -148,21 +148,21 @@ tsJsonClass :: Ts.QClassName
148148
tsJsonClass = (Ts.MkPackageName "lbr-prelude", Ts.MkModuleName "LbrPrelude", Ts.MkClassName "Json")
149149

150150
-- | LambdaBuffers.Codegen.LamVal.Json specification printing
151-
lvJsonBuiltins :: LV.PrintRead Builtin
152-
lvJsonBuiltins = LV.MkPrintRead $ \(tys, refName) ->
151+
lvJsonBuiltins :: Ts.PkgMap -> LV.PrintRead Builtin
152+
lvJsonBuiltins pkgMap = LV.MkPrintRead $ \(tys, refName) ->
153153
case (refName, tys) of
154154
("toJson", [ty]) -> do
155155
ty' <- lamTy2PCTy ty
156156
return $
157157
OverloadedBuiltin
158-
(Ts.primValName $ instanceDictIdent tsJsonClass ty')
158+
(Ts.primValName $ instanceDictIdent pkgMap tsJsonClass ty')
159159
0 -- index in the list of substitutions for the type we're overloading on
160160
".toJson"
161161
("fromJson", [ty]) -> do
162162
ty' <- lamTy2PCTy ty
163163
return $
164164
OverloadedBuiltin
165-
(Ts.primValName (instanceDictIdent tsJsonClass ty'))
165+
(Ts.primValName (instanceDictIdent pkgMap tsJsonClass ty'))
166166
0 -- index in the list of substitutions for the type we're overloading on
167167
".fromJson"
168168
("jsonObject", _) -> return $ Builtin $ Ts.normalValName "lbr-prelude" "LbrPrelude" "jsonObject"
@@ -176,7 +176,7 @@ lvJsonBuiltins = LV.MkPrintRead $ \(tys, refName) ->
176176
"lbr-prelude"
177177
"LbrPrelude"
178178
( PrettyPrinter.Text.renderStrict . layoutPretty defaultLayoutOptions $
179-
"caseJsonConstructor" <> surround (Typescript.Print.Ty.printTyInner ty') langle rangle
179+
"caseJsonConstructor" <> surround (Typescript.Print.Ty.printTyInner pkgMap ty') langle rangle
180180
)
181181
("caseJsonArray", _) -> return $ Builtin $ Ts.normalValName "lbr-prelude" "LbrPrelude" "caseJsonArray"
182182
("caseJsonObject", _) -> return $ Builtin $ Ts.normalValName "lbr-prelude" "LbrPrelude" "caseJsonObject"
@@ -192,12 +192,12 @@ toJsonClassMethodName = Ts.MkValueName "toJson"
192192
fromJsonClassMethodName :: Ts.ValueName
193193
fromJsonClassMethodName = Ts.MkValueName "fromJson"
194194

195-
printDeriveJson :: PC.ModuleName -> PC.TyDefs -> (Doc ann -> Doc ann) -> PC.Ty -> Either P.InternalError (Doc ann, Set Ts.QValName)
196-
printDeriveJson mn iTyDefs mkExportInstanceDeclDoc ty = do
195+
printDeriveJson :: Ts.PkgMap -> PC.ModuleName -> PC.TyDefs -> (Doc ann -> Doc ann) -> PC.Ty -> Either P.InternalError (Doc ann, Set Ts.QValName)
196+
printDeriveJson pkgMap mn iTyDefs mkExportInstanceDeclDoc ty = do
197197
toJsonValE <- deriveToJsonImpl mn iTyDefs ty
198-
(toJsonImplDoc, impsA) <- LV.runPrint lvJsonBuiltins (printValueE toJsonValE)
198+
(toJsonImplDoc, impsA) <- LV.runPrint (lvJsonBuiltins pkgMap) (printValueE toJsonValE)
199199
fromJsonValE <- deriveFromJsonImpl mn iTyDefs ty
200-
(fromJsonImplDoc, impsB) <- LV.runPrint lvJsonBuiltins (printValueE fromJsonValE)
200+
(fromJsonImplDoc, impsB) <- LV.runPrint (lvJsonBuiltins pkgMap) (printValueE fromJsonValE)
201201

202202
let valueDefDoc =
203203
align $

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Typescript/Print/InstanceDef.hs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ $(Lens.TH.makeLenses ''InstanceDict)
7979
given in 4.3.2 of
8080
https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-630004.1
8181
-}
82-
printInstanceDict :: forall ann. Ts.QClassName -> PC.Ty -> InstanceDict (Doc ann)
83-
printInstanceDict qcn ty =
82+
printInstanceDict :: forall ann. Ts.PkgMap -> Ts.QClassName -> PC.Ty -> InstanceDict (Doc ann)
83+
printInstanceDict pkgMap qcn ty =
8484
let go :: PC.Ty -> InstanceDict (Doc ann)
8585
go (PC.TyVarI PC.TyVar {PC.varName = varName}) =
8686
ArgumentInstanceDict $ "dict" <> printVarName varName
@@ -90,7 +90,7 @@ printInstanceDict qcn ty =
9090
tyRefDoc = case tyRef of
9191
PC.LocalI PC.LocalRef {PC.tyName = tyName} -> pretty $ Lens.view #name tyName
9292
PC.ForeignI foreignRef ->
93-
printTsQTyNameKey (Ts.fromLbForeignRef foreignRef)
93+
printTsQTyNameKey (Ts.fromLbForeignRef pkgMap foreignRef)
9494
in TopLevelInstanceDict
9595
(mconcat [qClassNameDoc, brackets tyRefDoc])
9696
qClassNameDoc
@@ -99,10 +99,10 @@ printInstanceDict qcn ty =
9999

100100
-- Prints e.g.
101101
-- Eq<$a>
102-
printInstanceType :: Ts.QClassName -> PC.Ty -> Doc ann
103-
printInstanceType qcn ty =
102+
printInstanceType :: Ts.PkgMap -> Ts.QClassName -> PC.Ty -> Doc ann
103+
printInstanceType pkgMap qcn ty =
104104
let crefDoc = printTsQClassName qcn
105-
tyDoc = printTyInner ty
105+
tyDoc = printTyInner pkgMap ty
106106
in crefDoc <> surround tyDoc langle rangle
107107

108108
{- | Prints an instance context argument e.g.
@@ -112,19 +112,19 @@ printInstanceType qcn ty =
112112
113113
dict$a : Eq<$a>
114114
-}
115-
printInstanceContextArg :: Ts.QClassName -> PC.Ty -> Doc ann
116-
printInstanceContextArg qcn ty =
117-
Lens.view dict (printInstanceDict qcn ty)
115+
printInstanceContextArg :: Ts.PkgMap -> Ts.QClassName -> PC.Ty -> Doc ann
116+
printInstanceContextArg pkgMap qcn ty =
117+
Lens.view dict (printInstanceDict pkgMap qcn ty)
118118
<+> ":"
119-
<+> printInstanceType qcn ty
119+
<+> printInstanceType pkgMap qcn ty
120120

121121
-- Prints e.g.
122122
-- > <$a,$b>(dict$a : Eq<$a>, dict$b : Eq<$b>)
123-
printInstanceContext :: Ts.QClassName -> [PC.Ty] -> Doc ann
124-
printInstanceContext hsQClassName tys =
123+
printInstanceContext :: Ts.PkgMap -> Ts.QClassName -> [PC.Ty] -> Doc ann
124+
printInstanceContext pkgMap hsQClassName tys =
125125
align . group $
126-
encloseSep langle rangle comma (map printTyInner tys)
127-
<> encloseSep lparen rparen comma (printInstanceContextArg hsQClassName <$> tys)
126+
encloseSep langle rangle comma (map (printTyInner pkgMap) tys)
127+
<> encloseSep lparen rparen comma (printInstanceContextArg pkgMap hsQClassName <$> tys)
128128

129129
collectInstanceDeclTypeVars :: PC.Ty -> [PC.Ty]
130130
collectInstanceDeclTypeVars = collectTyVars
@@ -141,12 +141,12 @@ collectVars' collected (PC.TyAppI (PC.TyApp _ args _)) = collected `Set.union` (
141141
collectVars' collected _ = collected
142142

143143
-- See the INVARIANT note below
144-
printExportInstanceDecl :: MonadPrint m => Ts.QClassName -> PC.Ty -> m (Doc ann -> Doc ann)
145-
printExportInstanceDecl tsQClassName ty = do
144+
printExportInstanceDecl :: MonadPrint m => Ts.PkgMap -> Ts.QClassName -> PC.Ty -> m (Doc ann -> Doc ann)
145+
printExportInstanceDecl pkgMap tsQClassName ty = do
146146
let
147-
instanceType = printInstanceType tsQClassName ty
147+
instanceType = printInstanceType pkgMap tsQClassName ty
148148

149-
lhsInstanceDecl = printInstanceDict tsQClassName ty
149+
lhsInstanceDecl = printInstanceDict pkgMap tsQClassName ty
150150

151151
instanceDeclTypeVars = collectInstanceDeclTypeVars ty
152152

@@ -192,15 +192,16 @@ printExportInstanceDecl tsQClassName ty = do
192192
, lbrace
193193
, indent 2 $
194194
vsep
195-
[ "export" <+> "interface" <+> (printTsUnqualifiedQClassName tsQClassName <> "Instances")
195+
[ -- TODO(jaredponn): typeclasses probably aren't supported..
196+
"export" <+> "interface" <+> (printTsUnqualifiedQClassName tsQClassName <> "Instances")
196197
, lbrace
197198
, indent 2 $
198199
vsep
199200
[ brackets tyDoc
200201
<+> colon
201202
<+> case instanceDeclTypeVars of
202203
[] -> instanceType
203-
_ -> printInstanceContext tsQClassName instanceDeclTypeVars <+> "=>" <+> instanceType
204+
_ -> printInstanceContext pkgMap tsQClassName instanceDeclTypeVars <+> "=>" <+> instanceType
204205
]
205206
, rbrace
206207
]
@@ -213,7 +214,7 @@ printExportInstanceDecl tsQClassName ty = do
213214
[] -> dictDoc <+> equals <+> bodyDoc
214215
_ ->
215216
vsep
216-
[ dictDoc <+> equals <+> "function" <> printInstanceContext tsQClassName instanceDeclTypeVars <+> colon <+> instanceType
217+
[ dictDoc <+> equals <+> "function" <> printInstanceContext pkgMap tsQClassName instanceDeclTypeVars <+> colon <+> instanceType
217218
, indent 2 $
218219
vsep
219220
[ lbrace

0 commit comments

Comments
 (0)