Skip to content

Commit

Permalink
Separate repl into separate project
Browse files Browse the repository at this point in the history
- Move tracing flags to repl sublibrary
- Fix repl builtins charging gas on the repl
  • Loading branch information
jmcardon committed Nov 4, 2024
1 parent c0c1861 commit bc0ba1b
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 90 deletions.
4 changes: 2 additions & 2 deletions docs/builtins/General/enforce.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Use the following arguments to specify the test expression and error message for

| Argument | Type | Description |
|----------|--------|------------------------------------------------|
| `expression` | bool | Specifies the expression to evaluate. |
| expression | bool | Specifies the expression to evaluate. |
| `message` | string | Specifies the error message to display if the `expression` evaluates as false. |

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

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

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

Expand Down
14 changes: 12 additions & 2 deletions gasmodel/Pact/Core/GasModel/ContractBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import Pact.Core.Gas
import Pact.Core.Namespace
import Pact.Core.Serialise
import Pact.Core.Persistence.MockPersistence
import qualified Pact.Core.IR.Eval.Direct.Evaluator as Direct
import qualified Pact.Core.IR.Eval.Direct.CoreBuiltin as Direct

import Pact.Core.Errors
import Pact.Core.Interpreter
Expand All @@ -59,7 +61,15 @@ interpretBigStep :: Interpreter ExecRuntime CoreBuiltin Info
interpretBigStep = evalInterpreter

interpretDirect :: Interpreter ExecRuntime CoreBuiltin Info
interpretDirect = evalDirectInterpreter
interpretDirect =
Interpreter runGuard runTerm resume evalWithCap
where
runTerm purity term = Direct.eval purity benv term
runGuard info g = Direct.interpretGuard info benv g
resume info defPact = Direct.evalResumePact info benv defPact
evalWithCap info purity ct term =
Direct.evalWithinCap info purity benv ct term
benv = Direct.coreBuiltinEnv


data CoinBenchSenders
Expand Down Expand Up @@ -179,7 +189,7 @@ setupCoinTxs pdb = do
putStrLn "Setting up the coin contract and the default funds"
source <- T.readFile (contractsPath </> "coin-v5-create.pact")
ee <- setupBenchEvalEnv pdb coinInitSigners coinInitData
() <$ runPactTxFromSource ee source evalDirectInterpreter
() <$ runPactTxFromSource ee source interpretDirect


_run :: IO ()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ applyLam
-> EvalM e b i (EvalValue e b i)
applyLam nclo@(N (NativeFn b env fn arity i)) args
| arity == argLen = do
chargeFlatNativeGas i b
when (builtinChargesGas b) $ chargeFlatNativeGas i b
fn i b env args
| argLen > arity = throwExecutionError i ClosureAppliedToTooManyArgs
| null args = return (VClosure nclo)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import Language.Haskell.TH.Syntax
import System.Directory
import System.FilePath

import Data.Default
import Control.Monad
import Control.Monad.IO.Class
import qualified Data.Text.IO as T
import qualified Data.Text as T
import qualified Data.Map.Strict as M
import Data.Functor
import Text.Pandoc hiding (runIO)

listBuiltinDocs :: IO [FilePath]
listBuiltinDocs = do
Expand Down Expand Up @@ -41,10 +44,20 @@ mkBuiltinDocs = embedIO action
files <- listBuiltinDocs
cnt <- forM files $ \f -> do
let bname = takeBaseName f
content <- T.readFile f
content <- runIOorExplode $
writeANSI writerOpts =<< readMarkdown readerOpts =<< liftIO (T.readFile f)
pure (normalizedNameToBuiltin $ T.pack bname, MarkdownDoc content)
pure $ M.fromList cnt

readerOpts :: ReaderOptions
readerOpts = def
{ readerExtensions = pandocExtensions
}

writerOpts :: WriterOptions
writerOpts = def
{ writerExtensions = pandocExtensions
}

