X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=mjc2wsl.git;a=blobdiff_plain;f=src%2Fmjc2wsl.java;h=c482ccfeaa9c294df9d3a1dc75485686dc93be14;hp=970089e1674a9360aad8d7627b2e2c139718e0b4;hb=b7f0a8052e0644d10da8ecba9b3fc413a793d263;hpb=34d91ce4e669ada036d2d8dfb969218465c9f7e2 diff --git a/src/mjc2wsl.java b/src/mjc2wsl.java index 970089e..c482ccf 100644 --- a/src/mjc2wsl.java +++ b/src/mjc2wsl.java @@ -11,15 +11,41 @@ import java.util.*; */ public class mjc2wsl{ public static String versionN = "0.1.2"; - - //regular comments from the original file - //OC when original code is inserted in the file, next to the translations - //SPEC special messages from the translator - //ERR error messages from the translator - public static final char C_REG = ' ', C_OC = '#', C_SPEC = '&', C_ERR = '!'; - /** instruction code */ - public static final int + public static final int M_ERR = 2, M_WAR = 1; + + private int printLevel = 0; + + 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 = ' '; + /** + * Constant used for marking when original code is inserted in the file, + * next to the translations + */ + public static final char C_OC = '#'; + /** Constant used for marking special messages from the translator */ + public static final char C_SPEC = '&'; + /** Constant used for marking error messages from the translator */ + public static final char C_ERR = '!'; + + /** instruction code in MicroJava bytecode. */ + public static final int load = 1, load_0 = 2, load_1 = 3, @@ -179,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++) @@ -243,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; } @@ -254,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; } @@ -290,6 +352,7 @@ public class mjc2wsl{ } default: prl(createComment("unknown op error: " + op, C_ERR)); + message("unknown op error: "+ op, M_ERR); break; } @@ -373,10 +436,12 @@ public class mjc2wsl{ } if (f.exists()) { Calendar now = Calendar.getInstance(); + printLevel=10; convertFile(f); 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");