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)
77fc553Andy Arvanitis
pcc/Main.hs+11−1
@@ -52,6 +52,7 @@ data PCCMakeOptions = PCCMakeOptions
52 { pccmInput :: [FilePath]
52 { pccmInput :: [FilePath]
53 , pccmOutputDir :: FilePath
53 , pccmOutputDir :: FilePath
54 , pccmOpts :: P.Options
54 , pccmOpts :: P.Options
55+ , pccmOtherOpts :: OtherOptions
55 , pccmUsePrefix :: Bool
56 , pccmUsePrefix :: Bool
56 , pccmJSONErrors :: Bool
57 , pccmJSONErrors :: Bool
57 , pccmXcode :: Bool
58 , pccmXcode :: Bool
@@ -88,7 +89,7 @@ compile PCCMakeOptions{..} = do
88 (makeErrors, makeWarnings) <- runMake pccmOpts $ do
89 (makeErrors, makeWarnings) <- runMake pccmOpts $ do
89 ms <- P.parseModulesFromFiles id moduleFiles
90 ms <- P.parseModulesFromFiles id moduleFiles
90 let filePathMap = M.fromList $ map (\(fp, P.Module _ _ mn _ _) -> (mn, Right fp)) ms
91 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
92 P.make makeActions (map snd ms)
93 P.make makeActions (map snd ms)
93 printWarningsAndErrors (P.optionsVerboseErrors pccmOpts) pccmJSONErrors makeWarnings makeErrors
94 printWarningsAndErrors (P.optionsVerboseErrors pccmOpts) pccmJSONErrors makeWarnings makeErrors
94 exitSuccess
95 exitSuccess
@@ -173,6 +174,11 @@ xcode = switch $
173 long "xcode"
174 long "xcode"
174 <> help "Generate support files for Xcode"
175 <> help "Generate support files for Xcode"
175
176
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+
176options :: Parser P.Options
182options :: Parser P.Options
177options = P.Options <$> noTco
183options = P.Options <$> noTco
178 <*> noMagicDo
184 <*> noMagicDo
@@ -183,10 +189,14 @@ options = P.Options <$> noTco
183 <*> sourceMaps
189 <*> sourceMaps
184 <*> dumpCoreFn
190 <*> dumpCoreFn
185
191
192+otherOptions :: Parser OtherOptions
193+otherOptions = OtherOptions <$> ucns
194+
186pccMakeOptions :: Parser PCCMakeOptions
195pccMakeOptions :: Parser PCCMakeOptions
187pccMakeOptions = PCCMakeOptions <$> many inputFile
196pccMakeOptions = PCCMakeOptions <$> many inputFile
188 <*> outputDirectory
197 <*> outputDirectory
189 <*> options
198 <*> options
199+ <*> otherOptions
190 <*> (not <$> noPrefix)
200 <*> (not <$> noPrefix)
191 <*> jsonErrors
201 <*> jsonErrors
192 <*> xcode
202 <*> xcode
pcc/Make.hs+4−2
@@ -23,6 +23,7 @@ module Make
23 ( Make(..)
23 ( Make(..)
24 , runMake
24 , runMake
25 , buildMakeActions
25 , buildMakeActions
26+ , CPP.OtherOptions(..)
26 ) where
27 ) where
27
28
28import Control.Monad
29import Control.Monad
@@ -57,8 +58,9 @@ makeIO f io = do
57buildMakeActions :: FilePath
58buildMakeActions :: FilePath
58 -> M.Map P.ModuleName (Either P.RebuildPolicy FilePath)
59 -> M.Map P.ModuleName (Either P.RebuildPolicy FilePath)
59 -> Bool
60 -> Bool
61+ -> CPP.OtherOptions
60 -> P.MakeActions Make
62 -> P.MakeActions Make
61−buildMakeActions outputDir filePathMap usePrefix =
63+buildMakeActions outputDir filePathMap usePrefix otherOpts =
62 P.MakeActions getInputTimestamp getOutputTimestamp readExterns codegen progress
64 P.MakeActions getInputTimestamp getOutputTimestamp readExterns codegen progress
63 where
65 where
64
66
@@ -102,7 +104,7 @@ buildMakeActions outputDir filePathMap usePrefix =
102 headerFile = addExtension fileBase headerExt
104 headerFile = addExtension fileBase headerExt
103 externsFile = outputDir </> filePath </> "externs.json"
105 externsFile = outputDir </> filePath </> "externs.json"
104 prefix = ["Generated by pcc version " ++ showVersion Paths.version | usePrefix]
106 prefix = ["Generated by pcc version " ++ showVersion Paths.version | usePrefix]
105− cpps <- CPP.moduleToCpp env m
107+ cpps <- CPP.moduleToCpp otherOpts env m
106 let (hdrs,srcs) = span (/= CPP.CppEndOfHeader) cpps
108 let (hdrs,srcs) = span (/= CPP.CppEndOfHeader) cpps
107 psrcs <- CPP.prettyPrintCpp <$> pure srcs
109 psrcs <- CPP.prettyPrintCpp <$> pure srcs
108 phdrs <- CPP.prettyPrintCpp <$> pure hdrs
110 phdrs <- CPP.prettyPrintCpp <$> pure hdrs
purescript.cabal+1
@@ -195,6 +195,7 @@ library
195 Language.PureScript.CodeGen.Cpp.Optimizer.Uncurry
195 Language.PureScript.CodeGen.Cpp.Optimizer.Uncurry
196 Language.PureScript.CodeGen.Cpp.Optimizer.Unused
196 Language.PureScript.CodeGen.Cpp.Optimizer.Unused
197 Language.PureScript.CodeGen.Cpp.Types
197 Language.PureScript.CodeGen.Cpp.Types
198+ Language.PureScript.CodeGen.Cpp.Unicode
198 Language.PureScript.Pretty.Cpp
199 Language.PureScript.Pretty.Cpp
199
200
200 Language.PureScript.Constants
201 Language.PureScript.Constants
src/Language/PureScript/CodeGen/Cpp.hs+15−3
@@ -23,6 +23,7 @@ module Language.PureScript.CodeGen.Cpp
23 ( module AST
23 ( module AST
24 , module Common
24 , module Common
25 , moduleToCpp
25 , moduleToCpp
26+ , OtherOptions(..)
26 , P.prettyPrintCpp
27 , P.prettyPrintCpp
27 ) where
28 ) where
28
29
@@ -44,6 +45,7 @@ import Language.PureScript.CodeGen.Cpp.File
44import Language.PureScript.CodeGen.Cpp.Optimizer
45import Language.PureScript.CodeGen.Cpp.Optimizer
45import Language.PureScript.CodeGen.Cpp.Optimizer.TCO
46import Language.PureScript.CodeGen.Cpp.Optimizer.TCO
46import Language.PureScript.CodeGen.Cpp.Types
47import Language.PureScript.CodeGen.Cpp.Types
48+import Language.PureScript.CodeGen.Cpp.Unicode
47import Language.PureScript.Comments
49import Language.PureScript.Comments
48import Language.PureScript.CoreFn
50import Language.PureScript.CoreFn
49import Language.PureScript.Names
51import Language.PureScript.Names
@@ -56,6 +58,10 @@ import qualified Language.PureScript.Environment as E
56import qualified Language.PureScript.Pretty.Cpp as P
58import qualified Language.PureScript.Pretty.Cpp as P
57import qualified Language.PureScript.Types as T
59import qualified Language.PureScript.Types as T
58
60
61+data OtherOptions = OtherOptions
62+ { optionsUseUCNs :: Bool
63+ } deriving Show
64+
59-- |
65-- |
60-- Generate code in the simplified C++1x intermediate representation for all declarations in a
66-- Generate code in the simplified C++1x intermediate representation for all declarations in a
61-- module.
67-- module.
@@ -64,9 +70,9 @@ import qualified Language.PureScript.Types as T
64moduleToCpp
70moduleToCpp
65 :: forall m.
71 :: forall m.
66 (Applicative m, Monad m, MonadReader Options m, MonadSupply m)
72 (Applicative m, Monad m, MonadReader Options m, MonadSupply m)
67− => E.Environment -> Module Ann -> m [Cpp]
73+ => OtherOptions -> E.Environment -> Module Ann -> m [Cpp]
68---------------------------------------------------------------------------------------------------
74---------------------------------------------------------------------------------------------------
69−moduleToCpp env (Module _ mn imps _ foreigns decls) = do
75+moduleToCpp otherOpts env (Module _ mn imps _ foreigns decls) = do
70 cppImports <-
76 cppImports <-
71 traverse (pure . runModuleName) . delete (ModuleName [ProperName C.prim]) . (\\ [mn]) $
77 traverse (pure . runModuleName) . delete (ModuleName [ProperName C.prim]) . (\\ [mn]) $
72 (snd <$> imps)
78 (snd <$> imps)
@@ -83,7 +89,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
83 E.dataConstructors env
89 E.dataConstructors env
84 cpps' <- traverse (optimize $ namesmap ++ datamap) cpps
90 cpps' <- traverse (optimize $ namesmap ++ datamap) cpps
85 foreignWrappers <- curriedForeigns
91 foreignWrappers <- curriedForeigns
86− let optimized = cpps' ++ foreignWrappers
92+ let optimized = (otherTransforms otherOpts <$> cpps') ++ foreignWrappers
87 let moduleHeader =
93 let moduleHeader =
88 fileBegin mn "HH" ++
94 fileBegin mn "HH" ++
89 P.linebreak ++
95 P.linebreak ++
@@ -917,3 +923,9 @@ filterInlineFuncs = everywhereOnCpp convert
917 shouldRemove CppLambda {} = True
923 shouldRemove CppLambda {} = True
918 shouldRemove _ = False
924 shouldRemove _ = False
919 convert cpp = cpp
925 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