Skip to content

Commit bbd15c2

Browse files
Preserve interpreter state across invocations
1 parent fe7b977 commit bbd15c2

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

src/Eval.hs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE TypeApplications #-}
33
module Eval
4-
( initDisco
4+
( RefRepl
5+
, initDisco
56
, eval
67
) where
78

@@ -13,10 +14,6 @@ import Disco.Module
1314
import Disco.Names
1415
( ModuleProvenance
1516
)
16-
import Disco.Parser
17-
( term
18-
, runParser
19-
)
2017
import Polysemy
2118
( Embed
2219
, Sem
@@ -26,30 +23,37 @@ import System.Environment
2623

2724
import Interpreter
2825
( Repl
29-
, initial
3026
, execute
27+
, initial
3128
)
29+
import Data.IORef
3230

3331
{-----------------------------------------------------------------------------
3432
Rendering Logic
3533
------------------------------------------------------------------------------}
36-
eval :: String -> IO String
37-
eval command = fst <$> execute command initial
34+
type RefRepl = IORef Repl
3835

39-
parseTest :: String -> String
40-
parseTest = show . runParser term "<interactive>"
36+
eval :: RefRepl -> String -> IO String
37+
eval ref command = do
38+
repl0 <- readIORef ref
39+
(result, repl1) <- execute command repl0
40+
writeIORef ref repl1
41+
pure result
4142

4243
resolveModule'
4344
:: Resolver -> String
4445
-> Sem '[Embed IO] (Maybe (FilePath, ModuleProvenance))
4546
resolveModule' = resolveModule
4647

47-
initDisco :: IO ()
48+
initDisco :: IO RefRepl
4849
initDisco = do
4950
-- NOTE: We set path environment variables here,
5051
-- because processing the .wasm module with `wizer` may bake
5152
-- them into the code.
5253
setEnv "disco_datadir" "stdlib"
5354

55+
-- Debug output
5456
s <- runM $ resolveModule' FromStdlib "num"
5557
print s
58+
59+
newIORef initial

src/Interpreter.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Interpreter
1111
, execute
1212
) where
1313

14+
import Disco.AST.Surface (emptyModule)
1415
import Disco.Context hiding (filter)
1516
import Disco.Error
1617
import Disco.Eval
@@ -21,9 +22,11 @@ import Disco.Interactive.Commands
2122
)
2223
import Disco.Messages
2324
import Disco.Module
25+
import Disco.Names (ModuleName (REPLModule))
2426
import Disco.Pretty
2527
import Disco.Value
2628

29+
import Control.Monad (void)
2730
import Control.Lens
2831
( makeLenses
2932
, toListOf
@@ -79,6 +82,14 @@ initTopInfo = TopInfo
7982
, _discoConfig = initDiscoConfig
8083
}
8184

85+
-- | Load the standard library.
86+
-- TODO: Doesn't work yet.
87+
loadStdlib :: Repl -> IO Repl
88+
loadStdlib =
89+
fmap snd
90+
. runDiscoEffects
91+
(loadParsedDiscoModule True FromStdlib REPLModule emptyModule)
92+
8293
-- | Interpreter command
8394
type Command = String
8495

@@ -91,7 +102,7 @@ execute command = runDiscoEffects (runCommand command)
91102

92103
-- | Run a top-level computation.
93104
runDiscoEffects
94-
:: (forall r. Members DiscoEffects r => Sem r ())
105+
:: (forall r. Members DiscoEffects r => Sem r a)
95106
-> Repl
96107
-> IO (Result, Repl)
97108
runDiscoEffects action Repl{topInfo,mem} = do
@@ -110,7 +121,7 @@ runDiscoEffects action Repl{topInfo,mem} = do
110121
. mapError EvalErr -- Embed runtime errors into top-level error type
111122
. failToError Panic -- Turn pattern-match failures into a Panic error
112123
. runReader (view topEnv topInfo) -- Keep track of current Env
113-
$ action
124+
. void $ action
114125
let repl' = Repl topInfo' mem'
115126
pure (showOutputs outputs, repl')
116127
where

src/Main.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Main where
33
import Control.Concurrent
44
import GHC.Wasm.Prim
55

6-
import Eval (eval, initDisco)
6+
import Eval (RefRepl, eval, initDisco)
77

88
{-----------------------------------------------------------------------------
99
JavaScript Imports
@@ -40,20 +40,20 @@ foreign export javascript "setup" setup :: IO ()
4040
-- | Main entrypoint.
4141
setup :: IO ()
4242
setup = do
43+
ref <- initDisco
44+
4345
-- Register callback for button click.
4446
evalButton <- js_document_getElementById (toJSString "eval")
45-
onEvalButtonCallback <- asEventListener onEvalButtonClick
47+
onEvalButtonCallback <- asEventListener (onEvalButtonClick ref)
4648
js_addEventListener evalButton (toJSString "click") onEvalButtonCallback
4749

48-
initDisco
49-
5050
-- | Handle button clicks.
51-
onEvalButtonClick :: JSVal -> IO ()
52-
onEvalButtonClick event = do
51+
onEvalButtonClick :: RefRepl -> JSVal -> IO ()
52+
onEvalButtonClick ref event = do
5353
module_ <- fromJSString <$> js_view_state_doc_toString
5454
exprIn <- js_document_getElementById (toJSString "expr")
5555
expr <- fromJSString <$> js_input_value exprIn
5656

57-
result <- eval expr
57+
result <- eval ref expr
5858
outDiv <- js_document_getElementById (toJSString "out")
5959
js_element_setInnerHtml outDiv (toJSString result)

0 commit comments

Comments
 (0)