gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - more detail to the messages system, add command line options and the help...
[mjc2wsl.git] / src / mjc2wsl.java
index 4a30186..574adcc 100644 (file)
@@ -12,6 +12,26 @@ import java.util.*;
 public class mjc2wsl{
        public static String versionN = "0.1.2";
 
+       public static final int M_ERR = 2, M_WAR = 1, M_DEB = 0;
+       
+       private int printLevel = M_ERR;
+       
+       private int[] messageCounters = new int[M_ERR+1];
+       
+       private void message(String mes, int level){
+                       if (level>=printLevel)
+                                       System.out.println(mes);
+                       messageCounters[level]++;
+       }
+       
+       private void printMessageCounters(){
+                       printMessageCounters(System.out);
+       }
+       
+       private void printMessageCounters(PrintStream out){
+                       out.println("total errors:"+messageCounters[M_ERR]+" warnings:"+messageCounters[M_WAR]);
+       }
+       
        /** Constant used for marking a regular comment from the original file */
        public static final char C_REG = ' ';
        /**
@@ -185,8 +205,20 @@ public class mjc2wsl{
        private String getTop() {
                return cmdFromEStack("tempa");
        }
+       
+       private String getRelationFor(int opcode) throws Exception {
+                       switch (opcode) {
+                                       case jeq: return "=";
+                                       case jne: return "<>";
+                                       case jlt: return "<";
+                                       case jle: return "<=";
+                                       case jgt: return ">";
+                                       case jge: return ">=";
+                       }
+                       throw new Exception("Wrong opcode for a relation");
+       }
 
-       public void convertStream(InputStream ins){
+       public void convertStream(InputStream ins) throws Exception{
                mainIn = ins;
                //skip start TODO make better
                for (int i = 0; i < 14; i++)
@@ -249,7 +281,8 @@ public class mjc2wsl{
                        case jgt:
                        case jge: {
                                prl(getTopTwo());
-                               prl("IF tempb >= tempa THEN CALL a" + (counter + get2())
+                               prl("IF tempb "+ getRelationFor(op)
+                                               +" tempa THEN CALL a" + (counter + get2())
                                                + " FI;");
                                break;
                        }
@@ -260,25 +293,48 @@ public class mjc2wsl{
                                prl(cmdToEStack("tempres"));
                                break;
                        }
+                       case sub: {
+                               prl(getTopTwo());
+                               prl("tempres := tempb - tempa;");
+                               prl(cmdToEStack("tempres"));
+                               break;
+                       }
+                       case mul: {
+                               prl(getTopTwo());
+                               prl("tempres := tempb * tempa;");
+                               prl(cmdToEStack("tempres"));
+                               break;
+                       }
                        case div: {
                                prl(getTopTwo());
-                               prl("tempres := tempb / tempa;");
+                               prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
+                               prl("tempres := tempb DIV tempa;");
+                               prl(cmdToEStack("tempres"));
+                               break;
+                       }
+                       case rem: {
+                               prl(getTopTwo());
+                               prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
+                               prl("tempres := tempb MOD tempa;");
                                prl(cmdToEStack("tempres"));
                                break;
                        }
 
                        case enter: {
                                prl(createComment("enter not fully procesed yet"));
+                               message("enter not fully procesed yet", M_WAR);
                                get();
                                get();
                                break;
                        }
                        case return_: {
                                prl(createComment("return not fully procesed yet"));
+                               message("return not fully procesed yet", M_WAR);
                                break;
                        }
                        case exit: {
                                prl(createComment("exit not fully procesed yet"));
+                               message("exit not fully procesed yet", M_WAR);
                                break;
                        }
 
@@ -296,6 +352,7 @@ public class mjc2wsl{
                        }
                        default:
                                prl(createComment("unknown op error: " + op, C_ERR));
+                               message("unknown op error: "+ op, M_ERR);
                                break;
                        }
 
@@ -321,7 +378,10 @@ public class mjc2wsl{
                                + ", by Doni Pracner");
                System.out.println("usage:\n\t {options} mjc2wsl  filename [outfile]");
                System.out.println("options:\n\t--screen print output to screen");
-               System.out.println("\t-o --oc include original code in comments");
+               System.out.println("\t-o --oc[+-] include original code in comments");
+               System.out.println("\t-v verbose, print warning messages");
+               System.out.println("\t-q don't print even the error messages");
+               System.out.println("\t-d print detailed debug messages");
        }
        
        public String makeDefaultOutName(String inname){
@@ -350,6 +410,12 @@ public class mjc2wsl{
                                                originalInComments = true;
                                } else if (args[i].startsWith("--screen")) {
                                        out = new PrintWriter(System.out);
+                               } else if (args[i].compareTo("-d") == 0) {
+                                       printLevel = M_DEB;//print debug info
+                               } else if (args[i].compareTo("-v") == 0) {
+                                       printLevel = M_WAR;//print warnings
+                               } else if (args[i].compareTo("-q") == 0) {
+                                       printLevel = M_ERR+1;//no printing
                                }
                                i++;
                        }
@@ -383,6 +449,7 @@ public class mjc2wsl{
                                long mili = Calendar.getInstance().getTimeInMillis()
                                                - now.getTimeInMillis();
                                System.out.println("conversion time:" + mili + " ms");
+                               printMessageCounters();
                                out.close();
                        } else
                                System.out.println("file does not exist");
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner