git.iko.soy/rowtype-yoga/purescript-native
on 24fdff2 help sign in
181 lines 4.7 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Types
4-- Copyright : (c) Phil Freeman 2014
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Type declarations and associated basic functions for PSCI.
13--
14-----------------------------------------------------------------------------
16module Types where
18import qualified Data.Map as M
19import qualified Language.PureScript as P
21data PSCiOptions = PSCiOptions
22 { psciMultiLineMode :: Bool
23 , psciInputFile :: [FilePath]
24 , psciForeignInputFiles :: [FilePath]
25 , psciInputNodeFlags :: [String]
26 }
28-- |
29-- The PSCI state.
30-- Holds a list of imported modules, loaded files, and partial let bindings.
31-- The let bindings are partial,
32-- because it makes more sense to apply the binding to the final evaluated expression.
33--
34data PSCiState = PSCiState
35 { psciImportedFilenames :: [FilePath]
36 , psciImportedModules :: [ImportedModule]
37 , psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
38 , psciForeignFiles :: M.Map P.ModuleName FilePath
39 , psciLetBindings :: [P.Declaration]
40 , psciNodeFlags :: [String]
41 }
43-- | All of the data that is contained by an ImportDeclaration in the AST.
44-- That is:
45--
46-- * A module name, the name of the module which is being imported
47-- * An ImportDeclarationType which specifies whether there is an explicit
48-- import list, a hiding list, or neither.
49-- * If the module is imported qualified, its qualified name in the importing
50-- module. Otherwise, Nothing.
51--
52type ImportedModule = (P.ModuleName, P.ImportDeclarationType, Maybe P.ModuleName)
54psciImportedModuleNames :: PSCiState -> [P.ModuleName]
55psciImportedModuleNames (PSCiState{psciImportedModules = is}) =
56 map (\(mn, _, _) -> mn) is
58allImportsOf :: P.Module -> PSCiState -> [ImportedModule]
59allImportsOf m (PSCiState{psciImportedModules = is}) =
60 filter isImportOfThis is
61 where
62 name = P.getModuleName m
63 isImportOfThis (name', _, _) = name == name'
65-- State helpers
67-- |
68-- Updates the state to have more imported modules.
69--
70updateImportedFiles :: FilePath -> PSCiState -> PSCiState
71updateImportedFiles filename st = st { psciImportedFilenames = filename : psciImportedFilenames st }
73-- |
74-- Updates the state to have more imported modules.
75--
76updateImportedModules :: ImportedModule -> PSCiState -> PSCiState
77updateImportedModules im st = st { psciImportedModules = im : psciImportedModules st }
79-- |
80-- Updates the state to have more loaded files.
81--
82updateModules :: [(Either P.RebuildPolicy FilePath, P.Module)] -> PSCiState -> PSCiState
83updateModules modules st = st { psciLoadedModules = psciLoadedModules st ++ modules }
85-- |
86-- Updates the state to have more let bindings.
87--
88updateLets :: [P.Declaration] -> PSCiState -> PSCiState
89updateLets ds st = st { psciLetBindings = psciLetBindings st ++ ds }
91-- |
92-- Updates the state to have more let bindings.
93--
94updateForeignFiles :: M.Map P.ModuleName FilePath -> PSCiState -> PSCiState
95updateForeignFiles fs st = st { psciForeignFiles = psciForeignFiles st `M.union` fs }
97-- |
98-- Valid Meta-commands for PSCI
99--
100data Command
101 -- |
102 -- A purescript expression
103 --
104 = Expression P.Expr
105 -- |
106 -- Show the help (ie, list of directives)
107 --
108 | ShowHelp
109 -- |
110 -- Import a module from a loaded file
111 --
112 | Import ImportedModule
113 -- |
114 -- Browse a module
115 --
116 | BrowseModule P.ModuleName
117 -- |
118 -- Load a file for use with importing
119 --
120 | LoadFile FilePath
121 -- |
122 -- Load a foreign module
123 --
124 | LoadForeign FilePath
125 -- |
126 -- Exit PSCI
127 --
128 | QuitPSCi
129 -- |
130 -- Reset the state of the REPL
131 --
132 | ResetState
133 -- |
134 -- Add some declarations to the current evaluation context.
135 --
136 | Decls [P.Declaration]
137 -- |
138 -- Find the type of an expression
139 --
140 | TypeOf P.Expr
141 -- |
142 -- Find the kind of an expression
143 --
144 | KindOf P.Type
145 -- |
146 -- Shows information about the current state of the REPL
147 --
148 | ShowInfo ReplQuery
150data ReplQuery
151 = QueryLoaded
152 | QueryImport
153 deriving (Eq, Show)
155-- | A list of all ReplQuery values.
156replQueries :: [ReplQuery]
157replQueries = [QueryLoaded, QueryImport]
159replQueryStrings :: [String]
160replQueryStrings = map showReplQuery replQueries
162showReplQuery :: ReplQuery -> String
163showReplQuery QueryLoaded = "loaded"
164showReplQuery QueryImport = "import"
166parseReplQuery :: String -> Maybe ReplQuery
167parseReplQuery "loaded" = Just QueryLoaded
168parseReplQuery "import" = Just QueryImport
169parseReplQuery _ = Nothing
171data Directive
172 = Help
173 | Quit
174 | Reset
175 | Browse
176 | Load
177 | Foreign
178 | Type
179 | Kind
180 | Show
181 deriving (Eq, Show)