<project name="lab3" default="build" basedir=".">

   <!--  Change the "YourToolPath" value below to the folder or directory
         where you have installed the CUP and JFlex jar file s-->
   
   <property 
       name="toolpath" 
       value="YourToolPath"
   />

	<property
		name="lab.jar"
		value="${user.name}lab3.jar"
	/>
	
   <!-- 
     The following  property causes Eclipse to use its own compiler 
     (rather than javac) from this build file.  
     If  you're not using Eclipse you should delete
     the following property from this file.
     -->
   <property 
       name="build.compiler" 
       value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
     

   <!-- You should not need to change anything below this line -->

   <property 
       name="cup.jar" 
       value="java-cup-11a.jar"/> 
   <property 
       name="jflex.jar" 
       value="JFlex.jar"/>

<target name="build" depends="genParser,genScanner,refreshScanner">
  <javac debug="true" nowarn="true" srcdir="." destdir="." classpath=".:${toolpath}/${cup.jar}:"/>
</target>

<target name="genParser.depend">
	<uptodate property="genParser.uptodate" targetfile="Parser.java" srcfile="jmm.cup"/>
	</target>

<target name="genParser" depends="genParser.depend" unless="genParser.uptodate">
	<java jar="${toolpath}/${cup.jar}" 
		fork="true" 
		failonerror="true"
		>
		<arg value="-parser"/> <arg value="Parser"/>
		<arg value="jmm.cup"/>
	</java>
	<java jar="${toolpath}/makesymstring.jar"
	     		input="sym.java"	
	        	output="symString.java"
	        	fork="true"
	        	failonerror="true"
	  		/>
</target>
	
<target name="refreshScanner" unless="scanner.fresh">
	<touch file="Yylex.java"/>
</target>
	
<target name="genScanner.depend">
	<condition property="genScanner.uptodate">
		<uptodate srcfile="jmm.jflex" targetfile="Yylex.java"/>
	</condition>
	<!--
	   The following condition tests whether the contents of symString has
	   changed, in which case we must force a recomile of Yylex.java by touching it.
	   
	   The reason for this is that the sym.java file contains final static constants that
	   are not mentioned by name in Yylex.class, but are instead "optimized" by using
	   their values directly.  The <javac> task has no idea that Yylex depends on sym.
	   
	   Why didn't we just test sym.java? It changes everytime CUP runs, thanks to
	   a time-sensitive comment at the top.  The symString.java file does not have
	   such a comment, but it changes in concert with sym.java.
	   
	   The symString.bak file is a record of symString.java the last time Yylex was
	   compiled.  If symString.bak != symString.java, then we should recompile Yylex.java
	   -->
	
	<condition property="scanner.fresh">
		<filesmatch file1="symString.java" file2="symString.bak"/>
	</condition>
</target>

<target name="genScanner" depends="genParser,genScanner.depend" unless="genScanner.uptodate">
	<java jar="${toolpath}/${jflex.jar}"
		fork="true"
		failonerror="true"
		>
		<arg value="-nobak"/>
		<arg value="jmm.jflex"/>
   </java>
	<copy file="symString.java" tofile="symString.bak" failonerror="false"/>
</target>
	

<target name="clean">
  <delete>
    <fileset dir="." includes="**/*.class"/>
  </delete>
  <delete file="sym.java"/>
  <delete file="symString.java"/>
  <delete file="Parser.java"/>
  <delete file="Yylex.java"/>
  <delete file="symString.bak"/>
  <delete file="${lab.jar}"/>
</target>

<target name="runMySolution">
	<fail message="Internal target, cannot be called from outside" unless="run.input"/>
	<echo message="Run of my solution for input ${run.input}"/>
	<java classname="main" input="${run.input}" fork="true" failonerror="true">
		<classpath>
			<pathelement path="."/>
			<pathelement location="${toolpath}/${cup.jar}"/>
		</classpath>
	</java>
</target>
	
<target name="runClassSolution">
	<fail message="Internal target, cannot be called from outside" unless="run.input"/>
	<echo message="Run of class solution for input ${run.input}"/>
	<java jar="solved.jar" input="${run.input}" fork="true" failonerror="true">
	</java>
</target>
		

<target name="runTestsOnTarget" description="Run my solution on testfiles">
	<fail message="Internal target, cannot be called from outside" unless="run.target"/>
	<antcall target="${run.target}">
			<param name="run.input" value="TestFiles/good1"/>
	</antcall>
	<antcall target="${run.target}">
			<param name="run.input" value="TestFiles/good2"/>
	</antcall>
	<antcall target="${run.target}">
			<param name="run.input" value="TestFiles/good3"/>
	</antcall>
	<antcall target="${run.target}">
			<param name="run.input" value="TestFiles/good4"/>
	</antcall>
		<antcall target="${run.target}">
				<param name="run.input" value="TestFiles/good5"/>
		</antcall>
	<antcall target="${run.target}">
			<param name="run.input" value="TestFiles/good6"/>
	</antcall>
	<antcall target="${run.target}">
			<param name="run.input" value="TestFiles/good7"/>
	</antcall>

</target>

<target name="testMySolution" description="Run all tests using my code" depends="build">
	<antcall target="runTestsOnTarget">
		<param name="run.target" value="runMySolution"/>
	</antcall>
</target>

<target name="testClassSolution" description="Run all tests using my code">
	<antcall target="runTestsOnTarget">
		<param name="run.target" value="runClassSolution"/>
	</antcall>
</target>
		

<!-- target name="jar" depends="clean,build">
	<delete file="${lab.jar}"/>
	  
  	<jar destfile="${lab.jar}">
		<fileset dir="." includes="**/*.class"/>
		<zipfileset src="${toolpath}/${cup.jar}" includes="java_cup/runtime/**"/>
  		<manifest>
	        <attribute name="Built-By" value="${user.name}"/>
	        <attribute name="Main-Class" value="main"/>
	    </manifest>
  </jar>
</target -->
</project>

