Skip to content

Commit

Permalink
Save defs in REPL dynamically!
Browse files Browse the repository at this point in the history
  • Loading branch information
SophieBosio committed May 3, 2024
1 parent 65c2036 commit 345a3dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ Like a normal programming language, you can also execute Contra programs. By def
contra <program-name>.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 <filename>.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 <filename>.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
Expand Down
36 changes: 25 additions & 11 deletions src/Semantics/REPL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <filename.con>' and start calling functions interactively.
in a program text.
After starting the REPL, load a program in the terminal with
':l <filename.con>' 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.
-}

Expand All @@ -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)

Check warning on line 50 in src/Semantics/REPL.hs

View workflow job for this annotation

GitHub Actions / hlint

Suggestion in loop in module Semantics.REPL: Redundant bracket ▫︎ Found: "residual <> (Function [x] interpreted End)" ▫︎ Perhaps: "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
Expand Down

0 comments on commit 345a3dc

Please sign in to comment.