gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - new switch for global/local VAR blocks; added display of default options...
[mjc2wsl.git] / src / com / quemaster / transformations / mjc2wsl / mjc2wsl.java
index 4522d65..26452e8 100644 (file)
@@ -17,8 +17,16 @@ package com.quemaster.transformations.mjc2wsl;
     You should have received a copy of the GNU General Public License
     along with mjc2wsl.  If not, see <http://www.gnu.org/licenses/>.
 */
-import java.io.*;
-import java.util.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Calendar;
+import java.util.Properties;
 
 import com.quemaster.transformations.TransMessages;
 
@@ -34,7 +42,7 @@ public class mjc2wsl{
        //default version name, used if the file is not found
        private static String versionN = "0.1.x";
 
-       private String versionFile = "version.properties";
+       private String versionFile = "/version.properties";
        
        private TransMessages messages = new TransMessages();
 
@@ -44,7 +52,9 @@ public class mjc2wsl{
                
        private boolean genPopPush=false;
        
-       private boolean genInlinePrint=false;
+       private boolean genInlinePrint = false;
+       
+       private boolean genLocalVars = true;
 
        /** Constant used for marking a regular comment from the original file */
        public static final char C_REG = ' ';
@@ -170,6 +180,9 @@ public class mjc2wsl{
 
                ret.append("\nBEGIN");
                ret.append("\nVAR <\n\t");
+               if (!genLocalVars){ 
+                       ret.append("\n\ttempa := 0, tempb :=0, tempres := 0,");
+               }
                ret.append("mjvm_locals := ARRAY(1,0),");
                ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0),");
                ret.append("\n\tmjvm_arrays := < >,");
@@ -216,17 +229,23 @@ public class mjc2wsl{
        }
 
        private String createStartVar(String... vars){
-               StringBuilder ret = new StringBuilder("VAR < ");
-               ret.append(vars[0] + " := 0");
-               for (int i=1; i<vars.length; i++)
-                               ret.append(", "+ vars[i] +" := 0");
-               ret.append(" > : ");
-               
-               return ret.toString();
+               if (genLocalVars) {
+                       StringBuilder ret = new StringBuilder("VAR < ");
+                       ret.append(vars[0] + " := 0");
+                       for (int i = 1; i < vars.length; i++)
+                               ret.append(", " + vars[i] + " := 0");
+                       ret.append(" > : ");
+
+                       return ret.toString();
+               }
+               return "";
        }
        
        private String createEndVar(){
-               return "ENDVAR;";
+               if (genLocalVars)
+                       return "ENDVAR;";
+               else
+                       return "";
        }
        
        private String createLocal(int i) {
@@ -735,12 +754,21 @@ public class mjc2wsl{
        }
 
        public void printHelpDirectives(){
-               System.out.println("Alternatives for code generation:");
-               System.out.println("  --genPopPush generate POP/PUSH instead of TAIL/HEAD");
-               System.out.println("  --genHeadTail generate TAIL/HEAD instead of POP/PUSH ");
+               System.out.println("Alternatives for code generation (* are the defaults):");
+               System.out.print(genPopPush?'*':' ');
+               System.out.println(" --genPopPush generate POP/PUSH instead of TAIL/HEAD");
+               System.out.print(!genPopPush?'*':' ');
+               System.out.println(" --genHeadTail generate TAIL/HEAD instead of POP/PUSH ");
                System.out.println();
-               System.out.println("  --genInlinePrint generate prints directly instead of procedure calls");
-               System.out.println("  --genProcedurePrint generate prints as custom procedure calls");
+               System.out.print(genInlinePrint?'*':' ');
+               System.out.println(" --genInlinePrint generate prints directly instead of procedure calls");
+               System.out.print(!genInlinePrint?'*':' ');
+               System.out.println(" --genProcedurePrint generate prints as custom procedure calls");
+               System.out.println();
+               System.out.print(genLocalVars?'*':' ');
+               System.out.println(" --genLocalVars generate local VAR block for temp variables");
+               System.out.print(!genLocalVars?'*':' ');
+               System.out.println(" --genGlobalVars do NOT generate local VAR block for temp variables");
        }
 
        public void printHelpHelp() {
@@ -819,15 +847,24 @@ public class mjc2wsl{
                                        genPopPush = false;
                                } else if (args[i].compareToIgnoreCase("--genProcedurePrint") == 0) {
                                        genInlinePrint = false;
+                               } else if (args[i].compareToIgnoreCase("--genLocalVars") == 0) {
+                                       genLocalVars = true;
+                               } else if (args[i].compareToIgnoreCase("--genGlobalVars") == 0) {
+                                       genLocalVars = false;
                                }
                                i++;
                        }
 
                        if (i >= args.length) {
-                               System.out.println("no filename supplied");
+                               System.err.println("no filename supplied");
                                System.exit(2);
                        }
-                       File f = new File(args[i]);
+
+                       Path p = FileSystems.getDefault().getPath(args[i]);
+                       if (!Files.exists(p)){
+                               System.err.println("input file does not exist");
+                               System.exit(1);
+                       }
 
                        if (i + 1 < args.length) {
                                try {
@@ -846,16 +883,18 @@ public class mjc2wsl{
                                        e.printStackTrace();
                                }
                        }
-                       if (f.exists()) {
-                               Calendar now = Calendar.getInstance();
-                               convertFile(f);
-                               long mili = Calendar.getInstance().getTimeInMillis()
-                                               - now.getTimeInMillis();
-                               System.out.println("conversion time:" + mili + " ms");
-                               messages.printMessageCounters();
-                               out.close();
-                       } else
-                               System.out.println("file does not exist");
+                       Calendar now = Calendar.getInstance();
+                       try {
+                               convertStream(Files.newInputStream(p));
+                       } catch (Exception e) {
+                               // TODO Auto-generated catch block
+                               e.printStackTrace();
+                       }
+                       long mili = Calendar.getInstance().getTimeInMillis()
+                                       - now.getTimeInMillis();
+                       System.out.println("conversion time:" + mili + " ms");
+                       messages.printMessageCounters();
+                       out.close();
                }
        }
        
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner