Skip to content

Commit 7f5b3fa

Browse files
committed
Create utility module for special commands
1 parent 7c77c4b commit 7f5b3fa

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

psci/Directive.hs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
-----------------------------------------------------------------------------
2+
--
3+
-- Module : Directive
4+
-- Copyright :
5+
-- License : MIT
6+
--
7+
-- Maintainer :
8+
-- Stability : experimental
9+
-- Portability :
10+
--
11+
-- |
12+
-- Directives for PSCI.
13+
--
14+
-----------------------------------------------------------------------------
15+
16+
module Directive where
17+
18+
import Control.Applicative
19+
import Data.List (find, isPrefixOf)
20+
21+
data Directive
22+
= Help
23+
| Quit
24+
| Reset
25+
| Import
26+
| Browse
27+
| Load
28+
| Type
29+
| Kind
30+
| Show
31+
32+
-- |
33+
-- Maps given directive relating command strings.
34+
--
35+
commands :: Directive -> [String]
36+
commands Help = ["?", "help"]
37+
commands Quit = ["quit"]
38+
commands Reset = ["reset"]
39+
commands Import = ["import"]
40+
commands Browse = ["browse"]
41+
commands Load = ["load", "module"]
42+
commands Type = ["type"]
43+
commands Kind = ["kind"]
44+
commands Show = ["show"]
45+
46+
-- |
47+
-- Tries to parse given string into a directive.
48+
--
49+
parseDirective :: String -> Maybe Directive
50+
parseDirective cmd = fst <$> find (matches . snd) mapping
51+
where
52+
mapping :: [(Directive, [String])]
53+
mapping = zip directives (map commands directives)
54+
55+
matches :: [String] -> Bool
56+
matches = any (cmd `isPrefixOf`)
57+
58+
-- |
59+
-- The help menu.
60+
--
61+
help :: [(Directive, String, String)]
62+
help =
63+
[ (Help, "", "Show this help menu")
64+
, (Quit, "", "Quit PSCi")
65+
, (Reset, "", "Reset")
66+
, (Import, "<module>", "Import <module> for use in PSCI")
67+
, (Browse, "<module>", "Browse <module>")
68+
, (Load, "<file>", "Load <file> for importing")
69+
, (Type, "<expr>", "Show the type of <expr>")
70+
, (Kind, "<type>", "Show the kind of <type>")
71+
, (Show, "import", "Show imported modules")
72+
, (Show, "loaded", "Show loaded modules")
73+
]
74+
75+
-- |
76+
-- List of all avaliable directives.
77+
--
78+
directives :: [Directive]
79+
directives = map (\(dir, _, _) -> dir) help

0 commit comments

Comments
 (0)