summary patch for Nov->Jan work
[sbp.git] / src / SBP.lhs
similarity index 50%
rename from src/SBP.hs
rename to src/SBP.lhs
index 89fd9ca..61a30a5 100644 (file)
@@ -1,10 +1,93 @@
+\begin{code}
 --
 -- These bindings are highly experimental and subject to change
 -- without notice.  You've been warned.
 --
+module SBP(Tree(Tree),Location(Location),Region(Region),parseFile,prettyPrintTree)
+ where
+
+#if defined(java_HOST_OS)
+
+#define CONCAT(x,y) x/**/y
+#define DEFINE_OBJECT(s,name) \
+data CONCAT(name,_); \
+type name = Object CONCAT(name,_); \
+foreign import jvm s CONCAT(_,name) :: JClass; \
+instance JType_ CONCAT(name,_) where jClass_ _ = CONCAT(_,name);
 
-module SBP
+import Foreign
+import Foreign.Java
+import Text.PrettyPrint.HughesPJ
+
+data Location = Location Int Int
+data Region   = Region Location Location
+
+data Tree     = Tree String [Tree] Region
+instance Show Tree
  where
+  show t@(Tree _ _ _) = show $ prettyPrintTree $ t
+
+coalesceFlatHeadlessNodes t@(Tree s children r)
+  | s==[], flat t = Tree (concat $ map (\(Tree s _ _) -> s) children) [] r
+  | otherwise     = Tree s (map coalesceFlatHeadlessNodes children) r
+ where
+  flat (Tree _ children _) = not (any id $ map notFlatComponent children)
+  notFlatComponent (Tree _ [] _) = False
+  notFlatComponent (Tree _ _  _) = True
+
+prettyPrintTree (Tree "" []       _) = empty
+prettyPrintTree (Tree s  []       _) = text s
+prettyPrintTree t@(Tree s children _)
+  | s==[]     = (text "{") <+> ((prettyPrintTreeList children) <+> (text "}"))
+  | otherwise = ((text s) <> (text ":")) <+> prettyPrintTreeList children
+   where
+    prettyPrintTreeList children = (vcat $ map prettyPrintTree children)
+
+nullRegion = (Region (Location 0 0) (Location 0 0))
+
+foreign import jvm type "edu.berkeley.sbp.Tree" JTree#
+data JTree = JTree JTree#
+
+foreign import jvm safe "edu.berkeley.sbp.misc.RegressionTests.main" regressionTests :: IO ()
+foreign import jvm safe "edu.berkeley.sbp.misc.HaskellHelper.help" haskellHelper :: JString -> JString -> IO JTree
+foreign import jvm safe "edu.berkeley.sbp.misc.HaskellHelper.isNull" isNull :: (Object a) -> IO Bool
+foreign import jvm safe "getHead" getHead :: JTree -> IO (Object a)
+foreign import jvm safe "child" getChild :: JTree -> Int32 -> IO JTree
+foreign import jvm safe "size" size :: JTree -> IO Int32
+foreign import jvm safe "toString" jtoString :: (Object a) -> IO JString
+
+toString o  = do isn <- isNull o
+                 if isn then return ""
+                        else do str <- jtoString o
+                                return (unpackJString str)
+
+         
+haskify :: JTree -> IO Tree
+haskify t =
+  do head <- getHead t
+     str  <- toString head
+     numChildren <- size t
+     children    <- if numChildren == 0
+                        then do return []
+                        else do children <- mapM (\i -> getChild t i)
+                                              $ take (fromIntegral numChildren)
+                                                $ iterate (+1) 0
+                                h        <- mapM haskify children
+                                return h
+     return $ Tree str children nullRegion
+
+parseFile ::
+ String   ->   -- grammar *.g file
+ String   ->   -- file to be parsed
+ IO Tree
+
+parseFile g f = do g' <- packJString g
+                   f' <- packJString f
+                   tree <- haskellHelper g' f'
+                   x <- haskify tree
+                   return x
+
+#else
   import Header_Java;
   import Class_edu_berkeley_sbp_misc_HaskellHelper;
   import Class_java_lang_Object;
@@ -86,4 +169,5 @@ module SBP
          return $ Tree str children nullRegion
       ) :: JVM Tree)
 
-
+#endif
+\end{code}