X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2FSBP.lhs;fp=src%2FSBP.hs;h=61a30a53b080902907a1760cf5930af03e5dd437;hp=89fd9ca98e85558bd30ca7d42e5912235070a4dc;hb=e84029a8b861075d6d0ed5040f919b2e4da4c98f;hpb=a8478f5ddfbfbc8d910d09f27163cbd55752d3b6 diff --git a/src/SBP.hs b/src/SBP.lhs similarity index 50% rename from src/SBP.hs rename to src/SBP.lhs index 89fd9ca..61a30a5 100644 --- a/src/SBP.hs +++ b/src/SBP.lhs @@ -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}