summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2018-02-07 17:38:48 +0100
committerCamil Staps2018-02-07 17:38:48 +0100
commita58bf7e338b2ed7a42c1d33d7854eb59550a2ee8 (patch)
tree35b03c64ce5c7e94f97703e5d4bb68ced0151083
parentCorrectly parsing the example (diff)
Integrate pretty printer
-rw-r--r--src/Main.hs16
-rw-r--r--src/SPL/PrettyPrinter.hs23
2 files changed, 18 insertions, 21 deletions
diff --git a/src/Main.hs b/src/Main.hs
index 71734c4..15a97fa 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -8,15 +8,15 @@ import Text.Parsec.Error (ParseError)
import SPL.Syntax
import SPL.Lex
import SPL.Parse
+import SPL.PrettyPrinter
main :: IO ()
main = do
contents <- readFile "test/example1.spl"
- putStrLn $ (show . lex') contents
- putStrLn $ (show . result) contents
- where
- lex' :: String -> Maybe [Token]
- lex' = lex
-
- result :: String -> Maybe (Either ParseError Program)
- result c = parse <$> lex c
+ case lex contents of
+ Nothing -> putStrLn "Failed to lex"
+ Just tks -> case parse tks of
+ Left e -> do
+ putStrLn $ "Failed to parse (" ++ show e ++ "). Tokens were:"
+ putStrLn $ show tks
+ Right pgm -> putStrLn $ prettyPrint pgm
diff --git a/src/SPL/PrettyPrinter.hs b/src/SPL/PrettyPrinter.hs
index d711203..6d55d1b 100644
--- a/src/SPL/PrettyPrinter.hs
+++ b/src/SPL/PrettyPrinter.hs
@@ -10,6 +10,9 @@ import Control.Monad.State.Lazy
import Control.Monad.Identity
import Data.Char
+prettyPrint :: Pprint t => t -> String
+prettyPrint t = result $ execState (pprint t) (St 0 "")
+
-- For Extensibility
data St = St
{ indent :: Int
@@ -37,6 +40,7 @@ instance Pprint Function where
Just x -> pprint x
pprint ") :: "
pprint $ fvars f
+ pprint "("
pprint $ fname f
instance Pprint Variable where
@@ -53,6 +57,7 @@ instance Pprint Type where
pprint TInt = pprint "Int"
pprint TBool = pprint "Bool"
pprint TChar = pprint "Char"
+ pprint TVoid = pprint "Void"
pprint (TList t) = do
pprint "]"
pprint t
@@ -67,6 +72,7 @@ instance Pprint Type where
pprint ts
pprint "->"
pprint t
+ pprint (TVar v) = pprint v
instance Pprint Statement where
pprint (If expr smt1 (Just smt2)) = do
@@ -89,8 +95,9 @@ instance Pprint Statement where
pprint "){"
pprint expr
pprint "while ("
- pprint (Assign name expr) = do
+ pprint (Assign name fields expr) = do
pprint expr
+ pprint fields
pprint " = "
pprint name
pprint (Eval expr) = do
@@ -108,6 +115,7 @@ instance Pprint Statement where
pprint _ = error "Cannot pprint unknow statement"
instance Pprint Expression where
+ pprint (Var v) = pprint v
pprint (Field name field) = do
pprint field
pprint name
@@ -172,18 +180,7 @@ instance Pprint Literal where
pprint _ = error "Cannot pprint unknown literal"
instance Pprint a => Pprint [a] where
- pprint xs = mapM_ pprint xs
+ pprint = mapM_ pprint . reverse
instance Pprint Char where
pprint c = modify (\st -> st { result = (c : result st) })
-
--- prettyPrint :: [] -> String
--- prettyPrint ts = prettyPrint` ts newState
--- where
--- newState :: State
--- newState = State {indent = 0}
---
--- prettyPrint` :: [Token] -> Print
--- prettyPrint` [] = \s ->
--- prettyPrint` (t:ts) =
-