]> hydra-www.ietfng.org Git - dyna2/commitdiff
Code shuffling for debugging backends
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Wed, 12 Dec 2012 21:44:00 +0000 (16:44 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Wed, 12 Dec 2012 21:44:00 +0000 (16:44 -0500)
bin/utils.py
src/Dyna/Analysis/ANF.hs
src/Dyna/Backend/Debugging.hs [new file with mode: 0644]
src/Dyna/Backend/Python.hs
src/Dyna/XXX/Trifecta.hs

index 1d27421d7b9624e8094d6746cd5598059c724539..3f164c344469f76b1d9df4f8eaf184808ec505c1 100644 (file)
@@ -9,7 +9,7 @@ def toANF(code, f='/tmp/tmp.dyna'):
     with file(f, 'wb') as tmp:
         tmp.write(code)
     os.system('rm -f %s.anf' % f)  # clean up any existing ANF output
-    assert 0 == os.system("""ghc -isrc Dyna.Backend.Python -e 'normalizeFile "%s"' """ % f), \
+    assert 0 == os.system("""ghc -isrc Dyna.Backend.Debugging -e 'normalizeFile "%s"' """ % f), \
         'failed to convert file.'
     with file('%s.anf' % f) as h:
         return h.read()
index 66e7dd2c1da30857c546e2c453fcede64d1daf62..bde68921b30aa736775bd7b2269716b00d044b54 100644 (file)
@@ -92,7 +92,7 @@ import           Dyna.XXX.PPrint (valign)
 
 import qualified Data.Char as C
 
-import Dyna.XXX.Trifecta (renderSpan)
+import           Dyna.XXX.Trifecta (prettySpanLoc)
 
 ------------------------------------------------------------------------}}}
 -- Preliminaries                                                        {{{
@@ -346,7 +346,7 @@ normTerm_ c   ss (P.TFunctor f as) = do
                        x@(NTVar v) | v `elem` vs -> do
                             v' <- newAssign   "_x" (Left x)
                             return (vs,v':r)
-                       x@(NTVar v) -> do
+                       NTVar v -> do
                             return (v:vs,v:r)
                        _ -> do
                             v' <- newAssignNT "_x" x
@@ -404,19 +404,19 @@ runNormalize =
 ------------------------------------------------------------------------}}}
 -- Pretty Printer                                                       {{{
 
-printANF :: FRule -> Doc T.Effect
+printANF :: FRule -> Doc e
 printANF (FRule h a s result span
             (AS {as_evals = evals, as_assgn = assgn, as_unifs = unifs})) =
- --";;" <+> (renderSpan span) `above` 
- (
-   parens $ (pretty a)
+          text ";;" <+> prettySpanLoc span
+  `above`
+  ( parens $ (pretty a)
             <+> valign [ (pretty h)
                        , parens $ text "side"   <+> (valign $ map pretty s)
                        , parens $ text "evals"  <+> pev
                        , parens $ text "unifs"  <+> pun
                        , parens $ text "result" <+> (pretty result)
                        ]
-   )
+  ) <> line
   where
     pft :: FDT -> Doc e
     pft (fn,args)  = parens $ hsep $ (pretty fn : (map pretty args))
diff --git a/src/Dyna/Backend/Debugging.hs b/src/Dyna/Backend/Debugging.hs
new file mode 100644 (file)
index 0000000..eaa69d1
--- /dev/null
@@ -0,0 +1,39 @@
+---------------------------------------------------------------------------
+-- | A variety of debugging backends.
+--
+-- XXX Eventually, these may want such things kicked on by flags rather
+-- than invoked directly.
+
+-- Header material                                                      {{{
+module Dyna.Backend.Debugging where
+
+import           Control.Applicative ((<*))
+import           Control.Exception
+import           Dyna.Analysis.ANF
+import qualified Dyna.ParserHS.Parser                as P
+import           System.IO
+import           Text.PrettyPrint.Free               as PP
+import qualified Text.Trifecta                       as T
+
+------------------------------------------------------------------------}}}
+-- File to ANF                                                          {{{
+
+-- | Normalize all the rules in a file and emit S-exprs for the ANF
+-- normalized form.
+--
+-- NOTE: This is used by bin/prototype.py
+normalizeFile_ file oh = do
+  pr <- T.parseFromFileEx (P.dlines <* T.eof) file
+  case pr of
+    T.Failure td -> T.display td
+    T.Success rs -> mapM_ (PP.hPutDoc oh)
+                    $ map (\(P.LRule x T.:~ _) -> printANF $ normRule x) rs
+
+normalizeFile i o = bracket
+  (openFile o WriteMode)
+  (hClose)
+  $ normalizeFile_ i
+
+normalizeFileStdout file = normalizeFile_ file stdout
+
+------------------------------------------------------------------------}}}
index 780e7ff53653248dd7c04e1e08db87451910aac7..61d22f894e5a8ef485f22a07eff6dad46c372f49 100644 (file)
@@ -12,8 +12,7 @@
 
 module Dyna.Backend.Python where
 
-import Control.Applicative ((<*))
-
+import           Control.Applicative ((<*))
 import qualified Control.Arrow              as A
 import           Control.Exception
 import           Control.Monad
@@ -40,7 +39,7 @@ import           System.IO
 import           Text.PrettyPrint.Free
 import qualified Text.Trifecta              as T
 
-import Dyna.XXX.Trifecta (renderSpan)
+import Dyna.XXX.Trifecta (prettySpanLoc)
 
 
 ------------------------------------------------------------------------}}}
@@ -174,27 +173,22 @@ printPlan :: Handle
 printPlan fh fa mu (r, cost, dope) = do         -- display plan
   hPutStrLn fh $ "# --"
   displayIO fh $ prefixSD "# " $ renderPretty 1.0 100
-                 $ (renderSpan $ fr_span r) <> line
+                 $ (prettySpanLoc $ fr_span r) <> line
   hPutStrLn fh $ "# Cost: " ++ (show cost)
   displayIO fh $ renderPretty 1.0 100
                  $ py fa mu r dope <> line
   hPutStrLn fh ""
 
-
-
-processFile fileName = do
-  fh <- openFile (fileName ++ ".plan") WriteMode
-  processFile_ fileName fh
-  hClose fh
-
+processFile fileName = bracket
+  (openFile (fileName ++ ".plan") WriteMode)
+  hClose
+  $ processFile_ fileName
 
 processFileStdout fileName = do
   processFile_ fileName stdout
 
-
 processFile_ fileName fh = do
   pr <- T.parseFromFileEx (P.dlines <* T.eof) fileName
-
   case pr of
     T.Failure td -> T.display td
     T.Success rs ->
@@ -236,21 +230,6 @@ processFile_ fileName fh = do
   valVar  = "_v"
 
 
--- TEST: processFile "examples/cky.dyna"
-
-------------------------------------------------------------------------}}}
--- Experimental Residuals?                                              {{{
+-- TEST: processFileStdout "examples/cky.dyna"
 
--- | Normalize all the rules in a file and emit S-exprs for the ANF
--- normalized form.
---
--- NOTE: This is used by bin/prototype.py
-normalizeFile file = do
-    contents <- B.readFile file
-    writeFile (file ++ ".anf")
-              (show $ vcat (map (\(P.LRule x T.:~ _) ->
-                                printANF $ normRule x)
-                                (unsafeParse P.dlines contents))
-                      <> line)
-    return ()
 ------------------------------------------------------------------------}}}
