Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can invoke it from GHCi as hairyhum says.</p> <p>But if what you're really trying to do is to call it from a program ( perhaps you're in the same class as the person who asked this: <a href="https://stackoverflow.com/questions/15791213/dissecting-java-class-file-in-haskell">Dissecting java Class file in haskell</a>), then put it in the same directory as your main program, and at the top of the program, put:</p> <pre><code>import Parse </code></pre> <p>Then in your program, read the Java class file. You've probably seen something like this:</p> <pre><code>s &lt;- readFile "MyJava.class" </code></pre> <p>That would work <em>if</em> we wanted to read the contents of the file into a <code>String</code>. But the <code>parse</code> command expects a <code>ByteString</code>, so we need to use a different implementation of <code>readFile</code>. So at the top of your program, put:</p> <pre><code>import qualified Data.ByteString as BS </code></pre> <p>Now you can read the file like so:</p> <pre><code>s &lt;- BS.readFile "MyJava.class" </code></pre> <p>Now you have the class data, and can invoke the <code>parse</code> command. That should be enough information to help you finish that part of the assignment.</p> <hr> <p><strong>UPDATE</strong></p> <p>Let's look at the type signature for the function <code>parse</code> in the code you were given.</p> <pre><code>parse :: B.ByteString -&gt; JavaClass </code></pre> <p>So <code>parse</code> takes a <code>ByteString</code> and returns a <code>JavaClass</code>. Now let's look at the definition of <code>JavaClass</code>.</p> <pre><code>data JavaClass = JavaClass { classMinorVersion :: Word16 , classMajorVersion :: MajorVersion , classConstantPoolCount :: Word16 } deriving Show </code></pre> <p>So as written, all <code>parse</code>is going to give you is the minor version, major version, and the constant pool count. But if you analyse the code, you should be able to see how to modify it to get the additional information you want. I suggest you analyse that code in detail to understand how it works.</p> <hr> <p><strong>UPDATE 2</strong></p> <p>If you just want to try it out in GHCI, you could do something like this:</p> <pre><code>:load Parse :m + Data.ByteString classData &lt;- Data.ByteString.readFile "file.class" Parse.parse classData </code></pre> <p>Here's a GHCi session where I did exactly that.</p> <pre><code>ghci&gt; :load Parse [1 of 1] Compiling Parse ( Parse.hs, interpreted ) Ok, modules loaded: Parse. ghci&gt; :m + Data.ByteString ghci&gt; classData &lt;- Data.ByteString.readFile "file.class" Loading package array-0.4.0.1 ... linking ... done. Loading package deepseq-1.3.0.1 ... linking ... done. Loading package bytestring-0.10.0.2 ... linking ... done. ghci&gt; Parse.parse classData Loading package containers-0.5.0.0 ... linking ... done. Loading package binary-0.7.0.1 ... linking ... done. JavaClass {classMinorVersion = 3, classMajorVersion = JDK_1_1, classConstantPoolCount = 85} </code></pre> <p>But to take it to the next step, you would write a program to invoke <code>parse</code> as I've described above. Suppose your program is in a file called "MyProgram.hs". Then you can run it from the <em>command line</em> by typing <code>runghc MyProgram.hs</code>.</p> <p>I recommend reading Chapter 1 of <a href="http://book.realworldhaskell.org/read/getting-started.html" rel="nofollow noreferrer" title="Real World Haskell">Real World Haskell</a>.</p> <p><strong>EDIT:</strong> Changed "class" to "classData" because "class" is a Haskell keyword.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload