gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - new option to toggle locals as array or separate
[mjc2wsl.git] / src / com / quemaster / transformations / mjc2wsl / mjc2wsl.java
index f5bb3ea..233d5ae 100644 (file)
@@ -1,6 +1,6 @@
 package com.quemaster.transformations.mjc2wsl;
 /*
-   Copyright (C) 2014  Doni Pracner
+   Copyright (C) 2014,2015, 2016  Doni Pracner
    
     This file is part of mjc2wsl.
 
@@ -40,9 +40,9 @@ import com.quemaster.transformations.TransMessages;
  */
 public class mjc2wsl{
        //default version name, used if the file is not found
-       private static String versionN = "0.1.x";
+       private static String versionN = "v0.2.x";
 
-       private String versionFile = "/version.properties";
+       private String versionFile = "version.properties";
        
        private TransMessages messages = new TransMessages();
 
@@ -56,6 +56,9 @@ public class mjc2wsl{
        
        private boolean genLocalVars = true;
 
+       // make an array for all local variables OTHERWISE make them separate
+       private boolean genLocalsAsArray = true;
+
        /** Constant used for marking a regular comment from the original file */
        public static final char C_REG = ' ';
        /**
@@ -137,8 +140,9 @@ public class mjc2wsl{
                        versionData = new Properties();
                        try {
                                versionData.load(getClass().getResourceAsStream(versionFile));
-                       } catch (IOException e) {
-                               e.printStackTrace();
+                       } catch (IOException | NullPointerException e) {
+                               // it doesn't matter
+                               //e.printStackTrace();
                        }
                }
                String ver = versionData.getProperty("version");
@@ -176,7 +180,7 @@ public class mjc2wsl{
        public String createStandardStart(int numWords){
                StringBuilder ret = new StringBuilder(
                        "C:\" This file automatically converted from microjava bytecode\";\n"
-                       +"C:\" with mjc2wsl "+getVersion()+"\";\n");
+                       +"C:\" with mjc2wsl "+getVersion()+"\";\n");
 
                ret.append("\nBEGIN");
                ret.append("\nVAR <\n\t");
@@ -184,7 +188,10 @@ public class mjc2wsl{
                        ret.append("\n\ttempa := 0, tempb :=0, tempres := 0,");
                } else
                        ret.append("\n\tmjvm_flag_jump := 0,");
-               ret.append("mjvm_locals := ARRAY(1,0),");
+
+               if (genLocalsAsArray)
+                       ret.append("mjvm_locals := ARRAY(1,0),");
+
                ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0),");
                ret.append("\n\tmjvm_arrays := < >,");
                ret.append("\n\tmjvm_objects := < >,");
@@ -206,22 +213,17 @@ public class mjc2wsl{
                ret.append("\nWHERE\n");
                
                ret.append("\nFUNCT CHR(num) ==:\n");
-               ret.append("\t@List_To_String(< num >)\n");
+               ret.append("\t(@List_To_String(< num >))\n");
                ret.append("END\n");
 
                if (!genInlinePrint) {
                                ret.append("\nPROC Print_MJ(val, format VAR)==\n");
-                               ret.append(createComment("print spacing", C_SPEC));
-                               ret.append("\n\tIF format>1 THEN\n\t\tFOR i:=1 TO ");
-                               ret.append("MAX(0, format-SLENGTH(@String(tempb))) STEP 1 DO PRINFLUSH(\" \") OD\n");
-                               ret.append("\tFI;\n\tPRINFLUSH(val)\nEND\n");
+                               ret.append("PRINFLUSH(@Format(format, val ))");
+                               ret.append("\nEND\n");
 
                                ret.append("\nPROC Print_MJ_CHAR(val, format VAR)==\n");
-                               ret.append(createComment("print spacing", C_SPEC));
-                               ret.append("\n\tIF format>1 THEN\n\t\tFOR i:=2 TO ");
-                               ret.append("format STEP 1 DO PRINFLUSH(\" \") OD\n");
-                               ret.append("\tFI;\n\tPRINFLUSH(CHR(val))\n");
-                               ret.append("END\n");
+                               ret.append("PRINFLUSH(@Format(format, CHR(val)))");
+                               ret.append("\nEND\n");
                }
 
                ret.append("\nEND");
@@ -250,7 +252,10 @@ public class mjc2wsl{
        
        private String createLocal(int i) {
                // arrays start at 1 in WSL, so we need an offset
-               return "mjvm_locals[" + (i + 1) + "]";
+               if (genLocalsAsArray)
+                       return "mjvm_locals[" + (i + 1) + "]";
+               else
+                       return "mjvm_locals_" + i;
        }
 
        private String createStatic(int i) {
@@ -358,6 +363,10 @@ public class mjc2wsl{
                
                prl(createStandardStart(mjInput.getNumberOfWords(this)));
                prl("SKIP;\n ACTIONS a" + (14 + mjInput.getMainAdr(this)) + " :");
+
+               // the number of Locals for procedures; need to remember it for exits
+               int numberOfLocals = 0;
+
                int op = mjInput.get();
                while (op >= 0) {
                        prl(" a" + mjInput.getCounter() + " ==");
@@ -366,7 +375,7 @@ public class mjc2wsl{
                        if (genPrintForEachAddress) {
                                prl("PRINT(\"a" + mjInput.getCounter() + "\");");
                                if (genPauseAfterEachAddress)
-                                       prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
+                                       prl("@Read_Line_Proc(VAR debug_disposable_string, Standard_Input_Port);");
                        }
                        switch (op) {
                        case load: {
@@ -639,27 +648,46 @@ public class mjc2wsl{
                        case enter: {
                                int parameters = mjInput.get();
 
-                               int locals = mjInput.get();
-                               prl(createToMStack("mjvm_locals"));
-                               prl("mjvm_locals := ARRAY(" + locals + ",0);");
+                               numberOfLocals = mjInput.get();
+                               if (genLocalsAsArray) {
+                                       prl(createToMStack("mjvm_locals"));
+                                       prl("mjvm_locals := ARRAY(" + numberOfLocals + ",0);");
+                               } else {
+                                       // TODO maybe we should generate VAR block for these somewhere
+                                       for (int i = 0; i < numberOfLocals; i++) {
+                                               prl(createToMStack(createLocal(i)));
+                                       }
+                               }
+
                                for (int i = parameters - 1; i >= 0; i--)
                                        prl(createFromEStack(createLocal(i)));
                                break;
                        }
                        case exit: {
-                               prl(createFromMStack("mjvm_locals"));
+                               if (genLocalsAsArray) {
+                                       prl(createFromMStack("mjvm_locals"));
+                               } else {
+                                       // there are as many locals as defined in the last enter
+                                       for (int i = numberOfLocals -1; i >= 0; i--)
+                                               prl(createFromMStack(createLocal(i)));
+                               }
                                break;
                        }
 
                        // read, print
                        case bread: {
-                               // TODO make it a char for read
-                               messages.message("char is read like a number", TransMessages.M_WAR);
-                               prl(createComment("char is read like a number", C_SPEC));
+                               // TODO maybe we'll need a bufer for multi chars!
+                               prl(createStartVar("tempa"));
+                               prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
+                               prl("tempa := @String_To_List(tempa)[1];");
+                               prl(createToEStack("tempa"));
+                               prl(createEndVar());
+                               break;
                        }
                        case read: {
                                prl(createStartVar("tempa"));
-                               prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
+                               prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
+                               prl("tempa := @String_To_Num(tempa);");
                                prl(createToEStack("tempa"));
                                prl(createEndVar());
                                break;
@@ -671,7 +699,7 @@ public class mjc2wsl{
                                prl(createTopTwoEStack());
                                if (genInlinePrint){
                                        prl(createComment("print spacing and transformation",C_SPEC));
-                                       prl("PRINFLUSH(SUBSTR(\"          \", 0, MIN(10, MAX(0,tempa-1))), @List_To_String(< tempb >));");
+                                       prl("PRINFLUSH(@Format(tempa, @List_To_String(< tempb >)));");
                                } else
                                        prl("Print_MJ_CHAR(tempb,tempa);");
                                prl(createEndVar());
@@ -683,7 +711,7 @@ public class mjc2wsl{
                                prl(createTopTwoEStack());
                                if (genInlinePrint){
                                        prl(createComment("print spacing",C_SPEC));
-                                       prl("PRINFLUSH(SUBSTR(\"          \", 0, MIN(10, MAX(0, tempa-SLENGTH(@String(tempb))))), tempb);");
+                                       prl("PRINFLUSH(@Format(tempa,tempb));");
                                }
                                else
                                        prl("Print_MJ(tempb,tempa);");
@@ -776,6 +804,11 @@ public class mjc2wsl{
                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");
+               System.out.println();
+               System.out.print(genLocalsAsArray?'*':' ');
+               System.out.println(" --genLocalsAsArray generate local variables as an array");
+               System.out.print(!genLocalsAsArray?'*':' ');
+               System.out.println(" --genLocalsSeparate generate local variables as separate entities");
        }
 
        public void printHelpHelp() {
@@ -790,7 +823,7 @@ public class mjc2wsl{
        }
 
        public void printVersion() {
-               System.out.println("MicroJava bytecode to WSL converter. v " + getVersion()
+               System.out.println("MicroJava bytecode to WSL converter " + getVersion()
                                + ", by Doni Pracner");
        }
        
@@ -806,7 +839,7 @@ public class mjc2wsl{
                        printHelp();
                } else {
                        int i = 0;
-                       while (i < args.length && args[i].charAt(0) == '-') {
+                       while (i < args.length && args[i].length() > 0 && args[i].charAt(0) == '-') {
                                if (args[i].compareTo("-h") == 0) {
                                        printHelp();
                                        return;
@@ -858,6 +891,12 @@ public class mjc2wsl{
                                        genLocalVars = true;
                                } else if (args[i].compareToIgnoreCase("--genGlobalVars") == 0) {
                                        genLocalVars = false;
+                               } else if (args[i].compareToIgnoreCase("--genLocalsAsArray") == 0) {
+                                       genLocalsAsArray = true;
+                               } else if (args[i].compareToIgnoreCase("--genLocalsSeparate") == 0) {
+                                       genLocalsAsArray = false;
+                               } else {
+                                       System.err.println("unknown option: "+args[i]);
                                }
                                i++;
                        }
@@ -868,7 +907,7 @@ public class mjc2wsl{
                        }
 
                        Path p = FileSystems.getDefault().getPath(args[i]);
-                       if (!Files.exists(p)){
+                       if (!Files.isRegularFile(p)){
                                System.err.println("input file does not exist");
                                System.exit(1);
                        }
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner