Skip to content

Commit 400dedf

Browse files
committed
[update] SExp.fmtPrint を削除して SExp の Show 実装へ
1 parent 80f2a98 commit 400dedf

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

app/main/src/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Prelude hiding (lex)
66
import Copager2.Prelude
77
import Copager2.Lex.RegexLex (lex)
88
import Copager2.Parse.LR.Common.Driver (parse)
9-
import Copager2.IR.SExp (SExp, consume, fmtPrint)
9+
import Copager2.IR.SExp (SExp, consume)
1010

1111
import Language.Arithmetic (ArithmeticToken, ArithmeticRule)
1212

@@ -15,7 +15,7 @@ main = do
1515
input <- getLine
1616
result <- runProcessor mkProcessor input
1717
case result of
18-
Right sexp -> fmtPrint sexp
18+
Right sexp -> print sexp
1919
Left err -> putStrLn $ "error: " ++ show err
2020

2121
mkProcessor :: ProcessorT IO ArithmeticToken ArithmeticRule (SExp _ _)

lib/copager2/src/Copager2/IR/SExp.hs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ module Copager2.IR.SExp
33
, feed
44
, build
55
, consume
6-
, fmtPrint
76
) where
87

8+
import Data.Function ((&))
9+
910
import Copager2.Core.Process (ProcessT, foldProcess)
1011
import Copager2.Core.Progress (Progress(..))
1112
import Copager2.Core.State (applyBuilding, updateBuilding)
@@ -19,7 +20,13 @@ data SExp ts rs
1920
= Empty
2021
| Atom (Token ts)
2122
| List (rs, [SExp ts rs])
22-
deriving Show
23+
24+
instance (Show ts, Show rs) => Show (SExp ts rs) where
25+
show Empty = ""
26+
show (Atom token) = "\"" ++ raw token ++ "\""
27+
show (List (rule, rhs)) =
28+
let formattedRhs = rhs & map show
29+
in "(" ++ show rule ++ " " ++ unwords formattedRhs ++ ")"
2330

2431
newtype SExpStack ts rs = SExpStack [SExp ts rs]
2532

@@ -54,17 +61,3 @@ consume events = foldProcess events $ \event -> do
5461
Accept -> build
5562
Error err -> return $ Fail err
5663
_ -> feed event
57-
58-
fmtPrint :: (Show ts, Show rs) => SExp ts rs -> IO ()
59-
fmtPrint = fmtPrint' 0
60-
61-
fmtPrint' :: (Show ts, Show rs) => Int -> SExp ts rs -> IO ()
62-
fmtPrint' indent (List (rule, [Atom token])) = do
63-
indentPrint indent ("- " ++ show rule ++ ": " ++ raw token)
64-
fmtPrint' indent (List (rule, rhs)) = do
65-
indentPrint indent ("- " ++ show rule)
66-
mapM_ (fmtPrint' (indent + 2)) rhs
67-
fmtPrint' _ _ = return ()
68-
69-
indentPrint :: Int -> String -> IO ()
70-
indentPrint indent str = putStrLn $ replicate indent ' ' ++ str

0 commit comments

Comments
 (0)