<!-- ant build file for cse minijava compiler project -->
<!-- hp, 1/10, 8/11, 1/16, 9/18. aj, 9/19, hp 4/20 -->

<!-- Possible future changes:
     - use symbolic variables for various class and path names
     - add options to interface better with internal eclipse compiler  -->

<project name="minijava" default="compile">

  <!-- build targets for compiler -->
  <!-- you might not need to change much of anything here except for compiler warnings -->

  <target name="init">
    <mkdir dir="build/classes" />
  </target>

  <target name="clean">
    <delete dir="build"/>
    <delete file="src/Parser/parser.java"/>
    <delete file="src/Parser/sym.java"/>
    <delete file="src/Scanner/scanner.java"/>
    <delete file="src/Scanner/scanner.java~"/>
  </target>

  <target name="check.parse.files">
    <uptodate targetfile="src/Parser/parser.java"
	      srcfile="src/Parser/minijava.cup"
	      property="parser.uptodate"/>
    <uptodate targetfile="src/Scanner/scanner.java"
	      srcfile="src/Scanner/minijava.jflex"
	      property="scanner.uptodate"/>
    <condition property="scanner.parser.uptodate">
      <and>
	<isset property="scanner.uptodate"/>
	<isset property="parser.uptodate"/>
      </and>
    </condition>
  </target>

  <target name="gen-parser" depends="check.parse.files"
	  unless="parser.uptodate">
    <java jar="lib/java-cup-11b.jar" fork="true" failonerror="true"
	  output="build/cup.out">
      <arg value="-locations"/>
      <arg value="-dump_states"/>
      <arg value="-dump_grammar"/>
      <arg value="-dump_tables"/>
      <arg value="-destdir"/> <arg value="src/Parser"/>
      <arg value="src/Parser/minijava.cup"/>
    </java>
  </target>

  <target name="gen-scanner" depends="gen-parser, check.parse.files"
	  unless="scanner.parser.uptodate">
    <java classname="jflex.Main" classpath="lib/jflex-full-1.8.2.jar"
	  fork="true" failonerror="true">
      <arg value="src/Scanner/minijava.jflex"/>
    </java>
  </target>

<!-- Remove comment on compilerarg line below to generate compiler warnings 
     for unchecked casts.  Even if the minijava code is clean, there may be
     warnings in java code generated by cup. -->

  <target name="compile" depends="init, gen-scanner">
    <javac srcdir="src" destdir="build/classes" classpath="lib/java-cup-11b.jar"
           debug="true"
           includeAntRuntime="false">
      <!-- <compilerarg value="-Xlint:unchecked"/> -->
    </javac>
  </target>

  <!-- targets for running demo programs -->

  <!-- run scanner and parser test programs on initial demo input
       located in SamplePrograms/Example.java. -->

  <target name="demo-scanner" depends="compile">
    <java classname="DemoScanner" classpath="build/classes;lib/java-cup-11b.jar"
          fork="true"
          input="SamplePrograms/Example.java">
      <assertions> <enable/> </assertions>
    </java>
  </target>

  <target name="demo-parser" depends="compile">
    <java classname="DemoParser" classpath="build/classes;lib/java-cup-11b.jar"
          fork="true"
          input="SamplePrograms/Example.java">
      <assertions> <enable/> </assertions>
    </java>
  </target>

  <target name="demo-interpreter" depends="compile">
    <java classname="Interpreter" classpath="build/classes;lib/java-cup-11b.jar"
          fork="true"
          input="SamplePrograms/Example.java">
      <assertions> <enable/> </assertions>
    </java>
  </target>

  <!-- targets for running test cases -->

  <!-- you may want to expand on what's here! -->

  <target name="compile-test" depends="compile">
    <javac srcdir="test/junit" destdir="build/classes"
           classpath="lib/hamcrest-core-1.3.jar;lib/junit-4.12.jar;lib/java-cup-11b.jar"
           debug="true"
           includeAntRuntime="false">
    </javac>
  </target>

  <!-- run all JUnit tests in the test/junit subdirectory -->
  <!-- this target runs all tests in test/junit, but you may find it easier 
       to break things up, e.g. by portion of the project.  to do so, copy
       this target as needed and modify the <fileset> tag to <include> only
       the test files you want. -->

  <target name="test" depends="compile-test">
    <junit>
      <classpath>
      <pathelement location="build/classes" />   
      <pathelement location="lib/hamcrest-core-1.3.jar"/> 
      <pathelement location="lib/junit-4.12.jar"/>
      <pathelement location="lib/java-cup-11b.jar"/>
    </classpath>
    <batchtest>
       <fileset dir="test/junit">
            <include name="**/*.java" />
       </fileset>
    </batchtest>
    <formatter type="plain" usefile="false"/>
    </junit>
  </target>

</project>
