Skip to content

Commit f3d5168

Browse files
authored
Merge pull request #48 from mlabs-haskell/bladyjoker/proto-compat-rework
Reworking the Compiler ProtoCompat (big one)
2 parents 60fe2d1 + 54100bd commit f3d5168

File tree

22 files changed

+1871
-1101
lines changed

22 files changed

+1871
-1101
lines changed

lambda-buffers-compiler/app/LambdaBuffers/Compiler/Cli/Compile.hs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +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.KindCheck (check)
11-
import LambdaBuffers.Compiler.ProtoCompat (
12-
FromProtoErr (NamingError, ProtoError),
13-
IsMessage (fromProto, toProto),
14-
)
15-
import LambdaBuffers.Compiler.ProtoCompat.Types qualified as ProtoCompat
16-
import Proto.Compiler as ProtoLib (CompilerInput, CompilerOutput)
11+
import LambdaBuffers.Compiler (runCompiler)
12+
import Proto.Compiler (CompilerError, CompilerInput, CompilerOutput)
13+
import Proto.Compiler_Fields (compilerError, compilerResult)
1714
import System.FilePath.Lens (extension)
1815

1916
data CompileOpts = CompileOpts
@@ -24,20 +21,19 @@ data CompileOpts = CompileOpts
2421

2522
makeLenses ''CompileOpts
2623

27-
-- NOTE(cstml) - let's use Katip instead of print.
24+
-- NOTE(cstml): Let's use Katip instead of print.
2825

2926
-- | Compile LambdaBuffers modules
3027
compile :: CompileOpts -> IO ()
3128
compile opts = do
32-
compIn <- readCompilerInput (opts ^. input)
33-
case fromProto @CompilerInput @ProtoCompat.CompilerInput compIn of
34-
Left err -> case err of
35-
NamingError ne -> print $ "Encountered a naming error " <> show ne
36-
ProtoError pe -> print $ "Encountered a proto error " <> show pe
37-
Right compIn' -> do
38-
print @String "Successfully processed the CompilerInput"
39-
let result = check compIn'
40-
writeCompilerOutput (opts ^. output) (toProto result)
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)
4137
return ()
4238

4339
readCompilerInput :: FilePath -> IO CompilerInput
@@ -52,7 +48,10 @@ readCompilerInput fp = do
5248
return $ PbText.readMessageOrDie content
5349
_ -> error $ "Unknown CompilerInput format " <> ext
5450

55-
writeCompilerOutput :: FilePath -> ProtoLib.CompilerOutput -> IO ()
51+
writeCompilerError :: FilePath -> CompilerError -> IO ()
52+
writeCompilerError fp err = writeCompilerOutput fp (defMessage & compilerError .~ err)
53+
54+
writeCompilerOutput :: FilePath -> CompilerOutput -> IO ()
5655
writeCompilerOutput fp cr = do
5756
let ext = fp ^. extension
5857
case ext of

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ library
107107
, text >=1.2
108108

109109
exposed-modules:
110+
LambdaBuffers.Compiler
110111
LambdaBuffers.Compiler.KindCheck
111112
LambdaBuffers.Compiler.KindCheck.Context
112113
LambdaBuffers.Compiler.KindCheck.Derivation
@@ -117,6 +118,7 @@ library
117118
LambdaBuffers.Compiler.KindCheck.Variable
118119
LambdaBuffers.Compiler.NamingCheck
119120
LambdaBuffers.Compiler.ProtoCompat
121+
LambdaBuffers.Compiler.ProtoCompat.FromProto
120122
LambdaBuffers.Compiler.ProtoCompat.Types
121123
LambdaBuffers.Compiler.TypeClass.Pat
122124
LambdaBuffers.Compiler.TypeClass.Pretty
@@ -126,7 +128,7 @@ library
126128

127129
hs-source-dirs: src
128130

129-
-- note(cstml): should we name this something shorter? lb-cli?
131+
-- NOTE(cstml): should we name this something shorter? lb-cli?
130132
executable lambda-buffers-compiler-cli
131133
import: common-language
132134
import: common-imports
@@ -149,8 +151,10 @@ test-suite tests
149151
hs-source-dirs: test
150152
main-is: Test.hs
151153
build-depends:
154+
, containers >=0.6
152155
, lambda-buffers-compiler
153156
, lambda-buffers-compiler-pb >=0.1
157+
, mtl >=2.2
154158
, proto-lens >=0.7
155159
, QuickCheck >=2.14
156160
, tasty >=1.4
@@ -160,9 +164,10 @@ test-suite tests
160164

161165
other-modules:
162166
Test.KindCheck
167+
Test.LambdaBuffers.Compiler
168+
Test.LambdaBuffers.Compiler.Gen
163169
Test.TypeClassCheck
164170
Test.Utils.CompilerInput
165171
Test.Utils.Constructors
166172
Test.Utils.Module
167-
Test.Utils.SourceInfo
168173
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)