Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary paren #143

Merged
merged 3 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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