totally hid all the ugly JVM stuff, created a pretty ADT
authoradam <adam@megacz.com>
Mon, 4 Sep 2006 06:06:27 +0000 (02:06 -0400)
committeradam <adam@megacz.com>
Mon, 4 Sep 2006 06:06:27 +0000 (02:06 -0400)
darcs-hash:20060904060627-5007d-57181d6412ed3ba43f1d02b7ba0818cbdf08f53a.gz

Makefile
src/HaskellDemo.hs
src/SBP.hs [new file with mode: 0644]
src/edu/berkeley/sbp/Tree.java

index 2ee8bb8..3a9e7b5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -152,11 +152,13 @@ with_jvmdir = --with-jvmdir=/System/Library/Frameworks/JavaVM.framework/
 jvmlink     = -framework JavaVM -optl -fexceptions
 patchmac    = patch -p0 < $(shell pwd)/misc/intel-mac-patch &&
 linkopts    =
+platlink    = -L$(prefix)/lib/ -L$(JAVA_HOME)/jre/lib/i386/ -L$(JAVA_HOME)/jre/lib/i386/client/
 else
 with_jvmdir = --with-jvmdir=$(JAVA_HOME)
 jvmlink     = -ljava -ljvm -lverify
 patchmac    =
 linkopts    = -optl "-Wl,-rpath,$(JAVA_HOME)/jre/lib/i386/" -optl "-Wl,-rpath,$(JAVA_HOME)/jre/lib/i386/client/"
+platlink    =
 endif
 
 prefix          = $(shell pwd)/prefix
@@ -164,10 +166,7 @@ jvm-bridge-site = umn.dl.sourceforge.net
 jvm-bridge-url  = http://$(jvm-bridge-site)/sourceforge/jvm-bridge/haskell-jvm-bridge-0.3.tar.gz
 
 #              The double -lHaskellJVMBridge is due to a circular link dep; the linux linker gets upset
-link            = -L$(prefix)/lib/ \
-                  -L$(JAVA_HOME)/jre/lib/i386/ \
-                  -L$(JAVA_HOME)/jre/lib/i386/client/ \
-                  -lstdc++  -lHaskellJVMBridge -lJVMBridge -lJVMInvocation -lHaskellJVMBridge $(jvmlink)
+link            = $(platlink) -lstdc++ -lHaskellJVMBridge -lJVMBridge -lJVMInvocation -lHaskellJVMBridge $(jvmlink)
 packages        = -package haskell98 -package base -package rts
 ghc             = ghc -fglasgow-exts $(packages) -i$(prefix)/imports/
 bin             = $(prefix)/bin/
@@ -216,11 +215,13 @@ bin/JVM_%.o: bin/Class_%.o
 
 edu_berkeley_sbp_misc_HaskellHelper_list = "edu.berkeley.sbp.misc.HaskellHelper\njava.lang.String\nedu.berkeley.sbp.Tree\njava.lang.Object"
 
-bin/HaskellDemo: src/HaskellDemo.hs \
-               bin/Class_java_lang_Object.o \
-               bin/Class_java_lang_String.o \
-               bin/Class_edu_berkeley_sbp_Tree.o \
-               bin/JVM_edu_berkeley_sbp_misc_HaskellHelper.o
-       cd bin; $(ghc) -c ../src/HaskellDemo.hs $(link) -o HaskellDemo.o
+bin/HaskellDemo: src/SBP.hs \
+                 src/HaskellDemo.hs \
+                bin/Class_java_lang_Object.o \
+                bin/Class_java_lang_String.o \
+                bin/Class_edu_berkeley_sbp_Tree.o \
+                bin/JVM_edu_berkeley_sbp_misc_HaskellHelper.o
+       cd bin; $(ghc) -c ../src/SBP.hs $(link) -o SBP.o
+       cd bin; $(ghc) -i../src/ -c ../src/HaskellDemo.hs $(link) -o HaskellDemo.o
        cd bin; for A in *.hs; do $(ghc) -c $$A $(link); done
        cd bin; $(ghc) $(linkopts) $(link) -o HaskellDemo *.o
index 7b545e6..eaac568 100644 (file)
@@ -1,48 +1,7 @@
--- This is written in Haskell.
-{--
-JVM-Bridge -- bridge from FP languages and others to the Java VM
-Copyright (C) 2001 Ashley Yakeley <ashley@semantic.org>
-
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Lesser General Public
-License as published by the Free Software Foundation; either
-version 2.1 of the License, or (at your option) any later version.
-
-This library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Lesser General Public License for more details.
-
-You should have received a copy of the GNU Lesser General Public
-License along with this library; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
---}
-
 module Main where
