git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: prelim UTF8 string literal support
commitadfd8dc6664fc79c8280c465fb5c75971285fbb0 authorAndy Arvanitis <andyarvanitis@gmail.com> dateSun 11 Oct 2015, 20:48 (11 years ago) parentfc60b7a
3 files changed+24−17
pcc/Main.hs+2−1 pcc/Make.hs+19−14 src/Language/PureScript/Pretty/Cpp.hs+3−2
pcc/Main.hs+2−1
@@ -31,6 +31,7 @@ import Options.Applicative as Opts
3131
3232import System.Exit (exitSuccess, exitFailure)
3333import System.IO (hPutStrLn, stderr)
34+import System.IO.UTF8
3435
3536import qualified Language.PureScript as P
3637import qualified Paths_purescript as Paths
@@ -72,7 +73,7 @@ compile (PCCOptions input _ outputDir opts usePrefix) = do
7273 exitSuccess
7374
7475readInput :: 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
7677
7778parseInputs :: (Functor m, Applicative m, MonadError P.MultipleErrors m, MonadWriter P.MultipleErrors m)
7879 => [(Either P.RebuildPolicy FilePath, String)]
pcc/Make.hs+19−14
@@ -37,11 +37,13 @@ import Data.String (fromString)
3737import Data.Time.Clock
3838import Data.Version (showVersion)
3939import 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
4142
4243import System.Directory (doesDirectoryExist, doesFileExist, getModificationTime, createDirectoryIfMissing)
4344import System.FilePath ((</>), takeDirectory, addExtension, dropExtension)
4445import System.IO.Error (tryIOError)
46+import System.IO.UTF8
4547
4648import qualified Language.PureScript as P
4749import qualified Language.PureScript.CodeGen.Cpp as CPP
@@ -94,10 +96,10 @@ buildMakeActions outputDir filePathMap usePrefix =
9496 externsFile = outputDir </> filePath </> "externs.purs"
9597 min <$> getTimestamp srcFile <*> getTimestamp externsFile
9698
97− readExterns :: P.ModuleName -> Make (FilePath, B.ByteString)
99+ readExterns :: P.ModuleName -> Make (FilePath, BL.ByteString)
98100 readExterns mn = do
99101 let path = outputDir </> (dotsTo '/' $ P.runModuleName mn) </> "externs.purs"
100− (path, ) <$> readTextFile path
102+ (path, ) <$> readTextFile' path
101103
102104 codegen :: CF.Module CF.Ann -> P.Environment -> P.Externs -> P.SupplyT Make ()
103105 codegen m env exts = do
@@ -116,18 +118,18 @@ buildMakeActions outputDir filePathMap usePrefix =
116118 hdr = unlines $ map ("// " ++) prefix ++ [phdrs]
117119
118120 lift $ do
119− writeTextFile srcFile (fromString src)
120− writeTextFile headerFile (fromString hdr)
121− writeTextFile externsFile exts
121+ writeTextFile srcFile src
122+ writeTextFile headerFile hdr
123+ writeTextFile externsFile $ B.unpack $ BL.toStrict exts
122124
123125 let supportDir = outputDir </> "PureScript"
124126 supportFilesExist <- dirExists supportDir
125127 when (not supportFilesExist) $ do
126128 writeTextFile (outputDir </> "CMakeLists.txt") (fromString cmakeListsTxt)
127− writeTextFile (supportDir </> "PureScript.hh") $ B.fromStrict $(embedFile "pcc/include/purescript.hh")
128− writeTextFile (supportDir </> "PureScript.cc") $ B.fromStrict $(embedFile "pcc/include/purescript.cc")
129− writeTextFile (supportDir </> "map_key.hh") $ B.fromStrict $(embedFile "pcc/include/map_key.hh")
130− writeTextFile (supportDir </> "hash.hh") $ B.fromStrict $(embedFile "pcc/include/hash.hh")
129+ writeTextFile (supportDir </> "PureScript.hh") $ B.unpack $(embedFile "pcc/include/purescript.hh")
130+ writeTextFile (supportDir </> "PureScript.cc") $ B.unpack $(embedFile "pcc/include/purescript.cc")
131+ writeTextFile (supportDir </> "map_key.hh") $ B.unpack $(embedFile "pcc/include/map_key.hh")
132+ writeTextFile (supportDir </> "hash.hh") $ B.unpack $(embedFile "pcc/include/hash.hh")
131133
132134 when (requiresForeign m) $ do
133135 let inputPath = dropExtension $ getInputFile mn
@@ -158,13 +160,16 @@ buildMakeActions outputDir filePathMap usePrefix =
158160 exists <- doesFileExist path
159161 traverse (const $ getModificationTime path) $ guard exists
160162
161− readTextFile :: FilePath -> Make B.ByteString
162− readTextFile path = makeIO (const (P.SimpleErrorWrapper $ P.CannotReadFile path)) $ B.readFile path
163+ readTextFile :: FilePath -> Make String
164+ readTextFile path = makeIO (const (P.SimpleErrorWrapper $ P.CannotReadFile path)) $ readUTF8File path
163165
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 ()
165170 writeTextFile path text = makeIO (const (P.SimpleErrorWrapper $ P.CannotWriteFile path)) $ do
166171 mkdirp path
167− B.writeFile path text
172+ writeFile path text
168173 where
169174 mkdirp :: FilePath -> IO ()
170175 mkdirp = createDirectoryIfMissing True . takeDirectory
src/Language/PureScript/Pretty/Cpp.hs+3−2
@@ -52,7 +52,8 @@ literals = mkPattern' match
5252 match CppEndOfHeader = return []
5353 match (CppNumericLiteral (Left n)) = return $ show n ++ "L"
5454 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
5657 match (CppStringLiteralHash s) = return $ "HASH" ++ parens (string s)
5758 match (CppCharLiteral c) = return $ show c
5859 match (CppBooleanLiteral True) = return "true"
@@ -277,7 +278,7 @@ indexer :: Pattern PrinterState Cpp (String, Cpp)
277278indexer = mkPattern' match
278279 where
279280 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)
281282 match _ = mzero
282283
283284lam :: Pattern PrinterState Cpp ((String, [(String, Maybe CppType)], Maybe CppType), Cpp)