Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

":let" command for REPL #248

Merged
merged 2 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Idris/Parser.idr
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,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 @@ -1721,6 +1724,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 @@ -1785,6 +1789,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 @@ -1845,6 +1861,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 @@ -304,6 +304,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