index a6ba7abec6b43496b831fd82fb56f5f70cf61d09..113ef5d52c50f8bac326629510d99605aa1c75d0 100644 (file)
@@ -2,8 +2,9 @@
 {-# LANGUAGE OverloadedStrings #-}
 -- XXX contribute back to trifecta
 
+-- Header material                                                      {{{
 module Dyna.XXX.Trifecta (
-    identNL, pureSpanned, stringLiteralSQ, triInteract, renderSpan
+    identNL, pureSpanned, stringLiteralSQ, triInteract, prettySpanLoc
 ) where
 
 import           Control.Applicative
@@ -18,10 +19,13 @@ import           Text.Parser.Token.Highlight
 import           Text.Trifecta
 import           Text.Trifecta.Delta
 
-import           Text.PrettyPrint.Free ((<+>), above, indent)
+import qualified Text.PrettyPrint.Free               as PP
 
 -- import Debug.Trace
 
+------------------------------------------------------------------------}}}
+-- identNL                                                              {{{
+
 -- | Just like ident but without the "token $" prefix
 --
 -- belongs in Text.Parser.Token
@@ -32,6 +36,9 @@ identNL s = try $ do
   when (HashSet.member name (styleReserved s)) $ unexpected $ "reserved " ++ styleName s ++ " " ++ show name
   return name
 
+------------------------------------------------------------------------}}}
+-- String literal variants                                              {{{
+
 -- | Just like stringLiteral but with single quotes.
 --
 -- belongs in Text.Parser.Token
@@ -87,6 +94,8 @@ number :: TokenParsing m => Integer -> m Char -> m Integer
 number base baseDigit =
   foldl' (\x d -> base*x + toInteger (digitToInt d)) 0 <$> some baseDigit
 
+------------------------------------------------------------------------}}}
+-- pureSpanned                                                          {{{
 
 -- | Just like "pure" but right here in the parsing state
 --
@@ -94,6 +103,8 @@ number base baseDigit =
 pureSpanned :: DeltaParsing m => a -> m (Spanned a)
 pureSpanned r = (liftA (r :~) $ Span <$> position <*> position <*> line)
 
+------------------------------------------------------------------------}}}
+-- Interaction                                                          {{{
 
 -- | A multi-line interaction mechanism, for the REPL.
 --
@@ -119,11 +130,13 @@ triInteract p c s f i = loop (feed (BU.fromString i) $ stepParser (release dd *>
 
 
 ------------------------------------------------------------------------}}}
--- Utilities                                                            {{{
-
-renderSpan (Span s e bs) =
-       prettyTerm s
-   <+> "-"
-   <+> prettyTerm e
-   <+> ":"
-   `above` (indent 2 (prettyTerm $ rendering s bs))
+-- Diagnostic utilities                                                 {{{
+
+-- XXX I'd really like (but cannot seem to get) the ability to suppress the
+-- file name if it's the same in both cases.  Stripping Directed to Lines
+-- results in the lie of "(interactive)".  In any case, this function is
+-- here as a placeholder for doing the right thing.
+prettySpanLoc :: Span -> PP.Doc e
+prettySpanLoc (Span s e l) = PP.pretty s PP.<> PP.char '-' PP.<> PP.pretty e
+
+------------------------------------------------------------------------}}}