git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #483 from purescript/implicit-prim

Remove special cases from parser for JS types

commit22900aab20b36479c20f413791ddf8c7098d0f3b authorPhil Freeman <paf31@cantab.net> dateTue 15 Jul 2014, 12:52 (12 years ago) parentb5a13bbe8cc611
5 files changed+27−34
examples/passing/EmptyDataDecls.purs+6−6 src/Language/PureScript.hs+16−13 src/Language/PureScript/Environment.hs+1−1 src/Language/PureScript/Parser/Types.hs+1−13 src/Language/PureScript/Sugar/Names.hs+3−1
examples/passing/EmptyDataDecls.purs+6−6
@@ -5,10 +5,10 @@ import Prelude
55data Z
66data S n
77
8−data Array n a = Array [a]
8+data ArrayBox n a = ArrayBox [a]
99
10−nil :: forall a. Array Z a
11−nil = Array []
10+nil :: forall a. ArrayBox Z a
11+nil = ArrayBox []
1212
1313foreign import concat
1414 "function concat(l1) {\
@@ -17,8 +17,8 @@ foreign import concat
1717 \ };\
1818 \}" :: forall a. [a] -> [a] -> [a]
1919
20−cons' :: forall a n. a -> Array n a -> Array (S n) a
21−cons' x (Array xs) = Array $ concat [x] xs
20+cons' :: forall a n. a -> ArrayBox n a -> ArrayBox (S n) a
21+cons' x (ArrayBox xs) = ArrayBox $ concat [x] xs
2222
2323foreign import error
2424 "function error(msg) {\
@@ -26,5 +26,5 @@ foreign import error
2626 \}" :: forall a. String -> a
2727
2828main = case cons' 1 $ cons' 2 $ cons' 3 nil of
29− Array [1, 2, 3] -> Debug.Trace.trace "Done"
29+ ArrayBox [1, 2, 3] -> Debug.Trace.trace "Done"
3030 _ -> error "Failed"
src/Language/PureScript.hs+16−13
@@ -70,7 +70,7 @@ compile = compile' initEnvironment
7070
7171compile' :: Environment -> Options -> [Module] -> Either String (String, String, Environment)
7272compile' env opts ms = do
73− (sorted, _) <- sortModules $ if optionsNoPrelude opts then ms else (map importPrelude ms)
73+ (sorted, _) <- sortModules $ map importPrim $ if optionsNoPrelude opts then ms else (map importPrelude ms)
7474 (desugared, nextVar) <- stringifyErrorStack True $ runSupplyT 0 $ desugar sorted
7575 (elaborated, env') <- runCheck' opts env $ forM desugared $ typeCheckModule mainModuleIdent
7676 regrouped <- stringifyErrorStack True $ createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
@@ -170,7 +170,7 @@ make :: (Functor m, Applicative m, Monad m, MonadMake m) => FilePath -> Options
170170make outputDir opts ms = do
171171 let filePathMap = M.fromList (map (\(fp, Module mn _ _) -> (mn, fp)) ms)
172172
173− (sorted, graph) <- liftError $ sortModules $ if optionsNoPrelude opts then map snd ms else (map (importPrelude . snd) ms)
173+ (sorted, graph) <- liftError $ sortModules $ map importPrim $ if optionsNoPrelude opts then map snd ms else (map (importPrelude . snd) ms)
174174
175175 toRebuild <- foldM (\s (Module moduleName' _ _) -> do
176176 let filePath = runModuleName moduleName'
@@ -241,16 +241,19 @@ reverseDependencies g = combine [ (dep, mn) | (mn, deps) <- g, dep <- deps ]
241241 combine = M.fromList . map ((fst . head) &&& map snd) . groupBy ((==) `on` fst) . sortBy (compare `on` fst)
242242
243243-- |
244−-- Add an import declaration for the Prelude to a module if it does not already explicitly import
245−-- it.
244+-- Add an import declaration for a module if it does not already explicitly import it.
246245--
247−importPrelude :: Module -> Module
248−importPrelude m@(Module mn decls exps) =
249− if isPreludeImport `any` decls || mn == prelude then m
250− else Module mn (preludeImport : decls) exps
246+addDefaultImport :: ModuleName -> Module -> Module
247+addDefaultImport toImport m@(Module mn decls exps) =
248+ if isExistingImport `any` decls || mn == toImport then m
249+ else Module mn (ImportDeclaration toImport Nothing Nothing : decls) exps
251250 where
252− prelude = ModuleName [ProperName C.prelude]
253− isPreludeImport (ImportDeclaration (ModuleName [ProperName mn']) _ _) | mn' == C.prelude = True
254− isPreludeImport (PositionedDeclaration _ d) = isPreludeImport d
255− isPreludeImport _ = False
256− preludeImport = ImportDeclaration prelude Nothing Nothing
251+ isExistingImport (ImportDeclaration mn' _ _) | mn' == toImport = True
252+ isExistingImport (PositionedDeclaration _ d) = isExistingImport d
253+ isExistingImport _ = False
254+
255+importPrim :: Module -> Module
256+importPrim = addDefaultImport (ModuleName [ProperName C.prim])
257+
258+importPrelude :: Module -> Module
259+importPrelude = addDefaultImport (ModuleName [ProperName C.prelude])
src/Language/PureScript/Environment.hs+1−1
@@ -1,6 +1,6 @@
11-----------------------------------------------------------------------------
22--
3−-- Module : Language.PureScript.Prim
3+-- Module : Language.PureScript.Environment
44-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
55-- License : MIT
66--
src/Language/PureScript/Parser/Types.hs+1−13
@@ -30,15 +30,6 @@ import Language.PureScript.Environment
3030import qualified Text.Parsec as P
3131import qualified Text.Parsec.Expr as P
3232
33−parseNumber :: P.Parsec String ParseState Type
34−parseNumber = const tyNumber <$> reserved "Number"
35−
36−parseString :: P.Parsec String ParseState Type
37−parseString = const tyString <$> reserved "String"
38−
39−parseBoolean :: P.Parsec String ParseState Type
40−parseBoolean = const tyBoolean <$> reserved "Boolean"
41−
4233parseArray :: P.Parsec String ParseState Type
4334parseArray = squares $ return tyArray
4435
@@ -69,10 +60,7 @@ parseForAll = mkForAll <$> (P.try (reserved "forall") *> P.many1 (indented *> id
6960--
7061parseTypeAtom :: P.Parsec String ParseState Type
7162parseTypeAtom = indented *> P.choice (map P.try
72− [ parseNumber
73− , parseString
74− , parseBoolean
75− , parseArray
63+ [ parseArray
7664 , parseArrayOf
7765 , parseFunction
7866 , parseObject
src/Language/PureScript/Sugar/Names.hs+3−1
@@ -31,6 +31,8 @@ import Language.PureScript.Environment
3131import Language.PureScript.Errors
3232import Language.PureScript.Traversals
3333
34+import qualified Language.PureScript.Constants as C
35+
3436-- |
3537-- The global export environment - every declaration exported from every module.
3638--
@@ -272,7 +274,7 @@ renameInModule imports exports (Module mn decls exps) =
272274-- Finds all exported declarations in a set of modules.
273275--
274276findExports :: [Module] -> Either ErrorStack ExportEnvironment
275−findExports = foldM addModule $ M.singleton (ModuleName [ProperName "Prim"]) primExports
277+findExports = foldM addModule $ M.singleton (ModuleName [ProperName C.prim]) primExports
276278 where
277279
278280 -- The exported types from the Prim module