Skip to content

Commit 419b807

Browse files
committed
Separate repl into separate project, fix REPL docs output
1 parent c0c1861 commit 419b807

File tree

17 files changed

+98
-54
lines changed

17 files changed

+98
-54
lines changed

docs/builtins/General/enforce.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Use the following arguments to specify the test expression and error message for
1818

1919
| Argument | Type | Description |
2020
|----------|--------|------------------------------------------------|
21-
| `expression` | bool | Specifies the expression to evaluate. |
21+
| expression | bool | Specifies the expression to evaluate. |
2222
| `message` | string | Specifies the error message to display if the `expression` evaluates as false. |
2323

2424
### Return values
@@ -34,7 +34,7 @@ pact> (enforce (= (+ 2 2) 4) "All is well")
3434
true
3535
```
3636

37-
Because the specified expression (`2 + 2 = 4`) is true, the function returns true and the transaction continues.
37+
Because the specified expression (`2 + 2 = 4`) is true, the function returns true and the transaction continues.
3838

3939
The following example demonstrates how to use the `enforce` function to evaluate the expression `(2 + 2) != 4`:
4040

File renamed without changes.
File renamed without changes.

pact/Pact/Core/Repl/BuiltinDocs/Internal.hs renamed to pact-repl/Pact/Core/Repl/BuiltinDocs/Internal.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import Language.Haskell.TH.Syntax
99
import System.Directory
1010
import System.FilePath
1111

12+
import Data.Default
1213
import Control.Monad
14+
import Control.Monad.IO.Class
1315
import qualified Data.Text.IO as T
1416
import qualified Data.Text as T
1517
import qualified Data.Map.Strict as M
1618
import Data.Functor
19+
import Text.Pandoc hiding (runIO)
1720

1821
listBuiltinDocs :: IO [FilePath]
1922
listBuiltinDocs = do
@@ -41,10 +44,20 @@ mkBuiltinDocs = embedIO action
4144
files <- listBuiltinDocs
4245
cnt <- forM files $ \f -> do
4346
let bname = takeBaseName f
44-
content <- T.readFile f
47+
content <- runIOorExplode $
48+
writeANSI writerOpts =<< readMarkdown readerOpts =<< liftIO (T.readFile f)
4549
pure (normalizedNameToBuiltin $ T.pack bname, MarkdownDoc content)
4650
pure $ M.fromList cnt
4751

52+
readerOpts :: ReaderOptions
53+
readerOpts = def
54+
{ readerExtensions = pandocExtensions
55+
}
56+
57+
writerOpts :: WriterOptions
58+
writerOpts = def
59+
{ writerExtensions = pandocExtensions
60+
}
4861

4962
builtinToNormalizedName :: T.Text -> T.Text
5063
builtinToNormalizedName = \case
File renamed without changes.

pact/Pact/Core/Repl/Utils.hs renamed to pact-repl/Pact/Core/Repl/Utils.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
{-# LANGUAGE TypeFamilies #-}
1010
{-# LANGUAGE RankNTypes #-}
1111
{-# LANGUAGE DataKinds #-}
12+
{-# OPTIONS_GHC -Wno-orphans #-}
1213

1314

1415
module Pact.Core.Repl.Utils
@@ -68,6 +69,7 @@ import Pact.Core.Environment
6869
import Pact.Core.Type
6970
import Pact.Core.Builtin
7071
import Pact.Core.PactValue
72+
import Pact.Core.Debug
7173
import qualified Pact.Core.IR.Term as Term
7274

7375
import System.Console.Haskeline.Completion
@@ -246,3 +248,24 @@ replPrintLn' :: Text -> EvalM 'ReplRuntime b SpanInfo ()
246248
replPrintLn' p = do
247249
r <- getReplState
248250
_replOutputLine r p
251+
252+
-- This orphan instance allows us to separate
253+
-- the repl declaration out, as ugly as it is
254+
instance DebugPrintable 'ReplRuntime (ReplBuiltin CoreBuiltin) where
255+
debugPrint dp term =
256+
ask >>= \case
257+
ReplEnv _ -> do
258+
case dp of
259+
DPLexer -> whenReplFlagSet ReplDebugLexer $ liftIO $ do
260+
putStrLn "----------- Lexer output -----------------"
261+
print (pretty term)
262+
DPParser -> whenReplFlagSet ReplDebugParser $
263+
liftIO $ do
264+
putStrLn "----------- Parser output ----------------"
265+
print (pretty term)
266+
DPDesugar -> whenReplFlagSet ReplDebugDesugar $ case term of
267+
Term.TLTerm t ->
268+
liftIO $ do
269+
putStrLn "----------- Desugar output ---------------"
270+
print (pretty t)
271+
_ -> pure ()

pact-tng.cabal

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ library
246246
Pact.Core.Evaluate
247247
Pact.Core.Scheme
248248
Pact.Core.SPV
249-
Pact.Core.Repl
250249
Pact.Core.SizeOf
251250
Pact.Core.SizeOf.Deriving
252251
Pact.Core.StackFrame
@@ -267,11 +266,6 @@ library
267266
Pact.Core.IR.Term
268267
Pact.Core.IR.Desugar
269268

270-
Pact.Core.IR.Eval.Direct.Evaluator
271-
Pact.Core.IR.Eval.Direct.CoreBuiltin
272-
Pact.Core.IR.Eval.Direct.ReplBuiltin
273-
Pact.Core.IR.Eval.Direct.Types
274-
275269
-- Core IR Evaluator modules
276270
Pact.Core.IR.Eval.Runtime
277271
Pact.Core.IR.Eval.Runtime.Types
@@ -287,12 +281,13 @@ library
287281
Pact.Core.Version
288282

289283
-- Repl
290-
Pact.Core.Repl.Utils
291-
Pact.Core.Repl.Runtime.ReplBuiltin
292-
Pact.Core.Repl.Compile
293-
Pact.Core.Repl.BuiltinDocs
294-
Pact.Core.Repl.BuiltinDocs.Internal
295-
Pact.Core.Repl.UserDocs
284+
-- Pact.Core.Repl
285+
-- Pact.Core.Repl.Utils
286+
-- Pact.Core.Repl.Runtime.ReplBuiltin
287+
-- Pact.Core.Repl.Compile
288+
-- Pact.Core.Repl.BuiltinDocs
289+
-- Pact.Core.Repl.BuiltinDocs.Internal
290+
-- Pact.Core.Repl.UserDocs
296291

297292
-- Serialization
298293
Pact.Core.Serialise
@@ -311,6 +306,30 @@ library
311306

312307
extra-libraries: mpfr
313308

309+
library pact-repl
310+
import: pact-common
311+
hs-source-dirs: pact-repl
312+
313+
exposed-modules:
314+
Pact.Core.Repl
315+
Pact.Core.Repl.Utils
316+
Pact.Core.Repl.Runtime.ReplBuiltin
317+
Pact.Core.Repl.Compile
318+
Pact.Core.Repl.BuiltinDocs
319+
Pact.Core.Repl.BuiltinDocs.Internal
320+
Pact.Core.Repl.UserDocs
321+
322+
Pact.Core.IR.Eval.Direct.Evaluator
323+
Pact.Core.IR.Eval.Direct.CoreBuiltin
324+
Pact.Core.IR.Eval.Direct.ReplBuiltin
325+
Pact.Core.IR.Eval.Direct.Types
326+
327+
build-depends:
328+
, pact-tng
329+
, pact-tng:pact-crypto
330+
, filepath
331+
, pandoc
332+
314333
library pact-lsp
315334
import: pact-common
316335
hs-source-dirs: pact-lsp
@@ -322,6 +341,7 @@ library pact-lsp
322341

323342
build-depends:
324343
, pact-tng
344+
, pact-tng:pact-repl
325345
, lsp
326346
, lsp-types
327347
, filepath
@@ -369,10 +389,11 @@ executable profile-tx
369389

370390
executable pact
371391
import: pact-common
372-
main-is: repl/Main.hs
392+
main-is: pact-repl/Main.hs
373393

374394
build-depends: base
375395
, pact-tng
396+
, pact-tng:pact-repl
376397
, pact-tng:pact-lsp
377398
, optparse-applicative
378399
, pact-tng:pact-request-api
@@ -523,6 +544,7 @@ test-suite core-tests
523544
, semirings
524545
, neat-interpolation
525546
, pact-tng:pact-request-api
547+
, pact-tng:pact-repl
526548
, pact-tng:pact-lsp
527549
, pact-tng:test-utils
528550
, pact-tng:unsafe

pact/Pact/Core/Compile.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ evalModuleGovernance interpreter tl = do
153153

154154
compileDesugarOnly
155155
:: forall e b i
156-
. (HasCompileEnv b i)
156+
. (HasCompileEnv b i, DebugPrintable e b)
157157
=> Interpreter e b i
158158
-> Lisp.TopLevel i
159159
-> EvalM e b i (EvalTopLevel b i, S.Set ModuleName)
@@ -169,7 +169,7 @@ compileDesugarOnly interpreter tl = do
169169

170170
interpretTopLevel
171171
:: forall e b i
172-
. (HasCompileEnv b i)
172+
. (HasCompileEnv b i, DebugPrintable e b)
173173
=> Interpreter e b i
174174
-> RawCode
175175
-> Lisp.TopLevel i

pact/Pact/Core/Debug.hs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
{-# LANGUAGE GADTs #-}
22
{-# LANGUAGE MultiParamTypeClasses #-}
33
{-# LANGUAGE CPP #-}
4+
{-# LANGUAGE KindSignatures #-}
5+
{-# LANGUAGE DataKinds #-}
6+
{-# LANGUAGE FunctionalDependencies #-}
47

58
module Pact.Core.Debug
69
( DebugFlag(..)
710
, DebugPrint(..)
8-
, debugPrint
11+
, DebugPrintable(..)
912
) where
1013

11-
import Control.Monad.Reader
1214
import Pact.Core.Type
1315
import Pact.Core.Names
1416
import Pact.Core.Syntax.LexUtils(PosToken)
1517
import Pact.Core.Environment
1618
import qualified Pact.Core.Syntax.ParseTree as Syntax
1719
import qualified Pact.Core.IR.Term as Term
18-
import Pact.Core.Repl.Utils
19-
import Pact.Core.Pretty
20+
import Pact.Core.Builtin (CoreBuiltin)
2021

2122
data DebugPrint b i term where
2223
DPLexer :: DebugPrint b i [PosToken]
@@ -29,23 +30,8 @@ data DebugFlag
2930
| DFDesugar
3031
deriving (Show, Eq, Ord, Enum, Bounded)
3132

32-
debugPrint :: (Pretty b) => DebugPrint b i term -> term -> EvalM e b i ()
33-
debugPrint dp term =
34-
ask >>= \case
35-
ExecEnv _ -> pure ()
36-
ReplEnv _ -> do
37-
case dp of
38-
DPLexer -> whenReplFlagSet ReplDebugLexer $ liftIO $ do
39-
putStrLn "----------- Lexer output -----------------"
40-
print (pretty term)
41-
DPParser -> whenReplFlagSet ReplDebugParser $
42-
liftIO $ do
43-
putStrLn "----------- Parser output ----------------"
44-
print (pretty term)
45-
DPDesugar -> whenReplFlagSet ReplDebugDesugar $ case term of
46-
Term.TLTerm t ->
47-
liftIO $ do
48-
putStrLn "----------- Desugar output ---------------"
49-
print (pretty t)
50-
_ -> pure ()
33+
class DebugPrintable (e :: RuntimeMode) b | e -> b where
34+
debugPrint :: DebugPrint b i term -> term -> EvalM e b i ()
5135

36+
instance DebugPrintable 'ExecRuntime CoreBuiltin where
37+
debugPrint _ _ = pure ()

pact/Pact/Core/Evaluate.hs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Pact.Core.Evaluate
2323
, Eval
2424
, EvalBuiltinEnv
2525
, allModuleExports
26-
, evalDirectInterpreter
26+
-- , evalDirectInterpreter
2727
, evalInterpreter
2828
, EvalInput
2929
, EnableGasLogs(..)
@@ -64,8 +64,8 @@ import Pact.Core.Info
6464
import Pact.Core.Signer
6565
import Pact.Core.IR.Eval.CEK.CoreBuiltin
6666
import qualified Pact.Core.IR.Eval.CEK.Evaluator as CEK
67-
import qualified Pact.Core.IR.Eval.Direct.Evaluator as Direct
68-
import qualified Pact.Core.IR.Eval.Direct.CoreBuiltin as Direct
67+
-- import qualified Pact.Core.IR.Eval.Direct.Evaluator as Direct
68+
-- import qualified Pact.Core.IR.Eval.Direct.CoreBuiltin as Direct
6969
import qualified Pact.Core.Syntax.Lexer as Lisp
7070
import qualified Pact.Core.Syntax.Parser as Lisp
7171
import qualified Pact.Core.Syntax.ParseTree as Lisp
@@ -118,16 +118,16 @@ evalInterpreter =
118118
cekEnv :: CEK.BuiltinEnv ExecRuntime CoreBuiltin Info
119119
cekEnv = coreBuiltinEnv @ExecRuntime
120120

121-
evalDirectInterpreter :: Interpreter ExecRuntime CoreBuiltin Info
122-
evalDirectInterpreter =
123-
Interpreter runGuard runTerm resume evalWithCap
124-
where
125-
runTerm purity term = Direct.eval purity env term
126-
runGuard info g = Direct.interpretGuard info env g
127-
resume info defPact = Direct.evalResumePact info env defPact
128-
evalWithCap info purity ct term =
129-
Direct.evalWithinCap info purity env ct term
130-
env = Direct.coreBuiltinEnv
121+
-- evalDirectInterpreter :: Interpreter ExecRuntime CoreBuiltin Info
122+
-- evalDirectInterpreter =
123+
-- Interpreter runGuard runTerm resume evalWithCap
124+
-- where
125+
-- runTerm purity term = Direct.eval purity env term
126+
-- runGuard info g = Direct.interpretGuard info env g
127+
-- resume info defPact = Direct.evalResumePact info env defPact
128+
-- evalWithCap info purity ct term =
129+
-- Direct.evalWithinCap info purity env ct term
130+
-- env = Direct.coreBuiltinEnv
131131

132132
-- | Transaction-payload related environment data.
133133
data MsgData = MsgData

0 commit comments

Comments
 (0)