git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add unicode string literal rendering
commitba3f4b5fef2b8f40004655b05a0703eb2d2ea7d1 authorAndy Arvanitis <andyarvanitis@gmail.com> dateThu 2 Aug 2018, 23:07 (8 years ago) parent3885660
1 file changed+12−1
src/CodeGen/Cpp/Printer.hs+12−1
src/CodeGen/Cpp/Printer.hs+12−1
@@ -14,6 +14,7 @@ import Control.Arrow ((<+>))
1414import Control.Monad (forM, mzero)
1515import Control.Monad.State (StateT, evalStateT)
1616import Control.PatternArrows
17+import Data.Char (isAscii)
1718import Data.List ((\\))
1819import qualified Control.Arrow as A
1920
@@ -43,7 +44,14 @@ literals = mkPattern' match'
4344
4445 match :: (Emit gen) => AST -> StateT PrinterState Maybe gen
4546 match (NumericLiteral _ n) = return $ emit $ T.pack $ either show show n
46− match (StringLiteral _ s) = return $ emit $ prettyPrintString s
47+ match (StringLiteral _ s)
48+ | Just s' <- decodeString s = return $ emit $
49+ if T.all isAscii s'
50+ then stringLiteral s'
51+ else "u8" <> stringLiteral s'
52+ match (StringLiteral _ s)
53+ | Just s' <- decodeString s = return $ emit $ "u8" <> stringLiteral s'
54+ match (StringLiteral _ s) = error $ "Bad character in string literal " ++ show s
4755 match (BooleanLiteral _ True) = return $ emit "true"
4856 match (BooleanLiteral _ False) = return $ emit "false"
4957 -- match (ArrayLiteral _ []) = return $ emit "std::initializer_list<boxed>{}"
@@ -373,6 +381,9 @@ prettyPrintCpp' = A.runKleisli $ runPattern matchValue
373381prettyPrintCpp1 :: AST -> Text
374382prettyPrintCpp1 = maybe (internalError "Incomplete pattern") runPlainString . flip evalStateT (PrinterState 0) . prettyPrintCpp'
375383
384+stringLiteral :: Text -> Text
385+stringLiteral s = "\"" <> s <> "\""
386+
376387unbox :: Text -> AST -> Text
377388unbox _ v@(NumericLiteral{}) = prettyPrintCpp1 v
378389unbox _ v@(BooleanLiteral{}) = prettyPrintCpp1 v