Skip to content

Commit bc7dcb3

Browse files
committed
Merge remote-tracking branch 'origin/main' into bladyjoker/compiler-class-check
2 parents e96f533 + b7c7f60 commit bc7dcb3

File tree

33 files changed

+3357
-1152
lines changed

33 files changed

+3357
-1152
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ nix profile install nixpkgs#direnv
102102
nix profile install nixpkgs#nix-direnv
103103
```
104104

105-
Your shell and editors should pick up on the `.envrc` files in different directories and prepare the environment accordingly.
106-
Use `direnv allow` to enable the direnv environment and `direnv reload` to reload it when necessary.
105+
Your shell and editors should pick up on the `.envrc` files in different
106+
directories and prepare the environment accordingly. Use `direnv allow` to
107+
enable the direnv environment and `direnv reload` to reload it when necessary.
107108

108109
Additionally, throughout the repository one can use:
109110

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
module LambdaBuffers.Compiler.Cli.Compile (CompileOpts (..), compile) where
22

3-
import Control.Lens (makeLenses)
3+
import Control.Lens (makeLenses, (&), (.~))
44
import Control.Lens.Getter ((^.))
55
import Data.ByteString qualified as BS
6+
import Data.ProtoLens (Message (defMessage))
67
import Data.ProtoLens qualified as Pb
78
import Data.ProtoLens.TextFormat qualified as PbText
89
import Data.Text.Lazy qualified as Text
910
import Data.Text.Lazy.IO qualified as Text
10-
import LambdaBuffers.Compiler.ProtoCompat (FromProtoErr (NamingError, ProtoError), IsMessage (fromProto, toProto))
11-
import LambdaBuffers.Compiler.ProtoCompat.Types qualified as ProtoCompat
12-
import Proto.Compiler (CompilerInput)
11+
import LambdaBuffers.Compiler (runCompiler)
12+
import Proto.Compiler (CompilerError, CompilerInput, CompilerOutput)
13+
import Proto.Compiler_Fields (compilerError, compilerResult)
1314
import System.FilePath.Lens (extension)
1415

1516
data CompileOpts = CompileOpts
@@ -20,18 +21,19 @@ data CompileOpts = CompileOpts
2021

2122
makeLenses ''CompileOpts
2223

24+
-- NOTE(cstml): Let's use Katip instead of print.
25+
2326
-- | Compile LambdaBuffers modules
2427
compile :: CompileOpts -> IO ()
2528
compile opts = do
26-
compIn <- readCompilerInput (opts ^. input)
27-
case fromProto @CompilerInput @ProtoCompat.CompilerInput compIn of
28-
Left err -> case err of
29-
NamingError ne -> print $ "Encountered a naming error " <> show ne
30-
ProtoError pe -> print $ "Encountered a proto error " <> show pe
31-
Right compIn' -> do
32-
print @String "Successfully processed the CompilerInput"
33-
writeCompilerOutput (opts ^. output) (toProto compIn')
34-
29+
compInp <- readCompilerInput (opts ^. input)
30+
case runCompiler compInp of
31+
Left compErr -> do
32+
putStrLn "Encountered errors during Compilation"
33+
writeCompilerError (opts ^. output) compErr
34+
Right compRes -> do
35+
putStrLn "Compilation succeeded"
36+
writeCompilerOutput (opts ^. output) (defMessage & compilerResult .~ compRes)
3537
return ()
3638

3739
readCompilerInput :: FilePath -> IO CompilerInput
@@ -46,11 +48,13 @@ readCompilerInput fp = do
4648
return $ PbText.readMessageOrDie content
4749
_ -> error $ "Unknown CompilerInput format " <> ext
4850

49-
-- FIXME(bladyjoker): Do this properly when you figure out what the CompilerOutput is.
50-
writeCompilerOutput :: FilePath -> CompilerInput -> IO ()
51-
writeCompilerOutput fp co = do
51+
writeCompilerError :: FilePath -> CompilerError -> IO ()
52+
writeCompilerError fp err = writeCompilerOutput fp (defMessage & compilerError .~ err)
53+
54+
writeCompilerOutput :: FilePath -> CompilerOutput -> IO ()
55+
writeCompilerOutput fp cr = do
5256
let ext = fp ^. extension
5357
case ext of
54-
".pb" -> BS.writeFile fp (Pb.encodeMessage co)
55-
".textproto" -> Text.writeFile fp (Text.pack . show $ PbText.pprintMessage co)
58+
".pb" -> BS.writeFile fp (Pb.encodeMessage cr)
59+
".textproto" -> Text.writeFile fp (Text.pack . show $ PbText.pprintMessage cr)
5660
_ -> error $ "Unknown CompilerOutput format " <> ext

lambda-buffers-compiler/app/Main.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import Options.Applicative (
2323
subparser,
2424
)
2525

26-
newtype Command
27-
= Compile CompileOpts
26+
newtype Command = Compile CompileOpts
2827

2928
inputPathP :: Parser FilePath
3029
inputPathP =

lambda-buffers-compiler/lambda-buffers-compiler.cabal

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ common common-language
3737
DeriveLift
3838
DeriveTraversable
3939
DerivingStrategies
40+
DerivingVia
4041
DoAndIfThenElse
42+
DuplicateRecordFields
4143
EmptyCase
4244
EmptyDataDecls
4345
EmptyDataDeriving
@@ -59,6 +61,7 @@ common common-language
5961
NamedFieldPuns
6062
NamedWildCards
6163
NumericUnderscores
64+
OverloadedLabels
6265
OverloadedStrings
6366
PartialTypeSignatures
6467
PatternGuards
@@ -92,29 +95,40 @@ library
9295
build-depends:
9396
, containers >=0.6.5.1
9497
, freer-simple >=1.2
98+
, generic-arbitrary
9599
, generic-lens >=2.2
96100
, lambda-buffers-compiler-pb >=0.1.0.0
97101
, mtl >=2.2
98102
, parsec >=3.1
99103
, prettyprinter >=1.7
100104
, proto-lens >=0.7
105+
, QuickCheck >=2.14
106+
, quickcheck-instances >=0.3
101107
, text >=1.2
102108

103109
exposed-modules:
104-
LambdaBuffers.Compiler.Canonical
110+
LambdaBuffers.Compiler
105111
LambdaBuffers.Compiler.KindCheck
106112
LambdaBuffers.Compiler.KindCheck.Context
113+
LambdaBuffers.Compiler.KindCheck.Derivation
107114
LambdaBuffers.Compiler.KindCheck.Inference
115+
LambdaBuffers.Compiler.KindCheck.Judgement
108116
LambdaBuffers.Compiler.KindCheck.Kind
109117
LambdaBuffers.Compiler.KindCheck.Type
110118
LambdaBuffers.Compiler.KindCheck.Variable
111119
LambdaBuffers.Compiler.NamingCheck
112120
LambdaBuffers.Compiler.ProtoCompat
121+
LambdaBuffers.Compiler.ProtoCompat.FromProto
113122
LambdaBuffers.Compiler.ProtoCompat.Types
123+
LambdaBuffers.Compiler.TypeClass.Pat
124+
LambdaBuffers.Compiler.TypeClass.Pretty
125+
LambdaBuffers.Compiler.TypeClass.Rules
126+
LambdaBuffers.Compiler.TypeClass.Solve
114127
LambdaBuffers.Compiler.TypeClassCheck
115128

116129
hs-source-dirs: src
117130

131+
-- NOTE(cstml): should we name this something shorter? lb-cli?
118132
executable lambda-buffers-compiler-cli
119133
import: common-language
120134
import: common-imports
@@ -137,13 +151,23 @@ test-suite tests
137151
hs-source-dirs: test
138152
main-is: Test.hs
139153
build-depends:
154+
, containers >=0.6
140155
, lambda-buffers-compiler
141156
, lambda-buffers-compiler-pb >=0.1
157+
, mtl >=2.2
142158
, proto-lens >=0.7
159+
, QuickCheck >=2.14
143160
, tasty >=1.4
144161
, tasty-hunit >=0.10
162+
, tasty-quickcheck >=0.10
145163
, text >=1.2
146164

147165
other-modules:
148166
Test.KindCheck
167+
Test.LambdaBuffers.Compiler
168+
Test.LambdaBuffers.Compiler.Gen
149169
Test.TypeClassCheck
170+
Test.Utils.CompilerInput
171+
Test.Utils.Constructors
172+
Test.Utils.Module
173+
Test.Utils.TyDef
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module LambdaBuffers.Compiler (runCompiler) where
2+
3+
import Data.ProtoLens (Message (defMessage))
4+
import LambdaBuffers.Compiler.KindCheck (check_)
5+
import LambdaBuffers.Compiler.ProtoCompat.FromProto (
6+
runFromProto,
7+
toProto,
8+
)
9+
import Proto.Compiler (CompilerError, CompilerInput, CompilerResult)
10+
11+
runCompiler :: CompilerInput -> Either CompilerError CompilerResult
12+
runCompiler compInp = do
13+
compInp' <- runFromProto compInp
14+
case check_ compInp' of
15+
Left err -> Left $ toProto err
16+
Right _ -> Right defMessage

0 commit comments

Comments
 (0)