git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: add pcc flag for using UCNs for unicode identifiers (mostly for gcc)
commit77fc553248f9dbacee6e3d903a98db7e25962946 authorAndy Arvanitis <andyarvanitis@gmail.com> dateSun 4 Dec 2016, 18:08 (10 years ago) parent7672d7a
5 files changed+91−6
pcc/Main.hs+11−1 pcc/Make.hs+4−2 purescript.cabal+1 src/Language/PureScript/CodeGen/Cpp.hs+15−3 src/Language/PureScript/CodeGen/Cpp/Unicode.hs+60
pcc/Main.hs+11−1
@@ -52,6 +52,7 @@ data PCCMakeOptions = PCCMakeOptions
5252 { pccmInput :: [FilePath]
5353 , pccmOutputDir :: FilePath
5454 , pccmOpts :: P.Options
55+ , pccmOtherOpts :: OtherOptions
5556 , pccmUsePrefix :: Bool
5657 , pccmJSONErrors :: Bool
5758 , pccmXcode :: Bool
@@ -88,7 +89,7 @@ compile PCCMakeOptions{..} = do
8889 (makeErrors, makeWarnings) <- runMake pccmOpts $ do
8990 ms <- P.parseModulesFromFiles id moduleFiles
9091 let filePathMap = M.fromList $ map (\(fp, P.Module _ _ mn _ _) -> (mn, Right fp)) ms
91− let makeActions = buildMakeActions pccmOutputDir filePathMap pccmUsePrefix
92+ let makeActions = buildMakeActions pccmOutputDir filePathMap pccmUsePrefix pccmOtherOpts
9293 P.make makeActions (map snd ms)
9394 printWarningsAndErrors (P.optionsVerboseErrors pccmOpts) pccmJSONErrors makeWarnings makeErrors
9495 exitSuccess
@@ -173,6 +174,11 @@ xcode = switch $
173174 long "xcode"
174175 <> help "Generate support files for Xcode"
175176
177+ucns :: Parser Bool
178+ucns = switch $
179+ long "use-ucns"
180+ <> help "Use UCNs for unicode identifiers (for compilers that do not fully support them, such as gcc)"
181+
176182options :: Parser P.Options
177183options = P.Options <$> noTco
178184 <*> noMagicDo
@@ -183,10 +189,14 @@ options = P.Options <$> noTco
183189 <*> sourceMaps
184190 <*> dumpCoreFn
185191
192+otherOptions :: Parser OtherOptions
193+otherOptions = OtherOptions <$> ucns
194+
186195pccMakeOptions :: Parser PCCMakeOptions
187196pccMakeOptions = PCCMakeOptions <$> many inputFile
188197 <*> outputDirectory
189198 <*> options
199+ <*> otherOptions
190200 <*> (not <$> noPrefix)
191201 <*> jsonErrors
192202 <*> xcode
pcc/Make.hs+4−2
@@ -23,6 +23,7 @@ module Make
2323 ( Make(..)
2424 , runMake
2525 , buildMakeActions
26+ , CPP.OtherOptions(..)
2627 ) where
2728
2829import Control.Monad
@@ -57,8 +58,9 @@ makeIO f io = do
5758buildMakeActions :: FilePath
5859 -> M.Map P.ModuleName (Either P.RebuildPolicy FilePath)
5960 -> Bool
61+ -> CPP.OtherOptions
6062 -> P.MakeActions Make
61−buildMakeActions outputDir filePathMap usePrefix =
63+buildMakeActions outputDir filePathMap usePrefix otherOpts =
6264 P.MakeActions getInputTimestamp getOutputTimestamp readExterns codegen progress
6365 where
6466
@@ -102,7 +104,7 @@ buildMakeActions outputDir filePathMap usePrefix =
102104 headerFile = addExtension fileBase headerExt
103105 externsFile = outputDir </> filePath </> "externs.json"
104106 prefix = ["Generated by pcc version " ++ showVersion Paths.version | usePrefix]
105− cpps <- CPP.moduleToCpp env m
107+ cpps <- CPP.moduleToCpp otherOpts env m
106108 let (hdrs,srcs) = span (/= CPP.CppEndOfHeader) cpps
107109 psrcs <- CPP.prettyPrintCpp <$> pure srcs
108110 phdrs <- CPP.prettyPrintCpp <$> pure hdrs
purescript.cabal+1
@@ -195,6 +195,7 @@ library
195195 Language.PureScript.CodeGen.Cpp.Optimizer.Uncurry
196196 Language.PureScript.CodeGen.Cpp.Optimizer.Unused
197197 Language.PureScript.CodeGen.Cpp.Types
198+ Language.PureScript.CodeGen.Cpp.Unicode
198199 Language.PureScript.Pretty.Cpp
199200
200201 Language.PureScript.Constants
src/Language/PureScript/CodeGen/Cpp.hs+15−3
@@ -23,6 +23,7 @@ module Language.PureScript.CodeGen.Cpp
2323 ( module AST
2424 , module Common
2525 , moduleToCpp
26+ , OtherOptions(..)
2627 , P.prettyPrintCpp
2728 ) where
2829
@@ -44,6 +45,7 @@ import Language.PureScript.CodeGen.Cpp.File
4445import Language.PureScript.CodeGen.Cpp.Optimizer
4546import Language.PureScript.CodeGen.Cpp.Optimizer.TCO
4647import Language.PureScript.CodeGen.Cpp.Types
48+import Language.PureScript.CodeGen.Cpp.Unicode
4749import Language.PureScript.Comments
4850import Language.PureScript.CoreFn
4951import Language.PureScript.Names
@@ -56,6 +58,10 @@ import qualified Language.PureScript.Environment as E
5658import qualified Language.PureScript.Pretty.Cpp as P
5759import qualified Language.PureScript.Types as T
5860
61+data OtherOptions = OtherOptions
62+ { optionsUseUCNs :: Bool
63+ } deriving Show
64+
5965-- |
6066-- Generate code in the simplified C++1x intermediate representation for all declarations in a
6167-- module.
@@ -64,9 +70,9 @@ import qualified Language.PureScript.Types as T
6470moduleToCpp
6571 :: forall m.
6672 (Applicative m, Monad m, MonadReader Options m, MonadSupply m)
67− => E.Environment -> Module Ann -> m [Cpp]
73+ => OtherOptions -> E.Environment -> Module Ann -> m [Cpp]
6874---------------------------------------------------------------------------------------------------
69−moduleToCpp env (Module _ mn imps _ foreigns decls) = do
75+moduleToCpp otherOpts env (Module _ mn imps _ foreigns decls) = do
7076 cppImports <-
7177 traverse (pure . runModuleName) . delete (ModuleName [ProperName C.prim]) . (\\ [mn]) $
7278 (snd <$> imps)
@@ -83,7 +89,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
8389 E.dataConstructors env
8490 cpps' <- traverse (optimize $ namesmap ++ datamap) cpps
8591 foreignWrappers <- curriedForeigns
86− let optimized = cpps' ++ foreignWrappers
92+ let optimized = (otherTransforms otherOpts <$> cpps') ++ foreignWrappers
8793 let moduleHeader =
8894 fileBegin mn "HH" ++
8995 P.linebreak ++
@@ -917,3 +923,9 @@ filterInlineFuncs = everywhereOnCpp convert
917923 shouldRemove CppLambda {} = True
918924 shouldRemove _ = False
919925 convert cpp = cpp
926+
927+---------------------------------------------------------------------------------------------------
928+otherTransforms :: OtherOptions -> Cpp -> Cpp
929+---------------------------------------------------------------------------------------------------
930+otherTransforms OtherOptions {optionsUseUCNs = True} = everywhereOnCpp unicodeToUCNs
931+otherTransforms _ = id
src/Language/PureScript/CodeGen/Cpp/Unicode.hsnew file+60
@@ -0,0 +1,60 @@
1+-----------------------------------------------------------------------------
2+--
3+-- Module : Language.PureScript.CodeGen.Unicode
4+-- Copyright : (c) 2016 Andy Arvanitis
5+-- License : MIT
6+--
7+-- Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
8+-- Stability : experimental
9+-- Portability :
10+--
11+-- |
12+-- Unicode support functions
13+--
14+-----------------------------------------------------------------------------
15+{-# LANGUAGE PatternGuards #-}
16+
17+module Language.PureScript.CodeGen.Cpp.Unicode
18+ ( unicodeToUCNs
19+ ) where
20+
21+import Prelude.Compat
22+import Data.Char (isAscii, ord)
23+import Text.Printf (printf)
24+
25+import Language.PureScript.CodeGen.Cpp.AST
26+
27+---------------------------------------------------------------------------------------------------
28+unicodeToUCNs :: Cpp -> Cpp
29+---------------------------------------------------------------------------------------------------
30+unicodeToUCNs (CppVar s) =
31+ CppVar (toUcns s)
32+unicodeToUCNs (CppSymbol s) =
33+ CppSymbol (toUcns s)
34+unicodeToUCNs (CppFunction s ss rty qs cpp) =
35+ CppFunction (toUcns s) ((\(a,t) -> (toUcns a, t)) <$> ss) rty qs cpp
36+unicodeToUCNs (CppLambda cps ss rty cpp) =
37+ CppLambda cps ((\(a,t) -> (toUcns a, t)) <$> ss) rty cpp
38+unicodeToUCNs (CppVariableIntroduction (ss,t) qs cpp) =
39+ CppVariableIntroduction (toUcns ss, t) qs cpp
40+unicodeToUCNs (CppEnum s t ss) =
41+ CppEnum (toUcns <$> s) t (toUcns <$> ss)
42+unicodeToUCNs (CppStruct s cpps) =
43+ CppStruct (toUcns s) cpps
44+unicodeToUCNs (CppNamespace s cpps) =
45+ CppNamespace (toUcns s) cpps
46+unicodeToUCNs (CppUseNamespace s) =
47+ CppUseNamespace (toUcns s)
48+unicodeToUCNs (CppTypeAlias (s1,ss) t s2) =
49+ CppTypeAlias (toUcns s1, (\(a,n) -> (toUcns a, n)) <$> ss) t (toUcns s2)
50+unicodeToUCNs cpp = cpp
51+
52+---------------------------------------------------------------------------------------------------
53+toUcns :: String -> String
54+---------------------------------------------------------------------------------------------------
55+toUcns s | any (not . isAscii) s = concatMap toUcn s
56+ where
57+ toUcn :: Char -> String
58+ toUcn c | isAscii c = [c]
59+ toUcn c = printf "\\U%08x" $ ord c
60+toUcns s = s