git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: minor cleanup of rendering of size() and empty()
commit204a4937f959a71bb0156c9cc168587553809e48 authorAndy Arvanitis <andyarvanitis@gmail.com> dateWed 21 Dec 2016, 15:53 (10 years ago) parent3942bea
3 files changed+19−11
src/Language/PureScript/CodeGen/Cpp.hs+2−2 src/Language/PureScript/CodeGen/Cpp/AST.hs+6−6 src/Language/PureScript/Pretty/Cpp.hs+11−3
src/Language/PureScript/CodeGen/Cpp.hs+2−2
@@ -684,10 +684,10 @@ moduleToCpp otherOpts env (Module _ mn imps _ foreigns decls) = do
684684 cpp <- go done 0 bs
685685 let cond =
686686 case length bs of
687− 0 -> CppBinary Dot (CppVar varName) (CppApp (CppVar "empty") [])
687+ 0 -> CppUnary Empty (CppVar varName)
688688 n -> CppBinary
689689 EqualTo
690− (CppBinary Dot (CppVar varName) (CppApp (CppVar "size") []))
690+ (CppUnary Size (CppVar varName))
691691 (CppNumericLiteral (Left (fromIntegral n)))
692692 return [CppIfElse cond (CppBlock cpp) Nothing]
693693 where
src/Language/PureScript/CodeGen/Cpp/AST.hs+6−6
@@ -51,9 +51,13 @@ data UnaryOperator
5151 --
5252 | Positive
5353 -- |
54− -- Constructor
54+ -- Get size
5555 --
56− | New
56+ | Size
57+ -- |
58+ -- Check if empty
59+ --
60+ | Empty
5761 deriving (Show, Eq)
5862
5963-- |
@@ -81,10 +85,6 @@ data BinaryOperator
8185 --
8286 | Modulus
8387 -- |
84− -- TODO: revisit this
85− --
86− | Dot
87− -- |
8888 -- Generic equality test
8989 --
9090 | EqualTo
src/Language/PureScript/Pretty/Cpp.hs+11−3
@@ -237,6 +237,14 @@ literals = mkPattern' match
237237 return . prettyPrintCpp1 $ CppApp (CppVar $ mapNS <> "::get" <> angles (prettyPrintCpp1 i)) [val]
238238 match (CppDataGet i val) =
239239 return . prettyPrintCpp1 $ CppApp (CppVar $ dataNS <> "::get" <> angles (prettyPrintCpp1 i)) [val]
240+ match (CppUnary op val) = return $ s <> "." <> f <> "()"
241+ where
242+ v = prettyPrintCpp1 val
243+ s | CppVar {} <- val = v
244+ | otherwise = parensT v
245+ f | Size <- op = "size"
246+ | Empty <- op = "empty"
247+ | otherwise = error $ "Incorrect match on unary " <> show op
240248 match (CppVar ident) = return $ renderName ident
241249 match (CppSymbol ident) = return $ "symbol" <> parensT (safeName ident)
242250 match (CppDefineSymbol ident) = return $ "define_symbol" <> parensT (safeName ident)
@@ -417,6 +425,8 @@ unary' op mkStr = Wrap match (<>)
417425 match :: Pattern PrinterState Cpp (Text, Cpp)
418426 match = mkPattern match'
419427 where
428+ match' (CppUnary Size _) = Nothing
429+ match' (CppUnary Empty _) = Nothing
420430 match' (CppUnary op' val)
421431 | op' == op = Just (mkStr val, val)
422432 match' _ = Nothing
@@ -483,7 +493,6 @@ prettyPrintCpp' = A.runKleisli $ runPattern matchValue
483493 OperatorTable [ [ Wrap accessor $ \prop val -> val <> "::" <> prop ]
484494 , [ Wrap indexer $ \index val -> val <> "[" <> index <> "]" ]
485495 , [ Wrap app $ \args val -> val <> parensT args ]
486− , [ unary New "new " ]
487496 , [ Wrap lam $ \(caps, args, rty) ret -> '[' `T.cons` caps <> "]"
488497 <> let args' = argstr <$> args in
489498 parensT (T.intercalate ", " args')
@@ -496,8 +505,7 @@ prettyPrintCpp' = A.runKleisli $ runPattern matchValue
496505 , negateOperator ]
497506 , [ binary Multiply " * "
498507 , binary Divide " / "
499− , binary Modulus " % "
500− , binary Dot "." ]
508+ , binary Modulus " % " ]
501509 , [ binary Add " + "
502510 , binary Subtract " - " ]
503511 , [ binary ShiftLeft " << "