git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: prelim UTF8 string literal support
adfd8dcAndy Arvanitis
pcc/Main.hs+2−1
@@ -31,6 +31,7 @@ import Options.Applicative as Opts
31
31
32import System.Exit (exitSuccess, exitFailure)
32import System.Exit (exitSuccess, exitFailure)
33import System.IO (hPutStrLn, stderr)
33import System.IO (hPutStrLn, stderr)
34+import System.IO.UTF8
34
35
35import qualified Language.PureScript as P
36import qualified Language.PureScript as P
36import qualified Paths_purescript as Paths
37import qualified Paths_purescript as Paths
@@ -72,7 +73,7 @@ compile (PCCOptions input _ outputDir opts usePrefix) = do
72 exitSuccess
73 exitSuccess
73
74
74readInput :: InputOptions -> IO [(Either P.RebuildPolicy FilePath, String)]
75readInput :: InputOptions -> IO [(Either P.RebuildPolicy FilePath, String)]
75−readInput InputOptions{..} = forM ioInputFiles $ \inFile -> (Right inFile, ) <$> readFile inFile
76+readInput InputOptions{..} = forM ioInputFiles $ \inFile -> (Right inFile, ) <$> readUTF8File inFile
76
77
77parseInputs :: (Functor m, Applicative m, MonadError P.MultipleErrors m, MonadWriter P.MultipleErrors m)
78parseInputs :: (Functor m, Applicative m, MonadError P.MultipleErrors m, MonadWriter P.MultipleErrors m)
78 => [(Either P.RebuildPolicy FilePath, String)]
79 => [(Either P.RebuildPolicy FilePath, String)]
pcc/Make.hs+19−14
@@ -37,11 +37,13 @@ import Data.String (fromString)
37import Data.Time.Clock
37import Data.Time.Clock
38import Data.Version (showVersion)
38import Data.Version (showVersion)
39import qualified Data.Map as M
39import qualified Data.Map as M
40−import qualified Data.ByteString.Lazy as B
40+import qualified Data.ByteString.Lazy as BL
41+import qualified Data.ByteString.Char8 as B
41
42
42import System.Directory (doesDirectoryExist, doesFileExist, getModificationTime, createDirectoryIfMissing)
43import System.Directory (doesDirectoryExist, doesFileExist, getModificationTime, createDirectoryIfMissing)
43import System.FilePath ((</>), takeDirectory, addExtension, dropExtension)
44import System.FilePath ((</>), takeDirectory, addExtension, dropExtension)
44import System.IO.Error (tryIOError)
45import System.IO.Error (tryIOError)
46+import System.IO.UTF8
45
47
46import qualified Language.PureScript as P
48import qualified Language.PureScript as P
47import qualified Language.PureScript.CodeGen.Cpp as CPP
49import qualified Language.PureScript.CodeGen.Cpp as CPP
@@ -94,10 +96,10 @@ buildMakeActions outputDir filePathMap usePrefix =
94 externsFile = outputDir </> filePath </> "externs.purs"
96 externsFile = outputDir </> filePath </> "externs.purs"
95 min <$> getTimestamp srcFile <*> getTimestamp externsFile
97 min <$> getTimestamp srcFile <*> getTimestamp externsFile
96
98
97− readExterns :: P.ModuleName -> Make (FilePath, B.ByteString)
99+ readExterns :: P.ModuleName -> Make (FilePath, BL.ByteString)
98 readExterns mn = do
100 readExterns mn = do
99 let path = outputDir </> (dotsTo '/' $ P.runModuleName mn) </> "externs.purs"
101 let path = outputDir </> (dotsTo '/' $ P.runModuleName mn) </> "externs.purs"
100− (path, ) <$> readTextFile path
102+ (path, ) <$> readTextFile' path
101
103
102 codegen :: CF.Module CF.Ann -> P.Environment -> P.Externs -> P.SupplyT Make ()
104 codegen :: CF.Module CF.Ann -> P.Environment -> P.Externs -> P.SupplyT Make ()
103 codegen m env exts = do
105 codegen m env exts = do
@@ -116,18 +118,18 @@ buildMakeActions outputDir filePathMap usePrefix =
116 hdr = unlines $ map ("// " ++) prefix ++ [phdrs]
118 hdr = unlines $ map ("// " ++) prefix ++ [phdrs]
117
119
118 lift $ do
120 lift $ do
119− writeTextFile srcFile (fromString src)
121+ writeTextFile srcFile src
120− writeTextFile headerFile (fromString hdr)
122+ writeTextFile headerFile hdr
121− writeTextFile externsFile exts
123+ writeTextFile externsFile $ B.unpack $ BL.toStrict exts
122
124
123 let supportDir = outputDir </> "PureScript"
125 let supportDir = outputDir </> "PureScript"
124 supportFilesExist <- dirExists supportDir
126 supportFilesExist <- dirExists supportDir
125 when (not supportFilesExist) $ do
127 when (not supportFilesExist) $ do
126 writeTextFile (outputDir </> "CMakeLists.txt") (fromString cmakeListsTxt)
128 writeTextFile (outputDir </> "CMakeLists.txt") (fromString cmakeListsTxt)
127− writeTextFile (supportDir </> "PureScript.hh") $ B.fromStrict $(embedFile "pcc/include/purescript.hh")
129+ writeTextFile (supportDir </> "PureScript.hh") $ B.unpack $(embedFile "pcc/include/purescript.hh")
128− writeTextFile (supportDir </> "PureScript.cc") $ B.fromStrict $(embedFile "pcc/include/purescript.cc")
130+ writeTextFile (supportDir </> "PureScript.cc") $ B.unpack $(embedFile "pcc/include/purescript.cc")
129− writeTextFile (supportDir </> "map_key.hh") $ B.fromStrict $(embedFile "pcc/include/map_key.hh")
131+ writeTextFile (supportDir </> "map_key.hh") $ B.unpack $(embedFile "pcc/include/map_key.hh")
130− writeTextFile (supportDir </> "hash.hh") $ B.fromStrict $(embedFile "pcc/include/hash.hh")
132+ writeTextFile (supportDir </> "hash.hh") $ B.unpack $(embedFile "pcc/include/hash.hh")
131
133
132 when (requiresForeign m) $ do
134 when (requiresForeign m) $ do
133 let inputPath = dropExtension $ getInputFile mn
135 let inputPath = dropExtension $ getInputFile mn
@@ -158,13 +160,16 @@ buildMakeActions outputDir filePathMap usePrefix =
158 exists <- doesFileExist path
160 exists <- doesFileExist path
159 traverse (const $ getModificationTime path) $ guard exists
161 traverse (const $ getModificationTime path) $ guard exists
160
162
161− readTextFile :: FilePath -> Make B.ByteString
163+ readTextFile :: FilePath -> Make String
162− readTextFile path = makeIO (const (P.SimpleErrorWrapper $ P.CannotReadFile path)) $ B.readFile path
164+ readTextFile path = makeIO (const (P.SimpleErrorWrapper $ P.CannotReadFile path)) $ readUTF8File path
163
165
164− writeTextFile :: FilePath -> B.ByteString -> Make ()
166+ readTextFile' :: FilePath -> Make BL.ByteString
167+ readTextFile' path = makeIO (const (P.SimpleErrorWrapper $ P.CannotReadFile path)) $ BL.readFile path
168+
169+ writeTextFile :: FilePath -> String -> Make ()
165 writeTextFile path text = makeIO (const (P.SimpleErrorWrapper $ P.CannotWriteFile path)) $ do
170 writeTextFile path text = makeIO (const (P.SimpleErrorWrapper $ P.CannotWriteFile path)) $ do
166 mkdirp path
171 mkdirp path
167− B.writeFile path text
172+ writeFile path text
168 where
173 where
169 mkdirp :: FilePath -> IO ()
174 mkdirp :: FilePath -> IO ()
170 mkdirp = createDirectoryIfMissing True . takeDirectory
175 mkdirp = createDirectoryIfMissing True . takeDirectory
src/Language/PureScript/Pretty/Cpp.hs+3−2
@@ -52,7 +52,8 @@ literals = mkPattern' match
52 match CppEndOfHeader = return []
52 match CppEndOfHeader = return []
53 match (CppNumericLiteral (Left n)) = return $ show n ++ "L"
53 match (CppNumericLiteral (Left n)) = return $ show n ++ "L"
54 match (CppNumericLiteral n) = return $ either show show n
54 match (CppNumericLiteral n) = return $ either show show n
55− match (CppStringLiteral s) = return $ string s
55+ match (CppStringLiteral s) | all isAscii s = return $ string s
56+ match (CppStringLiteral s) = return $ "u8" ++ string s
56 match (CppStringLiteralHash s) = return $ "HASH" ++ parens (string s)
57 match (CppStringLiteralHash s) = return $ "HASH" ++ parens (string s)
57 match (CppCharLiteral c) = return $ show c
58 match (CppCharLiteral c) = return $ show c
58 match (CppBooleanLiteral True) = return "true"
59 match (CppBooleanLiteral True) = return "true"
@@ -277,7 +278,7 @@ indexer :: Pattern PrinterState Cpp (String, Cpp)
277indexer = mkPattern' match
278indexer = mkPattern' match
278 where
279 where
279 match (CppIndexer index@(CppNumericLiteral {}) val) = (,) <$> prettyPrintCpp' index <*> pure val
280 match (CppIndexer index@(CppNumericLiteral {}) val) = (,) <$> prettyPrintCpp' index <*> pure val
280− match (CppIndexer index@(CppStringLiteral {}) val) = (,) <$> (liftM (("KEY" ++) . parens) . prettyPrintCpp') index <*> pure val
281+ match (CppIndexer (CppStringLiteral index) val) = return ("KEY" ++ parens (show index), val)
281 match _ = mzero
282 match _ = mzero
282
283
283lam :: Pattern PrinterState Cpp ((String, [(String, Maybe CppType)], Maybe CppType), Cpp)
284lam :: Pattern PrinterState Cpp ((String, [(String, Maybe CppType)], Maybe CppType), Cpp)