Skip to content

Commit

Permalink
Merge pull request #143 from uta8a/130-remove-unnecessary-paren
Browse files Browse the repository at this point in the history
Remove unnecessary paren
  • Loading branch information
kmyk authored Aug 5, 2021
2 parents 3da0762 + 76d6a61 commit 6203047
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Jikka/CPlusPlus/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ formatExpr = \case
At -> case args of
[e1, e2] ->
let e1' = formatExpr' FunCallPrec e1
e2' = formatExpr' FunCallPrec e2
e2' = formatExpr' ParenPrec e2
in (e1' ++ "[" ++ e2' ++ "]", FunCallPrec)
_ -> error $ "Jikka.CPlusPlus.Language.Format.formatExpr: wrong number of arguments for subscription: " ++ show (length args)
Cast t -> call $ formatType t
Expand Down
89 changes: 60 additions & 29 deletions test/Jikka/CPlusPlus/FormatSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,64 @@ run'' :: Program -> [String]
run'' prog = dropWhile ("#include" `isPrefixOf`) (lines (run' prog))

spec :: Spec
spec = describe "run" $ do
it "works" $ do
let program =
Program
[ FunDef
TyInt64
(VarName "solve")
[(TyInt32, VarName "n")]
[ Declare TyInt64 (VarName "x") (DeclareCopy (Lit (LitInt64 0))),
For
TyInt32
(VarName "i")
(Lit (LitInt32 0))
(BinOp LessThan (Var (VarName "i")) (Var (VarName "n")))
(AssignIncr (LeftVar (VarName "i")))
[ Assign (AssignExpr AddAssign (LeftVar (VarName "x")) (Call (Cast TyInt64) [Var (VarName "i")]))
],
Return (Var (VarName "x"))
]
spec = do
describe "run" $ do
it "works" $ do
let program =
Program
[ FunDef
TyInt64
(VarName "solve")
[(TyInt32, VarName "n")]
[ Declare TyInt64 (VarName "x") (DeclareCopy (Lit (LitInt64 0))),
For
TyInt32
(VarName "i")
(Lit (LitInt32 0))
(BinOp LessThan (Var (VarName "i")) (Var (VarName "n")))
(AssignIncr (LeftVar (VarName "i")))
[ Assign (AssignExpr AddAssign (LeftVar (VarName "x")) (Call (Cast TyInt64) [Var (VarName "i")]))
],
Return (Var (VarName "x"))
]
]
let formatted =
[ "int64_t solve(int32_t n) {",
" int64_t x = 0;",
" for (int32_t i = 0; i < n; ++ i) {",
" x += int64_t(i);",
" }",
" return x;",
"}"
]
let formatted =
[ "int64_t solve(int32_t n) {",
" int64_t x = 0;",
" for (int32_t i = 0; i < n; ++ i) {",
" x += int64_t(i);",
" }",
" return x;",
"}"
]
run'' program `shouldBe` formatted
run'' program `shouldBe` formatted
describe "no unnecessary paren in index" $ do
it "works" $ do
let program =
Program
[ FunDef
TyInt64
(VarName "solve")
[(TyInt32, VarName "n"), (TyVector TyInt64, VarName "h")]
[ Declare TyInt64 (VarName "x") (DeclareCopy (Lit (LitInt64 0))),
For
TyInt32
(VarName "i")
(Lit (LitInt32 2))
(BinOp LessThan (Var (VarName "i")) (Var (VarName "n")))
(AssignIncr (LeftVar (VarName "i")))
[ Assign (AssignExpr AddAssign (LeftVar (VarName "x")) (Call At [Var (VarName "h"), BinOp Sub (Var (VarName "i")) (Lit (LitInt32 2))]))
],
Return (Var (VarName "x"))
]
]
let formatted =
[ "int64_t solve(int32_t n, std::vector<int64_t> h) {",
" int64_t x = 0;",
" for (int32_t i = 2; i < n; ++ i) {",
" x += h[i - 2];",
" }",
" return x;",
"}"
]
run'' program `shouldBe` formatted

0 comments on commit 6203047

Please sign in to comment.