diff --git a/README.md b/README.md index da42d03..af51307 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,11 @@ Like a normal programming language, you can also execute Contra programs. By def contra .con ``` -This prototype also comes with a rudimentary REPL. Start a blank interactive session by typing just `contra`. Load files into the REPL with `:l .con` and quit with `:q`. +This prototype also comes with a rudimentary REPL. Start a blank interactive session by typing just `contra`. + +Load files into the REPL with `:l .con` and quit with `:q`. + +You can save function definitions (and nullary functions/constants) for the session by using the special syntax `def x = ...`. ```shell # blank REPL session diff --git a/src/Semantics/REPL.hs b/src/Semantics/REPL.hs index 680bb8c..da02c09 100644 --- a/src/Semantics/REPL.hs +++ b/src/Semantics/REPL.hs @@ -12,8 +12,13 @@ This is a rudimentary REPL (Read-Eval-Print Loop) for Contra. It can be used to interact with the core language and the function definitions - in a program text. After starting the REPL, load a program in the terminal - with ':l ' and start calling functions interactively. + in a program text. + + After starting the REPL, load a program in the terminal with + ':l ' and start calling functions interactively. + + Define a new variable with "def x = ...", which is saved as a function + definition in the program for that session. -} @@ -33,15 +38,24 @@ loop :: Program Type -> IO () loop p = do input <- readLine case input of - ":q" -> return () - (':':'l':' ': file) -> do program <- loadProgram file - putStrLn $ "Loaded file " ++ show file - loop program - command -> do parsed <- parseLine command - typed <- typeCheck parsed - let (interpreted, residual) = eval p typed - print interpreted - loop residual + ":q" -> return () + (':':'l':' ': file) -> + do program <- loadProgram file + putStrLn $ "Loaded file " ++ show file + loop program + ('d':'e':'f':' ':x:' ':'=':' ':expr) -> + do parsed <- parseLine expr + typed <- typeCheck parsed + let (interpreted, residual) = eval p typed + let residual' = residual <> (Function [x] interpreted End) + print interpreted + loop residual' + expr -> + do parsed <- parseLine expr + typed <- typeCheck parsed + let (interpreted, residual) = eval p typed + print interpreted + loop residual -- Utility