builtinToNormalizedName :: T.Text -> T.Text
builtinToNormalizedName = \case
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions pact/Pact/Core/Repl/Utils.hs → pact-repl/Pact/Core/Repl/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE DataKinds #-}
{-# OPTIONS_GHC -Wno-orphans #-}


module Pact.Core.Repl.Utils
Expand Down Expand Up @@ -68,6 +69,7 @@ import Pact.Core.Environment
import Pact.Core.Type
import Pact.Core.Builtin
import Pact.Core.PactValue
import Pact.Core.Debug
import qualified Pact.Core.IR.Term as Term

import System.Console.Haskeline.Completion
Expand Down Expand Up @@ -246,3 +248,24 @@ replPrintLn' :: Text -> EvalM 'ReplRuntime b SpanInfo ()
replPrintLn' p = do
r <- getReplState
_replOutputLine r p

-- This orphan instance allows us to separate
-- the repl declaration out, as ugly as it is
instance DebugPrintable 'ReplRuntime (ReplBuiltin CoreBuiltin) where
debugPrint dp term =
ask >>= \case
ReplEnv _ -> do
case dp of
DPLexer -> whenReplFlagSet ReplDebugLexer $ liftIO $ do
putStrLn "----------- Lexer output -----------------"
print (pretty term)
DPParser -> whenReplFlagSet ReplDebugParser $
liftIO $ do
putStrLn "----------- Parser output ----------------"
print (pretty term)
DPDesugar -> whenReplFlagSet ReplDebugDesugar $ case term of
Term.TLTerm t ->
liftIO $ do
putStrLn "----------- Desugar output ---------------"
print (pretty t)
_ -> pure ()
86 changes: 43 additions & 43 deletions pact-tng.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,6 @@ library
import: pact-common
hs-source-dirs: pact

if (flag(with-funcall-tracing))
cpp-options: -DWITH_FUNCALL_TRACING

if (flag(with-native-tracing))
cpp-options: -DWITH_NATIVE_TRACING

if (flag(with-native-tracing) || flag(with-funcall-tracing))
cpp-options: -DWITH_TRACING

if (flag(with-crypto))
build-depends: pact-tng:pact-crypto
else
Expand Down Expand Up @@ -246,7 +237,6 @@ library
Pact.Core.Evaluate
Pact.Core.Scheme
Pact.Core.SPV
Pact.Core.Repl
Pact.Core.SizeOf
Pact.Core.SizeOf.Deriving
Pact.Core.StackFrame
Expand All @@ -267,11 +257,6 @@ library
Pact.Core.IR.Term
Pact.Core.IR.Desugar

Pact.Core.IR.Eval.Direct.Evaluator
Pact.Core.IR.Eval.Direct.CoreBuiltin
Pact.Core.IR.Eval.Direct.ReplBuiltin
Pact.Core.IR.Eval.Direct.Types

-- Core IR Evaluator modules
Pact.Core.IR.Eval.Runtime
Pact.Core.IR.Eval.Runtime.Types
Expand All @@ -286,14 +271,6 @@ library
Pact.Core.Trans.MPFR
Pact.Core.Version

-- Repl
Pact.Core.Repl.Utils
Pact.Core.Repl.Runtime.ReplBuiltin
Pact.Core.Repl.Compile
Pact.Core.Repl.BuiltinDocs
Pact.Core.Repl.BuiltinDocs.Internal
Pact.Core.Repl.UserDocs

-- Serialization
Pact.Core.Serialise
Pact.Core.Serialise.LegacyPact
Expand All @@ -311,6 +288,44 @@ library

extra-libraries: mpfr

library pact-repl
import: pact-common
hs-source-dirs: pact-repl

if (flag(with-crypto))
build-depends: pact-tng:pact-crypto
else
cpp-options: -DWITHOUT_CRYPTO

if (flag(with-funcall-tracing))
cpp-options: -DWITH_FUNCALL_TRACING

if (flag(with-native-tracing))
cpp-options: -DWITH_NATIVE_TRACING

if (flag(with-native-tracing) || flag(with-funcall-tracing))
cpp-options: -DWITH_TRACING

exposed-modules:
Pact.Core.Repl
Pact.Core.Repl.Utils
Pact.Core.Repl.Runtime.ReplBuiltin
Pact.Core.Repl.Compile
Pact.Core.Repl.BuiltinDocs
Pact.Core.Repl.BuiltinDocs.Internal
Pact.Core.Repl.UserDocs

Pact.Core.IR.Eval.Direct.Evaluator
Pact.Core.IR.Eval.Direct.CoreBuiltin
Pact.Core.IR.Eval.Direct.ReplBuiltin
Pact.Core.IR.Eval.Direct.Types

build-depends:
, pact-tng
, pact-tng:pact-crypto
, filepath
, pandoc

library pact-lsp
import: pact-common
hs-source-dirs: pact-lsp
Expand All @@ -322,6 +337,7 @@ library pact-lsp

build-depends:
, pact-tng
, pact-tng:pact-repl
, lsp
, lsp-types
, filepath
Expand All @@ -334,6 +350,7 @@ executable gasmodel

build-depends:
, pact-tng
, pact-tng:pact-repl
, criterion
, terminal-progress-bar
, neat-interpolation
Expand Down Expand Up @@ -369,10 +386,11 @@ executable profile-tx

executable pact
import: pact-common
main-is: repl/Main.hs
main-is: pact-repl/Main.hs

build-depends: base
, pact-tng
, pact-tng:pact-repl
, pact-tng:pact-lsp
, optparse-applicative
, pact-tng:pact-request-api
Expand Down Expand Up @@ -523,6 +541,7 @@ test-suite core-tests
, semirings
, neat-interpolation
, pact-tng:pact-request-api
, pact-tng:pact-repl
, pact-tng:pact-lsp
, pact-tng:test-utils
, pact-tng:unsafe
Expand Down Expand Up @@ -568,22 +587,3 @@ test-suite core-tests
, Pact.Core.Test.ServerUtils
if (flag(with-crypto))
build-depends: pact-tng:pact-crypto


-- -- tools
-- executable pact-server
-- import: pact-common
-- hs-source-dirs: pact-server

-- main-is: Pact/Server.hs

-- build-depends:
-- , pact-tng
-- , async
-- , fast-logger
-- , filepath
-- , servant
-- , servant-server
-- ghc-options: -Wall -threaded -rtsopts -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints
-- ghc-prof-options: -fprof-auto -fprof-auto-calls
-- default-language: Haskell2010
6 changes: 6 additions & 0 deletions pact/Pact/Core/Builtin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ coreBuiltinToUserText = \case

instance IsBuiltin CoreBuiltin where
builtinName = NativeName . coreBuiltinToText
builtinChargesGas _ = True
builtinArity = \case
CoreAdd -> 2
-- Num ->
Expand Down Expand Up @@ -783,6 +784,7 @@ data ReplOnlyBuiltin

instance IsBuiltin ReplOnlyBuiltin where
builtinName = NativeName . replBuiltinsToText
builtinChargesGas _ = False
builtinArity = \case
RExpect -> 3
RExpectFailure -> 2
Expand Down Expand Up @@ -843,6 +845,9 @@ instance IsBuiltin b => IsBuiltin (ReplBuiltin b) where
builtinArity = \case
RBuiltinWrap b -> builtinArity b
RBuiltinRepl b -> builtinArity b
builtinChargesGas = \case
RBuiltinWrap b -> builtinChargesGas b
RBuiltinRepl b -> builtinChargesGas b

-- RLoad -> 1

Expand Down Expand Up @@ -961,6 +966,7 @@ replCoreBuiltinOnlyMap =
class Show b => IsBuiltin b where
builtinArity :: b -> Int
builtinName :: b -> NativeName
builtinChargesGas :: b -> Bool


instance Pretty CoreBuiltin where
Expand Down
4 changes: 2 additions & 2 deletions pact/Pact/Core/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ evalModuleGovernance interpreter tl = do

compileDesugarOnly
:: forall e b i
. (HasCompileEnv b i)
. (HasCompileEnv b i, DebugPrintable e b)
=> Interpreter e b i
-> Lisp.TopLevel i
-> EvalM e b i (EvalTopLevel b i, S.Set ModuleName)
Expand All @@ -169,7 +169,7 @@ compileDesugarOnly interpreter tl = do

interpretTopLevel
:: forall e b i
. (HasCompileEnv b i)
. (HasCompileEnv b i, DebugPrintable e b)
=> Interpreter e b i
-> RawCode
-> Lisp.TopLevel i
Expand Down
Loading

0 comments on commit bc0ba1b

Please sign in to comment.