-        {
-        import Header_Java;
-        import Class_edu_berkeley_sbp_misc_HaskellHelper;
-        import Class_java_lang_Object;
-        import Class_java_lang_Class;
-        import Class_java_lang_String;
-
-        import Class_edu_berkeley_sbp_Tree;
-
-        import JVM_edu_berkeley_sbp_misc_HaskellHelper;
-        import TypedString;
-        import JVMBridge;
-        import JavaText;
-
-        main :: IO ();
-        main = runWithClasspath ["edu.berkeley.sbp.jar"]
-                ((do
-                    class_JHaskellHelper
-                    s1   <- new_JString_ArrayJchar $ toJavaString "tests/meta.g"
-                    s2   <- new_JString_ArrayJchar $ toJavaString "tests/testcase.g"
-                    tree <- main_JHaskellHelper_JString_JString (s1, s2)
-                    strx <- toString_JObject ((castTLRef tree) :: Jjava_lang_Object) ()
-                    str  <- getStringUTF strx
-                    callIO $ putStrLn $ showUTF8 str
-                    return ()
-                 ) :: JVM ())
-        }
+  import SBP;
+  main = do x <- SBP.parseFile ["edu.berkeley.sbp.jar"] "tests/meta.g" "tests/testcase.g"
+            putStr "\n"
+            putStr $ show x
+            putStr "\n"
+            putStr "\n"
diff --git a/src/SBP.hs b/src/SBP.hs
new file mode 100644 (file)
index 0000000..510a2d4
--- /dev/null
@@ -0,0 +1,74 @@
+--
+-- These bindings are highly experimental and subject to change
+-- without notice.  You've been warned.
+--
+
+module SBP
+ where
+  import Header_Java;
+  import Class_edu_berkeley_sbp_misc_HaskellHelper;
+  import Class_java_lang_Object;
+  import Class_java_lang_Class;
+  import Class_java_lang_String;
+  import Class_edu_berkeley_sbp_Tree;
+  import Header_edu_berkeley_sbp_Tree;
+  import JVM_edu_berkeley_sbp_misc_HaskellHelper;
+  import Header_edu_berkeley_sbp_misc_HaskellHelper;
+  import TypedString;
+  import JVMBridge;
+  import JavaText;
+  import JavaTypes;
+  import Data.Int;
+  import Invocation;
+
+  -- Why do I need this?
+  instance SubJavaClassMarker
+      Header_edu_berkeley_sbp_Tree.Class_Jedu_berkeley_sbp_Tree
+      Header_edu_berkeley_sbp_misc_HaskellHelper.Class_Jedu_berkeley_sbp_Tree
+
+  data Location = Location Int Int
+  data Region   = Region Location Location
+
+  data Tree     = Tree String [Tree] Region
+  instance Show Tree
+   where
+    show (Tree ""  []       region) = ""
+    show (Tree tag []       region) = tag
+    show (Tree tag children region) = tag ++ ":" ++ "{" ++ (unwords $ map show children) ++ "}"
+
+  nullRegion = (Region (Location 0 0) (Location 0 0))
+
+  parseFile ::
+   [String] ->   -- class path
+   String   ->   -- grammar *.g file
+   String   ->   -- file to be parsed
+   IO Tree
+
+  parseFile classPath grammarFile inputFile =
+     runJVM classPath
+        ((do class_JHaskellHelper
+             s1   <- new_JString_ArrayJchar $ toJavaString grammarFile
+             s2   <- new_JString_ArrayJchar $ toJavaString inputFile
+             tree <- main_JHaskellHelper_JString_JString (s1, s2)
+             t <- haskifyTree tree
+             return t
+          ) :: JVM Tree)
+
+  haskifyTree t = 
+    ((do class_JHaskellHelper
+         class_JTree
+         head <- getHead_JTree t ()
+         strx <- toString_JObject ((castTLRef head) :: Jjava_lang_Object) ()
+         str  <- getStringUTF strx
+         numChildren <- size_JTree t()
+         children    <- if numChildren == 0
+                        then do return []
+                        else do children <- mapM (\i -> child_JTree_Jint t ((fromIntegral i)::Int32))
+                                              $ take (fromIntegral numChildren)
+                                                $ iterate (+1) 0
+                                h        <- mapM (\c -> haskifyTree (castTLRef c)) children
+                                return h
+         return $ Tree (showUTF8 str) children nullRegion
+      ) :: JVM Tree)
+
+
index 558d234..d655aa4 100644 (file)
@@ -14,13 +14,10 @@ public class Tree<NodeType>
     implements Iterable<Tree<NodeType>> {
 
     private final Input.Region location;
-    private final NodeType     head;
+    private final NodeType     ihead;
     private final Tree<NodeType>[]    children;
     private final boolean      lift;
 
-    /** the element at the head of the tree */
-    public NodeType                 head()        { return head; }
-
     private Tree<NodeType> lifted() { return children[children.length-1]; }
 
     /** the number of children the tree has */
@@ -30,6 +27,10 @@ public class Tree<NodeType>
             : children.length;
     }
 
+    /** the element at the head of the tree */
+    public NodeType                 head()        { return ihead; }
+    public NodeType              getHead()        { return ihead; }
+
     /** the tree's children */
     public Iterable<Tree<NodeType>> children()    { return this; }
 
@@ -57,7 +58,7 @@ public class Tree<NodeType>
     /** package-private constructor, allows setting the "lift" bit */
     Tree(Input.Region loc, NodeType head, Tree<NodeType>[] children, boolean lift) {
         this.location = loc;
-        this.head = head;
+        this.ihead = head;
         this.lift = lift && children != null && children.length > 0;
         this.children = ArrayUtil.clone(children, Tree.class);
     }