Skip to content

Commit

Permalink
Merge pull request #248 from vilunov/repl-let
Browse files Browse the repository at this point in the history
":let" command for REPL
  • Loading branch information
edwinb committed Jun 8, 2020
2 parents ff56062 + 018c17b commit abcb1b4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Idris/Parser.idr
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,9 @@ data CmdArg : Type where
||| The command takes an expression.
ExprArg : CmdArg

||| The command takes a list of declarations
DeclsArg : CmdArg

||| The command takes a number.
NumberArg : CmdArg

Expand All @@ -1725,6 +1728,7 @@ Show CmdArg where
show NoArg = ""
show NameArg = "<name>"
show ExprArg = "<expr>"
show DeclsArg = "<decls>"
show NumberArg = "<number>"
show OptionArg = "<option>"
show FileArg = "<filename>"
Expand Down Expand Up @@ -1789,6 +1793,18 @@ exprArgCmd parseCmd command doc = (names, ExprArg, doc, parse)
tm <- expr pdef "(interactive)" init
pure (command tm)

declsArgCmd : ParseCmd -> (List PDecl -> REPLCmd) -> String -> CommandDefinition
declsArgCmd parseCmd command doc = (names, DeclsArg, doc, parse)
where
names : List String
names = extractNames parseCmd
parse : Rule REPLCmd
parse = do
symbol ":"
runParseCmd parseCmd
tm <- topDecl "(interactive)" init
pure (command tm)

optArgCmd : ParseCmd -> (REPLOpt -> REPLCmd) -> Bool -> String -> CommandDefinition
optArgCmd parseCmd command set doc = (names, OptionArg, doc, parse)
where
Expand Down Expand Up @@ -1849,6 +1865,7 @@ parserCommandsForHelp =
, noArgCmd (ParseREPLCmd ["m", "metavars"]) Metavars "Show remaining proof obligations (metavariables or holes)"
, noArgCmd (ParseREPLCmd ["version"]) ShowVersion "Display the Idris version"
, noArgCmd (ParseREPLCmd ["?", "h", "help"]) Help "Display this help text"
, declsArgCmd (ParseKeywordCmd "let") NewDefn "Define a new value"
]

export
Expand Down
16 changes: 16 additions & 0 deletions src/Idris/REPL.idr
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ data REPLResult : Type where
OptionsSet : List REPLOpt -> REPLResult
LogLevelSet : Nat -> REPLResult
VersionIs : Version -> REPLResult
DefDeclared : REPLResult
Exited : REPLResult
Edited : EditResult -> REPLResult

Expand All @@ -443,6 +444,20 @@ execExp ctm
pure $ Executed ctm


execDecls : {auto c : Ref Ctxt Defs} ->
{auto u : Ref UST UState} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto m : Ref MD Metadata} ->
List PDecl -> Core REPLResult
execDecls decls = do
traverse_ execDecl decls
pure DefDeclared
where
execDecl : PDecl -> Core ()
execDecl decl = do
i <- desugarDecl [] decl
traverse_ (processDecl [] (MkNested []) []) i

export
compileExp : {auto c : Ref Ctxt Defs} ->
{auto u : Ref UST UState} ->
Expand Down Expand Up @@ -492,6 +507,7 @@ process : {auto c : Ref Ctxt Defs} ->
{auto m : Ref MD Metadata} ->
{auto o : Ref ROpts REPLOpts} ->
REPLCmd -> Core REPLResult
process (NewDefn decls) = execDecls decls
process (Eval itm)
= do opts <- get ROpts
case evalMode opts of
Expand Down
1 change: 1 addition & 0 deletions src/Idris/Syntax.idr
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ data EditCmd : Type where

public export
data REPLCmd : Type where
NewDefn : List PDecl -> REPLCmd
Eval : PTerm -> REPLCmd
Check : PTerm -> REPLCmd
PrintDef : Name -> REPLCmd
Expand Down
2 changes: 1 addition & 1 deletion tests/Main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ idrisTests
"interface009", "interface010", "interface011", "interface012",
"interface013", "interface014", "interface015",
-- Miscellaneous REPL
"interpreter001",
"interpreter001", "interpreter002",
-- Implicit laziness, lazy evaluation
"lazy001",
-- QTT and linearity related
Expand Down
3 changes: 3 additions & 0 deletions tests/idris2/interpreter002/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Main> Main> Main> "Privet, mir!"
Main>
Bye for now!
3 changes: 3 additions & 0 deletions tests/idris2/interpreter002/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:let i : String
:let i = "Privet, mir!"
i
3 changes: 3 additions & 0 deletions tests/idris2/interpreter002/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$1 --no-banner --no-prelude < input

rm -rf build

0 comments on commit abcb1b4

Please sign in to comment.