Skip to content

Commit 858a4a5

Browse files
author
jared
committed
Fix code generator bugs
1 parent b33bfa0 commit 858a4a5

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ printExportInstanceDecl pkgMap tsQClassName ty = do
211211
dictDeclDoc =
212212
vsep
213213
[ case instanceDeclTypeVars of
214-
[] -> dictDoc <+> equals <+> bodyDoc
214+
[] -> dictDoc <+> equals <+> vsep [lbrace, indent 2 bodyDoc, rbrace]
215215
_ ->
216216
vsep
217217
[ dictDoc <+> equals <+> "function" <> printInstanceContext pkgMap tsQClassName instanceDeclTypeVars <+> colon <+> instanceType

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

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import Data.Text qualified as Text
1818
import Data.Traversable (for)
1919
import LambdaBuffers.Codegen.LamVal qualified as LV
2020
import LambdaBuffers.Codegen.LamVal.MonadPrint qualified as LV
21-
import LambdaBuffers.Codegen.Typescript.Print.Names (printCtorName, printFieldName, printMkCtor, printTsQValName)
21+
import LambdaBuffers.Codegen.Typescript.Print.Names (printCtorName, printFieldName, printTsQValName)
2222
import LambdaBuffers.Codegen.Typescript.Syntax qualified as Ts
2323
import LambdaBuffers.Compiler.LamTy qualified as LT
2424
import LambdaBuffers.ProtoCompat qualified as PC
25-
import Prettyprinter (Doc, Pretty (pretty), align, colon, comma, dot, dquotes, encloseSep, equals, group, hsep, indent, lbrace, lbracket, lparen, parens, rbrace, rbracket, rparen, space, vsep, (<+>))
25+
import Prettyprinter (Doc, Pretty (pretty), align, colon, comma, dot, dquotes, encloseSep, equals, group, indent, lbrace, lbracket, lparen, parens, rbrace, rbracket, rparen, vsep, (<+>))
2626
import Proto.Codegen_Fields qualified as P
2727

2828
{- | 'Builtin' is the result of a symbol table lookup for things like
@@ -252,11 +252,26 @@ printLetE ((_, _tyN), fields) prodVal letCont = do
252252
-- See
253253
-- 'LambdaBuffers.Codegen.Typescript.Print.Ty.printProd'
254254
-- for details.
255-
[singleField] -> ["let" <+> singleField <+> equals <+> letRHSDoc <> ".fields"]
255+
[singleField] ->
256+
[ "let" <+> singleField <+> equals <+> letRHSDoc
257+
-- WARN(jaredponn): recall that products are "raw
258+
-- tuples", so it's wrong to access `.field`
259+
-- <> ".fields"
260+
]
256261
_ ->
257262
zipWith
258263
( \i argDoc ->
259-
"let" <+> argDoc <+> equals <+> letRHSDoc <> ".fields" <> lbracket <> pretty (show i) <> rbracket
264+
"let"
265+
<+> argDoc
266+
<+> equals
267+
<+> letRHSDoc
268+
<>
269+
-- WARN(jaredponn): see above warning about
270+
-- these raw tuples.
271+
-- ".fields" <>
272+
lbracket
273+
<> pretty (show i)
274+
<> rbracket
260275
)
261276
[0 :: Int ..]
262277
argDocs
@@ -460,15 +475,45 @@ printRecordE ((_, tyN), _) vals = do
460475
Just fieldNDoc -> do
461476
valDoc <- printValueE val
462477
return $ group $ fieldNDoc <+> colon <+> valDoc
463-
let ctorDoc = printMkCtor (withInfo tyN)
464-
return $ ctorDoc <+> align (lbrace <+> encloseSep mempty mempty (comma <> space) fieldDocs <+> rbrace)
478+
-- Note(jaredponn): Again, we have no need for the constructor name because
479+
-- languages like Haskell have:
480+
-- @
481+
-- MyRecord { field1 = a, field2 = b }
482+
-- @
483+
-- whereas in TS we remove the constructor altogether i.e., we just have
484+
-- @
485+
-- { field1 = a, field2 = b }
486+
-- @
487+
-- so the following is unnecessary:
488+
-- > let ctorDoc = printMkCtor (withInfo tyN)
489+
return $ align (encloseSep lbrace rbrace comma fieldDocs)
465490

466491
printProductE :: MonadPrint m => LV.QProduct -> [LV.ValueE] -> m (Doc ann)
467-
printProductE ((_, tyN), _) vals = do
492+
printProductE ((_, _tyN), _) vals = do
493+
-- If there's a unique element in the fields,
494+
-- recall that it's not wrapped in a tuple
495+
-- list.
496+
-- See
497+
-- 'LambdaBuffers.Codegen.Typescript.Print.Ty.printProd'
498+
-- for details.
468499
fieldDocs <- for vals printValueE
469-
let ctorDoc = printMkCtor (withInfo tyN)
470-
return $ ctorDoc <+> align (hsep fieldDocs)
500+
-- Note(jaredponn): normally, we'd have to write down the constructor name
501+
-- e.g.
502+
-- @MyProd a b c@
503+
-- but we recall that TS products are literally anonymous tuples, so this
504+
-- would be just
505+
-- @[a,b,c]@
506+
--
507+
-- In other words, the following makes no sense at all:
508+
-- @
509+
-- let ctorDoc = printMkCtor (withInfo tyN)
510+
-- return $ ctorDoc <+> align (hsep fieldDocs)
511+
-- @
512+
case fieldDocs of
513+
[f] -> return f
514+
fs -> return (encloseSep lbracket rbracket comma fs)
471515

516+
-- We only use bigints and they are suffixed by a @n@
472517
printIntE :: MonadPrint m => Int -> m (Doc ann)
473518
printIntE i =
474519
return $ pretty i <> "n"

0 commit comments

Comments
 (0)